0

我想在一个无向多重图中列出一个从根节点(Tarjan 的索引 0)开始的循环,该循环在根节点处开始和结束,而不通过先前访问的节点返回某个循环。

我使用这些指令在 Perl 中编写了Tarjan 的强连接组件算法Cycle detection in a Multigraph。这是我的图表

V   E   E   E
1   2   3   4
2   1   3   
3   1   2   
4           1

我得到这个结果

1 root
3 2 1
------------
2 root
3 1 2
------------
3 root
2 1 3
------------
4 root
3 2 1 4
------------

When 4 is selected as index 0 or the root I would like it to return 1 4 because the path must pass through 1 twice to complete the cycle with the solution of 3 2 1 4.

谢谢

4

1 回答 1

0

用邻居搜索改变Tarjan 的强连接组件算法,以确保每个节点共享一条边满足我的需求。它省略了一些解决方案。

for each v in V do
 index := 0
 S := empty
    strongconnect(v)
repeat
...
while (w != v)
  if (loopcount = 0)
   w:=v
  else
   w := S.pop()
  end if
while (continuation = false)
 x := S.pop()
 for each (y, x) in E do
   if (y = w) then
     continuation = true
   end if
  repeat
  if (x = v) then
   continuation = true
 S.push(v)
 loopcount := loopcount+1
if(continuation = true)
  add x to current strongly connected component
endif
 repeat


        2   

8   3   1   6

    4       7
        5   

1   2   5   
2   3   6   1
3   4   2   8
4   3   5   
5   4   7   1
6   2   7   
7   5   6   
8           3



1 list
5 4 3 2 1 
------------
2 list
1 5 4 3 2 
------------
3 list
8 3 
------------
4 list
5 7 6 2 3 4 
------------
5 list
1 2 3 4 5 
------------
6 list
7 5 4 3 2 6 
------------
7 list
6 2 3 4 5 7 
------------
8 list
3 8 
------------
于 2013-03-16T08:01:42.840 回答