Vous êtes sur la page 1sur 75

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 1 6 2

Graph Algorithms
Graphs - Trees
6 2 1 4

6 2

1 4

Graph Algorithms
Graphs - Directed Acyclic Graphs (DAG)
6 2

1 4

Graph Algorithms
Graphs - Representations - Adjacency matrix 1 2 3 4 5 6
6 2 1

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 2

1 2 3

6 6 2 1 2 1 3 3 5

4 5 6

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

Graph Algorithms
Breadth-First Search - Example
a s 0 b c

d Q

Graph Algorithms
Breadth-First Search - Example
a 1 s 0 b c

d Q

1 e

Graph Algorithms
Breadth-First Search - Example
a 1 s 0 b 2 c

d Q

1 e

2 f

Graph Algorithms
Breadth-First Search - Example
a 1 s 0 b 2 c

2 d Q

1 e

2 f

Graph Algorithms
Breadth-First Search - Example
a 1 s 0 b 2 c 3

2 d Q

1 e

2 f

Graph Algorithms
Breadth-First Search - Example
a 1 s 0 b 2 c 3

2 d Q

1 e

2 f

3 g

Graph Algorithms
Breadth-First Search - Example
a 1 s 0 b 2 c 3

2 d Q

1 e

2 f

3 g

Graph Algorithms
Breadth-First Search - Example
a 1 s 0 b 2 c 3

2 d Q

1 e

2 f

3 g

Graph Algorithms
Breadth-First Search - Example
a 1 s 0 b 2 c 3

2 d Q=

1 e

2 f

3 g

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

(V) without for cycle (E) for all while cycles together

Thus T(V,E)=(V+E)

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) d[u] time colour[u] gray time time + 1 for v Adj[u] do if colour[v] = white then p[v] u DFSVisit(v) colour[u] black f[u] time time time + 1

Graph Algorithms
Depth-First Search - Example
s a b

Graph Algorithms
Depth-First Search - Example
s 1/ a b

Graph Algorithms
Depth-First Search - Example
s 1/ a 2/ b

Graph Algorithms
Depth-First Search - Example
s 1/ a 2/ b

3/ d

Graph Algorithms
Depth-First Search - Example
s 1/ a 2/ b

4/ c

3/ d

Graph Algorithms
Depth-First Search - Example
s 1/ B 4/ c 3/ d a 2/ b

Graph Algorithms
Depth-First Search - Example
s 1/ B 4/5 c 3/ d a 2/ b

Graph Algorithms
Depth-First Search - Example
s 1/ B 4/5 c 3/6 d a 2/ b

Graph Algorithms
Depth-First Search - Example
s 1/ B 4/5 c 3/6 d a 2/7 b

Graph Algorithms
Depth-First Search - Example
s 1/ F 4/5 c B 3/6 d a 2/7 b

Graph Algorithms
Depth-First Search - Example
s 1/8 F 4/5 c B 3/6 d a 2/7 b

Graph Algorithms
Depth-First Search - Example
s 1/8 F 4/5 c B 3/6 d a 2/7 b 9/

Graph Algorithms
Depth-First Search - Example
s 1/8 F 4/5 c B 3/6 d a 2/7 C b 9/

Graph Algorithms
Depth-First Search - Example
s 1/8 F 4/5 c B 3/6 d a 2/7 C 10/ e b 9/

Graph Algorithms
Depth-First Search - Example
s 1/8 F 4/5 c B 3/6 d a 2/7 C B 10/ e b 9/

Graph Algorithms
Depth-First Search - Example
s 1/8 F 4/5 c B 3/6 d a 2/7 C B 10/11 e b 9/

Graph Algorithms
Depth-First Search - Example
s 1/8 F 4/5 c B 3/6 d a 2/7 C B 10/11 e b 9/12

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) d[u] time colour[u] gray time time + 1 for v Adj[u] do if colour[v] = white then (E) for all DFSVisit calls together p[v] u DFSVisit(v) colour[u] black f[u] time Thus T(V,E)=(V+E) time time + 1

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

Graph Algorithms
Depth-First Search - Timestamps
a 3/6 B 4/5 d 7/8 e b 2/9 F s 1/10 C C 12/13 f C 14/15 g c 11/16 B

Graph Algorithms
Depth-First Search - Timestamps
s b a d 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) e f c g

Graph Algorithms
Depth-First Search - Timestamps
s F e C C c

C f

B g

b a 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 jacket watch

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
11/16 undershorts socks pants shoes 6/7 belt shirt tie jacket 1/8 2/5 3/4 13/14 17/18 watch 9/10

12/15

Graph Algorithms
DFS - Topological Sorting - Example 1

socks 17/18

undershorts 11/16

pants 12/15

shoes 13/14

watch 9/10

shirt 1/8

belt 6/7

tie 2/5

jacket 3/4

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
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 uV 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 uV 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,vV 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]

Vous aimerez peut-être aussi