From 06243997adad32df1ba5983855c4c2a4516f1a80 Mon Sep 17 00:00:00 2001 From: Humberto Arcaya Date: Wed, 2 Oct 2019 11:04:11 -0400 Subject: [PATCH 1/5] creating a dijkstra file with clases --- Graph_Algorithms/src/Dijkstra/Dijkstra.py | 50 ++++++++++++++++++++ Graph_Algorithms/src/Dijkstra/Dijkstra_v2.py | 32 +++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 Graph_Algorithms/src/Dijkstra/Dijkstra.py create mode 100644 Graph_Algorithms/src/Dijkstra/Dijkstra_v2.py diff --git a/Graph_Algorithms/src/Dijkstra/Dijkstra.py b/Graph_Algorithms/src/Dijkstra/Dijkstra.py new file mode 100644 index 00000000..92009e12 --- /dev/null +++ b/Graph_Algorithms/src/Dijkstra/Dijkstra.py @@ -0,0 +1,50 @@ +class Graph: + def __init__(self): + self.nodes = set() + self.edges = defaultdict(list) + self.distances = {} + + def add_node(self, value): + self.nodes.add(value) + + def add_edge(self, from_node, to_node, distance): + self.edges[from_node].append(to_node) + self.edges[to_node].append(from_node) + self.distances[(from_node, to_node)] = distance + + +def dijsktra(graph, initial): + visited = {initial: 0} + path = {} + + 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 edge in graph.edges[min_node]: + weight = current_weight + graph.distance[(min_node, edge)] + if edge not in visited or weight < visited[edge]: + visited[edge] = weight + path[edge] = min_node + + return visited, path + + +def init_values(): + count = input('How many nodes would you like to add?\n') + graph = Graph() + +init_values() diff --git a/Graph_Algorithms/src/Dijkstra/Dijkstra_v2.py b/Graph_Algorithms/src/Dijkstra/Dijkstra_v2.py new file mode 100644 index 00000000..f3d7359f --- /dev/null +++ b/Graph_Algorithms/src/Dijkstra/Dijkstra_v2.py @@ -0,0 +1,32 @@ +class Arch: + initial_node = None + arch_value = 0 + final_node = None + +class Node: + number = None + archs = {} + visited = False + +class Graph: + nodes = [] + + def print_nodes(self): + for node in self.nodes: + print( str(node.number) ) + + +def init(): + count = int(5) + nodes = [] + + for i in range(count): + node = Node() + node.number = (i + 1) + nodes.append(node) + + graph = Graph() + graph.nodes = nodes + graph.print_nodes() + +init() From de38ccfb75074531d0ae7d1590cc990868ea8ac3 Mon Sep 17 00:00:00 2001 From: Humberto Arcaya Date: Wed, 2 Oct 2019 11:54:47 -0400 Subject: [PATCH 2/5] working in the algorithm implementation --- Graph_Algorithms/src/Dijkstra/Dijkstra.py | 2 +- Graph_Algorithms/src/Dijkstra/Dijkstra_v2.py | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Graph_Algorithms/src/Dijkstra/Dijkstra.py b/Graph_Algorithms/src/Dijkstra/Dijkstra.py index 92009e12..5b2ced29 100644 --- a/Graph_Algorithms/src/Dijkstra/Dijkstra.py +++ b/Graph_Algorithms/src/Dijkstra/Dijkstra.py @@ -13,7 +13,7 @@ def add_edge(self, from_node, to_node, distance): self.distances[(from_node, to_node)] = distance -def dijsktra(graph, initial): +def dijkstra(graph, initial): visited = {initial: 0} path = {} diff --git a/Graph_Algorithms/src/Dijkstra/Dijkstra_v2.py b/Graph_Algorithms/src/Dijkstra/Dijkstra_v2.py index f3d7359f..3c8df89b 100644 --- a/Graph_Algorithms/src/Dijkstra/Dijkstra_v2.py +++ b/Graph_Algorithms/src/Dijkstra/Dijkstra_v2.py @@ -1,3 +1,5 @@ +import random + class Arch: initial_node = None arch_value = 0 @@ -5,7 +7,7 @@ class Arch: class Node: number = None - archs = {} + archs = [] visited = False class Graph: @@ -15,7 +17,6 @@ def print_nodes(self): for node in self.nodes: print( str(node.number) ) - def init(): count = int(5) nodes = [] @@ -25,6 +26,12 @@ def init(): node.number = (i + 1) nodes.append(node) + for i in range(count): + arch = Arch() + if i < count: + arch.initial_node = nodes[i] + arch.value = random.randint(1, 50) + graph = Graph() graph.nodes = nodes graph.print_nodes() From 95c23b5c8a47e46e31bc07493939dfd1308207dd Mon Sep 17 00:00:00 2001 From: Humberto Arcaya Date: Thu, 3 Oct 2019 08:44:57 -0400 Subject: [PATCH 3/5] initializing values and showing the output --- Graph_Algorithms/src/Dijkstra/Dijkstra.py | 30 ++++++++++++--- Graph_Algorithms/src/Dijkstra/Dijkstra_v2.py | 39 -------------------- 2 files changed, 25 insertions(+), 44 deletions(-) delete mode 100644 Graph_Algorithms/src/Dijkstra/Dijkstra_v2.py diff --git a/Graph_Algorithms/src/Dijkstra/Dijkstra.py b/Graph_Algorithms/src/Dijkstra/Dijkstra.py index 5b2ced29..348bd928 100644 --- a/Graph_Algorithms/src/Dijkstra/Dijkstra.py +++ b/Graph_Algorithms/src/Dijkstra/Dijkstra.py @@ -1,3 +1,6 @@ +from collections import defaultdict +import math + class Graph: def __init__(self): self.nodes = set() @@ -35,7 +38,10 @@ def dijkstra(graph, initial): current_weight = visited[min_node] for edge in graph.edges[min_node]: - weight = current_weight + graph.distance[(min_node, edge)] + try: + weight = current_weight + graph.distances[(min_node, edge)] + except: + weight = current_weight + math.inf if edge not in visited or weight < visited[edge]: visited[edge] = weight path[edge] = min_node @@ -43,8 +49,22 @@ def dijkstra(graph, initial): return visited, path -def init_values(): - count = input('How many nodes would you like to add?\n') - graph = Graph() +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')) -init_values() +main() diff --git a/Graph_Algorithms/src/Dijkstra/Dijkstra_v2.py b/Graph_Algorithms/src/Dijkstra/Dijkstra_v2.py deleted file mode 100644 index 3c8df89b..00000000 --- a/Graph_Algorithms/src/Dijkstra/Dijkstra_v2.py +++ /dev/null @@ -1,39 +0,0 @@ -import random - -class Arch: - initial_node = None - arch_value = 0 - final_node = None - -class Node: - number = None - archs = [] - visited = False - -class Graph: - nodes = [] - - def print_nodes(self): - for node in self.nodes: - print( str(node.number) ) - -def init(): - count = int(5) - nodes = [] - - for i in range(count): - node = Node() - node.number = (i + 1) - nodes.append(node) - - for i in range(count): - arch = Arch() - if i < count: - arch.initial_node = nodes[i] - arch.value = random.randint(1, 50) - - graph = Graph() - graph.nodes = nodes - graph.print_nodes() - -init() From 402090f8a6b221adfc8b3f7170df2af71005a544 Mon Sep 17 00:00:00 2001 From: Humberto Arcaya Date: Thu, 3 Oct 2019 08:51:54 -0400 Subject: [PATCH 4/5] last changes --- Graph_Algorithms/src/Dijkstra/Dijkstra.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Graph_Algorithms/src/Dijkstra/Dijkstra.py b/Graph_Algorithms/src/Dijkstra/Dijkstra.py index 348bd928..950c4138 100644 --- a/Graph_Algorithms/src/Dijkstra/Dijkstra.py +++ b/Graph_Algorithms/src/Dijkstra/Dijkstra.py @@ -4,16 +4,16 @@ class Graph: def __init__(self): self.nodes = set() - self.edges = defaultdict(list) - self.distances = {} + self.archs = defaultdict(list) + self.values = {} def add_node(self, value): self.nodes.add(value) - def add_edge(self, from_node, to_node, distance): - self.edges[from_node].append(to_node) - self.edges[to_node].append(from_node) - self.distances[(from_node, to_node)] = distance + 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): @@ -37,14 +37,14 @@ def dijkstra(graph, initial): nodes.remove(min_node) current_weight = visited[min_node] - for edge in graph.edges[min_node]: + for arch in graph.archs[min_node]: try: - weight = current_weight + graph.distances[(min_node, edge)] + weight = current_weight + graph.values[(min_node, arch)] except: weight = current_weight + math.inf - if edge not in visited or weight < visited[edge]: - visited[edge] = weight - path[edge] = min_node + if arch not in visited or weight < visited[arch]: + visited[arch] = weight + path[arch] = min_node return visited, path From 5fafa2aa4ef0baaa2eda8da53150958ab36cc2b8 Mon Sep 17 00:00:00 2001 From: Humberto Arcaya Date: Thu, 3 Oct 2019 08:53:31 -0400 Subject: [PATCH 5/5] showing only the distances instead of the path --- Graph_Algorithms/src/Dijkstra/Dijkstra.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Graph_Algorithms/src/Dijkstra/Dijkstra.py b/Graph_Algorithms/src/Dijkstra/Dijkstra.py index 950c4138..04d03070 100644 --- a/Graph_Algorithms/src/Dijkstra/Dijkstra.py +++ b/Graph_Algorithms/src/Dijkstra/Dijkstra.py @@ -18,7 +18,6 @@ def add_edge(self, init_node, final_node, value): def dijkstra(graph, initial): visited = {initial: 0} - path = {} nodes = set(graph.nodes) @@ -44,9 +43,8 @@ def dijkstra(graph, initial): weight = current_weight + math.inf if arch not in visited or weight < visited[arch]: visited[arch] = weight - path[arch] = min_node - return visited, path + return visited def main():