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:
- Find distance between AB in graph, using BFS.
- Find all paths between AB with length from step 1 using DFS.
- Generate spanning tree from every path from step 2.
- 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.