Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the shortest path between any two nodes belonging to two disjoint subsets of a graph

There is an undirected graph in which every node is assigned some color. I have to find the shortest path from any blue colored node to any red colored node. (Other colors may also exist in the graph and although it doesnt matter but it is not known how many colors are there.) How can I do it in polynomial time?

like image 741
anirudh Avatar asked Mar 14 '12 19:03

anirudh


1 Answers

As a hint, add two new nodes to the graph- call them s and t. Connect s to each blue node with an edge of cost 0 and each red node to t with an edge of cost 0. Then find the shortest path from s to t.

Hope this helps!

like image 94
templatetypedef Avatar answered Nov 15 '22 06:11

templatetypedef