Relations and Graphs Composition and Graph Powers Sections 6.3-6.5
Relations and Graphs Composition and Graph Powers Sections 6.3-6.5
Composition and
Graph Powers
Sections 6.3-6.5
Prof. Sandy Irani
Directed Graphs
A directed graph G = (V, E) consists of
V: set of verties
E: set of edges. E ⊆ V x V
Example: V = {a, b, c, d}
E = { (a,b), (b,b), (b,c), (c,b), (c,d) }
a b
A directed graph G=(V,E)
is the same as a relation
d c E on set V
Directed Graph Terminology
In-degree of c =
Out-degree of b =
A Walk in A Directed Graph
The walk in a directed graph is an alternating
sequence of vertices and edges, starting and ending
with a vertex.
Each edge comes after its tail and before its head.
< b, (b,a), a, (a,b), b, (b,d), d, (d,e), e, (e,c), c, (c,c), c, (c,d), d >
c <b, a, b, d, e, c, c, d>
a
<b, a, e> is not a walk because
d (a, e) is not an edge.
e
A Path in A Directed Graph
The walk in a directed graph is also a path if it has no
repeated vertices.
b
<a, b, d, e, c>
c
a <b, a, d, e>
<c, d, e>
d e
<d, e>
A Circuit in A Directed Graph
The walk in a directed graph is also a circuit if it
starts and ends at the same vertex.
b
<e, c, d, e>
c
a <a, b, a>
<c, c>
d e
<e, c, c, d, e, c, d, e>
A Circuit in A Directed Graph
The circuit in a directed graph is also a cycle if there are
no repeated vertices except for the first and the last.
b
<e, d, c, e>
c
a <a, b, a>
<c, c>
d e
< a, d, e, c, b, a >
Walks
Paths
Circuits
Cycles
Directed Graph Terminology *
Pick the correct description for the sequence
<c, d, a, b, d, e, c>
b <e, d, c, e>
c <a, b, a>
a
<c, c>
d e <b, a, b, e, d, c, c, e>
<d>
Transitive Closure of a Directed Graph
Is it possible to travel from b to c
b by a walk?
c From c to b?
a
We can build a new graph with
The same vertex set.
d e
The new graph has an edge (x,y)
Whenever Y can be reached from x by a walk.
What is the smallest set of edges we need to add to make edges
in the graph transitive?
….the Transitive Closure.
Composition of Binary Relations
a b a b
d c d c
R S
a b
Add an edge from x to y
If there is a red-blue walk
d c x z y
S◦R Can have x = z or x = y or z = y
Composing a Binary Relation
with Itself
a a b
b
d c
d c
R
R◦R
Add an edge from x to y
if there is a walk of length 2
From x to y in R.
x z y
Can have x = z or x = y or z = y
Composition of Binary Relations *
A)3(R◦R)9
R is a binary relation on ℤ
D) 81(R◦R)3
Powers of a Relation
We can compose a relation multiple times with
itself:
R1 = A
R2 = R ◦ R
R3 = R ◦ R◦ R = R2 ◦ R
kth power of R
…
Rk = R ◦ R◦ R ◦… ◦ R = Rk-1 ◦ R
k times
Graph Powers
Graph G = (V, E)
1 4 1 4
6 5 6 5
G3
2 3
1 4
6 5
Graph Powers *
What is the out-degree of vertex 2 in G4?
2 3 A) 2
1 4 B) 3
C) 4
6 5
G D) 5
Graph Powers
Graph Gk has the same vertex set as G.
(x,y) is an edge in Gk if and only if there is a walk
of length k in G from x to y.
x z y
walk of length k-1 edge from z to y in G
from x to z in G
G+ = G1 ∪ G2 ∪ G3∪ G4 ∪ ….. ∪ Gn
If there is a walk from x to y in G, then there is a walk of length at
most n from x to y. (n is the number of vertices in G.)
Worst Case:
Transitive Closure of a Directed Graph
Transitive closure of G: G+ = G1 ∪ G2 ∪ G3∪ G4…..
(Same vertex set. Take the union of the edges.)
y
x z
Transitive Closure of a Directed Graph *
Which pair is not an edge in the transitive closure of G?
2 3 A) (1,1)
1 4 B) (1,4)
C) (5,5)
6 5
G D) (3,6)
Transitive Closure of a Binary Relation
Transitive closure of Binary Relation R:
R+ = R1 ∪ R2 ∪ R3∪ R4…..
(Same domain. Take the union of the pairs.)
2 3
1 4
6 5