如何在visual basic中删除图形中的循环或循环?

时间:2010-08-15 14:03:35

标签: .net visual-studio-2008 graph

本网站有很多关于我的问题的文章。 我有一个矩阵例如(10 x 10)代表10个节点。该矩阵称为MyMat(9,9)

此矩阵的行表示源节点(From Node),列表示目标节点(To Node)。它有14个随机分布的链接。非零值表示节点之间的连接。

0 0 0 0 1 0 0 0 0 0 
0 0 0 1 0 0 0 0 1 0
0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 0 0 1
0 0 0 0 0 0 1 0 0 0
0 1 1 0 0 1 0 0 0 0

我想要的是防止系统中每个节点的循环(循环)。例如: 节点1:无循环

节点2:2,9,7,8,10,2。这里循环存在,因为它以2开始并以2完成。我想要阻止此网络中的循环。这意味着:MyMat(9,1)必须为0 节点2:2,9,7,8,10,3,2。这意味着MyMat(2,1)必须为0

节点3:无循环

节点4:4,7,8,4。这意味着MyMat(7,3)必须为0

节点5:5,8,10,6,5。这意味着MyMat(5,4)必须为0

节点6:无循环

节点7:无循环

节点8:无循环

节点9:无循环

节点10:无循环

从上面的矩阵中删除了4个连接。

我通过一种名为Depth first search的技术完成了这项工作,但它非常慢,并且加重了程序的运行时间,特别是当我使用60个节点和100个连接时! 如果您使用Google,可以找到几个编程示例。

在visual basic或C#中有更简单(更快)的方法吗?

2 个答案:

答案 0 :(得分:1)

深度优先搜索不应该花很长时间。

Procedure Depth_First_Clean(graph, v){
  color v grey
  for each edge (v,u){
     if u is grey 
       delete edge (v,u)
     else if u is white
       Depth_First_Clean(graph, u)
  }
  color v black
}

Procedure Clean_all(graph) {
  Mark all nodes white
  While there are white nodes left {
    v = a white node
    Depth_First_Clean(graph, v)
  }

这会遍历每个边缘一次,因此在具有100个边缘的图形中几乎不需要时间。

从示例中确定(我将重新编号节点以摆脱我们的示例中的一个问题的混乱

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

we mark all nodes white.  
We start with node 0
  mark node 0 grey
   traverse edge (0,4)
     mark node 4 grey
       traverse edge (4, 7)
         mark node 7 grey
           traverse edge (7, 3)
             mark node 3 grey 
               traverse edge (3,6)
                 mark node 6 grey
                   delete edge (6, 7) -- node 7 is grey break cycle 7 3 6 7
                 color node 6 black
             color node 3 black 
           traverse edge (7, 9)
             mark node 9 grey
               traverse edge (9, 1)
                 mark node 1 grey
                   skip edge (1,3) -- 3 is black there are no cycles through 3
                   traverse edge (1, 8)
                     mark node 8 grey
                       skip edge (8, 6)
                     color node 8 black
                 color node 1 black
               traverse edge (9, 2)   
                 color node 2 grey
                   skip edge (2,1)
                 color node 2 black
               traverse edge (9, 5)
                 color node 5 grey
                   delete edge (5, 4)
                 color node 5 black
             color node 9 black
         color node 7 black       
     color node 4 black       
 color node 0 black       
None of the remening nodes are white so we are done

答案 1 :(得分:0)

我找到了解决方案。

Public Function DFS3(ByVal V As Integer) As Integer

    TheList(V) = "G"
    For U = 0 To NumberofNodes - 1
        If Matt(V, U) > 0 Then
            If TheList(U) = "G" Then
                Matt(V, U) = 0
                RichTextBox1.AppendText("Link  " & V + 1 & " " & U + 1 & "   Deleted " & vbNewLine)
            ElseIf TheList(U) = "W" Then
                DFS3(U)
            End If
        End If
    Next U
    TheList(V) = "B"

End Function
相关问题