-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdial.py
More file actions
237 lines (183 loc) · 7.17 KB
/
dial.py
File metadata and controls
237 lines (183 loc) · 7.17 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import os
from collections import deque, namedtuple
from random import seed
from random import random
seed(1)
# we'll use infinity as a default distance to nodes.
inf = float('inf')
Edge = namedtuple('Edge', 'start, end, cost')
pathlist=[]
router_matrix = []
matrix_set = 0
nodes = []
distances = {}
unvisited = {}
previous = {}
visited = {}
interface = {}
path = []
start = 0
end = 0
def make_edge(start, end, cost=1):
return Edge(start, end, cost)
def print_choices():
os.system("figlet Link State Routing Simulator")
print("\n######################################################\n")
print("(1) Input Network Topology File")
print("(2) Build a Connection Table")
print("(3) Shortest Path to Destination Router")
print("(4) Exit")
print("\n######################################################\n")
pass
# Function to check if entered command is valid or not - i.e. :
# 1: Should be a digit.
# 2: Should be from the range of given choices.
def check_choices(command):
if not command.isdigit():
print("Please enter a number as command from given choices..")
return -1
else:
command = int(command)
if command > 4 or command < 1 :
print("Please enter a valid command from given choices..")
return -1
else:
return command
# Function to process the given input file.
def process_file(fname):
print("process_file")
global matrix_set
global router_matrix
matrix_set = 0
router_matrix = []
with open(fname) as f:
path=router_matrix=[list(map(str,x.split()))for x in f] # Data from input file is stored in a two dimensional list(array).
for k in path:
(s,d,w)=k
pathlist.append((s,d,int(w)))
print("\nReview original topology matrix:\n")
print(pathlist)
# Function to store the distances in dictionary format.
class Graph:
def __init__(self,pathlist):
# let's check that the data is right
wrong_edges = [i for i in pathlist if len(i) not in [2, 3]]
if wrong_edges:
raise ValueError('Wrong edges data: {}'.format(wrong_edges))
self.pathlist = [make_edge(*path) for path in pathlist]
@property
def vertices(self):
return set(
sum(
([edge.start, edge.end] for edge in self.pathlist), []
)
)
def get_node_pairs(self, n1, n2, both_ends=True):
if both_ends:
node_pairs = [[n1, n2], [n2, n1]]
else:
node_pairs = [[n1, n2]]
return node_pairs
def remove_edge(self, n1, n2, both_ends=True):
node_pairs = self.get_node_pairs(n1, n2, both_ends)
edges = self.edges[:]
for edge in edges:
if [edge.start, edge.end] in node_pairs:
self.edges.remove(edge)
def add_edge(self, n1, n2, cost=1, both_ends=True):
node_pairs = self.get_node_pairs(n1, n2, both_ends)
for edge in self.edges:
if [edge.start, edge.end] in node_pairs:
return ValueError('Edge {} {} already exists'.format(n1, n2))
self.edges.append(Edge(start=n1, end=n2, cost=cost))
if both_ends:
self.edges.append(Edge(start=n2, end=n1, cost=cost))
@property
def neighbours(self):
neighbours = {vertex: set() for vertex in self.vertices}
for path in self.pathlist:
neighbours[path.start].add((path.end, path.cost))
return neighbours
def dijkstra(self, source, dest):
assert source in self.vertices, 'Such source node doesn\'t exist'
distances = {vertex: inf for vertex in self.vertices}
previous_vertices = {
vertex: None for vertex in self.vertices
}
distances[source] = 0
vertices = self.vertices.copy()
total=0
while vertices:
current_vertex = min(
vertices, key=lambda vertex: distances[vertex])
vertices.remove(current_vertex)
if distances[current_vertex] == inf:
break
for neighbour, cost in self.neighbours[current_vertex]:
alternative_route = distances[current_vertex] + cost + random() # random for queue time
if alternative_route < distances[neighbour]:
distances[neighbour] = alternative_route
previous_vertices[neighbour] = current_vertex
total+=alternative_route
print("Total End-End Time = ",total,"ms")
path, current_vertex = deque(), dest
while previous_vertices[current_vertex] is not None:
path.appendleft(current_vertex)
current_vertex = previous_vertices[current_vertex]
if path:
path.appendleft(current_vertex)
return path
if __name__ == "__main__":
print_choices()
command = 0
# Run till user wants to exit.
while command !=4 :
command = check_choices(input("Choice: "))
# Accept the topology file.
if command == 1:
if matrix_set == 1:
answer = input("\nThe network topology is already uploaded. Do you want to overwrite? (Y/N) :")
if matrix_set == 0 or answer == 'Y' or answer == 'y':
filename = input("\nInput original network topology matrix data file[ NxN distance matrix. (value : -1 for no link, 0 for self loop) : ")
if os.path.isfile(filename):
process_file(filename)
start = 0
end = 0
graph = Graph(pathlist)
matrix_set=1
else:
print("\nThe file does not exist. Please try again..")
# Accept the source router and display the connection table.
elif command == 2:
if matrix_set == 1 :
start = input("\nSelect the current hop : ")
print("\nNext-Hop Weight")
for key in pathlist:
# print(key,"\t\t", interface[key])
if(key[0]==start):
print(key[1],key[2])
else:
start = 0
print("\nPlease enter a valid source router.")
else:
print("\nNo network topology matrix exist. Please upload the data file first.. ")
# Accept the destination router and display the shortest path and cost.
elif command == 3:
if matrix_set == 1 :
start = input("\nSelect a source router : ")
end = input("\nSelect a destination router : ")
print("start:- ",start)
print("end:- ",end)
path=graph.dijkstra(start,end)
# print(path)
route=""
c=0
print(len(path))
for hop in path:
c+=1
if(c<len(path)):
route+=hop+"->"
else:
route+=hop
print(route)
# print(graph.dijkstra("a", "e"))