graph algorithms graphs - definition g(v,e) - graph with vertex set v and edge set e e {(a,b)| a v...
TRANSCRIPT
![Page 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/1.jpg)
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}
![Page 2: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/2.jpg)
Graph Algorithms
Graphs - Examples
6
2
3 54
1 6
2
3 54
1
![Page 3: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/3.jpg)
Graph Algorithms
Graphs - Trees
6
2
3 5
4
1 6
2
3 5
4
1
![Page 4: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/4.jpg)
Graph Algorithms
Graphs - Directed Acyclic Graphs (DAG)
6
2
3 5
4
1
![Page 5: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/5.jpg)
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
![Page 6: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/6.jpg)
Graph AlgorithmsGraphs - Representations - Adjacency lists
6
2
3 54
1 1
2
3
4
5
6
53
2
1
2
6
6
1
3
![Page 7: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/7.jpg)
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
![Page 8: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/8.jpg)
Graph Algorithms
Breadth-First Search - Example
sa b c
d e f g
![Page 9: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/9.jpg)
Graph Algorithms
Breadth-First Search - Example
0
s
Q s
a b c
d e f g
![Page 10: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/10.jpg)
Graph Algorithms
Breadth-First Search - Example
01
1
s
Q e a
a b c
d e f g
![Page 11: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/11.jpg)
Graph Algorithms
Breadth-First Search - Example
01 2
1 2
s
Q a b f
a b c
d e f g
![Page 12: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/12.jpg)
Graph Algorithms
Breadth-First Search - Example
01 2
12 2
s
Q b f d
a b c
d e f g
![Page 13: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/13.jpg)
Graph Algorithms
Breadth-First Search - Example
01 32
12 2
s
Q f d c
a b c
d e f g
![Page 14: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/14.jpg)
Graph Algorithms
Breadth-First Search - Example
01 32
12 32
s
Q d c g
a b c
d e f g
![Page 15: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/15.jpg)
Graph Algorithms
Breadth-First Search - Example
01 32
12 32
s
Q c g
a b c
d e f g
![Page 16: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/16.jpg)
Graph Algorithms
Breadth-First Search - Example
01 32
12 32
s
Q g
a b c
d e f g
![Page 17: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/17.jpg)
Graph Algorithms
Breadth-First Search - Example
01 32
12 32
s
Q =
a b c
d e f g
![Page 18: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/18.jpg)
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)
![Page 19: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/19.jpg)
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)
![Page 20: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/20.jpg)
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)
![Page 21: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/21.jpg)
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
![Page 22: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/22.jpg)
Graph Algorithms
Depth-First Search - Example
s a b
c d e
![Page 23: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/23.jpg)
Graph Algorithms
Depth-First Search - Example
1/s a b
c d e
![Page 24: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/24.jpg)
Graph Algorithms
Depth-First Search - Example
1/ 2/s a b
c d e
![Page 25: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/25.jpg)
Graph Algorithms
Depth-First Search - Example
1/
3/
2/s a b
c d e
![Page 26: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/26.jpg)
Graph Algorithms
Depth-First Search - Example
1/
3/
2/
4/
s a b
c d e
![Page 27: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/27.jpg)
Graph Algorithms
Depth-First Search - Example
1/
3/
2/
4/
s a b
c d e
B
![Page 28: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/28.jpg)
Graph Algorithms
Depth-First Search - Example
1/
3/
2/
4/5
s a b
c d e
B
![Page 29: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/29.jpg)
Graph Algorithms
Depth-First Search - Example
1/
3/6
2/
4/5
s a b
c d e
B
![Page 30: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/30.jpg)
Graph Algorithms
Depth-First Search - Example
1/
3/6
2/7
4/5
s a b
c d e
B
![Page 31: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/31.jpg)
Graph Algorithms
Depth-First Search - Example
1/
3/6
2/7
4/5
s a b
c d e
BF
![Page 32: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/32.jpg)
Graph Algorithms
Depth-First Search - Example
1/8
3/6
2/7
4/5
s a b
c d e
BF
![Page 33: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/33.jpg)
Graph Algorithms
Depth-First Search - Example
1/8
3/6
9/2/7
4/5
s a b
c d e
BF
![Page 34: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/34.jpg)
Graph Algorithms
Depth-First Search - Example
1/8
3/6
9/2/7
4/5
s a b
c d e
BF C
![Page 35: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/35.jpg)
Graph Algorithms
Depth-First Search - Example
1/8
3/6
9/2/7
10/4/5
s a b
c d e
BF C
![Page 36: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/36.jpg)
Graph Algorithms
Depth-First Search - Example
1/8
3/6
9/2/7
10/4/5
s a b
c d e
BF CB
![Page 37: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/37.jpg)
Graph Algorithms
Depth-First Search - Example
1/8
3/6
9/2/7
10/114/5
s a b
c d e
BF CB
![Page 38: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/38.jpg)
Graph Algorithms
Depth-First Search - Example
1/8
3/6
9/122/7
10/114/5
s a b
c d e
BF CB
![Page 39: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/39.jpg)
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
![Page 40: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/40.jpg)
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
![Page 41: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/41.jpg)
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.
![Page 42: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/42.jpg)
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.
![Page 43: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/43.jpg)
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
![Page 44: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/44.jpg)
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
![Page 45: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/45.jpg)
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
![Page 46: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/46.jpg)
Graph Algorithms
Depth-First Search - Timestamps
a e
s
b f
d
B
F
c
g
C
CC
C
B
![Page 47: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/47.jpg)
Graph Algorithms
DFS - Checking for cycles
[Adapted from M.Golin]
![Page 48: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/48.jpg)
Graph Algorithms
DFS - Checking for cycles
[Adapted from M.Golin]
![Page 49: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/49.jpg)
Graph Algorithms
DFS - Checking for cycles
[Adapted from M.Golin]
![Page 50: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/50.jpg)
Graph Algorithms
DFS - Checking for cycles
[Adapted from M.Golin]
![Page 51: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/51.jpg)
Graph Algorithms
DFS - Topological Sorting
shoes
sockswatc
h
jacket
tie
shirt
pants
belt
undershorts
![Page 52: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/52.jpg)
Graph Algorithms
DFS - Topological Sorting
[Adapted from M.Golin]
![Page 53: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/53.jpg)
Graph Algorithms
DFS - Topological Sorting
[Adapted from M.Golin]
![Page 54: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/54.jpg)
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
![Page 55: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/55.jpg)
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
![Page 56: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/56.jpg)
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
![Page 57: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/57.jpg)
Graph Algorithms
DFS - Topological Sorting - Example 2
[Adapted from M.Golin]
![Page 58: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/58.jpg)
Graph Algorithms
DFS - Topological Sorting
Theorem
TopologicalSort(G) produces a topological sort of adirected acyclic graph G.
![Page 59: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/59.jpg)
Graph Algorithms
DFS - Strongly Connected Components
![Page 60: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/60.jpg)
Graph Algorithms
DFS - Strongly Connected Components
![Page 61: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/61.jpg)
Graph Algorithms
DFS - Strongly Connected Components
[Adapted from L.Joskowicz]
![Page 62: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/62.jpg)
Graph Algorithms
DFS - Strongly Connected Components
[Adapted from L.Joskowicz]
![Page 63: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/63.jpg)
Graph Algorithms
DFS - Strongly Connected Components
[Adapted from L.Joskowicz]
![Page 64: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/64.jpg)
Graph Algorithms
DFS - Strongly Connected Components
[Adapted from L.Joskowicz]
![Page 65: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/65.jpg)
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
![Page 66: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/66.jpg)
Graph Algorithms
DFS - Strongly Connected Components
13/14
3/4
1/1011/16
2/712/15
8/9
5/6
![Page 67: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/67.jpg)
Graph Algorithms
DFS - Strongly Connected Components
13/14
3/4
1/1011/16
2/712/15
8/9
5/6
![Page 68: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/68.jpg)
Graph Algorithms
DFS - Strongly Connected Components
13/14
3/4
1/1011/16
2/712/15
8/9
5/6
![Page 69: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/69.jpg)
Graph Algorithms
DFS - SCC - Correctness
13/14
3/4
1/1011/16
2/712/15
8/9
5/6
![Page 70: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/70.jpg)
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.
![Page 71: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/71.jpg)
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.
![Page 72: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/72.jpg)
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.
![Page 73: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/73.jpg)
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.
![Page 74: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/74.jpg)
Graph Algorithms
DFS - SCC - Correctness 2
[Adapted from S.Whitesides]
![Page 75: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/75.jpg)
Graph Algorithms
DFS - SCC - Correctness 2
[Adapted from S.Whitesides]
![Page 76: 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](https://reader036.vdocuments.us/reader036/viewer/2022062515/56649ccf5503460f9499b63e/html5/thumbnails/76.jpg)
Graph Algorithms
DFS - SCC - Applications
[Adapted from L.Joskowicz]