Advertisement
spider68

Shortest Path Algorithms

Jul 13th, 2021
1,027
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1.    
  2.     Bellman Ford's Algorithm:
  3.    
  4.    Time Complexity of Bellman Ford algorithm is relatively high O(V.E), in case E=V^2, O(V^3).
  5.    Shortest path contains at most (N-1) edges, because the shortest path couldn't have a cycle.
  6.    
  7.     V- vertex
  8.     E- Edge
  9.    
  10.     vector <int> v [V];
  11.     int dis [V];
  12.  
  13.     for(int i = 0; i < V; i++){
  14.        
  15.         v[i].clear();
  16.         dis[i] = 1e9;
  17.     }
  18.  
  19.    for(int i = 0; i < E; i++){
  20.  
  21.         scanf("%d%d%d", &from , &next , &weight);
  22.  
  23.         v[i].push_back(from);
  24.         v[i].push_back(next);
  25.         v[i].push_back(weight);
  26.    }
  27.  
  28.     dis[0] = 0;
  29.     for(int i = 1; i <= V - 1; i++){
  30.         for(int j=0;j<E;j++){
  31.            
  32.             if(dis[ v[j][0] ] + v[j][2] < dis[ v[j][1] ] ){
  33.                 dis[ v[j][1] ] = dis[ v[j][0]  ] + v[j][2];
  34.             }
  35.         }
  36.     }
  37.    
  38.    
  39.     Dijkstra's Algorithm:
  40.    
  41.    Time Complexity of Dijkstra's Algorithm is O(V^2) but with min-priority queue it drops down to O(V+ElogV) .
  42.    
  43.     #define SIZE 100000 + 1
  44.  
  45.     vector < pair < int , int > > v [SIZE];   // each vertex has all the connected vertices with the edges weights
  46.     int dist [SIZE];
  47.     bool vis [SIZE];
  48.    
  49.     void dijkstra(){
  50.                                                     // set the vertices distances as infinity
  51.         memset(vis, false , sizeof vis);            // set all vertex as unvisited
  52.         dist[1] = 0;
  53.         multiset < pair < int , int > > s;          // multiset do the job as a min-priority queue
  54.    
  55.         s.insert({0 , 1});                          // insert the source node with distance = 0
  56.    
  57.         while(!s.empty()){
  58.    
  59.             pair <int , int> p = *s.begin();        // pop the vertex with the minimum distance
  60.             s.erase(s.begin());
  61.    
  62.             int x = p.s; int wei = p.f;
  63.             if( vis[x] ) continue;                  // check if the popped vertex is visited before
  64.              vis[x] = true;
  65.    
  66.             for(int i = 0; i < v[x].size(); i++){
  67.                 int e = v[x][i].f; int w = v[x][i].s;
  68.                 if(dist[x] + w < dist[e]  ){            // check if the next vertex distance could be minimized
  69.                     dist[e] = dist[x] + w;
  70.                     s.insert({dist[e],  e} );           // insert the next vertex with the updated distance
  71.                 }
  72.             }
  73.         }
  74.     }
  75.    
  76.     However, if we have to find the shortest path between all pairs of vertices, both of the above methods would be expensive in terms of time.
  77.    
  78.    
  79.    
  80.     Floyd Warshall's Algorithm:
  81.    
  82.    Floyd\u2013Warshall's Algorithm is used to find the shortest paths between between all pairs of vertices in a graph, where each edge in the graph has a weight which is positive or negative. The biggest advantage of using this algorithm is that all the shortest distances between any  vertices could be calculated in V^3.
  83.  
  84.     for(int k = 1; k <= n; k++){
  85.         for(int i = 1; i <= n; i++){
  86.             for(int j = 1; j <= n; j++){
  87.                 dist[i][j] = min( dist[i][j], dist[i][k] + dist[k][j] );
  88.             }
  89.         }
  90.     }
  91.     Time Complexity of FloydWarshall's Algorithm is V^3, where V is the number of vertices in a graph.
  92.        
  93.    
  94.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement