Skip to content

Commit

Permalink
Merge pull request codezoned#174 from bettoarcaya/adding-dijkstra-alg…
Browse files Browse the repository at this point in the history
…orithm

Adding dijkstra algorithm
  • Loading branch information
ionicc authored Oct 4, 2019
2 parents 36a6300 + 5fafa2a commit 15b2124
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions Graph_Algorithms/src/Dijkstra/Dijkstra.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from collections import defaultdict
import math

class Graph:
def __init__(self):
self.nodes = set()
self.archs = defaultdict(list)
self.values = {}

def add_node(self, value):
self.nodes.add(value)

def add_edge(self, init_node, final_node, value):
self.archs[init_node].append(final_node)
self.archs[init_node].append(final_node)
self.values[(init_node, final_node)] = value


def dijkstra(graph, initial):
visited = {initial: 0}

nodes = set(graph.nodes)

while nodes:
min_node = None
for node in nodes:
if node in visited:
if min_node is None:
min_node = node
elif visited[node] < visited[min_node]:
min_node = node

if min_node is None:
break

nodes.remove(min_node)
current_weight = visited[min_node]

for arch in graph.archs[min_node]:
try:
weight = current_weight + graph.values[(min_node, arch)]
except:
weight = current_weight + math.inf
if arch not in visited or weight < visited[arch]:
visited[arch] = weight

return visited


def main():

#initializing values
g = Graph()
g.add_node('a')
g.add_node('b')
g.add_node('c')
g.add_node('d')

g.add_edge('a', 'b', 10)
g.add_edge('b', 'c', 2)
g.add_edge('a', 'c', 1)
g.add_edge('c', 'd', 1)
g.add_edge('b', 'd', 8)

#output
print(dijkstra(g, 'a'))

main()

0 comments on commit 15b2124

Please sign in to comment.