---
abstract: |
    Python implementation of selected weighted graph algorithms is presented. The minimal graph interface is defined together with several classes implementing this interface. Graph nodes can be any hashable Python objects. Directed edges are instances of the `Edge` class. Graphs are instances of the `Graph` class. It is based on the adjacency-list representation, but with fast lookup of nodes and neighbors (dict-of-dict structure). Other implementations of this class are also possible.

    In this work, many algorithms are implemented using a unified approach. There are separate classes and modules devoted to different algorithms. Three algorithms for finding a minimum spanning tree are implemented: the Boruvka’s algorithm, the Prim’s algorithm (three implementations), and the Kruskal’s algorithm. Three algorithms for solving the single-source shortest path problem are implemented: the dag shortest path algorithm, the Bellman-Ford algorithm, and the Dijkstra’s algorithm (two implementations). Two algorithms for solving all-pairs shortest path problem are implemented: the Floyd-Warshall algorithm and the Johnson’s algorithm.

    All algorithms were tested by means of the `unittest` module, the Python unit testing framework. Additional computer experiments were done in order to compare real and theoretical computational complexity. The source code is available from the public GitHub repository.
author:
- 'A. Kapanowski'
- '[Ł]{}. Ga[ł]{}uszka'
title: Weighted graph algorithms with Python
---

