-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.py
33 lines (25 loc) · 806 Bytes
/
graph.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
import numpy as np
import scipy.stats as stats
import random
import networkx as nx
def generate_random_graph(N, p):
A_ER = np.zeros((N, N))
for i in xrange(N):
for j in xrange(N):
if i == j:
A_ER[i, j] = 1
else:
if random.random() <= p:
A_ER[i, j] = stats.uniform.rvs(loc=-1, scale=2)
return A_ER
def generate_watts_strogatz_graph(N, p):
k = int((N - 1) * p)
graph_WS = nx.watts_strogatz_graph(N, k, p)
A = np.array(nx.adjacency_matrix(graph_WS).todense(), dtype=float)
for i in xrange(N):
for j in xrange(N):
if A[i, j] == 1. and i != j:
A[i, j] = stats.uniform.rvs(loc=-1, scale=2)
for diag in xrange(N):
A[diag, diag] = 1.
return A