|Keep It Simple, Stupid|
Re^2: OT(ish) - Best Search Algorithmby vrk (Chaplain)
|on Oct 15, 2007 at 14:34 UTC||Need Help??|
I agree, definitely cycle detection. Dijkstra's Algorithm and A* are for finding the shortest path from any given node to all nodes in the graph. However, let's look at the problem description more closely.
You want to find cycles in a connected graph, to put it in the terminology that will give you most hits when googling. So you have here a cycle of four nodes (assuming friendship is mutual, i.e. the graph is undirected):
And this would be a cycle you want, provided that there no edges between A and C or B and D:
So, you want to find the shortest cycle that includes a given node, if such a cycle exists. (Sorry for being verbose.)
Detecting cycles is easier than finding the shortest one. Since a cycle is simply a path that begins from the same node where it ends, you could just find the shortest paths to all nodes from a given node, say A, then for all nodes reachable from A, find a path back to A that does not include any of the nodes on the shortest paths. If you find such a path for node B, you have a cycle from A to B and back to A.
For example, supposing you have a pathfinding algorithm:
Caveat: I don't know much about graph theory, so it's up to you to figure out or prove if this really finds cycles and if it does, if those are the shortest ones. The running time of this naive approach is probably also horrible. But you should be able to get some results.
Whether that is the shortest is harder to find out.