See also slides

Node as information and edges as relationship between data

Directed acyclic graph (DAG)

Application: Merkle DAG

undirected.

(N,E)(\mathcal{N}, \mathcal{E})

  • N\mathcal{N}: set of vertices
  • E\mathcal{E}: set of undirected edges: EN×N\mathcal{E} \subseteq \mathcal{N} \times \mathcal{N}

path is a sequence of nodes and edges connect two nodes.

A graph is connected if there is a path between every pair of vertices.

In a weight undirected graph each edge has a weight: w:ERw: \mathcal{E} \to \mathbb{R}

See also Graph isomorphism

directed.

(N,E)(\mathcal{N}, \mathcal{E})

  • N\mathcal{N}: set of vertices
  • E\mathcal{E}: set of edges containing node pairs: EN×N\mathcal{E} \subseteq \mathcal{N} \times \mathcal{N}

path difference

sequence of nodes and edges are directional, edges are ordered pairs.

cycle

path with at-least one edge from a node

Strongly component: maximal sub-graph in which all node pairs are strongly connected.

Let G=(N,E)\mathcal{G} = (\mathcal{N}, \mathcal{E}) be a directed graph with nNid(n) with 0id(n)Nn \in \mathcal{N} \land id(n) \text{ with } 0 \leq id(n) \leq |\mathcal{N}|

Let M=N×NM = | \mathcal{N} | \times | \mathcal{N} | matrix

For every pairs of nodes (m,n)(m, n) set M[id(m),id(n)](m,n)EM[id(m), id(n)] \coloneqq (m, n) \in \mathcal{E}

The adjacency list representation

Important

