scripts/graph/minmax.py
author Sverre Rabbelier <srabbelier@gmail.com>
Thu, 27 Nov 2008 23:41:08 +0000
changeset 599 32a30f609530
parent 594 app/graph/minmax.py@06c2228e39cb
permissions -rw-r--r--
Moved check_includes and graph from app to scripts Patch by: Sverre Rabbelier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
#                         Rhys Ulerich <rhys.ulerich@gmail.com>
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     3
#
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
# Permission is hereby granted, free of charge, to any person
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# obtaining a copy of this software and associated documentation
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# files (the "Software"), to deal in the Software without
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# restriction, including without limitation the rights to use,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
# copy, modify, merge, publish, distribute, sublicense, and/or sell
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
# 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
    10
# Software is furnished to do so, subject to the following
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# conditions:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# The above copyright notice and this permission notice shall be
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# included in all copies or substantial portions of the Software.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
# 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
    17
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
# 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
    23
# OTHER DEALINGS IN THE SOFTWARE.
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
"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
Minimization and maximization algorithms for python-graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
@sort: minimal_spanning_tree, shortest_path, _first_unvisited, _lightest_edge
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
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
# Minimal spanning tree
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
def minimal_spanning_tree(graph, root=None):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
	"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
	Minimal spanning tree.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
	@attention: Minimal spanning tree meaningful only for weighted graphs.
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  graph: graph
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
	@param graph: Graph.
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
	@type  root: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
	@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
    46
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
	@rtype:  dictionary
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
	@return: Generated spanning tree.
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 = {}		# MInimal Spanning tree
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    53
	# Initialization
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    54
	if (root is not None):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    55
		visited.append(root)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    56
		nroot = root
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
	else:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
		nroot = 1
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
	while (nroot is not None):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
		ledge = _lightest_edge(graph, visited)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
		if (ledge == (-1, -1)):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    65
			if (root is not None):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    66
				break
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    67
			nroot = _first_unvisited(graph, visited)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    68
			if (nroot is not None):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    69
				spanning_tree[nroot] = None
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    70
			visited.append(nroot)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    71
		else:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    72
			spanning_tree[ledge[1]] = ledge[0]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    73
			visited.append(ledge[1])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    74
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    75
	return spanning_tree
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    76
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
def _first_unvisited(graph, visited):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    79
	"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    80
	Return first unvisited node.
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  graph: graph
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    83
	@param graph: Graph.
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  visited: list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    86
	@param visited: List of nodes.
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
	@rtype:  node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    89
	@return: First unvisited node.
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
	for each in graph:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    92
		if (each not in visited):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    93
			return each
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    94
	return None
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
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    97
def _lightest_edge(graph, visited):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    98
	"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    99
	Return the lightest edge in graph going from a visited node to an unvisited one.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   100
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   101
	@type  graph: graph
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   102
	@param graph: Graph.
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
	@type  visited: list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   105
	@param visited: List of nodes.
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
	@rtype:  tuple
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   108
	@return: Lightest edge in graph going from a visited node to an unvisited one.
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
	lightest_edge = (-1, -1)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   111
	weight = -1
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   112
	for each in visited:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   113
		for other in graph[each]:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   114
			if (other not in visited):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   115
				w = graph.get_edge_weight(each, other)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   116
				if (w < weight or weight < 0):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   117
					lightest_edge = (each, other)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   118
					weight = w
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   119
	return lightest_edge
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
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   122
# Shortest Path
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   123
# Code donated by Rhys Ulerich
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
def shortest_path(graph, source):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   126
	"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   127
	Return the shortest path distance between source and all other nodes using Dijkstra's algorithm.
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
	@attention: All weights must be nonnegative.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   130
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   131
	@type  graph: graph
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   132
	@param graph: 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
	@type  source: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   135
	@param source: Node from which to start the search.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   136
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   137
	@rtype:  tuple
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   138
	@return: A tuple containing two dictionaries, each keyed by target nodes.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   139
		1. Shortest path spanning tree
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   140
		2. Shortest distance from given source to each target node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   141
	Inaccessible target nodes do not appear in either dictionary.
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
	# Initialization
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   144
	dist	 = { source: 0 }
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   145
	previous = { source: None}
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   146
	q = graph.nodes()
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
	# Algorithm loop
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   149
	while q:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   150
		# examine_min process performed using O(nodes) pass here.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   151
		# May be improved using another examine_min data structure.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   152
		u = q[0]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   153
		for node in q[1:]:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   154
			if ((not dist.has_key(u)) 
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   155
				or (dist.has_key(node) and dist[node] < dist[u])):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   156
				u = node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   157
		q.remove(u)
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
		# Process reachable, remaining nodes from u
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   160
		if (dist.has_key(u)):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   161
			for v in graph[u]:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   162
				if v in q:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   163
					alt = dist[u] + graph.get_edge_weight(u, v)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   164
					if (not dist.has_key(v)) or (alt < dist[v]):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   165
						dist[v] = alt
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   166
						previous[v] = u
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   167
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   168
	return previous, dist