Dijkstra in Python
Posted: , Last Updated:
def dijkstra(graph, start):
"""
Implementation of dijkstra using adjacency matrix.
This returns an array containing the length of the shortest path from the start node to each other node.
It is only guaranteed to return correct results if there are no negative edges in the graph. Positive cycles are fine.
This has a runtime of O(V^2) (V = number of Nodes), for a faster implementation see @see ../fast/Dijkstra.java (using adjacency lists)
:param graph: an adjacencymatrixrepresentation of the graph where (x,y) is the weight of the edge or 0 if there is no edge.
:param start: the node to start from.
:return: an array containing the shortest distances from the given start node to each other node
"""
# This contains the distances from the start node to all other nodes
distances = [float("inf") for _ in range(len(graph))]
# This contains whether a node was already visited
visited = [False for _ in range(len(graph))]
# The distance from the start node to itself is of course 0
distances[start] = 0
# While there are nodes left to visit...
while True:
# ... find the node with the currently shortest distance from the start node...
shortest_distance = float("inf")
shortest_index = 1
for i in range(len(graph)):
# ... by going through all nodes that haven't been visited yet
if distances[i] < shortest_distance and not visited[i]:
shortest_distance = distances[i]
shortest_index = i
# print("Visiting node " + str(shortest_index) + " with current distance " + str(shortest_distance))
if shortest_index == 1:
# There was no node not yet visited > We are done
return distances
# ...then, for all neighboring nodes that haven't been visited yet....
for i in range(len(graph[shortest_index])):
# ...if the path over this edge is shorter...
if graph[shortest_index][i] != 0 and distances[i] > distances[shortest_index] + graph[shortest_index][i]:
# ...Save this path as new shortest path.
distances[i] = distances[shortest_index] + graph[shortest_index][i]
# print("Updating distance of node " + str(i) + " to " + str(distances[i]))
# Lastly, note that we are finished with this node.
visited[shortest_index] = True
# print("Visited nodes: " + str(visited))
# print("Currently lowest distances: " + str(distances))
About the algorithm and language used in this code snippet:
Dijkstra's Algorithm
The Dijkstra algorithm is an algorithm used to solve the shortest path problem in a graph. This means that given a number of nodes and the edges between them as well as the “length” of the edges (referred to as “weight”), the Dijkstra algorithm is finds the shortest path from the specified start node to all other nodes. Nodes are sometimes referred to as vertices (plural of vertex)  here, we’ll call them nodes.
Description of the Algorithm
The basic principle behind the Dijkstra algorithm is to iteratively look at the node with the currently smallest distance to the source and update all not yet visited neighbors if the path to it via the current node is shorter. In more detail, this leads to the following Steps:
 Initialize the distance to the starting node as 0 and the distances to all other nodes as infinite
 Set all nodes to “unvisited”

While we haven’t visited all nodes:
 Find the node with currently shortest distance from the source (for the first pass, this will be the source node itself)
 For all nodes next to it that we haven’t visited yet, check if the currently smallest distance to that neighbor is bigger than if we were to go via the current node
 If it is, update the smallest distance of that neighbor to be the distance from the source to the current node plus the distance from the current node to that neighbor
In the end, the array we used to keep track of the currently shortest distance from the source to all other nodes will contain the (final) shortest distances.
Example of the Algorithm
The steps the algorithm performs on this graph if given node 0 as a starting point, in order, are:
 Visiting node 0
 Updating distance of node 1 to 3
 Updating distance of node 2 to 1
 Visited nodes: 0
 Currently lowest distances: [0, 3, 1, Infinite, Infinite, Infinite]

Visiting node 1 with current distance 1
 Updating distance of node 3 to 5
 Visited nodes: 0, 2
 Currently lowest distances: [0, 3, 1, 5, Infinite, Infinite]

Visiting node 3 with current distance 3
 Updating distance of node 4 to 4
 Visited nodes: 0, 1, 2
 Currently lowest distances: [0, 3, 1, 5, 4, Infinite]

Visiting node 4 with current distance 4
 Updating distance of node 5 to 5
 Visited nodes: 0, 1, 2, 4
 Currently lowest distances: [0, 3, 1, 5, 4, 5]

Visiting node 5 with current distance 5
 No distances to update
 Visited nodes: 0, 1, 2, 3, 4
 Currently lowest distances: [0, 3, 1, 5, 4, 5]

Visiting node 5 with current distance 5
 No distances to update
 Visited nodes: 0, 1, 2, 3, 4, 5
