4

It is said that DFS can't be used to find the shortest path in the unweighted graph. I have read multiple post and blogs but not get satisfied as a little modification in DFS can make it possible.

I think if we use Modified DFS in this way, then we can find the shortest distances from the source.

  1. Initialise a array of distances from root with infinity and distance of root from itself as 0.
  2. While traversing, we keep track of no. of edges. On moving forward increment no. of edges and while back track decrement no. of edges. And each time check if(dist(v) > dist(u) + 1 ) then dist(v) = dist(u) + 1.

In this way we can find the shortest distances from the root using DFS. And in this way, we can find it in O(V+E) instead of O(ElogV) by Dijkstra.

If I am wrong at some point. Please tell me.

coderAJ
  • 310
  • 5
  • 17
  • See [this](https://cs.stackexchange.com/questions/4914/why-cant-dfs-be-used-to-find-shortest-paths-in-unweighted-graphs) – Cid Jan 15 '19 at 12:50
  • I have read this. But a little Modified DFS can make it possible. Do u find any mistake in my method ? – coderAJ Jan 15 '19 at 12:55

3 Answers3

5

Yes, if the DFS algorithm is modified in the way you mentioned, it can be used to find the shortest paths from a root in an unweighted graph. The problem is that in modifying the algorithm you have fundamentally changed what it is.

It may seem like I am exaggerating as the change looks minor superficially but it changes it more than you might think.

Consider a graph with n nodes numbered 1 to n. Let there be an edge between each k and k + 1. Also, let 1 be connected to every node.

Since DFS can pick adjacent neighbors in any order, let's also assume that this algorithm always picks them in increasing numerical order.

Now try running algorithm in your head or your computer with root 1. First the algorithm will reach n in n-1 steps using edges between 1-2, 2-3 and so on. Then after backtracking, the algorithm moves on to the second neighbor of 1, namely 3. This time there will be n-2 steps. The same process will repeat until the algorithm finally sees 1-n. The algorithm will need O(n ^ 2) rather than O(n) steps to finish. Remember that V = n & E = 2 * n - 3. So it is not O(V + E).

Actually, the algorithm you have described will always finish in O(V^2) on unweighted graphs. I will leave the proof of this claim as an exercise for the reader.

O(V^2) is not that bad. Especially if a graph is dense. But since BFS already provides an answer in O(V + E), nobody uses DFS for shortest distance calculation.

merlyn
  • 2,273
  • 1
  • 19
  • 26
  • can you please give some hints on how to prove that this algorithm runs in O(V^2) time ?, as far as I understand it here we are only backtracking if dist(v) < dist(u) + 1 so I am unable to see how this translates to an O(V^2) algorithm, In the normal dfs algorithm we visit each node only once and explore each edge only once so its easy to see why normal dfs translates to O(V+E) – Freelancer Jan 26 '20 at 14:12
  • In this [question](https://cs.stackexchange.com/questions/80797/shortest-path-using-dfs-on-weighted-graphs) also I believe the same thing is being talked about(for weighted graphs however),from seeing the pseudo-code it is obvious that for each call to dfs(u) we will explore all edges going from "u" if the condition dist(u)+1 – Freelancer Jan 26 '20 at 20:42
2

In an unweighted graph, you can use a breadth-first search (not DFS) to find shortest paths in O(E) time.

In fact, if all edges have the same weight, then Dijkstra's algorithm and breadth-first search are pretty much equivalent -- reduceKey() is never called, and the priority queue can be replaced with a FIFO queue, since newly added vertices never have smaller weight than previously-added ones.

Your modification to DFS does not work, because once you visit a vertex, you will not examine its children again, even if its weight changes. You will get the wrong answer for this graph if you follow S->A before S->B

S---->A---->C---->D---->E---->T
 \               /
  ------->B-----/
Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87
1

The way Depth First Search on graphs is defined, it only visits each node once. When it encounters a node that was visited before, it backtracks.

So assume you have a triangle with nodes A, B, C and you want to find the shortest path from A to B. One possibility of a DFS traversal is A -> C -> B and you are done. This however is not the shortest path.

SaiBot
  • 3,595
  • 1
  • 13
  • 19
  • You are Rite in one way. What to say about Modified DFS in the way I explained ? – coderAJ Jan 15 '19 at 12:50
  • @coderAJ yes you can adapt DFS to support shortest-path search, but then it is not DFS anymore. Your adaptation is missing one important part: When to backtrack. I assume you want to backtrack if dist(v) < dist(u) + 1. However, if you do this the algorithm is very inefficient, since you may (and will) explore paths that are much longer than the shortest path. In contrast BFS will only explore paths that are at most as long as the shortest path. – SaiBot Jan 15 '19 at 13:15