Skip to content

Commit

Permalink
Specified node BCC or edge BCC
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn-martinsson committed Sep 25, 2023
1 parent ec69341 commit f5321cf
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pyrival/graphs/bcc.py → pyrival/graphs/edge_bcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def find_SCC(graph):

"""
Given an undirected simple graph, find_BCC returns a list of lists
containing the biconnected components of the graph. Runs in O(n + m) time.
containing the edge biconnected components of the graph (i.e. no bridges).
Runs in O(n + m) time.
"""
def find_BCC(graph):
d = 0
Expand Down
62 changes: 62 additions & 0 deletions pyrival/graphs/node_bcc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
Given an undirected graph,
1 4 7 10
/ \ / \ / \ /
2 0 5 9
\ / \ / \ /
3 6 8
the cut_tree function returns the cut-block tree made out of 2 sets of nodes, the original
nodes U in the graph and new nodes V corresponding to each node biconnected component.
There is an edge between (u,v) for u in U and v in V if u lies in bicomponent v.
In the case of an unconnected graph, the function returns a forest of block-cut trees.
For the graph above, this is the tree returned by cut_tree
1 4 7 10
| | | |
2--(14)--0--(13)--5--(12)--9--(11)
| | |
3 6 8
Here the nodes in () denotes bicomponent nodes.
"""
def cut_tree(graph):
n = len(graph)
new_graph = [[] for _ in range(n)]

P = [-1] * n
depth = [-1] * n
biconnect = [None] * n
root = 0

preorder = []
stack = [root]
while stack:
node = stack.pop()
if depth[node] >= 0:
continue
depth[node] = len(preorder)
preorder.append(node)
for nei in graph[node]:
if depth[nei] == -1:
P[nei] = node
stack.append(nei)
preorder.pop(0)

for node in reversed(preorder):
depth[node] = min(depth[nei] for nei in graph[node])
if depth[P[node]] == depth[node]:
bicon = biconnect[node] = [P[node], node]
new_graph.append(bicon)

for node in preorder:
if biconnect[node] is None:
bicon = biconnect[node] = biconnect[P[node]]
bicon.append(node)

for bicon_ind in range(n, len(new_graph)):
for node in new_graph[bicon_ind]:
new_graph[node].append(bicon_ind)

return new_graph

0 comments on commit f5321cf

Please sign in to comment.