简体   繁体   中英

I modified BFS to find shortest path in weighted undirected graph instead using Dijkstra's algo and it worked

To find shortest path in undirected weighted graph I was comparing BFS and dijkstra's algo to understand why we need priority queue.
I wrote some code modifying BFS to find the shortest path to all nodes in a given graph.
Problem link:- https://practice.geeksforgeeks.org/problems/implementing-dijkstra-set-1-adjacency-matrix/1#

The below code got accepted on GeeksForGeeks that I wrote instead of dijkstra algo:-

 vector <int> dijkstra(int vertices, vector<vector<int>> graph[], int src)
  {
   // modified bfs

  vector<int> dist(vertices + 1,INT_MAX);
    queue<int> nodes;
    nodes.push(src);
    dist[src] = 0;
    while(!nodes.empty()){
        int curNode = nodes.front();
        nodes.pop();
        for(auto adjNode : graph[curNode]){
            if(dist[adjNode[0]] > dist[curNode] + adjNode[1] ){
                dist[adjNode[0]] = dist[curNode] + adjNode[1];
                nodes.push(adjNode[0]);
            }
        }
    }
    return dist;
}
 

Ques:- Although it got accepted at GeeksForGeeks, I was wondering maybe it is wrong as GeeksForGeeks maybe having a limited number of test cases?

Ques:- Or if it is a correct method, then what is the time complexity? (wondering maybe because of more time complexity than dijkstra algo above approach is not used)

The algorithm you wrote is a variant of Bellman-Ford Algorithm .

Shortest_Path_Faster_Algorithm is an improvement of the Bellman–Ford algorithm(as well as yours). The only difference between SPFA and your algorithm is that SPFA checks if the vertex is already in queue before pushing it. But its worst-case time complexity is still O(V^2).

Consider this simple case, every time you visit a new vertex, you will update the long chain from that vertex to vertex#2.

       30
    ┌───────2
    │       │ 1
    │  20   │
    ├───────3
    │       │ 1
    │  15   │
1───┼───────4
    │       │ 1
    │  12   │
    ├───────5
    │       │ 1
    │  10   │
    └───────6

There are many other optimized variants of SPFA but most of them have worse worst-case time complexity than Dijkstra's algorithm. A simple random weighted grid-shap graph can make most of them runing much slower than Dijkstra's algorithm.

Update :

A simple comparison between SPFA and Dijkstra's algorithm on grid-shap graph( live demo ):

dijkstra  27ms spfa  216ms    (V=300*300 E~=3V on https://godbolt.org/z/b8qbWdbEP)
dijkstra  12ms spfa   87ms    (V=300*300 E~=3V on my computer)
dijkstra 152ms spfa 4819ms    (V=1000*1000 E~=3V on my computer)

Update2 :

Modified the generator( live demo ) to use a fixed small weight for vertical edges. SPFA become much much slower and this is more intuitive to estimate its time complexity.

dijkstra  12ms spfa   393ms  (V=200*200 on https://godbolt.org/z/hKnMqPvMM)
dijkstra   7ms spfa   192ms  (V=200*200 on my computer)
dijkstra  15ms spfa   653ms  (V=300*300 on my computer)
dijkstra 187ms spfa 40351ms  (V=1000*1000 on my computer)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM