| 1 |
""" |
| 2 |
altgraph.GraphUtil - Utility classes and functions |
| 3 |
================================================== |
| 4 |
""" |
| 5 |
|
| 6 |
import random |
| 7 |
from collections import deque |
| 8 |
|
| 9 |
from altgraph import Graph, GraphError |
| 10 |
|
| 11 |
|
| 12 |
def generate_random_graph(node_num, edge_num, self_loops=False, multi_edges=False): |
| 13 |
""" |
| 14 |
Generates and returns a :py:class:`~altgraph.Graph.Graph` instance with |
| 15 |
*node_num* nodes randomly connected by *edge_num* edges. |
| 16 |
""" |
| 17 |
g = Graph.Graph() |
| 18 |
|
| 19 |
if not multi_edges: |
| 20 |
if self_loops: |
| 21 |
max_edges = node_num * node_num |
| 22 |
else: |
| 23 |
max_edges = node_num * (node_num - 1) |
| 24 |
|
| 25 |
if edge_num > max_edges: |
| 26 |
raise GraphError("inconsistent arguments to 'generate_random_graph'") |
| 27 |
|
| 28 |
nodes = range(node_num) |
| 29 |
|
| 30 |
for node in nodes: |
| 31 |
g.add_node(node) |
| 32 |
|
| 33 |
while 1: |
| 34 |
head = random.choice(nodes) |
| 35 |
tail = random.choice(nodes) |
| 36 |
|
| 37 |
|
| 38 |
if head == tail and not self_loops: |
| 39 |
continue |
| 40 |
|
| 41 |
|
| 42 |
if g.edge_by_node(head, tail) is not None and not multi_edges: |
| 43 |
continue |
| 44 |
|
| 45 |
|
| 46 |
g.add_edge(head, tail) |
| 47 |
if g.number_of_edges() >= edge_num: |
| 48 |
break |
| 49 |
|
| 50 |
return g |
| 51 |
|
| 52 |
|
| 53 |
def generate_scale_free_graph(steps, growth_num, self_loops=False, multi_edges=False): |
| 54 |
""" |
| 55 |
Generates and returns a :py:class:`~altgraph.Graph.Graph` instance that |
| 56 |
will have *steps* \\* *growth_num* nodes and a scale free (powerlaw) |
| 57 |
connectivity. Starting with a fully connected graph with *growth_num* |
| 58 |
nodes at every step *growth_num* nodes are added to the graph and are |
| 59 |
connected to existing nodes with a probability proportional to the degree |
| 60 |
of these existing nodes. |
| 61 |
""" |
| 62 |
|
| 63 |
graph = Graph.Graph() |
| 64 |
|
| 65 |
|
| 66 |
store = [] |
| 67 |
for i in range(growth_num): |
| 68 |
for j in range(i + 1, growth_num): |
| 69 |
store.append(i) |
| 70 |
store.append(j) |
| 71 |
graph.add_edge(i, j) |
| 72 |
|
| 73 |
|
| 74 |
for node in range(growth_num, steps * growth_num): |
| 75 |
graph.add_node(node) |
| 76 |
while graph.out_degree(node) < growth_num: |
| 77 |
nbr = random.choice(store) |
| 78 |
|
| 79 |
|
| 80 |
if node == nbr and not self_loops: |
| 81 |
continue |
| 82 |
|
| 83 |
|
| 84 |
if graph.edge_by_node(node, nbr) and not multi_edges: |
| 85 |
continue |
| 86 |
|
| 87 |
graph.add_edge(node, nbr) |
| 88 |
|
| 89 |
for nbr in graph.out_nbrs(node): |
| 90 |
store.append(node) |
| 91 |
store.append(nbr) |
| 92 |
|
| 93 |
return graph |
| 94 |
|
| 95 |
|
| 96 |
def filter_stack(graph, head, filters): |
| 97 |
""" |
| 98 |
Perform a walk in a depth-first order starting |
| 99 |
at *head*. |
| 100 |
|
| 101 |
Returns (visited, removes, orphans). |
| 102 |
|
| 103 |
* visited: the set of visited nodes |
| 104 |
* removes: the list of nodes where the node |
| 105 |
data does not all *filters* |
| 106 |
* orphans: tuples of (last_good, node), |
| 107 |
where node is not in removes, is directly |
| 108 |
reachable from a node in *removes* and |
| 109 |
*last_good* is the closest upstream node that is not |
| 110 |
in *removes*. |
| 111 |
""" |
| 112 |
|
| 113 |
visited, removes, orphans = {head}, set(), set() |
| 114 |
stack = deque([(head, head)]) |
| 115 |
get_data = graph.node_data |
| 116 |
get_edges = graph.out_edges |
| 117 |
get_tail = graph.tail |
| 118 |
|
| 119 |
while stack: |
| 120 |
last_good, node = stack.pop() |
| 121 |
data = get_data(node) |
| 122 |
if data is not None: |
| 123 |
for filtfunc in filters: |
| 124 |
if not filtfunc(data): |
| 125 |
removes.add(node) |
| 126 |
break |
| 127 |
else: |
| 128 |
last_good = node |
| 129 |
for edge in get_edges(node): |
| 130 |
tail = get_tail(edge) |
| 131 |
if last_good is not node: |
| 132 |
orphans.add((last_good, tail)) |
| 133 |
if tail not in visited: |
| 134 |
visited.add(tail) |
| 135 |
stack.append((last_good, tail)) |
| 136 |
|
| 137 |
orphans = [(lg, tl) for (lg, tl) in orphans if tl not in removes] |
| 138 |
|
| 139 |
return visited, removes, orphans |