/usr/lib/python3/dist-packages/pygraph/algorithms/generators.py is in python3-pygraph 1.8.2-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | # Copyright (c) 2008-2009 Pedro Matiello <pmatiello@gmail.com>
# Zsolt Haraszti <zsolt@drawwell.net>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
"""
Random graph generators.
@sort: generate, generate_hypergraph
"""
# Imports
from pygraph.classes.graph import graph
from pygraph.classes.digraph import digraph
from pygraph.classes.hypergraph import hypergraph
from random import randint, choice, shuffle #@UnusedImport
from time import time
# Generator
def generate(num_nodes, num_edges, directed=False, weight_range=(1, 1)):
"""
Create a random graph.
@type num_nodes: number
@param num_nodes: Number of nodes.
@type num_edges: number
@param num_edges: Number of edges.
@type directed: bool
@param directed: Whether the generated graph should be directed or not.
@type weight_range: tuple
@param weight_range: tuple of two integers as lower and upper limits on randomly generated
weights (uniform distribution).
"""
# Graph creation
if directed:
random_graph = digraph()
else:
random_graph = graph()
# Nodes
nodes = range(num_nodes)
random_graph.add_nodes(nodes)
# Build a list of all possible edges
edges = []
edges_append = edges.append
for x in nodes:
for y in nodes:
if ((directed and x != y) or (x > y)):
edges_append((x, y))
# Randomize the list
shuffle(edges)
# Add edges to the graph
min_wt = min(weight_range)
max_wt = max(weight_range)
for i in range(num_edges):
each = edges[i]
random_graph.add_edge((each[0], each[1]), wt = randint(min_wt, max_wt))
return random_graph
def generate_hypergraph(num_nodes, num_edges, r = 0):
"""
Create a random hyper graph.
@type num_nodes: number
@param num_nodes: Number of nodes.
@type num_edges: number
@param num_edges: Number of edges.
@type r: number
@param r: Uniform edges of size r.
"""
# Graph creation
random_graph = hypergraph()
# Nodes
nodes = list(map(str, list(range(num_nodes))))
random_graph.add_nodes(nodes)
# Base edges
edges = list(map(str, list(range(num_nodes, num_nodes+num_edges))))
random_graph.add_hyperedges(edges)
# Connect the edges
if 0 == r:
# Add each edge with 50/50 probability
for e in edges:
for n in nodes:
if choice([True, False]):
random_graph.link(n, e)
else:
# Add only uniform edges
for e in edges:
# First shuffle the nodes
shuffle(nodes)
# Then take the first r nodes
for i in range(r):
random_graph.link(nodes[i], e)
return random_graph
|