简体   繁体   English

在无向图中找到长度为 4 的环

[英]find cycles of length 4 in undirected graph

I'm looking to print the found cycles of length 4, this code helps me correctly count the number of cycles but I also wish to print those cycles for example in this particular input graph the cycles are:我想打印找到的长度为 4 的循环,这段代码帮助我正确计算循环数,但我也希望打印这些循环,例如在这个特定的输入图中,循环是:

0 -> 1 -> 2 -> 3 -> 0
0 -> 1 -> 4 -> 3 -> 0
1 -> 2 -> 3 -> 4 -> 1

but I am not able to print them, can anyone please help or hint how I can print them?但我无法打印它们,任何人都可以帮忙或提示我如何打印它们吗?

here is the code to count using dfs:这是使用dfs进行计数的代码:

# Python Program to count 
# cycles of length n 
# in a given graph. 

# Number of vertices 
V = 5

def DFS(graph, marked, n, vert, start, count): 

    # mark the vertex vert as visited 
    marked[vert] = True

    # if the path of length (n-1) is found 
    if n == 0: 

        # mark vert as un-visited to make 
        # it usable again. 
        marked[vert] = False

        # Check if vertex vert can end with 
        # vertex start 
        if graph[vert][start] == 1: 
            count = count + 1
            return count 
        else: 
            return count 

    # For searching every possible path of 
    # length (n-1) 
    for i in range(V): 
        if marked[i] == False and graph[vert][i] == 1: 

            # DFS for searching path by decreasing 
            # length by 1 
            count = DFS(graph, marked, n-1, i, start, count) 

    # marking vert as unvisited to make it 
    # usable again. 
    marked[vert] = False
    return count 

# Counts cycles of length 
# N in an undirected 
# and connected graph. 
def countCycles( graph, n): 

    # all vertex are marked un-visited initially. 
    marked = [False] * V 

    # Searching for cycle by using v-n+1 vertices 
    count = 0
    for i in range(V-(n-1)): 
        count = DFS(graph, marked, n-1, i, i, count) 

        # ith vertex is marked as visited and 
        # will not be visited again. 
        marked[i] = True

    return int(count/2) 

# main : 
graph = [[0, 1, 0, 1, 0], 
        [1 ,0 ,1 ,0, 1], 
        [0, 1, 0, 1, 0], 
        [1, 0, 1, 0, 1], 
        [0, 1, 0, 1, 0]] 

n = 4
print("Total cycles of length ",n," are ",countCycles(graph, n))```


Keep the nodes you are visiting in list and pass it along in the dfs function.将您正在访问的节点保留在列表中,并将其传递到dfs函数中。 If you find a cycle add the path to the list of all paths.如果您找到一个循环,请将路径添加到所有路径的列表中。

Here is the modified code:这是修改后的代码:

# cycles of length n 
# in a given graph. 

# Number of vertices 
V = 5
paths = []
def DFS(graph, marked, n, vert, start, count, path): 

    # mark the vertex vert as visited 
    marked[vert] = True

    # if the path of length (n-1) is found 
    if n == 0: 

        # mark vert as un-visited to make 
        # it usable again. 
        marked[vert] = False

        # Check if vertex vert can end with 
        # vertex start 
        if graph[vert][start] == 1: 
            count = count + 1
            paths.append(path)
            return count 
        else: 
            return count 

    # For searching every possible path of 
    # length (n-1) 
    for i in range(V): 
        if marked[i] == False and graph[vert][i] == 1: 

            # DFS for searching path by decreasing 
            # length by 1 
            next_path = path[:]
            next_path.append(i)
            count = DFS(graph, marked, n-1, i, start, count, next_path) 

    # marking vert as unvisited to make it 
    # usable again. 
    marked[vert] = False
    return count 

# Counts cycles of length 
# N in an undirected 
# and connected graph. 
def countCycles( graph, n): 

    # all vertex are marked un-visited initially. 
    marked = [False] * V 

    # Searching for cycle by using v-n+1 vertices 
    count = 0
    for i in range(V-(n-1)): 
        count = DFS(graph, marked, n-1, i, i, count,[i]) 

        # ith vertex is marked as visited and 
        # will not be visited again. 
        marked[i] = True

    return int(count/2) 

# main : 
graph = [[0, 1, 0, 1, 0], 
        [1 ,0 ,1 ,0, 1], 
        [0, 1, 0, 1, 0], 
        [1, 0, 1, 0, 1], 
        [0, 1, 0, 1, 0]] 

n = 4
print("Total cycles of length ",n," are ",countCycles(graph, n))

if you print the paths list you will get this: [[0, 1, 2, 3], [0, 1, 4, 3], [0, 3, 2, 1], [0, 3, 4, 1], [1, 2, 3, 4], [1, 4, 3, 2]]如果你打印paths列表你会得到这个: [[0, 1, 2, 3], [0, 1, 4, 3], [0, 3, 2, 1], [0, 3, 4, 1], [1, 2, 3, 4], [1, 4, 3, 2]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM