Just another Perl shrine PerlMonks

Comment on

 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.

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.

--

In reply to Re^2: OT(ish) - Best Search Algorithm by vrk
in thread OT(ish) - Best Search Algorithm by Melly

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2018-10-20 08:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I need money for a bigger acquisition, I usually ...

Results (112 votes). Check out past polls.

Notices?