You are on page 1of 6

National Institute of Technology, Uttarakhand

Computer Networks

Lab File

Course Code – CSP255


Department of Computer Science & Engineering

Submitted To : Submitted By:


Dr. Surendra Singh Minal Kapse
Assistant Professor BT20ECE021
(CSE department)
11. WAP for Open Shortest Path First (OSPF)

class Router:
def __init__(self, id):
self.id = id
self.adjacent_routers = {}
self.routing_table = {}

def add_neighbor(self, neighbor, distance):


self.adjacent_routers[neighbor] = distance

def update_routing_table(self, source_router, distance_vector):


for router in distance_vector:
if router == self.id:
self.routing_table[router] = 0
elif router in self.adjacent_routers:
self.routing_table[router] = distance_vector[router] +
self.adjacent_routers[router]
elif router in source_router.adjacent_routers:
self.routing_table[router] = distance_vector[router] +
source_router.adjacent_routers[router]

def get_shortest_path(self, destination):


return self.routing_table[destination]

class Network:
def __init__(self):
self.routers = {}

def add_router(self, router_id):


self.routers[router_id] = Router(router_id)

def add_connection(self, router1_id, router2_id, distance):


self.routers[router1_id].add_neighbor(router2_id, distance)
self.routers[router2_id].add_neighbor(router1_id, distance)

def update_routing_tables(self):
for router_id in self.routers:
router = self.routers[router_id]
distance_vector = {router_id: 0}
for neighbor in router.adjacent_routers:
distance_vector[neighbor] = router.adjacent_routers[neighbor]
neighbor_router = self.routers[neighbor]
for n in neighbor_router.routing_table:
if n not in distance_vector:
distance_vector[n] = neighbor_router.routing_table[n]
+ router.adjacent_routers[neighbor]
router.update_routing_table(neighbor_router, distance_vector)

def get_shortest_path(self, source, destination):


return self.routers[source].get_shortest_path(destination)

network=Network()
network.add_router(1)

1
network.add_router(2)
network.add_router(3)
network.add_connection(1,2,1)
network.add_connection(2,3,2)
network.add_connection(1,3,3)

network.update_routing_tables()
shortest_path=network.get_shortest_path(3,2)/2
#################################
print(shortest_path)

Output

2
12. WAP for Shortest Path Routing
# shortest path routing algorithm for
# optical networks

# importing random module


import random

# Number of nodes
NODES = 7

# very small invalid


# when no link exists
INVALID = 0.001

distance_links = [[INVALID for i in range(NODES)]


for j in range(NODES)]
# distance of each link
distance_links[0][1] = 7
distance_links[1][0] = 7
distance_links[1][2] = 8
distance_links[2][1] = 8
distance_links[0][2] = 9
distance_links[2][0] = 9
distance_links[3][0] = 9
distance_links[0][3] = 9
distance_links[4][3] = 4
distance_links[3][4] = 4
distance_links[5][4] = 6
distance_links[4][5] = 6
distance_links[5][2] = 4
distance_links[2][5] = 4
distance_links[4][6] = 8
distance_links[6][4] = 8
distance_links[0][6] = 5
distance_links[6][0] = 5

# Finds next node from current node


def next_node(s):
nxt = []

for i in range(NODES):
if(distance_links[s][i] != INVALID):
nxt.append(i)
return nxt

# Find simple paths for each


def find_simple_paths(start, end):
visited = set()
visited.add(start)

nodestack = list()
indexstack = list()
current = start
i = 0

3
while True:

# get a list of the neighbors


# of the current node
neighbors = next_node(current)

# Find the next unvisited neighbor


# of this node, if any
while i < len(neighbors) and neighbors[i] in visited:
i += 1

if i >= len(neighbors):
visited.remove(current)

if len(nodestack) < 1:
break

current = nodestack.pop()
i = indexstack.pop()

elif neighbors[i] == end:


yield nodestack + [current, end]
i += 1

else:
nodestack.append(current)
indexstack.append(i + 1)
visited.add(neighbors[i])
current = neighbors[i]
i = 0

# Find the shortest path


def solution(sour, dest):

block = 0
l = []
for path in find_simple_paths(sour, dest):
l.append(path)

k = 0
for i in range(len(l)):
su = 0
for j in range(1, len(l[i])):
su += (distance_links[l[i][j-1]]
[l[i][j]])
k += su

# print k
dist_prob = []
probability = []

for i in range(len(l)):
s, su = 0, 0

for j in range(1, len(l[i])):

su += (distance_links[l[i][j-1]]
[l[i][j]])

4
dist_prob.append(su/(1.0 * k))

for m in range(len(dist_prob)):
z = (dist_prob[m])
probability.append(z)

for i in range(len(probability)):
if(probability[i] == min(probability)):

z = l[i]
print("Shortest Path is", end = " ")
print(z)

# Driver Code
if __name__ == '__main__' :
source, dest = 1,5 ###########################

# Calling the solution function


solution(source, dest)

Output

You might also like