-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPAdict.py
64 lines (52 loc) · 1.38 KB
/
PAdict.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
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 22 13:51:15 2016
@author: eva
"""
import random
import numpy
graph={}
n=input('Enter the desired number of nodes in the seed graph: ')
m=input('Enter the desired parameter m: ')
N=input('Enter the desired total number of nodes: ')-n
def SeedGraph(x):
prob=m*1.0/n
graph[0]=[]
for i in range(1,x):
graph[i]=[]
for j in range(i):
coin=numpy.random.choice(numpy.arange(0, 2), p=[1-prob,prob])
if coin==1:
graph[j].append(i)
graph[i].append(j)
def AddNode():
global n
global graph
graph[n]=[]
n=n+1
def AddLinks():
global graph
PD=CurrentProbability()
for i in range(m):
A=numpy.random.choice(numpy.arange(0, n), p=PD)
while A in graph[n-1]:
A=numpy.random.choice(numpy.arange(0, n), p=PD)
graph[A].append(n-1)
graph[n-1].append(A)
def CurrentProbability():
Degrees=[]
for i in graph:
Degrees.append(len(graph[i]))
SumOfDegrees=0
for i in Degrees:
SumOfDegrees=SumOfDegrees+i
ProbabilityDistribution=[]
for i in Degrees:
A=i*1.0/SumOfDegrees
ProbabilityDistribution.append(A)
return ProbabilityDistribution
SeedGraph(n)
for k in range(N):
AddNode()
AddLinks()
print graph