|
| 1 | +""" |
| 2 | +This snippet implements dijkstra's algorithm in Python. |
| 3 | +Currently it works only with graphs defined by integers for both directed and undirected graphs |
| 4 | +It doesn't work with negative weight edges. |
| 5 | +Python version: 3.5.2 |
| 6 | +
|
| 7 | +Possible improvements (I think so, not sure): |
| 8 | + Remove dependency of integer type and sys.maxsize, using comparable types and logic instead |
| 9 | + Add test cases to an unit test class as seen here: https://docs.python.org/2/library/unittest.html |
| 10 | + Add more test cases |
| 11 | +""" |
| 12 | + |
| 13 | +import sys |
| 14 | + |
| 15 | +""" |
| 16 | +utility function to find the minimun value between values that aren't processed yet |
| 17 | +
|
| 18 | +param dist_array: Array of integers with the distances from the vertex vert |
| 19 | +param processed: Array of booleans with True for processed values, False otherwise |
| 20 | +return min: the index of the shortest distance vertex that is not processed, -1 if all the values are already processed |
| 21 | +""" |
| 22 | +def min_dist_index(dist_array, processed): |
| 23 | + vert = -1 |
| 24 | + min = sys.maxsize # Maximum value for int value in python 3.0 |
| 25 | + |
| 26 | + for i in range(len(dist_array)): |
| 27 | + if not(processed[i]) and min > dist_array[i]: |
| 28 | + min = dist_array[i] |
| 29 | + vert = i |
| 30 | + |
| 31 | + return vert |
| 32 | + |
| 33 | +""" |
| 34 | +Algorithm to find the shortest distance from all vertex in a graph to the vertex vert |
| 35 | +
|
| 36 | +param adj: Adjancency matrix of the graph, non-existing edges should be represented by -1 |
| 37 | +param vert: The vertex from which the distance will be calculated |
| 38 | +return distance: Array with the shortest path from vert to all other vertices |
| 39 | +""" |
| 40 | +def dijsktra(adj, vert): |
| 41 | + distance = [0] * len(adj[vert]) |
| 42 | + processed = [False] * len(adj[vert]) |
| 43 | + |
| 44 | + # Initializes the distance array of initial vertex with values from the adjacency matrix |
| 45 | + # if the edge is unexistent (-1), sets the distance as maxsize |
| 46 | + for i in range(len(adj[vert])): |
| 47 | + if adj[vert][i] == -1: |
| 48 | + distance[i] = sys.maxsize |
| 49 | + else: |
| 50 | + distance[i] = adj[vert][i] |
| 51 | + |
| 52 | + # initializes distance to initial vertex as 0 |
| 53 | + distance[vert] = 0 |
| 54 | + processed[vert] = True |
| 55 | + |
| 56 | + # It keeps running until all minimum distances between vertex have been calculated |
| 57 | + while True: |
| 58 | + |
| 59 | + v = min_dist_index(distance, processed) |
| 60 | + |
| 61 | + if v == -1: break |
| 62 | + |
| 63 | + processed[v] = True |
| 64 | + |
| 65 | + # Ternary operator to determine the shortest path between: |
| 66 | + # initial vertex (vert) -> ith vertex and |
| 67 | + # initial vertex (vert) -> current closest vertex (v) -> ith vertex |
| 68 | + for i in range(len(distance)): |
| 69 | + distance[i] = distance[i] if distance[i] < distance[v] + adj[v][i] else distance[v] + adj[v][i] |
| 70 | + |
| 71 | + return distance |
| 72 | + |
| 73 | +def printSolution(dist): |
| 74 | + print ("Vertex Distance from source") |
| 75 | + for d, i in zip(dist, range(len(dist))): |
| 76 | + print (str(i) + " tt " + str(d)) |
| 77 | + |
| 78 | +def test_undirect(): |
| 79 | + adj = [[0, 4, -1, -1, -1, -1, -1, 8, -1], |
| 80 | + [4, 0, 8, -1, -1, -1, -1, 11, -1], |
| 81 | + [-1, 8, 0, 7, -1, 4, -1, -1, 2], |
| 82 | + [-1, -1, 7, 0, 9, 14, -1, -1, -1], |
| 83 | + [-1, -1, -1, 9, 0, 10, -1, -1, -1], |
| 84 | + [-1, -1, 4, 14, 10, 0, 2, -1, -1], |
| 85 | + [-1, -1, -1, -1, -1, 2, 0, 1, 6], |
| 86 | + [8, 11, -1, -1, -1, -1, 1, 0, 7], |
| 87 | + [-1, -1, 2, -1, -1, -1, 6, 7, 0]] |
| 88 | + |
| 89 | + for i in range(len(adj)): |
| 90 | + for j in range(len(adj[i])): |
| 91 | + if adj[i][j] == -1: |
| 92 | + adj[i][j] = sys.maxsize |
| 93 | + |
| 94 | + print ("Testing undirected graph") |
| 95 | + print ("Results: ") |
| 96 | + shortest = dijsktra(adj, 0) |
| 97 | + printSolution(shortest) |
| 98 | + |
| 99 | +def test_directed(): |
| 100 | + adj = [[0, 2, -1, 1, -1, -1, -1], |
| 101 | + [-1, 0, -1, 3, 10, -1, -1], |
| 102 | + [4, -1, 0, -1, -1, 5, -1], |
| 103 | + [-1, -1, 2, 0, 2, 8, 4], |
| 104 | + [-1, -1, -1, -1, 0, -1, 6], |
| 105 | + [-1, -1, -1, -1, -1, 0, -1], |
| 106 | + [-1, -1, -1, -1, -1, 1, 0]] |
| 107 | + |
| 108 | + for i in range(len(adj)): |
| 109 | + for j in range(len(adj[i])): |
| 110 | + if adj[i][j] == -1: |
| 111 | + adj[i][j] = sys.maxsize |
| 112 | + |
| 113 | + print ("Testing directed graph... ") |
| 114 | + print ("Result: ") |
| 115 | + shortest = dijsktra(adj, 0) |
| 116 | + printSolution(shortest) |
| 117 | + |
| 118 | +test_undirect() |
| 119 | +test_directed() |
0 commit comments