Home > Enterprise >  Is connected graph a requirement for Dijkstra algorithm?
Is connected graph a requirement for Dijkstra algorithm?

Time:10-05

Apart from the graph having non-negative weights, does Dijkstra algorithm require connectedness? E.g. Would dijkstra algorithm work for a graph that is disconnected, where 3 vertices are connected in a component and 2 other vertices are in another component?

CodePudding user response:

Here is Dijkstra's algorithm from Wikipedia:

  1. Mark all nodes unvisited. Create a set of all the unvisited nodes called the unvisited set.
  2. Assign to every node a tentative distance value: set it to zero for our initial node and to infinity for all other nodes. During the run of the algorithm, the tentative distance of a node v is the length of the shortest path discovered so far between the node v and the starting node. Since initially no path is known to any other vertex than the source itself (which is a path of length zero), all other tentative distances are initially set to infinity. Set the initial node as current.[15]
  3. For the current node, consider all of its unvisited neighbors and calculate their tentative distances through the current node. Compare the newly calculated tentative distance to the one currently assigned to the neighbor and assign it the smaller one. For example, if the current node A is marked with a distance of 6, and the edge connecting it with a neighbor B has length 2, then the distance to B through A will be 6 2 = 8. If B was previously marked with a distance greater than 8 then change it to 8. Otherwise, the current value will be kept.
  4. When we are done considering all of the unvisited neighbors of the current node, mark the current node as visited and remove it from the unvisited set. A visited node will never be checked again (this is valid and optimal in connection with the behavior in step 6.: that the next nodes to visit will always be in the order of 'smallest distance from initial node first' so any visits after would have a greater distance).
  5. If the destination node has been marked visited (when planning a route between two specific nodes) or if the smallest tentative distance among the nodes in the unvisited set is infinity (when planning a complete traversal; occurs when there is no connection between the initial node and remaining unvisited nodes), then stop. The algorithm has finished.
  6. Otherwise, select the unvisited node that is marked with the smallest tentative distance, set it as the new current node, and go back to step 3.

Note the part I emphasized. Laid out this way, if there is no path between the initial node and the destination node, the algorithm still stops once all nodes in the component of the initial node are visited.

So no, the graph being connected is not a requirement. The algorithm will tell you whether the path exists.

CodePudding user response:

According to the Djikstra's algorithm given in psuedocode on Wikipedia (copied below for convenience), the disconnected vertices distance from the source would remain as INFINITY. The answer to your question is no, Dijkstra's algorithm still returns a correct result even if the graph is disconnected.

 1  function Dijkstra(Graph, source):
 2      
 3      for each vertex v in Graph.Vertices:
 4          dist[v] ← INFINITY
 5          prev[v] ← UNDEFINED
 6          add v to Q
 7      dist[source] ← 0
 8      
 9      while Q is not empty:
10          u ← vertex in Q with min dist[u]
11          remove u from Q
12          
13          for each neighbor v of u still in Q:
14              alt ← dist[u]   Graph.Edges(u, v)
15              if alt < dist[v]:
16                  dist[v] ← alt
17                  prev[v] ← u
18
19      return dist[], prev[]
  • Related