If you want to find all "circles of friends," i.e., all cycles to which you are a member, then you need to use a modified depth first traversal algorithm that begins and ends with you.
It is similar to finding all acyclic s-d paths, except in your case s and d (source and destination) are the same - you.
The algorithm is fast, especially if you don't have many friends - like most perl hackers :).
# Pseudo-code
array dflabels[]
for each node I in graph
dflables[I] = 0
s := 0
d := 0 # same as "s", which is you
# as recursive
function acyclic (start_node)
if 1 > dflabels[start_node]# max num visits is 1
dflabels[start_node]++
for each i in adjacent_nodes(start_node)
acyclic(i)
end for
dflabels[start_node]-- # unlike strict dft decrement
else # indicates we've been here before
if start_node == s # check if this is YOU
print "Cycle back to me found!"
end if
end if
return
end function acyclic
# initiate
call acyclic(s)
Of course, the above pseudo-code doesn't facilitate storing the actual paths, this is easily done using input and output parameters.
Hope that helps.
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.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, 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, summary, 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?
See Writeup Formatting Tips and other pages linked from there for more info.