comparison planemo/lib/python3.7/site-packages/networkx/generators/stochastic.py @ 1:56ad4e20f292 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:32:28 -0400
parents
children
comparison
equal deleted inserted replaced
0:d30785e31577 1:56ad4e20f292
1 # Copyright (C) 2010-2013 by
2 # Aric Hagberg <hagberg@lanl.gov>
3 # Dan Schult <dschult@colgate.edu>
4 # Pieter Swart <swart@lanl.gov>
5 # All rights reserved.
6 # BSD license.
7 """Functions for generating stochastic graphs from a given weighted directed
8 graph.
9
10 """
11
12 from networkx.classes import DiGraph
13 from networkx.classes import MultiDiGraph
14 from networkx.utils import not_implemented_for
15
16 __author__ = "Aric Hagberg <aric.hagberg@gmail.com>"
17 __all__ = ['stochastic_graph']
18
19
20 @not_implemented_for('undirected')
21 def stochastic_graph(G, copy=True, weight='weight'):
22 """Returns a right-stochastic representation of directed graph `G`.
23
24 A right-stochastic graph is a weighted digraph in which for each
25 node, the sum of the weights of all the out-edges of that node is
26 1. If the graph is already weighted (for example, via a 'weight'
27 edge attribute), the reweighting takes that into account.
28
29 Parameters
30 ----------
31 G : directed graph
32 A :class:`~networkx.DiGraph` or :class:`~networkx.MultiDiGraph`.
33
34 copy : boolean, optional
35 If this is True, then this function returns a new graph with
36 the stochastic reweighting. Otherwise, the original graph is
37 modified in-place (and also returned, for convenience).
38
39 weight : edge attribute key (optional, default='weight')
40 Edge attribute key used for reading the existing weight and
41 setting the new weight. If no attribute with this key is found
42 for an edge, then the edge weight is assumed to be 1. If an edge
43 has a weight, it must be a a positive number.
44
45 """
46 if copy:
47 G = MultiDiGraph(G) if G.is_multigraph() else DiGraph(G)
48 # There is a tradeoff here: the dictionary of node degrees may
49 # require a lot of memory, whereas making a call to `G.out_degree`
50 # inside the loop may be costly in computation time.
51 degree = dict(G.out_degree(weight=weight))
52 for u, v, d in G.edges(data=True):
53 if degree[u] == 0:
54 d[weight] = 0
55 else:
56 d[weight] = d.get(weight, 1) / degree[u]
57 return G