-
Notifications
You must be signed in to change notification settings - Fork 1
/
flooding_every.py
153 lines (108 loc) · 3.82 KB
/
flooding_every.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import os
import numpy as np
from queue import Queue
import math
from gen import *
import networkx as nx
import time
import numpy as np
import math
import argparse
import sys
import random
import numpy
if __name__== "__main__":
d = int(sys.argv[1])
N = int(sys.argv[2])
ds = int(sys.argv[3])
de = int(sys.argv[4])
newNodes = int(sys.argv[5])
#print("Generating for")
#print(sys.argv)
G = generate_graph(ds, de, N)
# add random weights
for i, j in G.edges():
rand_weight = random.randint(1,10)
G.remove_edge(i, j)
#G.remove_edge(j, i)
G.add_edge(i, j, weight=rand_weight)
G.add_edge(j, i, weight=rand_weight)
for i in range(N, newNodes+N):
# add a new node
G.add_node(i)
# pick a random node in the network and add and undirected edge to the new node
random_node = random.randint(0, N)
G.add_edge(random_node, i)
G.add_edge(i, random_node)
##
##
## SCENARIO 2b: Random radius
##
##
level_mean = []
messages_list = []
pre_visited = {}
pre_level = {}
#pre-flood if depth > 1
for n in G.nodes():
if d > 1:
# each node knows other nodes up to some radius so previsit till some depth
#pre_visit_neighbor_list = [i for i in ]
#while not all(i==1 for i in pre_visited):
tmp_depth = 1
tmp_queue = [n, None]
pre_visited[n] = [0 for i in range(N+newNodes)]
pre_level[n] = [math.inf for i in range(N+newNodes)]
while tmp_depth != d:
if not tmp_queue:
break
ele = tmp_queue.pop(0)
if ele is None:
tmp_depth += 1
continue
pre_visited[n][ele] = 1
pre_level[n][ele] = tmp_depth
neigh = list(G.neighbors(ele))
tmp_queue = tmp_queue + [i for i in neigh if i not in tmp_queue and pre_visited[n][i]!= 1]
if tmp_queue[0] is None:
tmp_queue.append(None)
#import ipdb ; ipdb.set_trace()
# Flood until you find N
for n in G.nodes():
visited = [0 for i in range(N+newNodes)]
level = [math.inf for i in range(N+newNodes)]
depth = 1
queue = [n, None]
messages = 0
#import ipdb ; ipdb.set_trace()
while not all(i==1 or i==2 for i in visited):
if not queue:
break
ele = queue.pop(0)
if ele is None:
depth += 1
continue
messages += 1
visited[ele] = 1
level[ele] = depth
# For the visited node, visit all the nodes it has pre-visited.
# you'll visit those nodes for free because it has already been
# visited before. It will not cost you messages, but it will cost
# you time if you were to look for neighbors through it
for i, j in enumerate(pre_level[ele]):
#
if j != math.inf:
visited[i] = 2
level[i] = depth + j
neigh = list(G.neighbors(ele))
# don't add duplicates and visited
queue = queue + [i for i in neigh if i not in queue and visited[i]!= 1]
if queue[0] is None:
queue.append(None)
#import ipdb ; ipdb.set_trace()
level_mean.append(numpy.mean(level))
messages_list.append(messages)
#print("Average time for node ", n," to send messages to all nodes: ", numpy.mean(level))
#print(visited, level)
#print("Average time for ", N," nodes to send messages to every nodes: ", numpy.mean(level_mean))
print(numpy.mean(level_mean), " ,", numpy.mean(messages_list))