graph algorithms graphs - definition g(v,e) - graph with vertex set v and edge set e e {(a,b)| a v...
TRANSCRIPT
Graph Algorithms
Graphs - Definition
G(V,E) - graph with vertex set V and edge set E
E {(a,b)| aV and bV} - for directed graphs
E {{a,b}| aV and bV} - 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
2
3 54
1 6
2
3 54
1
Graph Algorithms
Graphs - Trees
6
2
3 5
4
1 6
2
3 5
4
1
Graph Algorithms
Graphs - Directed Acyclic Graphs (DAG)
6
2
3 5
4
1
Graph AlgorithmsGraphs - Representations - Adjacency matrix
6
2
3 54
1 0 0 0 0 0 1
0 0 0 0 0 1
0 1 1 0 0 0
1 0 1 0 1 0
0 1 0 0 0 0
1 0 0 0 0 0
1 2 3 4 5 6
1
2
3
4
5
6
Graph AlgorithmsGraphs - Representations - Adjacency lists
6
2
3 54
1 1
2
3
4
5
6
53
2
1
2
6
6
1
3
Graph Algorithms
Breadth-First Search - Algorithm
BreadthFirstSearch(graph G, vertex s)for u V[G] {s} do
colour[u] white; d[u] ; p[u] 0colour[s] gray; d[s] 0; p[s] 0Q {s}while Q 0 do
u Head[Q]for v Adj[u] do
if colour[v] = white thencolour[v] gray; d[v] d[u] + 1; p[v] uEnQueue(Q,v)
DeQueue(Q)colour[u] black
Graph Algorithms
Breadth-First Search - Example
sa b c
d e f g
Graph Algorithms
Breadth-First Search - Example
0
s
Q s
a b c
d e f g
Graph Algorithms
Breadth-First Search - Example
01
1
s
Q e a
a b c
d e f g
Graph Algorithms
Breadth-First Search - Example
01 2
1 2
s
Q a b f
a b c
d e f g
Graph Algorithms
Breadth-First Search - Example
01 2
12 2
s
Q b f d
a b c
d e f g
Graph Algorithms
Breadth-First Search - Example
01 32
12 2
s
Q f d c
a b c
d e f g
Graph Algorithms
Breadth-First Search - Example
01 32
12 32
s
Q d c g
a b c
d e f g
Graph Algorithms
Breadth-First Search - Example
01 32
12 32
s
Q c g
a b c
d e f g
Graph Algorithms
Breadth-First Search - Example
01 32
12 32
s
Q g
a b c
d e f g
Graph Algorithms
Breadth-First Search - Example
01 32
12 32
s
Q =
a b c
d e f g
Graph Algorithms
Breadth-First Search - Complexity
BreadthFirstSearch(graph G, vertex s)for u V[G] {s} do
colour[u] white; d[u] ; p[u] 0colour[s] gray; d[s] 0; p[s] 0Q {s}while Q 0 do
u Head[Q]for v Adj[u] do
if colour[v] = white thencolour[v] gray; d[v] d[u] + 1; p[v] uEnQueue(Q,v)
DeQueue(Q)
colour[u] black
(V)
(V) without for cycle(E) for all while cycles together
Thus T(V,E)=(V+E)
Graph AlgorithmsBreadth-First Search - Shortest DistancesTheorem
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] whitep[u] 0
time 0for u V[G] do
if colour[v] = white thenDFSVisit(v)
Graph Algorithms
Depth-First Search - Algorithm
DFSVisit(vertex u)time time + 1d[u] timecolour[u] grayfor v Adj[u] do
if colour[v] = white thenp[v] uDFSVisit(v)
colour[u] blacktime time + 1 f[u] time
Graph Algorithms
Depth-First Search - Example
s a b
c d e
Graph Algorithms
Depth-First Search - Example
1/s a b
c d e
Graph Algorithms
Depth-First Search - Example
1/ 2/s a b
c d e
Graph Algorithms
Depth-First Search - Example
1/
3/
2/s a b
c d e
Graph Algorithms
Depth-First Search - Example
1/
3/
2/
4/
s a b
c d e
Graph Algorithms
Depth-First Search - Example
1/
3/
2/
4/
s a b
c d e
B
Graph Algorithms
Depth-First Search - Example
1/
3/
2/
4/5
s a b
c d e
B
Graph Algorithms
Depth-First Search - Example
1/
3/6
2/
4/5
s a b
c d e
B
Graph Algorithms
Depth-First Search - Example
1/
3/6
2/7
4/5
s a b
c d e
B
Graph Algorithms
Depth-First Search - Example
1/
3/6
2/7
4/5
s a b
c d e
BF
Graph Algorithms
Depth-First Search - Example
1/8
3/6
2/7
4/5
s a b
c d e
BF
Graph Algorithms
Depth-First Search - Example
1/8
3/6
9/2/7
4/5
s a b
c d e
BF
Graph Algorithms
Depth-First Search - Example
1/8
3/6
9/2/7
4/5
s a b
c d e
BF C
Graph Algorithms
Depth-First Search - Example
1/8
3/6
9/2/7
10/4/5
s a b
c d e
BF C
Graph Algorithms
Depth-First Search - Example
1/8
3/6
9/2/7
10/4/5
s a b
c d e
BF CB
Graph Algorithms
Depth-First Search - Example
1/8
3/6
9/2/7
10/114/5
s a b
c d e
BF CB
Graph Algorithms
Depth-First Search - Example
1/8
3/6
9/122/7
10/114/5
s a b
c d e
BF CB
Graph AlgorithmsDepth-First Search - Complexity
DFSVisit(vertex u)time time + 1d[u] timecolour[u] grayfor v Adj[u] do
if colour[v] = white thenp[v] uDFSVisit(v)
colour[u] blacktime time + 1f[u] time
DepthFirstSearch(graph G)for u V[G] do
colour[u] whitep[u] 0
time 0for u V[G] do
if colour[v] = white thenDFSVisit(v)
(V)
executed (V) times
Thus T(V,E)=(V+E)
(E) for all DFSVisit calls together
Graph AlgorithmsDepth-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 adescendant v in a depth-first tree
Cross edges - all other edges
Graph AlgorithmsDepth-First Search - Classification of Edges
Theorem
In a depth-first search of an undirected graph G, everyedge of G is either a tree edge or a back edge.
Graph AlgorithmsDepth-First Search - White Path Theorem
Theorem
If during depth-first search a “white” vertex u is reachablefrom a “grey” vertex v via path that contains only “white”vertices, then vertex u will be a descendant on v indepth-first search forest.
Graph Algorithms
Depth-First Search - Timestamps
Parenthesis TheoremAfter 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 depth- first 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 depth- first tree
Graph Algorithms
Depth-First Search - Timestamps
3/6
7/8
1/102/9
12/134/5
a b s
d e f
B F
11/16
14/15
c
gC C
C
C
B
Graph Algorithms
Depth-First Search - Timestamps
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16(s (b (a (d d) a) (e e) b) s) (c (f f) (g g) c)
s
b
c
a e
d
f g
Graph Algorithms
Depth-First Search - Timestamps
a e
s
b f
d
B
F
c
g
C
CC
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
shoes
sockswatc
h
jacket
tie
shirt
pants
belt
undershorts
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
shoes
sockswatc
h
jacket
tie
shirt
pants
belt
undershorts11/16
12/15
6/7 1/8
2/5
3/4
17/18
13/14
9/10
Graph Algorithms
DFS - Topological Sorting - Example 1
shoessockswatc
h
jacket
tieshirt
pants
belt
undershorts
11/16 12/15
6/71/8 2/5 3/4
17/18 13/14 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 adirected 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
3/4
1/1011/16
2/712/15
8/9
5/6
Graph Algorithms
DFS - Strongly Connected Components
13/14
3/4
1/1011/16
2/712/15
8/9
5/6
Graph Algorithms
DFS - Strongly Connected Components
13/14
3/4
1/1011/16
2/712/15
8/9
5/6
Graph Algorithms
DFS - SCC - Correctness
13/14
3/4
1/1011/16
2/712/15
8/9
5/6
Graph Algorithms
DFS - SCC - Correctness
y' y
x
C(x)
Assume that y preceded by y' is the closest vertex to x outside C(x). Then:
- d(y)<f(y)<d(x)<d(y) (otherwise we will have xy (in G).
- 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 connectedcomponent.
TheoremIn any depth-first search, all vertices in the same stronglyconnected component are placed in the same depth-firsttree.
Graph Algorithms
DFS - SCC - Correctness
TheoremIn a directed graph G = (V,E) the forefather (u) of any vertex uV in any depth-first search of G is an ancestor of u.
CorollaryIn any depth-first search of a directed graph G = (V,E)for all uV vertices u and (u) lie in the same stronglyconnected component.
Graph Algorithms
DFS - SCC - Correctness
TheoremIn a directed graph G = (V,E) two vertices u,vVlie in the same strongly connected componentif and only if they have the same forefather in adepth-first search of G.
TheoremStronglyConnectedComponents(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]