scripts/graph/__init__.py
author Sverre Rabbelier <srabbelier@gmail.com>
Thu, 27 Nov 2008 23:41:08 +0000
changeset 599 32a30f609530
parent 594 app/graph/__init__.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
#                         Christian Muise <christian.muise@gmail.com>
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     3
#                         Nathan Davis <davisn90210@gmail.com>
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#                         Zsolt Haraszti <zsolt@drawwell.net>
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
#
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# Permission is hereby granted, free of charge, to any person
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# obtaining a copy of this software and associated documentation
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
# files (the "Software"), to deal in the Software without
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
# restriction, including without limitation the rights to use,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
# copy, modify, merge, publish, distribute, sublicense, and/or sell
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# 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
    12
# Software is furnished to do so, subject to the following
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# conditions:
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 above copyright notice and this permission notice shall be
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
# included in all copies or substantial portions of the Software.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
# 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
    19
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
# 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
    25
# OTHER DEALINGS IN THE SOFTWARE.
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
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
python-graph
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
A library for working with graphs in Python.
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
@version: 1.3.1
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
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
# Imports
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
import accessibility
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
import generators
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
import minmax
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
import searching
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
import sorting
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
import readwrite
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
import traversal
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
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
# Graph class --------------------------------------------------------------------------------------
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
class graph (object):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
	"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
	Graph class.
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
	Graphs are built of nodes and edges.
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
	@sort:  __init__, __getitem__, __iter__, __len__, __str__, add_edge, add_edge_attribute,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    56
	add_graph, add_node, add_node_attribute, add_nodes, add_spanning_tree, complete, del_edge,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    57
	del_node, edges, get_edge_attributes, get_edge_label, get_edge_weight, get_node_attributes,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
	has_edge, has_node, inverse, neighbors, nodes, order, set_edge_label, set_edge_weight,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
	traversal, generate, read, write, accessibility, breadth_first_search, connected_components,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
	cut_edges, cut_nodes, depth_first_search, minimal_spanning_tree, shortest_path
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
	"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
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
	def __init__(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    65
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    66
		Initialize a graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    67
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    68
		self.node_neighbors = {}		# Pairing: Node -> Neighbors
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    69
		self.edge_properties = {}		# Pairing: Edge -> (Label, Weight)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    70
		self.node_attr = {}				# Pairing: Node -> Attributes
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    71
		self.edge_attr = {}				# Pairing: Edge -> Attributes
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    72
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
	def __str__(self):
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 a string representing the graph when requested by str() (or print).
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
		@rtype:  string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    79
		@return: String representing the graph.
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
		return "<graph object " + str(self.nodes()) + " " + str(self.edges()) + ">"
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
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    84
	def __len__(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    85
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    86
		Return the order of the graph when requested by len().
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:  number
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    89
		@return: Size of the graph.
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
		return len(self.node_neighbors)
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
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    94
	def __iter__(self):
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
		Return a iterator passing through all nodes in the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    97
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    98
		@rtype:  iterator
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    99
		@return: Iterator passing through all nodes in the graph.
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
		for each in self.node_neighbors.iterkeys():
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   102
			yield each
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
	def __getitem__(self, node):
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
		Return a iterator passing through all neighbors of the given 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
		@rtype:  iterator
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   110
		@return: Iterator passing through all neighbors of the given node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   111
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   112
		for each in self.node_neighbors[node]:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   113
			yield each
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   114
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
	def read(self, string, fmt='xml'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   117
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   118
		Read a graph from a string. Nodes and edges specified in the input will be added to the
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   119
		current graph.
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
		@type  string: string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   122
		@param string: Input string specifying a graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   123
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   124
		@type  fmt: string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   125
		@param fmt: Input format. Possible formats are:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   126
			1. 'xml' - XML (default)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   127
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   128
		if (fmt == 'xml'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   129
			readwrite.read_xml(self, string)
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
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   132
	def write(self, fmt='xml'):
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
		Write the graph to a string. Depending of the output format, this string can be used by
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   135
		read() to rebuild the graph.
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
		@type  fmt: string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   138
		@param fmt: Output format. Possible formats are:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   139
			1. 'xml' - XML (default)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   140
			2. 'dot' - DOT Language (for GraphViz)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   141
			3. 'dotwt' - DOT Language with weight information
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
		@rtype:  string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   144
		@return: String specifying the graph.
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
		if (fmt == 'xml'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   147
			return readwrite.write_xml(self)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   148
		elif (fmt == 'dot'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   149
			return readwrite.write_dot_graph(self, False)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   150
		elif (fmt == 'dotwt'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   151
			return readwrite.write_dot_graph(self, True)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   152
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
	def generate(self, num_nodes, num_edges, weight_range=(1, 1)):
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
		Add nodes and random edges to the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   157
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   158
		@type  num_nodes: number
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   159
		@param num_nodes: Number of nodes.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   160
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   161
		@type  num_edges: number
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   162
		@param num_edges: Number of edges.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   163
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   164
		@type  weight_range: tuple
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   165
		@param weight_range: tuple of two integers as lower and upper limits on randomly generated
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   166
		weights (uniform distribution).
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
		generators.generate(self, num_nodes, num_edges, weight_range)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   169
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
	def nodes(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   172
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   173
		Return node list.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   174
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   175
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   176
		@return: Node list.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   177
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   178
		return self.node_neighbors.keys()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   179
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   180
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   181
	def neighbors(self, node):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   182
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   183
		Return all nodes that are directly accessible from given node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   184
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   185
		@type  node: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   186
		@param node: Node identifier
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   187
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   188
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   189
		@return: List of nodes directly accessible from given node.
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
		return self.node_neighbors[node]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   192
	
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
	def edges(self):
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
		Return all edges in the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   197
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   198
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   199
		@return: List of all edges in the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   200
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   201
		return self.edge_properties.keys()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   202
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   203
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   204
	def has_node(self, node):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   205
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   206
		Return whether the requested node exists.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   207
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   208
		@type  node: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   209
		@param node: Node identifier
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   210
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   211
		@rtype:  boolean
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   212
		@return: Truth-value for node existence.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   213
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   214
		return self.node_neighbors.has_key(node)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   215
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   216
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   217
	def add_node(self, node, attrs=[]):
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
		Add given node to the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   220
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   221
		@attention: While nodes can be of any type, it's strongly recommended to use only numbers
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   222
		and single-line strings as node identifiers if you intend to use write().
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   223
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   224
		@type  node: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   225
		@param node: Node identifier.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   226
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   227
		@type  attrs: list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   228
		@param attrs: List of node attributes specified as (attribute, value) tuples.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   229
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   230
		if (not node in self.node_neighbors):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   231
			self.node_neighbors[node] = []
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   232
			self.node_attr[node] = attrs
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   233
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   234
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   235
	def add_nodes(self, nodelist):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   236
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   237
		Add given nodes to the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   238
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   239
		@attention: While nodes can be of any type, it's strongly recommended to use only numbers
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   240
		and single-line strings as node identifiers if you intend to use write().
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   241
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   242
		@type  nodelist: list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   243
		@param nodelist: List of nodes to be added to the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   244
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   245
		for each in nodelist:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   246
			self.add_node(each)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   247
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   248
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   249
	def add_edge(self, u, v, wt=1, label='', attrs=[]):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   250
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   251
		Add an edge (u,v) to the graph connecting nodes u and v.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   252
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   253
		@type  u: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   254
		@param u: One node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   255
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   256
		@type  v: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   257
		@param v: Other node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   258
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   259
		@type  wt: number
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   260
		@param wt: Edge weight.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   261
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   262
		@type  label: string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   263
		@param label: Edge label.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   264
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   265
		@type  attrs: list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   266
		@param attrs: List of node attributes specified as (attribute, value) tuples.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   267
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   268
		if (v not in self.node_neighbors[u] and u not in self.node_neighbors[v]):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   269
			self.node_neighbors[u].append(v)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   270
			self.node_neighbors[v].append(u)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   271
			self.edge_properties[(u, v)] = [label, wt]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   272
			self.edge_properties[(v, u)] = [label, wt]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   273
			self.edge_attr[(u, v)] = attrs
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   274
			self.edge_attr[(v, u)] = attrs
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   275
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   276
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   277
	def del_node(self, node):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   278
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   279
		Remove a node from the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   280
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   281
		@type  node: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   282
		@param node: Node identifier.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   283
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   284
		for each in list(self.neighbors(node)):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   285
			self.del_edge(each, node)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   286
		del(self.node_neighbors[node])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   287
		del(self.node_attr[node])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   288
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   289
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   290
	def del_edge(self, u, v):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   291
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   292
		Remove an edge (u, v) from the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   293
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   294
		@type  u: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   295
		@param u: One node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   296
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   297
		@type  v: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   298
		@param v: Other node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   299
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   300
		self.node_neighbors[u].remove(v)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   301
		self.node_neighbors[v].remove(u)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   302
		del(self.edge_properties[(u,v)])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   303
		del(self.edge_properties[(v,u)])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   304
		del(self.edge_attr[(u,v)])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   305
		del(self.edge_attr[(v,u)])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   306
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   307
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   308
	def get_edge_weight(self, u, v):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   309
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   310
		Get the weight of an edge.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   311
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   312
		@type  u: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   313
		@param u: One node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   314
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   315
		@type  v: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   316
		@param v: Other node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   317
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   318
		@rtype:  number
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   319
		@return: Edge weight.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   320
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   321
		return self.edge_properties[(u, v)][1]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   322
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   323
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   324
	def set_edge_weight(self, u, v, wt):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   325
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   326
		Set the weight of an edge.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   327
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   328
		@type  u: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   329
		@param u: One node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   330
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   331
		@type  v: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   332
		@param v: Other node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   333
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   334
		@type  wt: number
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   335
		@param wt: Edge weight.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   336
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   337
		self.edge_properties[(u, v)][1] = wt
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   338
		self.edge_properties[(v, u)][1] = wt
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   339
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   340
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   341
	def get_edge_label(self, u, v):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   342
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   343
		Get the label of an edge.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   344
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   345
		@type  u: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   346
		@param u: One node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   347
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   348
		@type  v: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   349
		@param v: Other node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   350
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   351
		@rtype:  string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   352
		@return: Edge label
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   353
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   354
		return self.edge_properties[(u, v)][0]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   355
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   356
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   357
	def set_edge_label(self, u, v, label):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   358
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   359
		Set the label of an edge.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   360
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   361
		@type  u: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   362
		@param u: One node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   363
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   364
		@type  v: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   365
		@param v: Other node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   366
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   367
		@type  label: string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   368
		@param label: Edge label.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   369
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   370
		self.edge_properties[(u, v)][0] = label
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   371
		self.edge_properties[(v, u)][0] = label
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   372
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   373
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   374
	def add_node_attribute(self, node, attr):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   375
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   376
		Add attribute to the given node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   377
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   378
		@type  node: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   379
		@param node: Node identifier
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   380
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   381
		@type  attr: tuple
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   382
		@param attr: Node attribute specified as a tuple in the form (attribute, value).
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   383
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   384
		self.node_attr[node] = self.node_attr[node] + [attr]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   385
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   386
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   387
	def get_node_attributes(self, node):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   388
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   389
		Return the attributes of the given node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   390
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   391
		@type  node: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   392
		@param node: Node identifier
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   393
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   394
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   395
		@return: List of attributes specified tuples in the form (attribute, value).
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   396
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   397
		return self.node_attr[node]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   398
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   399
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   400
	def add_edge_attribute(self, u, v, attr):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   401
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   402
		Add attribute to the given edge.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   403
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   404
		@type  u: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   405
		@param u: One node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   406
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   407
		@type  v: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   408
		@param v: Other node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   409
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   410
		@type  attr: tuple
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   411
		@param attr: Node attribute specified as a tuple in the form (attribute, value).
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   412
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   413
		self.edge_attr[(u,v)] = self.edge_attr[(u,v)] + [attr]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   414
		self.edge_attr[(v,u)] = self.edge_attr[(v,u)] + [attr]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   415
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   416
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   417
	def get_edge_attributes(self, u, v):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   418
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   419
		Return the attributes of the given edge.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   420
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   421
		@type  u: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   422
		@param u: One node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   423
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   424
		@type  v: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   425
		@param v: Other node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   426
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   427
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   428
		@return: List of attributes specified tuples in the form (attribute, value).
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   429
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   430
		return self.edge_attr[(u,v)]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   431
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   432
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   433
	def has_edge(self, u, v):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   434
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   435
		Return whether an edge between nodes u and v exists.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   436
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   437
		@type  u: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   438
		@param u: One node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   439
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   440
		@type  v: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   441
		@param v: Other node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   442
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   443
		@rtype:  boolean
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   444
		@return: Truth-value for edge existence.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   445
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   446
		return self.edge_properties.has_key((u,v)) and self.edge_properties.has_key((v,u))
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   447
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   448
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   449
	def order(self, node):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   450
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   451
		Return the order of the given node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   452
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   453
		@rtype:  number
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   454
		@return: Order of the given node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   455
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   456
		return len(self.neighbors(node))
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   457
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   458
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   459
	def complete(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   460
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   461
		Make the graph a complete graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   462
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   463
		@attention: This will modify the current graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   464
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   465
		for each in self.nodes():
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   466
			for other in self.nodes():
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   467
				if (each != other):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   468
					self.add_edge(each, other)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   469
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   470
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   471
	def inverse(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   472
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   473
		Return the inverse of the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   474
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   475
		@rtype:  graph
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   476
		@return: Complement graph for the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   477
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   478
		inv = graph()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   479
		inv.add_nodes(self.nodes())
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   480
		inv.complete()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   481
		for each in self.edges():
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   482
			if (inv.has_edge(each[0], each[1])):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   483
				inv.del_edge(each[0], each[1])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   484
		return inv
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   485
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   486
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   487
	def add_graph(self, graph):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   488
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   489
		Add other graph to the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   490
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   491
		@attention: Attributes and labels are not preserved.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   492
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   493
		@type  graph: graph
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   494
		@param graph: Graph
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   495
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   496
		self.add_nodes(graph.nodes())
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   497
		for each_node in graph.nodes():
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   498
			for each_edge in graph.neighbors(each_node):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   499
				self.add_edge(each_node, each_edge)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   500
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   501
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   502
	def add_spanning_tree(self, st):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   503
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   504
		Add a spanning tree to the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   505
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   506
		@type  st: dictionary
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   507
		@param st: Spanning tree.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   508
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   509
		self.add_nodes(st.keys())
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   510
		for each in st:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   511
			if (st[each] is not None):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   512
				self.add_edge(st[each], each)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   513
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   514
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   515
	def traversal(self, node, order='pre'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   516
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   517
		Graph traversal iterator.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   518
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   519
		@type  node: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   520
		@param node: Node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   521
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   522
		@type  order: string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   523
		@param order: traversal ordering. Possible values are:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   524
			2. 'pre' - Preordering (default)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   525
			1. 'post' - Postordering
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   526
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   527
		@rtype:  iterator
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   528
		@return: Traversal iterator.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   529
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   530
		for each in traversal.traversal(self, node, order):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   531
			yield each
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   532
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   533
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   534
	def depth_first_search(self, root=None):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   535
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   536
		Depht-first search.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   537
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   538
		@type  root: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   539
		@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
   540
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   541
		@rtype:  tuple
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   542
		@return:  tupple containing a dictionary and two lists:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   543
			1. Generated spanning tree
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   544
			2. Graph's preordering
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   545
			3. Graph's postordering
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   546
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   547
		return searching.depth_first_search(self, root)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   548
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   549
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   550
	def breadth_first_search(self, root=None):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   551
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   552
		Breadth-first search.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   553
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   554
		@type  root: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   555
		@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
   556
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   557
		@rtype:  dictionary
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   558
		@return: A tuple containing a dictionary and a list.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   559
			1. Generated spanning tree
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   560
			2. Graph's level-based ordering
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   561
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   562
		return searching.breadth_first_search(self, root)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   563
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   564
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   565
	def accessibility(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   566
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   567
		Accessibility matrix (transitive closure).
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   568
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   569
		@rtype:  dictionary
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   570
		@return: Accessibility information for each node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   571
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   572
		return accessibility.accessibility(self)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   573
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   574
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   575
	def connected_components(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   576
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   577
		Connected components.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   578
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   579
		@attention: Indentification of connected components is meaningful only for non-directed
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   580
		graphs.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   581
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   582
		@rtype:  dictionary
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   583
		@return: Pairing that associates each node to its connected component.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   584
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   585
		return accessibility.connected_components(self)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   586
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   587
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   588
	def minimal_spanning_tree(self, root=None):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   589
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   590
		Minimal spanning tree.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   591
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   592
		@type  root: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   593
		@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
   594
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   595
		@attention: Minimal spanning tree meaningful only for weighted graphs.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   596
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   597
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   598
		@return: Generated spanning tree.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   599
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   600
		return minmax.minimal_spanning_tree(self, root)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   601
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   602
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   603
	def shortest_path(self, source):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   604
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   605
		Return the shortest path distance between source node and all other nodes using Dijkstra's
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   606
		algorithm.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   607
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   608
		@attention: All weights must be nonnegative.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   609
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   610
		@type  source: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   611
		@param source: Node from which to start the search.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   612
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   613
		@rtype:  tuple
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   614
		@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
   615
			1. Shortest path spanning tree
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   616
			2. Shortest distance from given source to each target node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   617
		Inaccessible target nodes do not appear in either dictionary.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   618
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   619
		return minmax.shortest_path(self, source)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   620
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   621
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   622
	def cut_edges(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   623
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   624
		Return the cut-edges of the given graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   625
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   626
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   627
		@return: List of cut-edges.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   628
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   629
		return accessibility.cut_edges(self)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   630
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   631
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   632
	def cut_nodes(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   633
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   634
		Return the cut-nodes of the given graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   635
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   636
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   637
		@return: List of cut-nodes.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   638
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   639
		return accessibility.cut_nodes(self)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   640
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   641
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   642
# Digraph class ------------------------------------------------------------------------------------
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   643
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   644
class digraph (object):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   645
	"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   646
	Digraph class.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   647
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   648
	Digraphs are built of nodes and directed edges.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   649
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   650
	@sort: __init__, __getitem__, __iter__, __len__, __str__, add_edge, add_edge_attribute,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   651
	add_graph, add_node, add_node_attribute, add_nodes, add_spanning_tree, complete, degree,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   652
	del_edge, del_node, edges, get_edge_attributes, get_edge_label, get_edge_weight,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   653
	get_node_attributes, has_edge, has_node, incidents, inverse, neighbors, nodes, order,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   654
	set_edge_label, set_edge_weight, traversal, generate, read, write, accessibility,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   655
	breadth_first_search, cut_edges, cut_nodes, depth_first_search, minimal_spanning_tree,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   656
	mutual_accessibility, shortest_path, topological_sorting
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   657
	"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   658
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   659
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   660
	def __init__(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   661
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   662
		Initialize a digraph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   663
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   664
		self.node_neighbors = {}	# Pairing: Node -> Neighbors
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   665
		self.edge_properties = {}	# Pairing: Edge -> (Label, Weight)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   666
		self.node_incidence = {}	# Pairing: Node -> Incident nodes
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   667
		self.node_attr = {}			# Pairing: Node -> Attributes
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   668
		self.edge_attr = {}			# Pairing: Edge -> Attributes
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   669
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   670
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   671
	def __str__(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   672
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   673
		Return a string representing the digraph when requested by str() (or print).
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   674
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   675
		@rtype:  string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   676
		@return: String representing the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   677
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   678
		return "<graph object " + str(self.nodes()) + " " + str(self.edges()) + ">"
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   679
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   680
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   681
	def __len__(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   682
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   683
		Return the order of the digraph when requested by len().
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   684
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   685
		@rtype:  number
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   686
		@return: Size of the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   687
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   688
		return len(self.node_neighbors)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   689
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   690
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   691
	def __iter__(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   692
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   693
		Return a iterator passing through all nodes in the digraph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   694
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   695
		@rtype:  iterator
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   696
		@return: Iterator passing through all nodes in the digraph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   697
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   698
		for each in self.node_neighbors.iterkeys():
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   699
			yield each
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   700
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   701
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   702
	def __getitem__(self, node):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   703
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   704
		Return a iterator passing through all neighbors of the given node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   705
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   706
		@rtype:  iterator
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   707
		@return: Iterator passing through all neighbors of the given node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   708
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   709
		for each in self.node_neighbors[node]:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   710
			yield each
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   711
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   712
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   713
	def read(self, string, fmt='xml'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   714
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   715
		Read a graph from a string. Nodes and edges specified in the input will be added to the
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   716
		current graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   717
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   718
		@type  string: string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   719
		@param string: Input string specifying a graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   720
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   721
		@type  fmt: string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   722
		@param fmt: Input format. Possible formats are:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   723
			1. 'xml' - XML (default)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   724
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   725
		if (fmt == 'xml'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   726
			readwrite.read_xml(self, string)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   727
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   728
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   729
	def write(self, fmt='xml'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   730
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   731
		Write the graph to a string. Depending of the output format, this string can be used by
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   732
		read() to rebuild the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   733
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   734
		@type  fmt: string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   735
		@param fmt: Output format. Possible formats are:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   736
			1. 'xml' - XML (default)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   737
			2. 'dot' - DOT Language (for GraphViz)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   738
			3. 'dotwt' - DOT Language with weight information
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   739
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   740
		@rtype:  string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   741
		@return: String specifying the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   742
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   743
		if (fmt == 'xml'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   744
			return readwrite.write_xml(self)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   745
		elif (fmt == 'dot'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   746
			return readwrite.write_dot_digraph(self, False)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   747
		elif (fmt == 'dotwt'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   748
			return readwrite.write_dot_digraph(self, True)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   749
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   750
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   751
	def generate(self, num_nodes, num_edges, weight_range=(1, 1)):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   752
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   753
		Add nodes and random edges to the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   754
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   755
		@type  num_nodes: number
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   756
		@param num_nodes: Number of nodes.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   757
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   758
		@type  num_edges: number
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   759
		@param num_edges: Number of edges.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   760
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   761
		@type  weight_range: tuple
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   762
		@param weight_range: tuple of two integers as lower and upper limits on randomly generated
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   763
		weights (uniform distribution).
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   764
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   765
		generators.generate(self, num_nodes, num_edges, weight_range)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   766
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   767
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   768
	def nodes(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   769
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   770
		Return node list.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   771
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   772
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   773
		@return: Node list.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   774
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   775
		return self.node_neighbors.keys()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   776
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   777
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   778
	def neighbors(self, node):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   779
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   780
		Return all nodes that are directly accessible from given node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   781
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   782
		@type  node: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   783
		@param node: Node identifier
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   784
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   785
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   786
		@return: List of nodes directly accessible from given node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   787
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   788
		return self.node_neighbors[node]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   789
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   790
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   791
	def incidents(self, node):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   792
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   793
		Return all nodes that are incident to the given node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   794
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   795
		@type  node: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   796
		@param node: Node identifier
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   797
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   798
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   799
		@return: List of nodes directly accessible from given node.	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   800
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   801
		return self.node_incidence[node]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   802
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   803
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   804
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   805
	def edges(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   806
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   807
		Return all edges in the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   808
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   809
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   810
		@return: List of all edges in the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   811
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   812
		return self.edge_properties.keys()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   813
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   814
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   815
	def has_node(self, node):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   816
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   817
		Return whether the requested node exists.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   818
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   819
		@type  node: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   820
		@param node: Node identifier
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   821
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   822
		@rtype:  boolean
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   823
		@return: Truth-value for node existence.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   824
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   825
		return self.node_neighbors.has_key(node)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   826
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   827
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   828
	def add_node(self, node, attrs=[]):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   829
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   830
		Add given node to the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   831
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   832
		@attention: While nodes can be of any type, it's strongly recommended to use only numbers
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   833
		and single-line strings as node identifiers if you intend to use write().
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   834
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   835
		@type  node: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   836
		@param node: Node identifier.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   837
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   838
		@type  attrs: list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   839
		@param attrs: List of node attributes specified as (attribute, value) tuples.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   840
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   841
		if (node not in self.node_neighbors):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   842
			self.node_neighbors[node] = []
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   843
			self.node_incidence[node] = []
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   844
			self.node_attr[node] = attrs
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   845
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   846
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   847
	def add_nodes(self, nodelist):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   848
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   849
		Add given nodes to the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   850
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   851
		@attention: While nodes can be of any type, it's strongly recommended to use only numbers
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   852
		and single-line strings as node identifiers if you intend to use write().
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   853
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   854
		@type  nodelist: list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   855
		@param nodelist: List of nodes to be added to the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   856
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   857
		for each in nodelist:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   858
			self.add_node(each)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   859
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   860
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   861
	def add_edge(self, u, v, wt=1, label='', attrs=[]):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   862
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   863
		Add an directed edge (u,v) to the graph connecting nodes u to v.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   864
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   865
		@type  u: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   866
		@param u: One node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   867
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   868
		@type  v: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   869
		@param v: Other node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   870
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   871
		@type  wt: number
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   872
		@param wt: Edge weight.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   873
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   874
		@type  label: string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   875
		@param label: Edge label.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   876
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   877
		@type  attrs: list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   878
		@param attrs: List of node attributes specified as (attribute, value) tuples.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   879
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   880
		if (v not in self.node_neighbors[u]):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   881
			self.node_neighbors[u].append(v)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   882
			self.node_incidence[v].append(u)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   883
			self.edge_properties[(u, v)] = [label, wt]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   884
			self.edge_attr[(u, v)] = attrs
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   885
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   886
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   887
	def del_node(self, node):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   888
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   889
		Remove a node from the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   890
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   891
		@type  node: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   892
		@param node: Node identifier.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   893
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   894
		for each in list(self.incidents(node)):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   895
			self.del_edge(each, node)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   896
		for each in list(self.neighbors(node)):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   897
			self.del_edge(node, each)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   898
		del(self.node_neighbors[node])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   899
		del(self.node_incidence[node])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   900
		del(self.node_attr[node])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   901
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   902
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   903
	def del_edge(self, u, v):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   904
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   905
		Remove an directed edge (u, v) from the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   906
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   907
		@type  u: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   908
		@param u: One node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   909
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   910
		@type  v: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   911
		@param v: Other node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   912
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   913
		self.node_neighbors[u].remove(v)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   914
		self.node_incidence[v].remove(u)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   915
		del(self.edge_properties[(u,v)])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   916
		del(self.edge_attr[(u,v)])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   917
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   918
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   919
	def get_edge_weight(self, u, v):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   920
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   921
		Get the weight of an edge.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   922
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   923
		@type  u: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   924
		@param u: One node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   925
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   926
		@type  v: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   927
		@param v: Other node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   928
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   929
		@rtype:  number
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   930
		@return: Edge weight.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   931
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   932
		return self.edge_properties[(u, v)][1]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   933
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   934
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   935
	def set_edge_weight(self, u, v, wt):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   936
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   937
		Set the weight of an edge.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   938
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   939
		@type  u: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   940
		@param u: One node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   941
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   942
		@type  v: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   943
		@param v: Other node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   944
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   945
		@type  wt: number
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   946
		@param wt: Edge weight.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   947
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   948
		self.edge_properties[(u, v)][1] = wt
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   949
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   950
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   951
	def get_edge_label(self, u, v):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   952
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   953
		Get the label of an edge.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   954
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   955
		@type  u: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   956
		@param u: One node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   957
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   958
		@type  v: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   959
		@param v: Other node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   960
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   961
		@rtype:  string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   962
		@return: Edge label
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   963
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   964
		return self.edge_properties[(u, v)][0]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   965
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   966
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   967
	def set_edge_label(self, u, v, label):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   968
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   969
		Set the label of an edge.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   970
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   971
		@type  u: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   972
		@param u: One node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   973
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   974
		@type  v: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   975
		@param v: Other node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   976
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   977
		@type  label: string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   978
		@param label: Edge label.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   979
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   980
		self.edge_properties[(u, v)][0] = label
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   981
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   982
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   983
	def add_node_attribute(self, node, attr):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   984
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   985
		Add attribute to the given node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   986
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   987
		@type  node: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   988
		@param node: Node identifier
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   989
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   990
		@type  attr: tuple
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   991
		@param attr: Node attribute specified as a tuple in the form (attribute, value).
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   992
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   993
		self.node_attr[node] = self.node_attr[node] + [attr]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   994
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   995
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   996
	def get_node_attributes(self, node):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   997
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   998
		Return the attributes of the given node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   999
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1000
		@type  node: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1001
		@param node: Node identifier
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1002
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1003
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1004
		@return: List of attributes specified tuples in the form (attribute, value).
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1005
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1006
		return self.node_attr[node]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1007
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1008
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1009
	def add_edge_attribute(self, u, v, attr):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1010
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1011
		Add attribute to the given edge.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1012
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1013
		@type  u: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1014
		@param u: One node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1015
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1016
		@type  v: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1017
		@param v: Other node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1018
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1019
		@type  attr: tuple
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1020
		@param attr: Node attribute specified as a tuple in the form (attribute, value).
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1021
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1022
		self.edge_attr[(u,v)] = self.edge_attr[(u,v)] + [attr]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1023
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1024
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1025
	def get_edge_attributes(self, u, v):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1026
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1027
		Return the attributes of the given edge.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1028
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1029
		@type  u: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1030
		@param u: One node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1031
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1032
		@type  v: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1033
		@param v: Other node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1034
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1035
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1036
		@return: List of attributes specified tuples in the form (attribute, value).
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1037
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1038
		return self.edge_attr[(u,v)]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1039
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1040
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1041
	def has_edge(self, u, v):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1042
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1043
		Return whether an edge between nodes u and v exists.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1044
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1045
		@type  u: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1046
		@param u: One node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1047
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1048
		@type  v: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1049
		@param v: Other node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1050
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1051
		@rtype:  boolean
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1052
		@return: Truth-value for edge existence.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1053
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1054
		return self.edge_properties.has_key((u,v))
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1055
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1056
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1057
	def order(self, node):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1058
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1059
		Return the order of the given node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1060
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1061
		@rtype:  number
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1062
		@return: Order of the given node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1063
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1064
		return len(self.neighbors(node))
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1065
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1066
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1067
	def degree(self, node):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1068
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1069
		Return the degree of the given node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1070
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1071
		@rtype:  number
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1072
		@return: Order of the given node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1073
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1074
		return len(self.node_incidence[node])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1075
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1076
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1077
	def complete(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1078
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1079
		Make the graph a complete graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1080
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1081
		@attention: This will modify the current graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1082
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1083
		for each in self.nodes():
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1084
			for other in self.nodes():
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1085
				if (each != other):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1086
					self.add_edge(each, other)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1087
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1088
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1089
	def inverse(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1090
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1091
		Return the inverse of the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1092
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1093
		@rtype:  graph
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1094
		@return: Complement graph for the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1095
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1096
		inv = digraph()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1097
		inv.add_nodes(self.nodes())
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1098
		inv.complete()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1099
		for each in self.edges():
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1100
			inv.del_edge(each[0], each[1])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1101
		return inv
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1102
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1103
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1104
	def add_graph(self, graph):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1105
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1106
		Add other graph to the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1107
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1108
		@attention: Attributes and labels are not preserved.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1109
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1110
		@type  graph: graph
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1111
		@param graph: Graph
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1112
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1113
		self.add_nodes(graph.nodes())
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1114
		for each_node in graph.nodes():
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1115
			for each_edge in graph.neighbors(each_node):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1116
				self.add_edge(each_node, each_edge)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1117
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1118
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1119
	def add_spanning_tree(self, st):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1120
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1121
		Add a spanning tree to the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1122
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1123
		@type  st: dictionary
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1124
		@param st: Spanning tree.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1125
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1126
		self.add_nodes(st.keys())
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1127
		for each in st:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1128
			if (st[each] is not None):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1129
				self.add_edge(st[each], each)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1130
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1131
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1132
	def traversal(self, node, order='pre'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1133
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1134
		Graph traversal iterator.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1135
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1136
		@type  node: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1137
		@param node: Node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1138
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1139
		@type  order: string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1140
		@param order: traversal ordering. Possible values are:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1141
			2. 'pre' - Preordering (default)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1142
			1. 'post' - Postordering
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1143
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1144
		@rtype:  iterator
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1145
		@return: Traversal iterator.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1146
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1147
		for each in traversal.traversal(self, node, order):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1148
			yield each
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1149
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1150
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1151
	def depth_first_search(self, root=None):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1152
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1153
		Depht-first search.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1154
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1155
		@type  root: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1156
		@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
  1157
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1158
		@rtype:  tuple
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1159
		@return:  tupple containing a dictionary and two lists:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1160
			1. Generated spanning tree
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1161
			2. Graph's preordering
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1162
			3. Graph's postordering
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1163
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1164
		return searching.depth_first_search(self, root)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1165
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1166
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1167
	def accessibility(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1168
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1169
		Accessibility matrix (transitive closure).
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1170
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1171
		@rtype:  dictionary
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1172
		@return: Accessibility information for each node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1173
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1174
		return accessibility.accessibility(self)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1175
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1176
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1177
	def breadth_first_search(self, root=None):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1178
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1179
		Breadth-first search.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1180
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1181
		@type  root: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1182
		@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
  1183
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1184
		@rtype:  dictionary
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1185
		@return: A tuple containing a dictionary and a list.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1186
			1. Generated spanning tree
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1187
			2. Graph's level-based ordering
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1188
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1189
		return searching.breadth_first_search(self, root)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1190
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1191
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1192
	def mutual_accessibility(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1193
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1194
		Mutual-accessibility matrix (strongly connected components).
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1195
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1196
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1197
		@return: Mutual-accessibility information for each node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1198
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1199
		return accessibility.mutual_accessibility(self)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1200
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1201
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1202
	def topological_sorting(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1203
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1204
		Topological sorting.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1205
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1206
		@attention: Topological sorting is meaningful only for directed acyclic graphs.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1207
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1208
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1209
		@return: Topological sorting for the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1210
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1211
		return sorting.topological_sorting(self)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1212
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1213
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1214
	def minimal_spanning_tree(self, root=None):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1215
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1216
		Minimal spanning tree.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1217
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1218
		@type  root: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1219
		@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
  1220
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1221
		@attention: Minimal spanning tree meaningful only for weighted graphs.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1222
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1223
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1224
		@return: Generated spanning tree.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1225
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1226
		return minmax.minimal_spanning_tree(self, root)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1227
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1228
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1229
	def shortest_path(self, source):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1230
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1231
		Return the shortest path distance between source node and all other nodes using Dijkstra's
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1232
		algorithm.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1233
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1234
		@attention: All weights must be nonnegative.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1235
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1236
		@type  source: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1237
		@param source: Node from which to start the search.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1238
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1239
		@rtype:  tuple
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1240
		@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
  1241
			1. Shortest path spanning tree
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1242
			2. Shortest distance from given source to each target node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1243
		Inaccessible target nodes do not appear in either dictionary.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1244
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1245
		return minmax.shortest_path(self, source)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1246
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1247
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1248
	def cut_edges(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1249
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1250
		Return the cut-edges of the given graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1251
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1252
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1253
		@return: List of cut-edges.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1254
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1255
		return accessibility.cut_edges(self)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1256
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1257
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1258
	def cut_nodes(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1259
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1260
		Return the cut-nodes of the given graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1261
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1262
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1263
		@return: List of cut-nodes.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1264
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1265
		return accessibility.cut_nodes(self)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1266
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1267
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1268
# Hypergraph class ---------------------------------------------------------------------------------
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1269
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1270
class hypergraph (object):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1271
	"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1272
	Hypergraph class.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1273
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1274
	Hypergraphs are a generalization of graphs where an edge (called hyperedge) can connect more
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1275
	than two nodes.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1276
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1277
	@sort: __init__, __len__, __str__, add_hyperedge, add_hyperedges, add_node,	add_nodes, has_node,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1278
	hyperedges, link, links, nodes, unlink, read, write, accessibility,	connected_components,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1279
	cut_hyperedges, cut_nodes
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1280
	"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1281
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1282
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1283
	def __init__(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1284
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1285
		Initialize a hypergraph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1286
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1287
		self.node_links = {}	# Pairing: Node -> Hyperedge
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1288
		self.edge_links = {} 	# Pairing: Hyperedge -> Node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1289
		self.graph = graph()	# Ordinary graph
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1290
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1291
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1292
	def __str__(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1293
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1294
		Return a string representing the hypergraph when requested by str() (or print).
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1295
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1296
		@rtype:  string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1297
		@return: String representing the hypergraph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1298
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1299
		return "<hypergraph object " + str(self.nodes()) + " " + str(self.edge_links) + ">"
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1300
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1301
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1302
	def __len__(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1303
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1304
		Return the size of the hypergraph when requested by len().
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1305
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1306
		@rtype:  number
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1307
		@return: Size of the hypergraph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1308
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1309
		return len(self.node_links)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1310
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1311
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1312
	def read(self, string, fmt='xml'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1313
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1314
		Read a hypergraph from a string. Nodes and hyperedges specified in the input will be added
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1315
		to the current graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1316
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1317
		@type  string: string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1318
		@param string: Input string specifying a graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1319
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1320
		@type  fmt: string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1321
		@param fmt: Input format. Possible formats are:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1322
			1. 'xml' - XML (default)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1323
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1324
		if (fmt == 'xml'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1325
			readwrite.read_xml_hypergraph(self, string)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1326
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1327
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1328
	def write(self, fmt='xml'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1329
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1330
		Write the hypergraph to a string. Depending of the output format, this string can be used by
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1331
		read() to rebuild the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1332
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1333
		@type  fmt: string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1334
		@param fmt: Output format. Possible formats are:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1335
			1. 'xml' - XML (default)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1336
			2. 'dot' - DOT Language (for GraphViz)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1337
			3. 'dotclr' - DOT Language, coloured
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1338
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1339
		@rtype:  string
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1340
		@return: String specifying the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1341
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1342
		if (fmt == 'xml'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1343
			return readwrite.write_xml_hypergraph(self)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1344
		elif (fmt == 'dot'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1345
			return readwrite.write_dot_hypergraph(self)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1346
		elif (fmt == 'dotclr'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1347
			return readwrite.write_dot_hypergraph(self, coloured=True)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1348
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1349
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1350
	def nodes(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1351
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1352
		Return node list.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1353
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1354
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1355
		@return: Node list.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1356
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1357
		return self.node_links.keys()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1358
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1359
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1360
	def hyperedges(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1361
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1362
		Return hyperedge list.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1363
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1364
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1365
		@return: List of hyperedges linked to the given node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1366
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1367
		return self.edge_links.keys()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1368
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1369
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1370
	def links(self, obj):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1371
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1372
		Return all objects linked to the given one.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1373
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1374
		If given a node, linked hyperedges will be returned. If given a hyperedge, linked nodes will
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1375
		be returned.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1376
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1377
		@type  obj: node or hyperedge
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1378
		@param obj: Object identifier.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1379
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1380
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1381
		@return: List of objects linked to the given one.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1382
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1383
		if (obj in self.node_links):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1384
			return self.node_links[obj]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1385
		else:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1386
			return self.edge_links[obj]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1387
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1388
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1389
	def has_node(self, node):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1390
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1391
		Return whether the requested node exists.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1392
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1393
		@type  node: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1394
		@param node: Node identifier
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1395
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1396
		@rtype:  boolean
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1397
		@return: Truth-value for node existence.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1398
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1399
		return self.node_links.has_key(node)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1400
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1401
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1402
	def add_node(self, node):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1403
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1404
		Add given node to the hypergraph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1405
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1406
		@attention: While nodes can be of any type, it's strongly recommended to use only numbers
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1407
		and single-line strings as node identifiers if you intend to use write().
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1408
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1409
		@type  node: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1410
		@param node: Node identifier.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1411
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1412
		if (not node in self.node_links):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1413
			self.node_links[node] = []
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1414
			self.graph.add_node((node,'n'))
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1415
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1416
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1417
	def add_nodes(self, nodelist):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1418
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1419
		Add given nodes to the hypergraph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1420
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1421
		@attention: While nodes can be of any type, it's strongly recommended to use only numbers
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1422
		and single-line strings as node identifiers if you intend to use write().
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1423
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1424
		@type  nodelist: list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1425
		@param nodelist: List of nodes to be added to the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1426
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1427
		for each in nodelist:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1428
			self.add_node(each)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1429
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1430
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1431
	def add_hyperedge(self, hyperedge):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1432
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1433
		Add given hyperedge to the hypergraph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1434
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1435
		@attention: While hyperedge-nodes can be of any type, it's strongly recommended to use only
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1436
		numbers and single-line strings as node identifiers if you intend to use write().
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1437
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1438
		@type  hyperedge: hyperedge
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1439
		@param hyperedge: Hyperedge identifier.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1440
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1441
		if (not hyperedge in self.edge_links):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1442
			self.edge_links[hyperedge] = []
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1443
			self.graph.add_node((hyperedge,'h'))
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1444
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1445
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1446
	def add_hyperedges(self, edgelist):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1447
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1448
		Add given hyperedges to the hypergraph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1449
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1450
		@attention: While hyperedge-nodes can be of any type, it's strongly recommended to use only
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1451
		numbers and single-line strings as node identifiers if you intend to use write().
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1452
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1453
		@type  edgelist: list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1454
		@param edgelist: List of hyperedge-nodes to be added to the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1455
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1456
		for each in edgelist:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1457
			self.add_hyperedge(each)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1458
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1459
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1460
	def link(self, node, hyperedge):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1461
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1462
		Link given node and hyperedge.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1463
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1464
		@type  node: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1465
		@param node: Node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1466
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1467
		@type  hyperedge: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1468
		@param hyperedge: Hyperedge.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1469
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1470
		if (hyperedge not in self.node_links[node]):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1471
			self.node_links[node].append(hyperedge)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1472
			self.edge_links[hyperedge].append(node)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1473
			self.graph.add_edge((node,'n'), (hyperedge,'h'))
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1474
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1475
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1476
	def unlink(self, node, hyperedge):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1477
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1478
		Unlink given node and hyperedge.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1479
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1480
		@type  node: node
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1481
		@param node: Node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1482
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1483
		@type  hyperedge: hyperedge
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1484
		@param hyperedge: Hyperedge.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1485
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1486
		self.node_links[node].remove(hyperedge)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1487
		self.edge_links[hyperedge].remove(node)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1488
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1489
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1490
	def accessibility(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1491
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1492
		Accessibility matrix (transitive closure).
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1493
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1494
		@rtype:  dictionary
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1495
		@return: Accessibility information for each node.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1496
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1497
		access_ = accessibility.accessibility(self.graph)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1498
		access = {}
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1499
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1500
		for each in access_.keys():
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1501
			if (each[1] == 'n'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1502
				access[each[0]] = []
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1503
				for other in access_[each]:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1504
					if (other[1] == 'n'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1505
						access[each[0]].append(other[0])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1506
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1507
		return access
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1508
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1509
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1510
	def connected_components(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1511
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1512
		Connected components.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1513
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1514
		@rtype:  dictionary
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1515
		@return: Pairing that associates each node to its connected component.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1516
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1517
		components_ = accessibility.connected_components(self.graph)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1518
		components = {}
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1519
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1520
		for each in components_.keys():
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1521
			if (each[1] == 'n'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1522
				components[each[0]] = components_[each]
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1523
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1524
		return components
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1525
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1526
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1527
	def cut_nodes(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1528
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1529
		Return the cut-nodes of the given hypergraph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1530
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1531
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1532
		@return: List of cut-nodes.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1533
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1534
		cut_nodes_ = accessibility.cut_nodes(self.graph)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1535
		cut_nodes = []
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1536
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1537
		for each in cut_nodes_:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1538
			if (each[1] == 'n'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1539
				cut_nodes.append(each[0])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1540
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1541
		return cut_nodes
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1542
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1543
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1544
	def cut_hyperedges(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1545
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1546
		Return the cut-hyperedges of the given hypergraph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1547
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1548
		@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1549
		@return: List of cut-nodes.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1550
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1551
		cut_nodes_ = accessibility.cut_nodes(self.graph)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1552
		cut_nodes = []
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1553
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1554
		for each in cut_nodes_:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1555
			if (each[1] == 'h'):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1556
				cut_nodes.append(each[0])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1557
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1558
		return cut_nodes
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1559
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1560
	def rank(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1561
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1562
		Return the rank of the given hypergraph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1563
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1564
		@rtype:  int
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1565
		@return: Rank of graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1566
		"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1567
		max_rank = 0
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1568
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1569
		for each in hyperedges:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1570
			if len(each) > max_rank:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1571
				max_rank = len(each)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1572
				
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
  1573
		return max_rank