Kinds of Graphs. Data Structures For Graph Representation. Connectivity: The Na Ive Algorithm and Warshall Algorithm
Kinds of Graphs. Data Structures For Graph Representation. Connectivity: The Na Ive Algorithm and Warshall Algorithm
a e b or a (loop)
Graphical representation:
The directed edges are called arcs.
Simple graph: graph (directed or not) with at most one arc between any
pair of nodes, and no loop.
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 2 / 25
Common types of graphs
Taxonomy
G = (V , E )
e1 , e2 ∈ E are parallel edges if they are incident to the same nodes,
and
If G is directed, then start(e1 ) = start(e2 ) and
end(e1 ) = end(e2 )
a 5 1 f
4
7
1
e d
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 4 / 25
Graphical representations of graphs
Example (continued)
Example
b c List of nodes V = [a, b, c, d, e]
List of edges E = [{a, b}, {a, c}, {a, d}, {b, c}, {c, e}, {d, e}]
e Remarks: {a, b} = {b, a}, {a, c} = {c, a}, a.s.o.
a edge ↔ set of nodes adjacent to the edge
d
b c List of nodes V = [a, b, c, d, e]
List of arcs E = [(a, b), (c, a), (c, b), (d, a), (e, c), (e, d)]
e Remarks: (a, b) 6= (b, a), (a, c) 6= (c, a), a.s.o.
a edge ↔ pair (start,end)
d
Remark
If the graph has no isolated nodes (with 0 neighbors), there is no
need to store the list of nodes V :
I V can be computed from E
For every node u ∈ V we keep the list of nodes that are adjacent
to u
I If G is undirected, v is adjacent to u of there is an edge with
endpoints u and v .
In undirected graphs, the relation of adjacency is symmetric.
I If G is directed, v is adjacent to u if there is an arc e ∈ E
from u to v , i.e. start(e) = u and end(e) = v .
Example
b c a 7→ [b, c, d] d 7→ [a, e]
b 7→ [a, c] e 7→ [c, d]
e c 7→ [a, b, e]
a d
b c a 7→ [b] d 7→ [a]
b 7→ [] e 7→ [c, d]
e c 7→ [a, b]
a d
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 8 / 25
Graphs
The representation with adjacency matrix AG
Remarks
1 Before computing M
G from G , be must fix an enumeration of
its nodes: [v1 , v2 , . . . , vn ]
2 If G is undirected, AG is a symmetric matrix
3 If G is a simple graph, AG contains just 0s and 1s
b c
G: e
a d
for the node enumeration [a, b, c, d, e] is
0 1 1 1 0
1 0 1 0 0
AG = 1 1 0 0 1
1 0 0 0 1
0 0 1 1 0
Example
0 1 0 2 v2 v3
1 0 1 0
A= 0 ⇒G :
1 0 1
2 0 1 0 v1 v4
b c
G: e
a d
for the enumeration [a, b, c, d, e] of nodes is
0 1 0 0 0
0 0 0 0 0
AG = 1 1 0 0 0
1 0 0 0 0
0 0 1 1 0
Example
0 1 0 2 v2 v3
0 0 1 0
A=
0 ⇒G :
1 1 1
1 0 0 0 v1 v4
Example
If V = [a, b, c, d, e], L = [e1 , e2 , e3 , e4 , e5 , e6 , e7 , e8 ] and
−1 0 0 −1 1 0 0 −1
b
e2
1 −1 0 0 0 1 0 0 e1
MG = −1 −1 ⇒ e4
0 1 1 0 0 0
a c
e6
e5
0 0 1 0 0 0 1 1 e8
−1 −1
e3
0 0 0 0 0 0
e e7 d
∞ 9 0 ∞ 1 12
G :a f ⇒ WG =
1
5
4 ∞ ∞ ∞ 0 ∞ 7
7
1 ∞ 5 ∞ 1 0 ∞
e d ∞ ∞ ∞ ∞ ∞ 0
Properties
(k) (k)
1 If AkG = (aij ) for k ≥ 1 then aij = 1 if and only if there is a
path with length k from node vi to vj .
2 Let A∗G = In ⊕ AG ⊕ A2G ⊕ . . . ⊕ An−1
G = (aij ). Then
aij = 1 if and only if there is a path with length
j ∈ {1, . . . , n − 1} from node vi to vj .
A∗G can be computed in O(n4 ).
A∗G is called reflexive and transitive closure of AG .
3 vi and vj are connected ⇔ ∃ a simple path vi vj ⇔ aij = 1.
4 In ⊕ AG ⊕ A2G ⊕ . . . ⊕ Ak+1
G = In ⊕ (In ⊕ AG ⊕ A2G ⊕ . . . ⊕ AkG ) AG
⇒ A∗ can be computed in O(n4 ).
Properties
(k) (k)
1 If AkG = (aij ) for k ≥ 1 then aij = 1 if and only if there is a
path with length k from node vi to vj .
2 Let A∗G = In ⊕ AG ⊕ A2G ⊕ . . . ⊕ An−1
G = (aij ). Then
aij = 1 if and only if there is a path with length
j ∈ {1, . . . , n − 1} from node vi to vj .
A∗G can be computed in O(n4 ).
A∗G is called reflexive and transitive closure of AG .
3 vi and vj are connected ⇔ ∃ a simple path vi vj ⇔ aij = 1.
4 In ⊕ AG ⊕ A2G ⊕ . . . ⊕ Ak+1
G = In ⊕ (In ⊕ AG ⊕ A2G ⊕ . . . ⊕ AkG ) AG
⇒ A∗ can be computed in O(n4 ).
Corollary
The connectivity between all pairs of nodes in a simple digraph can
be checked in O(n4 ).
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 18 / 25
Simple digraphs
Better ways to compute A∗G
Properties
1 C [0] = AG
[k]
2 cij = 1 if and only if there is a path π : vi vj where all
intermediary nodes are from the subset {v1 , . . . , vk }
3 C [n] = A∗G
4 C [n] can be computed in O(n3 ).
[k−1] [k−1]
where pik pkj denotes the concatenation of the paths
[k−1] [k−1]
pik and pkj . If both exist (i.e., they are not •), and •
otherwise.
Remark
[n] [n]
If pij = •, there is no path from node i to j. Otherwise, pij is a
lightest path from node i to j.
Remark
W [n] and P [n] can be computed in O(n3 ).