Let A[0NA \lbrack 0 \dots |\mathcal{N}| be an array of bags For every edge (m,nE)(m, n \in \mathcal{E}) add nn to (m,n)(m,n) to bag A[id(m)]A \lbrack id(m) \rbrack

opscomplexity
add/remove nodesΘ(N)\Theta(\|\mathcal{N}\|) (copy array)
add/remove edges (n,m)(n, m)Θ(out(n))\Theta(\|out(n)\|) (adding to bag)
check an edge (n,m)(n, m) existsΘ(out(n))\Theta(\|out(n)\|) (searching bags)
iterate over all incoming edges of nnΘ(E)\Theta(\|\mathcal{E}\|) (scan all bags)
iterate over all outgoing edges of nnΘ(out(n))\Theta(\|out(n)\|) (scan a bag)
Check or change the weight of (n,m)(n, m)Θ(1)\Theta(1)

comparison.

Dense graph: EΘ(N2)|\mathcal{E}| \approx \Theta(|\mathcal{N}|^2) > Sparse graph: EΘ(N)|\mathcal{E}| \approx \Theta(|\mathcal{N}|)

Traversing undirected graph.

Depth-first search (DFS)

"\\begin{algorithm}\n\\caption{DFS-R(G, marked, n)}\n\\begin{algorithmic}\n\\REQUIRE $G = (\\mathcal{N}, \\mathcal{E}), \\text{marked}, n \\in \\mathcal{N}$\n\\FORALL{$ (n, m) \\in \\mathcal{E} $}\n \\IF{$\\neq \\text{marked}[m]$}\n \\STATE $\\text{marked}[m] \\coloneqq \\text{true}$\n \\STATE $\\text{DFS-R}{(G, \\text{marked}, m)}$\n \\ENDIF\n\\ENDFOR\n\\end{algorithmic}\n\\end{algorithm}"

Algorithm 1 DFS-R(G, marked, n)

Require: G=(N,E),marked,nNG = (\mathcal{N}, \mathcal{E}), \text{marked}, n \in \mathcal{N}

1:for all (n,m)E (n, m) \in \mathcal{E} do

2:if marked[m]\neq \text{marked}[m] then

3:marked[m]true\text{marked}[m] \coloneqq \text{true}

4:DFS-R(G,marked,m)\text{DFS-R}{(G, \text{marked}, m)}

5:end if

6:end for

marked{n(ns)nN}\text{marked} \coloneqq \lbrace n \longmapsto (n \neq s) \mid n \in \mathcal{N} \rbrace

Conclusion

  • all nodes to which n3n_3 is connected
  • G\mathcal{G} is not a connected graph
  • The order of recursive call determines all n3n_3 is connected to.

Complexity

  • N|\mathcal{N}| memory for marked and at-most N|\mathcal{N}| recursive calls
  • inspect each node at-most once and traverse each edge once: Θ(N+E)\Theta(|\mathcal{N}| + |\mathcal{E}|)

Connected-components

"\\begin{algorithm}\n\\caption{DFS-CC-R(G, cc, n)}\n\\begin{algorithmic}\n\\REQUIRE $G = (\\mathcal{N}, \\mathcal{E}), cc, n \\in \\mathcal{N}$\n\\FORALL{$(n, m) \\in \\mathcal{E}$}\n \\IF{$cc[m] = \\text{unmarked}$}\n \\STATE $cc[m] \\coloneqq cc[n]$\n \\STATE $\\text{DFS-CC-R}(G, cc, m)$\n \\ENDIF\n\\ENDFOR\n\\end{algorithmic}\n\\end{algorithm}"

Algorithm 2 DFS-CC-R(G, cc, n)

Require: G=(N,E),cc,nNG = (\mathcal{N}, \mathcal{E}), cc, n \in \mathcal{N}

1:for all (n,m)E(n, m) \in \mathcal{E} do

2:if cc[m]=unmarkedcc[m] = \text{unmarked} then

3:cc[m]cc[n]cc[m] \coloneqq cc[n]

4:DFS-CC-R(G,cc,m)\text{DFS-CC-R}(G, cc, m)

5:end if

6:end for

"\\begin{algorithm}\n\\caption{COMPONENTS(G, s)}\n\\begin{algorithmic}\n\\REQUIRE $G = (\\mathcal{N}, \\mathcal{E}), s \\in \\mathcal{N}$\n\\STATE $cc \\coloneqq \\{ n \\mapsto \\text{unmarked} \\}$\n\\FORALL{$n \\in \\mathcal{N}$}\n \\IF{$cc[n] = \\text{unmarked}$}\n \\STATE $cc[n] \\coloneqq n$\n \\STATE $\\text{DFS-CC-R}(G, cc, n)$\n \\ENDIF\n\\ENDFOR\n\\end{algorithmic}\n\\end{algorithm}"

Algorithm 3 COMPONENTS(G, s)

Require: G=(N,E),sNG = (\mathcal{N}, \mathcal{E}), s \in \mathcal{N}

1:cc{nunmarked}cc \coloneqq \{ n \mapsto \text{unmarked} \}

2:for all nNn \in \mathcal{N} do

3:if cc[n]=unmarkedcc[n] = \text{unmarked} then

4:cc[n]ncc[n] \coloneqq n

5:DFS-CC-R(G,cc,n)\text{DFS-CC-R}(G, cc, n)

6:end if

7:end for

Two-colourability

Bipartite graph

A graph is bipartite if we can partition the nodes in two sets such that no two nodes in the same set share an edge.

"\\begin{algorithm}\n\\caption{DFS-TC-R(G, colors, n)}\n\\begin{algorithmic}\n\\REQUIRE $G = (\\mathcal{N}, \\mathcal{E}), \\text{colors}, n \\in \\mathcal{N}$\n\\FORALL{$(n, m) \\in \\mathcal{E}$}\n \\IF{$\\text{colors}[m] = 0$}\n \\STATE $\\text{colors}[m] \\coloneqq -\\text{colors}[n]$\n \\STATE $\\text{DFS-TC-R}(G, \\text{colors}, m)$\n \\ELSIF{$\\text{colors}[m] = \\text{colors}[n]$}\n \\STATE \\textbf{print} \"This graph is not bipartite.\"\n \\ENDIF\n\\ENDFOR\n\\end{algorithmic}\n\\end{algorithm}"

Algorithm 4 DFS-TC-R(G, colors, n)

Require: G=(N,E),colors,nNG = (\mathcal{N}, \mathcal{E}), \text{colors}, n \in \mathcal{N}

1:for all (n,m)E(n, m) \in \mathcal{E} do

2:if colors[m]=0\text{colors}[m] = 0 then

3:colors[m]colors[n]\text{colors}[m] \coloneqq -\text{colors}[n]

4:DFS-TC-R(G,colors,m)\text{DFS-TC-R}(G, \text{colors}, m)

5:else if colors[m]=colors[n]\text{colors}[m] = \text{colors}[n] then

6:print "This graph is not bipartite."

7:end if

8:end for

"\\begin{algorithm}\n\\caption{TwoColors(G)}\n\\begin{algorithmic}\n\\REQUIRE $G = (\\mathcal{N}, \\mathcal{E})$\n\\STATE $\\text{colors} \\coloneqq \\{ n \\mapsto 0 \\mid n \\in \\mathcal{N} \\}$\n\\FORALL{$n \\in \\mathcal{N}$}\n \\IF{$\\text{colors}[n] = 0$}\n \\STATE $\\text{colors}[n] \\coloneqq 1$\n \\STATE $\\text{DFS-TC-R}(G, \\text{colors}, n)$\n \\ENDIF\n\\ENDFOR\n\\end{algorithmic}\n\\end{algorithm}"

Algorithm 5 TwoColors(G)

Require: G=(N,E)G = (\mathcal{N}, \mathcal{E})

1:colors{n0nN}\text{colors} \coloneqq \{ n \mapsto 0 \mid n \in \mathcal{N} \}

2:for all nNn \in \mathcal{N} do

3:if colors[n]=0\text{colors}[n] = 0 then

4:colors[n]1\text{colors}[n] \coloneqq 1

5:DFS-TC-R(G,colors,n)\text{DFS-TC-R}(G, \text{colors}, n)

6:end if

7:end for

Breadth-first search (BFS)

"\\begin{algorithm}\n\\caption{BFS(G, s)}\n\\begin{algorithmic}\n\\REQUIRE $G = (\\mathcal{N}, \\mathcal{E}), s \\in \\mathcal{N}$\n\\STATE $\\text{marked} \\coloneqq \\{ n \\mapsto (n \\neq s) \\mid n \\in \\mathcal{N} \\}$\n\\STATE $Q \\coloneqq \\text{a queue holding only } s$\n\\WHILE{$\\neg\\text{Empty}(Q)$}\n \\STATE $n \\coloneqq \\text{Dequeue}(Q)$\n \\FORALL{$(n, m) \\in \\mathcal{E}$}\n \\IF{$\\neg\\text{marked}[m]$}\n \\STATE $\\text{marked}[m] \\coloneqq \\text{true}$\n \\STATE $\\text{Enqueue}(Q, m)$\n \\ENDIF\n \\ENDFOR\n\\ENDWHILE\n\\end{algorithmic}\n\\end{algorithm}"

Algorithm 6 BFS(G, s)

Require: G=(N,E),sNG = (\mathcal{N}, \mathcal{E}), s \in \mathcal{N}

1:marked{n(ns)nN}\text{marked} \coloneqq \{ n \mapsto (n \neq s) \mid n \in \mathcal{N} \}

2:Qa queue holding only sQ \coloneqq \text{a queue holding only } s

3:while ¬Empty(Q)\neg\text{Empty}(Q) do

4:nDequeue(Q)n \coloneqq \text{Dequeue}(Q)

5:for all (n,m)E(n, m) \in \mathcal{E} do

6:if ¬marked[m]\neg\text{marked}[m] then

7:marked[m]true\text{marked}[m] \coloneqq \text{true}

8:Enqueue(Q,m)\text{Enqueue}(Q, m)

9:end if

10:end for

11:end while

Single-source shortest path

Given an undirected graph without weight and node sNs \in \mathcal{N}, find a shortest path from node ss to all nodes ss can reach.

"\\begin{algorithm}\n\\caption{BFS-SSSP(G, s)}\n\\begin{algorithmic}\n\\REQUIRE $G = (\\mathcal{N}, \\mathcal{E}), s \\in \\mathcal{N}$\n\\STATE $\\text{distance} \\coloneqq \\{ n \\mapsto \\infty \\mid n \\in \\mathcal{N} \\}$\n\\STATE $\\text{distance}[s] \\coloneqq 0$\n\\STATE $Q \\coloneqq \\text{a queue holding only } s$\n\\WHILE{$\\neg\\text{Empty}(Q)$}\n \\STATE $n \\coloneqq \\text{Dequeue}(Q)$\n \\FORALL{$(n, m) \\in \\mathcal{E}$}\n \\IF{$\\text{distance}[m] = \\infty$}\n \\STATE $\\text{distance}[m] \\coloneqq \\text{distance}[n] + 1$\n \\STATE $\\text{Enqueue}(Q, m)$\n \\ENDIF\n \\ENDFOR\n\\ENDWHILE\n\\end{algorithmic}\n\\end{algorithm}"

Algorithm 7 BFS-SSSP(G, s)

Require: G=(N,E),sNG = (\mathcal{N}, \mathcal{E}), s \in \mathcal{N}

1:distance{nnN}\text{distance} \coloneqq \{ n \mapsto \infty \mid n \in \mathcal{N} \}

2:distance[s]0\text{distance}[s] \coloneqq 0

3:Qa queue holding only sQ \coloneqq \text{a queue holding only } s

4:while ¬Empty(Q)\neg\text{Empty}(Q) do

5:nDequeue(Q)n \coloneqq \text{Dequeue}(Q)

6:for all (n,m)E(n, m) \in \mathcal{E} do

7:if distance[m]=\text{distance}[m] = \infty then

8:distance[m]distance[n]+1\text{distance}[m] \coloneqq \text{distance}[n] + 1

9:Enqueue(Q,m)\text{Enqueue}(Q, m)

10:end if

11:end for

12:end while