176k views
4 votes
Suppose we are given an undirected graph G=(V,E), and we identify two nodes v and w in G. Give an algorithm that computes the number of shortest v - w paths in G. (The algorithm should not list all the paths; just the number suffices.) The running time of your algorithm should be O(m+n) for a graph with n nodes and m edges. Hint: Slightly modify the BFS algorithm to also account for the number of possible paths to each node, but do it in an efficient way that does not require you to add any (asymptotic) runtime.

User FHannes
by
7.6k points

2 Answers

2 votes

Final answer:

An algorithm to compute the number of shortest paths between two nodes in an undirected graph can be created by modifying the Breadth-First Search algorithm to track the distance and the number of shortest paths to each node. The run time is O(m+n).

Step-by-step explanation:

The designing an algorithm to compute the number of shortest paths between two nodes in an undirected graph, specifically, from node v to node w. To answer this, we can modify the Breadth-First Search (BFS) algorithm. The modification involves keeping track of two additional data for each node: the distance from the start node v, and the number of shortest paths that reach the node.

Initialize the number of paths to v as 1 and the rest as 0. Perform a BFS starting from node v. When a node is explored for the first time, update its distance to be one more than the distance of its predecessor. If an unvisited neighbor is discovered and it contributes to a shortest path, increase its number of paths by the number of paths to the current node.

If a neighbor is discovered that has been visited and the current path is also a shortest path (equal distance as recorded), increment the neighbor's path count by the current node's path count. Continue the search until node w is reached, and the result will be the number of shortest paths to node w. The run time remains O(m+n) since we are simply augmenting the standard BFS with additional bookkeeping that does not increase the asymptotic complexity.

User Pretty
by
7.3k points
2 votes

Final answer:

To calculate the number of shortest paths between two nodes in an undirected graph, modify the BFS algorithm to track distances and path counts without increasing its O(m+n) time complexity.

Step-by-step explanation:

To compute the number of shortest paths between two nodes v and w in an undirected graph G=(V,E), we can modify the Breadth-First Search (BFS) algorithm. Here's a concise version of such an algorithm:

  • Initialize a queue and enqueue the starting node v with a path count of 1.
  • Maintain an array of distances, initialized to infinity for all vertices except v which is set to 0, to track the shortest distances from v.
  • Maintain an array of path counts, initialized to 0 for all vertices except v which is set to 1, to track the number of shortest paths to each node.
  • While the queue is not empty, dequeue the next node. For each of its neighbors, if the neighbor has not been visited (distance is infinity), set its distance and update its path count. If the neighbor has been visited and the current path to it is also the shortest, increment the path count by the path count of the current node.
  • Stop when w is reached or the queue is empty.
  • The number of shortest paths to w is the value in the path count array for w.

This algorithm runs in O(m+n) time since it only processes each node and edge once. It effectively counts all the shortest paths from v to w without the need to list them.

User David Scarlett
by
7.6k points