Introduction {#sec:intro}
============

Algorithms are at the heart of computer science. They are expressed as a finite sequence of operations with clearly defined input and output. Algorithms can be expressed by natural languages, pseudocode, flowcharts or programming languages. Pseudocode is often used in textbooks and scientific publications because it is compact and augmented with natural language description. However, in depth understanding of algorithms is almost impossible without computer experiments where algorithms are implemented by means of a programming language.

Our aim is to show that the Python programming language [@python] can be used to implement algorithms with simplicity and elegance. On the other hand, the code is almost as readable as pseudocode because the Python syntax is very clear. That is why Python has become one of the most popular teaching languages in colleges and universities [@2015_Shein], and it is used in scientific research [@2015_Perkel]. Python implementation of some algorithms from computational group theory was shown in Ref. [@2014_Kapanowski]. In this paper we are interested in graph theory and weighted graph algorithms. Other algorithms will be discussed elsewhere. The source code of our programs is available from the public GitHub repository [@graph-dict]. We strongly support a movement toward open source scientific software [@2015_Hayden].

Graphs can be used to model many types of relations and processes in physical, biological, social, and information systems [@wiki_graph_theory]. Many practical problems can be represented by graphs and this can be the first step to finding the solution of the problem. Sometimes the solution can be known for a long time because graph theory was born in 1736. In this year Leonard Euler solved the famous Seven Bridges of Königsberg (Królewiec in Polish) problem.

We examined several Python graph packages from The Python Package Index [@pypi] in order to checked different approaches. Some graph libraries written in other programming languages were checked briefly [@boost], [@boost_graph], [@leda], [@leda_book], [@gtl].

-   Package *NetworkX 1.9* by Aric A. Hagberg, Daniel A. Schult, and Pieter J. Swart [@networkx]. A library for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks. Basic classes are `Graph`, `DiGraph`, `MultiGraph`, and `MultiDiGraph`. All graph classes allow any hashable object as a node. Arbitrary edge attributes can be associated with an edge. The dictionary of dictionaries data structure is used to store graphs. NetworkX is integrated into Sage. [@sage].

-   Package *python-igraph 0.7.0* [@python-igraph]. *igraph* is the network analysis package with versions for R, Python, and C/C++.

-   Package *python-graph 1.8.2* by Pedro Matiello [@python-graph]. A library for working with graphs in Python. Edges are standard Python tuples, weights or labels are kept separately. There are different classes for directed graphs, undirected graphs, and hypergraphs.

-   Package *graph 0.4* by Robert Dick and Kosta Gaitanis [@Dick_Gaitanis]. Directed and undirected graph data structures and algorithms.

None of the available implementations satisfy our needs. Usually high computational efficiency leads to the unreadable code. On the other hand, C/C++ syntax or Java syntax are not very close to the pseudocode used for learning algorithms. That is why we develop and advocate our approach. We note that presented Python implementations may be not as fast as C/C++ or Java counterparts but they scale with the input size according to the theory. The presented algorithms are well known and that is why we used references to many Wikipedia pages.

The paper is organized as follows. In Section \[sec:definitions\] basic definitions from graph theory are given. In Section \[sec:graph\_iface\] the graph interface is presented. In Sections \[sec:mst\], \[sec:single-source\], and \[sec:all-pairs\] the following algorithms are shown: for finding a minimum spanning tree, for solving the single-source shortest path problem, for solving all-pair shortest path problem. Conclusions are contained in Section \[sec:conclusions\].

Definitions {#sec:definitions}
===========

Definitions of graphs vary and that is why we will present our choice which is very common [@CLRS]. We will not consider multigraphs with loops and multiple edges.

Graphs
------

A *(simple) graph* is an ordered pair $G=(V,E)$, where $V$ is a finite set of nodes (vertices, points) and $E$ is a finite set of edges (lines, arcs, links). An edge is an ordered pair of *different* nodes from $V$, $(s,t)$, where $s$ is the source node and $t$ is the target node. This is a *directed edge* and in that case $G$ is called a *directed graph*.

An edge can be defined as a 2-element subset of $V$, $\{s,t\}=\{t,s\}$, and then it is called an *undirected edge*. The nodes $s$ and $t$ are called the ends of the edge (endpoints) and they are *adjacent* to one another. The edge connects or joins the two endpoints. A graph with undirected edges is called an *undirected graph*. In our approach, an undirected edge corresponds to the set of two directed edges $\{(s,t), (t,s)\}$ and the representative is usually $(s,t)$ with $s<t$.

The *order* of a graph $G=(V,E)$ is the number of nodes $|V|$. The *degree* of a node in an undirected graph is the number of edges that connect to it.

A graph $G'=(V',E')$ is a *subgraph* of a graph $G=(V,E)$ if $V'$ is a subset of $V$ and $E'$ is a subset of $E$.

Graphs with weights
-------------------

A graph structure can be extended by assigning a number (weight) $w(s,t)$ to each edge $(s,t)$ of the graph. Weights can represent lengths, costs or capacities. In that case a graph is a *weighted graph*.

Paths and cycles
----------------

A *path* $P$ from $s$ to $t$ in a graph $G=(V,E)$ is a sequence of nodes from $V$, $(v_0,v_1,\ldots,v_n)$, where $v_0=s$, $v_n=t$, and $(v_{i-1},v_{i})$ ($i=1,\ldots,n$) are edges from $E$. The length of the path $P$ is $n$. A *simple path* is a path with distinct nodes. The *weight (cost or length) of the path* in a weighted graph is the sum of the weights of the corresponding edges.

A *cycle* is a path $C$ starting and ending at the same node, $v_0 = v_n$. A *simple cycle* is a cycle with no repetitions of nodes allowed, other than the repetition of the starting and ending node.

A *directed path (cycle)* is a path (cycle) where the corresponding edges are directed. In our implementation, a path is a list of nodes. A graph is *connected* if for every pair of nodes $s$ and $t$, there is a path from $s$ to $t$.

Trees
-----

A *(free) tree* is a connected undirected graph $T$ with no cycles. A *forest* is a disjoint union of trees. A *spanning tree* of a connected, undirected graph $G$ is a tree $T$ that includes all nodes of $G$ and is a subgraph of $G$ [@wiki_spanning_tree]. Spanning trees are important because they construct a sparse subgraph that tells a lot about the original graph. Also some hard problems can be solved approximately by using spanning trees (e.g. traveling salesman problem).

A *rooted tree* is a tree $T$ where one node is designated the *root*. In that case, the edges can be oriented towards or away from the root. In our implementation, a rooted tree is kept as a dictionary, where keys are nodes and values are parent nodes. The parent node of the root is `None`. A forest of rooted trees can be kept in a dictionary with many roots.

A *shortest-path tree* rooted at node $s$ is a spanning tree $T$ of $G$, such that the path distance from root $s$ to any other node $t$ in $T$ is the shortest path distance from $s$ to $t$ in $G$.

Interface for graphs {#sec:graph_iface}
====================

According to the definitions from Section \[sec:definitions\], graphs are composed of nodes and edges. In our implementation nodes can be any hashable object that can be sorted. Usually they are integer or string.

Edges are instances of the `Edge` class (`edges` module) and they are directed, hashable, and comparable. Any edge has the starting node (`edge.source`), the ending node (`edge.target`), and the weight (`edge.weight`). The default weight is one. The edge with the opposite direction is equal to `~edge`. This is very useful for combinatorial maps used to represent planar graphs [@wiki_combinatorial_maps].

Simple graphs (directed and undirected) are instances of the `Graph` class (`graphs` module). Multigraphs (directed and undirected) are instances of the `MultiGraph` class (`multigraphs` module) and they will not be discussed here. Let us show some properties of graphs that are listed in Table \[tab:class\_graph\]. There are methods to report some numbers (nodes, edges, degrees). There are iterators over nodes and edges. There are also some logical functions.

    >>> from edges import Edge
    >>> from graphs import Graph
    >>> G = Graph(n=3, directed=False)
    # n is for compatibility with other implementations.
    >>> G.is_directed()
    False
    >>> G.add_edge(Edge('A', 'B', 5))
    >>> G.add_edge(Edge('A', 'C', 7))
    # Nodes are added by default.
    >>> print G.v(), G.e()        # numbers of nodes and edges
    3 2
    >>> list(G.iternodes())
    ['A', 'C', 'B']               # random order of nodes
    >>> sorted((G.degree(v) for v in G.iternodes()), reverse=True)
    [2, 1, 1]                     # the degree sequence
    >>> list(v for v in G.iternodes() if G.degree(v) == 1)
    ['C', 'B']                    # leafs
    >>> sum(edge.weight for edge in G.iteredges())
    12                            # the graph (tree) weight
    # Typical usage of an algorithm Foo from the module foo.
    >>> from foo import Foo
    >>> algorithm = Foo(G)        # initialization
    >>> algorithm.run()           # calculations
    >>> print algorithm.result    # results can be more...

Note that in the case of undirected graphs, `edge` and `~edge` are two representatives of the same undirected edge. The method `iteredges` returns a representative with the ordering `edge.source < edge.target`.

Graph algorithms are implemented using a unified approach. There is a separate class for every algorithm. Different implementations of the same algorithm are grouped in one module. In the `__init__` method, main variables and data structures are initialized. The name space of a class instance is used to access the variables and that is why interfaces of the auxiliary methods can be very short.

  Method name                 Short description
  --------------------------- -------------------------------------------
  `Graph(n)`                  return an empty undirected graph
  `Graph(n, directed=True)`   return an empty directed graph
  `G.is_directed()`           return `True` if $G$ is a directed graph
  `G.v()`                     return the number of nodes
  `G.e()`                     return the number of edges
  `G.add_node(s)`             add $s$ to $G$
  `G.del_node(s)`             remove $s$ form $G$
  `G.has_node(s)`             return `True` if $s$ is in $G$
  `G.add_edge(edge)`          add a new edge to $G$
  `G.del_edge(edge)`          remove the edge form $G$
  `G.has_edge(edge)`          return `True` if the edge is in $G$
  `G.weight(edge)`            return the edge weight or zero
  `G.iternodes()`             generate nodes on demand
  `G.iteredges()`             generate edges on demand
  `G.iteroutedges(s)`         generate outedges on demand
  `G.iterinedges(s)`          generate inedges on demand
  `G.degree(s)`               return the degree of $s$ ($G$ undirected)
  `G.indegree(s)`             return the indegree of $s$
  `G.outdegree(s)`            return the outdegree of $s$

  :  \[tab:class\_graph\] Interface for graphs; $G$ is a graph, $s$ and $t$ are nodes.

Minimum spanning tree {#sec:mst}
=====================

Let us assume that $G=(V,E)$ is a connected, undirected, weighted graph, and $T$ is a spanning tree of $G$. We can assign a weight to the spanning tree $T$ by computing the sum of the weights of the edges in $T$. A *minimum spanning tree (MST)* is a spanning tree with the weight less than or equal to the weight of every other spanning tree [@wiki_mst]. In general, MST is not unique. There is only one MST if each edge has a distinct weight (a proof by contradiction).

Minimum spanning trees have many practical applications: the design of networks, taxonomy, cluster analysis, and others [@wiki_mst]. We would like to present three classical algorithms for finding MST that run in polynomial time.

Boruvka’s algorithm
-------------------

The Boruvka’s algorithm works for a connected graph whose edges have distinct weights what implies the unique MST. In the beginning, the cheapest edge from each node to another in the graph is found, without regard to already added edges. Then joining these groupings continues in this way until MST is completed [@wiki_boruvka]. Components of MST are tracked using a disjoint-set data structure. The algorithm runs in $O(E \log V)$ time.

Our implementation of the Boruvka’s algorithm works also for disconnected graphs. In that case a forest of minimum spanning trees is created. What is more, repeated edge weights are allowed because our edge comparison use also nodes, when weights are equal.

    from edges import Edge
    from unionfind import UnionFind

    class BoruvkaMST:
        """Boruvka's algorithm for finding MST."""

        def __init__(self, graph):
            """The algorithm initialization."""
            self.graph = graph
            self.mst = graph.__class__(graph.v()) # MST as a graph
            self.uf = UnionFind()

        def run(self):
            """Executable pseudocode."""
            for node in self.graph.iternodes():
                self.uf.create(node)
            forest = set(node for node in self.graph.iternodes())
            dummy_edge = Edge(None, None, float("inf"))
            new_len = len(forest)
            old_len = new_len + 1
            while old_len > new_len:
                old_len = new_len
                min_edges = dict(((node, dummy_edge)
                    for node in forest))
                # Finding the cheapest edges.
                for edge in self.graph.iteredges(): # O(E) time
                    source = self.uf.find(edge.source)
                    target = self.uf.find(edge.target)
                    if source != target:   # different components
                        if edge < min_edges[source]:
                            min_edges[source] = edge
                        if edge < min_edges[target]:
                            min_edges[target] = edge
                # Connecting components, total time is O(V).
                forest = set()
                for edge in min_edges.itervalues():
                    if edge is dummy_edge: # a disconnected graph
                        continue
                    source = self.uf.find(edge.source)
                    target = self.uf.find(edge.target)
                    if source != target:   # different components
                        self.uf.union(source, target)
                        forest.add(source)
                        self.mst.add_edge(edge)
                # Remove duplicates, total time is O(V).
                forest = set(self.uf.find(node) for node in forest)
                new_len = len(forest)
                if new_len == 1:   # a connected graph
                    break

We note that the Boruvka’s algorithm is well suited for parallel computation.

Prim’s algorithm
----------------

The Prim’s algorithm has the property that the edges in growing $T$ always form a single tree. The weights from $G$ can be negative [@wiki_prim]. We begin with some node $s$ from $G$ and $s$ is added to the empty $T$. Then, in each iteration, we choose a minimum-weight edge $(s,t)$, joining $s$ inside $T$ to $t$ outside $T$. Then the minimum-weight edge is added to $T$. This process is repeated until MST is formed.

The performance of Prim’s algorithm depends on how the priority queue is implemented. In the case of the first implementation a binary heap is used and it takes $O(E \log V)$ time.

    from edges import Edge
    from Queue import PriorityQueue

    class PrimMST:
        """Prim's algorithm for finding MST."""

        def __init__(self, graph):
            """The algorithm initialization."""
            self.graph = graph
            self.distance = dict((node, float("inf")) 
                for node in self.graph.iternodes())
            self.parent = dict((node, None) 
                for node in self.graph.iternodes()) # MST as a dict
            self.in_queue = dict((node, True) 
                for node in self.graph.iternodes())
            self.pq = PriorityQueue()

        def run(self, source=None):
            """Executable pseudocode."""
            if source is None:   # get first random node
                source = self.graph.iternodes().next()
            self.source = source
            self.distance[source] = 0
            for node in self.graph.iternodes():
                self.pq.put((self.distance[node], node))
            while not self.pq.empty():
                _, node = self.pq.get()
                if self.in_queue[node]:
                    self.in_queue[node] = False
                else:
                    continue
                for edge in self.graph.iteroutedges(node):
                    if (self.in_queue[edge.target] and 
                        edge.weight < self.distance[edge.target]):
                            self.distance[edge.target] = edge.weight
                            self.parent[edge.target] = edge.source
                            self.pq.put((edge.weight, edge.target))

The second implementation is better for dense graphs ($|E| \sim |V|^2$), where the adjacency-matrix representation of graphs is often used. For-loop is executed $|V|$ times, finding the minimum takes $O(V)$. Therefore, the total time is $O(V^2)$.

    class PrimMatrixMST:
        """Prim's algorithm for finding MST in O(V**2) time."""

        def __init__(self, graph):
            """The algorithm initialization."""
            self.graph = graph
            self.distance = dict((node, float("inf")) 
                for node in self.graph.iternodes())
            self.parent = dict((node, None) 
                for node in self.graph.iternodes()) # MST as a dict
            self.in_queue = dict((node, True) 
                for node in self.graph.iternodes())

        def run(self, source=None):
            """Executable pseudocode."""
            if source is None:   # get first random node
                source = self.graph.iternodes().next()
            self.source = source
            self.distance[source] = 0
            for step in xrange(self.graph.v()): # |V| times
                # Find min node in the graph, O(V) time.
                node = min((node for node in self.graph.iternodes() 
                    if self.in_queue[node]), key=self.distance.get)
                self.in_queue[node] = False
                for edge in self.graph.iteroutedges(node): # O(V) time
                    if (self.in_queue[edge.target] and
                        edge.weight < self.distance[edge.target]):
                            self.distance[edge.target] = edge.weight
                            self.parent[edge.target] = edge.source

Kruskal’s algorithm
-------------------

The Kruskal’s algorithm builds the MST in forest [@1956_Kruskal], [@wiki_kruskal]. In the beginning, each node is in its own tree in forest. Then all edges are scanned in increasing weight order. If an edge connects two different trees, then the edge is added to the MST and the trees are merged. If an edge connects two nodes in the same tree, then the edge is discarded.

The presented implementation uses a priority queue in order to sort edges by weights. Components of the MST are tracked using a disjoint-set data structure (the `UnionFind` class). The total time is $O(E \log V)$.

    from unionfind import UnionFind
    from Queue import PriorityQueue

    class KruskalMST:
        """Kruskal's algorithm for finding MST."""

        def __init__(self, graph):
            """The algorithm initialization."""
            self.graph = graph
            self.mst = graph.__class__(graph.v()) # MST as a graph
            self.uf = UnionFind()
            self.pq = PriorityQueue()

        def run(self):
            """Executable pseudocode."""
            for node in self.graph.iternodes(): # O(V) time
                self.uf.create(node)
            for edge in self.graph.iteredges(): # O(E*log(V)) time
                self.pq.put((edge.weight, edge))
            while not self.pq.empty():     # |E| steps
                _, edge = self.pq.get()  # O(log(V)) time
                if (self.uf.find(edge.source) != 
                    self.uf.find(edge.target)):
                        self.uf.union(edge.source, edge.target)
                        self.mst.add_edge(edge)

The Kruskal’s algorithm is regarded as best when the edges can be sorted fast or are already sorted.

Single-source shortest path problem {#sec:single-source}
===================================

Let us assume that $G=(V,E)$ is a weighted directed graph. The *shortest path problem* is the problem of finding a path between two nodes in $G$ such that the path weight is minimized [@wiki_shortest_path]. The negative edge weights are allowed but the negative weight cycles are forbidden (in that case there is no shortest path).

The Dijkstra’s algorithm and the Bellman-Ford are based on the principle of *relaxation*. Approximate distances (overestimates) are gradually replaced by more accurate values until the correct distances are reached. However, the details of the relaxation process differ. Many additional algorithms may be found in Ref. [@1996_Cherkassky].

Bellman-Ford algorithm
----------------------

The Bellman-Ford algorithm computes shortest paths from a single source to all of the other nodes in a weighted graph $G$ in which some of the edge weights are negative [@wiki_bellman_ford]. The algorithm relaxes all the edges and it does this $|V|-1$ times. At the last stage, negative cycles detection is performed and their existence is reported. The algorithm runs in $O(V \cdot E)$ time.

    class BellmanFord:
        """The Bellman-Ford algorithm for the shortest path problem."""

        def __init__(self, graph):
            """The algorithm initialization."""
            if not graph.is_directed():
                raise ValueError("graph is not directed")
            self.graph = graph
            self.distance = dict(((node, float("inf"))
                for node in self.graph.iternodes()))
            # Shortest path tree as a dictionary.
            self.parent = dict(((node, None)
                for node in self.graph.iternodes()))

        def run(self, source):
            """Executable pseudocode."""
            self.source = source
            self.distance[source] = 0
            for step in xrange(self.graph.v()-1):   # |V|-1 times
                for edge in self.graph.iteredges():   # O(E) time
                    self._relax(edge)
            # Check for negative cycles.
            for edge in self.graph.iteredges():   # O(E) time
                if (self.distance[edge.target] > self.distance[edge.source]
                    + edge.weight):
                        raise ValueError("negative cycle")

        def _relax(self, edge):
            """Edge relaxation."""
            alt = self.distance[edge.source] + edge.weight
            if self.distance[edge.target] > alt:
                self.distance[edge.target] = alt
                self.parent[edge.target] = edge.source
                return True
            return False

        def path(self, target):
            """Construct a path from source to target."""
            if self.source == target:
                return [self.source]
            elif self.parent[target] is None:
                raise ValueError("no path to target")
            else:
                return self.path(self.parent[target]) + [target]

Dijkstra’s algorithm
--------------------

The Dijkstra’s algorithm solves the single-source shortest path problem for a graph $G$ with non-negative edge weights, producing a shortest path tree $T$ [@1959_Dijkstra], [@wiki_dijkstra]. It is a greedy algorithm that starts at the source node, then it grows $T$ and spans all nodes reachable from the source. Nodes are added to $T$ in order of distance. The relaxation process is performed on outgoing edges of minimum-weight nodes. The total time is $O(E \log V)$.

    from Queue import PriorityQueue

    class Dijkstra:
        """Dijkstra's algorithm for the shortest path problem."""

        def __init__(self, graph):
            """The algorithm initialization."""
            if not graph.is_directed():
                raise ValueError("graph is not directed")
            self.graph = graph
            self.distance = dict((node, float("inf")) 
                for node in self.graph.iternodes())
            self.parent = dict((node, None) 
                for node in self.graph.iternodes())
            self.in_queue = dict((node, True) 
                for node in self.graph.iternodes())
            self.pq = PriorityQueue()

        def run(self, source):
            """Executable pseudocode."""
            self.source = source
            self.distance[source] = 0
            for node in self.graph.iternodes():
                self.pq.put((self.distance[node], node))
            while not self.pq.empty():
                _, node = self.pq.get()
                if self.in_queue[node]:
                    self.in_queue[node] = False
                else:
                    continue
                for edge in self.graph.iteroutedges(node):
                    if self.in_queue[edge.target] and self._relax(edge):
                        self.pq.put((self.distance[edge.target],
                            edge.target))

The second implementation is better for dense graphs with the adjacency-matrix representation, the total time is $O(V^2)$.

    class DijkstraMatrix:
        """Dijkstra's algorithm with O(V**2) time."""

        def __init__(self, graph):
            """The algorithm initialization."""
            if not graph.is_directed():
                raise ValueError("graph is not directed")
            self.graph = graph
            self.distance = dict((node, float("inf"))
                for node in self.graph.iternodes())
            self.parent = dict((node, None)
                for node in self.graph.iternodes())
            self.in_queue = dict((node, True)
                for node in self.graph.iternodes())

        def run(self, source):
            """Executable pseudocode."""
            self.source = source
            self.distance[source] = 0
            for step in xrange(self.graph.v()):   # |V| times
                # Find min node, O(V) time.
                node = min((node for node in self.graph.iternodes() 
                    if self.in_queue[node]), key=self.distance.get)
                self.in_queue[node] = False
                for edge in self.graph.iteroutedges(node): # O(V) time
                    if self.in_queue[edge.target]:
                        self._relax(edge)

Le us note that a time of $O(E+V \log V)$ is best possible for Dijkstra’s algorithm, if edge weights are real numbers and only binary comparisons are used. This bond is attainable using Fibonacci heaps, relaxed heaps or Vheaps [@1990_Ahuja]. Researchers are also working on adaptive algorithms which profit from graph easiness (small density, not many cycles).

DAG Shortest Path
-----------------

Shortest paths in DAG are always defined because there are no cycles. The algorithm runs in $O(V+E)$ time because topological sorting of graph nodes is conducted [@CLRS].

    from topsort import TopologicalSort

    class DAGShortestPath:
        """The shortest path problem for DAG."""

        def __init__(self, graph):
            """The algorithm initialization."""
            if not graph.is_directed():
                raise ValueError("graph is not directed")
            self.graph = graph
            self.distance = dict((node, float("inf"))
                for node in self.graph.iternodes())
            self.parent = dict((node, None)
                for node in self.graph.iternodes())

        def run(self, source):
            """Executable pseudocode."""
            self.source = source
            self.distance[source] = 0
            algorithm = TopologicalSort(self.graph)
            algorithm.run()
            for source in algorithm.sorted_nodes:
                for edge in self.graph.iteroutedges(source):
                    self._relax(edge)

All-pairs shortest path problem {#sec:all-pairs}
===============================

Let us assume that $G=(V,E)$ is a weighted directed graph. The *all-pairs shortest path problem* is the problem of finding shortest paths between every pair of nodes [@wiki_shortest_path]. Two algorithms solving this problem are shown: the Floyd-Warshall algorithm and the Johnson’s algorithm. There is the third algorithm in our repository, which is based on matrix multiplication (the `allpairs.py` module) [@CLRS]. A basic version has a running time of $O(V^4)$ (the `SlowAllPairs` class), but it is improved to $O(V^3 \log V)$ (the `FasterAllPairs` class).

Floyd-Warshall algorithm
------------------------

The Floyd-Warshall algorithm computes shortest paths in a weighted graph with positive or negative edge weights, but with no negative cycles [@wiki_floyd_warshal]. The algorithm uses a method of dynamic programming. The shortest path from $s$ to $t$ without intermediate nodes has the length $w(s,t)$. The shortest paths estimates are incrementally improved using a growing set of intermediate nodes. $O(V^3)$ comparisons are needed to solve the problem. Three nested for-loops are the heart of the algorithm.

Our implementation allows the reconstruction of the path between any two endpoint nodes. The shortest-path tree for each node is calculated and $O(V^2)$ memory is used. The diagonal of the path matrix is inspected and the presence of negative numbers indicates negative cycles.

    class FloydWarshallPaths:
        """The Floyd-Warshall algorithm with path reconstruction."""

        def __init__(self, graph):
            """The algorithm initialization."""
            if not graph.is_directed():
                raise ValueError("graph is not directed")
            self.graph = graph
            self.distance = dict()
            self.parent = dict()
            for source in self.graph.iternodes():
                self.distance[source] = dict()
                self.parent[source] = dict()
                for target in self.graph.iternodes():
                    self.distance[source][target] = float("inf")
                    self.parent[source][target] = None
                self.distance[source][source] = 0
            for edge in self.graph.iteredges():
                self.distance[edge.source][edge.target] = edge.weight
                self.parent[edge.source][edge.target] = edge.source

        def run(self):
            """Executable pseudocode."""
            for node in self.graph.iternodes():
                for source in self.graph.iternodes():
                    for target in self.graph.iternodes():
                        alt = self.distance[source][node] + \
                            self.distance[node][target]
                        if alt < self.distance[source][target]:
                            self.distance[source][target] = alt
                            self.parent[source][target] = \
                                self.parent[node][target]
            if any(self.distance[node][node] < 0
            for node in self.graph.iternodes()):
                raise ValueError("negative cycle detected")

        def path(self, source, target):
            """Path reconstruction."""
            if source == target:
                return [source]
            elif self.parent[source][target] is None:
                raise ValueError("no path to target")
            else:
                return self.path(source, self.parent[target]) + [target]

Johnson’s algorithm
-------------------

The Johnson’s algorithm finds the shortest paths between all pairs of nodes in a sparse directed graph. The algorithm uses the technique of *reweighting*. It works by using the Bellman-Ford algorithm to compute a transformation of the input graph that removes all negative weights, allowing Dijkstra’s algorithm to be used on the transformed graph [@wiki_johnson]. The time complexity of our implementation is $O(V E \log V)$, because the binary min-heap is used in the Dijkstra’s algorithm. It is asymptotically faster than the Floyd-Warshall algorithm if the graph is sparse.

    from edges import Edge
    from bellmanford import BellmanFord
    from dijkstra import Dijkstra

    class Johnson:
        """The Johnson algorithm for the shortest path problem."""

        def __init__(self, graph):
            """The algorithm initialization."""
            if not graph.is_directed():
                raise ValueError("graph is not directed")
            self.graph = graph

        def run(self):
            """Executable pseudocode."""
            self.new_graph = self.graph.__class__(
                self.graph.v() + 1, directed=True)
            for node in self.graph.iternodes():   # O(V) time
                self.new_graph.add_node(node)
            for edge in self.graph.iteredges():   # O(E) time
                self.new_graph.add_edge(edge)
            self.new_node = self.graph.v()
            self.new_graph.add_node(self.new_node)
            for node in self.graph.iternodes():   # O(V) time
                self.new_graph.add_edge(Edge(self.new_node, node, 0))
            self.bf = BellmanFord(self.new_graph)
            # If this step detects a negative cycle,
            # the algorithm is terminated.
            self.bf.run(self.new_node)   # O(V*E) time
            # Edges are reweighted.
            for edge in list(self.new_graph.iteredges()):   # O(E) time
                edge.weight = (edge.weight 
                    + self.bf.distance[edge.source]
                    - self.bf.distance[edge.target])
                self.new_graph.del_edge(edge)
                self.new_graph.add_edge(edge)
            # Remove new_node with edges.
            self.new_graph.del_node(self.new_node)
            # Weights are now modified!
            self.distance = dict()
            for source in self.graph.iternodes():
                self.distance[source] = dict()
                algorithm = Dijkstra(self.new_graph)
                algorithm.run(source)
                for target in self.graph.iternodes():
                    self.distance[source][target] = (
                        algorithm.distance[target]
                        - self.bf.distance[source] 
                        + self.bf.distance[target])

Conclusions {#sec:conclusions}
===========

In this paper, we presented Python implementation of several weighted graph algorithms. The algorithms are represented by classes where graph objects are processed via proposed graph interface. The presented implementation is unique in several ways. The source code is readable like a pseudocode from textbooks or scientific articles. On the other hand, the code can be executed with efficiency established by the corresponding theory. Python’s class mechanism adds classes with minimum of new syntax and it is easy to create desired data structures (e.g., an edge, a graph, a union-find data structure) or to use objects from standard modules (e.g., queues, stacks).

The source code is available from the public GitHub repository [@graph-dict]. It can be used in education, scientific research, or as a starting point for implementations in other programming languages. The number of available algorithms is growing, let us list some of them:

-   Graph traversal (breadth-first search, depth-first search)

-   Connectivity (connected components, strongly connected components)

-   Accessibility (transitive closure)

-   Topological sorting

-   Cycle detection

-   Testing bipartiteness

-   Minimum spanning tree

-   Matching (Augmenting path algorithm, Hopcroft-Karp algorithm)

-   Shortest path search

-   Maximum flow (Ford-Fulkerson algorithm, Edmonds-Karp algorithm)

-   Graph generators

[99]{}

Python Programming Language,\
<http://www.python.org/>.

E. Shein, *Python for Beginners*, Communications of the ACM 58, 19-21 (2015),\
<http://cacm.acm.org/magazines/2015/3/183588-python-for-beginners/fulltext>.

J. M. Perkel, *Programming: Pick up Python*, Nature 518, 125-126 (2015).

A. Kapanowski, *Python for education: permutations*, The Python Papers 9, 3 (2014),\
<http://ojs.pythonpapers.org/index.php/tpp/article/view/258>.

A. Kapanowski, *graph-dict*, GitHub repository, 2015,\
<https://github.com/ufkapano/graphs-dict/>.

E. C. Hayden, *Rule rewrite aims to clean up scientific software*, Nature 520, 276-277 (2015).

Wikipedia, Graph theory, 2015,\
<https://en.wikipedia.org/wiki/Graph_theory>.

PyPI - the Python Package Index,\
<https://pypi.python.org/pypi>.

Boost C++ Libraries, 2015,\
<http://www.boost.org/>.

The Boost Graph Library (BGL), 2015,\
<http://www.boost.org/doc/libs/1_58_0/libs/graph/doc/index.html>.

Algorithmic Solutions Software GMBH, LEDA C++ library, 2015,\
<http://www.algorithmic-solutions.com/leda/index.htm>.

K. Mehlhorn and St. Näher, *The LEDA Platform of Combinatorial and Geometric Computing*, Cambridge University Press, 1999.

The Graph Template Library (GTL), GitHub repository, 2015,\
<https://github.com/rdmpage/graph-template-library/>.

A. Hagberg, D. A. Schult, and P. J. Swart, *NetworkX*,\
<http://networkx.github.io/>.

W. Stein, *Sage: Open Source Mathematical Software (Version 6.2)*, The Sage Group, 2014, <http://www.sagemath.org/>.

*igraph* - The network analysis package, 2015,\
<http://igraph.org/python/>.

P. Matiello, *python-graph*, GitHub repository, 2015,\
<https://github.com/pmatiello/python-graph/>.

R. Dick and K. Gaitanis, The *graph* package, 2005,\
<http://robertdick.org/python/mods.html>.

T. H. Cormen, C. E. Leiserson, R. L. Rivest, and C. Stein, *Introduction to Algorithms, third edition*, The MIT Press, Cambridge, London, 2009.

Wikipedia, Spanning tree, 2015,\
<https://en.wikipedia.org/wiki/Spanning_tree>.

Wikipedia, Combinatorial map, 2015,\
<https://en.wikipedia.org/wiki/Combinatorial_map>.

Wikipedia, Minimum spanning tree, 2015,\
<https://en.wikipedia.org/wiki/Minimum_spanning_tree>.

Wikipedia, Boruvka’s algorithm, 2015,\
<http://en.wikipedia.org/wiki/Boruvka's_algorithm>.

Wikipedia, Prim’s algorithm, 2015,\
<http://en.wikipedia.org/wiki/Prim's_algorithm>.

J. B. Kruskal, *On the shortest spanning subtree of a graph and the traveling salesman problem*, Proc. Amer. Math. Soc. 7, 48-50 (1956).

Wikipedia, Kruskal’s algorithm, 2015,\
<http://en.wikipedia.org/wiki/Kruskal's_algorithm>.

Wikipedia, Shortest path problem, 2015,\
<http://en.wikipedia.org/wiki/Shortest_path>.

B. V.Cherkassky, A. V. Goldberg, and T. Radzik, *Shortest paths algorithms: theory and experimental evaluation*, Mathematical Programming 73, 129-174 (1996).

Wikipedia, Bellman-Ford algorithm, 2015,\
<http://en.wikipedia.org/wiki/Bellman-Ford_algorithm>.

E. W. Dijkstra, *A Note on Two Problems in Connexion with Graphs*, Numerische Mathematik 1, 269-271 (1959).

Wikipedia, Dijkstra’s algorithm, 2015,\
<http://en.wikipedia.org/wiki/Dijkstra's_algorithm>.

R. K. Ahuja, K. Mehlhorn, J. B. Orlin, and R. E. Trajan, *Faster Algorithms for the Shortest Path Problem*, Journal of the ACM 37, 213-223 (1990).

Wikipedia, Floyd-Warshall algorithm, 2015,\
<http://en.wikipedia.org/wiki/Floyd-Warshall_algorithm>.

Wikipedia, Johnson’s algorithm, 2015,\
<http://en.wikipedia.org/wiki/Johnson's_algorithm>.
