Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- wemm/lib/python3.10/site-packages/botocore/data/forecastquery/2018-06-26/endpoint-rule-set-1.json.gz +3 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/flow/__pycache__/mincost.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/flow/capacityscaling.py +407 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/flow/tests/__pycache__/test_gomory_hu.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/traversal/edgebfs.py +178 -0
- wemm/lib/python3.10/site-packages/networkx/drawing/__pycache__/nx_pylab.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/__pycache__/__init__.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/__pycache__/atlas.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/__pycache__/classic.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/__pycache__/degree_seq.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/__pycache__/geometric.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/__pycache__/random_clustered.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/__pycache__/random_graphs.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/__pycache__/small.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/__pycache__/spectral_graph_forge.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/cographs.py +68 -0
- wemm/lib/python3.10/site-packages/networkx/generators/duplication.py +174 -0
- wemm/lib/python3.10/site-packages/networkx/generators/ego.py +66 -0
- wemm/lib/python3.10/site-packages/networkx/generators/internet_as_graphs.py +441 -0
- wemm/lib/python3.10/site-packages/networkx/generators/interval_graph.py +70 -0
- wemm/lib/python3.10/site-packages/networkx/generators/lattice.py +367 -0
- wemm/lib/python3.10/site-packages/networkx/generators/nonisomorphic_trees.py +212 -0
- wemm/lib/python3.10/site-packages/networkx/generators/random_clustered.py +117 -0
- wemm/lib/python3.10/site-packages/networkx/generators/social.py +554 -0
- wemm/lib/python3.10/site-packages/networkx/generators/stochastic.py +54 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_atlas.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_cographs.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_degree_seq.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_duplication.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_expanders.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_geometric.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_harary_graph.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_internet_as_graphs.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_intersection.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_interval_graph.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_joint_degree_seq.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_lattice.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_line.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_mycielski.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_random_clustered.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_random_graphs.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_small.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_sudoku.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_time_series.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_trees.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/test_atlas.py +75 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/test_classic.py +640 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/test_ego.py +39 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/test_expanders.py +162 -0
- wemm/lib/python3.10/site-packages/networkx/generators/tests/test_geometric.py +488 -0
wemm/lib/python3.10/site-packages/botocore/data/forecastquery/2018-06-26/endpoint-rule-set-1.json.gz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2dc67e787a865b46d80969022aad9aaa54c4caac3e024949f04c45f4009fbfaf
|
| 3 |
+
size 1151
|
wemm/lib/python3.10/site-packages/networkx/algorithms/flow/__pycache__/mincost.cpython-310.pyc
ADDED
|
Binary file (13.1 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/flow/capacityscaling.py
ADDED
|
@@ -0,0 +1,407 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Capacity scaling minimum cost flow algorithm.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
__all__ = ["capacity_scaling"]
|
| 6 |
+
|
| 7 |
+
from itertools import chain
|
| 8 |
+
from math import log
|
| 9 |
+
|
| 10 |
+
import networkx as nx
|
| 11 |
+
|
| 12 |
+
from ...utils import BinaryHeap, arbitrary_element, not_implemented_for
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def _detect_unboundedness(R):
|
| 16 |
+
"""Detect infinite-capacity negative cycles."""
|
| 17 |
+
G = nx.DiGraph()
|
| 18 |
+
G.add_nodes_from(R)
|
| 19 |
+
|
| 20 |
+
# Value simulating infinity.
|
| 21 |
+
inf = R.graph["inf"]
|
| 22 |
+
# True infinity.
|
| 23 |
+
f_inf = float("inf")
|
| 24 |
+
for u in R:
|
| 25 |
+
for v, e in R[u].items():
|
| 26 |
+
# Compute the minimum weight of infinite-capacity (u, v) edges.
|
| 27 |
+
w = f_inf
|
| 28 |
+
for k, e in e.items():
|
| 29 |
+
if e["capacity"] == inf:
|
| 30 |
+
w = min(w, e["weight"])
|
| 31 |
+
if w != f_inf:
|
| 32 |
+
G.add_edge(u, v, weight=w)
|
| 33 |
+
|
| 34 |
+
if nx.negative_edge_cycle(G):
|
| 35 |
+
raise nx.NetworkXUnbounded(
|
| 36 |
+
"Negative cost cycle of infinite capacity found. "
|
| 37 |
+
"Min cost flow may be unbounded below."
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
@not_implemented_for("undirected")
|
| 42 |
+
def _build_residual_network(G, demand, capacity, weight):
|
| 43 |
+
"""Build a residual network and initialize a zero flow."""
|
| 44 |
+
if sum(G.nodes[u].get(demand, 0) for u in G) != 0:
|
| 45 |
+
raise nx.NetworkXUnfeasible("Sum of the demands should be 0.")
|
| 46 |
+
|
| 47 |
+
R = nx.MultiDiGraph()
|
| 48 |
+
R.add_nodes_from(
|
| 49 |
+
(u, {"excess": -G.nodes[u].get(demand, 0), "potential": 0}) for u in G
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
inf = float("inf")
|
| 53 |
+
# Detect selfloops with infinite capacities and negative weights.
|
| 54 |
+
for u, v, e in nx.selfloop_edges(G, data=True):
|
| 55 |
+
if e.get(weight, 0) < 0 and e.get(capacity, inf) == inf:
|
| 56 |
+
raise nx.NetworkXUnbounded(
|
| 57 |
+
"Negative cost cycle of infinite capacity found. "
|
| 58 |
+
"Min cost flow may be unbounded below."
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# Extract edges with positive capacities. Self loops excluded.
|
| 62 |
+
if G.is_multigraph():
|
| 63 |
+
edge_list = [
|
| 64 |
+
(u, v, k, e)
|
| 65 |
+
for u, v, k, e in G.edges(data=True, keys=True)
|
| 66 |
+
if u != v and e.get(capacity, inf) > 0
|
| 67 |
+
]
|
| 68 |
+
else:
|
| 69 |
+
edge_list = [
|
| 70 |
+
(u, v, 0, e)
|
| 71 |
+
for u, v, e in G.edges(data=True)
|
| 72 |
+
if u != v and e.get(capacity, inf) > 0
|
| 73 |
+
]
|
| 74 |
+
# Simulate infinity with the larger of the sum of absolute node imbalances
|
| 75 |
+
# the sum of finite edge capacities or any positive value if both sums are
|
| 76 |
+
# zero. This allows the infinite-capacity edges to be distinguished for
|
| 77 |
+
# unboundedness detection and directly participate in residual capacity
|
| 78 |
+
# calculation.
|
| 79 |
+
inf = (
|
| 80 |
+
max(
|
| 81 |
+
sum(abs(R.nodes[u]["excess"]) for u in R),
|
| 82 |
+
2
|
| 83 |
+
* sum(
|
| 84 |
+
e[capacity]
|
| 85 |
+
for u, v, k, e in edge_list
|
| 86 |
+
if capacity in e and e[capacity] != inf
|
| 87 |
+
),
|
| 88 |
+
)
|
| 89 |
+
or 1
|
| 90 |
+
)
|
| 91 |
+
for u, v, k, e in edge_list:
|
| 92 |
+
r = min(e.get(capacity, inf), inf)
|
| 93 |
+
w = e.get(weight, 0)
|
| 94 |
+
# Add both (u, v) and (v, u) into the residual network marked with the
|
| 95 |
+
# original key. (key[1] == True) indicates the (u, v) is in the
|
| 96 |
+
# original network.
|
| 97 |
+
R.add_edge(u, v, key=(k, True), capacity=r, weight=w, flow=0)
|
| 98 |
+
R.add_edge(v, u, key=(k, False), capacity=0, weight=-w, flow=0)
|
| 99 |
+
|
| 100 |
+
# Record the value simulating infinity.
|
| 101 |
+
R.graph["inf"] = inf
|
| 102 |
+
|
| 103 |
+
_detect_unboundedness(R)
|
| 104 |
+
|
| 105 |
+
return R
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def _build_flow_dict(G, R, capacity, weight):
|
| 109 |
+
"""Build a flow dictionary from a residual network."""
|
| 110 |
+
inf = float("inf")
|
| 111 |
+
flow_dict = {}
|
| 112 |
+
if G.is_multigraph():
|
| 113 |
+
for u in G:
|
| 114 |
+
flow_dict[u] = {}
|
| 115 |
+
for v, es in G[u].items():
|
| 116 |
+
flow_dict[u][v] = {
|
| 117 |
+
# Always saturate negative selfloops.
|
| 118 |
+
k: (
|
| 119 |
+
0
|
| 120 |
+
if (
|
| 121 |
+
u != v or e.get(capacity, inf) <= 0 or e.get(weight, 0) >= 0
|
| 122 |
+
)
|
| 123 |
+
else e[capacity]
|
| 124 |
+
)
|
| 125 |
+
for k, e in es.items()
|
| 126 |
+
}
|
| 127 |
+
for v, es in R[u].items():
|
| 128 |
+
if v in flow_dict[u]:
|
| 129 |
+
flow_dict[u][v].update(
|
| 130 |
+
(k[0], e["flow"]) for k, e in es.items() if e["flow"] > 0
|
| 131 |
+
)
|
| 132 |
+
else:
|
| 133 |
+
for u in G:
|
| 134 |
+
flow_dict[u] = {
|
| 135 |
+
# Always saturate negative selfloops.
|
| 136 |
+
v: (
|
| 137 |
+
0
|
| 138 |
+
if (u != v or e.get(capacity, inf) <= 0 or e.get(weight, 0) >= 0)
|
| 139 |
+
else e[capacity]
|
| 140 |
+
)
|
| 141 |
+
for v, e in G[u].items()
|
| 142 |
+
}
|
| 143 |
+
flow_dict[u].update(
|
| 144 |
+
(v, e["flow"])
|
| 145 |
+
for v, es in R[u].items()
|
| 146 |
+
for e in es.values()
|
| 147 |
+
if e["flow"] > 0
|
| 148 |
+
)
|
| 149 |
+
return flow_dict
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
@nx._dispatchable(
|
| 153 |
+
node_attrs="demand", edge_attrs={"capacity": float("inf"), "weight": 0}
|
| 154 |
+
)
|
| 155 |
+
def capacity_scaling(
|
| 156 |
+
G, demand="demand", capacity="capacity", weight="weight", heap=BinaryHeap
|
| 157 |
+
):
|
| 158 |
+
r"""Find a minimum cost flow satisfying all demands in digraph G.
|
| 159 |
+
|
| 160 |
+
This is a capacity scaling successive shortest augmenting path algorithm.
|
| 161 |
+
|
| 162 |
+
G is a digraph with edge costs and capacities and in which nodes
|
| 163 |
+
have demand, i.e., they want to send or receive some amount of
|
| 164 |
+
flow. A negative demand means that the node wants to send flow, a
|
| 165 |
+
positive demand means that the node want to receive flow. A flow on
|
| 166 |
+
the digraph G satisfies all demand if the net flow into each node
|
| 167 |
+
is equal to the demand of that node.
|
| 168 |
+
|
| 169 |
+
Parameters
|
| 170 |
+
----------
|
| 171 |
+
G : NetworkX graph
|
| 172 |
+
DiGraph or MultiDiGraph on which a minimum cost flow satisfying all
|
| 173 |
+
demands is to be found.
|
| 174 |
+
|
| 175 |
+
demand : string
|
| 176 |
+
Nodes of the graph G are expected to have an attribute demand
|
| 177 |
+
that indicates how much flow a node wants to send (negative
|
| 178 |
+
demand) or receive (positive demand). Note that the sum of the
|
| 179 |
+
demands should be 0 otherwise the problem in not feasible. If
|
| 180 |
+
this attribute is not present, a node is considered to have 0
|
| 181 |
+
demand. Default value: 'demand'.
|
| 182 |
+
|
| 183 |
+
capacity : string
|
| 184 |
+
Edges of the graph G are expected to have an attribute capacity
|
| 185 |
+
that indicates how much flow the edge can support. If this
|
| 186 |
+
attribute is not present, the edge is considered to have
|
| 187 |
+
infinite capacity. Default value: 'capacity'.
|
| 188 |
+
|
| 189 |
+
weight : string
|
| 190 |
+
Edges of the graph G are expected to have an attribute weight
|
| 191 |
+
that indicates the cost incurred by sending one unit of flow on
|
| 192 |
+
that edge. If not present, the weight is considered to be 0.
|
| 193 |
+
Default value: 'weight'.
|
| 194 |
+
|
| 195 |
+
heap : class
|
| 196 |
+
Type of heap to be used in the algorithm. It should be a subclass of
|
| 197 |
+
:class:`MinHeap` or implement a compatible interface.
|
| 198 |
+
|
| 199 |
+
If a stock heap implementation is to be used, :class:`BinaryHeap` is
|
| 200 |
+
recommended over :class:`PairingHeap` for Python implementations without
|
| 201 |
+
optimized attribute accesses (e.g., CPython) despite a slower
|
| 202 |
+
asymptotic running time. For Python implementations with optimized
|
| 203 |
+
attribute accesses (e.g., PyPy), :class:`PairingHeap` provides better
|
| 204 |
+
performance. Default value: :class:`BinaryHeap`.
|
| 205 |
+
|
| 206 |
+
Returns
|
| 207 |
+
-------
|
| 208 |
+
flowCost : integer
|
| 209 |
+
Cost of a minimum cost flow satisfying all demands.
|
| 210 |
+
|
| 211 |
+
flowDict : dictionary
|
| 212 |
+
If G is a digraph, a dict-of-dicts keyed by nodes such that
|
| 213 |
+
flowDict[u][v] is the flow on edge (u, v).
|
| 214 |
+
If G is a MultiDiGraph, a dict-of-dicts-of-dicts keyed by nodes
|
| 215 |
+
so that flowDict[u][v][key] is the flow on edge (u, v, key).
|
| 216 |
+
|
| 217 |
+
Raises
|
| 218 |
+
------
|
| 219 |
+
NetworkXError
|
| 220 |
+
This exception is raised if the input graph is not directed,
|
| 221 |
+
not connected.
|
| 222 |
+
|
| 223 |
+
NetworkXUnfeasible
|
| 224 |
+
This exception is raised in the following situations:
|
| 225 |
+
|
| 226 |
+
* The sum of the demands is not zero. Then, there is no
|
| 227 |
+
flow satisfying all demands.
|
| 228 |
+
* There is no flow satisfying all demand.
|
| 229 |
+
|
| 230 |
+
NetworkXUnbounded
|
| 231 |
+
This exception is raised if the digraph G has a cycle of
|
| 232 |
+
negative cost and infinite capacity. Then, the cost of a flow
|
| 233 |
+
satisfying all demands is unbounded below.
|
| 234 |
+
|
| 235 |
+
Notes
|
| 236 |
+
-----
|
| 237 |
+
This algorithm does not work if edge weights are floating-point numbers.
|
| 238 |
+
|
| 239 |
+
See also
|
| 240 |
+
--------
|
| 241 |
+
:meth:`network_simplex`
|
| 242 |
+
|
| 243 |
+
Examples
|
| 244 |
+
--------
|
| 245 |
+
A simple example of a min cost flow problem.
|
| 246 |
+
|
| 247 |
+
>>> G = nx.DiGraph()
|
| 248 |
+
>>> G.add_node("a", demand=-5)
|
| 249 |
+
>>> G.add_node("d", demand=5)
|
| 250 |
+
>>> G.add_edge("a", "b", weight=3, capacity=4)
|
| 251 |
+
>>> G.add_edge("a", "c", weight=6, capacity=10)
|
| 252 |
+
>>> G.add_edge("b", "d", weight=1, capacity=9)
|
| 253 |
+
>>> G.add_edge("c", "d", weight=2, capacity=5)
|
| 254 |
+
>>> flowCost, flowDict = nx.capacity_scaling(G)
|
| 255 |
+
>>> flowCost
|
| 256 |
+
24
|
| 257 |
+
>>> flowDict
|
| 258 |
+
{'a': {'b': 4, 'c': 1}, 'd': {}, 'b': {'d': 4}, 'c': {'d': 1}}
|
| 259 |
+
|
| 260 |
+
It is possible to change the name of the attributes used for the
|
| 261 |
+
algorithm.
|
| 262 |
+
|
| 263 |
+
>>> G = nx.DiGraph()
|
| 264 |
+
>>> G.add_node("p", spam=-4)
|
| 265 |
+
>>> G.add_node("q", spam=2)
|
| 266 |
+
>>> G.add_node("a", spam=-2)
|
| 267 |
+
>>> G.add_node("d", spam=-1)
|
| 268 |
+
>>> G.add_node("t", spam=2)
|
| 269 |
+
>>> G.add_node("w", spam=3)
|
| 270 |
+
>>> G.add_edge("p", "q", cost=7, vacancies=5)
|
| 271 |
+
>>> G.add_edge("p", "a", cost=1, vacancies=4)
|
| 272 |
+
>>> G.add_edge("q", "d", cost=2, vacancies=3)
|
| 273 |
+
>>> G.add_edge("t", "q", cost=1, vacancies=2)
|
| 274 |
+
>>> G.add_edge("a", "t", cost=2, vacancies=4)
|
| 275 |
+
>>> G.add_edge("d", "w", cost=3, vacancies=4)
|
| 276 |
+
>>> G.add_edge("t", "w", cost=4, vacancies=1)
|
| 277 |
+
>>> flowCost, flowDict = nx.capacity_scaling(
|
| 278 |
+
... G, demand="spam", capacity="vacancies", weight="cost"
|
| 279 |
+
... )
|
| 280 |
+
>>> flowCost
|
| 281 |
+
37
|
| 282 |
+
>>> flowDict
|
| 283 |
+
{'p': {'q': 2, 'a': 2}, 'q': {'d': 1}, 'a': {'t': 4}, 'd': {'w': 2}, 't': {'q': 1, 'w': 1}, 'w': {}}
|
| 284 |
+
"""
|
| 285 |
+
R = _build_residual_network(G, demand, capacity, weight)
|
| 286 |
+
|
| 287 |
+
inf = float("inf")
|
| 288 |
+
# Account cost of negative selfloops.
|
| 289 |
+
flow_cost = sum(
|
| 290 |
+
0
|
| 291 |
+
if e.get(capacity, inf) <= 0 or e.get(weight, 0) >= 0
|
| 292 |
+
else e[capacity] * e[weight]
|
| 293 |
+
for u, v, e in nx.selfloop_edges(G, data=True)
|
| 294 |
+
)
|
| 295 |
+
|
| 296 |
+
# Determine the maximum edge capacity.
|
| 297 |
+
wmax = max(chain([-inf], (e["capacity"] for u, v, e in R.edges(data=True))))
|
| 298 |
+
if wmax == -inf:
|
| 299 |
+
# Residual network has no edges.
|
| 300 |
+
return flow_cost, _build_flow_dict(G, R, capacity, weight)
|
| 301 |
+
|
| 302 |
+
R_nodes = R.nodes
|
| 303 |
+
R_succ = R.succ
|
| 304 |
+
|
| 305 |
+
delta = 2 ** int(log(wmax, 2))
|
| 306 |
+
while delta >= 1:
|
| 307 |
+
# Saturate Δ-residual edges with negative reduced costs to achieve
|
| 308 |
+
# Δ-optimality.
|
| 309 |
+
for u in R:
|
| 310 |
+
p_u = R_nodes[u]["potential"]
|
| 311 |
+
for v, es in R_succ[u].items():
|
| 312 |
+
for k, e in es.items():
|
| 313 |
+
flow = e["capacity"] - e["flow"]
|
| 314 |
+
if e["weight"] - p_u + R_nodes[v]["potential"] < 0:
|
| 315 |
+
flow = e["capacity"] - e["flow"]
|
| 316 |
+
if flow >= delta:
|
| 317 |
+
e["flow"] += flow
|
| 318 |
+
R_succ[v][u][(k[0], not k[1])]["flow"] -= flow
|
| 319 |
+
R_nodes[u]["excess"] -= flow
|
| 320 |
+
R_nodes[v]["excess"] += flow
|
| 321 |
+
# Determine the Δ-active nodes.
|
| 322 |
+
S = set()
|
| 323 |
+
T = set()
|
| 324 |
+
S_add = S.add
|
| 325 |
+
S_remove = S.remove
|
| 326 |
+
T_add = T.add
|
| 327 |
+
T_remove = T.remove
|
| 328 |
+
for u in R:
|
| 329 |
+
excess = R_nodes[u]["excess"]
|
| 330 |
+
if excess >= delta:
|
| 331 |
+
S_add(u)
|
| 332 |
+
elif excess <= -delta:
|
| 333 |
+
T_add(u)
|
| 334 |
+
# Repeatedly augment flow from S to T along shortest paths until
|
| 335 |
+
# Δ-feasibility is achieved.
|
| 336 |
+
while S and T:
|
| 337 |
+
s = arbitrary_element(S)
|
| 338 |
+
t = None
|
| 339 |
+
# Search for a shortest path in terms of reduce costs from s to
|
| 340 |
+
# any t in T in the Δ-residual network.
|
| 341 |
+
d = {}
|
| 342 |
+
pred = {s: None}
|
| 343 |
+
h = heap()
|
| 344 |
+
h_insert = h.insert
|
| 345 |
+
h_get = h.get
|
| 346 |
+
h_insert(s, 0)
|
| 347 |
+
while h:
|
| 348 |
+
u, d_u = h.pop()
|
| 349 |
+
d[u] = d_u
|
| 350 |
+
if u in T:
|
| 351 |
+
# Path found.
|
| 352 |
+
t = u
|
| 353 |
+
break
|
| 354 |
+
p_u = R_nodes[u]["potential"]
|
| 355 |
+
for v, es in R_succ[u].items():
|
| 356 |
+
if v in d:
|
| 357 |
+
continue
|
| 358 |
+
wmin = inf
|
| 359 |
+
# Find the minimum-weighted (u, v) Δ-residual edge.
|
| 360 |
+
for k, e in es.items():
|
| 361 |
+
if e["capacity"] - e["flow"] >= delta:
|
| 362 |
+
w = e["weight"]
|
| 363 |
+
if w < wmin:
|
| 364 |
+
wmin = w
|
| 365 |
+
kmin = k
|
| 366 |
+
emin = e
|
| 367 |
+
if wmin == inf:
|
| 368 |
+
continue
|
| 369 |
+
# Update the distance label of v.
|
| 370 |
+
d_v = d_u + wmin - p_u + R_nodes[v]["potential"]
|
| 371 |
+
if h_insert(v, d_v):
|
| 372 |
+
pred[v] = (u, kmin, emin)
|
| 373 |
+
if t is not None:
|
| 374 |
+
# Augment Δ units of flow from s to t.
|
| 375 |
+
while u != s:
|
| 376 |
+
v = u
|
| 377 |
+
u, k, e = pred[v]
|
| 378 |
+
e["flow"] += delta
|
| 379 |
+
R_succ[v][u][(k[0], not k[1])]["flow"] -= delta
|
| 380 |
+
# Account node excess and deficit.
|
| 381 |
+
R_nodes[s]["excess"] -= delta
|
| 382 |
+
R_nodes[t]["excess"] += delta
|
| 383 |
+
if R_nodes[s]["excess"] < delta:
|
| 384 |
+
S_remove(s)
|
| 385 |
+
if R_nodes[t]["excess"] > -delta:
|
| 386 |
+
T_remove(t)
|
| 387 |
+
# Update node potentials.
|
| 388 |
+
d_t = d[t]
|
| 389 |
+
for u, d_u in d.items():
|
| 390 |
+
R_nodes[u]["potential"] -= d_u - d_t
|
| 391 |
+
else:
|
| 392 |
+
# Path not found.
|
| 393 |
+
S_remove(s)
|
| 394 |
+
delta //= 2
|
| 395 |
+
|
| 396 |
+
if any(R.nodes[u]["excess"] != 0 for u in R):
|
| 397 |
+
raise nx.NetworkXUnfeasible("No flow satisfying all demands.")
|
| 398 |
+
|
| 399 |
+
# Calculate the flow cost.
|
| 400 |
+
for u in R:
|
| 401 |
+
for v, es in R_succ[u].items():
|
| 402 |
+
for e in es.values():
|
| 403 |
+
flow = e["flow"]
|
| 404 |
+
if flow > 0:
|
| 405 |
+
flow_cost += flow * e["weight"]
|
| 406 |
+
|
| 407 |
+
return flow_cost, _build_flow_dict(G, R, capacity, weight)
|
wemm/lib/python3.10/site-packages/networkx/algorithms/flow/tests/__pycache__/test_gomory_hu.cpython-310.pyc
ADDED
|
Binary file (4.74 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/traversal/edgebfs.py
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
=============================
|
| 3 |
+
Breadth First Search on Edges
|
| 4 |
+
=============================
|
| 5 |
+
|
| 6 |
+
Algorithms for a breadth-first traversal of edges in a graph.
|
| 7 |
+
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from collections import deque
|
| 11 |
+
|
| 12 |
+
import networkx as nx
|
| 13 |
+
|
| 14 |
+
FORWARD = "forward"
|
| 15 |
+
REVERSE = "reverse"
|
| 16 |
+
|
| 17 |
+
__all__ = ["edge_bfs"]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@nx._dispatchable
|
| 21 |
+
def edge_bfs(G, source=None, orientation=None):
|
| 22 |
+
"""A directed, breadth-first-search of edges in `G`, beginning at `source`.
|
| 23 |
+
|
| 24 |
+
Yield the edges of G in a breadth-first-search order continuing until
|
| 25 |
+
all edges are generated.
|
| 26 |
+
|
| 27 |
+
Parameters
|
| 28 |
+
----------
|
| 29 |
+
G : graph
|
| 30 |
+
A directed/undirected graph/multigraph.
|
| 31 |
+
|
| 32 |
+
source : node, list of nodes
|
| 33 |
+
The node from which the traversal begins. If None, then a source
|
| 34 |
+
is chosen arbitrarily and repeatedly until all edges from each node in
|
| 35 |
+
the graph are searched.
|
| 36 |
+
|
| 37 |
+
orientation : None | 'original' | 'reverse' | 'ignore' (default: None)
|
| 38 |
+
For directed graphs and directed multigraphs, edge traversals need not
|
| 39 |
+
respect the original orientation of the edges.
|
| 40 |
+
When set to 'reverse' every edge is traversed in the reverse direction.
|
| 41 |
+
When set to 'ignore', every edge is treated as undirected.
|
| 42 |
+
When set to 'original', every edge is treated as directed.
|
| 43 |
+
In all three cases, the yielded edge tuples add a last entry to
|
| 44 |
+
indicate the direction in which that edge was traversed.
|
| 45 |
+
If orientation is None, the yielded edge has no direction indicated.
|
| 46 |
+
The direction is respected, but not reported.
|
| 47 |
+
|
| 48 |
+
Yields
|
| 49 |
+
------
|
| 50 |
+
edge : directed edge
|
| 51 |
+
A directed edge indicating the path taken by the breadth-first-search.
|
| 52 |
+
For graphs, `edge` is of the form `(u, v)` where `u` and `v`
|
| 53 |
+
are the tail and head of the edge as determined by the traversal.
|
| 54 |
+
For multigraphs, `edge` is of the form `(u, v, key)`, where `key` is
|
| 55 |
+
the key of the edge. When the graph is directed, then `u` and `v`
|
| 56 |
+
are always in the order of the actual directed edge.
|
| 57 |
+
If orientation is not None then the edge tuple is extended to include
|
| 58 |
+
the direction of traversal ('forward' or 'reverse') on that edge.
|
| 59 |
+
|
| 60 |
+
Examples
|
| 61 |
+
--------
|
| 62 |
+
>>> nodes = [0, 1, 2, 3]
|
| 63 |
+
>>> edges = [(0, 1), (1, 0), (1, 0), (2, 0), (2, 1), (3, 1)]
|
| 64 |
+
|
| 65 |
+
>>> list(nx.edge_bfs(nx.Graph(edges), nodes))
|
| 66 |
+
[(0, 1), (0, 2), (1, 2), (1, 3)]
|
| 67 |
+
|
| 68 |
+
>>> list(nx.edge_bfs(nx.DiGraph(edges), nodes))
|
| 69 |
+
[(0, 1), (1, 0), (2, 0), (2, 1), (3, 1)]
|
| 70 |
+
|
| 71 |
+
>>> list(nx.edge_bfs(nx.MultiGraph(edges), nodes))
|
| 72 |
+
[(0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 2, 0), (1, 2, 0), (1, 3, 0)]
|
| 73 |
+
|
| 74 |
+
>>> list(nx.edge_bfs(nx.MultiDiGraph(edges), nodes))
|
| 75 |
+
[(0, 1, 0), (1, 0, 0), (1, 0, 1), (2, 0, 0), (2, 1, 0), (3, 1, 0)]
|
| 76 |
+
|
| 77 |
+
>>> list(nx.edge_bfs(nx.DiGraph(edges), nodes, orientation="ignore"))
|
| 78 |
+
[(0, 1, 'forward'), (1, 0, 'reverse'), (2, 0, 'reverse'), (2, 1, 'reverse'), (3, 1, 'reverse')]
|
| 79 |
+
|
| 80 |
+
>>> list(nx.edge_bfs(nx.MultiDiGraph(edges), nodes, orientation="ignore"))
|
| 81 |
+
[(0, 1, 0, 'forward'), (1, 0, 0, 'reverse'), (1, 0, 1, 'reverse'), (2, 0, 0, 'reverse'), (2, 1, 0, 'reverse'), (3, 1, 0, 'reverse')]
|
| 82 |
+
|
| 83 |
+
Notes
|
| 84 |
+
-----
|
| 85 |
+
The goal of this function is to visit edges. It differs from the more
|
| 86 |
+
familiar breadth-first-search of nodes, as provided by
|
| 87 |
+
:func:`networkx.algorithms.traversal.breadth_first_search.bfs_edges`, in
|
| 88 |
+
that it does not stop once every node has been visited. In a directed graph
|
| 89 |
+
with edges [(0, 1), (1, 2), (2, 1)], the edge (2, 1) would not be visited
|
| 90 |
+
if not for the functionality provided by this function.
|
| 91 |
+
|
| 92 |
+
The naming of this function is very similar to bfs_edges. The difference
|
| 93 |
+
is that 'edge_bfs' yields edges even if they extend back to an already
|
| 94 |
+
explored node while 'bfs_edges' yields the edges of the tree that results
|
| 95 |
+
from a breadth-first-search (BFS) so no edges are reported if they extend
|
| 96 |
+
to already explored nodes. That means 'edge_bfs' reports all edges while
|
| 97 |
+
'bfs_edges' only report those traversed by a node-based BFS. Yet another
|
| 98 |
+
description is that 'bfs_edges' reports the edges traversed during BFS
|
| 99 |
+
while 'edge_bfs' reports all edges in the order they are explored.
|
| 100 |
+
|
| 101 |
+
See Also
|
| 102 |
+
--------
|
| 103 |
+
bfs_edges
|
| 104 |
+
bfs_tree
|
| 105 |
+
edge_dfs
|
| 106 |
+
|
| 107 |
+
"""
|
| 108 |
+
nodes = list(G.nbunch_iter(source))
|
| 109 |
+
if not nodes:
|
| 110 |
+
return
|
| 111 |
+
|
| 112 |
+
directed = G.is_directed()
|
| 113 |
+
kwds = {"data": False}
|
| 114 |
+
if G.is_multigraph() is True:
|
| 115 |
+
kwds["keys"] = True
|
| 116 |
+
|
| 117 |
+
# set up edge lookup
|
| 118 |
+
if orientation is None:
|
| 119 |
+
|
| 120 |
+
def edges_from(node):
|
| 121 |
+
return iter(G.edges(node, **kwds))
|
| 122 |
+
|
| 123 |
+
elif not directed or orientation == "original":
|
| 124 |
+
|
| 125 |
+
def edges_from(node):
|
| 126 |
+
for e in G.edges(node, **kwds):
|
| 127 |
+
yield e + (FORWARD,)
|
| 128 |
+
|
| 129 |
+
elif orientation == "reverse":
|
| 130 |
+
|
| 131 |
+
def edges_from(node):
|
| 132 |
+
for e in G.in_edges(node, **kwds):
|
| 133 |
+
yield e + (REVERSE,)
|
| 134 |
+
|
| 135 |
+
elif orientation == "ignore":
|
| 136 |
+
|
| 137 |
+
def edges_from(node):
|
| 138 |
+
for e in G.edges(node, **kwds):
|
| 139 |
+
yield e + (FORWARD,)
|
| 140 |
+
for e in G.in_edges(node, **kwds):
|
| 141 |
+
yield e + (REVERSE,)
|
| 142 |
+
|
| 143 |
+
else:
|
| 144 |
+
raise nx.NetworkXError("invalid orientation argument.")
|
| 145 |
+
|
| 146 |
+
if directed:
|
| 147 |
+
neighbors = G.successors
|
| 148 |
+
|
| 149 |
+
def edge_id(edge):
|
| 150 |
+
# remove direction indicator
|
| 151 |
+
return edge[:-1] if orientation is not None else edge
|
| 152 |
+
|
| 153 |
+
else:
|
| 154 |
+
neighbors = G.neighbors
|
| 155 |
+
|
| 156 |
+
def edge_id(edge):
|
| 157 |
+
return (frozenset(edge[:2]),) + edge[2:]
|
| 158 |
+
|
| 159 |
+
check_reverse = directed and orientation in ("reverse", "ignore")
|
| 160 |
+
|
| 161 |
+
# start BFS
|
| 162 |
+
visited_nodes = set(nodes)
|
| 163 |
+
visited_edges = set()
|
| 164 |
+
queue = deque([(n, edges_from(n)) for n in nodes])
|
| 165 |
+
while queue:
|
| 166 |
+
parent, children_edges = queue.popleft()
|
| 167 |
+
for edge in children_edges:
|
| 168 |
+
if check_reverse and edge[-1] == REVERSE:
|
| 169 |
+
child = edge[0]
|
| 170 |
+
else:
|
| 171 |
+
child = edge[1]
|
| 172 |
+
if child not in visited_nodes:
|
| 173 |
+
visited_nodes.add(child)
|
| 174 |
+
queue.append((child, edges_from(child)))
|
| 175 |
+
edgeid = edge_id(edge)
|
| 176 |
+
if edgeid not in visited_edges:
|
| 177 |
+
visited_edges.add(edgeid)
|
| 178 |
+
yield edge
|
wemm/lib/python3.10/site-packages/networkx/drawing/__pycache__/nx_pylab.cpython-310.pyc
ADDED
|
Binary file (54.5 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (1.46 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/__pycache__/atlas.cpython-310.pyc
ADDED
|
Binary file (4.31 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/__pycache__/classic.cpython-310.pyc
ADDED
|
Binary file (29.8 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/__pycache__/degree_seq.cpython-310.pyc
ADDED
|
Binary file (25.6 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/__pycache__/geometric.cpython-310.pyc
ADDED
|
Binary file (38.9 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/__pycache__/random_clustered.cpython-310.pyc
ADDED
|
Binary file (4.12 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/__pycache__/random_graphs.cpython-310.pyc
ADDED
|
Binary file (40.2 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/__pycache__/small.cpython-310.pyc
ADDED
|
Binary file (26.4 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/__pycache__/spectral_graph_forge.cpython-310.pyc
ADDED
|
Binary file (4.22 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/cographs.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
r"""Generators for cographs
|
| 2 |
+
|
| 3 |
+
A cograph is a graph containing no path on four vertices.
|
| 4 |
+
Cographs or $P_4$-free graphs can be obtained from a single vertex
|
| 5 |
+
by disjoint union and complementation operations.
|
| 6 |
+
|
| 7 |
+
References
|
| 8 |
+
----------
|
| 9 |
+
.. [0] D.G. Corneil, H. Lerchs, L.Stewart Burlingham,
|
| 10 |
+
"Complement reducible graphs",
|
| 11 |
+
Discrete Applied Mathematics, Volume 3, Issue 3, 1981, Pages 163-174,
|
| 12 |
+
ISSN 0166-218X.
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
import networkx as nx
|
| 16 |
+
from networkx.utils import py_random_state
|
| 17 |
+
|
| 18 |
+
__all__ = ["random_cograph"]
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@py_random_state(1)
|
| 22 |
+
@nx._dispatchable(graphs=None, returns_graph=True)
|
| 23 |
+
def random_cograph(n, seed=None):
|
| 24 |
+
r"""Returns a random cograph with $2 ^ n$ nodes.
|
| 25 |
+
|
| 26 |
+
A cograph is a graph containing no path on four vertices.
|
| 27 |
+
Cographs or $P_4$-free graphs can be obtained from a single vertex
|
| 28 |
+
by disjoint union and complementation operations.
|
| 29 |
+
|
| 30 |
+
This generator starts off from a single vertex and performs disjoint
|
| 31 |
+
union and full join operations on itself.
|
| 32 |
+
The decision on which operation will take place is random.
|
| 33 |
+
|
| 34 |
+
Parameters
|
| 35 |
+
----------
|
| 36 |
+
n : int
|
| 37 |
+
The order of the cograph.
|
| 38 |
+
seed : integer, random_state, or None (default)
|
| 39 |
+
Indicator of random number generation state.
|
| 40 |
+
See :ref:`Randomness<randomness>`.
|
| 41 |
+
|
| 42 |
+
Returns
|
| 43 |
+
-------
|
| 44 |
+
G : A random graph containing no path on four vertices.
|
| 45 |
+
|
| 46 |
+
See Also
|
| 47 |
+
--------
|
| 48 |
+
full_join
|
| 49 |
+
union
|
| 50 |
+
|
| 51 |
+
References
|
| 52 |
+
----------
|
| 53 |
+
.. [1] D.G. Corneil, H. Lerchs, L.Stewart Burlingham,
|
| 54 |
+
"Complement reducible graphs",
|
| 55 |
+
Discrete Applied Mathematics, Volume 3, Issue 3, 1981, Pages 163-174,
|
| 56 |
+
ISSN 0166-218X.
|
| 57 |
+
"""
|
| 58 |
+
R = nx.empty_graph(1)
|
| 59 |
+
|
| 60 |
+
for i in range(n):
|
| 61 |
+
RR = nx.relabel_nodes(R.copy(), lambda x: x + len(R))
|
| 62 |
+
|
| 63 |
+
if seed.randint(0, 1) == 0:
|
| 64 |
+
R = nx.full_join(R, RR)
|
| 65 |
+
else:
|
| 66 |
+
R = nx.disjoint_union(R, RR)
|
| 67 |
+
|
| 68 |
+
return R
|
wemm/lib/python3.10/site-packages/networkx/generators/duplication.py
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
| 1 |
+
"""Functions for generating graphs based on the "duplication" method.
|
| 2 |
+
|
| 3 |
+
These graph generators start with a small initial graph then duplicate
|
| 4 |
+
nodes and (partially) duplicate their edges. These functions are
|
| 5 |
+
generally inspired by biological networks.
|
| 6 |
+
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import networkx as nx
|
| 10 |
+
from networkx.exception import NetworkXError
|
| 11 |
+
from networkx.utils import py_random_state
|
| 12 |
+
from networkx.utils.misc import check_create_using
|
| 13 |
+
|
| 14 |
+
__all__ = ["partial_duplication_graph", "duplication_divergence_graph"]
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@py_random_state(4)
|
| 18 |
+
@nx._dispatchable(graphs=None, returns_graph=True)
|
| 19 |
+
def partial_duplication_graph(N, n, p, q, seed=None, *, create_using=None):
|
| 20 |
+
"""Returns a random graph using the partial duplication model.
|
| 21 |
+
|
| 22 |
+
Parameters
|
| 23 |
+
----------
|
| 24 |
+
N : int
|
| 25 |
+
The total number of nodes in the final graph.
|
| 26 |
+
|
| 27 |
+
n : int
|
| 28 |
+
The number of nodes in the initial clique.
|
| 29 |
+
|
| 30 |
+
p : float
|
| 31 |
+
The probability of joining each neighbor of a node to the
|
| 32 |
+
duplicate node. Must be a number in the between zero and one,
|
| 33 |
+
inclusive.
|
| 34 |
+
|
| 35 |
+
q : float
|
| 36 |
+
The probability of joining the source node to the duplicate
|
| 37 |
+
node. Must be a number in the between zero and one, inclusive.
|
| 38 |
+
|
| 39 |
+
seed : integer, random_state, or None (default)
|
| 40 |
+
Indicator of random number generation state.
|
| 41 |
+
See :ref:`Randomness<randomness>`.
|
| 42 |
+
|
| 43 |
+
create_using : Graph constructor, optional (default=nx.Graph)
|
| 44 |
+
Graph type to create. If graph instance, then cleared before populated.
|
| 45 |
+
Multigraph and directed types are not supported and raise a ``NetworkXError``.
|
| 46 |
+
|
| 47 |
+
Notes
|
| 48 |
+
-----
|
| 49 |
+
A graph of nodes is grown by creating a fully connected graph
|
| 50 |
+
of size `n`. The following procedure is then repeated until
|
| 51 |
+
a total of `N` nodes have been reached.
|
| 52 |
+
|
| 53 |
+
1. A random node, *u*, is picked and a new node, *v*, is created.
|
| 54 |
+
2. For each neighbor of *u* an edge from the neighbor to *v* is created
|
| 55 |
+
with probability `p`.
|
| 56 |
+
3. An edge from *u* to *v* is created with probability `q`.
|
| 57 |
+
|
| 58 |
+
This algorithm appears in [1].
|
| 59 |
+
|
| 60 |
+
This implementation allows the possibility of generating
|
| 61 |
+
disconnected graphs.
|
| 62 |
+
|
| 63 |
+
References
|
| 64 |
+
----------
|
| 65 |
+
.. [1] Knudsen Michael, and Carsten Wiuf. "A Markov chain approach to
|
| 66 |
+
randomly grown graphs." Journal of Applied Mathematics 2008.
|
| 67 |
+
<https://doi.org/10.1155/2008/190836>
|
| 68 |
+
|
| 69 |
+
"""
|
| 70 |
+
create_using = check_create_using(create_using, directed=False, multigraph=False)
|
| 71 |
+
if p < 0 or p > 1 or q < 0 or q > 1:
|
| 72 |
+
msg = "partial duplication graph must have 0 <= p, q <= 1."
|
| 73 |
+
raise NetworkXError(msg)
|
| 74 |
+
if n > N:
|
| 75 |
+
raise NetworkXError("partial duplication graph must have n <= N.")
|
| 76 |
+
|
| 77 |
+
G = nx.complete_graph(n, create_using)
|
| 78 |
+
for new_node in range(n, N):
|
| 79 |
+
# Pick a random vertex, u, already in the graph.
|
| 80 |
+
src_node = seed.randint(0, new_node - 1)
|
| 81 |
+
|
| 82 |
+
# Add a new vertex, v, to the graph.
|
| 83 |
+
G.add_node(new_node)
|
| 84 |
+
|
| 85 |
+
# For each neighbor of u...
|
| 86 |
+
for nbr_node in list(nx.all_neighbors(G, src_node)):
|
| 87 |
+
# Add the neighbor to v with probability p.
|
| 88 |
+
if seed.random() < p:
|
| 89 |
+
G.add_edge(new_node, nbr_node)
|
| 90 |
+
|
| 91 |
+
# Join v and u with probability q.
|
| 92 |
+
if seed.random() < q:
|
| 93 |
+
G.add_edge(new_node, src_node)
|
| 94 |
+
return G
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
@py_random_state(2)
|
| 98 |
+
@nx._dispatchable(graphs=None, returns_graph=True)
|
| 99 |
+
def duplication_divergence_graph(n, p, seed=None, *, create_using=None):
|
| 100 |
+
"""Returns an undirected graph using the duplication-divergence model.
|
| 101 |
+
|
| 102 |
+
A graph of `n` nodes is created by duplicating the initial nodes
|
| 103 |
+
and retaining edges incident to the original nodes with a retention
|
| 104 |
+
probability `p`.
|
| 105 |
+
|
| 106 |
+
Parameters
|
| 107 |
+
----------
|
| 108 |
+
n : int
|
| 109 |
+
The desired number of nodes in the graph.
|
| 110 |
+
p : float
|
| 111 |
+
The probability for retaining the edge of the replicated node.
|
| 112 |
+
seed : integer, random_state, or None (default)
|
| 113 |
+
Indicator of random number generation state.
|
| 114 |
+
See :ref:`Randomness<randomness>`.
|
| 115 |
+
create_using : Graph constructor, optional (default=nx.Graph)
|
| 116 |
+
Graph type to create. If graph instance, then cleared before populated.
|
| 117 |
+
Multigraph and directed types are not supported and raise a ``NetworkXError``.
|
| 118 |
+
|
| 119 |
+
Returns
|
| 120 |
+
-------
|
| 121 |
+
G : Graph
|
| 122 |
+
|
| 123 |
+
Raises
|
| 124 |
+
------
|
| 125 |
+
NetworkXError
|
| 126 |
+
If `p` is not a valid probability.
|
| 127 |
+
If `n` is less than 2.
|
| 128 |
+
|
| 129 |
+
Notes
|
| 130 |
+
-----
|
| 131 |
+
This algorithm appears in [1].
|
| 132 |
+
|
| 133 |
+
This implementation disallows the possibility of generating
|
| 134 |
+
disconnected graphs.
|
| 135 |
+
|
| 136 |
+
References
|
| 137 |
+
----------
|
| 138 |
+
.. [1] I. Ispolatov, P. L. Krapivsky, A. Yuryev,
|
| 139 |
+
"Duplication-divergence model of protein interaction network",
|
| 140 |
+
Phys. Rev. E, 71, 061911, 2005.
|
| 141 |
+
|
| 142 |
+
"""
|
| 143 |
+
if p > 1 or p < 0:
|
| 144 |
+
msg = f"NetworkXError p={p} is not in [0,1]."
|
| 145 |
+
raise nx.NetworkXError(msg)
|
| 146 |
+
if n < 2:
|
| 147 |
+
msg = "n must be greater than or equal to 2"
|
| 148 |
+
raise nx.NetworkXError(msg)
|
| 149 |
+
|
| 150 |
+
create_using = check_create_using(create_using, directed=False, multigraph=False)
|
| 151 |
+
G = nx.empty_graph(create_using=create_using)
|
| 152 |
+
|
| 153 |
+
# Initialize the graph with two connected nodes.
|
| 154 |
+
G.add_edge(0, 1)
|
| 155 |
+
i = 2
|
| 156 |
+
while i < n:
|
| 157 |
+
# Choose a random node from current graph to duplicate.
|
| 158 |
+
random_node = seed.choice(list(G))
|
| 159 |
+
# Make the replica.
|
| 160 |
+
G.add_node(i)
|
| 161 |
+
# flag indicates whether at least one edge is connected on the replica.
|
| 162 |
+
flag = False
|
| 163 |
+
for nbr in G.neighbors(random_node):
|
| 164 |
+
if seed.random() < p:
|
| 165 |
+
# Link retention step.
|
| 166 |
+
G.add_edge(i, nbr)
|
| 167 |
+
flag = True
|
| 168 |
+
if not flag:
|
| 169 |
+
# Delete replica if no edges retained.
|
| 170 |
+
G.remove_node(i)
|
| 171 |
+
else:
|
| 172 |
+
# Successful duplication.
|
| 173 |
+
i += 1
|
| 174 |
+
return G
|
wemm/lib/python3.10/site-packages/networkx/generators/ego.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Ego graph.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
__all__ = ["ego_graph"]
|
| 6 |
+
|
| 7 |
+
import networkx as nx
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@nx._dispatchable(preserve_all_attrs=True, returns_graph=True)
|
| 11 |
+
def ego_graph(G, n, radius=1, center=True, undirected=False, distance=None):
|
| 12 |
+
"""Returns induced subgraph of neighbors centered at node n within
|
| 13 |
+
a given radius.
|
| 14 |
+
|
| 15 |
+
Parameters
|
| 16 |
+
----------
|
| 17 |
+
G : graph
|
| 18 |
+
A NetworkX Graph or DiGraph
|
| 19 |
+
|
| 20 |
+
n : node
|
| 21 |
+
A single node
|
| 22 |
+
|
| 23 |
+
radius : number, optional
|
| 24 |
+
Include all neighbors of distance<=radius from n.
|
| 25 |
+
|
| 26 |
+
center : bool, optional
|
| 27 |
+
If False, do not include center node in graph
|
| 28 |
+
|
| 29 |
+
undirected : bool, optional
|
| 30 |
+
If True use both in- and out-neighbors of directed graphs.
|
| 31 |
+
|
| 32 |
+
distance : key, optional
|
| 33 |
+
Use specified edge data key as distance. For example, setting
|
| 34 |
+
distance='weight' will use the edge weight to measure the
|
| 35 |
+
distance from the node n.
|
| 36 |
+
|
| 37 |
+
Notes
|
| 38 |
+
-----
|
| 39 |
+
For directed graphs D this produces the "out" neighborhood
|
| 40 |
+
or successors. If you want the neighborhood of predecessors
|
| 41 |
+
first reverse the graph with D.reverse(). If you want both
|
| 42 |
+
directions use the keyword argument undirected=True.
|
| 43 |
+
|
| 44 |
+
Node, edge, and graph attributes are copied to the returned subgraph.
|
| 45 |
+
"""
|
| 46 |
+
if undirected:
|
| 47 |
+
if distance is not None:
|
| 48 |
+
sp, _ = nx.single_source_dijkstra(
|
| 49 |
+
G.to_undirected(), n, cutoff=radius, weight=distance
|
| 50 |
+
)
|
| 51 |
+
else:
|
| 52 |
+
sp = dict(
|
| 53 |
+
nx.single_source_shortest_path_length(
|
| 54 |
+
G.to_undirected(), n, cutoff=radius
|
| 55 |
+
)
|
| 56 |
+
)
|
| 57 |
+
else:
|
| 58 |
+
if distance is not None:
|
| 59 |
+
sp, _ = nx.single_source_dijkstra(G, n, cutoff=radius, weight=distance)
|
| 60 |
+
else:
|
| 61 |
+
sp = dict(nx.single_source_shortest_path_length(G, n, cutoff=radius))
|
| 62 |
+
|
| 63 |
+
H = G.subgraph(sp).copy()
|
| 64 |
+
if not center:
|
| 65 |
+
H.remove_node(n)
|
| 66 |
+
return H
|
wemm/lib/python3.10/site-packages/networkx/generators/internet_as_graphs.py
ADDED
|
@@ -0,0 +1,441 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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phs resembling the Internet Autonomous System network"""
|
| 2 |
+
|
| 3 |
+
import networkx as nx
|
| 4 |
+
from networkx.utils import py_random_state
|
| 5 |
+
|
| 6 |
+
__all__ = ["random_internet_as_graph"]
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def uniform_int_from_avg(a, m, seed):
|
| 10 |
+
"""Pick a random integer with uniform probability.
|
| 11 |
+
|
| 12 |
+
Returns a random integer uniformly taken from a distribution with
|
| 13 |
+
minimum value 'a' and average value 'm', X~U(a,b), E[X]=m, X in N where
|
| 14 |
+
b = 2*m - a.
|
| 15 |
+
|
| 16 |
+
Notes
|
| 17 |
+
-----
|
| 18 |
+
p = (b-floor(b))/2
|
| 19 |
+
X = X1 + X2; X1~U(a,floor(b)), X2~B(p)
|
| 20 |
+
E[X] = E[X1] + E[X2] = (floor(b)+a)/2 + (b-floor(b))/2 = (b+a)/2 = m
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
from math import floor
|
| 24 |
+
|
| 25 |
+
assert m >= a
|
| 26 |
+
b = 2 * m - a
|
| 27 |
+
p = (b - floor(b)) / 2
|
| 28 |
+
X1 = round(seed.random() * (floor(b) - a) + a)
|
| 29 |
+
if seed.random() < p:
|
| 30 |
+
X2 = 1
|
| 31 |
+
else:
|
| 32 |
+
X2 = 0
|
| 33 |
+
return X1 + X2
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def choose_pref_attach(degs, seed):
|
| 37 |
+
"""Pick a random value, with a probability given by its weight.
|
| 38 |
+
|
| 39 |
+
Returns a random choice among degs keys, each of which has a
|
| 40 |
+
probability proportional to the corresponding dictionary value.
|
| 41 |
+
|
| 42 |
+
Parameters
|
| 43 |
+
----------
|
| 44 |
+
degs: dictionary
|
| 45 |
+
It contains the possible values (keys) and the corresponding
|
| 46 |
+
probabilities (values)
|
| 47 |
+
seed: random state
|
| 48 |
+
|
| 49 |
+
Returns
|
| 50 |
+
-------
|
| 51 |
+
v: object
|
| 52 |
+
A key of degs or None if degs is empty
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
if len(degs) == 0:
|
| 56 |
+
return None
|
| 57 |
+
s = sum(degs.values())
|
| 58 |
+
if s == 0:
|
| 59 |
+
return seed.choice(list(degs.keys()))
|
| 60 |
+
v = seed.random() * s
|
| 61 |
+
|
| 62 |
+
nodes = list(degs.keys())
|
| 63 |
+
i = 0
|
| 64 |
+
acc = degs[nodes[i]]
|
| 65 |
+
while v > acc:
|
| 66 |
+
i += 1
|
| 67 |
+
acc += degs[nodes[i]]
|
| 68 |
+
return nodes[i]
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
class AS_graph_generator:
|
| 72 |
+
"""Generates random internet AS graphs."""
|
| 73 |
+
|
| 74 |
+
def __init__(self, n, seed):
|
| 75 |
+
"""Initializes variables. Immediate numbers are taken from [1].
|
| 76 |
+
|
| 77 |
+
Parameters
|
| 78 |
+
----------
|
| 79 |
+
n: integer
|
| 80 |
+
Number of graph nodes
|
| 81 |
+
seed: random state
|
| 82 |
+
Indicator of random number generation state.
|
| 83 |
+
See :ref:`Randomness<randomness>`.
|
| 84 |
+
|
| 85 |
+
Returns
|
| 86 |
+
-------
|
| 87 |
+
GG: AS_graph_generator object
|
| 88 |
+
|
| 89 |
+
References
|
| 90 |
+
----------
|
| 91 |
+
[1] A. Elmokashfi, A. Kvalbein and C. Dovrolis, "On the Scalability of
|
| 92 |
+
BGP: The Role of Topology Growth," in IEEE Journal on Selected Areas
|
| 93 |
+
in Communications, vol. 28, no. 8, pp. 1250-1261, October 2010.
|
| 94 |
+
"""
|
| 95 |
+
|
| 96 |
+
self.seed = seed
|
| 97 |
+
self.n_t = min(n, round(self.seed.random() * 2 + 4)) # num of T nodes
|
| 98 |
+
self.n_m = round(0.15 * n) # number of M nodes
|
| 99 |
+
self.n_cp = round(0.05 * n) # number of CP nodes
|
| 100 |
+
self.n_c = max(0, n - self.n_t - self.n_m - self.n_cp) # number of C nodes
|
| 101 |
+
|
| 102 |
+
self.d_m = 2 + (2.5 * n) / 10000 # average multihoming degree for M nodes
|
| 103 |
+
self.d_cp = 2 + (1.5 * n) / 10000 # avg multihoming degree for CP nodes
|
| 104 |
+
self.d_c = 1 + (5 * n) / 100000 # average multihoming degree for C nodes
|
| 105 |
+
|
| 106 |
+
self.p_m_m = 1 + (2 * n) / 10000 # avg num of peer edges between M and M
|
| 107 |
+
self.p_cp_m = 0.2 + (2 * n) / 10000 # avg num of peer edges between CP, M
|
| 108 |
+
self.p_cp_cp = 0.05 + (2 * n) / 100000 # avg num of peer edges btwn CP, CP
|
| 109 |
+
|
| 110 |
+
self.t_m = 0.375 # probability M's provider is T
|
| 111 |
+
self.t_cp = 0.375 # probability CP's provider is T
|
| 112 |
+
self.t_c = 0.125 # probability C's provider is T
|
| 113 |
+
|
| 114 |
+
def t_graph(self):
|
| 115 |
+
"""Generates the core mesh network of tier one nodes of a AS graph.
|
| 116 |
+
|
| 117 |
+
Returns
|
| 118 |
+
-------
|
| 119 |
+
G: Networkx Graph
|
| 120 |
+
Core network
|
| 121 |
+
"""
|
| 122 |
+
|
| 123 |
+
self.G = nx.Graph()
|
| 124 |
+
for i in range(self.n_t):
|
| 125 |
+
self.G.add_node(i, type="T")
|
| 126 |
+
for r in self.regions:
|
| 127 |
+
self.regions[r].add(i)
|
| 128 |
+
for j in self.G.nodes():
|
| 129 |
+
if i != j:
|
| 130 |
+
self.add_edge(i, j, "peer")
|
| 131 |
+
self.customers[i] = set()
|
| 132 |
+
self.providers[i] = set()
|
| 133 |
+
return self.G
|
| 134 |
+
|
| 135 |
+
def add_edge(self, i, j, kind):
|
| 136 |
+
if kind == "transit":
|
| 137 |
+
customer = str(i)
|
| 138 |
+
else:
|
| 139 |
+
customer = "none"
|
| 140 |
+
self.G.add_edge(i, j, type=kind, customer=customer)
|
| 141 |
+
|
| 142 |
+
def choose_peer_pref_attach(self, node_list):
|
| 143 |
+
"""Pick a node with a probability weighted by its peer degree.
|
| 144 |
+
|
| 145 |
+
Pick a node from node_list with preferential attachment
|
| 146 |
+
computed only on their peer degree
|
| 147 |
+
"""
|
| 148 |
+
|
| 149 |
+
d = {}
|
| 150 |
+
for n in node_list:
|
| 151 |
+
d[n] = self.G.nodes[n]["peers"]
|
| 152 |
+
return choose_pref_attach(d, self.seed)
|
| 153 |
+
|
| 154 |
+
def choose_node_pref_attach(self, node_list):
|
| 155 |
+
"""Pick a node with a probability weighted by its degree.
|
| 156 |
+
|
| 157 |
+
Pick a node from node_list with preferential attachment
|
| 158 |
+
computed on their degree
|
| 159 |
+
"""
|
| 160 |
+
|
| 161 |
+
degs = dict(self.G.degree(node_list))
|
| 162 |
+
return choose_pref_attach(degs, self.seed)
|
| 163 |
+
|
| 164 |
+
def add_customer(self, i, j):
|
| 165 |
+
"""Keep the dictionaries 'customers' and 'providers' consistent."""
|
| 166 |
+
|
| 167 |
+
self.customers[j].add(i)
|
| 168 |
+
self.providers[i].add(j)
|
| 169 |
+
for z in self.providers[j]:
|
| 170 |
+
self.customers[z].add(i)
|
| 171 |
+
self.providers[i].add(z)
|
| 172 |
+
|
| 173 |
+
def add_node(self, i, kind, reg2prob, avg_deg, t_edge_prob):
|
| 174 |
+
"""Add a node and its customer transit edges to the graph.
|
| 175 |
+
|
| 176 |
+
Parameters
|
| 177 |
+
----------
|
| 178 |
+
i: object
|
| 179 |
+
Identifier of the new node
|
| 180 |
+
kind: string
|
| 181 |
+
Type of the new node. Options are: 'M' for middle node, 'CP' for
|
| 182 |
+
content provider and 'C' for customer.
|
| 183 |
+
reg2prob: float
|
| 184 |
+
Probability the new node can be in two different regions.
|
| 185 |
+
avg_deg: float
|
| 186 |
+
Average number of transit nodes of which node i is customer.
|
| 187 |
+
t_edge_prob: float
|
| 188 |
+
Probability node i establish a customer transit edge with a tier
|
| 189 |
+
one (T) node
|
| 190 |
+
|
| 191 |
+
Returns
|
| 192 |
+
-------
|
| 193 |
+
i: object
|
| 194 |
+
Identifier of the new node
|
| 195 |
+
"""
|
| 196 |
+
|
| 197 |
+
regs = 1 # regions in which node resides
|
| 198 |
+
if self.seed.random() < reg2prob: # node is in two regions
|
| 199 |
+
regs = 2
|
| 200 |
+
node_options = set()
|
| 201 |
+
|
| 202 |
+
self.G.add_node(i, type=kind, peers=0)
|
| 203 |
+
self.customers[i] = set()
|
| 204 |
+
self.providers[i] = set()
|
| 205 |
+
self.nodes[kind].add(i)
|
| 206 |
+
for r in self.seed.sample(list(self.regions), regs):
|
| 207 |
+
node_options = node_options.union(self.regions[r])
|
| 208 |
+
self.regions[r].add(i)
|
| 209 |
+
|
| 210 |
+
edge_num = uniform_int_from_avg(1, avg_deg, self.seed)
|
| 211 |
+
|
| 212 |
+
t_options = node_options.intersection(self.nodes["T"])
|
| 213 |
+
m_options = node_options.intersection(self.nodes["M"])
|
| 214 |
+
if i in m_options:
|
| 215 |
+
m_options.remove(i)
|
| 216 |
+
d = 0
|
| 217 |
+
while d < edge_num and (len(t_options) > 0 or len(m_options) > 0):
|
| 218 |
+
if len(m_options) == 0 or (
|
| 219 |
+
len(t_options) > 0 and self.seed.random() < t_edge_prob
|
| 220 |
+
): # add edge to a T node
|
| 221 |
+
j = self.choose_node_pref_attach(t_options)
|
| 222 |
+
t_options.remove(j)
|
| 223 |
+
else:
|
| 224 |
+
j = self.choose_node_pref_attach(m_options)
|
| 225 |
+
m_options.remove(j)
|
| 226 |
+
self.add_edge(i, j, "transit")
|
| 227 |
+
self.add_customer(i, j)
|
| 228 |
+
d += 1
|
| 229 |
+
|
| 230 |
+
return i
|
| 231 |
+
|
| 232 |
+
def add_m_peering_link(self, m, to_kind):
|
| 233 |
+
"""Add a peering link between two middle tier (M) nodes.
|
| 234 |
+
|
| 235 |
+
Target node j is drawn considering a preferential attachment based on
|
| 236 |
+
other M node peering degree.
|
| 237 |
+
|
| 238 |
+
Parameters
|
| 239 |
+
----------
|
| 240 |
+
m: object
|
| 241 |
+
Node identifier
|
| 242 |
+
to_kind: string
|
| 243 |
+
type for target node j (must be always M)
|
| 244 |
+
|
| 245 |
+
Returns
|
| 246 |
+
-------
|
| 247 |
+
success: boolean
|
| 248 |
+
"""
|
| 249 |
+
|
| 250 |
+
# candidates are of type 'M' and are not customers of m
|
| 251 |
+
node_options = self.nodes["M"].difference(self.customers[m])
|
| 252 |
+
# candidates are not providers of m
|
| 253 |
+
node_options = node_options.difference(self.providers[m])
|
| 254 |
+
# remove self
|
| 255 |
+
if m in node_options:
|
| 256 |
+
node_options.remove(m)
|
| 257 |
+
|
| 258 |
+
# remove candidates we are already connected to
|
| 259 |
+
for j in self.G.neighbors(m):
|
| 260 |
+
if j in node_options:
|
| 261 |
+
node_options.remove(j)
|
| 262 |
+
|
| 263 |
+
if len(node_options) > 0:
|
| 264 |
+
j = self.choose_peer_pref_attach(node_options)
|
| 265 |
+
self.add_edge(m, j, "peer")
|
| 266 |
+
self.G.nodes[m]["peers"] += 1
|
| 267 |
+
self.G.nodes[j]["peers"] += 1
|
| 268 |
+
return True
|
| 269 |
+
else:
|
| 270 |
+
return False
|
| 271 |
+
|
| 272 |
+
def add_cp_peering_link(self, cp, to_kind):
|
| 273 |
+
"""Add a peering link to a content provider (CP) node.
|
| 274 |
+
|
| 275 |
+
Target node j can be CP or M and it is drawn uniformly among the nodes
|
| 276 |
+
belonging to the same region as cp.
|
| 277 |
+
|
| 278 |
+
Parameters
|
| 279 |
+
----------
|
| 280 |
+
cp: object
|
| 281 |
+
Node identifier
|
| 282 |
+
to_kind: string
|
| 283 |
+
type for target node j (must be M or CP)
|
| 284 |
+
|
| 285 |
+
Returns
|
| 286 |
+
-------
|
| 287 |
+
success: boolean
|
| 288 |
+
"""
|
| 289 |
+
|
| 290 |
+
node_options = set()
|
| 291 |
+
for r in self.regions: # options include nodes in the same region(s)
|
| 292 |
+
if cp in self.regions[r]:
|
| 293 |
+
node_options = node_options.union(self.regions[r])
|
| 294 |
+
|
| 295 |
+
# options are restricted to the indicated kind ('M' or 'CP')
|
| 296 |
+
node_options = self.nodes[to_kind].intersection(node_options)
|
| 297 |
+
|
| 298 |
+
# remove self
|
| 299 |
+
if cp in node_options:
|
| 300 |
+
node_options.remove(cp)
|
| 301 |
+
|
| 302 |
+
# remove nodes that are cp's providers
|
| 303 |
+
node_options = node_options.difference(self.providers[cp])
|
| 304 |
+
|
| 305 |
+
# remove nodes we are already connected to
|
| 306 |
+
for j in self.G.neighbors(cp):
|
| 307 |
+
if j in node_options:
|
| 308 |
+
node_options.remove(j)
|
| 309 |
+
|
| 310 |
+
if len(node_options) > 0:
|
| 311 |
+
j = self.seed.sample(list(node_options), 1)[0]
|
| 312 |
+
self.add_edge(cp, j, "peer")
|
| 313 |
+
self.G.nodes[cp]["peers"] += 1
|
| 314 |
+
self.G.nodes[j]["peers"] += 1
|
| 315 |
+
return True
|
| 316 |
+
else:
|
| 317 |
+
return False
|
| 318 |
+
|
| 319 |
+
def graph_regions(self, rn):
|
| 320 |
+
"""Initializes AS network regions.
|
| 321 |
+
|
| 322 |
+
Parameters
|
| 323 |
+
----------
|
| 324 |
+
rn: integer
|
| 325 |
+
Number of regions
|
| 326 |
+
"""
|
| 327 |
+
|
| 328 |
+
self.regions = {}
|
| 329 |
+
for i in range(rn):
|
| 330 |
+
self.regions["REG" + str(i)] = set()
|
| 331 |
+
|
| 332 |
+
def add_peering_links(self, from_kind, to_kind):
|
| 333 |
+
"""Utility function to add peering links among node groups."""
|
| 334 |
+
peer_link_method = None
|
| 335 |
+
if from_kind == "M":
|
| 336 |
+
peer_link_method = self.add_m_peering_link
|
| 337 |
+
m = self.p_m_m
|
| 338 |
+
if from_kind == "CP":
|
| 339 |
+
peer_link_method = self.add_cp_peering_link
|
| 340 |
+
if to_kind == "M":
|
| 341 |
+
m = self.p_cp_m
|
| 342 |
+
else:
|
| 343 |
+
m = self.p_cp_cp
|
| 344 |
+
|
| 345 |
+
for i in self.nodes[from_kind]:
|
| 346 |
+
num = uniform_int_from_avg(0, m, self.seed)
|
| 347 |
+
for _ in range(num):
|
| 348 |
+
peer_link_method(i, to_kind)
|
| 349 |
+
|
| 350 |
+
def generate(self):
|
| 351 |
+
"""Generates a random AS network graph as described in [1].
|
| 352 |
+
|
| 353 |
+
Returns
|
| 354 |
+
-------
|
| 355 |
+
G: Graph object
|
| 356 |
+
|
| 357 |
+
Notes
|
| 358 |
+
-----
|
| 359 |
+
The process steps are the following: first we create the core network
|
| 360 |
+
of tier one nodes, then we add the middle tier (M), the content
|
| 361 |
+
provider (CP) and the customer (C) nodes along with their transit edges
|
| 362 |
+
(link i,j means i is customer of j). Finally we add peering links
|
| 363 |
+
between M nodes, between M and CP nodes and between CP node couples.
|
| 364 |
+
For a detailed description of the algorithm, please refer to [1].
|
| 365 |
+
|
| 366 |
+
References
|
| 367 |
+
----------
|
| 368 |
+
[1] A. Elmokashfi, A. Kvalbein and C. Dovrolis, "On the Scalability of
|
| 369 |
+
BGP: The Role of Topology Growth," in IEEE Journal on Selected Areas
|
| 370 |
+
in Communications, vol. 28, no. 8, pp. 1250-1261, October 2010.
|
| 371 |
+
"""
|
| 372 |
+
|
| 373 |
+
self.graph_regions(5)
|
| 374 |
+
self.customers = {}
|
| 375 |
+
self.providers = {}
|
| 376 |
+
self.nodes = {"T": set(), "M": set(), "CP": set(), "C": set()}
|
| 377 |
+
|
| 378 |
+
self.t_graph()
|
| 379 |
+
self.nodes["T"] = set(self.G.nodes())
|
| 380 |
+
|
| 381 |
+
i = len(self.nodes["T"])
|
| 382 |
+
for _ in range(self.n_m):
|
| 383 |
+
self.nodes["M"].add(self.add_node(i, "M", 0.2, self.d_m, self.t_m))
|
| 384 |
+
i += 1
|
| 385 |
+
for _ in range(self.n_cp):
|
| 386 |
+
self.nodes["CP"].add(self.add_node(i, "CP", 0.05, self.d_cp, self.t_cp))
|
| 387 |
+
i += 1
|
| 388 |
+
for _ in range(self.n_c):
|
| 389 |
+
self.nodes["C"].add(self.add_node(i, "C", 0, self.d_c, self.t_c))
|
| 390 |
+
i += 1
|
| 391 |
+
|
| 392 |
+
self.add_peering_links("M", "M")
|
| 393 |
+
self.add_peering_links("CP", "M")
|
| 394 |
+
self.add_peering_links("CP", "CP")
|
| 395 |
+
|
| 396 |
+
return self.G
|
| 397 |
+
|
| 398 |
+
|
| 399 |
+
@py_random_state(1)
|
| 400 |
+
@nx._dispatchable(graphs=None, returns_graph=True)
|
| 401 |
+
def random_internet_as_graph(n, seed=None):
|
| 402 |
+
"""Generates a random undirected graph resembling the Internet AS network
|
| 403 |
+
|
| 404 |
+
Parameters
|
| 405 |
+
----------
|
| 406 |
+
n: integer in [1000, 10000]
|
| 407 |
+
Number of graph nodes
|
| 408 |
+
seed : integer, random_state, or None (default)
|
| 409 |
+
Indicator of random number generation state.
|
| 410 |
+
See :ref:`Randomness<randomness>`.
|
| 411 |
+
|
| 412 |
+
Returns
|
| 413 |
+
-------
|
| 414 |
+
G: Networkx Graph object
|
| 415 |
+
A randomly generated undirected graph
|
| 416 |
+
|
| 417 |
+
Notes
|
| 418 |
+
-----
|
| 419 |
+
This algorithm returns an undirected graph resembling the Internet
|
| 420 |
+
Autonomous System (AS) network, it uses the approach by Elmokashfi et al.
|
| 421 |
+
[1]_ and it grants the properties described in the related paper [1]_.
|
| 422 |
+
|
| 423 |
+
Each node models an autonomous system, with an attribute 'type' specifying
|
| 424 |
+
its kind; tier-1 (T), mid-level (M), customer (C) or content-provider (CP).
|
| 425 |
+
Each edge models an ADV communication link (hence, bidirectional) with
|
| 426 |
+
attributes:
|
| 427 |
+
|
| 428 |
+
- type: transit|peer, the kind of commercial agreement between nodes;
|
| 429 |
+
- customer: <node id>, the identifier of the node acting as customer
|
| 430 |
+
('none' if type is peer).
|
| 431 |
+
|
| 432 |
+
References
|
| 433 |
+
----------
|
| 434 |
+
.. [1] A. Elmokashfi, A. Kvalbein and C. Dovrolis, "On the Scalability of
|
| 435 |
+
BGP: The Role of Topology Growth," in IEEE Journal on Selected Areas
|
| 436 |
+
in Communications, vol. 28, no. 8, pp. 1250-1261, October 2010.
|
| 437 |
+
"""
|
| 438 |
+
|
| 439 |
+
GG = AS_graph_generator(n, seed)
|
| 440 |
+
G = GG.generate()
|
| 441 |
+
return G
|
wemm/lib/python3.10/site-packages/networkx/generators/interval_graph.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Generators for interval graph.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from collections.abc import Sequence
|
| 6 |
+
|
| 7 |
+
import networkx as nx
|
| 8 |
+
|
| 9 |
+
__all__ = ["interval_graph"]
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@nx._dispatchable(graphs=None, returns_graph=True)
|
| 13 |
+
def interval_graph(intervals):
|
| 14 |
+
"""Generates an interval graph for a list of intervals given.
|
| 15 |
+
|
| 16 |
+
In graph theory, an interval graph is an undirected graph formed from a set
|
| 17 |
+
of closed intervals on the real line, with a vertex for each interval
|
| 18 |
+
and an edge between vertices whose intervals intersect.
|
| 19 |
+
It is the intersection graph of the intervals.
|
| 20 |
+
|
| 21 |
+
More information can be found at:
|
| 22 |
+
https://en.wikipedia.org/wiki/Interval_graph
|
| 23 |
+
|
| 24 |
+
Parameters
|
| 25 |
+
----------
|
| 26 |
+
intervals : a sequence of intervals, say (l, r) where l is the left end,
|
| 27 |
+
and r is the right end of the closed interval.
|
| 28 |
+
|
| 29 |
+
Returns
|
| 30 |
+
-------
|
| 31 |
+
G : networkx graph
|
| 32 |
+
|
| 33 |
+
Examples
|
| 34 |
+
--------
|
| 35 |
+
>>> intervals = [(-2, 3), [1, 4], (2, 3), (4, 6)]
|
| 36 |
+
>>> G = nx.interval_graph(intervals)
|
| 37 |
+
>>> sorted(G.edges)
|
| 38 |
+
[((-2, 3), (1, 4)), ((-2, 3), (2, 3)), ((1, 4), (2, 3)), ((1, 4), (4, 6))]
|
| 39 |
+
|
| 40 |
+
Raises
|
| 41 |
+
------
|
| 42 |
+
:exc:`TypeError`
|
| 43 |
+
if `intervals` contains None or an element which is not
|
| 44 |
+
collections.abc.Sequence or not a length of 2.
|
| 45 |
+
:exc:`ValueError`
|
| 46 |
+
if `intervals` contains an interval such that min1 > max1
|
| 47 |
+
where min1,max1 = interval
|
| 48 |
+
"""
|
| 49 |
+
intervals = list(intervals)
|
| 50 |
+
for interval in intervals:
|
| 51 |
+
if not (isinstance(interval, Sequence) and len(interval) == 2):
|
| 52 |
+
raise TypeError(
|
| 53 |
+
"Each interval must have length 2, and be a "
|
| 54 |
+
"collections.abc.Sequence such as tuple or list."
|
| 55 |
+
)
|
| 56 |
+
if interval[0] > interval[1]:
|
| 57 |
+
raise ValueError(f"Interval must have lower value first. Got {interval}")
|
| 58 |
+
|
| 59 |
+
graph = nx.Graph()
|
| 60 |
+
|
| 61 |
+
tupled_intervals = [tuple(interval) for interval in intervals]
|
| 62 |
+
graph.add_nodes_from(tupled_intervals)
|
| 63 |
+
|
| 64 |
+
while tupled_intervals:
|
| 65 |
+
min1, max1 = interval1 = tupled_intervals.pop()
|
| 66 |
+
for interval2 in tupled_intervals:
|
| 67 |
+
min2, max2 = interval2
|
| 68 |
+
if max1 >= min2 and max2 >= min1:
|
| 69 |
+
graph.add_edge(interval1, interval2)
|
| 70 |
+
return graph
|
wemm/lib/python3.10/site-packages/networkx/generators/lattice.py
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Functions for generating grid graphs and lattices
|
| 2 |
+
|
| 3 |
+
The :func:`grid_2d_graph`, :func:`triangular_lattice_graph`, and
|
| 4 |
+
:func:`hexagonal_lattice_graph` functions correspond to the three
|
| 5 |
+
`regular tilings of the plane`_, the square, triangular, and hexagonal
|
| 6 |
+
tilings, respectively. :func:`grid_graph` and :func:`hypercube_graph`
|
| 7 |
+
are similar for arbitrary dimensions. Useful relevant discussion can
|
| 8 |
+
be found about `Triangular Tiling`_, and `Square, Hex and Triangle Grids`_
|
| 9 |
+
|
| 10 |
+
.. _regular tilings of the plane: https://en.wikipedia.org/wiki/List_of_regular_polytopes_and_compounds#Euclidean_tilings
|
| 11 |
+
.. _Square, Hex and Triangle Grids: http://www-cs-students.stanford.edu/~amitp/game-programming/grids/
|
| 12 |
+
.. _Triangular Tiling: https://en.wikipedia.org/wiki/Triangular_tiling
|
| 13 |
+
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
from itertools import repeat
|
| 17 |
+
from math import sqrt
|
| 18 |
+
|
| 19 |
+
import networkx as nx
|
| 20 |
+
from networkx.classes import set_node_attributes
|
| 21 |
+
from networkx.exception import NetworkXError
|
| 22 |
+
from networkx.generators.classic import cycle_graph, empty_graph, path_graph
|
| 23 |
+
from networkx.relabel import relabel_nodes
|
| 24 |
+
from networkx.utils import flatten, nodes_or_number, pairwise
|
| 25 |
+
|
| 26 |
+
__all__ = [
|
| 27 |
+
"grid_2d_graph",
|
| 28 |
+
"grid_graph",
|
| 29 |
+
"hypercube_graph",
|
| 30 |
+
"triangular_lattice_graph",
|
| 31 |
+
"hexagonal_lattice_graph",
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
@nx._dispatchable(graphs=None, returns_graph=True)
|
| 36 |
+
@nodes_or_number([0, 1])
|
| 37 |
+
def grid_2d_graph(m, n, periodic=False, create_using=None):
|
| 38 |
+
"""Returns the two-dimensional grid graph.
|
| 39 |
+
|
| 40 |
+
The grid graph has each node connected to its four nearest neighbors.
|
| 41 |
+
|
| 42 |
+
Parameters
|
| 43 |
+
----------
|
| 44 |
+
m, n : int or iterable container of nodes
|
| 45 |
+
If an integer, nodes are from `range(n)`.
|
| 46 |
+
If a container, elements become the coordinate of the nodes.
|
| 47 |
+
|
| 48 |
+
periodic : bool or iterable
|
| 49 |
+
If `periodic` is True, both dimensions are periodic. If False, none
|
| 50 |
+
are periodic. If `periodic` is iterable, it should yield 2 bool
|
| 51 |
+
values indicating whether the 1st and 2nd axes, respectively, are
|
| 52 |
+
periodic.
|
| 53 |
+
|
| 54 |
+
create_using : NetworkX graph constructor, optional (default=nx.Graph)
|
| 55 |
+
Graph type to create. If graph instance, then cleared before populated.
|
| 56 |
+
|
| 57 |
+
Returns
|
| 58 |
+
-------
|
| 59 |
+
NetworkX graph
|
| 60 |
+
The (possibly periodic) grid graph of the specified dimensions.
|
| 61 |
+
|
| 62 |
+
"""
|
| 63 |
+
G = empty_graph(0, create_using)
|
| 64 |
+
row_name, rows = m
|
| 65 |
+
col_name, cols = n
|
| 66 |
+
G.add_nodes_from((i, j) for i in rows for j in cols)
|
| 67 |
+
G.add_edges_from(((i, j), (pi, j)) for pi, i in pairwise(rows) for j in cols)
|
| 68 |
+
G.add_edges_from(((i, j), (i, pj)) for i in rows for pj, j in pairwise(cols))
|
| 69 |
+
|
| 70 |
+
try:
|
| 71 |
+
periodic_r, periodic_c = periodic
|
| 72 |
+
except TypeError:
|
| 73 |
+
periodic_r = periodic_c = periodic
|
| 74 |
+
|
| 75 |
+
if periodic_r and len(rows) > 2:
|
| 76 |
+
first = rows[0]
|
| 77 |
+
last = rows[-1]
|
| 78 |
+
G.add_edges_from(((first, j), (last, j)) for j in cols)
|
| 79 |
+
if periodic_c and len(cols) > 2:
|
| 80 |
+
first = cols[0]
|
| 81 |
+
last = cols[-1]
|
| 82 |
+
G.add_edges_from(((i, first), (i, last)) for i in rows)
|
| 83 |
+
# both directions for directed
|
| 84 |
+
if G.is_directed():
|
| 85 |
+
G.add_edges_from((v, u) for u, v in G.edges())
|
| 86 |
+
return G
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
@nx._dispatchable(graphs=None, returns_graph=True)
|
| 90 |
+
def grid_graph(dim, periodic=False):
|
| 91 |
+
"""Returns the *n*-dimensional grid graph.
|
| 92 |
+
|
| 93 |
+
The dimension *n* is the length of the list `dim` and the size in
|
| 94 |
+
each dimension is the value of the corresponding list element.
|
| 95 |
+
|
| 96 |
+
Parameters
|
| 97 |
+
----------
|
| 98 |
+
dim : list or tuple of numbers or iterables of nodes
|
| 99 |
+
'dim' is a tuple or list with, for each dimension, either a number
|
| 100 |
+
that is the size of that dimension or an iterable of nodes for
|
| 101 |
+
that dimension. The dimension of the grid_graph is the length
|
| 102 |
+
of `dim`.
|
| 103 |
+
|
| 104 |
+
periodic : bool or iterable
|
| 105 |
+
If `periodic` is True, all dimensions are periodic. If False all
|
| 106 |
+
dimensions are not periodic. If `periodic` is iterable, it should
|
| 107 |
+
yield `dim` bool values each of which indicates whether the
|
| 108 |
+
corresponding axis is periodic.
|
| 109 |
+
|
| 110 |
+
Returns
|
| 111 |
+
-------
|
| 112 |
+
NetworkX graph
|
| 113 |
+
The (possibly periodic) grid graph of the specified dimensions.
|
| 114 |
+
|
| 115 |
+
Examples
|
| 116 |
+
--------
|
| 117 |
+
To produce a 2 by 3 by 4 grid graph, a graph on 24 nodes:
|
| 118 |
+
|
| 119 |
+
>>> from networkx import grid_graph
|
| 120 |
+
>>> G = grid_graph(dim=(2, 3, 4))
|
| 121 |
+
>>> len(G)
|
| 122 |
+
24
|
| 123 |
+
>>> G = grid_graph(dim=(range(7, 9), range(3, 6)))
|
| 124 |
+
>>> len(G)
|
| 125 |
+
6
|
| 126 |
+
"""
|
| 127 |
+
from networkx.algorithms.operators.product import cartesian_product
|
| 128 |
+
|
| 129 |
+
if not dim:
|
| 130 |
+
return empty_graph(0)
|
| 131 |
+
|
| 132 |
+
try:
|
| 133 |
+
func = (cycle_graph if p else path_graph for p in periodic)
|
| 134 |
+
except TypeError:
|
| 135 |
+
func = repeat(cycle_graph if periodic else path_graph)
|
| 136 |
+
|
| 137 |
+
G = next(func)(dim[0])
|
| 138 |
+
for current_dim in dim[1:]:
|
| 139 |
+
Gnew = next(func)(current_dim)
|
| 140 |
+
G = cartesian_product(Gnew, G)
|
| 141 |
+
# graph G is done but has labels of the form (1, (2, (3, 1))) so relabel
|
| 142 |
+
H = relabel_nodes(G, flatten)
|
| 143 |
+
return H
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
@nx._dispatchable(graphs=None, returns_graph=True)
|
| 147 |
+
def hypercube_graph(n):
|
| 148 |
+
"""Returns the *n*-dimensional hypercube graph.
|
| 149 |
+
|
| 150 |
+
The nodes are the integers between 0 and ``2 ** n - 1``, inclusive.
|
| 151 |
+
|
| 152 |
+
For more information on the hypercube graph, see the Wikipedia
|
| 153 |
+
article `Hypercube graph`_.
|
| 154 |
+
|
| 155 |
+
.. _Hypercube graph: https://en.wikipedia.org/wiki/Hypercube_graph
|
| 156 |
+
|
| 157 |
+
Parameters
|
| 158 |
+
----------
|
| 159 |
+
n : int
|
| 160 |
+
The dimension of the hypercube.
|
| 161 |
+
The number of nodes in the graph will be ``2 ** n``.
|
| 162 |
+
|
| 163 |
+
Returns
|
| 164 |
+
-------
|
| 165 |
+
NetworkX graph
|
| 166 |
+
The hypercube graph of dimension *n*.
|
| 167 |
+
"""
|
| 168 |
+
dim = n * [2]
|
| 169 |
+
G = grid_graph(dim)
|
| 170 |
+
return G
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
@nx._dispatchable(graphs=None, returns_graph=True)
|
| 174 |
+
def triangular_lattice_graph(
|
| 175 |
+
m, n, periodic=False, with_positions=True, create_using=None
|
| 176 |
+
):
|
| 177 |
+
r"""Returns the $m$ by $n$ triangular lattice graph.
|
| 178 |
+
|
| 179 |
+
The `triangular lattice graph`_ is a two-dimensional `grid graph`_ in
|
| 180 |
+
which each square unit has a diagonal edge (each grid unit has a chord).
|
| 181 |
+
|
| 182 |
+
The returned graph has $m$ rows and $n$ columns of triangles. Rows and
|
| 183 |
+
columns include both triangles pointing up and down. Rows form a strip
|
| 184 |
+
of constant height. Columns form a series of diamond shapes, staggered
|
| 185 |
+
with the columns on either side. Another way to state the size is that
|
| 186 |
+
the nodes form a grid of `m+1` rows and `(n + 1) // 2` columns.
|
| 187 |
+
The odd row nodes are shifted horizontally relative to the even rows.
|
| 188 |
+
|
| 189 |
+
Directed graph types have edges pointed up or right.
|
| 190 |
+
|
| 191 |
+
Positions of nodes are computed by default or `with_positions is True`.
|
| 192 |
+
The position of each node (embedded in a euclidean plane) is stored in
|
| 193 |
+
the graph using equilateral triangles with sidelength 1.
|
| 194 |
+
The height between rows of nodes is thus $\sqrt(3)/2$.
|
| 195 |
+
Nodes lie in the first quadrant with the node $(0, 0)$ at the origin.
|
| 196 |
+
|
| 197 |
+
.. _triangular lattice graph: http://mathworld.wolfram.com/TriangularGrid.html
|
| 198 |
+
.. _grid graph: http://www-cs-students.stanford.edu/~amitp/game-programming/grids/
|
| 199 |
+
.. _Triangular Tiling: https://en.wikipedia.org/wiki/Triangular_tiling
|
| 200 |
+
|
| 201 |
+
Parameters
|
| 202 |
+
----------
|
| 203 |
+
m : int
|
| 204 |
+
The number of rows in the lattice.
|
| 205 |
+
|
| 206 |
+
n : int
|
| 207 |
+
The number of columns in the lattice.
|
| 208 |
+
|
| 209 |
+
periodic : bool (default: False)
|
| 210 |
+
If True, join the boundary vertices of the grid using periodic
|
| 211 |
+
boundary conditions. The join between boundaries is the final row
|
| 212 |
+
and column of triangles. This means there is one row and one column
|
| 213 |
+
fewer nodes for the periodic lattice. Periodic lattices require
|
| 214 |
+
`m >= 3`, `n >= 5` and are allowed but misaligned if `m` or `n` are odd
|
| 215 |
+
|
| 216 |
+
with_positions : bool (default: True)
|
| 217 |
+
Store the coordinates of each node in the graph node attribute 'pos'.
|
| 218 |
+
The coordinates provide a lattice with equilateral triangles.
|
| 219 |
+
Periodic positions shift the nodes vertically in a nonlinear way so
|
| 220 |
+
the edges don't overlap so much.
|
| 221 |
+
|
| 222 |
+
create_using : NetworkX graph constructor, optional (default=nx.Graph)
|
| 223 |
+
Graph type to create. If graph instance, then cleared before populated.
|
| 224 |
+
|
| 225 |
+
Returns
|
| 226 |
+
-------
|
| 227 |
+
NetworkX graph
|
| 228 |
+
The *m* by *n* triangular lattice graph.
|
| 229 |
+
"""
|
| 230 |
+
H = empty_graph(0, create_using)
|
| 231 |
+
if n == 0 or m == 0:
|
| 232 |
+
return H
|
| 233 |
+
if periodic:
|
| 234 |
+
if n < 5 or m < 3:
|
| 235 |
+
msg = f"m > 2 and n > 4 required for periodic. m={m}, n={n}"
|
| 236 |
+
raise NetworkXError(msg)
|
| 237 |
+
|
| 238 |
+
N = (n + 1) // 2 # number of nodes in row
|
| 239 |
+
rows = range(m + 1)
|
| 240 |
+
cols = range(N + 1)
|
| 241 |
+
# Make grid
|
| 242 |
+
H.add_edges_from(((i, j), (i + 1, j)) for j in rows for i in cols[:N])
|
| 243 |
+
H.add_edges_from(((i, j), (i, j + 1)) for j in rows[:m] for i in cols)
|
| 244 |
+
# add diagonals
|
| 245 |
+
H.add_edges_from(((i, j), (i + 1, j + 1)) for j in rows[1:m:2] for i in cols[:N])
|
| 246 |
+
H.add_edges_from(((i + 1, j), (i, j + 1)) for j in rows[:m:2] for i in cols[:N])
|
| 247 |
+
# identify boundary nodes if periodic
|
| 248 |
+
from networkx.algorithms.minors import contracted_nodes
|
| 249 |
+
|
| 250 |
+
if periodic is True:
|
| 251 |
+
for i in cols:
|
| 252 |
+
H = contracted_nodes(H, (i, 0), (i, m))
|
| 253 |
+
for j in rows[:m]:
|
| 254 |
+
H = contracted_nodes(H, (0, j), (N, j))
|
| 255 |
+
elif n % 2:
|
| 256 |
+
# remove extra nodes
|
| 257 |
+
H.remove_nodes_from((N, j) for j in rows[1::2])
|
| 258 |
+
|
| 259 |
+
# Add position node attributes
|
| 260 |
+
if with_positions:
|
| 261 |
+
ii = (i for i in cols for j in rows)
|
| 262 |
+
jj = (j for i in cols for j in rows)
|
| 263 |
+
xx = (0.5 * (j % 2) + i for i in cols for j in rows)
|
| 264 |
+
h = sqrt(3) / 2
|
| 265 |
+
if periodic:
|
| 266 |
+
yy = (h * j + 0.01 * i * i for i in cols for j in rows)
|
| 267 |
+
else:
|
| 268 |
+
yy = (h * j for i in cols for j in rows)
|
| 269 |
+
pos = {(i, j): (x, y) for i, j, x, y in zip(ii, jj, xx, yy) if (i, j) in H}
|
| 270 |
+
set_node_attributes(H, pos, "pos")
|
| 271 |
+
return H
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
@nx._dispatchable(graphs=None, returns_graph=True)
|
| 275 |
+
def hexagonal_lattice_graph(
|
| 276 |
+
m, n, periodic=False, with_positions=True, create_using=None
|
| 277 |
+
):
|
| 278 |
+
"""Returns an `m` by `n` hexagonal lattice graph.
|
| 279 |
+
|
| 280 |
+
The *hexagonal lattice graph* is a graph whose nodes and edges are
|
| 281 |
+
the `hexagonal tiling`_ of the plane.
|
| 282 |
+
|
| 283 |
+
The returned graph will have `m` rows and `n` columns of hexagons.
|
| 284 |
+
`Odd numbered columns`_ are shifted up relative to even numbered columns.
|
| 285 |
+
|
| 286 |
+
Positions of nodes are computed by default or `with_positions is True`.
|
| 287 |
+
Node positions creating the standard embedding in the plane
|
| 288 |
+
with sidelength 1 and are stored in the node attribute 'pos'.
|
| 289 |
+
`pos = nx.get_node_attributes(G, 'pos')` creates a dict ready for drawing.
|
| 290 |
+
|
| 291 |
+
.. _hexagonal tiling: https://en.wikipedia.org/wiki/Hexagonal_tiling
|
| 292 |
+
.. _Odd numbered columns: http://www-cs-students.stanford.edu/~amitp/game-programming/grids/
|
| 293 |
+
|
| 294 |
+
Parameters
|
| 295 |
+
----------
|
| 296 |
+
m : int
|
| 297 |
+
The number of rows of hexagons in the lattice.
|
| 298 |
+
|
| 299 |
+
n : int
|
| 300 |
+
The number of columns of hexagons in the lattice.
|
| 301 |
+
|
| 302 |
+
periodic : bool
|
| 303 |
+
Whether to make a periodic grid by joining the boundary vertices.
|
| 304 |
+
For this to work `n` must be even and both `n > 1` and `m > 1`.
|
| 305 |
+
The periodic connections create another row and column of hexagons
|
| 306 |
+
so these graphs have fewer nodes as boundary nodes are identified.
|
| 307 |
+
|
| 308 |
+
with_positions : bool (default: True)
|
| 309 |
+
Store the coordinates of each node in the graph node attribute 'pos'.
|
| 310 |
+
The coordinates provide a lattice with vertical columns of hexagons
|
| 311 |
+
offset to interleave and cover the plane.
|
| 312 |
+
Periodic positions shift the nodes vertically in a nonlinear way so
|
| 313 |
+
the edges don't overlap so much.
|
| 314 |
+
|
| 315 |
+
create_using : NetworkX graph constructor, optional (default=nx.Graph)
|
| 316 |
+
Graph type to create. If graph instance, then cleared before populated.
|
| 317 |
+
If graph is directed, edges will point up or right.
|
| 318 |
+
|
| 319 |
+
Returns
|
| 320 |
+
-------
|
| 321 |
+
NetworkX graph
|
| 322 |
+
The *m* by *n* hexagonal lattice graph.
|
| 323 |
+
"""
|
| 324 |
+
G = empty_graph(0, create_using)
|
| 325 |
+
if m == 0 or n == 0:
|
| 326 |
+
return G
|
| 327 |
+
if periodic and (n % 2 == 1 or m < 2 or n < 2):
|
| 328 |
+
msg = "periodic hexagonal lattice needs m > 1, n > 1 and even n"
|
| 329 |
+
raise NetworkXError(msg)
|
| 330 |
+
|
| 331 |
+
M = 2 * m # twice as many nodes as hexagons vertically
|
| 332 |
+
rows = range(M + 2)
|
| 333 |
+
cols = range(n + 1)
|
| 334 |
+
# make lattice
|
| 335 |
+
col_edges = (((i, j), (i, j + 1)) for i in cols for j in rows[: M + 1])
|
| 336 |
+
row_edges = (((i, j), (i + 1, j)) for i in cols[:n] for j in rows if i % 2 == j % 2)
|
| 337 |
+
G.add_edges_from(col_edges)
|
| 338 |
+
G.add_edges_from(row_edges)
|
| 339 |
+
# Remove corner nodes with one edge
|
| 340 |
+
G.remove_node((0, M + 1))
|
| 341 |
+
G.remove_node((n, (M + 1) * (n % 2)))
|
| 342 |
+
|
| 343 |
+
# identify boundary nodes if periodic
|
| 344 |
+
from networkx.algorithms.minors import contracted_nodes
|
| 345 |
+
|
| 346 |
+
if periodic:
|
| 347 |
+
for i in cols[:n]:
|
| 348 |
+
G = contracted_nodes(G, (i, 0), (i, M))
|
| 349 |
+
for i in cols[1:]:
|
| 350 |
+
G = contracted_nodes(G, (i, 1), (i, M + 1))
|
| 351 |
+
for j in rows[1:M]:
|
| 352 |
+
G = contracted_nodes(G, (0, j), (n, j))
|
| 353 |
+
G.remove_node((n, M))
|
| 354 |
+
|
| 355 |
+
# calc position in embedded space
|
| 356 |
+
ii = (i for i in cols for j in rows)
|
| 357 |
+
jj = (j for i in cols for j in rows)
|
| 358 |
+
xx = (0.5 + i + i // 2 + (j % 2) * ((i % 2) - 0.5) for i in cols for j in rows)
|
| 359 |
+
h = sqrt(3) / 2
|
| 360 |
+
if periodic:
|
| 361 |
+
yy = (h * j + 0.01 * i * i for i in cols for j in rows)
|
| 362 |
+
else:
|
| 363 |
+
yy = (h * j for i in cols for j in rows)
|
| 364 |
+
# exclude nodes not in G
|
| 365 |
+
pos = {(i, j): (x, y) for i, j, x, y in zip(ii, jj, xx, yy) if (i, j) in G}
|
| 366 |
+
set_node_attributes(G, pos, "pos")
|
| 367 |
+
return G
|
wemm/lib/python3.10/site-packages/networkx/generators/nonisomorphic_trees.py
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Implementation of the Wright, Richmond, Odlyzko and McKay (WROM)
|
| 3 |
+
algorithm for the enumeration of all non-isomorphic free trees of a
|
| 4 |
+
given order. Rooted trees are represented by level sequences, i.e.,
|
| 5 |
+
lists in which the i-th element specifies the distance of vertex i to
|
| 6 |
+
the root.
|
| 7 |
+
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
__all__ = ["nonisomorphic_trees", "number_of_nonisomorphic_trees"]
|
| 11 |
+
|
| 12 |
+
import networkx as nx
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@nx._dispatchable(graphs=None, returns_graph=True)
|
| 16 |
+
def nonisomorphic_trees(order, create="graph"):
|
| 17 |
+
"""Generates lists of nonisomorphic trees
|
| 18 |
+
|
| 19 |
+
Parameters
|
| 20 |
+
----------
|
| 21 |
+
order : int
|
| 22 |
+
order of the desired tree(s)
|
| 23 |
+
|
| 24 |
+
create : one of {"graph", "matrix"} (default="graph")
|
| 25 |
+
If ``"graph"`` is selected a list of ``Graph`` instances will be returned,
|
| 26 |
+
if matrix is selected a list of adjacency matrices will be returned.
|
| 27 |
+
|
| 28 |
+
.. deprecated:: 3.3
|
| 29 |
+
|
| 30 |
+
The `create` argument is deprecated and will be removed in NetworkX
|
| 31 |
+
version 3.5. In the future, `nonisomorphic_trees` will yield graph
|
| 32 |
+
instances by default. To generate adjacency matrices, call
|
| 33 |
+
``nx.to_numpy_array`` on the output, e.g.::
|
| 34 |
+
|
| 35 |
+
[nx.to_numpy_array(G) for G in nx.nonisomorphic_trees(N)]
|
| 36 |
+
|
| 37 |
+
Yields
|
| 38 |
+
------
|
| 39 |
+
list
|
| 40 |
+
A list of nonisomorphic trees, in one of two formats depending on the
|
| 41 |
+
value of the `create` parameter:
|
| 42 |
+
- ``create="graph"``: yields a list of `networkx.Graph` instances
|
| 43 |
+
- ``create="matrix"``: yields a list of list-of-lists representing adjacency matrices
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
if order < 2:
|
| 47 |
+
raise ValueError
|
| 48 |
+
# start at the path graph rooted at its center
|
| 49 |
+
layout = list(range(order // 2 + 1)) + list(range(1, (order + 1) // 2))
|
| 50 |
+
|
| 51 |
+
while layout is not None:
|
| 52 |
+
layout = _next_tree(layout)
|
| 53 |
+
if layout is not None:
|
| 54 |
+
if create == "graph":
|
| 55 |
+
yield _layout_to_graph(layout)
|
| 56 |
+
elif create == "matrix":
|
| 57 |
+
import warnings
|
| 58 |
+
|
| 59 |
+
warnings.warn(
|
| 60 |
+
(
|
| 61 |
+
"\n\nThe 'create=matrix' argument of nonisomorphic_trees\n"
|
| 62 |
+
"is deprecated and will be removed in version 3.5.\n"
|
| 63 |
+
"Use ``nx.to_numpy_array`` to convert graphs to adjacency "
|
| 64 |
+
"matrices, e.g.::\n\n"
|
| 65 |
+
" [nx.to_numpy_array(G) for G in nx.nonisomorphic_trees(N)]"
|
| 66 |
+
),
|
| 67 |
+
category=DeprecationWarning,
|
| 68 |
+
stacklevel=2,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
yield _layout_to_matrix(layout)
|
| 72 |
+
layout = _next_rooted_tree(layout)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
@nx._dispatchable(graphs=None)
|
| 76 |
+
def number_of_nonisomorphic_trees(order):
|
| 77 |
+
"""Returns the number of nonisomorphic trees
|
| 78 |
+
|
| 79 |
+
Parameters
|
| 80 |
+
----------
|
| 81 |
+
order : int
|
| 82 |
+
order of the desired tree(s)
|
| 83 |
+
|
| 84 |
+
Returns
|
| 85 |
+
-------
|
| 86 |
+
length : Number of nonisomorphic graphs for the given order
|
| 87 |
+
|
| 88 |
+
References
|
| 89 |
+
----------
|
| 90 |
+
|
| 91 |
+
"""
|
| 92 |
+
return sum(1 for _ in nonisomorphic_trees(order))
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def _next_rooted_tree(predecessor, p=None):
|
| 96 |
+
"""One iteration of the Beyer-Hedetniemi algorithm."""
|
| 97 |
+
|
| 98 |
+
if p is None:
|
| 99 |
+
p = len(predecessor) - 1
|
| 100 |
+
while predecessor[p] == 1:
|
| 101 |
+
p -= 1
|
| 102 |
+
if p == 0:
|
| 103 |
+
return None
|
| 104 |
+
|
| 105 |
+
q = p - 1
|
| 106 |
+
while predecessor[q] != predecessor[p] - 1:
|
| 107 |
+
q -= 1
|
| 108 |
+
result = list(predecessor)
|
| 109 |
+
for i in range(p, len(result)):
|
| 110 |
+
result[i] = result[i - p + q]
|
| 111 |
+
return result
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def _next_tree(candidate):
|
| 115 |
+
"""One iteration of the Wright, Richmond, Odlyzko and McKay
|
| 116 |
+
algorithm."""
|
| 117 |
+
|
| 118 |
+
# valid representation of a free tree if:
|
| 119 |
+
# there are at least two vertices at layer 1
|
| 120 |
+
# (this is always the case because we start at the path graph)
|
| 121 |
+
left, rest = _split_tree(candidate)
|
| 122 |
+
|
| 123 |
+
# and the left subtree of the root
|
| 124 |
+
# is less high than the tree with the left subtree removed
|
| 125 |
+
left_height = max(left)
|
| 126 |
+
rest_height = max(rest)
|
| 127 |
+
valid = rest_height >= left_height
|
| 128 |
+
|
| 129 |
+
if valid and rest_height == left_height:
|
| 130 |
+
# and, if left and rest are of the same height,
|
| 131 |
+
# if left does not encompass more vertices
|
| 132 |
+
if len(left) > len(rest):
|
| 133 |
+
valid = False
|
| 134 |
+
# and, if they have the same number or vertices,
|
| 135 |
+
# if left does not come after rest lexicographically
|
| 136 |
+
elif len(left) == len(rest) and left > rest:
|
| 137 |
+
valid = False
|
| 138 |
+
|
| 139 |
+
if valid:
|
| 140 |
+
return candidate
|
| 141 |
+
else:
|
| 142 |
+
# jump to the next valid free tree
|
| 143 |
+
p = len(left)
|
| 144 |
+
new_candidate = _next_rooted_tree(candidate, p)
|
| 145 |
+
if candidate[p] > 2:
|
| 146 |
+
new_left, new_rest = _split_tree(new_candidate)
|
| 147 |
+
new_left_height = max(new_left)
|
| 148 |
+
suffix = range(1, new_left_height + 2)
|
| 149 |
+
new_candidate[-len(suffix) :] = suffix
|
| 150 |
+
return new_candidate
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
def _split_tree(layout):
|
| 154 |
+
"""Returns a tuple of two layouts, one containing the left
|
| 155 |
+
subtree of the root vertex, and one containing the original tree
|
| 156 |
+
with the left subtree removed."""
|
| 157 |
+
|
| 158 |
+
one_found = False
|
| 159 |
+
m = None
|
| 160 |
+
for i in range(len(layout)):
|
| 161 |
+
if layout[i] == 1:
|
| 162 |
+
if one_found:
|
| 163 |
+
m = i
|
| 164 |
+
break
|
| 165 |
+
else:
|
| 166 |
+
one_found = True
|
| 167 |
+
|
| 168 |
+
if m is None:
|
| 169 |
+
m = len(layout)
|
| 170 |
+
|
| 171 |
+
left = [layout[i] - 1 for i in range(1, m)]
|
| 172 |
+
rest = [0] + [layout[i] for i in range(m, len(layout))]
|
| 173 |
+
return (left, rest)
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def _layout_to_matrix(layout):
|
| 177 |
+
"""Create the adjacency matrix for the tree specified by the
|
| 178 |
+
given layout (level sequence)."""
|
| 179 |
+
|
| 180 |
+
result = [[0] * len(layout) for i in range(len(layout))]
|
| 181 |
+
stack = []
|
| 182 |
+
for i in range(len(layout)):
|
| 183 |
+
i_level = layout[i]
|
| 184 |
+
if stack:
|
| 185 |
+
j = stack[-1]
|
| 186 |
+
j_level = layout[j]
|
| 187 |
+
while j_level >= i_level:
|
| 188 |
+
stack.pop()
|
| 189 |
+
j = stack[-1]
|
| 190 |
+
j_level = layout[j]
|
| 191 |
+
result[i][j] = result[j][i] = 1
|
| 192 |
+
stack.append(i)
|
| 193 |
+
return result
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
def _layout_to_graph(layout):
|
| 197 |
+
"""Create a NetworkX Graph for the tree specified by the
|
| 198 |
+
given layout(level sequence)"""
|
| 199 |
+
G = nx.Graph()
|
| 200 |
+
stack = []
|
| 201 |
+
for i in range(len(layout)):
|
| 202 |
+
i_level = layout[i]
|
| 203 |
+
if stack:
|
| 204 |
+
j = stack[-1]
|
| 205 |
+
j_level = layout[j]
|
| 206 |
+
while j_level >= i_level:
|
| 207 |
+
stack.pop()
|
| 208 |
+
j = stack[-1]
|
| 209 |
+
j_level = layout[j]
|
| 210 |
+
G.add_edge(i, j)
|
| 211 |
+
stack.append(i)
|
| 212 |
+
return G
|
wemm/lib/python3.10/site-packages/networkx/generators/random_clustered.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Generate graphs with given degree and triangle sequence."""
|
| 2 |
+
|
| 3 |
+
import networkx as nx
|
| 4 |
+
from networkx.utils import py_random_state
|
| 5 |
+
|
| 6 |
+
__all__ = ["random_clustered_graph"]
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
@py_random_state(2)
|
| 10 |
+
@nx._dispatchable(graphs=None, returns_graph=True)
|
| 11 |
+
def random_clustered_graph(joint_degree_sequence, create_using=None, seed=None):
|
| 12 |
+
r"""Generate a random graph with the given joint independent edge degree and
|
| 13 |
+
triangle degree sequence.
|
| 14 |
+
|
| 15 |
+
This uses a configuration model-like approach to generate a random graph
|
| 16 |
+
(with parallel edges and self-loops) by randomly assigning edges to match
|
| 17 |
+
the given joint degree sequence.
|
| 18 |
+
|
| 19 |
+
The joint degree sequence is a list of pairs of integers of the form
|
| 20 |
+
$[(d_{1,i}, d_{1,t}), \dotsc, (d_{n,i}, d_{n,t})]$. According to this list,
|
| 21 |
+
vertex $u$ is a member of $d_{u,t}$ triangles and has $d_{u, i}$ other
|
| 22 |
+
edges. The number $d_{u,t}$ is the *triangle degree* of $u$ and the number
|
| 23 |
+
$d_{u,i}$ is the *independent edge degree*.
|
| 24 |
+
|
| 25 |
+
Parameters
|
| 26 |
+
----------
|
| 27 |
+
joint_degree_sequence : list of integer pairs
|
| 28 |
+
Each list entry corresponds to the independent edge degree and
|
| 29 |
+
triangle degree of a node.
|
| 30 |
+
create_using : NetworkX graph constructor, optional (default MultiGraph)
|
| 31 |
+
Graph type to create. If graph instance, then cleared before populated.
|
| 32 |
+
seed : integer, random_state, or None (default)
|
| 33 |
+
Indicator of random number generation state.
|
| 34 |
+
See :ref:`Randomness<randomness>`.
|
| 35 |
+
|
| 36 |
+
Returns
|
| 37 |
+
-------
|
| 38 |
+
G : MultiGraph
|
| 39 |
+
A graph with the specified degree sequence. Nodes are labeled
|
| 40 |
+
starting at 0 with an index corresponding to the position in
|
| 41 |
+
deg_sequence.
|
| 42 |
+
|
| 43 |
+
Raises
|
| 44 |
+
------
|
| 45 |
+
NetworkXError
|
| 46 |
+
If the independent edge degree sequence sum is not even
|
| 47 |
+
or the triangle degree sequence sum is not divisible by 3.
|
| 48 |
+
|
| 49 |
+
Notes
|
| 50 |
+
-----
|
| 51 |
+
As described by Miller [1]_ (see also Newman [2]_ for an equivalent
|
| 52 |
+
description).
|
| 53 |
+
|
| 54 |
+
A non-graphical degree sequence (not realizable by some simple
|
| 55 |
+
graph) is allowed since this function returns graphs with self
|
| 56 |
+
loops and parallel edges. An exception is raised if the
|
| 57 |
+
independent degree sequence does not have an even sum or the
|
| 58 |
+
triangle degree sequence sum is not divisible by 3.
|
| 59 |
+
|
| 60 |
+
This configuration model-like construction process can lead to
|
| 61 |
+
duplicate edges and loops. You can remove the self-loops and
|
| 62 |
+
parallel edges (see below) which will likely result in a graph
|
| 63 |
+
that doesn't have the exact degree sequence specified. This
|
| 64 |
+
"finite-size effect" decreases as the size of the graph increases.
|
| 65 |
+
|
| 66 |
+
References
|
| 67 |
+
----------
|
| 68 |
+
.. [1] Joel C. Miller. "Percolation and epidemics in random clustered
|
| 69 |
+
networks". In: Physical review. E, Statistical, nonlinear, and soft
|
| 70 |
+
matter physics 80 (2 Part 1 August 2009).
|
| 71 |
+
.. [2] M. E. J. Newman. "Random Graphs with Clustering".
|
| 72 |
+
In: Physical Review Letters 103 (5 July 2009)
|
| 73 |
+
|
| 74 |
+
Examples
|
| 75 |
+
--------
|
| 76 |
+
>>> deg = [(1, 0), (1, 0), (1, 0), (2, 0), (1, 0), (2, 1), (0, 1), (0, 1)]
|
| 77 |
+
>>> G = nx.random_clustered_graph(deg)
|
| 78 |
+
|
| 79 |
+
To remove parallel edges:
|
| 80 |
+
|
| 81 |
+
>>> G = nx.Graph(G)
|
| 82 |
+
|
| 83 |
+
To remove self loops:
|
| 84 |
+
|
| 85 |
+
>>> G.remove_edges_from(nx.selfloop_edges(G))
|
| 86 |
+
|
| 87 |
+
"""
|
| 88 |
+
# In Python 3, zip() returns an iterator. Make this into a list.
|
| 89 |
+
joint_degree_sequence = list(joint_degree_sequence)
|
| 90 |
+
|
| 91 |
+
N = len(joint_degree_sequence)
|
| 92 |
+
G = nx.empty_graph(N, create_using, default=nx.MultiGraph)
|
| 93 |
+
if G.is_directed():
|
| 94 |
+
raise nx.NetworkXError("Directed Graph not supported")
|
| 95 |
+
|
| 96 |
+
ilist = []
|
| 97 |
+
tlist = []
|
| 98 |
+
for n in G:
|
| 99 |
+
degrees = joint_degree_sequence[n]
|
| 100 |
+
for icount in range(degrees[0]):
|
| 101 |
+
ilist.append(n)
|
| 102 |
+
for tcount in range(degrees[1]):
|
| 103 |
+
tlist.append(n)
|
| 104 |
+
|
| 105 |
+
if len(ilist) % 2 != 0 or len(tlist) % 3 != 0:
|
| 106 |
+
raise nx.NetworkXError("Invalid degree sequence")
|
| 107 |
+
|
| 108 |
+
seed.shuffle(ilist)
|
| 109 |
+
seed.shuffle(tlist)
|
| 110 |
+
while ilist:
|
| 111 |
+
G.add_edge(ilist.pop(), ilist.pop())
|
| 112 |
+
while tlist:
|
| 113 |
+
n1 = tlist.pop()
|
| 114 |
+
n2 = tlist.pop()
|
| 115 |
+
n3 = tlist.pop()
|
| 116 |
+
G.add_edges_from([(n1, n2), (n1, n3), (n2, n3)])
|
| 117 |
+
return G
|
wemm/lib/python3.10/site-packages/networkx/generators/social.py
ADDED
|
@@ -0,0 +1,554 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Famous social networks.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import networkx as nx
|
| 6 |
+
|
| 7 |
+
__all__ = [
|
| 8 |
+
"karate_club_graph",
|
| 9 |
+
"davis_southern_women_graph",
|
| 10 |
+
"florentine_families_graph",
|
| 11 |
+
"les_miserables_graph",
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@nx._dispatchable(graphs=None, returns_graph=True)
|
| 16 |
+
def karate_club_graph():
|
| 17 |
+
"""Returns Zachary's Karate Club graph.
|
| 18 |
+
|
| 19 |
+
Each node in the returned graph has a node attribute 'club' that
|
| 20 |
+
indicates the name of the club to which the member represented by that node
|
| 21 |
+
belongs, either 'Mr. Hi' or 'Officer'. Each edge has a weight based on the
|
| 22 |
+
number of contexts in which that edge's incident node members interacted.
|
| 23 |
+
|
| 24 |
+
The dataset is derived from the 'Club After Split From Data' column of Table 3 in [1]_.
|
| 25 |
+
This was in turn derived from the 'Club After Fission' column of Table 1 in the
|
| 26 |
+
same paper. Note that the nodes are 0-indexed in NetworkX, but 1-indexed in the
|
| 27 |
+
paper (the 'Individual Number in Matrix C' column of Table 3 starts at 1). This
|
| 28 |
+
means, for example, that ``G.nodes[9]["club"]`` returns 'Officer', which
|
| 29 |
+
corresponds to row 10 of Table 3 in the paper.
|
| 30 |
+
|
| 31 |
+
Examples
|
| 32 |
+
--------
|
| 33 |
+
To get the name of the club to which a node belongs::
|
| 34 |
+
|
| 35 |
+
>>> G = nx.karate_club_graph()
|
| 36 |
+
>>> G.nodes[5]["club"]
|
| 37 |
+
'Mr. Hi'
|
| 38 |
+
>>> G.nodes[9]["club"]
|
| 39 |
+
'Officer'
|
| 40 |
+
|
| 41 |
+
References
|
| 42 |
+
----------
|
| 43 |
+
.. [1] Zachary, Wayne W.
|
| 44 |
+
"An Information Flow Model for Conflict and Fission in Small Groups."
|
| 45 |
+
*Journal of Anthropological Research*, 33, 452--473, (1977).
|
| 46 |
+
"""
|
| 47 |
+
# Create the set of all members, and the members of each club.
|
| 48 |
+
all_members = set(range(34))
|
| 49 |
+
club1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 16, 17, 19, 21}
|
| 50 |
+
# club2 = all_members - club1
|
| 51 |
+
|
| 52 |
+
G = nx.Graph()
|
| 53 |
+
G.add_nodes_from(all_members)
|
| 54 |
+
G.name = "Zachary's Karate Club"
|
| 55 |
+
|
| 56 |
+
zacharydat = """\
|
| 57 |
+
0 4 5 3 3 3 3 2 2 0 2 3 2 3 0 0 0 2 0 2 0 2 0 0 0 0 0 0 0 0 0 2 0 0
|
| 58 |
+
4 0 6 3 0 0 0 4 0 0 0 0 0 5 0 0 0 1 0 2 0 2 0 0 0 0 0 0 0 0 2 0 0 0
|
| 59 |
+
5 6 0 3 0 0 0 4 5 1 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 0 0 0 3 0
|
| 60 |
+
3 3 3 0 0 0 0 3 0 0 0 0 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
| 61 |
+
3 0 0 0 0 0 2 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
| 62 |
+
3 0 0 0 0 0 5 0 0 0 3 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
| 63 |
+
3 0 0 0 2 5 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
| 64 |
+
2 4 4 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
| 65 |
+
2 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 4 3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row):
|
| 95 |
+
if entry >= 1:
|
| 96 |
+
G.add_edge(row, col, weight=entry)
|
| 97 |
+
|
| 98 |
+
# Add the name of each member's club as a node attribute.
|
| 99 |
+
for v in G:
|
| 100 |
+
G.nodes[v]["club"] = "Mr. Hi" if v in club1 else "Officer"
|
| 101 |
+
return G
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
@nx._dispatchable(graphs=None, returns_graph=True)
|
| 105 |
+
def davis_southern_women_graph():
|
| 106 |
+
"""Returns Davis Southern women social network.
|
| 107 |
+
|
| 108 |
+
This is a bipartite graph.
|
| 109 |
+
|
| 110 |
+
References
|
| 111 |
+
----------
|
| 112 |
+
.. [1] A. Davis, Gardner, B. B., Gardner, M. R., 1941. Deep South.
|
| 113 |
+
University of Chicago Press, Chicago, IL.
|
| 114 |
+
"""
|
| 115 |
+
G = nx.Graph()
|
| 116 |
+
# Top nodes
|
| 117 |
+
women = [
|
| 118 |
+
"Evelyn Jefferson",
|
| 119 |
+
"Laura Mandeville",
|
| 120 |
+
"Theresa Anderson",
|
| 121 |
+
"Brenda Rogers",
|
| 122 |
+
"Charlotte McDowd",
|
| 123 |
+
"Frances Anderson",
|
| 124 |
+
"Eleanor Nye",
|
| 125 |
+
"Pearl Oglethorpe",
|
| 126 |
+
"Ruth DeSand",
|
| 127 |
+
"Verne Sanderson",
|
| 128 |
+
"Myra Liddel",
|
| 129 |
+
"Katherina Rogers",
|
| 130 |
+
"Sylvia Avondale",
|
| 131 |
+
"Nora Fayette",
|
| 132 |
+
"Helen Lloyd",
|
| 133 |
+
"Dorothy Murchison",
|
| 134 |
+
"Olivia Carleton",
|
| 135 |
+
"Flora Price",
|
| 136 |
+
]
|
| 137 |
+
G.add_nodes_from(women, bipartite=0)
|
| 138 |
+
# Bottom nodes
|
| 139 |
+
events = [
|
| 140 |
+
"E1",
|
| 141 |
+
"E2",
|
| 142 |
+
"E3",
|
| 143 |
+
"E4",
|
| 144 |
+
"E5",
|
| 145 |
+
"E6",
|
| 146 |
+
"E7",
|
| 147 |
+
"E8",
|
| 148 |
+
"E9",
|
| 149 |
+
"E10",
|
| 150 |
+
"E11",
|
| 151 |
+
"E12",
|
| 152 |
+
"E13",
|
| 153 |
+
"E14",
|
| 154 |
+
]
|
| 155 |
+
G.add_nodes_from(events, bipartite=1)
|
| 156 |
+
|
| 157 |
+
G.add_edges_from(
|
| 158 |
+
[
|
| 159 |
+
("Evelyn Jefferson", "E1"),
|
| 160 |
+
("Evelyn Jefferson", "E2"),
|
| 161 |
+
("Evelyn Jefferson", "E3"),
|
| 162 |
+
("Evelyn Jefferson", "E4"),
|
| 163 |
+
("Evelyn Jefferson", "E5"),
|
| 164 |
+
("Evelyn Jefferson", "E6"),
|
| 165 |
+
("Evelyn Jefferson", "E8"),
|
| 166 |
+
("Evelyn Jefferson", "E9"),
|
| 167 |
+
("Laura Mandeville", "E1"),
|
| 168 |
+
("Laura Mandeville", "E2"),
|
| 169 |
+
("Laura Mandeville", "E3"),
|
| 170 |
+
("Laura Mandeville", "E5"),
|
| 171 |
+
("Laura Mandeville", "E6"),
|
| 172 |
+
("Laura Mandeville", "E7"),
|
| 173 |
+
("Laura Mandeville", "E8"),
|
| 174 |
+
("Theresa Anderson", "E2"),
|
| 175 |
+
("Theresa Anderson", "E3"),
|
| 176 |
+
("Theresa Anderson", "E4"),
|
| 177 |
+
("Theresa Anderson", "E5"),
|
| 178 |
+
("Theresa Anderson", "E6"),
|
| 179 |
+
("Theresa Anderson", "E7"),
|
| 180 |
+
("Theresa Anderson", "E8"),
|
| 181 |
+
("Theresa Anderson", "E9"),
|
| 182 |
+
("Brenda Rogers", "E1"),
|
| 183 |
+
("Brenda Rogers", "E3"),
|
| 184 |
+
("Brenda Rogers", "E4"),
|
| 185 |
+
("Brenda Rogers", "E5"),
|
| 186 |
+
("Brenda Rogers", "E6"),
|
| 187 |
+
("Brenda Rogers", "E7"),
|
| 188 |
+
("Brenda Rogers", "E8"),
|
| 189 |
+
("Charlotte McDowd", "E3"),
|
| 190 |
+
("Charlotte McDowd", "E4"),
|
| 191 |
+
("Charlotte McDowd", "E5"),
|
| 192 |
+
("Charlotte McDowd", "E7"),
|
| 193 |
+
("Frances Anderson", "E3"),
|
| 194 |
+
("Frances Anderson", "E5"),
|
| 195 |
+
("Frances Anderson", "E6"),
|
| 196 |
+
("Frances Anderson", "E8"),
|
| 197 |
+
("Eleanor Nye", "E5"),
|
| 198 |
+
("Eleanor Nye", "E6"),
|
| 199 |
+
("Eleanor Nye", "E7"),
|
| 200 |
+
("Eleanor Nye", "E8"),
|
| 201 |
+
("Pearl Oglethorpe", "E6"),
|
| 202 |
+
("Pearl Oglethorpe", "E8"),
|
| 203 |
+
("Pearl Oglethorpe", "E9"),
|
| 204 |
+
("Ruth DeSand", "E5"),
|
| 205 |
+
("Ruth DeSand", "E7"),
|
| 206 |
+
("Ruth DeSand", "E8"),
|
| 207 |
+
("Ruth DeSand", "E9"),
|
| 208 |
+
("Verne Sanderson", "E7"),
|
| 209 |
+
("Verne Sanderson", "E8"),
|
| 210 |
+
("Verne Sanderson", "E9"),
|
| 211 |
+
("Verne Sanderson", "E12"),
|
| 212 |
+
("Myra Liddel", "E8"),
|
| 213 |
+
("Myra Liddel", "E9"),
|
| 214 |
+
("Myra Liddel", "E10"),
|
| 215 |
+
("Myra Liddel", "E12"),
|
| 216 |
+
("Katherina Rogers", "E8"),
|
| 217 |
+
("Katherina Rogers", "E9"),
|
| 218 |
+
("Katherina Rogers", "E10"),
|
| 219 |
+
("Katherina Rogers", "E12"),
|
| 220 |
+
("Katherina Rogers", "E13"),
|
| 221 |
+
("Katherina Rogers", "E14"),
|
| 222 |
+
("Sylvia Avondale", "E7"),
|
| 223 |
+
("Sylvia Avondale", "E8"),
|
| 224 |
+
("Sylvia Avondale", "E9"),
|
| 225 |
+
("Sylvia Avondale", "E10"),
|
| 226 |
+
("Sylvia Avondale", "E12"),
|
| 227 |
+
("Sylvia Avondale", "E13"),
|
| 228 |
+
("Sylvia Avondale", "E14"),
|
| 229 |
+
("Nora Fayette", "E6"),
|
| 230 |
+
("Nora Fayette", "E7"),
|
| 231 |
+
("Nora Fayette", "E9"),
|
| 232 |
+
("Nora Fayette", "E10"),
|
| 233 |
+
("Nora Fayette", "E11"),
|
| 234 |
+
("Nora Fayette", "E12"),
|
| 235 |
+
("Nora Fayette", "E13"),
|
| 236 |
+
("Nora Fayette", "E14"),
|
| 237 |
+
("Helen Lloyd", "E7"),
|
| 238 |
+
("Helen Lloyd", "E8"),
|
| 239 |
+
("Helen Lloyd", "E10"),
|
| 240 |
+
("Helen Lloyd", "E11"),
|
| 241 |
+
("Helen Lloyd", "E12"),
|
| 242 |
+
("Dorothy Murchison", "E8"),
|
| 243 |
+
("Dorothy Murchison", "E9"),
|
| 244 |
+
("Olivia Carleton", "E9"),
|
| 245 |
+
("Olivia Carleton", "E11"),
|
| 246 |
+
("Flora Price", "E9"),
|
| 247 |
+
("Flora Price", "E11"),
|
| 248 |
+
]
|
| 249 |
+
)
|
| 250 |
+
G.graph["top"] = women
|
| 251 |
+
G.graph["bottom"] = events
|
| 252 |
+
return G
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
@nx._dispatchable(graphs=None, returns_graph=True)
|
| 256 |
+
def florentine_families_graph():
|
| 257 |
+
"""Returns Florentine families graph.
|
| 258 |
+
|
| 259 |
+
References
|
| 260 |
+
----------
|
| 261 |
+
.. [1] Ronald L. Breiger and Philippa E. Pattison
|
| 262 |
+
Cumulated social roles: The duality of persons and their algebras,1
|
| 263 |
+
Social Networks, Volume 8, Issue 3, September 1986, Pages 215-256
|
| 264 |
+
"""
|
| 265 |
+
G = nx.Graph()
|
| 266 |
+
G.add_edge("Acciaiuoli", "Medici")
|
| 267 |
+
G.add_edge("Castellani", "Peruzzi")
|
| 268 |
+
G.add_edge("Castellani", "Strozzi")
|
| 269 |
+
G.add_edge("Castellani", "Barbadori")
|
| 270 |
+
G.add_edge("Medici", "Barbadori")
|
| 271 |
+
G.add_edge("Medici", "Ridolfi")
|
| 272 |
+
G.add_edge("Medici", "Tornabuoni")
|
| 273 |
+
G.add_edge("Medici", "Albizzi")
|
| 274 |
+
G.add_edge("Medici", "Salviati")
|
| 275 |
+
G.add_edge("Salviati", "Pazzi")
|
| 276 |
+
G.add_edge("Peruzzi", "Strozzi")
|
| 277 |
+
G.add_edge("Peruzzi", "Bischeri")
|
| 278 |
+
G.add_edge("Strozzi", "Ridolfi")
|
| 279 |
+
G.add_edge("Strozzi", "Bischeri")
|
| 280 |
+
G.add_edge("Ridolfi", "Tornabuoni")
|
| 281 |
+
G.add_edge("Tornabuoni", "Guadagni")
|
| 282 |
+
G.add_edge("Albizzi", "Ginori")
|
| 283 |
+
G.add_edge("Albizzi", "Guadagni")
|
| 284 |
+
G.add_edge("Bischeri", "Guadagni")
|
| 285 |
+
G.add_edge("Guadagni", "Lamberteschi")
|
| 286 |
+
return G
|
| 287 |
+
|
| 288 |
+
|
| 289 |
+
@nx._dispatchable(graphs=None, returns_graph=True)
|
| 290 |
+
def les_miserables_graph():
|
| 291 |
+
"""Returns coappearance network of characters in the novel Les Miserables.
|
| 292 |
+
|
| 293 |
+
References
|
| 294 |
+
----------
|
| 295 |
+
.. [1] D. E. Knuth, 1993.
|
| 296 |
+
The Stanford GraphBase: a platform for combinatorial computing,
|
| 297 |
+
pp. 74-87. New York: AcM Press.
|
| 298 |
+
"""
|
| 299 |
+
G = nx.Graph()
|
| 300 |
+
G.add_edge("Napoleon", "Myriel", weight=1)
|
| 301 |
+
G.add_edge("MlleBaptistine", "Myriel", weight=8)
|
| 302 |
+
G.add_edge("MmeMagloire", "Myriel", weight=10)
|
| 303 |
+
G.add_edge("MmeMagloire", "MlleBaptistine", weight=6)
|
| 304 |
+
G.add_edge("CountessDeLo", "Myriel", weight=1)
|
| 305 |
+
G.add_edge("Geborand", "Myriel", weight=1)
|
| 306 |
+
G.add_edge("Champtercier", "Myriel", weight=1)
|
| 307 |
+
G.add_edge("Cravatte", "Myriel", weight=1)
|
| 308 |
+
G.add_edge("Count", "Myriel", weight=2)
|
| 309 |
+
G.add_edge("OldMan", "Myriel", weight=1)
|
| 310 |
+
G.add_edge("Valjean", "Labarre", weight=1)
|
| 311 |
+
G.add_edge("Valjean", "MmeMagloire", weight=3)
|
| 312 |
+
G.add_edge("Valjean", "MlleBaptistine", weight=3)
|
| 313 |
+
G.add_edge("Valjean", "Myriel", weight=5)
|
| 314 |
+
G.add_edge("Marguerite", "Valjean", weight=1)
|
| 315 |
+
G.add_edge("MmeDeR", "Valjean", weight=1)
|
| 316 |
+
G.add_edge("Isabeau", "Valjean", weight=1)
|
| 317 |
+
G.add_edge("Gervais", "Valjean", weight=1)
|
| 318 |
+
G.add_edge("Listolier", "Tholomyes", weight=4)
|
| 319 |
+
G.add_edge("Fameuil", "Tholomyes", weight=4)
|
| 320 |
+
G.add_edge("Fameuil", "Listolier", weight=4)
|
| 321 |
+
G.add_edge("Blacheville", "Tholomyes", weight=4)
|
| 322 |
+
G.add_edge("Blacheville", "Listolier", weight=4)
|
| 323 |
+
G.add_edge("Blacheville", "Fameuil", weight=4)
|
| 324 |
+
G.add_edge("Favourite", "Tholomyes", weight=3)
|
| 325 |
+
G.add_edge("Favourite", "Listolier", weight=3)
|
| 326 |
+
G.add_edge("Favourite", "Fameuil", weight=3)
|
| 327 |
+
G.add_edge("Favourite", "Blacheville", weight=4)
|
| 328 |
+
G.add_edge("Dahlia", "Tholomyes", weight=3)
|
| 329 |
+
G.add_edge("Dahlia", "Listolier", weight=3)
|
| 330 |
+
G.add_edge("Dahlia", "Fameuil", weight=3)
|
| 331 |
+
G.add_edge("Dahlia", "Blacheville", weight=3)
|
| 332 |
+
G.add_edge("Dahlia", "Favourite", weight=5)
|
| 333 |
+
G.add_edge("Zephine", "Tholomyes", weight=3)
|
| 334 |
+
G.add_edge("Zephine", "Listolier", weight=3)
|
| 335 |
+
G.add_edge("Zephine", "Fameuil", weight=3)
|
| 336 |
+
G.add_edge("Zephine", "Blacheville", weight=3)
|
| 337 |
+
G.add_edge("Zephine", "Favourite", weight=4)
|
| 338 |
+
G.add_edge("Zephine", "Dahlia", weight=4)
|
| 339 |
+
G.add_edge("Fantine", "Tholomyes", weight=3)
|
| 340 |
+
G.add_edge("Fantine", "Listolier", weight=3)
|
| 341 |
+
G.add_edge("Fantine", "Fameuil", weight=3)
|
| 342 |
+
G.add_edge("Fantine", "Blacheville", weight=3)
|
| 343 |
+
G.add_edge("Fantine", "Favourite", weight=4)
|
| 344 |
+
G.add_edge("Fantine", "Dahlia", weight=4)
|
| 345 |
+
G.add_edge("Fantine", "Zephine", weight=4)
|
| 346 |
+
G.add_edge("Fantine", "Marguerite", weight=2)
|
| 347 |
+
G.add_edge("Fantine", "Valjean", weight=9)
|
| 348 |
+
G.add_edge("MmeThenardier", "Fantine", weight=2)
|
| 349 |
+
G.add_edge("MmeThenardier", "Valjean", weight=7)
|
| 350 |
+
G.add_edge("Thenardier", "MmeThenardier", weight=13)
|
| 351 |
+
G.add_edge("Thenardier", "Fantine", weight=1)
|
| 352 |
+
G.add_edge("Thenardier", "Valjean", weight=12)
|
| 353 |
+
G.add_edge("Cosette", "MmeThenardier", weight=4)
|
| 354 |
+
G.add_edge("Cosette", "Valjean", weight=31)
|
| 355 |
+
G.add_edge("Cosette", "Tholomyes", weight=1)
|
| 356 |
+
G.add_edge("Cosette", "Thenardier", weight=1)
|
| 357 |
+
G.add_edge("Javert", "Valjean", weight=17)
|
| 358 |
+
G.add_edge("Javert", "Fantine", weight=5)
|
| 359 |
+
G.add_edge("Javert", "Thenardier", weight=5)
|
| 360 |
+
G.add_edge("Javert", "MmeThenardier", weight=1)
|
| 361 |
+
G.add_edge("Javert", "Cosette", weight=1)
|
| 362 |
+
G.add_edge("Fauchelevent", "Valjean", weight=8)
|
| 363 |
+
G.add_edge("Fauchelevent", "Javert", weight=1)
|
| 364 |
+
G.add_edge("Bamatabois", "Fantine", weight=1)
|
| 365 |
+
G.add_edge("Bamatabois", "Javert", weight=1)
|
| 366 |
+
G.add_edge("Bamatabois", "Valjean", weight=2)
|
| 367 |
+
G.add_edge("Perpetue", "Fantine", weight=1)
|
| 368 |
+
G.add_edge("Simplice", "Perpetue", weight=2)
|
| 369 |
+
G.add_edge("Simplice", "Valjean", weight=3)
|
| 370 |
+
G.add_edge("Simplice", "Fantine", weight=2)
|
| 371 |
+
G.add_edge("Simplice", "Javert", weight=1)
|
| 372 |
+
G.add_edge("Scaufflaire", "Valjean", weight=1)
|
| 373 |
+
G.add_edge("Woman1", "Valjean", weight=2)
|
| 374 |
+
G.add_edge("Woman1", "Javert", weight=1)
|
| 375 |
+
G.add_edge("Judge", "Valjean", weight=3)
|
| 376 |
+
G.add_edge("Judge", "Bamatabois", weight=2)
|
| 377 |
+
G.add_edge("Champmathieu", "Valjean", weight=3)
|
| 378 |
+
G.add_edge("Champmathieu", "Judge", weight=3)
|
| 379 |
+
G.add_edge("Champmathieu", "Bamatabois", weight=2)
|
| 380 |
+
G.add_edge("Brevet", "Judge", weight=2)
|
| 381 |
+
G.add_edge("Brevet", "Champmathieu", weight=2)
|
| 382 |
+
G.add_edge("Brevet", "Valjean", weight=2)
|
| 383 |
+
G.add_edge("Brevet", "Bamatabois", weight=1)
|
| 384 |
+
G.add_edge("Chenildieu", "Judge", weight=2)
|
| 385 |
+
G.add_edge("Chenildieu", "Champmathieu", weight=2)
|
| 386 |
+
G.add_edge("Chenildieu", "Brevet", weight=2)
|
| 387 |
+
G.add_edge("Chenildieu", "Valjean", weight=2)
|
| 388 |
+
G.add_edge("Chenildieu", "Bamatabois", weight=1)
|
| 389 |
+
G.add_edge("Cochepaille", "Judge", weight=2)
|
| 390 |
+
G.add_edge("Cochepaille", "Champmathieu", weight=2)
|
| 391 |
+
G.add_edge("Cochepaille", "Brevet", weight=2)
|
| 392 |
+
G.add_edge("Cochepaille", "Chenildieu", weight=2)
|
| 393 |
+
G.add_edge("Cochepaille", "Valjean", weight=2)
|
| 394 |
+
G.add_edge("Cochepaille", "Bamatabois", weight=1)
|
| 395 |
+
G.add_edge("Pontmercy", "Thenardier", weight=1)
|
| 396 |
+
G.add_edge("Boulatruelle", "Thenardier", weight=1)
|
| 397 |
+
G.add_edge("Eponine", "MmeThenardier", weight=2)
|
| 398 |
+
G.add_edge("Eponine", "Thenardier", weight=3)
|
| 399 |
+
G.add_edge("Anzelma", "Eponine", weight=2)
|
| 400 |
+
G.add_edge("Anzelma", "Thenardier", weight=2)
|
| 401 |
+
G.add_edge("Anzelma", "MmeThenardier", weight=1)
|
| 402 |
+
G.add_edge("Woman2", "Valjean", weight=3)
|
| 403 |
+
G.add_edge("Woman2", "Cosette", weight=1)
|
| 404 |
+
G.add_edge("Woman2", "Javert", weight=1)
|
| 405 |
+
G.add_edge("MotherInnocent", "Fauchelevent", weight=3)
|
| 406 |
+
G.add_edge("MotherInnocent", "Valjean", weight=1)
|
| 407 |
+
G.add_edge("Gribier", "Fauchelevent", weight=2)
|
| 408 |
+
G.add_edge("MmeBurgon", "Jondrette", weight=1)
|
| 409 |
+
G.add_edge("Gavroche", "MmeBurgon", weight=2)
|
| 410 |
+
G.add_edge("Gavroche", "Thenardier", weight=1)
|
| 411 |
+
G.add_edge("Gavroche", "Javert", weight=1)
|
| 412 |
+
G.add_edge("Gavroche", "Valjean", weight=1)
|
| 413 |
+
G.add_edge("Gillenormand", "Cosette", weight=3)
|
| 414 |
+
G.add_edge("Gillenormand", "Valjean", weight=2)
|
| 415 |
+
G.add_edge("Magnon", "Gillenormand", weight=1)
|
| 416 |
+
G.add_edge("Magnon", "MmeThenardier", weight=1)
|
| 417 |
+
G.add_edge("MlleGillenormand", "Gillenormand", weight=9)
|
| 418 |
+
G.add_edge("MlleGillenormand", "Cosette", weight=2)
|
| 419 |
+
G.add_edge("MlleGillenormand", "Valjean", weight=2)
|
| 420 |
+
G.add_edge("MmePontmercy", "MlleGillenormand", weight=1)
|
| 421 |
+
G.add_edge("MmePontmercy", "Pontmercy", weight=1)
|
| 422 |
+
G.add_edge("MlleVaubois", "MlleGillenormand", weight=1)
|
| 423 |
+
G.add_edge("LtGillenormand", "MlleGillenormand", weight=2)
|
| 424 |
+
G.add_edge("LtGillenormand", "Gillenormand", weight=1)
|
| 425 |
+
G.add_edge("LtGillenormand", "Cosette", weight=1)
|
| 426 |
+
G.add_edge("Marius", "MlleGillenormand", weight=6)
|
| 427 |
+
G.add_edge("Marius", "Gillenormand", weight=12)
|
| 428 |
+
G.add_edge("Marius", "Pontmercy", weight=1)
|
| 429 |
+
G.add_edge("Marius", "LtGillenormand", weight=1)
|
| 430 |
+
G.add_edge("Marius", "Cosette", weight=21)
|
| 431 |
+
G.add_edge("Marius", "Valjean", weight=19)
|
| 432 |
+
G.add_edge("Marius", "Tholomyes", weight=1)
|
| 433 |
+
G.add_edge("Marius", "Thenardier", weight=2)
|
| 434 |
+
G.add_edge("Marius", "Eponine", weight=5)
|
| 435 |
+
G.add_edge("Marius", "Gavroche", weight=4)
|
| 436 |
+
G.add_edge("BaronessT", "Gillenormand", weight=1)
|
| 437 |
+
G.add_edge("BaronessT", "Marius", weight=1)
|
| 438 |
+
G.add_edge("Mabeuf", "Marius", weight=1)
|
| 439 |
+
G.add_edge("Mabeuf", "Eponine", weight=1)
|
| 440 |
+
G.add_edge("Mabeuf", "Gavroche", weight=1)
|
| 441 |
+
G.add_edge("Enjolras", "Marius", weight=7)
|
| 442 |
+
G.add_edge("Enjolras", "Gavroche", weight=7)
|
| 443 |
+
G.add_edge("Enjolras", "Javert", weight=6)
|
| 444 |
+
G.add_edge("Enjolras", "Mabeuf", weight=1)
|
| 445 |
+
G.add_edge("Enjolras", "Valjean", weight=4)
|
| 446 |
+
G.add_edge("Combeferre", "Enjolras", weight=15)
|
| 447 |
+
G.add_edge("Combeferre", "Marius", weight=5)
|
| 448 |
+
G.add_edge("Combeferre", "Gavroche", weight=6)
|
| 449 |
+
G.add_edge("Combeferre", "Mabeuf", weight=2)
|
| 450 |
+
G.add_edge("Prouvaire", "Gavroche", weight=1)
|
| 451 |
+
G.add_edge("Prouvaire", "Enjolras", weight=4)
|
| 452 |
+
G.add_edge("Prouvaire", "Combeferre", weight=2)
|
| 453 |
+
G.add_edge("Feuilly", "Gavroche", weight=2)
|
| 454 |
+
G.add_edge("Feuilly", "Enjolras", weight=6)
|
| 455 |
+
G.add_edge("Feuilly", "Prouvaire", weight=2)
|
| 456 |
+
G.add_edge("Feuilly", "Combeferre", weight=5)
|
| 457 |
+
G.add_edge("Feuilly", "Mabeuf", weight=1)
|
| 458 |
+
G.add_edge("Feuilly", "Marius", weight=1)
|
| 459 |
+
G.add_edge("Courfeyrac", "Marius", weight=9)
|
| 460 |
+
G.add_edge("Courfeyrac", "Enjolras", weight=17)
|
| 461 |
+
G.add_edge("Courfeyrac", "Combeferre", weight=13)
|
| 462 |
+
G.add_edge("Courfeyrac", "Gavroche", weight=7)
|
| 463 |
+
G.add_edge("Courfeyrac", "Mabeuf", weight=2)
|
| 464 |
+
G.add_edge("Courfeyrac", "Eponine", weight=1)
|
| 465 |
+
G.add_edge("Courfeyrac", "Feuilly", weight=6)
|
| 466 |
+
G.add_edge("Courfeyrac", "Prouvaire", weight=3)
|
| 467 |
+
G.add_edge("Bahorel", "Combeferre", weight=5)
|
| 468 |
+
G.add_edge("Bahorel", "Gavroche", weight=5)
|
| 469 |
+
G.add_edge("Bahorel", "Courfeyrac", weight=6)
|
| 470 |
+
G.add_edge("Bahorel", "Mabeuf", weight=2)
|
| 471 |
+
G.add_edge("Bahorel", "Enjolras", weight=4)
|
| 472 |
+
G.add_edge("Bahorel", "Feuilly", weight=3)
|
| 473 |
+
G.add_edge("Bahorel", "Prouvaire", weight=2)
|
| 474 |
+
G.add_edge("Bahorel", "Marius", weight=1)
|
| 475 |
+
G.add_edge("Bossuet", "Marius", weight=5)
|
| 476 |
+
G.add_edge("Bossuet", "Courfeyrac", weight=12)
|
| 477 |
+
G.add_edge("Bossuet", "Gavroche", weight=5)
|
| 478 |
+
G.add_edge("Bossuet", "Bahorel", weight=4)
|
| 479 |
+
G.add_edge("Bossuet", "Enjolras", weight=10)
|
| 480 |
+
G.add_edge("Bossuet", "Feuilly", weight=6)
|
| 481 |
+
G.add_edge("Bossuet", "Prouvaire", weight=2)
|
| 482 |
+
G.add_edge("Bossuet", "Combeferre", weight=9)
|
| 483 |
+
G.add_edge("Bossuet", "Mabeuf", weight=1)
|
| 484 |
+
G.add_edge("Bossuet", "Valjean", weight=1)
|
| 485 |
+
G.add_edge("Joly", "Bahorel", weight=5)
|
| 486 |
+
G.add_edge("Joly", "Bossuet", weight=7)
|
| 487 |
+
G.add_edge("Joly", "Gavroche", weight=3)
|
| 488 |
+
G.add_edge("Joly", "Courfeyrac", weight=5)
|
| 489 |
+
G.add_edge("Joly", "Enjolras", weight=5)
|
| 490 |
+
G.add_edge("Joly", "Feuilly", weight=5)
|
| 491 |
+
G.add_edge("Joly", "Prouvaire", weight=2)
|
| 492 |
+
G.add_edge("Joly", "Combeferre", weight=5)
|
| 493 |
+
G.add_edge("Joly", "Mabeuf", weight=1)
|
| 494 |
+
G.add_edge("Joly", "Marius", weight=2)
|
| 495 |
+
G.add_edge("Grantaire", "Bossuet", weight=3)
|
| 496 |
+
G.add_edge("Grantaire", "Enjolras", weight=3)
|
| 497 |
+
G.add_edge("Grantaire", "Combeferre", weight=1)
|
| 498 |
+
G.add_edge("Grantaire", "Courfeyrac", weight=2)
|
| 499 |
+
G.add_edge("Grantaire", "Joly", weight=2)
|
| 500 |
+
G.add_edge("Grantaire", "Gavroche", weight=1)
|
| 501 |
+
G.add_edge("Grantaire", "Bahorel", weight=1)
|
| 502 |
+
G.add_edge("Grantaire", "Feuilly", weight=1)
|
| 503 |
+
G.add_edge("Grantaire", "Prouvaire", weight=1)
|
| 504 |
+
G.add_edge("MotherPlutarch", "Mabeuf", weight=3)
|
| 505 |
+
G.add_edge("Gueulemer", "Thenardier", weight=5)
|
| 506 |
+
G.add_edge("Gueulemer", "Valjean", weight=1)
|
| 507 |
+
G.add_edge("Gueulemer", "MmeThenardier", weight=1)
|
| 508 |
+
G.add_edge("Gueulemer", "Javert", weight=1)
|
| 509 |
+
G.add_edge("Gueulemer", "Gavroche", weight=1)
|
| 510 |
+
G.add_edge("Gueulemer", "Eponine", weight=1)
|
| 511 |
+
G.add_edge("Babet", "Thenardier", weight=6)
|
| 512 |
+
G.add_edge("Babet", "Gueulemer", weight=6)
|
| 513 |
+
G.add_edge("Babet", "Valjean", weight=1)
|
| 514 |
+
G.add_edge("Babet", "MmeThenardier", weight=1)
|
| 515 |
+
G.add_edge("Babet", "Javert", weight=2)
|
| 516 |
+
G.add_edge("Babet", "Gavroche", weight=1)
|
| 517 |
+
G.add_edge("Babet", "Eponine", weight=1)
|
| 518 |
+
G.add_edge("Claquesous", "Thenardier", weight=4)
|
| 519 |
+
G.add_edge("Claquesous", "Babet", weight=4)
|
| 520 |
+
G.add_edge("Claquesous", "Gueulemer", weight=4)
|
| 521 |
+
G.add_edge("Claquesous", "Valjean", weight=1)
|
| 522 |
+
G.add_edge("Claquesous", "MmeThenardier", weight=1)
|
| 523 |
+
G.add_edge("Claquesous", "Javert", weight=1)
|
| 524 |
+
G.add_edge("Claquesous", "Eponine", weight=1)
|
| 525 |
+
G.add_edge("Claquesous", "Enjolras", weight=1)
|
| 526 |
+
G.add_edge("Montparnasse", "Javert", weight=1)
|
| 527 |
+
G.add_edge("Montparnasse", "Babet", weight=2)
|
| 528 |
+
G.add_edge("Montparnasse", "Gueulemer", weight=2)
|
| 529 |
+
G.add_edge("Montparnasse", "Claquesous", weight=2)
|
| 530 |
+
G.add_edge("Montparnasse", "Valjean", weight=1)
|
| 531 |
+
G.add_edge("Montparnasse", "Gavroche", weight=1)
|
| 532 |
+
G.add_edge("Montparnasse", "Eponine", weight=1)
|
| 533 |
+
G.add_edge("Montparnasse", "Thenardier", weight=1)
|
| 534 |
+
G.add_edge("Toussaint", "Cosette", weight=2)
|
| 535 |
+
G.add_edge("Toussaint", "Javert", weight=1)
|
| 536 |
+
G.add_edge("Toussaint", "Valjean", weight=1)
|
| 537 |
+
G.add_edge("Child1", "Gavroche", weight=2)
|
| 538 |
+
G.add_edge("Child2", "Gavroche", weight=2)
|
| 539 |
+
G.add_edge("Child2", "Child1", weight=3)
|
| 540 |
+
G.add_edge("Brujon", "Babet", weight=3)
|
| 541 |
+
G.add_edge("Brujon", "Gueulemer", weight=3)
|
| 542 |
+
G.add_edge("Brujon", "Thenardier", weight=3)
|
| 543 |
+
G.add_edge("Brujon", "Gavroche", weight=1)
|
| 544 |
+
G.add_edge("Brujon", "Eponine", weight=1)
|
| 545 |
+
G.add_edge("Brujon", "Claquesous", weight=1)
|
| 546 |
+
G.add_edge("Brujon", "Montparnasse", weight=1)
|
| 547 |
+
G.add_edge("MmeHucheloup", "Bossuet", weight=1)
|
| 548 |
+
G.add_edge("MmeHucheloup", "Joly", weight=1)
|
| 549 |
+
G.add_edge("MmeHucheloup", "Grantaire", weight=1)
|
| 550 |
+
G.add_edge("MmeHucheloup", "Bahorel", weight=1)
|
| 551 |
+
G.add_edge("MmeHucheloup", "Courfeyrac", weight=1)
|
| 552 |
+
G.add_edge("MmeHucheloup", "Gavroche", weight=1)
|
| 553 |
+
G.add_edge("MmeHucheloup", "Enjolras", weight=1)
|
| 554 |
+
return G
|
wemm/lib/python3.10/site-packages/networkx/generators/stochastic.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Functions for generating stochastic graphs from a given weighted directed
|
| 2 |
+
graph.
|
| 3 |
+
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import networkx as nx
|
| 7 |
+
from networkx.classes import DiGraph, MultiDiGraph
|
| 8 |
+
from networkx.utils import not_implemented_for
|
| 9 |
+
|
| 10 |
+
__all__ = ["stochastic_graph"]
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@not_implemented_for("undirected")
|
| 14 |
+
@nx._dispatchable(
|
| 15 |
+
edge_attrs="weight", mutates_input={"not copy": 1}, returns_graph=True
|
| 16 |
+
)
|
| 17 |
+
def stochastic_graph(G, copy=True, weight="weight"):
|
| 18 |
+
"""Returns a right-stochastic representation of directed graph `G`.
|
| 19 |
+
|
| 20 |
+
A right-stochastic graph is a weighted digraph in which for each
|
| 21 |
+
node, the sum of the weights of all the out-edges of that node is
|
| 22 |
+
1. If the graph is already weighted (for example, via a 'weight'
|
| 23 |
+
edge attribute), the reweighting takes that into account.
|
| 24 |
+
|
| 25 |
+
Parameters
|
| 26 |
+
----------
|
| 27 |
+
G : directed graph
|
| 28 |
+
A :class:`~networkx.DiGraph` or :class:`~networkx.MultiDiGraph`.
|
| 29 |
+
|
| 30 |
+
copy : boolean, optional
|
| 31 |
+
If this is True, then this function returns a new graph with
|
| 32 |
+
the stochastic reweighting. Otherwise, the original graph is
|
| 33 |
+
modified in-place (and also returned, for convenience).
|
| 34 |
+
|
| 35 |
+
weight : edge attribute key (optional, default='weight')
|
| 36 |
+
Edge attribute key used for reading the existing weight and
|
| 37 |
+
setting the new weight. If no attribute with this key is found
|
| 38 |
+
for an edge, then the edge weight is assumed to be 1. If an edge
|
| 39 |
+
has a weight, it must be a positive number.
|
| 40 |
+
|
| 41 |
+
"""
|
| 42 |
+
if copy:
|
| 43 |
+
G = MultiDiGraph(G) if G.is_multigraph() else DiGraph(G)
|
| 44 |
+
# There is a tradeoff here: the dictionary of node degrees may
|
| 45 |
+
# require a lot of memory, whereas making a call to `G.out_degree`
|
| 46 |
+
# inside the loop may be costly in computation time.
|
| 47 |
+
degree = dict(G.out_degree(weight=weight))
|
| 48 |
+
for u, v, d in G.edges(data=True):
|
| 49 |
+
if degree[u] == 0:
|
| 50 |
+
d[weight] = 0
|
| 51 |
+
else:
|
| 52 |
+
d[weight] = d.get(weight, 1) / degree[u]
|
| 53 |
+
nx._clear_cache(G)
|
| 54 |
+
return G
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_atlas.cpython-310.pyc
ADDED
|
Binary file (3.57 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_cographs.cpython-310.pyc
ADDED
|
Binary file (638 Bytes). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_degree_seq.cpython-310.pyc
ADDED
|
Binary file (8.63 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_duplication.cpython-310.pyc
ADDED
|
Binary file (4.95 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_expanders.cpython-310.pyc
ADDED
|
Binary file (5.83 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_geometric.cpython-310.pyc
ADDED
|
Binary file (20.1 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_harary_graph.cpython-310.pyc
ADDED
|
Binary file (2.82 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_internet_as_graphs.cpython-310.pyc
ADDED
|
Binary file (4.81 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_intersection.cpython-310.pyc
ADDED
|
Binary file (1.54 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_interval_graph.cpython-310.pyc
ADDED
|
Binary file (4.17 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_joint_degree_seq.cpython-310.pyc
ADDED
|
Binary file (2.5 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_lattice.cpython-310.pyc
ADDED
|
Binary file (10.5 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_line.cpython-310.pyc
ADDED
|
Binary file (10.7 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_mycielski.cpython-310.pyc
ADDED
|
Binary file (1.47 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_random_clustered.cpython-310.pyc
ADDED
|
Binary file (2.17 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_random_graphs.cpython-310.pyc
ADDED
|
Binary file (17.1 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_small.cpython-310.pyc
ADDED
|
Binary file (7.41 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_sudoku.cpython-310.pyc
ADDED
|
Binary file (1.77 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_time_series.cpython-310.pyc
ADDED
|
Binary file (2.63 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_trees.cpython-310.pyc
ADDED
|
Binary file (5.69 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/test_atlas.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from itertools import groupby
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
import networkx as nx
|
| 6 |
+
from networkx import graph_atlas, graph_atlas_g
|
| 7 |
+
from networkx.generators.atlas import NUM_GRAPHS
|
| 8 |
+
from networkx.utils import edges_equal, nodes_equal, pairwise
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class TestAtlasGraph:
|
| 12 |
+
"""Unit tests for the :func:`~networkx.graph_atlas` function."""
|
| 13 |
+
|
| 14 |
+
def test_index_too_small(self):
|
| 15 |
+
with pytest.raises(ValueError):
|
| 16 |
+
graph_atlas(-1)
|
| 17 |
+
|
| 18 |
+
def test_index_too_large(self):
|
| 19 |
+
with pytest.raises(ValueError):
|
| 20 |
+
graph_atlas(NUM_GRAPHS)
|
| 21 |
+
|
| 22 |
+
def test_graph(self):
|
| 23 |
+
G = graph_atlas(6)
|
| 24 |
+
assert nodes_equal(G.nodes(), range(3))
|
| 25 |
+
assert edges_equal(G.edges(), [(0, 1), (0, 2)])
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class TestAtlasGraphG:
|
| 29 |
+
"""Unit tests for the :func:`~networkx.graph_atlas_g` function."""
|
| 30 |
+
|
| 31 |
+
@classmethod
|
| 32 |
+
def setup_class(cls):
|
| 33 |
+
cls.GAG = graph_atlas_g()
|
| 34 |
+
|
| 35 |
+
def test_sizes(self):
|
| 36 |
+
G = self.GAG[0]
|
| 37 |
+
assert G.number_of_nodes() == 0
|
| 38 |
+
assert G.number_of_edges() == 0
|
| 39 |
+
|
| 40 |
+
G = self.GAG[7]
|
| 41 |
+
assert G.number_of_nodes() == 3
|
| 42 |
+
assert G.number_of_edges() == 3
|
| 43 |
+
|
| 44 |
+
def test_names(self):
|
| 45 |
+
for i, G in enumerate(self.GAG):
|
| 46 |
+
assert int(G.name[1:]) == i
|
| 47 |
+
|
| 48 |
+
def test_nondecreasing_nodes(self):
|
| 49 |
+
# check for nondecreasing number of nodes
|
| 50 |
+
for n1, n2 in pairwise(map(len, self.GAG)):
|
| 51 |
+
assert n2 <= n1 + 1
|
| 52 |
+
|
| 53 |
+
def test_nondecreasing_edges(self):
|
| 54 |
+
# check for nondecreasing number of edges (for fixed number of
|
| 55 |
+
# nodes)
|
| 56 |
+
for n, group in groupby(self.GAG, key=nx.number_of_nodes):
|
| 57 |
+
for m1, m2 in pairwise(map(nx.number_of_edges, group)):
|
| 58 |
+
assert m2 <= m1 + 1
|
| 59 |
+
|
| 60 |
+
def test_nondecreasing_degree_sequence(self):
|
| 61 |
+
# Check for lexicographically nondecreasing degree sequences
|
| 62 |
+
# (for fixed number of nodes and edges).
|
| 63 |
+
#
|
| 64 |
+
# There are three exceptions to this rule in the order given in
|
| 65 |
+
# the "Atlas of Graphs" book, so we need to manually exclude
|
| 66 |
+
# those.
|
| 67 |
+
exceptions = [("G55", "G56"), ("G1007", "G1008"), ("G1012", "G1013")]
|
| 68 |
+
for n, group in groupby(self.GAG, key=nx.number_of_nodes):
|
| 69 |
+
for m, group in groupby(group, key=nx.number_of_edges):
|
| 70 |
+
for G1, G2 in pairwise(group):
|
| 71 |
+
if (G1.name, G2.name) in exceptions:
|
| 72 |
+
continue
|
| 73 |
+
d1 = sorted(d for v, d in G1.degree())
|
| 74 |
+
d2 = sorted(d for v, d in G2.degree())
|
| 75 |
+
assert d1 <= d2
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/test_classic.py
ADDED
|
@@ -0,0 +1,640 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
====================
|
| 3 |
+
Generators - Classic
|
| 4 |
+
====================
|
| 5 |
+
|
| 6 |
+
Unit tests for various classic graph generators in generators/classic.py
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import itertools
|
| 10 |
+
import typing
|
| 11 |
+
|
| 12 |
+
import pytest
|
| 13 |
+
|
| 14 |
+
import networkx as nx
|
| 15 |
+
from networkx.algorithms.isomorphism.isomorph import graph_could_be_isomorphic
|
| 16 |
+
from networkx.utils import edges_equal, nodes_equal
|
| 17 |
+
|
| 18 |
+
is_isomorphic = graph_could_be_isomorphic
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class TestGeneratorClassic:
|
| 22 |
+
def test_balanced_tree(self):
|
| 23 |
+
# balanced_tree(r,h) is a tree with (r**(h+1)-1)/(r-1) edges
|
| 24 |
+
for r, h in [(2, 2), (3, 3), (6, 2)]:
|
| 25 |
+
t = nx.balanced_tree(r, h)
|
| 26 |
+
order = t.order()
|
| 27 |
+
assert order == (r ** (h + 1) - 1) / (r - 1)
|
| 28 |
+
assert nx.is_connected(t)
|
| 29 |
+
assert t.size() == order - 1
|
| 30 |
+
dh = nx.degree_histogram(t)
|
| 31 |
+
assert dh[0] == 0 # no nodes of 0
|
| 32 |
+
assert dh[1] == r**h # nodes of degree 1 are leaves
|
| 33 |
+
assert dh[r] == 1 # root is degree r
|
| 34 |
+
assert dh[r + 1] == order - r**h - 1 # everyone else is degree r+1
|
| 35 |
+
assert len(dh) == r + 2
|
| 36 |
+
|
| 37 |
+
def test_balanced_tree_star(self):
|
| 38 |
+
# balanced_tree(r,1) is the r-star
|
| 39 |
+
t = nx.balanced_tree(r=2, h=1)
|
| 40 |
+
assert is_isomorphic(t, nx.star_graph(2))
|
| 41 |
+
t = nx.balanced_tree(r=5, h=1)
|
| 42 |
+
assert is_isomorphic(t, nx.star_graph(5))
|
| 43 |
+
t = nx.balanced_tree(r=10, h=1)
|
| 44 |
+
assert is_isomorphic(t, nx.star_graph(10))
|
| 45 |
+
|
| 46 |
+
def test_balanced_tree_path(self):
|
| 47 |
+
"""Tests that the balanced tree with branching factor one is the
|
| 48 |
+
path graph.
|
| 49 |
+
|
| 50 |
+
"""
|
| 51 |
+
# A tree of height four has five levels.
|
| 52 |
+
T = nx.balanced_tree(1, 4)
|
| 53 |
+
P = nx.path_graph(5)
|
| 54 |
+
assert is_isomorphic(T, P)
|
| 55 |
+
|
| 56 |
+
def test_full_rary_tree(self):
|
| 57 |
+
r = 2
|
| 58 |
+
n = 9
|
| 59 |
+
t = nx.full_rary_tree(r, n)
|
| 60 |
+
assert t.order() == n
|
| 61 |
+
assert nx.is_connected(t)
|
| 62 |
+
dh = nx.degree_histogram(t)
|
| 63 |
+
assert dh[0] == 0 # no nodes of 0
|
| 64 |
+
assert dh[1] == 5 # nodes of degree 1 are leaves
|
| 65 |
+
assert dh[r] == 1 # root is degree r
|
| 66 |
+
assert dh[r + 1] == 9 - 5 - 1 # everyone else is degree r+1
|
| 67 |
+
assert len(dh) == r + 2
|
| 68 |
+
|
| 69 |
+
def test_full_rary_tree_balanced(self):
|
| 70 |
+
t = nx.full_rary_tree(2, 15)
|
| 71 |
+
th = nx.balanced_tree(2, 3)
|
| 72 |
+
assert is_isomorphic(t, th)
|
| 73 |
+
|
| 74 |
+
def test_full_rary_tree_path(self):
|
| 75 |
+
t = nx.full_rary_tree(1, 10)
|
| 76 |
+
assert is_isomorphic(t, nx.path_graph(10))
|
| 77 |
+
|
| 78 |
+
def test_full_rary_tree_empty(self):
|
| 79 |
+
t = nx.full_rary_tree(0, 10)
|
| 80 |
+
assert is_isomorphic(t, nx.empty_graph(10))
|
| 81 |
+
t = nx.full_rary_tree(3, 0)
|
| 82 |
+
assert is_isomorphic(t, nx.empty_graph(0))
|
| 83 |
+
|
| 84 |
+
def test_full_rary_tree_3_20(self):
|
| 85 |
+
t = nx.full_rary_tree(3, 20)
|
| 86 |
+
assert t.order() == 20
|
| 87 |
+
|
| 88 |
+
def test_barbell_graph(self):
|
| 89 |
+
# number of nodes = 2*m1 + m2 (2 m1-complete graphs + m2-path + 2 edges)
|
| 90 |
+
# number of edges = 2*(nx.number_of_edges(m1-complete graph) + m2 + 1
|
| 91 |
+
m1 = 3
|
| 92 |
+
m2 = 5
|
| 93 |
+
b = nx.barbell_graph(m1, m2)
|
| 94 |
+
assert nx.number_of_nodes(b) == 2 * m1 + m2
|
| 95 |
+
assert nx.number_of_edges(b) == m1 * (m1 - 1) + m2 + 1
|
| 96 |
+
|
| 97 |
+
m1 = 4
|
| 98 |
+
m2 = 10
|
| 99 |
+
b = nx.barbell_graph(m1, m2)
|
| 100 |
+
assert nx.number_of_nodes(b) == 2 * m1 + m2
|
| 101 |
+
assert nx.number_of_edges(b) == m1 * (m1 - 1) + m2 + 1
|
| 102 |
+
|
| 103 |
+
m1 = 3
|
| 104 |
+
m2 = 20
|
| 105 |
+
b = nx.barbell_graph(m1, m2)
|
| 106 |
+
assert nx.number_of_nodes(b) == 2 * m1 + m2
|
| 107 |
+
assert nx.number_of_edges(b) == m1 * (m1 - 1) + m2 + 1
|
| 108 |
+
|
| 109 |
+
# Raise NetworkXError if m1<2
|
| 110 |
+
m1 = 1
|
| 111 |
+
m2 = 20
|
| 112 |
+
pytest.raises(nx.NetworkXError, nx.barbell_graph, m1, m2)
|
| 113 |
+
|
| 114 |
+
# Raise NetworkXError if m2<0
|
| 115 |
+
m1 = 5
|
| 116 |
+
m2 = -2
|
| 117 |
+
pytest.raises(nx.NetworkXError, nx.barbell_graph, m1, m2)
|
| 118 |
+
|
| 119 |
+
# nx.barbell_graph(2,m) = nx.path_graph(m+4)
|
| 120 |
+
m1 = 2
|
| 121 |
+
m2 = 5
|
| 122 |
+
b = nx.barbell_graph(m1, m2)
|
| 123 |
+
assert is_isomorphic(b, nx.path_graph(m2 + 4))
|
| 124 |
+
|
| 125 |
+
m1 = 2
|
| 126 |
+
m2 = 10
|
| 127 |
+
b = nx.barbell_graph(m1, m2)
|
| 128 |
+
assert is_isomorphic(b, nx.path_graph(m2 + 4))
|
| 129 |
+
|
| 130 |
+
m1 = 2
|
| 131 |
+
m2 = 20
|
| 132 |
+
b = nx.barbell_graph(m1, m2)
|
| 133 |
+
assert is_isomorphic(b, nx.path_graph(m2 + 4))
|
| 134 |
+
|
| 135 |
+
pytest.raises(
|
| 136 |
+
nx.NetworkXError, nx.barbell_graph, m1, m2, create_using=nx.DiGraph()
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
mb = nx.barbell_graph(m1, m2, create_using=nx.MultiGraph())
|
| 140 |
+
assert edges_equal(mb.edges(), b.edges())
|
| 141 |
+
|
| 142 |
+
def test_binomial_tree(self):
|
| 143 |
+
graphs = (None, nx.Graph, nx.DiGraph, nx.MultiGraph, nx.MultiDiGraph)
|
| 144 |
+
for create_using in graphs:
|
| 145 |
+
for n in range(4):
|
| 146 |
+
b = nx.binomial_tree(n, create_using)
|
| 147 |
+
assert nx.number_of_nodes(b) == 2**n
|
| 148 |
+
assert nx.number_of_edges(b) == (2**n - 1)
|
| 149 |
+
|
| 150 |
+
def test_complete_graph(self):
|
| 151 |
+
# complete_graph(m) is a connected graph with
|
| 152 |
+
# m nodes and m*(m+1)/2 edges
|
| 153 |
+
for m in [0, 1, 3, 5]:
|
| 154 |
+
g = nx.complete_graph(m)
|
| 155 |
+
assert nx.number_of_nodes(g) == m
|
| 156 |
+
assert nx.number_of_edges(g) == m * (m - 1) // 2
|
| 157 |
+
|
| 158 |
+
mg = nx.complete_graph(m, create_using=nx.MultiGraph)
|
| 159 |
+
assert edges_equal(mg.edges(), g.edges())
|
| 160 |
+
|
| 161 |
+
g = nx.complete_graph("abc")
|
| 162 |
+
assert nodes_equal(g.nodes(), ["a", "b", "c"])
|
| 163 |
+
assert g.size() == 3
|
| 164 |
+
|
| 165 |
+
# creates a self-loop... should it? <backward compatible says yes>
|
| 166 |
+
g = nx.complete_graph("abcb")
|
| 167 |
+
assert nodes_equal(g.nodes(), ["a", "b", "c"])
|
| 168 |
+
assert g.size() == 4
|
| 169 |
+
|
| 170 |
+
g = nx.complete_graph("abcb", create_using=nx.MultiGraph)
|
| 171 |
+
assert nodes_equal(g.nodes(), ["a", "b", "c"])
|
| 172 |
+
assert g.size() == 6
|
| 173 |
+
|
| 174 |
+
def test_complete_digraph(self):
|
| 175 |
+
# complete_graph(m) is a connected graph with
|
| 176 |
+
# m nodes and m*(m+1)/2 edges
|
| 177 |
+
for m in [0, 1, 3, 5]:
|
| 178 |
+
g = nx.complete_graph(m, create_using=nx.DiGraph)
|
| 179 |
+
assert nx.number_of_nodes(g) == m
|
| 180 |
+
assert nx.number_of_edges(g) == m * (m - 1)
|
| 181 |
+
|
| 182 |
+
g = nx.complete_graph("abc", create_using=nx.DiGraph)
|
| 183 |
+
assert len(g) == 3
|
| 184 |
+
assert g.size() == 6
|
| 185 |
+
assert g.is_directed()
|
| 186 |
+
|
| 187 |
+
def test_circular_ladder_graph(self):
|
| 188 |
+
G = nx.circular_ladder_graph(5)
|
| 189 |
+
pytest.raises(
|
| 190 |
+
nx.NetworkXError, nx.circular_ladder_graph, 5, create_using=nx.DiGraph
|
| 191 |
+
)
|
| 192 |
+
mG = nx.circular_ladder_graph(5, create_using=nx.MultiGraph)
|
| 193 |
+
assert edges_equal(mG.edges(), G.edges())
|
| 194 |
+
|
| 195 |
+
def test_circulant_graph(self):
|
| 196 |
+
# Ci_n(1) is the cycle graph for all n
|
| 197 |
+
Ci6_1 = nx.circulant_graph(6, [1])
|
| 198 |
+
C6 = nx.cycle_graph(6)
|
| 199 |
+
assert edges_equal(Ci6_1.edges(), C6.edges())
|
| 200 |
+
|
| 201 |
+
# Ci_n(1, 2, ..., n div 2) is the complete graph for all n
|
| 202 |
+
Ci7 = nx.circulant_graph(7, [1, 2, 3])
|
| 203 |
+
K7 = nx.complete_graph(7)
|
| 204 |
+
assert edges_equal(Ci7.edges(), K7.edges())
|
| 205 |
+
|
| 206 |
+
# Ci_6(1, 3) is K_3,3 i.e. the utility graph
|
| 207 |
+
Ci6_1_3 = nx.circulant_graph(6, [1, 3])
|
| 208 |
+
K3_3 = nx.complete_bipartite_graph(3, 3)
|
| 209 |
+
assert is_isomorphic(Ci6_1_3, K3_3)
|
| 210 |
+
|
| 211 |
+
def test_cycle_graph(self):
|
| 212 |
+
G = nx.cycle_graph(4)
|
| 213 |
+
assert edges_equal(G.edges(), [(0, 1), (0, 3), (1, 2), (2, 3)])
|
| 214 |
+
mG = nx.cycle_graph(4, create_using=nx.MultiGraph)
|
| 215 |
+
assert edges_equal(mG.edges(), [(0, 1), (0, 3), (1, 2), (2, 3)])
|
| 216 |
+
G = nx.cycle_graph(4, create_using=nx.DiGraph)
|
| 217 |
+
assert not G.has_edge(2, 1)
|
| 218 |
+
assert G.has_edge(1, 2)
|
| 219 |
+
assert G.is_directed()
|
| 220 |
+
|
| 221 |
+
G = nx.cycle_graph("abc")
|
| 222 |
+
assert len(G) == 3
|
| 223 |
+
assert G.size() == 3
|
| 224 |
+
G = nx.cycle_graph("abcb")
|
| 225 |
+
assert len(G) == 3
|
| 226 |
+
assert G.size() == 2
|
| 227 |
+
g = nx.cycle_graph("abc", nx.DiGraph)
|
| 228 |
+
assert len(g) == 3
|
| 229 |
+
assert g.size() == 3
|
| 230 |
+
assert g.is_directed()
|
| 231 |
+
g = nx.cycle_graph("abcb", nx.DiGraph)
|
| 232 |
+
assert len(g) == 3
|
| 233 |
+
assert g.size() == 4
|
| 234 |
+
|
| 235 |
+
def test_dorogovtsev_goltsev_mendes_graph(self):
|
| 236 |
+
G = nx.dorogovtsev_goltsev_mendes_graph(0)
|
| 237 |
+
assert edges_equal(G.edges(), [(0, 1)])
|
| 238 |
+
assert nodes_equal(list(G), [0, 1])
|
| 239 |
+
G = nx.dorogovtsev_goltsev_mendes_graph(1)
|
| 240 |
+
assert edges_equal(G.edges(), [(0, 1), (0, 2), (1, 2)])
|
| 241 |
+
assert nx.average_clustering(G) == 1.0
|
| 242 |
+
assert nx.average_shortest_path_length(G) == 1.0
|
| 243 |
+
assert sorted(nx.triangles(G).values()) == [1, 1, 1]
|
| 244 |
+
assert nx.is_planar(G)
|
| 245 |
+
G = nx.dorogovtsev_goltsev_mendes_graph(2)
|
| 246 |
+
assert nx.number_of_nodes(G) == 6
|
| 247 |
+
assert nx.number_of_edges(G) == 9
|
| 248 |
+
assert nx.average_clustering(G) == 0.75
|
| 249 |
+
assert nx.average_shortest_path_length(G) == 1.4
|
| 250 |
+
assert nx.is_planar(G)
|
| 251 |
+
G = nx.dorogovtsev_goltsev_mendes_graph(10)
|
| 252 |
+
assert nx.number_of_nodes(G) == 29526
|
| 253 |
+
assert nx.number_of_edges(G) == 59049
|
| 254 |
+
assert G.degree(0) == 1024
|
| 255 |
+
assert G.degree(1) == 1024
|
| 256 |
+
assert G.degree(2) == 1024
|
| 257 |
+
|
| 258 |
+
with pytest.raises(nx.NetworkXError, match=r"n must be greater than"):
|
| 259 |
+
nx.dorogovtsev_goltsev_mendes_graph(-1)
|
| 260 |
+
with pytest.raises(nx.NetworkXError, match=r"directed graph not supported"):
|
| 261 |
+
nx.dorogovtsev_goltsev_mendes_graph(7, create_using=nx.DiGraph)
|
| 262 |
+
with pytest.raises(nx.NetworkXError, match=r"multigraph not supported"):
|
| 263 |
+
nx.dorogovtsev_goltsev_mendes_graph(7, create_using=nx.MultiGraph)
|
| 264 |
+
with pytest.raises(nx.NetworkXError):
|
| 265 |
+
nx.dorogovtsev_goltsev_mendes_graph(7, create_using=nx.MultiDiGraph)
|
| 266 |
+
|
| 267 |
+
def test_create_using(self):
|
| 268 |
+
G = nx.empty_graph()
|
| 269 |
+
assert isinstance(G, nx.Graph)
|
| 270 |
+
pytest.raises(TypeError, nx.empty_graph, create_using=0.0)
|
| 271 |
+
pytest.raises(TypeError, nx.empty_graph, create_using="Graph")
|
| 272 |
+
|
| 273 |
+
G = nx.empty_graph(create_using=nx.MultiGraph)
|
| 274 |
+
assert isinstance(G, nx.MultiGraph)
|
| 275 |
+
G = nx.empty_graph(create_using=nx.DiGraph)
|
| 276 |
+
assert isinstance(G, nx.DiGraph)
|
| 277 |
+
|
| 278 |
+
G = nx.empty_graph(create_using=nx.DiGraph, default=nx.MultiGraph)
|
| 279 |
+
assert isinstance(G, nx.DiGraph)
|
| 280 |
+
G = nx.empty_graph(create_using=None, default=nx.MultiGraph)
|
| 281 |
+
assert isinstance(G, nx.MultiGraph)
|
| 282 |
+
G = nx.empty_graph(default=nx.MultiGraph)
|
| 283 |
+
assert isinstance(G, nx.MultiGraph)
|
| 284 |
+
|
| 285 |
+
G = nx.path_graph(5)
|
| 286 |
+
H = nx.empty_graph(create_using=G)
|
| 287 |
+
assert not H.is_multigraph()
|
| 288 |
+
assert not H.is_directed()
|
| 289 |
+
assert len(H) == 0
|
| 290 |
+
assert G is H
|
| 291 |
+
|
| 292 |
+
H = nx.empty_graph(create_using=nx.MultiGraph())
|
| 293 |
+
assert H.is_multigraph()
|
| 294 |
+
assert not H.is_directed()
|
| 295 |
+
assert G is not H
|
| 296 |
+
|
| 297 |
+
# test for subclasses that also use typing.Protocol. See gh-6243
|
| 298 |
+
class Mixin(typing.Protocol):
|
| 299 |
+
pass
|
| 300 |
+
|
| 301 |
+
class MyGraph(Mixin, nx.DiGraph):
|
| 302 |
+
pass
|
| 303 |
+
|
| 304 |
+
G = nx.empty_graph(create_using=MyGraph)
|
| 305 |
+
|
| 306 |
+
def test_empty_graph(self):
|
| 307 |
+
G = nx.empty_graph()
|
| 308 |
+
assert nx.number_of_nodes(G) == 0
|
| 309 |
+
G = nx.empty_graph(42)
|
| 310 |
+
assert nx.number_of_nodes(G) == 42
|
| 311 |
+
assert nx.number_of_edges(G) == 0
|
| 312 |
+
|
| 313 |
+
G = nx.empty_graph("abc")
|
| 314 |
+
assert len(G) == 3
|
| 315 |
+
assert G.size() == 0
|
| 316 |
+
|
| 317 |
+
# create empty digraph
|
| 318 |
+
G = nx.empty_graph(42, create_using=nx.DiGraph(name="duh"))
|
| 319 |
+
assert nx.number_of_nodes(G) == 42
|
| 320 |
+
assert nx.number_of_edges(G) == 0
|
| 321 |
+
assert isinstance(G, nx.DiGraph)
|
| 322 |
+
|
| 323 |
+
# create empty multigraph
|
| 324 |
+
G = nx.empty_graph(42, create_using=nx.MultiGraph(name="duh"))
|
| 325 |
+
assert nx.number_of_nodes(G) == 42
|
| 326 |
+
assert nx.number_of_edges(G) == 0
|
| 327 |
+
assert isinstance(G, nx.MultiGraph)
|
| 328 |
+
|
| 329 |
+
# create empty graph from another
|
| 330 |
+
pete = nx.petersen_graph()
|
| 331 |
+
G = nx.empty_graph(42, create_using=pete)
|
| 332 |
+
assert nx.number_of_nodes(G) == 42
|
| 333 |
+
assert nx.number_of_edges(G) == 0
|
| 334 |
+
assert isinstance(G, nx.Graph)
|
| 335 |
+
|
| 336 |
+
def test_ladder_graph(self):
|
| 337 |
+
for i, G in [
|
| 338 |
+
(0, nx.empty_graph(0)),
|
| 339 |
+
(1, nx.path_graph(2)),
|
| 340 |
+
(2, nx.hypercube_graph(2)),
|
| 341 |
+
(10, nx.grid_graph([2, 10])),
|
| 342 |
+
]:
|
| 343 |
+
assert is_isomorphic(nx.ladder_graph(i), G)
|
| 344 |
+
|
| 345 |
+
pytest.raises(nx.NetworkXError, nx.ladder_graph, 2, create_using=nx.DiGraph)
|
| 346 |
+
|
| 347 |
+
g = nx.ladder_graph(2)
|
| 348 |
+
mg = nx.ladder_graph(2, create_using=nx.MultiGraph)
|
| 349 |
+
assert edges_equal(mg.edges(), g.edges())
|
| 350 |
+
|
| 351 |
+
@pytest.mark.parametrize(("m", "n"), [(3, 5), (4, 10), (3, 20)])
|
| 352 |
+
def test_lollipop_graph_right_sizes(self, m, n):
|
| 353 |
+
G = nx.lollipop_graph(m, n)
|
| 354 |
+
assert nx.number_of_nodes(G) == m + n
|
| 355 |
+
assert nx.number_of_edges(G) == m * (m - 1) / 2 + n
|
| 356 |
+
|
| 357 |
+
@pytest.mark.parametrize(("m", "n"), [("ab", ""), ("abc", "defg")])
|
| 358 |
+
def test_lollipop_graph_size_node_sequence(self, m, n):
|
| 359 |
+
G = nx.lollipop_graph(m, n)
|
| 360 |
+
assert nx.number_of_nodes(G) == len(m) + len(n)
|
| 361 |
+
assert nx.number_of_edges(G) == len(m) * (len(m) - 1) / 2 + len(n)
|
| 362 |
+
|
| 363 |
+
def test_lollipop_graph_exceptions(self):
|
| 364 |
+
# Raise NetworkXError if m<2
|
| 365 |
+
pytest.raises(nx.NetworkXError, nx.lollipop_graph, -1, 2)
|
| 366 |
+
pytest.raises(nx.NetworkXError, nx.lollipop_graph, 1, 20)
|
| 367 |
+
pytest.raises(nx.NetworkXError, nx.lollipop_graph, "", 20)
|
| 368 |
+
pytest.raises(nx.NetworkXError, nx.lollipop_graph, "a", 20)
|
| 369 |
+
|
| 370 |
+
# Raise NetworkXError if n<0
|
| 371 |
+
pytest.raises(nx.NetworkXError, nx.lollipop_graph, 5, -2)
|
| 372 |
+
|
| 373 |
+
# raise NetworkXError if create_using is directed
|
| 374 |
+
with pytest.raises(nx.NetworkXError):
|
| 375 |
+
nx.lollipop_graph(2, 20, create_using=nx.DiGraph)
|
| 376 |
+
with pytest.raises(nx.NetworkXError):
|
| 377 |
+
nx.lollipop_graph(2, 20, create_using=nx.MultiDiGraph)
|
| 378 |
+
|
| 379 |
+
@pytest.mark.parametrize(("m", "n"), [(2, 0), (2, 5), (2, 10), ("ab", 20)])
|
| 380 |
+
def test_lollipop_graph_same_as_path_when_m1_is_2(self, m, n):
|
| 381 |
+
G = nx.lollipop_graph(m, n)
|
| 382 |
+
assert is_isomorphic(G, nx.path_graph(n + 2))
|
| 383 |
+
|
| 384 |
+
def test_lollipop_graph_for_multigraph(self):
|
| 385 |
+
G = nx.lollipop_graph(5, 20)
|
| 386 |
+
MG = nx.lollipop_graph(5, 20, create_using=nx.MultiGraph)
|
| 387 |
+
assert edges_equal(MG.edges(), G.edges())
|
| 388 |
+
|
| 389 |
+
@pytest.mark.parametrize(
|
| 390 |
+
("m", "n"),
|
| 391 |
+
[(4, "abc"), ("abcd", 3), ([1, 2, 3, 4], "abc"), ("abcd", [1, 2, 3])],
|
| 392 |
+
)
|
| 393 |
+
def test_lollipop_graph_mixing_input_types(self, m, n):
|
| 394 |
+
expected = nx.compose(nx.complete_graph(4), nx.path_graph(range(100, 103)))
|
| 395 |
+
expected.add_edge(0, 100) # Connect complete graph and path graph
|
| 396 |
+
assert is_isomorphic(nx.lollipop_graph(m, n), expected)
|
| 397 |
+
|
| 398 |
+
def test_lollipop_graph_non_builtin_ints(self):
|
| 399 |
+
np = pytest.importorskip("numpy")
|
| 400 |
+
G = nx.lollipop_graph(np.int32(4), np.int64(3))
|
| 401 |
+
expected = nx.compose(nx.complete_graph(4), nx.path_graph(range(100, 103)))
|
| 402 |
+
expected.add_edge(0, 100) # Connect complete graph and path graph
|
| 403 |
+
assert is_isomorphic(G, expected)
|
| 404 |
+
|
| 405 |
+
def test_null_graph(self):
|
| 406 |
+
assert nx.number_of_nodes(nx.null_graph()) == 0
|
| 407 |
+
|
| 408 |
+
def test_path_graph(self):
|
| 409 |
+
p = nx.path_graph(0)
|
| 410 |
+
assert is_isomorphic(p, nx.null_graph())
|
| 411 |
+
|
| 412 |
+
p = nx.path_graph(1)
|
| 413 |
+
assert is_isomorphic(p, nx.empty_graph(1))
|
| 414 |
+
|
| 415 |
+
p = nx.path_graph(10)
|
| 416 |
+
assert nx.is_connected(p)
|
| 417 |
+
assert sorted(d for n, d in p.degree()) == [1, 1, 2, 2, 2, 2, 2, 2, 2, 2]
|
| 418 |
+
assert p.order() - 1 == p.size()
|
| 419 |
+
|
| 420 |
+
dp = nx.path_graph(3, create_using=nx.DiGraph)
|
| 421 |
+
assert dp.has_edge(0, 1)
|
| 422 |
+
assert not dp.has_edge(1, 0)
|
| 423 |
+
|
| 424 |
+
mp = nx.path_graph(10, create_using=nx.MultiGraph)
|
| 425 |
+
assert edges_equal(mp.edges(), p.edges())
|
| 426 |
+
|
| 427 |
+
G = nx.path_graph("abc")
|
| 428 |
+
assert len(G) == 3
|
| 429 |
+
assert G.size() == 2
|
| 430 |
+
G = nx.path_graph("abcb")
|
| 431 |
+
assert len(G) == 3
|
| 432 |
+
assert G.size() == 2
|
| 433 |
+
g = nx.path_graph("abc", nx.DiGraph)
|
| 434 |
+
assert len(g) == 3
|
| 435 |
+
assert g.size() == 2
|
| 436 |
+
assert g.is_directed()
|
| 437 |
+
g = nx.path_graph("abcb", nx.DiGraph)
|
| 438 |
+
assert len(g) == 3
|
| 439 |
+
assert g.size() == 3
|
| 440 |
+
|
| 441 |
+
G = nx.path_graph((1, 2, 3, 2, 4))
|
| 442 |
+
assert G.has_edge(2, 4)
|
| 443 |
+
|
| 444 |
+
def test_star_graph(self):
|
| 445 |
+
assert is_isomorphic(nx.star_graph(""), nx.empty_graph(0))
|
| 446 |
+
assert is_isomorphic(nx.star_graph([]), nx.empty_graph(0))
|
| 447 |
+
assert is_isomorphic(nx.star_graph(0), nx.empty_graph(1))
|
| 448 |
+
assert is_isomorphic(nx.star_graph(1), nx.path_graph(2))
|
| 449 |
+
assert is_isomorphic(nx.star_graph(2), nx.path_graph(3))
|
| 450 |
+
assert is_isomorphic(nx.star_graph(5), nx.complete_bipartite_graph(1, 5))
|
| 451 |
+
|
| 452 |
+
s = nx.star_graph(10)
|
| 453 |
+
assert sorted(d for n, d in s.degree()) == [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 10]
|
| 454 |
+
|
| 455 |
+
pytest.raises(nx.NetworkXError, nx.star_graph, 10, create_using=nx.DiGraph)
|
| 456 |
+
|
| 457 |
+
ms = nx.star_graph(10, create_using=nx.MultiGraph)
|
| 458 |
+
assert edges_equal(ms.edges(), s.edges())
|
| 459 |
+
|
| 460 |
+
G = nx.star_graph("abc")
|
| 461 |
+
assert len(G) == 3
|
| 462 |
+
assert G.size() == 2
|
| 463 |
+
|
| 464 |
+
G = nx.star_graph("abcb")
|
| 465 |
+
assert len(G) == 3
|
| 466 |
+
assert G.size() == 2
|
| 467 |
+
G = nx.star_graph("abcb", create_using=nx.MultiGraph)
|
| 468 |
+
assert len(G) == 3
|
| 469 |
+
assert G.size() == 3
|
| 470 |
+
|
| 471 |
+
G = nx.star_graph("abcdefg")
|
| 472 |
+
assert len(G) == 7
|
| 473 |
+
assert G.size() == 6
|
| 474 |
+
|
| 475 |
+
def test_non_int_integers_for_star_graph(self):
|
| 476 |
+
np = pytest.importorskip("numpy")
|
| 477 |
+
G = nx.star_graph(np.int32(3))
|
| 478 |
+
assert len(G) == 4
|
| 479 |
+
assert G.size() == 3
|
| 480 |
+
|
| 481 |
+
@pytest.mark.parametrize(("m", "n"), [(3, 0), (3, 5), (4, 10), (3, 20)])
|
| 482 |
+
def test_tadpole_graph_right_sizes(self, m, n):
|
| 483 |
+
G = nx.tadpole_graph(m, n)
|
| 484 |
+
assert nx.number_of_nodes(G) == m + n
|
| 485 |
+
assert nx.number_of_edges(G) == m + n - (m == 2)
|
| 486 |
+
|
| 487 |
+
@pytest.mark.parametrize(("m", "n"), [("ab", ""), ("ab", "c"), ("abc", "defg")])
|
| 488 |
+
def test_tadpole_graph_size_node_sequences(self, m, n):
|
| 489 |
+
G = nx.tadpole_graph(m, n)
|
| 490 |
+
assert nx.number_of_nodes(G) == len(m) + len(n)
|
| 491 |
+
assert nx.number_of_edges(G) == len(m) + len(n) - (len(m) == 2)
|
| 492 |
+
|
| 493 |
+
def test_tadpole_graph_exceptions(self):
|
| 494 |
+
# Raise NetworkXError if m<2
|
| 495 |
+
pytest.raises(nx.NetworkXError, nx.tadpole_graph, -1, 3)
|
| 496 |
+
pytest.raises(nx.NetworkXError, nx.tadpole_graph, 0, 3)
|
| 497 |
+
pytest.raises(nx.NetworkXError, nx.tadpole_graph, 1, 3)
|
| 498 |
+
|
| 499 |
+
# Raise NetworkXError if n<0
|
| 500 |
+
pytest.raises(nx.NetworkXError, nx.tadpole_graph, 5, -2)
|
| 501 |
+
|
| 502 |
+
# Raise NetworkXError for digraphs
|
| 503 |
+
with pytest.raises(nx.NetworkXError):
|
| 504 |
+
nx.tadpole_graph(2, 20, create_using=nx.DiGraph)
|
| 505 |
+
with pytest.raises(nx.NetworkXError):
|
| 506 |
+
nx.tadpole_graph(2, 20, create_using=nx.MultiDiGraph)
|
| 507 |
+
|
| 508 |
+
@pytest.mark.parametrize(("m", "n"), [(2, 0), (2, 5), (2, 10), ("ab", 20)])
|
| 509 |
+
def test_tadpole_graph_same_as_path_when_m_is_2(self, m, n):
|
| 510 |
+
G = nx.tadpole_graph(m, n)
|
| 511 |
+
assert is_isomorphic(G, nx.path_graph(n + 2))
|
| 512 |
+
|
| 513 |
+
@pytest.mark.parametrize("m", [4, 7])
|
| 514 |
+
def test_tadpole_graph_same_as_cycle_when_m2_is_0(self, m):
|
| 515 |
+
G = nx.tadpole_graph(m, 0)
|
| 516 |
+
assert is_isomorphic(G, nx.cycle_graph(m))
|
| 517 |
+
|
| 518 |
+
def test_tadpole_graph_for_multigraph(self):
|
| 519 |
+
G = nx.tadpole_graph(5, 20)
|
| 520 |
+
MG = nx.tadpole_graph(5, 20, create_using=nx.MultiGraph)
|
| 521 |
+
assert edges_equal(MG.edges(), G.edges())
|
| 522 |
+
|
| 523 |
+
@pytest.mark.parametrize(
|
| 524 |
+
("m", "n"),
|
| 525 |
+
[(4, "abc"), ("abcd", 3), ([1, 2, 3, 4], "abc"), ("abcd", [1, 2, 3])],
|
| 526 |
+
)
|
| 527 |
+
def test_tadpole_graph_mixing_input_types(self, m, n):
|
| 528 |
+
expected = nx.compose(nx.cycle_graph(4), nx.path_graph(range(100, 103)))
|
| 529 |
+
expected.add_edge(0, 100) # Connect cycle and path
|
| 530 |
+
assert is_isomorphic(nx.tadpole_graph(m, n), expected)
|
| 531 |
+
|
| 532 |
+
def test_tadpole_graph_non_builtin_integers(self):
|
| 533 |
+
np = pytest.importorskip("numpy")
|
| 534 |
+
G = nx.tadpole_graph(np.int32(4), np.int64(3))
|
| 535 |
+
expected = nx.compose(nx.cycle_graph(4), nx.path_graph(range(100, 103)))
|
| 536 |
+
expected.add_edge(0, 100) # Connect cycle and path
|
| 537 |
+
assert is_isomorphic(G, expected)
|
| 538 |
+
|
| 539 |
+
def test_trivial_graph(self):
|
| 540 |
+
assert nx.number_of_nodes(nx.trivial_graph()) == 1
|
| 541 |
+
|
| 542 |
+
def test_turan_graph(self):
|
| 543 |
+
assert nx.number_of_edges(nx.turan_graph(13, 4)) == 63
|
| 544 |
+
assert is_isomorphic(
|
| 545 |
+
nx.turan_graph(13, 4), nx.complete_multipartite_graph(3, 4, 3, 3)
|
| 546 |
+
)
|
| 547 |
+
|
| 548 |
+
def test_wheel_graph(self):
|
| 549 |
+
for n, G in [
|
| 550 |
+
("", nx.null_graph()),
|
| 551 |
+
(0, nx.null_graph()),
|
| 552 |
+
(1, nx.empty_graph(1)),
|
| 553 |
+
(2, nx.path_graph(2)),
|
| 554 |
+
(3, nx.complete_graph(3)),
|
| 555 |
+
(4, nx.complete_graph(4)),
|
| 556 |
+
]:
|
| 557 |
+
g = nx.wheel_graph(n)
|
| 558 |
+
assert is_isomorphic(g, G)
|
| 559 |
+
|
| 560 |
+
g = nx.wheel_graph(10)
|
| 561 |
+
assert sorted(d for n, d in g.degree()) == [3, 3, 3, 3, 3, 3, 3, 3, 3, 9]
|
| 562 |
+
|
| 563 |
+
pytest.raises(nx.NetworkXError, nx.wheel_graph, 10, create_using=nx.DiGraph)
|
| 564 |
+
|
| 565 |
+
mg = nx.wheel_graph(10, create_using=nx.MultiGraph())
|
| 566 |
+
assert edges_equal(mg.edges(), g.edges())
|
| 567 |
+
|
| 568 |
+
G = nx.wheel_graph("abc")
|
| 569 |
+
assert len(G) == 3
|
| 570 |
+
assert G.size() == 3
|
| 571 |
+
|
| 572 |
+
G = nx.wheel_graph("abcb")
|
| 573 |
+
assert len(G) == 3
|
| 574 |
+
assert G.size() == 4
|
| 575 |
+
G = nx.wheel_graph("abcb", nx.MultiGraph)
|
| 576 |
+
assert len(G) == 3
|
| 577 |
+
assert G.size() == 6
|
| 578 |
+
|
| 579 |
+
def test_non_int_integers_for_wheel_graph(self):
|
| 580 |
+
np = pytest.importorskip("numpy")
|
| 581 |
+
G = nx.wheel_graph(np.int32(3))
|
| 582 |
+
assert len(G) == 3
|
| 583 |
+
assert G.size() == 3
|
| 584 |
+
|
| 585 |
+
def test_complete_0_partite_graph(self):
|
| 586 |
+
"""Tests that the complete 0-partite graph is the null graph."""
|
| 587 |
+
G = nx.complete_multipartite_graph()
|
| 588 |
+
H = nx.null_graph()
|
| 589 |
+
assert nodes_equal(G, H)
|
| 590 |
+
assert edges_equal(G.edges(), H.edges())
|
| 591 |
+
|
| 592 |
+
def test_complete_1_partite_graph(self):
|
| 593 |
+
"""Tests that the complete 1-partite graph is the empty graph."""
|
| 594 |
+
G = nx.complete_multipartite_graph(3)
|
| 595 |
+
H = nx.empty_graph(3)
|
| 596 |
+
assert nodes_equal(G, H)
|
| 597 |
+
assert edges_equal(G.edges(), H.edges())
|
| 598 |
+
|
| 599 |
+
def test_complete_2_partite_graph(self):
|
| 600 |
+
"""Tests that the complete 2-partite graph is the complete bipartite
|
| 601 |
+
graph.
|
| 602 |
+
|
| 603 |
+
"""
|
| 604 |
+
G = nx.complete_multipartite_graph(2, 3)
|
| 605 |
+
H = nx.complete_bipartite_graph(2, 3)
|
| 606 |
+
assert nodes_equal(G, H)
|
| 607 |
+
assert edges_equal(G.edges(), H.edges())
|
| 608 |
+
|
| 609 |
+
def test_complete_multipartite_graph(self):
|
| 610 |
+
"""Tests for generating the complete multipartite graph."""
|
| 611 |
+
G = nx.complete_multipartite_graph(2, 3, 4)
|
| 612 |
+
blocks = [(0, 1), (2, 3, 4), (5, 6, 7, 8)]
|
| 613 |
+
# Within each block, no two vertices should be adjacent.
|
| 614 |
+
for block in blocks:
|
| 615 |
+
for u, v in itertools.combinations_with_replacement(block, 2):
|
| 616 |
+
assert v not in G[u]
|
| 617 |
+
assert G.nodes[u] == G.nodes[v]
|
| 618 |
+
# Across blocks, all vertices should be adjacent.
|
| 619 |
+
for block1, block2 in itertools.combinations(blocks, 2):
|
| 620 |
+
for u, v in itertools.product(block1, block2):
|
| 621 |
+
assert v in G[u]
|
| 622 |
+
assert G.nodes[u] != G.nodes[v]
|
| 623 |
+
with pytest.raises(nx.NetworkXError, match="Negative number of nodes"):
|
| 624 |
+
nx.complete_multipartite_graph(2, -3, 4)
|
| 625 |
+
|
| 626 |
+
def test_kneser_graph(self):
|
| 627 |
+
# the petersen graph is a special case of the kneser graph when n=5 and k=2
|
| 628 |
+
assert is_isomorphic(nx.kneser_graph(5, 2), nx.petersen_graph())
|
| 629 |
+
|
| 630 |
+
# when k is 1, the kneser graph returns a complete graph with n vertices
|
| 631 |
+
for i in range(1, 7):
|
| 632 |
+
assert is_isomorphic(nx.kneser_graph(i, 1), nx.complete_graph(i))
|
| 633 |
+
|
| 634 |
+
# the kneser graph of n and n-1 is the empty graph with n vertices
|
| 635 |
+
for j in range(3, 7):
|
| 636 |
+
assert is_isomorphic(nx.kneser_graph(j, j - 1), nx.empty_graph(j))
|
| 637 |
+
|
| 638 |
+
# in general the number of edges of the kneser graph is equal to
|
| 639 |
+
# (n choose k) times (n-k choose k) divided by 2
|
| 640 |
+
assert nx.number_of_edges(nx.kneser_graph(8, 3)) == 280
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/test_ego.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
ego graph
|
| 3 |
+
---------
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import networkx as nx
|
| 7 |
+
from networkx.utils import edges_equal, nodes_equal
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class TestGeneratorEgo:
|
| 11 |
+
def test_ego(self):
|
| 12 |
+
G = nx.star_graph(3)
|
| 13 |
+
H = nx.ego_graph(G, 0)
|
| 14 |
+
assert nx.is_isomorphic(G, H)
|
| 15 |
+
G.add_edge(1, 11)
|
| 16 |
+
G.add_edge(2, 22)
|
| 17 |
+
G.add_edge(3, 33)
|
| 18 |
+
H = nx.ego_graph(G, 0)
|
| 19 |
+
assert nx.is_isomorphic(nx.star_graph(3), H)
|
| 20 |
+
G = nx.path_graph(3)
|
| 21 |
+
H = nx.ego_graph(G, 0)
|
| 22 |
+
assert edges_equal(H.edges(), [(0, 1)])
|
| 23 |
+
H = nx.ego_graph(G, 0, undirected=True)
|
| 24 |
+
assert edges_equal(H.edges(), [(0, 1)])
|
| 25 |
+
H = nx.ego_graph(G, 0, center=False)
|
| 26 |
+
assert edges_equal(H.edges(), [])
|
| 27 |
+
|
| 28 |
+
def test_ego_distance(self):
|
| 29 |
+
G = nx.Graph()
|
| 30 |
+
G.add_edge(0, 1, weight=2, distance=1)
|
| 31 |
+
G.add_edge(1, 2, weight=2, distance=2)
|
| 32 |
+
G.add_edge(2, 3, weight=2, distance=1)
|
| 33 |
+
assert nodes_equal(nx.ego_graph(G, 0, radius=3).nodes(), [0, 1, 2, 3])
|
| 34 |
+
eg = nx.ego_graph(G, 0, radius=3, distance="weight")
|
| 35 |
+
assert nodes_equal(eg.nodes(), [0, 1])
|
| 36 |
+
eg = nx.ego_graph(G, 0, radius=3, distance="weight", undirected=True)
|
| 37 |
+
assert nodes_equal(eg.nodes(), [0, 1])
|
| 38 |
+
eg = nx.ego_graph(G, 0, radius=3, distance="distance")
|
| 39 |
+
assert nodes_equal(eg.nodes(), [0, 1, 2])
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/test_expanders.py
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Unit tests for the :mod:`networkx.generators.expanders` module."""
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
import networkx as nx
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
@pytest.mark.parametrize("n", (2, 3, 5, 6, 10))
|
| 9 |
+
def test_margulis_gabber_galil_graph_properties(n):
|
| 10 |
+
g = nx.margulis_gabber_galil_graph(n)
|
| 11 |
+
assert g.number_of_nodes() == n * n
|
| 12 |
+
for node in g:
|
| 13 |
+
assert g.degree(node) == 8
|
| 14 |
+
assert len(node) == 2
|
| 15 |
+
for i in node:
|
| 16 |
+
assert int(i) == i
|
| 17 |
+
assert 0 <= i < n
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@pytest.mark.parametrize("n", (2, 3, 5, 6, 10))
|
| 21 |
+
def test_margulis_gabber_galil_graph_eigvals(n):
|
| 22 |
+
np = pytest.importorskip("numpy")
|
| 23 |
+
sp = pytest.importorskip("scipy")
|
| 24 |
+
|
| 25 |
+
g = nx.margulis_gabber_galil_graph(n)
|
| 26 |
+
# Eigenvalues are already sorted using the scipy eigvalsh,
|
| 27 |
+
# but the implementation in numpy does not guarantee order.
|
| 28 |
+
w = sorted(sp.linalg.eigvalsh(nx.adjacency_matrix(g).toarray()))
|
| 29 |
+
assert w[-2] < 5 * np.sqrt(2)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
@pytest.mark.parametrize("p", (3, 5, 7, 11)) # Primes
|
| 33 |
+
def test_chordal_cycle_graph(p):
|
| 34 |
+
"""Test for the :func:`networkx.chordal_cycle_graph` function."""
|
| 35 |
+
G = nx.chordal_cycle_graph(p)
|
| 36 |
+
assert len(G) == p
|
| 37 |
+
# TODO The second largest eigenvalue should be smaller than a constant,
|
| 38 |
+
# independent of the number of nodes in the graph:
|
| 39 |
+
#
|
| 40 |
+
# eigs = sorted(sp.linalg.eigvalsh(nx.adjacency_matrix(G).toarray()))
|
| 41 |
+
# assert_less(eigs[-2], ...)
|
| 42 |
+
#
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
@pytest.mark.parametrize("p", (3, 5, 7, 11, 13)) # Primes
|
| 46 |
+
def test_paley_graph(p):
|
| 47 |
+
"""Test for the :func:`networkx.paley_graph` function."""
|
| 48 |
+
G = nx.paley_graph(p)
|
| 49 |
+
# G has p nodes
|
| 50 |
+
assert len(G) == p
|
| 51 |
+
# G is (p-1)/2-regular
|
| 52 |
+
in_degrees = {G.in_degree(node) for node in G.nodes}
|
| 53 |
+
out_degrees = {G.out_degree(node) for node in G.nodes}
|
| 54 |
+
assert len(in_degrees) == 1 and in_degrees.pop() == (p - 1) // 2
|
| 55 |
+
assert len(out_degrees) == 1 and out_degrees.pop() == (p - 1) // 2
|
| 56 |
+
|
| 57 |
+
# If p = 1 mod 4, -1 is a square mod 4 and therefore the
|
| 58 |
+
# edge in the Paley graph are symmetric.
|
| 59 |
+
if p % 4 == 1:
|
| 60 |
+
for u, v in G.edges:
|
| 61 |
+
assert (v, u) in G.edges
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
@pytest.mark.parametrize("d, n", [(2, 7), (4, 10), (4, 16)])
|
| 65 |
+
def test_maybe_regular_expander(d, n):
|
| 66 |
+
pytest.importorskip("numpy")
|
| 67 |
+
G = nx.maybe_regular_expander(n, d)
|
| 68 |
+
|
| 69 |
+
assert len(G) == n, "Should have n nodes"
|
| 70 |
+
assert len(G.edges) == n * d / 2, "Should have n*d/2 edges"
|
| 71 |
+
assert nx.is_k_regular(G, d), "Should be d-regular"
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
@pytest.mark.parametrize("n", (3, 5, 6, 10))
|
| 75 |
+
def test_is_regular_expander(n):
|
| 76 |
+
pytest.importorskip("numpy")
|
| 77 |
+
pytest.importorskip("scipy")
|
| 78 |
+
G = nx.complete_graph(n)
|
| 79 |
+
|
| 80 |
+
assert nx.is_regular_expander(G) == True, "Should be a regular expander"
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
@pytest.mark.parametrize("d, n", [(2, 7), (4, 10), (4, 16)])
|
| 84 |
+
def test_random_regular_expander(d, n):
|
| 85 |
+
pytest.importorskip("numpy")
|
| 86 |
+
pytest.importorskip("scipy")
|
| 87 |
+
G = nx.random_regular_expander_graph(n, d)
|
| 88 |
+
|
| 89 |
+
assert len(G) == n, "Should have n nodes"
|
| 90 |
+
assert len(G.edges) == n * d / 2, "Should have n*d/2 edges"
|
| 91 |
+
assert nx.is_k_regular(G, d), "Should be d-regular"
|
| 92 |
+
assert nx.is_regular_expander(G) == True, "Should be a regular expander"
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def test_random_regular_expander_explicit_construction():
|
| 96 |
+
pytest.importorskip("numpy")
|
| 97 |
+
pytest.importorskip("scipy")
|
| 98 |
+
G = nx.random_regular_expander_graph(d=4, n=5)
|
| 99 |
+
|
| 100 |
+
assert len(G) == 5 and len(G.edges) == 10, "Should be a complete graph"
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
@pytest.mark.parametrize("graph_type", (nx.Graph, nx.DiGraph, nx.MultiDiGraph))
|
| 104 |
+
def test_margulis_gabber_galil_graph_badinput(graph_type):
|
| 105 |
+
with pytest.raises(
|
| 106 |
+
nx.NetworkXError, match="`create_using` must be an undirected multigraph"
|
| 107 |
+
):
|
| 108 |
+
nx.margulis_gabber_galil_graph(3, create_using=graph_type)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
@pytest.mark.parametrize("graph_type", (nx.Graph, nx.DiGraph, nx.MultiDiGraph))
|
| 112 |
+
def test_chordal_cycle_graph_badinput(graph_type):
|
| 113 |
+
with pytest.raises(
|
| 114 |
+
nx.NetworkXError, match="`create_using` must be an undirected multigraph"
|
| 115 |
+
):
|
| 116 |
+
nx.chordal_cycle_graph(3, create_using=graph_type)
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def test_paley_graph_badinput():
|
| 120 |
+
with pytest.raises(
|
| 121 |
+
nx.NetworkXError, match="`create_using` cannot be a multigraph."
|
| 122 |
+
):
|
| 123 |
+
nx.paley_graph(3, create_using=nx.MultiGraph)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def test_maybe_regular_expander_badinput():
|
| 127 |
+
pytest.importorskip("numpy")
|
| 128 |
+
pytest.importorskip("scipy")
|
| 129 |
+
|
| 130 |
+
with pytest.raises(nx.NetworkXError, match="n must be a positive integer"):
|
| 131 |
+
nx.maybe_regular_expander(n=-1, d=2)
|
| 132 |
+
|
| 133 |
+
with pytest.raises(nx.NetworkXError, match="d must be greater than or equal to 2"):
|
| 134 |
+
nx.maybe_regular_expander(n=10, d=0)
|
| 135 |
+
|
| 136 |
+
with pytest.raises(nx.NetworkXError, match="Need n-1>= d to have room"):
|
| 137 |
+
nx.maybe_regular_expander(n=5, d=6)
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def test_is_regular_expander_badinput():
|
| 141 |
+
pytest.importorskip("numpy")
|
| 142 |
+
pytest.importorskip("scipy")
|
| 143 |
+
|
| 144 |
+
with pytest.raises(nx.NetworkXError, match="epsilon must be non negative"):
|
| 145 |
+
nx.is_regular_expander(nx.Graph(), epsilon=-1)
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
def test_random_regular_expander_badinput():
|
| 149 |
+
pytest.importorskip("numpy")
|
| 150 |
+
pytest.importorskip("scipy")
|
| 151 |
+
|
| 152 |
+
with pytest.raises(nx.NetworkXError, match="n must be a positive integer"):
|
| 153 |
+
nx.random_regular_expander_graph(n=-1, d=2)
|
| 154 |
+
|
| 155 |
+
with pytest.raises(nx.NetworkXError, match="d must be greater than or equal to 2"):
|
| 156 |
+
nx.random_regular_expander_graph(n=10, d=0)
|
| 157 |
+
|
| 158 |
+
with pytest.raises(nx.NetworkXError, match="Need n-1>= d to have room"):
|
| 159 |
+
nx.random_regular_expander_graph(n=5, d=6)
|
| 160 |
+
|
| 161 |
+
with pytest.raises(nx.NetworkXError, match="epsilon must be non negative"):
|
| 162 |
+
nx.random_regular_expander_graph(n=4, d=2, epsilon=-1)
|
wemm/lib/python3.10/site-packages/networkx/generators/tests/test_geometric.py
ADDED
|
@@ -0,0 +1,488 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
import random
|
| 3 |
+
from itertools import combinations
|
| 4 |
+
|
| 5 |
+
import pytest
|
| 6 |
+
|
| 7 |
+
import networkx as nx
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def l1dist(x, y):
|
| 11 |
+
return sum(abs(a - b) for a, b in zip(x, y))
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class TestRandomGeometricGraph:
|
| 15 |
+
"""Unit tests for :func:`~networkx.random_geometric_graph`"""
|
| 16 |
+
|
| 17 |
+
def test_number_of_nodes(self):
|
| 18 |
+
G = nx.random_geometric_graph(50, 0.25, seed=42)
|
| 19 |
+
assert len(G) == 50
|
| 20 |
+
G = nx.random_geometric_graph(range(50), 0.25, seed=42)
|
| 21 |
+
assert len(G) == 50
|
| 22 |
+
|
| 23 |
+
def test_distances(self):
|
| 24 |
+
"""Tests that pairs of vertices adjacent if and only if they are
|
| 25 |
+
within the prescribed radius.
|
| 26 |
+
"""
|
| 27 |
+
# Use the Euclidean metric, the default according to the
|
| 28 |
+
# documentation.
|
| 29 |
+
G = nx.random_geometric_graph(50, 0.25)
|
| 30 |
+
for u, v in combinations(G, 2):
|
| 31 |
+
# Adjacent vertices must be within the given distance.
|
| 32 |
+
if v in G[u]:
|
| 33 |
+
assert math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
| 34 |
+
# Nonadjacent vertices must be at greater distance.
|
| 35 |
+
else:
|
| 36 |
+
assert not math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
| 37 |
+
|
| 38 |
+
def test_p(self):
|
| 39 |
+
"""Tests for providing an alternate distance metric to the generator."""
|
| 40 |
+
# Use the L1 metric.
|
| 41 |
+
G = nx.random_geometric_graph(50, 0.25, p=1)
|
| 42 |
+
for u, v in combinations(G, 2):
|
| 43 |
+
# Adjacent vertices must be within the given distance.
|
| 44 |
+
if v in G[u]:
|
| 45 |
+
assert l1dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
| 46 |
+
# Nonadjacent vertices must be at greater distance.
|
| 47 |
+
else:
|
| 48 |
+
assert not l1dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
| 49 |
+
|
| 50 |
+
def test_node_names(self):
|
| 51 |
+
"""Tests using values other than sequential numbers as node IDs."""
|
| 52 |
+
import string
|
| 53 |
+
|
| 54 |
+
nodes = list(string.ascii_lowercase)
|
| 55 |
+
G = nx.random_geometric_graph(nodes, 0.25)
|
| 56 |
+
assert len(G) == len(nodes)
|
| 57 |
+
|
| 58 |
+
for u, v in combinations(G, 2):
|
| 59 |
+
# Adjacent vertices must be within the given distance.
|
| 60 |
+
if v in G[u]:
|
| 61 |
+
assert math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
| 62 |
+
# Nonadjacent vertices must be at greater distance.
|
| 63 |
+
else:
|
| 64 |
+
assert not math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
| 65 |
+
|
| 66 |
+
def test_pos_name(self):
|
| 67 |
+
G = nx.random_geometric_graph(50, 0.25, seed=42, pos_name="coords")
|
| 68 |
+
assert all(len(d["coords"]) == 2 for n, d in G.nodes.items())
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
class TestSoftRandomGeometricGraph:
|
| 72 |
+
"""Unit tests for :func:`~networkx.soft_random_geometric_graph`"""
|
| 73 |
+
|
| 74 |
+
def test_number_of_nodes(self):
|
| 75 |
+
G = nx.soft_random_geometric_graph(50, 0.25, seed=42)
|
| 76 |
+
assert len(G) == 50
|
| 77 |
+
G = nx.soft_random_geometric_graph(range(50), 0.25, seed=42)
|
| 78 |
+
assert len(G) == 50
|
| 79 |
+
|
| 80 |
+
def test_distances(self):
|
| 81 |
+
"""Tests that pairs of vertices adjacent if and only if they are
|
| 82 |
+
within the prescribed radius.
|
| 83 |
+
"""
|
| 84 |
+
# Use the Euclidean metric, the default according to the
|
| 85 |
+
# documentation.
|
| 86 |
+
G = nx.soft_random_geometric_graph(50, 0.25)
|
| 87 |
+
for u, v in combinations(G, 2):
|
| 88 |
+
# Adjacent vertices must be within the given distance.
|
| 89 |
+
if v in G[u]:
|
| 90 |
+
assert math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
| 91 |
+
|
| 92 |
+
def test_p(self):
|
| 93 |
+
"""Tests for providing an alternate distance metric to the generator."""
|
| 94 |
+
|
| 95 |
+
# Use the L1 metric.
|
| 96 |
+
def dist(x, y):
|
| 97 |
+
return sum(abs(a - b) for a, b in zip(x, y))
|
| 98 |
+
|
| 99 |
+
G = nx.soft_random_geometric_graph(50, 0.25, p=1)
|
| 100 |
+
for u, v in combinations(G, 2):
|
| 101 |
+
# Adjacent vertices must be within the given distance.
|
| 102 |
+
if v in G[u]:
|
| 103 |
+
assert dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
| 104 |
+
|
| 105 |
+
def test_node_names(self):
|
| 106 |
+
"""Tests using values other than sequential numbers as node IDs."""
|
| 107 |
+
import string
|
| 108 |
+
|
| 109 |
+
nodes = list(string.ascii_lowercase)
|
| 110 |
+
G = nx.soft_random_geometric_graph(nodes, 0.25)
|
| 111 |
+
assert len(G) == len(nodes)
|
| 112 |
+
|
| 113 |
+
for u, v in combinations(G, 2):
|
| 114 |
+
# Adjacent vertices must be within the given distance.
|
| 115 |
+
if v in G[u]:
|
| 116 |
+
assert math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
| 117 |
+
|
| 118 |
+
def test_p_dist_default(self):
|
| 119 |
+
"""Tests default p_dict = 0.5 returns graph with edge count <= RGG with
|
| 120 |
+
same n, radius, dim and positions
|
| 121 |
+
"""
|
| 122 |
+
nodes = 50
|
| 123 |
+
dim = 2
|
| 124 |
+
pos = {v: [random.random() for i in range(dim)] for v in range(nodes)}
|
| 125 |
+
RGG = nx.random_geometric_graph(50, 0.25, pos=pos)
|
| 126 |
+
SRGG = nx.soft_random_geometric_graph(50, 0.25, pos=pos)
|
| 127 |
+
assert len(SRGG.edges()) <= len(RGG.edges())
|
| 128 |
+
|
| 129 |
+
def test_p_dist_zero(self):
|
| 130 |
+
"""Tests if p_dict = 0 returns disconnected graph with 0 edges"""
|
| 131 |
+
|
| 132 |
+
def p_dist(dist):
|
| 133 |
+
return 0
|
| 134 |
+
|
| 135 |
+
G = nx.soft_random_geometric_graph(50, 0.25, p_dist=p_dist)
|
| 136 |
+
assert len(G.edges) == 0
|
| 137 |
+
|
| 138 |
+
def test_pos_name(self):
|
| 139 |
+
G = nx.soft_random_geometric_graph(50, 0.25, seed=42, pos_name="coords")
|
| 140 |
+
assert all(len(d["coords"]) == 2 for n, d in G.nodes.items())
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def join(G, u, v, theta, alpha, metric):
|
| 144 |
+
"""Returns ``True`` if and only if the nodes whose attributes are
|
| 145 |
+
``du`` and ``dv`` should be joined, according to the threshold
|
| 146 |
+
condition for geographical threshold graphs.
|
| 147 |
+
|
| 148 |
+
``G`` is an undirected NetworkX graph, and ``u`` and ``v`` are nodes
|
| 149 |
+
in that graph. The nodes must have node attributes ``'pos'`` and
|
| 150 |
+
``'weight'``.
|
| 151 |
+
|
| 152 |
+
``metric`` is a distance metric.
|
| 153 |
+
"""
|
| 154 |
+
du, dv = G.nodes[u], G.nodes[v]
|
| 155 |
+
u_pos, v_pos = du["pos"], dv["pos"]
|
| 156 |
+
u_weight, v_weight = du["weight"], dv["weight"]
|
| 157 |
+
return (u_weight + v_weight) * metric(u_pos, v_pos) ** alpha >= theta
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
class TestGeographicalThresholdGraph:
|
| 161 |
+
"""Unit tests for :func:`~networkx.geographical_threshold_graph`"""
|
| 162 |
+
|
| 163 |
+
def test_number_of_nodes(self):
|
| 164 |
+
G = nx.geographical_threshold_graph(50, 100, seed=42)
|
| 165 |
+
assert len(G) == 50
|
| 166 |
+
G = nx.geographical_threshold_graph(range(50), 100, seed=42)
|
| 167 |
+
assert len(G) == 50
|
| 168 |
+
|
| 169 |
+
def test_distances(self):
|
| 170 |
+
"""Tests that pairs of vertices adjacent if and only if their
|
| 171 |
+
distances meet the given threshold.
|
| 172 |
+
"""
|
| 173 |
+
# Use the Euclidean metric and alpha = -2
|
| 174 |
+
# the default according to the documentation.
|
| 175 |
+
G = nx.geographical_threshold_graph(50, 10)
|
| 176 |
+
for u, v in combinations(G, 2):
|
| 177 |
+
# Adjacent vertices must exceed the threshold.
|
| 178 |
+
if v in G[u]:
|
| 179 |
+
assert join(G, u, v, 10, -2, math.dist)
|
| 180 |
+
# Nonadjacent vertices must not exceed the threshold.
|
| 181 |
+
else:
|
| 182 |
+
assert not join(G, u, v, 10, -2, math.dist)
|
| 183 |
+
|
| 184 |
+
def test_metric(self):
|
| 185 |
+
"""Tests for providing an alternate distance metric to the generator."""
|
| 186 |
+
# Use the L1 metric.
|
| 187 |
+
G = nx.geographical_threshold_graph(50, 10, metric=l1dist)
|
| 188 |
+
for u, v in combinations(G, 2):
|
| 189 |
+
# Adjacent vertices must exceed the threshold.
|
| 190 |
+
if v in G[u]:
|
| 191 |
+
assert join(G, u, v, 10, -2, l1dist)
|
| 192 |
+
# Nonadjacent vertices must not exceed the threshold.
|
| 193 |
+
else:
|
| 194 |
+
assert not join(G, u, v, 10, -2, l1dist)
|
| 195 |
+
|
| 196 |
+
def test_p_dist_zero(self):
|
| 197 |
+
"""Tests if p_dict = 0 returns disconnected graph with 0 edges"""
|
| 198 |
+
|
| 199 |
+
def p_dist(dist):
|
| 200 |
+
return 0
|
| 201 |
+
|
| 202 |
+
G = nx.geographical_threshold_graph(50, 1, p_dist=p_dist)
|
| 203 |
+
assert len(G.edges) == 0
|
| 204 |
+
|
| 205 |
+
def test_pos_weight_name(self):
|
| 206 |
+
gtg = nx.geographical_threshold_graph
|
| 207 |
+
G = gtg(50, 100, seed=42, pos_name="coords", weight_name="wt")
|
| 208 |
+
assert all(len(d["coords"]) == 2 for n, d in G.nodes.items())
|
| 209 |
+
assert all(d["wt"] > 0 for n, d in G.nodes.items())
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
class TestWaxmanGraph:
|
| 213 |
+
"""Unit tests for the :func:`~networkx.waxman_graph` function."""
|
| 214 |
+
|
| 215 |
+
def test_number_of_nodes_1(self):
|
| 216 |
+
G = nx.waxman_graph(50, 0.5, 0.1, seed=42)
|
| 217 |
+
assert len(G) == 50
|
| 218 |
+
G = nx.waxman_graph(range(50), 0.5, 0.1, seed=42)
|
| 219 |
+
assert len(G) == 50
|
| 220 |
+
|
| 221 |
+
def test_number_of_nodes_2(self):
|
| 222 |
+
G = nx.waxman_graph(50, 0.5, 0.1, L=1)
|
| 223 |
+
assert len(G) == 50
|
| 224 |
+
G = nx.waxman_graph(range(50), 0.5, 0.1, L=1)
|
| 225 |
+
assert len(G) == 50
|
| 226 |
+
|
| 227 |
+
def test_metric(self):
|
| 228 |
+
"""Tests for providing an alternate distance metric to the generator."""
|
| 229 |
+
# Use the L1 metric.
|
| 230 |
+
G = nx.waxman_graph(50, 0.5, 0.1, metric=l1dist)
|
| 231 |
+
assert len(G) == 50
|
| 232 |
+
|
| 233 |
+
def test_pos_name(self):
|
| 234 |
+
G = nx.waxman_graph(50, 0.5, 0.1, seed=42, pos_name="coords")
|
| 235 |
+
assert all(len(d["coords"]) == 2 for n, d in G.nodes.items())
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
class TestNavigableSmallWorldGraph:
|
| 239 |
+
def test_navigable_small_world(self):
|
| 240 |
+
G = nx.navigable_small_world_graph(5, p=1, q=0, seed=42)
|
| 241 |
+
gg = nx.grid_2d_graph(5, 5).to_directed()
|
| 242 |
+
assert nx.is_isomorphic(G, gg)
|
| 243 |
+
|
| 244 |
+
G = nx.navigable_small_world_graph(5, p=1, q=0, dim=3)
|
| 245 |
+
gg = nx.grid_graph([5, 5, 5]).to_directed()
|
| 246 |
+
assert nx.is_isomorphic(G, gg)
|
| 247 |
+
|
| 248 |
+
G = nx.navigable_small_world_graph(5, p=1, q=0, dim=1)
|
| 249 |
+
gg = nx.grid_graph([5]).to_directed()
|
| 250 |
+
assert nx.is_isomorphic(G, gg)
|
| 251 |
+
|
| 252 |
+
def test_invalid_diameter_value(self):
|
| 253 |
+
with pytest.raises(nx.NetworkXException, match=".*p must be >= 1"):
|
| 254 |
+
nx.navigable_small_world_graph(5, p=0, q=0, dim=1)
|
| 255 |
+
|
| 256 |
+
def test_invalid_long_range_connections_value(self):
|
| 257 |
+
with pytest.raises(nx.NetworkXException, match=".*q must be >= 0"):
|
| 258 |
+
nx.navigable_small_world_graph(5, p=1, q=-1, dim=1)
|
| 259 |
+
|
| 260 |
+
def test_invalid_exponent_for_decaying_probability_value(self):
|
| 261 |
+
with pytest.raises(nx.NetworkXException, match=".*r must be >= 0"):
|
| 262 |
+
nx.navigable_small_world_graph(5, p=1, q=0, r=-1, dim=1)
|
| 263 |
+
|
| 264 |
+
def test_r_between_0_and_1(self):
|
| 265 |
+
"""Smoke test for radius in range [0, 1]"""
|
| 266 |
+
# q=0 means no long-range connections
|
| 267 |
+
G = nx.navigable_small_world_graph(3, p=1, q=0, r=0.5, dim=2, seed=42)
|
| 268 |
+
expected = nx.grid_2d_graph(3, 3, create_using=nx.DiGraph)
|
| 269 |
+
assert nx.utils.graphs_equal(G, expected)
|
| 270 |
+
|
| 271 |
+
@pytest.mark.parametrize("seed", range(2478, 2578, 10))
|
| 272 |
+
def test_r_general_scaling(self, seed):
|
| 273 |
+
"""The probability of adding a long-range edge scales with `1 / dist**r`,
|
| 274 |
+
so a navigable_small_world graph created with r < 1 should generally
|
| 275 |
+
result in more edges than a navigable_small_world graph with r >= 1
|
| 276 |
+
(for 0 < q << n).
|
| 277 |
+
|
| 278 |
+
N.B. this is probabilistic, so this test may not hold for all seeds."""
|
| 279 |
+
G1 = nx.navigable_small_world_graph(7, q=3, r=0.5, seed=seed)
|
| 280 |
+
G2 = nx.navigable_small_world_graph(7, q=3, r=1, seed=seed)
|
| 281 |
+
G3 = nx.navigable_small_world_graph(7, q=3, r=2, seed=seed)
|
| 282 |
+
assert G1.number_of_edges() > G2.number_of_edges()
|
| 283 |
+
assert G2.number_of_edges() > G3.number_of_edges()
|
| 284 |
+
|
| 285 |
+
|
| 286 |
+
class TestThresholdedRandomGeometricGraph:
|
| 287 |
+
"""Unit tests for :func:`~networkx.thresholded_random_geometric_graph`"""
|
| 288 |
+
|
| 289 |
+
def test_number_of_nodes(self):
|
| 290 |
+
G = nx.thresholded_random_geometric_graph(50, 0.2, 0.1, seed=42)
|
| 291 |
+
assert len(G) == 50
|
| 292 |
+
G = nx.thresholded_random_geometric_graph(range(50), 0.2, 0.1, seed=42)
|
| 293 |
+
assert len(G) == 50
|
| 294 |
+
|
| 295 |
+
def test_distances(self):
|
| 296 |
+
"""Tests that pairs of vertices adjacent if and only if they are
|
| 297 |
+
within the prescribed radius.
|
| 298 |
+
"""
|
| 299 |
+
# Use the Euclidean metric, the default according to the
|
| 300 |
+
# documentation.
|
| 301 |
+
G = nx.thresholded_random_geometric_graph(50, 0.25, 0.1, seed=42)
|
| 302 |
+
for u, v in combinations(G, 2):
|
| 303 |
+
# Adjacent vertices must be within the given distance.
|
| 304 |
+
if v in G[u]:
|
| 305 |
+
assert math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
| 306 |
+
|
| 307 |
+
def test_p(self):
|
| 308 |
+
"""Tests for providing an alternate distance metric to the generator."""
|
| 309 |
+
|
| 310 |
+
# Use the L1 metric.
|
| 311 |
+
def dist(x, y):
|
| 312 |
+
return sum(abs(a - b) for a, b in zip(x, y))
|
| 313 |
+
|
| 314 |
+
G = nx.thresholded_random_geometric_graph(50, 0.25, 0.1, p=1, seed=42)
|
| 315 |
+
for u, v in combinations(G, 2):
|
| 316 |
+
# Adjacent vertices must be within the given distance.
|
| 317 |
+
if v in G[u]:
|
| 318 |
+
assert dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
| 319 |
+
|
| 320 |
+
def test_node_names(self):
|
| 321 |
+
"""Tests using values other than sequential numbers as node IDs."""
|
| 322 |
+
import string
|
| 323 |
+
|
| 324 |
+
nodes = list(string.ascii_lowercase)
|
| 325 |
+
G = nx.thresholded_random_geometric_graph(nodes, 0.25, 0.1, seed=42)
|
| 326 |
+
assert len(G) == len(nodes)
|
| 327 |
+
|
| 328 |
+
for u, v in combinations(G, 2):
|
| 329 |
+
# Adjacent vertices must be within the given distance.
|
| 330 |
+
if v in G[u]:
|
| 331 |
+
assert math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
| 332 |
+
|
| 333 |
+
def test_theta(self):
|
| 334 |
+
"""Tests that pairs of vertices adjacent if and only if their sum
|
| 335 |
+
weights exceeds the threshold parameter theta.
|
| 336 |
+
"""
|
| 337 |
+
G = nx.thresholded_random_geometric_graph(50, 0.25, 0.1, seed=42)
|
| 338 |
+
|
| 339 |
+
for u, v in combinations(G, 2):
|
| 340 |
+
# Adjacent vertices must be within the given distance.
|
| 341 |
+
if v in G[u]:
|
| 342 |
+
assert (G.nodes[u]["weight"] + G.nodes[v]["weight"]) >= 0.1
|
| 343 |
+
|
| 344 |
+
def test_pos_name(self):
|
| 345 |
+
trgg = nx.thresholded_random_geometric_graph
|
| 346 |
+
G = trgg(50, 0.25, 0.1, seed=42, pos_name="p", weight_name="wt")
|
| 347 |
+
assert all(len(d["p"]) == 2 for n, d in G.nodes.items())
|
| 348 |
+
assert all(d["wt"] > 0 for n, d in G.nodes.items())
|
| 349 |
+
|
| 350 |
+
|
| 351 |
+
def test_geometric_edges_pos_attribute():
|
| 352 |
+
G = nx.Graph()
|
| 353 |
+
G.add_nodes_from(
|
| 354 |
+
[
|
| 355 |
+
(0, {"position": (0, 0)}),
|
| 356 |
+
(1, {"position": (0, 1)}),
|
| 357 |
+
(2, {"position": (1, 0)}),
|
| 358 |
+
]
|
| 359 |
+
)
|
| 360 |
+
expected_edges = [(0, 1), (0, 2)]
|
| 361 |
+
assert expected_edges == nx.geometric_edges(G, radius=1, pos_name="position")
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
def test_geometric_edges_raises_no_pos():
|
| 365 |
+
G = nx.path_graph(3)
|
| 366 |
+
msg = "all nodes. must have a '"
|
| 367 |
+
with pytest.raises(nx.NetworkXError, match=msg):
|
| 368 |
+
nx.geometric_edges(G, radius=1)
|
| 369 |
+
|
| 370 |
+
|
| 371 |
+
def test_number_of_nodes_S1():
|
| 372 |
+
G = nx.geometric_soft_configuration_graph(
|
| 373 |
+
beta=1.5, n=100, gamma=2.7, mean_degree=10, seed=42
|
| 374 |
+
)
|
| 375 |
+
assert len(G) == 100
|
| 376 |
+
|
| 377 |
+
|
| 378 |
+
def test_set_attributes_S1():
|
| 379 |
+
G = nx.geometric_soft_configuration_graph(
|
| 380 |
+
beta=1.5, n=100, gamma=2.7, mean_degree=10, seed=42
|
| 381 |
+
)
|
| 382 |
+
kappas = nx.get_node_attributes(G, "kappa")
|
| 383 |
+
assert len(kappas) == 100
|
| 384 |
+
thetas = nx.get_node_attributes(G, "theta")
|
| 385 |
+
assert len(thetas) == 100
|
| 386 |
+
radii = nx.get_node_attributes(G, "radius")
|
| 387 |
+
assert len(radii) == 100
|
| 388 |
+
|
| 389 |
+
|
| 390 |
+
def test_mean_kappas_mean_degree_S1():
|
| 391 |
+
G = nx.geometric_soft_configuration_graph(
|
| 392 |
+
beta=2.5, n=50, gamma=2.7, mean_degree=10, seed=8023
|
| 393 |
+
)
|
| 394 |
+
|
| 395 |
+
kappas = nx.get_node_attributes(G, "kappa")
|
| 396 |
+
mean_kappas = sum(kappas.values()) / len(kappas)
|
| 397 |
+
assert math.fabs(mean_kappas - 10) < 0.5
|
| 398 |
+
|
| 399 |
+
degrees = dict(G.degree())
|
| 400 |
+
mean_degree = sum(degrees.values()) / len(degrees)
|
| 401 |
+
assert math.fabs(mean_degree - 10) < 1
|
| 402 |
+
|
| 403 |
+
|
| 404 |
+
def test_dict_kappas_S1():
|
| 405 |
+
kappas = {i: 10 for i in range(1000)}
|
| 406 |
+
G = nx.geometric_soft_configuration_graph(beta=1, kappas=kappas)
|
| 407 |
+
assert len(G) == 1000
|
| 408 |
+
kappas = nx.get_node_attributes(G, "kappa")
|
| 409 |
+
assert all(kappa == 10 for kappa in kappas.values())
|
| 410 |
+
|
| 411 |
+
|
| 412 |
+
def test_beta_clustering_S1():
|
| 413 |
+
G1 = nx.geometric_soft_configuration_graph(
|
| 414 |
+
beta=1.5, n=100, gamma=3.5, mean_degree=10, seed=42
|
| 415 |
+
)
|
| 416 |
+
G2 = nx.geometric_soft_configuration_graph(
|
| 417 |
+
beta=3.0, n=100, gamma=3.5, mean_degree=10, seed=42
|
| 418 |
+
)
|
| 419 |
+
assert nx.average_clustering(G1) < nx.average_clustering(G2)
|
| 420 |
+
|
| 421 |
+
|
| 422 |
+
def test_wrong_parameters_S1():
|
| 423 |
+
with pytest.raises(
|
| 424 |
+
nx.NetworkXError,
|
| 425 |
+
match="Please provide either kappas, or all 3 of: n, gamma and mean_degree.",
|
| 426 |
+
):
|
| 427 |
+
G = nx.geometric_soft_configuration_graph(
|
| 428 |
+
beta=1.5, gamma=3.5, mean_degree=10, seed=42
|
| 429 |
+
)
|
| 430 |
+
|
| 431 |
+
with pytest.raises(
|
| 432 |
+
nx.NetworkXError,
|
| 433 |
+
match="When kappas is input, n, gamma and mean_degree must not be.",
|
| 434 |
+
):
|
| 435 |
+
kappas = {i: 10 for i in range(1000)}
|
| 436 |
+
G = nx.geometric_soft_configuration_graph(
|
| 437 |
+
beta=1.5, kappas=kappas, gamma=2.3, seed=42
|
| 438 |
+
)
|
| 439 |
+
|
| 440 |
+
with pytest.raises(
|
| 441 |
+
nx.NetworkXError,
|
| 442 |
+
match="Please provide either kappas, or all 3 of: n, gamma and mean_degree.",
|
| 443 |
+
):
|
| 444 |
+
G = nx.geometric_soft_configuration_graph(beta=1.5, seed=42)
|
| 445 |
+
|
| 446 |
+
|
| 447 |
+
def test_negative_beta_S1():
|
| 448 |
+
with pytest.raises(
|
| 449 |
+
nx.NetworkXError, match="The parameter beta cannot be smaller or equal to 0."
|
| 450 |
+
):
|
| 451 |
+
G = nx.geometric_soft_configuration_graph(
|
| 452 |
+
beta=-1, n=100, gamma=2.3, mean_degree=10, seed=42
|
| 453 |
+
)
|
| 454 |
+
|
| 455 |
+
|
| 456 |
+
def test_non_zero_clustering_beta_lower_one_S1():
|
| 457 |
+
G = nx.geometric_soft_configuration_graph(
|
| 458 |
+
beta=0.5, n=100, gamma=3.5, mean_degree=10, seed=42
|
| 459 |
+
)
|
| 460 |
+
assert nx.average_clustering(G) > 0
|
| 461 |
+
|
| 462 |
+
|
| 463 |
+
def test_mean_degree_influence_on_connectivity_S1():
|
| 464 |
+
low_mean_degree = 2
|
| 465 |
+
high_mean_degree = 20
|
| 466 |
+
G_low = nx.geometric_soft_configuration_graph(
|
| 467 |
+
beta=1.2, n=100, gamma=2.7, mean_degree=low_mean_degree, seed=42
|
| 468 |
+
)
|
| 469 |
+
G_high = nx.geometric_soft_configuration_graph(
|
| 470 |
+
beta=1.2, n=100, gamma=2.7, mean_degree=high_mean_degree, seed=42
|
| 471 |
+
)
|
| 472 |
+
assert nx.number_connected_components(G_low) > nx.number_connected_components(
|
| 473 |
+
G_high
|
| 474 |
+
)
|
| 475 |
+
|
| 476 |
+
|
| 477 |
+
def test_compare_mean_kappas_different_gammas_S1():
|
| 478 |
+
G1 = nx.geometric_soft_configuration_graph(
|
| 479 |
+
beta=1.5, n=20, gamma=2.7, mean_degree=5, seed=42
|
| 480 |
+
)
|
| 481 |
+
G2 = nx.geometric_soft_configuration_graph(
|
| 482 |
+
beta=1.5, n=20, gamma=3.5, mean_degree=5, seed=42
|
| 483 |
+
)
|
| 484 |
+
kappas1 = nx.get_node_attributes(G1, "kappa")
|
| 485 |
+
mean_kappas1 = sum(kappas1.values()) / len(kappas1)
|
| 486 |
+
kappas2 = nx.get_node_attributes(G2, "kappa")
|
| 487 |
+
mean_kappas2 = sum(kappas2.values()) / len(kappas2)
|
| 488 |
+
assert math.fabs(mean_kappas1 - mean_kappas2) < 1
|