I was revising single source shortest path algorithms and in the video, the teacher mentions that BFS/DFS can't be used directly for finding shortest paths in a weighted graph (I guess everyone knows this already) and said to work out the reason on your own.
I was wondering the exact reason/explanation as to why it can't be used for weighted graphs. Is it due to the weights of the edges or anything else ? Can someone explain me as I feel a little confused.
Although this is true, but you could use
BFS/DFS
in weighted graphs, with a little change in the graph, if your graph's weights are positive integers you can replace an edge with weightn
withn
edges with weight 1 withn-1
middle nodes. Something like this:will be:
And don't consider these middle nodes (like M1,M2,M3 ) in your final BFS/DFS results.
This algorithm complexity is O(V * M) and M is the maximum weight of our edges, if we know that in our particular graphs
M<log V
this algorithm could be considerd, but in general this algorithm may not have a such a good performance.Consider a graph like this:
The shortest path from A to B is via C (with a total weight of 2). A normal BFS will take the path directly from A to B, marking B as seen, and A to C, marking C as seen.
At the next stage, propagating from C, B is already marked as seen, so the path from C to B will not be considered as a potential shorter path, and the BFS will tell you that the shortest path from A to B has a weight of 3.
You can use Dijkstra's algorithm instead of BFS to find the shortest path on a weighted graph. Functionally, the algorithm is very similar to BFS, and can be written in a similar way to BFS. The only thing that changes is the order in which you consider the nodes.
For example, in the above graph, starting at A, a BFS will process A --> B, then A --> C, and stop there because all nodes have been seen.
On the other hand, Dijkstra's algorithm will operate as follows:
Note that the difference lies simply in the order in which edges are inspected. A BFS will consider all edges from a single node before moving on to other nodes, while Dijkstra's algorithm will always consider the lowest-weight unseen edge, from the set of edges connected to all nodes that have been seen so far. It sounds confusing, but the pseudocode is very simple:
This GIF from Wikipedia provides a good visualisation of what happens:
Notice that this looks very similar to BFS code, the only real difference is the use of a heap, sorted by distance to the node, instead of a regular queue data structure.