From fe7fb12a78248d93f092d3bb781804af9e0fefd6 Mon Sep 17 00:00:00 2001 From: Cristian Bastidas Date: Sat, 8 Oct 2022 15:14:36 -0500 Subject: [PATCH] Renamed file dijkstra --- algorithms/Python/README.md | 1 + .../{python-dijkstra.py => dijkstra.py} | 0 algorithms/Python/graphs/python-dijsktra.py | 110 ------------------ 3 files changed, 1 insertion(+), 110 deletions(-) rename algorithms/Python/graphs/{python-dijkstra.py => dijkstra.py} (100%) delete mode 100644 algorithms/Python/graphs/python-dijsktra.py diff --git a/algorithms/Python/README.md b/algorithms/Python/README.md index 9bcc929b..e80a5727 100644 --- a/algorithms/Python/README.md +++ b/algorithms/Python/README.md @@ -77,6 +77,7 @@ - [Simple Graph](graphs/graph.py) - [BFS SEQUENCE](graphs/bfs-sequence.py) - [Depth First Search](graphs/depth-first-search.py) +- [Dijkstra's Algorithm](graphs/dijkstra.py) ## Trees - [Binary Tree](trees/binary_tree.py) diff --git a/algorithms/Python/graphs/python-dijkstra.py b/algorithms/Python/graphs/dijkstra.py similarity index 100% rename from algorithms/Python/graphs/python-dijkstra.py rename to algorithms/Python/graphs/dijkstra.py diff --git a/algorithms/Python/graphs/python-dijsktra.py b/algorithms/Python/graphs/python-dijsktra.py deleted file mode 100644 index ede86b7c..00000000 --- a/algorithms/Python/graphs/python-dijsktra.py +++ /dev/null @@ -1,110 +0,0 @@ -# A Dijkstra's algorithm implementation in Python -# using adjacency matrix - -from math import inf - -wmat = [[0, 2, 0, 0, 0, 1, 0, 0], - [2, 0, 2, 2, 4, 0, 0, 0], - [0, 2, 0, 0, 3, 0, 0, 1], - [0, 2, 0, 0, 4, 3, 0, 0], - [0, 4, 3, 4, 0, 0, 7, 0], - [1, 0, 0, 3, 0, 0, 5, 0], - [0, 0, 0, 0, 7, 5, 0, 6], - [0, 0, 1, 0, 0, 0, 6, 0]] - - -def find_all(wmat, start, end=-1): - """ - Returns a tuple with a distances' list and paths' list of - all remaining vertices with the same indexing. - - (distances, paths) - - For example, distances[x] are the shortest distances from x - vertex which shortest path is paths[x]. x is an element of - {0, 1, ..., n-1} where n is the number of vertices - - Args: - wmat -- weighted graph's adjacency matrix - start -- paths' first vertex - end -- (optional) path's end vertex. Return just the - distance and its path - - Exceptions: - Index out of range, Be careful with start and end vertices - """ - n = len(wmat) - - dist = [inf]*n - dist[start] = wmat[start][start] # 0 - - spVertex = [False]*n - parent = [-1]*n - - path = [{}]*n - - for count in range(n-1): - minix = inf - u = 0 - - for v in range(len(spVertex)): - if spVertex[v] == False and dist[v] <= minix: - minix = dist[v] - u = v - - spVertex[u] = True - for v in range(n): - if not(spVertex[v]) and wmat[u][v] != 0 and dist[u] + wmat[u][v] < dist[v]: - parent[v] = u - dist[v] = dist[u] + wmat[u][v] - - for i in range(n): - j = i - s = [] - while parent[j] != -1: - s.append(j) - j = parent[j] - s.append(start) - path[i] = s[::-1] - - return (dist[end], path[end]) if end >= 0 else (dist, path) - - -def find_shortest_path(wmat, start, end=-1): - """ - Returns paths' list of all remaining vertices. - - Args: - wmat -- weigthted graph's adjacency matrix - start -- paths' first vertex - end -- (optional) path's end vertex. Return just - the path - - Exceptions: - Index out of range, Be careful with start and end vertices. - """ - return find_all(wmat, start, end)[1] - - -def find_shortest_distance(wmat, start, end=-1): - """ - Returns distances' list of all remaining vertices. - - Args: - wmat -- weigthted graph's adjacency matrix - start -- paths' first vertex - end -- (optional) path's end vertex. Return just - the distance - - Exceptions: - Index out of range, Be careful with start and end vertices. - """ - return find_all(wmat, start, end)[0] - -if __name__ == "__main__": - i = 0 - for D, P in zip(*find_all(wmat, 0)): - print("Target: {}, Distance: {}, Path: {}".format(i, D, P)) - i += 1 - print("\nshortest path to last node", find_shortest_path(wmat, 0, 7)) - print("shortest distance to last node", find_shortest_distance(wmat, 0, 7))