You are on page 1of 4

In this programming assignment, we have used the Dijkstra’s algorithm to find the shortest

distance from the source node to every other node. Dijkstra’s algorithm gives the shortest
path from source to every other node. We have written a python class Graph, which
contains functions like dijkstra and printSolution which implements the Dijkstra algorithm
and outputs the shortest distance for every node from the source node 0 in a list. The
function inputs the graph as an adjacency matrix and hence the read function provided also
have been changed a bit. The class Graph is as follows:

class Graph():

def __init__(self, vertices):


self.V = vertices
self.graph = [[0 for column in range(vertices)]
for row in range(vertices)]
self.dist = [int(sys.maxsize)] * int(self.V)

def printSolution(self, dist):


print ("Vertex \tDistance from Source")
for node in range(self.V):
if dist[node] < 5000:
print (node, "\t", dist[node])

# A utility function to find the vertex with


# minimum distance value, from the set of vertices
# not yet included in shortest path tree
def minDistance(self, dist, sptSet):

# Initilaize minimum distance for next node


mini = sys.maxsize
min_index = -1
# Search not nearest vertex not in the
# shortest path tree
for v in range(self.V):
if dist[v] < mini and sptSet[v] == False:
mini = dist[v]
min_index = v

return min_index

# Funtion that implements Dijkstra's single source


# shortest path algorithm for a graph represented
# using adjacency matrix representation
def dijkstra(self, src):

self.dist[src] = 0
sptSet = [False] * self.V

for cout in range(self.V):

# Pick the minimum distance vertex from


# the set of vertices not yet processed.
# u is always equal to src in first iteration
u = self.minDistance(self.dist, sptSet)

# Put the minimum distance vertex in the


# shotest path tree
sptSet[u] = True

# Update dist value of the adjacent vertices


# of the picked vertex only if the current
# distance is greater than new distance and
# the vertex in not in the shotest path tree
for v in range(self.V):
if self.graph[u][v] > 0 and sptSet[v] == False and self.dist[v] > self.dist[u] + self.graph[u][v]:
self.dist[v] = self.dist[u] + self.graph[u][v]

self.printSolution(self.dist)

The read function is also changed as follows:

f = open('facebook_combined.txt', 'r')
g = Graph(4039)
for line in f:
line = line.strip()
nodes = line.split(' ')
g.graph[int(nodes[0])][int(nodes[1])]=1
f.close()

The driver function is called as:

g = Graph(4039)

..
g.dijkstra(0)

The complete code is as follows:


import os
import sys

class Graph():

def __init__(self, vertices):


self.V = vertices
self.graph = [[0 for column in range(vertices)]
for row in range(vertices)]
self.dist = [int(sys.maxsize)] * int(self.V)

def printSolution(self, dist):


print ("Vertex \tDistance from Source")
for node in range(self.V):
if dist[node] < 5000:
print (node, "\t", dist[node])
# A utility function to find the vertex with
# minimum distance value, from the set of vertices
# not yet included in shortest path tree
def minDistance(self, dist, sptSet):

# Initilaize minimum distance for next node


mini = sys.maxsize
min_index = -1
# Search not nearest vertex not in the
# shortest path tree
for v in range(self.V):
if dist[v] < mini and sptSet[v] == False:
mini = dist[v]
min_index = v

return min_index

# Funtion that implements Dijkstra's single source


# shortest path algorithm for a graph represented
# using adjacency matrix representation
def dijkstra(self, src):

self.dist[src] = 0
sptSet = [False] * self.V

for cout in range(self.V):

# Pick the minimum distance vertex from


# the set of vertices not yet processed.
# u is always equal to src in first iteration
u = self.minDistance(self.dist, sptSet)

# Put the minimum distance vertex in the


# shotest path tree
sptSet[u] = True

# Update dist value of the adjacent vertices


# of the picked vertex only if the current
# distance is greater than new distance and
# the vertex in not in the shotest path tree
for v in range(self.V):
if self.graph[u][v] > 0 and sptSet[v] == False and self.dist[v] > self.dist[u] + self.graph[u][v]:
self.dist[v] = self.dist[u] + self.graph[u][v]

self.printSolution(self.dist)

os.chdir('/Users/z001ys6/Downloads/Soumojit/PA')
f = open('facebook_combined.txt', 'r')
g = Graph(4039)
for line in f:
line = line.strip()
nodes = line.split(' ')
g.graph[int(nodes[0])][int(nodes[1])]=1
g.graph[int(nodes[1])][int(nodes[0])]=1
f.close()
g.dijkstra(0)

print("Average Exposure Time:", sum(g.dist)/4039)

Sample Output:
Average Exposure Time : 2.82941322109433

You might also like