author | Sverre Rabbelier <srabbelier@gmail.com> |
Thu, 27 Nov 2008 21:57:24 +0000 | |
changeset 597 | 66088092f849 |
parent 594 | 06c2228e39cb |
permissions | -rw-r--r-- |
594
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
1 |
# Copyright (c) 2007-2008 Pedro Matiello <pmatiello@gmail.com> |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
2 |
# |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
3 |
# Permission is hereby granted, free of charge, to any person |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
4 |
# obtaining a copy of this software and associated documentation |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
5 |
# files (the "Software"), to deal in the Software without |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
6 |
# restriction, including without limitation the rights to use, |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
7 |
# copy, modify, merge, publish, distribute, sublicense, and/or sell |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
8 |
# copies of the Software, and to permit persons to whom the |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
9 |
# Software is furnished to do so, subject to the following |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
10 |
# conditions: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
11 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
12 |
# The above copyright notice and this permission notice shall be |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
13 |
# included in all copies or substantial portions of the Software. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
14 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
15 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
16 |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
17 |
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
18 |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
19 |
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
20 |
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
21 |
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
22 |
# OTHER DEALINGS IN THE SOFTWARE. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
23 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
24 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
25 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
26 |
Accessibility algorithms for python-graph. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
27 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
28 |
@sort: accessibility, connected_components, cut_edges, cut_nodes, mutual_accessibility |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
29 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
30 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
31 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
32 |
# Transitive-closure |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
33 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
34 |
def accessibility(graph): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
35 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
36 |
Accessibility matrix (transitive closure). |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
37 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
38 |
@type graph: graph |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
39 |
@param graph: Graph. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
40 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
41 |
@rtype: dictionary |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
42 |
@return: Accessibility information for each node. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
43 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
44 |
accessibility = {} # Accessibility matrix |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
45 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
46 |
# For each node i, mark each node j if that exists a path from i to j. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
47 |
for each in graph: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
48 |
access = {} |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
49 |
# Perform DFS to explore all reachable nodes |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
50 |
_dfs(graph, access, 1, each) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
51 |
accessibility[each] = access.keys() |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
52 |
return accessibility |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
53 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
54 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
55 |
# Strongly connected components |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
56 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
57 |
def mutual_accessibility(graph): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
58 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
59 |
Mutual-accessibility matrix (strongly connected components). |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
60 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
61 |
@type graph: graph |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
62 |
@param graph: Graph. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
63 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
64 |
@rtype: dictionary |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
65 |
@return: Mutual-accessibility information for each node. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
66 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
67 |
mutual_access = {} |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
68 |
access = graph.accessibility() |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
69 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
70 |
for i in graph: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
71 |
mutual_access[i] = [] |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
72 |
for j in graph: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
73 |
if (i in access[j] and j in access[i]): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
74 |
mutual_access[i].append(j) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
75 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
76 |
return mutual_access |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
77 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
78 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
79 |
# Connected components |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
80 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
81 |
def connected_components(graph): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
82 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
83 |
Connected components. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
84 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
85 |
@attention: Indentification of connected components is meaningful only for non-directed graphs. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
86 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
87 |
@type graph: graph |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
88 |
@param graph: Graph. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
89 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
90 |
@rtype: dictionary |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
91 |
@return: Pairing that associates each node to its connected component. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
92 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
93 |
visited = {} |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
94 |
count = 1 |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
95 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
96 |
# For 'each' node not found to belong to a connected component, find its connected component. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
97 |
for each in graph: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
98 |
if (each not in visited): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
99 |
_dfs(graph, visited, count, each) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
100 |
count = count + 1 |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
101 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
102 |
return visited |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
103 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
104 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
105 |
# Limited DFS implementations used by algorithms here |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
106 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
107 |
def _dfs(graph, visited, count, node): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
108 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
109 |
Depht-first search subfunction adapted for accessibility algorithms. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
110 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
111 |
@type graph: graph |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
112 |
@param graph: Graph. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
113 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
114 |
@type visited: dictionary |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
115 |
@param visited: List of nodes (visited nodes are marked non-zero). |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
116 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
117 |
@type count: number |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
118 |
@param count: Counter of connected components. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
119 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
120 |
@type node: number |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
121 |
@param node: Node to be explored by DFS. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
122 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
123 |
visited[node] = count |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
124 |
# Explore recursively the connected component |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
125 |
for each in graph[node]: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
126 |
if (each not in visited): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
127 |
_dfs(graph, visited, count, each) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
128 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
129 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
130 |
# Cut-Edge and Cut-Vertex identification |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
131 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
132 |
def cut_edges(graph): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
133 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
134 |
Return the cut-edges of the given graph. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
135 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
136 |
@rtype: list |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
137 |
@return: List of cut-edges. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
138 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
139 |
pre = {} |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
140 |
low = {} |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
141 |
spanning_tree = {} |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
142 |
reply = [] |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
143 |
pre[None] = 0 |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
144 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
145 |
for each in graph: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
146 |
if (not pre.has_key(each)): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
147 |
spanning_tree[each] = None |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
148 |
_cut_dfs(graph, spanning_tree, pre, low, reply, each) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
149 |
return reply |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
150 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
151 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
152 |
def cut_nodes(graph): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
153 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
154 |
Return the cut-nodes of the given graph. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
155 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
156 |
@rtype: list |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
157 |
@return: List of cut-nodes. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
158 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
159 |
pre = {} # Pre-ordering |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
160 |
low = {} # Lowest pre[] reachable from this node going down the spanning tree + one backedge |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
161 |
reply = {} |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
162 |
spanning_tree = {} |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
163 |
pre[None] = 0 |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
164 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
165 |
# Create spanning trees, calculate pre[], low[] |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
166 |
for each in graph: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
167 |
if (not pre.has_key(each)): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
168 |
spanning_tree[each] = None |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
169 |
_cut_dfs(graph, spanning_tree, pre, low, [], each) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
170 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
171 |
# Find cuts |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
172 |
for each in graph: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
173 |
# If node is not a root |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
174 |
if (spanning_tree[each] is not None): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
175 |
for other in graph[each]: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
176 |
# If there is no back-edge from descendent to a ancestral of each |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
177 |
if (low[other] >= pre[each] and spanning_tree[other] == each): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
178 |
reply[each] = 1 |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
179 |
# If node is a root |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
180 |
else: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
181 |
children = 0 |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
182 |
for other in graph: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
183 |
if (spanning_tree[other] == each): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
184 |
children = children + 1 |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
185 |
# root is cut-vertex iff it has two or more children |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
186 |
if (children >= 2): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
187 |
reply[each] = 1 |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
188 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
189 |
return reply.keys() |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
190 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
191 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
192 |
def _cut_dfs(graph, spanning_tree, pre, low, reply, node): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
193 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
194 |
Depth first search adapted for identification of cut-edges and cut-nodes. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
195 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
196 |
@type graph: graph |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
197 |
@param graph: Graph |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
198 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
199 |
@type spanning_tree: dictionary |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
200 |
@param spanning_tree: Spanning tree being built for the graph by DFS. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
201 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
202 |
@type pre: dictionary |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
203 |
@param pre: Graph's preordering. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
204 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
205 |
@type low: dictionary |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
206 |
@param low: Associates to each node, the preordering index of the node of lowest preordering |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
207 |
accessible from the given node. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
208 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
209 |
@type reply: list |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
210 |
@param reply: List of cut-edges. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
211 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
212 |
@type node: node |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
213 |
@param node: Node to be explored by DFS. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
214 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
215 |
pre[node] = pre[None] |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
216 |
low[node] = pre[None] |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
217 |
pre[None] = pre[None] + 1 |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
218 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
219 |
for each in graph[node]: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
220 |
if (not pre.has_key(each)): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
221 |
spanning_tree[each] = node |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
222 |
_cut_dfs(graph, spanning_tree, pre, low, reply, each) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
223 |
if (low[node] > low[each]): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
224 |
low[node] = low[each] |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
225 |
if (low[each] == pre[each]): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
226 |
reply.append((node, each)) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
227 |
elif (low[node] > pre[each] and spanning_tree[node] != each): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
228 |
low[node] = pre[each] |