C# 在有机图中寻找圈

C# 在有机图中寻找圈,c#,detection,cycle,C#,Detection,Cycle,我想在一个有机构建的图中找到所有的循环。简而言之,当两个分支或路径在一个公共顶点结束时,就会发生循环。类似于 1->2->3->4 1->5->4 形成1->2->3->4->5->1的循环 由于图的性质,我不能使用DFS或类似的算法,因为没有后边。我已经找到了正确的方向,但在这两个线程中没有提出有效的算法 是否存在伪代码或实现形式的优化解决方案,或者我是否应该倾向于所提供的任何解决方案并自己进行优化?在后一种情况下,我应该使用哪个提供的代码示例来实现此目的 期待您的回复。我认为DFS适合您 探

我想在一个有机构建的图中找到所有的循环。简而言之,当两个分支或路径在一个公共顶点结束时,就会发生循环。类似于
1->2->3->4
1->5->4
形成1->2->3->4->5->1的循环

由于图的性质,我不能使用DFS或类似的算法,因为没有后边。我已经找到了正确的方向,但在这两个线程中没有提出有效的算法

是否存在伪代码或实现形式的优化解决方案,或者我是否应该倾向于所提供的任何解决方案并自己进行优化?在后一种情况下,我应该使用哪个提供的代码示例来实现此目的


期待您的回复。

我认为DFS适合您

探索图形,直到遇到已探索的顶点

这是伪代码:

function DFSFindCycle(Start, Goal)
push(Stack,Start)
while Stack is not empty
    var Node := Pop(Stack)
    Color(Node, Grey)
    for Child in ExpandNotExploredEdges(Node)
        if Node.color != White
    return true 
        if Node.color = White
           push(Stack, Child)
           label edge e, Node:Child as explored
    Color(Node, Black)
retrun false
nodes = dictionary(Vector2, Vertex)
vertices = GetData()

for each vertex in vertices

    bBackEdge = false
    //Check if vertex already exists
    //If true, we got a back-edge
    if nodes[vertex.Vector]
        tempVertex = nodes[vertex.Vector]
        bBackEdge = true
    else
        tempVertex = new Vertex(...)
        nodes.add(tempVertex.Vector, tempVertex )
    end of if

    for each root in tempVertex.Roots
        if nodes[root.Vector]
            tempRoot = nodes[root.Vector]
            tempRoot.Children.Add(vein)
            if bBackEdge
                vein.Children.Add(tempRoot)
            end of if
        end of if
    end of for
end of for

希望对您有所帮助

我得到了一个DFS,可以使用示例输入,但每当我使用生成的图形作为输入时,它都无法找到任何循环。也许你可以在这方面帮助我,因为我似乎陷入了一个逻辑黑洞

请注意,输入数据的顺序如下: 顶点顶点顶点得到零个或多个根顶点

我正在查看以下伪代码:

function DFSFindCycle(Start, Goal)
push(Stack,Start)
while Stack is not empty
    var Node := Pop(Stack)
    Color(Node, Grey)
    for Child in ExpandNotExploredEdges(Node)
        if Node.color != White
    return true 
        if Node.color = White
           push(Stack, Child)
           label edge e, Node:Child as explored
    Color(Node, Black)
retrun false
nodes = dictionary(Vector2, Vertex)
vertices = GetData()

for each vertex in vertices

    bBackEdge = false
    //Check if vertex already exists
    //If true, we got a back-edge
    if nodes[vertex.Vector]
        tempVertex = nodes[vertex.Vector]
        bBackEdge = true
    else
        tempVertex = new Vertex(...)
        nodes.add(tempVertex.Vector, tempVertex )
    end of if

    for each root in tempVertex.Roots
        if nodes[root.Vector]
            tempRoot = nodes[root.Vector]
            tempRoot.Children.Add(vein)
            if bBackEdge
                vein.Children.Add(tempRoot)
            end of if
        end of if
    end of for
end of for

如果我再次陷入困境,我会尽力向你汇报。编辑:我想我需要修改输入数据的工作方式。这段代码与我提出的代码非常不同(顶点不使用颜色,堆栈也不使用)。我想还是坚持我的准则吧……是的。它不是DFS,而是我在将其输入DFS之前在有机图中查找后边所使用的算法。我对造成的混乱表示歉意。因为我有一个基本的循环检测和枚举功能,所以我认为与您分享它是明智的。由于您建议使用DFS,正确准备输入数据对于获得所需结果非常重要。然而,这就是我现在被困的地方。因此,如果您能看看上面的伪代码并给出一些反馈,我们将不胜感激。谢谢。首先我不知道你从哪里回来。第二,eahc vertex将其改回false。我知道这对于简单的情况是有效的,但我认为我的方法可以解决所有的情况……我勾选了你的答案作为接受。你的原始答案很有帮助,因为我知道该使用哪种算法。我可能会在某个时候创建一个关于准备数据的新线程,但首先我会做一些其他的事情,稍后再回到这个问题。有时候,这有助于我和问题之间的距离。