<div dir="ltr"><div>Oi Blabos,<br><br></div>Não use cpan pra um problema simples, grafos são facilmente representados como Adjacency Lists ( <a href="http://en.wikipedia.org/wiki/Adjacency_list">http://en.wikipedia.org/wiki/Adjacency_list</a> ) em Perl: basicamente uma hash table onde cada vertex aponta para uma lista de vertices adjacentes.<br>
<br><div class="gmail_extra"><div class="gmail_quote"><div>Aqui tem uma explicação bem elegante (e traduzível pra Perl) <a href="http://www.python.org/doc/essays/graphs/">http://www.python.org/doc/essays/graphs/</a> <br>
Basicamente o grafo:<br>
<pre> <span style="font-family:courier new,monospace"> A -> B
A -> C
B -> C
B -> D
C -> D
D -> C
E -> F
F -> C</span></pre>Vai virar algo assim:<br><pre><span style="font-family:courier new,monospace">graph = {'A': ['B', 'C'],
'B': ['C', 'D'],
'C': ['D'],
'D': ['C'],
'E': ['F'],
'F': ['C']}</span></pre>Depois é so criar uma função que encontre o caminho entre dois nós:<br><pre><span style="font-family:courier new,monospace"> def find_path(graph, start, end, path=[]):
path = path + [start]
if start == end:
return path
if not graph.has_key(start):
return None
for node in graph[start]:
if node not in path:
newpath = find_path(graph, node, end, path)
if newpath: return newpath
return None</span></pre><br>E rodar o código:<br><pre> >>> find_path(graph, 'A', 'D')
['A', 'B', 'C', 'D']</pre>O tamanho do array retornado por find_path() vai ser o número que você procura.<br></div><div><br>( )s<br></div><div>Carlos.<br></div></div></div></div>