All nodes visited Final lowest distances: [0, 3, 1, 5, 4, 5]
Runtime of the Algorithm
The runtime complexity of Dijkstra depends on how it is implemented. If a minheap is used to determine the next node to visit, and adjacency is implemented using adjacency lists, the runtime is O(E + VlogV) (V = number of Nodes, E = number of Edges). If a we simply search all distances to find the node with the lowest distance in each step, and use a matrix to look up whether two nodes are adjacent, the runtime complexity increases to O(V^2).
Space of the Algorithm
The space complexity of Dijkstra depends on how it is implemented as well and is equal to the runtime complexity.
Python
Python™ is an interpreted language used for many purposes ranging from embedded programming to web development, with one of the largest use cases being data science.
Getting to “Hello World” in Python
The most important things first  here’s how you can run your first line of code in Python.
 Download and install the latest version of Python from python.org. You can also download an earlier version if your use case requires it  many technologies still require it due to the breaking changes introduced with Python 3.
 Open a terminal, make sure the
python
orpython3
command is working, and that the command your’re going to be using is referring to the version you just installed by runningpython3 version
orpython version
. If you’re getting a “command not found” error (or similar), try restarting your command line, and, if that doesn’t help, your computer. If the issue persists, here are some helpful StackOverflow questions for Windows, Mac and Linux.  As soon as that’s working, you can run the following snippet:
print("Hello World")
. You have two options to run this: 3.1 Runpython
in the command line, just paste the code snippet and press enter (PressCTRL + D
or writeexit()
and press enter to exit). 3.2 Save the snippet to a file, name it something ending with.py
, e.g.hello_world.py
, and runpython path/to/hello_world.py
. Tip: use thels
command (dir
in Windows) to figure out which files are in the folder your command line is currently in.
That’s it! Notice how printing something to the console is just a single line in Python  this low entry barrier and lack of required boilerplate code is a big part of the appeal of Python.
Fundamentals in Python
To understand algorithms and technologies implemented in Python, one first needs to understand what basic programming concepts look like in this particular language.
Variables and Arithmetic
Variables in Python are really simple, no need to declare a datatype or even declare that you’re defining a variable; Python knows this implicitly.
a = 1
b = {'c':2}
print(a + b['c']) # prints 3
Arrays
Working with arrays is similarly simple in Python:
arr = ["Hello", "World"]
print(arr[0]) # Hello
print(arr[1]) # World
# print(arr[2]) # IndexError
arr.append("!")
print(arr[2]) # !
As those of you familiar with other programming language like Java might have already noticed, those are not native arrays, but rather lists dressed like arrays.
This is evident by the fact that no size needs to be specified, and elements can be appended at will. In fact, print(type(arr))
prints <class 'list'>
.
This means that arrays in Python are considerably slower than in lower level programming languages.
There are, however, packages like numpy which implement real arrays that are considerably faster.
Conditions
Just like most programming languages, Python can do ifelse
statements:
value = 1
if value==1:
print("Value is 1")
elif value==2:
print("Value is 2")
else:
print("Value is something else")
Python does however not have case
statements that other languages like Java have.
In my opinion, this can be excused by the simplicity of the if
statements which make the “syntactic sugar” of case
statements obsolete.
Loops
Python supports both for
and while
loops as well as break
and continue
statements.
While it does not have dowhile
loops, it does have a number of builtin functions that make make looping very convenient, like ‘enumerate’ or range
.
Here are some examples:
value = 10
while value > 0:
print(value)
value = 1
for index, character in enumerate("banana"):
print("The %dth letter is a %s" % (index + 1, character))
Note that Python does not share the common iteratorvariable syntax of other languages (e.g. for(int i = 0; i < arr.length; i++)
in Java)  for this, the enumerate
function can be used.
Functions
Functions in Python are easily defined and, for better or worse, do not require specifying return or arguments types. Optionally, a default for arguments can be specified:
def print_something(something="Hello World"):
print(something)
return "Success"
print_something()
print(print_something("banana"))
(This will print “Hello World”, “Banana”, and then “Success”)
Syntax
As you might have noticed, Python does not use curly brackets ({}
) to surround code blocks in conditions, loops, functions etc.;
This is because Python depends on indentation (whitespace) as part of its syntax.
Whereas you can add and delete any amount of whitespace (spaces, tabs, newlines) in Java without changing the program, this will break the Syntax in Python.
This also means that semicolons are not required, which is a common syntax error in other languages.
Advanced Knowledge of Python
Python was first released in 1990 and is multiparadigm, meaning while it is primarily imperative and functional, it also has objectoriented and reflective elements. It’s dynamically typed, but has started offering syntax for gradual typing since version 3.5. For more information, Python has a great Wikipedia article.