1

I am trying to solve an algorithm challenge about graphs, which I have managed to break down to the following: Given an undirected spanning tree, find the 2 leaves such that the cost between them is minimal.

Now I know of the Floyd Warshall algorithm that can find all-pair shortest paths with time complexity O(N^3) and space complexity O(N^2). The input of the problem is N = 10^5 so O(N^3) and O(N^2) are too much.

Is there a way to optimize space and time complexity for this problem?

  • 1
    for "minimum" cost between 2 vertices in an undirected minimum spanning tree, are the 2 vertices fixed? or are you trying to find 2 vertices with min cost? – Petar Petrovic Mar 07 '17 at 11:07
  • 1
    As a minimum spanning tree is cycle-free, the path between any two nodes (without repetition of nodes) is uniquely determined. – Codor Mar 07 '17 at 12:34
  • @Codor , i am going forward and elaborate what you recommend as answer. – Amit Kumar Mar 07 '17 at 13:21
  • @PetarPetrovic Maybe my question was not clear enough. I meant find 2 (out of N) vertices such that the cost between them is minimal –  Mar 07 '17 at 14:57
  • Any explanation, on why my answer is downvoted .. :/ – Amit Kumar Mar 07 '17 at 17:06
  • @goaty if u just need a pair of vertices, with minimum cost/distance, then the nodes which are connected by least weighted edge will be the answer. – Amit Kumar Mar 07 '17 at 17:08
  • @AmitKumar Sorry my mistake again. The requirement is about finding 2 LEAVES (i.e. among nodes that only have 1 connection) –  Mar 07 '17 at 17:32
  • @goaty, then my answer that i have written works, u just need to find all leaves nodes and run them as (a,b) in my algorithm – Amit Kumar Mar 07 '17 at 17:56

1 Answers1

3

As @Codor said, elaborating on that, in a MST there is only one unique path b/w any pair of nodes, and same will be the shortest path. In order to calculate shortest path b/w all pairs. You can choose to follow this algorithm.

  • You can basically choose find the root of the MST by constantly removing leaf nodes till only one or two nodes are left .

    Complexity : centre node in a tree this can be achieved in O(V) i.e linear time

  • Choose one of them as root. Calculate distance of all the other nodes in respect to the root node using Breadth First Search(BFS).

    Complexity : O(V+E) ~ O(V) in case of tree

  • Now you can find distance b/w any pair of nodes call it a,b. Find its least common ancestor(lcp). Then there are two case if

    1. lcp(a,b) = r (root of the tree). dis(a,b) = dis[a] + dis[b]
    2. lcp(a,b) = c ( which is not the root node) dis(a,b) = dis[a] + dis[b] - 2 * dis[c]

where dis(x,y) = distance b/w node x,y and dis[x] distance of node x from root node If implemented using Ranked Union Find

Complexity : O(h) , where h is height of the tree per pair of (a,b). h = X/2, where X is the diameter of the tree. So total complexity depends on the no. of leaf node pairs.

Community
  • 1
  • 1
Amit Kumar
  • 1,477
  • 1
  • 16
  • 27