\(\) \(%\newcommand{\CLRS}{\href{https://mitpress.mit.edu/books/introduction-algorithms}{Cormen et al}}\) \(\newcommand{\CLRS}{\href{https://mitpress.mit.edu/books/introduction-algorithms}{Introduction to Algorithms, 3rd ed.} (\href{http://libproxy.aalto.fi/login?url=http://site.ebrary.com/lib/aalto/Doc?id=10397652}{online via Aalto lib})}\) \(\newcommand{\SW}{\href{http://algs4.cs.princeton.edu/home/}{Algorithms, 4th ed.}}\) \(%\newcommand{\SW}{\href{http://algs4.cs.princeton.edu/home/}{Sedgewick and Wayne}}\) \(\) \(\newcommand{\Java}{\href{http://java.com/en/}{Java}}\) \(\newcommand{\Python}{\href{https://www.python.org/}{Python}}\) \(\newcommand{\CPP}{\href{http://www.cplusplus.com/}{C++}}\) \(\newcommand{\ST}[1]{{\Blue{\textsf{#1}}}}\) \(\newcommand{\PseudoCode}[1]{{\color{blue}\textsf{#1}}}\) \(\) \(%\newcommand{\subheading}[1]{\textbf{\large\color{aaltodgreen}#1}}\) \(\newcommand{\subheading}[1]{\large{\usebeamercolor[fg]{frametitle} #1}}\) \(\) \(\newcommand{\Blue}[1]{{\color{flagblue}#1}}\) \(\newcommand{\Red}[1]{{\color{aaltored}#1}}\) \(\newcommand{\Emph}[1]{\emph{\color{flagblue}#1}}\) \(\) \(\newcommand{\Engl}[1]{({\em engl.}\ #1)}\) \(\) \(\newcommand{\Pointer}{\raisebox{-1ex}{\huge\ding{43}}\ }\) \(\) \(\newcommand{\Set}[1]{\{#1\}}\) \(\newcommand{\Setdef}[2]{\{{#1}\mid{#2}\}}\) \(\newcommand{\PSet}[1]{\mathcal{P}(#1)}\) \(\newcommand{\Card}[1]{{\vert{#1}\vert}}\) \(\newcommand{\Tuple}[1]{(#1)}\) \(\newcommand{\Implies}{\Rightarrow}\) \(\newcommand{\Reals}{\mathbb{R}}\) \(\newcommand{\Seq}[1]{(#1)}\) \(\newcommand{\Arr}[1]{[#1]}\) \(\newcommand{\Floor}[1]{{\lfloor{#1}\rfloor}}\) \(\newcommand{\Ceil}[1]{{\lceil{#1}\rceil}}\) \(\newcommand{\Path}[1]{(#1)}\) \(\) \(%\newcommand{\Lg}{\lg}\) \(\newcommand{\Lg}{\log_2}\) \(\) \(\newcommand{\BigOh}{O}\) \(%\newcommand{\BigOh}{\mathcal{O}}\) \(\newcommand{\Oh}[1]{\BigOh(#1)}\) \(\) \(\newcommand{\todo}[1]{\Red{\textbf{TO DO: #1}}}\) \(\) \(\newcommand{\NULL}{\textsf{null}}\) \(\) \(\newcommand{\Insert}{\ensuremath{\textsc{insert}}}\) \(\newcommand{\Search}{\ensuremath{\textsc{search}}}\) \(\newcommand{\Delete}{\ensuremath{\textsc{delete}}}\) \(\newcommand{\Remove}{\ensuremath{\textsc{remove}}}\) \(\) \(\newcommand{\Parent}[1]{\mathop{parent}(#1)}\) \(\) \(\newcommand{\ALengthOf}[1]{{#1}.\textit{length}}\) \(\) \(\newcommand{\TRootOf}[1]{{#1}.\textit{root}}\) \(\newcommand{\TLChildOf}[1]{{#1}.\textit{leftChild}}\) \(\newcommand{\TRChildOf}[1]{{#1}.\textit{rightChild}}\) \(\newcommand{\TNode}{x}\) \(\newcommand{\TNodeI}{y}\) \(\newcommand{\TKeyOf}[1]{{#1}.\textit{key}}\) \(\) \(\newcommand{\PEnqueue}[2]{{#1}.\textsf{enqueue}(#2)}\) \(\newcommand{\PDequeue}[1]{{#1}.\textsf{dequeue}()}\) \(\) \(\newcommand{\Def}{\mathrel{:=}}\) \(\) \(\newcommand{\Eq}{\mathrel{=}}\) \(\newcommand{\Asgn}{\mathrel{\leftarrow}}\) \(%\newcommand{\Asgn}{\mathrel{:=}}\) \(\) \(%\) \(% Heaps\) \(%\) \(\newcommand{\Downheap}{\textsc{downheap}}\) \(\newcommand{\Upheap}{\textsc{upheap}}\) \(\newcommand{\Makeheap}{\textsc{makeheap}}\) \(\) \(%\) \(% Dynamic sets\) \(%\) \(\newcommand{\SInsert}[1]{\textsc{insert}(#1)}\) \(\newcommand{\SSearch}[1]{\textsc{search}(#1)}\) \(\newcommand{\SDelete}[1]{\textsc{delete}(#1)}\) \(\newcommand{\SMin}{\textsc{min}()}\) \(\newcommand{\SMax}{\textsc{max}()}\) \(\newcommand{\SPredecessor}[1]{\textsc{predecessor}(#1)}\) \(\newcommand{\SSuccessor}[1]{\textsc{successor}(#1)}\) \(\) \(%\) \(% Union-find\) \(%\) \(\newcommand{\UFMS}[1]{\textsc{make-set}(#1)}\) \(\newcommand{\UFFS}[1]{\textsc{find-set}(#1)}\) \(\newcommand{\UFCompress}[1]{\textsc{find-and-compress}(#1)}\) \(\newcommand{\UFUnion}[2]{\textsc{union}(#1,#2)}\) \(\) \(\) \(%\) \(% Graphs\) \(%\) \(\newcommand{\Verts}{V}\) \(\newcommand{\Vtx}{v}\) \(\newcommand{\VtxA}{v_1}\) \(\newcommand{\VtxB}{v_2}\) \(\newcommand{\VertsA}{V_\textup{A}}\) \(\newcommand{\VertsB}{V_\textup{B}}\) \(\newcommand{\Edges}{E}\) \(\newcommand{\Edge}{e}\) \(\newcommand{\NofV}{\Card{V}}\) \(\newcommand{\NofE}{\Card{E}}\) \(\newcommand{\Graph}{G}\) \(\) \(\newcommand{\SCC}{C}\) \(\newcommand{\GraphSCC}{G^\text{SCC}}\) \(\newcommand{\VertsSCC}{V^\text{SCC}}\) \(\newcommand{\EdgesSCC}{E^\text{SCC}}\) \(\) \(\newcommand{\GraphT}{G^\text{T}}\) \(%\newcommand{\VertsT}{V^\textup{T}}\) \(\newcommand{\EdgesT}{E^\text{T}}\) \(\) \(%\) \(% NP-completeness etc\) \(%\) \(\newcommand{\Poly}{\textbf{P}}\) \(\newcommand{\NP}{\textbf{NP}}\) \(\newcommand{\PSPACE}{\textbf{PSPACE}}\) \(\newcommand{\EXPTIME}{\textbf{EXPTIME}}\)

Representations

In order to reason about graphs with algorithms, one has to represent graphs in the memory of a computer. First, observe that an undirected graph with \( n \) vertices can have at most \( \frac{n(n-1)}{2} \) edges, and a directed graph can have at most \( n^2 \) edges. We say that a graph is sparse if its number of edges is small compared to this maximum number of possible edges, otherwise it is dense. Sparsity is not an exactly defined concept as there is no exact definition for the phrase “small compared to” above.

In the following, we give the two common representations for graphs:

  • ​ adjacency matrices, which are mainly used for dense graphs, and

  • ​ adjacency lists, the most commonly used representation form.

In both of these representations, the \( n \) vertices in the graph are usually assumed to be the integers \( \Set{0,1,…,n-1} \) so that indexing is easy. If more complex vertex names such as strings are to be used, one can maintain a mapping from these to small integers so that the small integers can be used inside the graph algorithms. In the examples, we use symbolic names such as “\( a \)” for better human readability.

Adjacency matrices

The adjacency matrix of a directed graph \( \Graph=\Tuple{\Set{0,1,…,n-1},\Edges} \) is a \( n \times n \) binary matrix \( A = (a_{u,v}) \) such that the entry \( a_{u,v} \) is \( 1 \) if \( \Tuple{u,v}\in\Edges \) and \( 0 \) otherwise. The matrix is easy to store in a row-by-row bit-vector of length \( n^2 \) (an array with \( \Ceil{\frac{n^2}{32}} \) 32-bit integers). In such bit-vector, the entry \( a_{u,v} \) can be found at the index \( un+v \).

Example: A directed graph and its adjacency matrix representation

Consider the directed graph shown below.

_images/digraph-ex1.png

Its adjacency matrix representation is $$\begin{array}{c|ccccccc} & a & b & c & d & e & f & g\\ \hline a & 0 & 1 & 0 & 0 & 1 & 0 & 0\\ b & 0 & 1 & 0 & 0 & 0 & 1 & 0\\ c & 1 & 0 & 0 & 1 & 0 & 0 & 1\\ d & 0 & 0 & 0 & 0 & 0 & 0 & 1\\ e & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ f & 1 & 0 & 0 & 0 & 0 & 0 & 0\\ g & 0 & 0 & 0 & 1 & 0 & 0 & 0 \end{array}$$ As a bit-vector, the matrix is \( 0100100 0100010 1001001 0000001 0000010 1000000 0001000 \).

The adjacency matrix of an undirected graph \( \Graph=\Tuple{\Set{0,1,…,n-1},\Edges} \) is simply the upper right half of the matrix: with \( 0 \le u < v < n \), the entry \( a_{u,v} \) is \( 1 \) if \( \Set{u,v}\in\Edges \) and \( 0 \) otherwise. If the matrix is presented as a row-by-row bit-vector, then the entry \( a_{u,v} \) can be found at the index \( un-\frac{u(u+1)}{2}+(v-u-1) \).

Example: An undirected graph and its adjacency matrix representation

_images/graph-ex1.png

$$\begin{array}{c|ccccccc} & a & b & c & d & e & f & g\\ \hline a & & 1 & 1 & 0 & 1 & 1 & 0\\ b & & & 0 & 0 & 0 & 1 & 0\\ c & & & & 1 & 0 & 0 & 1\\ d & & & & & 0 & 0 & 1\\ e & & & & & & 1 & 0\\ f & & & & & & & 0\\ g & & & & & & & \\ \end{array}$$ As a bit vector, the matrix is \( 110110000101001001100 \) and the bit for the edge \( \Set{c,g} \) can be found at the index \( 2 \times 7 - \frac{2(2+1)}{2} + (6-2-1) = 14 - 3 + 3 = 14 \) under the mapping \( \left(\begin{smallmatrix}a&b&c&d&e&f&g\\0&1&2&3&4&5&6\end{smallmatrix}\right) \).

Some analysis:

  • ​ Good for dense (and smallish) graphs.

  • ​ Adding or removing an edge takes constant time.

  • ​ Adding a new vertex is expensive as the whole matrix has to be expanded.

  • ​ Graphs with many vertices take a lot of memory: the adjacency matrix for a directed graph with 1 million vertices would take over 100 gigabytes of memory.

  • ​ Traversing the neighbours of a vertex is not very optimal if the degree of the vertex is low compared to the number \( n \) of all vertices as one has to go through all the \( n/8 \) bytes making the row.

Adjacency lists

In the adjacency list representation, each vertex is associated to a list of its neighbours. Naturally, a resizable array or a mutable set could be used instead of a list as well.

Example: A directed graph and its adjacency list representation

Consider the following directed graph.

_images/digraph-ex1.png

An adjacency list representation for it is shown below.

_images/digraph-ex1-alist.png

Analysis:

  • ​ Compact for sparse graphs: memory use is \( \Theta(\Card{\Verts}+\Card{\Edges}) \) instead of \( \Theta(\Card{\Verts}^2) \) use by the adjacency matrices.

  • ​ Adding a new vertex is an amortized constant-time operation when dynamic arrays are used.

  • ​ Adding and removing edges takes linear time in the degree of the vertex involved in the worst case.

  • ​ Iterating over the neighbours of a vertex is easy.

The adjacency list representation is use more commonly than the adjacency matrix representation because in many applications the graphs tend to be both large and sparse.

Example: A directed graph and its adjacency list representation in a more compact form

It is also possible to represent the adjacency lists in an even more compact form by concatenating the neighbour lists into one array and indexing it with another array. As a concrete example, consider the directed graph

_images/digraph-ex1a.png

and the following representation

_images/digraph-ex1-alist2.png

Now the vertices are \( \Set{0,1,…,6} \) and the neighbours of a vertex \( u \) can be found in the sub-array edges[start[\( u \)]..start[\( u \)+1]-1] or edges[start[\( u \)]..edges.length-1] for the last vertex \( u=6 \).

In this representation method, the two arrays contain \( \Card{\Verts}+\Card{\Edges} \) integers in total. Of course, the drawback is that adding new vertices and edges is now more time consuming. Thus this representation should only be used when the graph is not modified often.

Representing vertex attributes

In many graph algorithms, we need to associate some data attributes to the vertices. There are multiple ways to achieve this but as/when the vertices are \( \Set{0,1,…,n-1} \), the easiest way is to have an array of \( n \) elements holding the attribute(s). From now on, we assume that accessing and setting a vertex attribute can be done in constant time.