Python networkx.stochastic_graph() Examples

The following are 15 code examples of networkx.stochastic_graph(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module networkx , or try the search function .
Example #1
Source File: test_stochastic.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_stochastic():
    G=nx.DiGraph()
    G.add_edge(0,1)
    G.add_edge(0,2)
    S=nx.stochastic_graph(G)
    assert_true(nx.is_isomorphic(G,S))
    assert_equal(sorted(S.edges(data=True)),
                 [(0, 1, {'weight': 0.5}), 
                  (0, 2, {'weight': 0.5})])
    S=nx.stochastic_graph(G,copy=True)
    assert_equal(sorted(S.edges(data=True)),
                 [(0, 1, {'weight': 0.5}), 
                  (0, 2, {'weight': 0.5})]) 
Example #2
Source File: test_stochastic.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_stochastic_ints():
    G=nx.DiGraph()
    G.add_edge(0,1,weight=1)
    G.add_edge(0,2,weight=1)
    S=nx.stochastic_graph(G)
    assert_equal(sorted(S.edges(data=True)),
                 [(0, 1, {'weight': 0.5}), 
                  (0, 2, {'weight': 0.5})]) 
Example #3
Source File: test_stochastic.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_stochastic_graph_input():
    S = nx.stochastic_graph(nx.Graph()) 
Example #4
Source File: test_stochastic.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_stochastic_multigraph_input():
    S = nx.stochastic_graph(nx.MultiGraph()) 
Example #5
Source File: test_stochastic.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_default_weights(self):
        G = nx.DiGraph()
        G.add_edge(0, 1)
        G.add_edge(0, 2)
        S = nx.stochastic_graph(G)
        assert_true(nx.is_isomorphic(G, S))
        assert_equal(sorted(S.edges(data=True)),
                     [(0, 1, {'weight': 0.5}), (0, 2, {'weight': 0.5})]) 
Example #6
Source File: test_stochastic.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_in_place(self):
        """Tests for an in-place reweighting of the edges of the graph.

        """
        G = nx.DiGraph()
        G.add_edge(0, 1, weight=1)
        G.add_edge(0, 2, weight=1)
        nx.stochastic_graph(G, copy=False)
        assert_equal(sorted(G.edges(data=True)),
                     [(0, 1, {'weight': 0.5}), (0, 2, {'weight': 0.5})]) 
Example #7
Source File: test_stochastic.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_arbitrary_weights(self):
        G = nx.DiGraph()
        G.add_edge(0, 1, weight=1)
        G.add_edge(0, 2, weight=1)
        S = nx.stochastic_graph(G)
        assert_equal(sorted(S.edges(data=True)),
                     [(0, 1, {'weight': 0.5}), (0, 2, {'weight': 0.5})]) 
Example #8
Source File: test_stochastic.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_multidigraph(self):
        G = nx.MultiDiGraph()
        G.add_edges_from([(0, 1), (0, 1), (0, 2), (0, 2)])
        S = nx.stochastic_graph(G)
        d = dict(weight=0.25)
        assert_equal(sorted(S.edges(data=True)),
                     [(0, 1, d), (0, 1, d), (0, 2, d), (0, 2, d)]) 
Example #9
Source File: test_stochastic.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_graph_disallowed(self):
        nx.stochastic_graph(nx.Graph()) 
Example #10
Source File: test_stochastic.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_default_weights(self):
        G = nx.DiGraph()
        G.add_edge(0, 1)
        G.add_edge(0, 2)
        S = nx.stochastic_graph(G)
        assert_true(nx.is_isomorphic(G, S))
        assert_equal(sorted(S.edges(data=True)),
                     [(0, 1, {'weight': 0.5}), (0, 2, {'weight': 0.5})]) 
Example #11
Source File: test_stochastic.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_in_place(self):
        """Tests for an in-place reweighting of the edges of the graph.

        """
        G = nx.DiGraph()
        G.add_edge(0, 1, weight=1)
        G.add_edge(0, 2, weight=1)
        nx.stochastic_graph(G, copy=False)
        assert_equal(sorted(G.edges(data=True)),
                     [(0, 1, {'weight': 0.5}), (0, 2, {'weight': 0.5})]) 
Example #12
Source File: test_stochastic.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_arbitrary_weights(self):
        G = nx.DiGraph()
        G.add_edge(0, 1, weight=1)
        G.add_edge(0, 2, weight=1)
        S = nx.stochastic_graph(G)
        assert_equal(sorted(S.edges(data=True)),
                     [(0, 1, {'weight': 0.5}), (0, 2, {'weight': 0.5})]) 
Example #13
Source File: test_stochastic.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_multidigraph(self):
        G = nx.MultiDiGraph()
        G.add_edges_from([(0, 1), (0, 1), (0, 2), (0, 2)])
        S = nx.stochastic_graph(G)
        d = dict(weight=0.25)
        assert_equal(sorted(S.edges(data=True)),
                     [(0, 1, d), (0, 1, d), (0, 2, d), (0, 2, d)]) 
Example #14
Source File: test_stochastic.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_graph_disallowed(self):
        nx.stochastic_graph(nx.Graph()) 
Example #15
Source File: pagerank_iterative.py    From complex_network with GNU General Public License v2.0 4 votes vote down vote up
def pagerank_iterative(G, d=0.85, max_iter=100, tol=1.0e-6, weight='weight'):
	"""
	PageRank calculation iteratively
	"""

	# Step 1: Initiate PageRank
	N = G.number_of_nodes()						# N = 11
	node_and_pr = dict.fromkeys(G, 1.0 / N)

	# Step 2: Create a copy in (right) stochastic form
	stochastic_graph = nx.stochastic_graph(G, weight=weight)	# M = 1/L(pj)

	# Step 3: Power iteration: make up to max_iter iterations
	dangling_value = (1-d)/N

	for _ in range(max_iter):		# for each iteration
		node_and_prev_pr = node_and_pr
		node_and_pr = dict.fromkeys(node_and_prev_pr.keys(), 0)

		for node in node_and_pr:	# for each node
			for out_node in stochastic_graph[node]:		# node --> out_node
				node_and_pr[out_node] += d * node_and_prev_pr[node] * stochastic_graph[node][out_node][weight] 	# PR(p_i) = d * PR(p_j)}/L(p_j)
		
			node_and_pr[node] += dangling_value

		# Plot graph with one iteration
		'''
		out_file = 'wikipedia_pagerank_example_iteration_1.pdf'
		node_size = [pr*30000 for node, pr in node_and_pr.items()]
		node_and_labels = {node : node+'\n'+str(round(pr, 3))
							for node, pr in node_and_pr.items()}

		plotnxgraph.plot_graph(G, out_file=out_file, node_size=node_size, node_and_labels=node_and_labels)
		return
		'''

		# check convergence, l1 norm
		err = sum([abs(node_and_pr[node] - node_and_prev_pr[node]) for node in node_and_pr])
		if err < N*tol:
			return node_and_pr

	raise NetworkXError('pagerank: power iteration failed to converge in {} iterations.'.format(max_iter))