-
Notifications
You must be signed in to change notification settings - Fork 8
/
Graph_networkx.py
126 lines (106 loc) · 3.57 KB
/
Graph_networkx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
AGraph = nx.Graph()
Nodes = range(1,5)
Edges = [(1,2), (2,3), (3,4), (4,5), (1,3), (1,5)]
AGraph.add_nodes_from(Nodes)
AGraph.add_edges_from(Edges)
AGraph.add_edge(2, 4)
AGraph.add_node(6)
sorted(nx.connected_components(AGraph))
nx.clustering(AGraph)
nx.degree_centrality(AGraph)
nx.closeness_centrality(AGraph)
nx.betweenness_centrality(AGraph)
nx.to_numpy_matrix(AGraph)
nx.to_scipy_sparse_matrix(AGraph)
nx.to_dict_of_lists(AGraph)
# nx.degree(AGraph).values()
# nx.draw(AGraph, with_labels=True)
graphA = {'A': ['B', 'C'],
'B': ['A', 'C', 'D'],
'C': ['A', 'B', 'D', 'E'],
'D': ['B', 'C', 'E', 'F'],
'E': ['C', 'D', 'F'],
'F': ['D', 'E']}
Graph = nx.Graph()
for node in graphA:
Graph.add_nodes_from(node)
for edge in graphA[node]:
Graph.add_edge(node,edge)
pos = { 'A': [0.00, 0.50], 'B': [0.25, 0.75],
'C': [0.25, 0.25], 'D': [0.75, 0.75],
'E': [0.75, 0.25], 'F': [1.00, 0.50]}
nx.draw(Graph, pos, with_labels=True)
nx.draw_networkx(Graph, pos)
plt.show()
def bfs(graph, start):
queue = [start]
queued = list()
path = list()
while queue:
print ('Queue is: %s' % queue)
vertex = queue.pop(0)
print ('Processing %s' % vertex)
for candidate in graph[vertex]:
if candidate not in queued:
queued.append(candidate)
queue.append(candidate)
path.append(vertex+'>'+candidate)
print ('Adding %s to the queue'% candidate)
return path
steps = bfs(graphA, 'A')
print ('\nBFS:', steps)
def dfs(graph, start):
stack = [start]
parents = {start: start}
path = list()
while stack:
print ('Stack is: %s' % stack)
vertex = stack.pop(-1)
print ('Processing %s' % vertex)
for candidate in graph[vertex]:
if candidate not in parents:
parents[candidate] = vertex
stack.append(candidate)
print ('Adding %s to the stack' % candidate)
path.append(parents[vertex]+'>'+vertex)
return path[1:]
steps = dfs(graphA, 'A')
print ('\nDFS:', steps)
Graph_A = nx.DiGraph()
Graph_B = nx.DiGraph()
Graph_C = nx.DiGraph()
Nodes = range(1,6)
Edges_OK = [(1,2),(1,3),(2,3),(3,1),(3,2),(3,4),(4,5),(4,6),(5,4),(5,6),(6,5),(6,1)]
Edges_dead_end = [(1,2),(1,3),(3,1),(3,2),(3,4),(4,5),(4,6),(5,4),(5,6),(6,5),(6,1)]
Edges_trap = [(1,2),(1,3),(2,3),(3,1),(3,2),(3,4),(4,5),(4,6),(5,4),(5,6),(6,5)]
Graph_A.add_nodes_from(Nodes)
Graph_A.add_edges_from(Edges_OK)
Graph_B.add_nodes_from(Nodes)
Graph_B.add_edges_from(Edges_dead_end)
Graph_C.add_nodes_from(Nodes)
Graph_C.add_edges_from(Edges_trap)
np.random.seed(2)
pos=nx.shell_layout(Graph_A)
nx.draw(Graph_A, pos, arrows=True, with_labels=True)
plt.show()
def initialize_PageRank(graph):
nodes = len(graph)
M = nx.to_numpy_matrix(graph)
outbound = np.squeeze(np.asarray(np.sum(M, axis=1)))
prob_outbound = np.array([1.0/count if count>0 else 0.0 for count in outbound])
G = np.asarray(np.multiply(M.T, prob_outbound))
p = np.ones(nodes) / float(nodes)
if np.min(np.sum(G,axis=0)) < 1.0:
print ('Warning: G is substochastic')
return G, p
G, p = initialize_PageRank(Graph_A)
print(G)
def PageRank_naive(graph, iters = 50):
G, p = initialize_PageRank(graph)
for i in range(iters):
p = np.dot(G,p)
return np.round(p,3)
print(PageRank_naive(Graph_A))