Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
5003CEM_Tasks/ADV_2 - Dijkstra's Algorithm.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
131 lines (104 sloc)
4.33 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
import sys | |
class Graph(): | |
def __init__(self, size): | |
self.edges = defaultdict(list) #dictionary of all connected nodes e.g. {'X': ['A', 'B', 'C', 'E'], ...} | |
self.weights = {} #dictionary of edges and weights e.g. {('X', 'A'): 7, ('X', 'B'): 2, ...} | |
self.size = size | |
self.dist = [] | |
for i in range(size): | |
self.dist.append(sys.maxsize) | |
self.previous = [] | |
for i in range(size): | |
self.previous.append(None) | |
def add_edge(self, from_node, to_node, weight): #bidirectional | |
self.edges[from_node].append(to_node) | |
self.edges[to_node].append(from_node) | |
self.weights[(from_node, to_node)] = weight | |
self.weights[(to_node, from_node)] = weight | |
def findSmallestNode(self): | |
smallest = self.dist[self.getIndex(self.Q[0])] | |
result = self.getIndex(self.Q[0]) | |
for i in range(len(self.dist)): | |
if self.dist[i] < smallest: | |
node = self.unpoppedQ[i] | |
if node in self.Q: | |
smallest = self.dist[i] | |
result = self.getIndex(node) | |
return result | |
def getIndex(self, neighbour): | |
for i in range(len(self.unpoppedQ)): | |
if neighbour == self.unpoppedQ[i]: | |
return i | |
def getPopPosition(self, uNode): | |
result = 0 | |
for i in range(len(self.Q)): | |
if self.Q[i] == uNode: | |
return i | |
return result | |
def getUnvisitedNodes(self, uNode): | |
resultList = [] | |
allNeighbours = self.edges[uNode] | |
for neighbour in allNeighbours: | |
if neighbour in self.Q: | |
resultList.append(neighbour) | |
return resultList | |
def dijsktra(self, start, end): | |
self.Q = [] | |
for key in self.edges: | |
self.Q.append(key) | |
for i in range(len(self.Q)): | |
if self.Q[i] == start: | |
self.dist[i] = 0 | |
self.unpoppedQ = self.Q[0:] | |
while self.Q: | |
u = self.findSmallestNode() | |
if self.dist[u] == sys.maxsize: | |
break | |
if self.unpoppedQ[u] == end: | |
break | |
uNode = self.unpoppedQ[u] | |
#v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v | |
## '''Dijkstra's Algorithm | |
## input: Nodes and weights | |
## output: Shortest path between two nodes | |
## Implements Dijkstra's shortest path algorithm | |
## ''' | |
for value in edges: #loops through all edges | |
prevNode = value[0] #sets first node as source node | |
nodeWeight = int(value[2]) #sets the weight of the node | |
temp = nodeWeight #creates temporary value using weight of the node | |
print("node weight: ", nodeWeight) #prints result for the user | |
print("previous node: ", prevNode) | |
print() | |
if temp < (nodeWeight + prevNode): #if the weight is less than the previous weight | |
nodeWeight = temp #set the weight as the new lower weight | |
prevNode = value | |
#^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ | |
shortest_path = [] | |
shortest_path.insert(0, end) | |
u = self.getIndex(end) | |
while self.previous[u] != None: | |
shortest_path.insert(0, self.previous[u]) | |
u = self.getIndex(self.previous[u]) | |
return shortest_path | |
graph = Graph(8) | |
edges = [ | |
('O', 'A', 2), | |
('O', 'B', 5), | |
('O', 'C', 4), | |
('A', 'B', 2), | |
('A', 'D', 7), | |
('A', 'F', 12), | |
('B', 'C', 1), | |
('B', 'D', 4), | |
('B', 'E', 3), | |
('C', 'E', 4), | |
('D', 'E', 1), | |
('D', 'T', 5), | |
('E', 'T', 7), | |
('F', 'T', 3), | |
] | |
for edge in edges: | |
graph.add_edge(*edge) | |
print(graph.dijsktra('O', 'T')) |