-
Notifications
You must be signed in to change notification settings - Fork 0
/
DistanceVector
125 lines (111 loc) · 3.91 KB
/
DistanceVector
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
#!/usr/bin/env python3
import math
import sys
import copy
from Graph import Graph
from Router import Router
nodes_list = []
graph = Graph()
# Read in nodes
line = sys.stdin.readline().strip()
while line != "DISTANCEVECTOR":
nodes_list.append(line)
router = Router(line)
graph.adj_list[router._name] = {}
line = sys.stdin.readline().strip()
# Read in edges
line = sys.stdin.readline().strip()
while line != "UPDATE":
node1, node2, weight = line.split()
graph.add_edge(node1, node2, int(weight))
line = sys.stdin.readline().strip()
# Run til convergence
routers_list = []
for node in nodes_list:
router = Router(node)
router.initialize_distance_table(nodes_list)
router.update_self(graph, routers_list)
routers_list.append(router)
# Print initial distance table
for router in routers_list:
print(f"{router._name} Distance Table at t=0")
router.print_distance_table()
print()
# Run algorithm, printing at each t until convergence
t = 1
previous_distance_table = {}
for router in routers_list: #Store previous distance table
previous_distance_table[router._name] = copy.deepcopy(router.distance_table)
while True:
for router in routers_list:
router.send_updates(graph.get_neighbors(router._name), routers_list)
for router in routers_list:
router.process_received_tables()
# Check if all distance tables are the same
if all([router.distance_table == previous_distance_table[router._name] for router in routers_list]):
break
previous_distance_table = {}
for router in routers_list: #Store previous distance table
previous_distance_table[router._name] = copy.deepcopy(router.distance_table)
# Print distance table
for router in routers_list:
print(f"{router._name} Distance Table at t={t}")
router.print_distance_table()
print()
t += 1
if all([router.update_neighbors == False for router in routers_list]):
break
# Print routing table
for router in routers_list:
router.print_routing_table()
# Take in updates to graph
while True:
line = sys.stdin.readline().strip()
if line == "END":
break
node1, node2, weight = line.split()
if int(weight) == -1:
graph.remove_edge(node1, node2)
else:
graph.add_edge(node1, node2, int(weight))
# Store original distance table
previous_distance_table = {}
for router in routers_list:
previous_distance_table[router._name] = copy.deepcopy(router.distance_table)
# Process initial changes to graph
for router in routers_list:
router.process_after_update(graph, routers_list)
# If nothing changed, exit
if all([router.distance_table == previous_distance_table[router._name] for router in routers_list]):
sys.exit()
# Print distance table
for router in routers_list:
print(f"{router._name} Distance Table at t={t}")
router.print_distance_table()
print()
t+= 1
previous_distance_table = {}
for router in routers_list: #Store previous distance table
previous_distance_table[router._name] = copy.deepcopy(router.distance_table)
while True:
for router in routers_list:
router.send_updates(graph.get_neighbors(router._name), routers_list)
for router in routers_list:
router.process_received_tables()
# Check if all distance tables are the same
if all([router.distance_table == previous_distance_table[router._name] for router in routers_list]):
break
previous_distance_table = {}
for router in routers_list: #Store previous distance table
previous_distance_table[router._name] = copy.deepcopy(router.distance_table)
# Print distance table
for router in routers_list:
print(f"{router._name} Distance Table at t={t}")
router.print_distance_table()
print()
t += 1
if all([router.update_neighbors == False for router in routers_list]):
break
# Print routing table
for router in routers_list:
router.print_routing_table()