Assignment (2016 EE 384)
Assignment (2016 EE 384)
Submitted to
Course Code and Name Computer Networking
Assignment Number 1
Date 1-july-2020
Reg. # 2016-EE-384
Marks 1
Computer Network Assignment
Assignment # 01
Aim: the task is to understand the Dijkstra’s shortest path algorithm and bellman–ford algorithm
implementation
Dijkstra’s algorithm finds a shortest path tree from a single source node, by building a set of nodes
that have minimum distance from the source.
distdist, an array of distances from the source node ss to each node in the graph,
initialized the following way: distdist(ss) = 0; and for all other nodes vv, distdist(vv)
= \infty∞. This is done at the beginning because as the algorithm proceeds, the distdist from
the source to each node vv in the graph will be recalculated and finalized when the shortest
distance to vv is found
2
Computer Network Assignment
QQ, a queue of all nodes in the graph. At the end of the algorithm's progress, QQ will be
empty.
SS, an empty set, to indicate which nodes the algorithm has visited. At the end of the
algorithm's run, SS will contain all the nodes of the graph.
The algorithm proceeds as follows:
1. While QQ is not empty, pop the node vv, that is not already in SS, from QQ with the
smallest distdist (vv). In the first run, source node ss will be chosen because distdist(ss) was
initialized to 0. In the next run, the next node with the smallest distdist value is chosen.
2. Add node vv to SS, to indicate that vv has been visited
3. Update distdist values of adjacent nodes of the current node vv as follows: for each new
adjacent node uu,
Code
class Graph:
3
Computer Network Assignment
#Base Case : If j is source
if parent[j] == -1 :
print j,
return
self.printPath(parent , parent[j])
print j,
# A utility function to print
# the constructed distance
# array
def printSolution(self, dist, parent):
src = 0
print("Vertex \t\tDistance from Source\tPath")
for i in range(1, len(dist)):
print("\n%d --> %d \t\t%d \t\t\t\t\t" % (src, i, dist[i])),
self.printPath(parent,i)
'''Function that implements Dijkstra's single source shortest path algorithm for a graph represented using
adjacency matrix representation'''
def dijkstra(self, graph, src):
row = len(graph)
col = len(graph[0])
g= Graph ()
graph = [[0, 4, 0, 0, 0, 0, 0, 8, 0],
[4, 0, 8, 0, 0, 0, 0, 11, 0],
[0, 8, 0, 7, 0, 4, 0, 0, 2],
[0, 0, 7, 0, 9, 14, 0, 0, 0],
[0, 0, 0, 9, 0, 10, 0, 0, 0],
[0, 0, 4, 14, 10, 0, 2, 0, 0],
[0, 0, 0, 0, 0, 2, 0, 1, 6],
[8, 11, 0, 0, 0, 0, 1, 0, 7],
[0, 0, 2, 0, 0, 0, 6, 7, 0]
]
# Print the solution
g.dijkstra(graph,0)
Result
5
Computer Network Assignment
Bellman–Ford Algorithm implementation
Algorithm
1. List down all the edges of the graph. This can be done easily if the graph is represented by
an adjacency list.
2. Calculate the number of iterations with “V - 1”. The number of iterations will be equal to
the number of vertices because the shortest distance to an edge can be adjusted V - 1 times
at maximum.
3. Start with an arbitrary vertex and assign it the minimum distance of zero. All other nodes
should be assigned infinity since we are exaggerating the actual distances.
4. In each iteration, update the distance for each edge if the new distance is smaller than the
one assigned before. The distance to each node will be the cumulative distance from the
starting node to this particular node.
5. We need to consider all the iterations to make sure that all possible paths are considered. If
we do this, we will end up with the shortest distance.
Implementation in python
Code
class Graph:
def __init__(self, vertices):
self.V = vertices # Total number of vertices in the graph
self.graph = [] # Array of edges
# Add edges
def add_edge(self, s, d, w):
6
Computer Network Assignment
self.graph.append([s, d, w])
g.bellman_ford(0)
7
Computer Network Assignment
Result