简体   繁体   中英

How to find maximum edge of path for all pairs of vertices of mst

Let as assume that we have a known minimum spanning tree.

Our task is to find the maximum edge for the path that exists between each pair of vertices.

To give an example,

We have the following minimum spanning tree:

    1---10---2
       \     
       5\   
           \ 
    4---4---3

Between vertex 1 and 2 , we have an edge with cost 10. Between vertex 1 and 3 , we have an edge with cost 5. Between vertex 3 and 4 , we have an edge with cost 4.

Maximum edge for each path:

path 1-2 : It contains only the edge with cost 10. So the answer is 10.

path 1-3: It contains only the edge with cost 5. So the answer is 5.

path 1-4: To get from vertex 1 to vertex 4 , the path is 1-3-4. It contains edge with cost 5 and edge with cost 4. So the answer is 5.

path 2-3: We need to follow the path 2-1-3. Maximum edge is 10.

path 2-4: We need to follow the path 2-1-3-4. Maximum edge 10.

path 3-4: Maximum edge 4.

So the final answer would be:

    X 10  5  5
    X  X 10 10
    X  X  X  4
    X  X  X  X

Which one is the most suitable algorithm for this task?

So far , I have considered the possibility of using DFS for each pair of vertices. However , since we have O(V^2) pairs of vertices , total complexity would be O(V^3), which does not look good.

For each vertex, you can do DFS to find the matrix entries for the row/column corresponding to that vertex. Something like

fill-entries-DFS(root, maxEdgeRootToV, v):
    set the entry for (root, v) to maxEdgeRootToV
    for each child w of v:
        fill-entries-DFS(root, max(maxEdgeRootToV, edgeWeight(v, w)), w)

for each vertex v:
    fill-entries-DFS(v, -infinity, v)

The running time is O(V^2), the asymptotic optimum.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM