Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bidirectional spanning tree

I came across this question from interviewstreet.com

Machines have once again attacked the kingdom of Xions. The kingdom of Xions has N cities and N-1 bidirectional roads. The road network is such that there is a unique path between any pair of cities.

Morpheus has the news that K Machines are planning to destroy the whole kingdom. These Machines are initially living in K different cities of the kingdom and anytime from now they can plan and launch an attack. So he has asked Neo to destroy some of the roads to disrupt the connection among Machines i.e after destroying those roads there should not be any path between any two Machines.

Since the attack can be at any time from now, Neo has to do this task as fast as possible. Each road in the kingdom takes certain time to get destroyed and they can be destroyed only one at a time.

You need to write a program that tells Neo the minimum amount of time he will require to disrupt the connection among machines.

Sample Input First line of the input contains two, space-separated integers, N and K. Cities are numbered 0 to N-1. Then follow N-1 lines, each containing three, space-separated integers, x y z, which means there is a bidirectional road connecting city x and city y, and to destroy this road it takes z units of time. Then follow K lines each containing an integer. Ith integer is the id of city in which ith Machine is currently located.

Output Format Print in a single line the minimum time required to disrupt the connection among Machines.

Sample Input

5 3
2 1 8
1 0 5
2 4 5
1 3 4
2
4
0

Sample Output

10

Explanation Neo can destroy the road connecting city 2 and city 4 of weight 5 , and the road connecting city 0 and city 1 of weight 5. As only one road can be destroyed at a time, the total minimum time taken is 10 units of time. After destroying these roads none of the Machines can reach other Machine via any path.

Constraints

2 <= N <= 100,000
2 <= K <= N
1 <= time to destroy a road <= 1000,000

Can someone give idea how to approach the solution.

like image 468
user1374210 Avatar asked May 04 '12 05:05

user1374210


People also ask

What is a bidirectional approach?

We can consider bidirectional approach when- Both initial and goal states are unique and completely defined. The branching factor is exactly the same in both directions.

Which algorithm performs bidirectional?

It is implemented using the Breadth First Search (BFS) Algorithm. (If you don't know what BFS is refer to this article first). BFS is run simultaneously on two vertices - the start and the end vertex. One single BFS tree is now replaced by two sub trees, and the search is terminated when the two trees intersect.

What is bidirectional search with example?

A bidirectional search is a searching technique that runs two way. It works with two who searches that run simultaneously, first one from source too goal and the other one from goal to source in a backward direction. In in an optimal state, both the searches will meet in the middle off the data structure.

What is the major disadvantage of bidirectional search?

Disadvantages. Below are the disadvantages: The fundamental issue with bidirectional search is that the user should be aware of the goal state to use bidirectional search and thereby to decrease its use cases drastically.


2 Answers

Tree

The kingdom has N cities, N-1 edges and it's fully connected, therefore our kingdom is tree (in graph theory). At this picture you can see tree representation of your input graph in which Machines are represented by red vertices.

By the way you should consider all paths from the root vertex to all leaf nodes. So in every path you would have several red nodes and during removing edges you should take in account only neighboring red nodes. For example in path 0-10 there are two meaningfull pairs - (0,3) and (3,10). And you must remove exactly one node (not less, not more) from each path which connected vertices in pairs.

I hope this advice was helpful.

like image 130
hsestupin Avatar answered Sep 29 '22 03:09

hsestupin


All the three answers will lead to correct solution but you can not achieve the solution within the time limit provided by interviewstreet.com. You have to think of some simple approach to solve this problem successfully.

HINT: start from the node where machine is present.

like image 29
Anantha Krishnan Avatar answered Sep 29 '22 03:09

Anantha Krishnan