author | Sverre Rabbelier <srabbelier@gmail.com> |
Thu, 27 Nov 2008 23:40:30 +0000 | |
changeset 598 | c1f9435bb645 |
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 |
Search 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: breadth_first_search, depth_first_search, _bfs, _dfs |
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 |
# Depth-first search |
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 depth_first_search(graph, root=None): |
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 |
Depth-first search. |
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 |
@type root: node |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
42 |
@param root: Optional root node (will explore only root's connected component) |
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 |
@rtype: tuple |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
45 |
@return: A tupple containing a dictionary and two lists: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
46 |
1. Generated spanning tree |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
47 |
2. Graph's preordering |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
48 |
3. Graph's postordering |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
49 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
50 |
visited = {} # List for marking visited and non-visited nodes |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
51 |
spanning_tree = {} # Spanning tree |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
52 |
pre = [] # Graph's preordering |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
53 |
post = [] # Graph's postordering |
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 |
# DFS from one node only |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
56 |
if (root is not None): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
57 |
spanning_tree[root] = None |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
58 |
_dfs(graph, visited, spanning_tree, pre, post, root) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
59 |
return spanning_tree, pre, post |
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 |
# Algorithm loop |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
62 |
for each in graph: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
63 |
# Select a non-visited node |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
64 |
if (each not in visited): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
65 |
spanning_tree[each] = None |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
66 |
# Explore node's connected component |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
67 |
_dfs(graph, visited, spanning_tree, pre, post, each) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
68 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
69 |
return spanning_tree, pre, post |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
70 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
71 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
72 |
def _dfs(graph, visited, spanning_tree, pre, post, node): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
73 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
74 |
Depht-first search subfunction. |
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 |
@type graph: graph |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
77 |
@param graph: Graph. |
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 |
@type visited: dictionary |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
80 |
@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
|
81 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
82 |
@type spanning_tree: dictionary |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
83 |
@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
|
84 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
85 |
@type pre: list |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
86 |
@param pre: Graph's preordering. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
87 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
88 |
@type post: list |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
89 |
@param post: Graph's postordering. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
90 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
91 |
@type node: node |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
92 |
@param node: Node to be explored by DFS. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
93 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
94 |
visited[node] = 1 |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
95 |
pre.append(node) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
96 |
# Explore recursively the connected component |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
97 |
for each in graph[node]: |
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 |
spanning_tree[each] = node |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
100 |
_dfs(graph, visited, spanning_tree, pre, post, each) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
101 |
post.append(node) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
102 |
|
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 |
# Breadth-first search |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
105 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
106 |
def breadth_first_search(graph, root=None): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
107 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
108 |
Breadth-first search. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
109 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
110 |
@type graph: graph |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
111 |
@param graph: Graph. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
112 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
113 |
@type root: node |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
114 |
@param root: Optional root node (will explore only root's connected component) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
115 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
116 |
@rtype: tuple |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
117 |
@return: A tuple containing a dictionary and a list. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
118 |
1. Generated spanning tree |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
119 |
2. Graph's level-based ordering |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
120 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
121 |
queue = [] # Visiting queue |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
122 |
spanning_tree = {} # Spanning tree |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
123 |
ordering = [] |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
124 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
125 |
# BFS from one node only |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
126 |
if (root is not None): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
127 |
queue.append(root) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
128 |
ordering.append(root) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
129 |
spanning_tree[root] = None |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
130 |
_bfs(graph, queue, spanning_tree, ordering) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
131 |
return spanning_tree, ordering |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
132 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
133 |
# Algorithm |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
134 |
for each in graph: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
135 |
if (each not in spanning_tree): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
136 |
queue.append(each) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
137 |
ordering.append(root) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
138 |
spanning_tree[each] = None |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
139 |
_bfs(graph, queue, spanning_tree, ordering) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
140 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
141 |
return spanning_tree, ordering[1:] |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
142 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
143 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
144 |
def _bfs(graph, queue, spanning_tree, ordering): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
145 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
146 |
Breadth-first search subfunction. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
147 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
148 |
@type graph: graph |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
149 |
@param graph: Graph. |
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 |
@type spanning_tree: dictionary |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
152 |
@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
|
153 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
154 |
@type ordering: list |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
155 |
@param ordering: Graph's level-based ordering. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
156 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
157 |
@type queue: list |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
158 |
@param queue: Nodes to be visited (ordered). |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
159 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
160 |
while (queue != []): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
161 |
node = queue.pop(0) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
162 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
163 |
for other in graph[node]: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
164 |
if (other not in spanning_tree): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
165 |
queue.append(other) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
166 |
ordering.append(other) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
167 |
spanning_tree[other] = node |