Graphs
See also slides
Node as information and edges as relationship between data
Directed acyclic graph (DAG)
Application: Merkle DAG
undirected.
(N,E)
- N: set of vertices
- E: set of undirected edges: E⊆N×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:E→R
See also Graph isomorphism
directed.
(N,E)
- N: set of vertices
- E: set of edges containing node pairs: E⊆N×N
sequence of nodes and edges are directional, edges are ordered pairs.
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) be a directed graph with n∈N∧id(n) with 0≤id(n)≤∣N∣
Let M=∣N∣×∣N∣ matrix
For every pairs of nodes (m,n) set M[id(m),id(n)]:=(m,n)∈E
The adjacency list representation
Let A[0…∣N∣ be an array of bags
For every edge (m,n∈E) add n to (m,n) to bag A[id(m)]
ops | complexity |
---|
add/remove nodes | Θ(∥N∥) (copy array) |
add/remove edges (n,m) | Θ(∥out(n)∥) (adding to bag) |
check an edge (n,m) exists | Θ(∥out(n)∥) (searching bags) |
iterate over all incoming edges of n | Θ(∥E∥) (scan all bags) |
iterate over all outgoing edges of n | Θ(∥out(n)∥) (scan a bag) |
Check or change the weight of (n,m) | Θ(1) |
comparison.
Dense graph: ∣E∣≈Θ(∣N∣2) > Sparse graph: ∣E∣≈Θ(∣N∣)
Traversing undirected graph.
Depth-first search (DFS)
Algorithm 6 DFS-R(G, marked, n)
Require: G=(N,E),marked,n∈N
1:for all (n,m)∈E do
2:if =marked[m] then
3:marked[m]:=true
4:DFS-R(G,marked,m)
5:end if
6:end for
marked:={n⟼(n=s)∣n∈N}
- all nodes to which n3 is connected
- G is not a connected graph
- The order of recursive call determines all n3 is connected to.
Complexity
- ∣N∣ memory for marked and at-most ∣N∣ recursive calls
- inspect each node at-most once and traverse each edge once: Θ(∣N∣+∣E∣)
Connected-components
Algorithm 7 DFS-CC-R(G, cc, n)
Require: G=(N,E),cc,n∈N
1:for all (n,m)∈E do
2:if cc[m]=unmarked then
3:cc[m]:=cc[n]
4:DFS-CC-R(G,cc,m)
5:end if
6:end for
Algorithm 8 COMPONENTS(G, s)
Require: G=(N,E),s∈N
1:cc:={n↦unmarked}
2:for all n∈N do
3:if cc[n]=unmarked then
4:cc[n]:=n
5:DFS-CC-R(G,cc,n)
6:end if
7:end for
Two-colourability
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 9 DFS-TC-R(G, colors, n)
Require: G=(N,E),colors,n∈N
1:for all (n,m)∈E do
2:if colors[m]=0 then
3:colors[m]:=−colors[n]
4:DFS-TC-R(G,colors,m)
5:else if colors[m]=colors[n] then
6:print "This graph is not bipartite."
7:end if
8:end for
Algorithm 10 TwoColors(G)
Require: G=(N,E)
1:colors:={n↦0∣n∈N}
2:for all n∈N do
3:if colors[n]=0 then
4:colors[n]:=1
5:DFS-TC-R(G,colors,n)
6:end if
7:end for
Breadth-first search (BFS)
Algorithm 11 BFS(G, s)
Require: G=(N,E),s∈N
1:marked:={n↦(n=s)∣n∈N}
2:Q:=a queue holding only s
3:while ¬Empty(Q) do
4:n:=Dequeue(Q)
5:for all (n,m)∈E do
6:if ¬marked[m] then
7:marked[m]:=true
8:Enqueue(Q,m)
9:end if
10:end for
11:end while
Single-source shortest path
Given an undirected graph without weight and node s∈N, find a shortest path from node s to all nodes s can reach.
Algorithm 12 BFS-SSSP(G, s)
Require: G=(N,E),s∈N
1:distance:={n↦∞∣n∈N}
2:distance[s]:=0
3:Q:=a queue holding only s
4:while ¬Empty(Q) do
5:n:=Dequeue(Q)
6:for all (n,m)∈E do
7:if distance[m]=∞ then
8:distance[m]:=distance[n]+1
9:Enqueue(Q,m)
10:end if
11:end for
12:end while