[英]Time Complexity of algorithm to find the number of cycles of length n in an undirected graph
[英]find cycles of length 4 in undirected graph
我想打印找到的長度為 4 的循環,這段代碼幫助我正確計算循環數,但我也希望打印這些循環,例如在這個特定的輸入圖中,循環是:
0 -> 1 -> 2 -> 3 -> 0
0 -> 1 -> 4 -> 3 -> 0
1 -> 2 -> 3 -> 4 -> 1
但我無法打印它們,任何人都可以幫忙或提示我如何打印它們嗎?
這是使用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))```
將您正在訪問的節點保留在列表中,並將其傳遞到dfs
函數中。 如果您找到一個循環,請將路徑添加到所有路徑的列表中。
這是修改后的代碼:
# 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))
如果你打印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.