class Graph:
#From www.geeksforgeeks.org
def worker(g, prefix, arr, u):
container = g.graph[u]
if u in g.AP():
print prefix
arr.append([prefix])
del g
else:
for i in container:
if str(i) not in prefix.split(' '):
new_prefix = prefix + ' ' + str(i)
new_g = copy.deepcopy(g)
new_g.combineNode(u, i)
if len(new_g.graph) > 1:
worker(new_g, new_prefix, arr, i)
struct = {
0:[1,12,13,14,15],
1:[2,12,14,15],
2:[3,4,5,14],
3:[4],
4:[5,6],
5:[6,9],
6:[7,8,9],
7:[8,10,11],
8:[9,10,11],
9:[10],
10:[11],
12:[15],
13:[14,15]
}
g1 = Graph (total)
for key,value in struct.iteritems():
for child in value:
g1.addEdge(key, child)
result = []
for i in range(0, total):
print 'Remove root ' + str(i)
worker(g1, str(i), result, i)
print result
一种方法是找到你的图形的“2-连通分量”。
2-连通图是指连接的图形,且不包含任何关节点。
任何简单图的边都可以分成一些2-连通子图。
第一个例子:
在上图中,以下是2-连通分量:time = 0
function DFS(vertex, adj[][], low[], disc[], parent[], visited[], V, stack)
disc[vertex]=low[vertex]=time+1
time = time + 1
visited[vertex]=true
child = 0
for i = 0 to V
if adj[vertex][i] == true
if visited[i] == false
child = child + 1
push edge(u,v) to stack
parent[i] = vertex
DFS(i, adj, low, disc, visited, V, time, stack)
low[vertex] = minimum(low[vertex], low[i])
if parent[vertex] == nil AND child > 1
while last element of stack != (u,v)
print last element of stack
pop from stack
print last element of stack
pop from stack
if parent[vertex] != nil AND low[i] >= disc[vertex]
while last element of stack != (u,v)
print last element of stack
pop from stack
print last element of stack
pop from stack
else if parent[vertex] != i AND disc[i] < low[vertex]
low[vertex] = disc[i]
push edge(u,v) to stack
fuction biconnected_components(adj[][], V)
for i = 0 to V
if visited[i] == false
DFS(i, adj, low, disc, parent, visited, V, time, stack)
while stack is not empty
print last element of stack
pop from stack
来源:
https://www.geeksforgeeks.org/biconnected-components/
https://en.wikipedia.org/wiki/Biconnected_component
https://www.hackerearth.com/practice/algorithms/graphs/biconnected-components/tutorial/