Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^2: OT(ish) - Best Search Algorithm

by vrk (Chaplain)
on Oct 15, 2007 at 14:34 UTC ( #644949=note: print w/replies, xml ) Need Help??

in reply to Re: OT(ish) - Best Search Algorithm
in thread OT(ish) - Best Search Algorithm

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.

For example, if I am "A", and I have two friends "B" and "D", and "C" is not a friend of mine, but is a friend of "B" and "D", then a valid circle would be: A->B->C->D->A (as an extra note, this circle would not be valid if "B" and "D" are friends, since "A" would then be superfluous to the circle).

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):

A --- B | | | | | | D --- C

And this would be a cycle you want, provided that there no edges between A and C or B and D:

A----B | / | | / | |/ | D----C

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:

for my $node ($graph->nodes) { my @paths = $graph->find_shortest_paths_starting_from($node); # Now, suppose @paths is an array containing arrays of # nodes that are the actual nodes on the shortest paths, # with the first being $node and the last being the end of # the path. For instance, [ $node, $other_node, ..., $end_node ] # for the shortest path from $node to $end_node. # Next, for each endpoint, try to find a path back, but # remove the nodes from consideration that are on the # known shortest path. This is to prevent the algorithm # from re-discovering the shortest path, and will ensure # that the two possible found paths are mutually exclusive, # i.e. do not contain same nodes. (Otherwise you would have # an even shorter cycle... could this be used for our benefit?) for my $path (@paths) { my $new_graph = $graph->copy; # Slice away the first and last nodes, as they are still of # interest to us. $new_graph->remove_nodes(@$path[1 .. $#{@$path}-1]); my @paths_back = $new_graph->find_shortest_paths_starting_from($pa +th->[-1]); for my $path_back (@paths_back) { if ($path_back->[-1] eq $node) { print "We have a cycle involving $node!\n"; } } } }

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.

print "Just Another Perl Adept\n";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://644949]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2018-03-23 21:23 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (296 votes). Check out past polls.