app/graph/generators.py
changeset 594 06c2228e39cb
equal deleted inserted replaced
593:01f8c7aabb7e 594:06c2228e39cb
       
     1 # Copyright (c) 2007-2008 Pedro Matiello <pmatiello@gmail.com>
       
     2 #                         Zsolt Haraszti <zsolt@drawwell.net>
       
     3 #
       
     4 # Permission is hereby granted, free of charge, to any person
       
     5 # obtaining a copy of this software and associated documentation
       
     6 # files (the "Software"), to deal in the Software without
       
     7 # restriction, including without limitation the rights to use,
       
     8 # copy, modify, merge, publish, distribute, sublicense, and/or sell
       
     9 # copies of the Software, and to permit persons to whom the
       
    10 # Software is furnished to do so, subject to the following
       
    11 # conditions:
       
    12 
       
    13 # The above copyright notice and this permission notice shall be
       
    14 # included in all copies or substantial portions of the Software.
       
    15 
       
    16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
       
    17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
       
    18 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
       
    19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
       
    20 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
       
    21 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
       
    22 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
       
    23 # OTHER DEALINGS IN THE SOFTWARE.
       
    24 
       
    25 
       
    26 """
       
    27 Random graph generators for python-graph.
       
    28 
       
    29 @sort: generate
       
    30 """
       
    31 
       
    32 
       
    33 # Imports
       
    34 import graph as classes
       
    35 from random import randint
       
    36 
       
    37 
       
    38 # Generator
       
    39 
       
    40 def generate(graph, num_nodes, num_edges, weight_range=(1, 1)):
       
    41 	"""
       
    42 	Add nodes and random edges to the graph.
       
    43 	
       
    44 	@type  graph: graph
       
    45 	@param graph: Graph.
       
    46 	
       
    47 	@type  num_nodes: number
       
    48 	@param num_nodes: Number of nodes.
       
    49 	
       
    50 	@type  num_edges: number
       
    51 	@param num_edges: Number of edges.
       
    52 
       
    53 	@type  weight_range: tuple
       
    54 	@param weight_range: tuple of two integers as lower and upper limits on randomly generated
       
    55 	weights (uniform distribution).
       
    56 	"""
       
    57 	# Discover if graph is directed or not
       
    58  	directed = (type(graph) == classes.digraph)
       
    59 
       
    60 	# Nodes first
       
    61 	nodes = xrange(num_nodes)
       
    62 	graph.add_nodes(nodes)
       
    63 	
       
    64 	# Build a list of all possible edges
       
    65 	edges = []
       
    66         edges_append = edges.append
       
    67 	for x in nodes:
       
    68 		for y in nodes:
       
    69 			if ((directed and x != y) or (x > y)):
       
    70 				edges_append((x, y))
       
    71 	
       
    72 	# Randomize the list
       
    73 	for i in xrange(len(edges)):
       
    74 		r = randint(0, len(edges)-1)
       
    75 		edges[i], edges[r] = edges[r], edges[i]
       
    76 	
       
    77 		# Add edges to the graph
       
    78 		min_wt = min(weight_range)
       
    79 		max_wt = max(weight_range)
       
    80 	for i in xrange(num_edges):
       
    81 		each = edges[i]
       
    82 		graph.add_edge(each[0], each[1], wt = randint(min_wt, max_wt))