Graph algorithms 1

Graph Algorithms
Graphs - Definition
G(V,E) - graph with vertex set V and edge set E
E  {(a,b)| aV and bV} - for directed graphs
E  {{a,b}| aV and bV} - for undirected graphs
w: E  R - weight function
|V| - number of vertices
|E| - number of edges
Often we will assume that V = {1, ,n}
Graph Algorithms
Graphs - Examples
6
1
6
2
3
1
2
4
5
3
4
5
Graph Algorithms
Graphs - Trees
6
1
6
2
3
4
5
1
2
3
4
5
Graph Algorithms
Graphs - Directed Acyclic Graphs (DAG)
6
1
2
3
4
5
Graph Algorithms
Graphs - Representations - Adjacency matrix
6
1
2
3
4
5
1
2
3
4
5
6
1
2
3
4
5
6
0
0
0
1
0
1
0
0
1
0
1
0
0
0
1
1
0
0
0
0
0
0
0
0
0
0
0
1
0
0
1
1
0
0
0
0
Graph Algorithms
Graphs - Representations - Adjacency lists
6
1
2
3
4
5
1
6
2
6
3
2
3
4
1
3
5
2
6
1
5
Graph Algorithms
Breadth-First Search - Algorithm
BreadthFirstSearch(graph G, vertex s)
for u  V[G]  {s} do
colour[u]  white; d[u] ; p[u]  0
colour[s]  gray; d[s]  0; p[s]  0
Q  {s}
while Q  0 do
u  Head[Q]
for v  Adj[u] do
if colour[v] = white then
colour[v]  gray; d[v]  d[u] + 1; p[v]  u
EnQueue(Q,v)
DeQueue(Q)
colour[u]  black
Graph Algorithms
Breadth-First Search - Example
a
s
b
c
d
e
f
g
Graph Algorithms
Breadth-First Search - Example
a

s
0
b

c


d

e

f

g
Q
s
Graph Algorithms
Breadth-First Search - Example
a
1
s
0
b

c


d
1
e

f

g
Q
e
a
Graph Algorithms
Breadth-First Search - Example
a
1
s
0
b
2
c


d
1
e
2
f

g
Q
a
b
f
Graph Algorithms
Breadth-First Search - Example
a
1
s
0
b
2
c

2
d
1
e
2
f

g
Q
b
f
d
Graph Algorithms
Breadth-First Search - Example
a
1
s
0
b
2
c
3
2
d
1
e
2
f

