-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphutils.py
127 lines (95 loc) · 2.92 KB
/
graphutils.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
127
# Public imports
from random import uniform, randrange, choice
# Private imports
from supergraphs import *
from basicutils import *
def makeCompleteGraph(size = 1, animate = False):
assert size > 0
g = Graph(size)
if animate: g.dumpToFile("{}.dot".format(nextFrame()))
for f in range(size):
for s in range(f + 1, size):
g.connect(f, s)
if animate: g.dumpToFile("{}.dot".format(nextFrame()))
return g
def makeRandomTreeGraph(size = 1, animate = False):
assert size > 0
g = Graph(size)
if animate: g.dumpToFile("{}.dot".format(nextFrame()))
for f in range(1, size):
s = randrange(0, f)
g.connect(f, s)
if animate: g.dumpToFile("{}.dot".format(nextFrame()))
return g
def makeERRandomGraph(size = 5, p = 0.5, animate = False):
assert 0 <= p <= 1
g = Graph(size)
if animate: g.dumpToFile("{}.dot".format(nextFrame()))
for f in range(size):
for s in range(f + 1, size):
if uniform(0, 1) <= p:
g.connect(f, s)
if animate: g.dumpToFile("{}.dot".format(nextFrame()))
return g
def makePAMRandomGraph(initialGraph = Graph(1), nodesToAdd = 5, m = 1, animate = False):
assert initialGraph.getNodeAmount() >= m > 0
assert nodesToAdd > 0
# assert initialGraph.isConnected() # Why did I add this?
g = initialGraph.deepClone()
if animate: g.dumpToFile("{}.dot".format(nextFrame()))
choices = []
for v in range(g.getNodeAmount()):
choices += [v] * g.getNeighbourCount(v)
# TODO
if g.getNodeAmount() == 1:
choices.append(0)
for i in range(nodesToAdd):
f = g.addNode();
currentChoices = choices[:]
for j in range(m):
s = choice(currentChoices)
currentChoices = [x for x in currentChoices if x != s]
g.connect(f, s)
if animate: g.dumpToFile("{}.dot".format(nextFrame()))
choices.append(s)
choices.append(f)
return g
# TODO
def makeERRandomSquareGrid(w, h, p = 0.5):
assert w <= h <= 2
assert 0 <= p <= 1
edges = []
g = Graph(w * h)
n = w * h
# Almost all horizontal edges
for y in range(h - 1):
for x in range(w - 1):
f = y * w + x
s = y * w + x + 1
edges.append(f, s)
# Almost all vertical edges
for y in range(h - 1):
for x in range(w):
f = y * w + x
s = (y + 1) * w + x
edges.append(f, s)
# TODO
def makeERRandomHexGrid(w, h, p = 0.5):
pass
def makeCompletePAMGraph(completeSize = 3, nodesToAdd = 5, m = 1, animate = False):
assert completeSize > 0
assert nodesToAdd > 0
assert m > 0
return makePAMRandomGraph(makeCompleteGraph(completeSize, animate = animate), nodesToAdd, m, animate = animate)
def makeERPAMGraph(ERsize = 5, p = 0.5, nodesToAdd = 5, m = 1):
assert ERsize > 0
assert 0 <= p <= 1
assert nodesToAdd > 0
assert 0 < m <= ERsize
return makePAMRandomGraph(makeERRandomGraph(ERsize, p), nodesToAdd, m)
# What do we want to know:
# Portion unaffected, portion affected
# Can be recorded by counting infected at every t?
# Duration of presence of disease
# Largest distance from patient 0
# When was the peak in infections