Graph Algorithms Graphs - Definition G(V,E) - graph with vertex set V and edge set E E {(a,b)| a V...

76
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}

Transcript of Graph Algorithms Graphs - Definition G(V,E) - graph with vertex set V and edge set E E {(a,b)| a V...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Graph Algorithms

DFS - SCC - Applications

[Adapted from L.Joskowicz]