g
Q
f
d
c
Graph Algorithms
Breadth-First Search - Example
a
1
s
0
b
2
c
3
2
d
1
e
2
f
3
g
Q
d
c
g
Graph Algorithms
Breadth-First Search - Example
a
1
s
0
b
2
c
3
2
d
1
e
2
f
3
g
Q
c
g
Graph Algorithms
Breadth-First Search - Example
a
1
s
0
b
2
c
3
2
d
1
e
2
f
3
g
Q
g
Graph Algorithms
Breadth-First Search - Example
a
1
s
0
b
2
c
3
2
d
1
e
2
f
3
g
Q=
Graph Algorithms
Breadth-First Search - Complexity
(V)
BreadthFirstSearch(graph G, vertex s)
for u  V[G]  {s} do
colour[u]  white; d[u] ; p[u]  0
colour[s]  gray; d[s]  0; p[s]  0
Q  {s}
while Q  0 do
u  Head[Q]
for v  Adj[u] do
if colour[v] = white then
colour[v]  gray; d[v]  d[u] + 1; p[v]  u
EnQueue(Q,v)
DeQueue(Q)
colour[u]  black
Thus T(V,E)=(V+E)
(V) without for cycle
(E) for all while cycles together
Graph Algorithms
Breadth-First Search - Shortest Distances
Theorem
After BreadthFirstSearch algorithm terminates
• d[v] is equal with shortest distance from s to v for all
vertices v
• for all vertices v reachable from s the one of the
shortest paths from s to v contains edge (p[v], v)
Graph Algorithms
Depth-First Search - Algorithm
DepthFirstSearch(graph G)
for u  V[G] do
colour[u]  white
p[u]  0
time  0
for u  V[G] do
if colour[v] = white then
DFSVisit(v)
Graph Algorithms
Depth-First Search - Algorithm
DFSVisit(vertex u)
time  time + 1
d[u]  time
colour[u]  gray
for v  Adj[u] do
if colour[v] = white then
p[v]  u
DFSVisit(v)
colour[u]  black
time  time + 1
f[u]  time
Graph Algorithms
Depth-First Search - Example
s
a
b
c
d
e
Graph Algorithms
Depth-First Search - Example
s
1/
a
b
c
d
e
Graph Algorithms
Depth-First Search - Example
s
1/
a
2/
b
c
d
e
Graph Algorithms
Depth-First Search - Example
s
1/
a
2/
c
3/
d
b
e
Graph Algorithms
Depth-First Search - Example
s
1/
a
2/
4/
c
3/
d
b
e
Graph Algorithms
Depth-First Search - Example
s
1/
a
2/
b
B
4/
c
3/
d
e
Graph Algorithms
Depth-First Search - Example
s
1/
a
2/
b
B
4/5
c
3/
d
e
Graph Algorithms
Depth-First Search - Example
s
1/
a
2/
b
B
4/5
c
3/6
d
e
Graph Algorithms
Depth-First Search - Example
s
1/
a
2/7
b
B
4/5
c
3/6
d
e
Graph Algorithms
Depth-First Search - Example
s
1/
F
4/5
c
a
2/7
b
B
3/6
d
e
Graph Algorithms
Depth-First Search - Example
s
1/8
F
4/5
c
a
2/7
b
B
3/6
d
e
Graph Algorithms
Depth-First Search - Example
s
1/8
F
4/5
c
a
2/7
b
9/
3/6
d
e
B
Graph Algorithms
Depth-First Search - Example
s
1/8
F
4/5
c
a
2/7
B
b
9/
C
3/6
d
e
Graph Algorithms
Depth-First Search - Example
s
1/8
F
4/5
c
a
2/7
B
b
9/
C
3/6
d
10/
e
Graph Algorithms
Depth-First Search - Example
s
1/8
F
4/5
c
a
2/7
B
b
9/
C
B
3/6
d
10/
e
Graph Algorithms
Depth-First Search - Example
s
1/8
F
4/5
c
a
2/7
B
b
9/
C
B
3/6
d
10/11
e
Graph Algorithms
Depth-First Search - Example
s
1/8
F
4/5
c
a
2/7
B
b
9/12
C
B
3/6
d
10/11
e
Graph Algorithms
Depth-First Search - Complexity
DepthFirstSearch(graph G)
for u  V[G] do
colour[u]  white
(V)
p[u]  0
time  0
for u  V[G] do
if colour[v] = white then
executed (V) times
DFSVisit(v)
DFSVisit(vertex u)
time  time + 1
d[u]  time
colour[u]  gray
for v  Adj[u] do
if colour[v] = white then
(E) for all DFSVisit calls together
p[v]  u
DFSVisit(v)
colour[u]  black
time  time + 1
Thus T(V,E)=(V+E)
f[u]  time
Graph Algorithms
Depth-First Search - Classification of Edges
Trees edges - edges in depth-first forest
Back edges - edges (u, v) connecting vertex u to an
v in a depth-first tree (including self-loops)
Forward edges - edges (u, v) connecting vertex u to a
descendant v in a depth-first tree
Cross edges - all other edges
Graph Algorithms
Depth-First Search - Classification of Edges
Theorem
In a depth-first search of an undirected graph G, every
edge of G is either a tree edge or a back edge.
Graph Algorithms
Depth-First Search - White Path Theorem
Theorem
If during depth-first search a “white” vertex u is reachable
from a “grey” vertex v via path that contains only “white”
vertices, then vertex u will be a descendant on v in
depth-first search forest.
Graph Algorithms
Depth-First Search - Timestamps
Parenthesis Theorem
After DepthFirstSearch algorithm terminates for any two
vertices u and v exactly one from the following three
conditions holds
• the intervals [d[u],f[u]] and [d[v],f[v]] are entirely disjoint
• the intervals [d[u],f[u]] is contained entirely within the
interval [d[v],f[v]] and u is a descendant of v in depthfirst tree
• the intervals [d[v],f[v]] is contained entirely within the
interval [d[u],f[u]] and v is a descendant of u in depthfirst tree
Graph Algorithms
Depth-First Search - Timestamps
a
3/6
b
2/9
B
4/5
d
s
1/10
F
C
7/8
e
c
11/16
C
C
12/13
f
B
C
14/15
g
Graph Algorithms
Depth-First Search - Timestamps
s
c
b
a
f
g
e
d
1 2 3
(s (b (a
4 5
(d d)
6 7 8 9 10 11 12 13 14 15 16
a) (e e) b) s) (c (f f) (g g) c)
Graph Algorithms
Depth-First Search - Timestamps
s
b
c
C
F
f
g
C
a
e
B
d
C
C
B
Graph Algorithms
DFS - Checking for cycles
[Adapted from M.Golin]
Graph Algorithms
DFS - Checking for cycles
[Adapted from M.Golin]
Graph Algorithms
DFS - Checking for cycles
[Adapted from M.Golin]
Graph Algorithms
DFS - Checking for cycles
[Adapted from M.Golin]
Graph Algorithms
DFS - Topological Sorting
undershorts
socks
pants
shoes
belt
shirt
tie
jacke
t
watc
h
Graph Algorithms
DFS - Topological Sorting
[Adapted from M.Golin]
Graph Algorithms
DFS - Topological Sorting
[Adapted from M.Golin]
Graph Algorithms
DFS - Topological Sorting
TopologicalSort(graph G)
•
call DFS(G) to compute f[v] for all vertices v
•
as f[v] for vertex v is computed, insert onto the front of
a linked list
•
return the linked list of vertices
Graph Algorithms
DFS - Topological Sorting - Example 1
undershorts
11/16
socks
17/18
watc
h
12/15 pants
shoes
6/7
belt
shirt
1/8
tie
2/5
jacke
t
3/4
13/14
9/10
Graph Algorithms
DFS - Topological Sorting - Example 1
socks
undershorts
pants
shoes
17/18
11/16
12/15
13/14
shirt
belt
tie
1/8
6/7
2/5
jacke
t
3/4
watc
h
9/10
Graph Algorithms
DFS - Topological Sorting - Example 2
[Adapted from M.Golin]
Graph Algorithms
DFS - Topological Sorting
Theorem
TopologicalSort(G) produces a topological sort of a
directed acyclic graph G.
Graph Algorithms
DFS - Strongly Connected Components
Graph Algorithms
DFS - Strongly Connected Components
Graph Algorithms
DFS - Strongly Connected Components
[Adapted from L.Joskowicz]
Graph Algorithms
DFS - Strongly Connected Components
[Adapted from L.Joskowicz]
Graph Algorithms
DFS - Strongly Connected Components
[Adapted from L.Joskowicz]
Graph Algorithms
DFS - Strongly Connected Components
[Adapted from L.Joskowicz]
Graph Algorithms
DFS - Strongly Connected Components
StronglyConnectedComponents(graph G)
•
call DFS(G) to compute f[v] for all vertices v
•
compute GT
•
call DFS(GT) consider vertices in order of decreasing
of f[v]
•
output the vertices of each tree in the depth-first forest
as a separate strongly connected component
Graph Algorithms
DFS - Strongly Connected Components
13/14
11/16
1/10
8/9
12/15
3/4
2/7
5/6
Graph Algorithms
DFS - Strongly Connected Components
13/14
11/16
1/10
8/9
12/15
3/4
2/7
5/6
Graph Algorithms
DFS - Strongly Connected Components
13/14
11/16
1/10
8/9
12/15
3/4
2/7
5/6
Graph Algorithms
DFS - SCC - Correctness
13/14
11/16
1/10
8/9
12/15
3/4
2/7
5/6
Graph Algorithms
DFS - SCC - Correctness
x
Assume that y preceded by y' is the
closest vertex to x outside C(x). Then:
- d(y)<f(y)<d(x)<f(x) (otherwise we will
have xy (in G).
y'
C(x)
y
- for all x'C(x): d(x)<d(x')<f(x')<f(x)
(the largest value of f(x) will have the
vertex first "discovered" in C(x)).
- thus we have d(y)<f(y)<d(y')<f(y'),
however there is and edge (y,y') in G,
implying f(y)<d(y') d(y')<y(y).
Contradiction.
Graph Algorithms
DFS - SCC - Correctness
Lemma
If two vertices are in the same strongly connected,
then no path between them leaves this strongly connected
component.
Theorem
In any depth-first search, all vertices in the same strongly
connected component are placed in the same depth-first
tree.
Graph Algorithms
DFS - SCC - Correctness
Theorem
In a directed graph G = (V,E) the forefather (u) of any
vertex uV in any depth-first search of G is an
ancestor of u.
Corollary
In any depth-first search of a directed graph G = (V,E)
for all uV vertices u and (u) lie in the same strongly
connected component.
Graph Algorithms
DFS - SCC - Correctness
Theorem
In a directed graph G = (V,E) two vertices u,vV
lie in the same strongly connected component
if and only if they have the same forefather in a
depth-first search of G.
Theorem
StronglyConnectedComponents(G) correctly computes
the strongly connected components of a directed graph G.
Graph Algorithms
DFS - SCC - Correctness 2
[Adapted from S.Whitesides]
Graph Algorithms
DFS - SCC - Correctness 2
[Adapted from S.Whitesides]
Graph Algorithms
DFS - SCC - Applications
[Adapted from L.Joskowicz]