See also slides

Node as information and edges as relationship between data

Directed acyclic graph (DAG)

Application: Merkle DAG

undirected.

  • : set of vertices
  • : set of undirected edges:

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:

See also Graph isomorphism

directed.

  • : set of vertices
  • : set of edges containing node pairs:

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.

matrix representation

Let be a directed graph with

Let matrix

For every pairs of nodes set

The adjacency list representation

Important

Let be an array of bags For every edge add to to bag

opscomplexity
add/remove nodes (copy array)
add/remove edges (adding to bag)
check an edge exists (searching bags)
iterate over all incoming edges of (scan all bags)
iterate over all outgoing edges of (scan a bag)
Check or change the weight of

comparison.

Dense graph: Sparse graph:

Traversing undirected graph.

Depth-first search (DFS)

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

Conclusion

  • all nodes to which is connected
  • is not a connected graph
  • The order of recursive call determines all is connected to.

Complexity

  • memory for marked and at-most recursive calls
  • inspect each node at-most once and traverse each edge once:

Connected-components

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

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.

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

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)

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 , find a shortest path from node to all nodes can reach.

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