1

I have weighted undirected graph. I need to find spanning tree with minimal possible cost, so that distance between point A and B will be as low as possible. For example, I have this graph: graph. Minimal distance between A and B is 2. Minimal spanning tree would look like this. But that would make distance between A and B = 3.

Right now I am doing this:

  1. Find distance between AB in graph, using BFS.
  2. Find all paths between AB with length from step 1 using DFS.
  3. Generate spanning tree from every path from step 2.
  4. Compare them and get minimal one.

Everything is OK until I got graph with A-B distance = 12. Second step then take too much time. Is there any faster way of doing this? Thanks.

Peter Jung
  • 23
  • 7

2 Answers2

2

The fastest/most efficient way to solve this problem is to use Dijkstra's Shortest Path algorithm. This is a greedy algorithm that has the following basic structure:

1-all nodes on the graph start "infinity" distance apart

2-start with your first node (node A in your example) and keep track of each edge weight to get from this node A to each of its neighbors.

3-Choose the shortest current edge and follow it to your next node, let's call it node C for now

4-Now, for each of C's neighbors, compare the current distance (including infinity if applicable) with the sum of A's edge to C and C's shortest edge to the current neighbor. If it is shorter than the current distance, update it to the new distance.

5-Continue this process until all nodes have been visited and you reach the node you were looking for the shortest path to (i.e. B in your example)

This is a pretty efficient way of finding the shortest path between two nodes, with a running time of O(V^2), not O(nlogn) as mentioned above. As you can see, by making this a greedy algorithm, we are constantly choosing the optimal solution based on the available local information, and therefore we will never have to go back and change our decisions.

This also eliminates the need for a BFS and a DFS like in your example. Hope this helps!

1

While your step two is correct, I think that the problem is that you are doing too many operations.

You are doing both a BFS and a DFS which is going to be very costly. Instead, what I would recommend doing is using one of several different traversal techniques that will minimize in the computational costs.

This is a common problem to find the shortest path, and one of the popular solutions is Dijkstra's algorithm. Here is an article that expounds on this topic. https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-greedy-algo-7/

In short, what this algorithm does, is it takes the starting point A, and then it generates the minimal spanning tree until point B is hit, then there is a single path in which A can get to B, and that is the shortest path.

Both this and your algorithm both run in O(nlogn), but in practice this solution can be thought of as running a single BFS instead of both a BFS and a DFS.

Camden Ko
  • 11
  • 1