Source code for networkx.generators.stochastic
import networkx as nx
# Copyright (C) 2010 by
# Aric Hagberg <firstname.lastname@example.org>
# Dan Schult <email@example.com>
# Pieter Swart <firstname.lastname@example.org>
# All rights reserved.
# BSD license.
__author__ = "Aric Hagberg <email@example.com>"
__all__ = ['stochastic_graph']
[docs]def stochastic_graph(G, copy=True, weight='weight'):
"""Return a right-stochastic representation of G.
A right-stochastic graph is a weighted graph in which all of
the node (out) neighbors edge weights sum to 1.
G : graph
A NetworkX graph, must have valid edge weights
copy : boolean, optional
If True make a copy of the graph, otherwise modify original graph
weight : key (optional)
Edge data key used for weight. If None all weights are set to 1.
if type(G) == nx.MultiGraph or type(G) == nx.MultiDiGraph:
raise Exception("stochastic_graph not implemented for multigraphs")
if not G.is_directed():
raise Exception("stochastic_graph not defined for undirected graphs")
W=G # reference original graph, no copy
for (u,v,d) in W.edges(data=True):