4

I have an undirected, unweighted graph, which doesn't have to be planar. I also have a subset of graph's nodes (true subset) and I need to find a node not belonging to the subset, with minimum sum of distances to all nodes in the subset.

So far, I have implemented breath-first search starting from each node in the subset, and the intersection that occurs first is the node I am looking for. Unfortunately, it is running too slow since the graph contains a large number of nodes.

ekad
  • 14,436
  • 26
  • 44
  • 46
Nikola
  • 41
  • 2
  • whats too slow? What language are you using? what would you like advise on? is it the speed aspect or the algorithm your using? – Glycerine May 20 '10 at 13:31

1 Answers1

1

An all-pair shortest path algorithm allows you to find the distance of all nodes to each other in O(V^3) time, see Floyd-warshall. Then summing afterwards will at least be quadratic and I believe worst case cubic as well. It's a very straightforward and not terribly fast way of doing it, but it sounds like it might be an order of magnitude faster than what you're doing right now.

wds
  • 31,873
  • 11
  • 59
  • 84
  • Hi, Thanks for the advice. In the mean time, I have realized that what I have suggested is not correct and needn't result in an optimal node. Floyd-Warshall was what I intended to avoid because of the time complexity, but it seems that it is the only correct way. Thank you, Nikola – Nikola May 24 '10 at 14:52