Dfs Weighted Graph
I was wondering the exact reason explanation as to why it can t be used for weighted graphs.
Dfs weighted graph. If you want to identify the shortest path you would use dijkstra algorithm. Detecting cycles in the graph. Therefore we should run dfs for the graph and verify for back edges. Depth first search dfs for undirected graphs depth first search or dfs is a way to traverse the graph initially it allows visiting vertices of the graph only but there are hundreds of algorithms for graphs which are based on dfs.
Therefore understanding the principles of depth first search is quite important to move ahead into the graph theory. Depth first search is an algorithm for traversing or searching tree or graph data structures. If we find a back edge while performing dfs in a graph then we can conclude that the graph has a cycle hence dfs is used to detect the cycles in a graph. However the resulting algorithm is no longer called dfs.
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. Detecting a cycle in a graph. There is a simple tweak to get from dfs to an algorithm that will find the shortest paths on an unweighted graph. In a weighted graph dfs graph traversal generates the shortest path tree and minimum spanning tree.
Following are implementations of simple depth first traversal. The c implementation uses adjacency list representation of graphs. A graph has a cycle if we found a back edge during dfs. Essentially you replace the stack used by dfs with a queue.
To be short performing a dfs or bfs on the graph will produce a spanning tree but neither of those algorithms takes edge weights into account. The algorithm starts at the root node selecting some arbitrary node as the root. So if you apply the dfs algorithm to a weighted graph it would be simply not consider the weight and print the output.