Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

35
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22

Transcript of Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

Page 1: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

Introduction to AlgorithmsSecond EditionbyCormen, Leiserson, Rivest & Stein

Chapter 22

Page 2: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

2

Elementary Graph Algorithms Graphs are similar to trees with the exception that trees

cannot contain cycles or loops, whereas graphs can. Definitions:

A graph G = (V, E) consists of a set of vertices, V, and a set of edges, E. Each edge is a pair (v, w), where v,w ε V. Edges are sometimes referred to as arcs.

If a pair is ordered, then the graph is directed (digraphs).

Vertex v is adjacent to w iff (v,w) ε E. In an undirected graph with edge (v,w), and hence (w,v), w is adjacent to v and v is adjacent to w. Sometimes an edge has a third component, known as either a “cost” or a “weight”.

Page 3: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

3

More definitions… A path in a graph is a sequence of vertices w1, w2, …,wN such

that (wi,wi+1) ε E for 1 ≤ i < N. The length of such a path is the number of edges on the path, which is equal to N-1.

If a graph contains an edge (v, v), then this is referred to as a “loop”. Here, the graphs we consider are “loopless”.

A path is simple if all vertices are distinct. A cycle in a directed graph is a path of length at least 1 such

that w1 = wN A directed graph is called “acyclic” if it has no cycles. A cycle is simple if the path is simple. For undirected graphs,

we require that the edges be distinct. An undirected graph is connected if there is a path from every

vertex to every other vertex. A directed graph with this property is called strongly connected.

A complete graph is a graph in which there is an edge between every pair of vertices.

Page 4: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

4

Applications

Computer Networks Traffic flow Airport/Flight Systems Transportation problem Scheduling problems

Page 5: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

5

Graph Representations

Page 6: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

6

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 7: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

7

Complexity of representations Adjacency-list representation consists of an

array Adj of |V| lists, one for each vertex in V.For each u ε V, the adjacency list Adj[u] contains all vertices adjacent to “u” in G. Figure 22.2 (b) If G is directed, the sum of the lengths of all

adjacency lists is |E|. If G is undirected, the sum of the lengths of all

adjacency lists is 2|E|.Memory requirements Θ(V + E).

Page 8: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

8

Complexity of representations…

Adjacency-matrix representation, vertices are numbered form 1,2,…,|V| arbitrarily.

Adjacency-matrix of a G consists of a |V|x|V| matrix A=(aij) s.t

Figures 22.1(c) and 22.2(c) Memory requirements Θ(V2)

otherwise

Ejiifaij 0),(1

Page 9: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

9

Transpose of Adj-Matrix Transpose of a matrix A=(aij ) is AT=(aij

T). Transpose of an adj-matrix of an

undirected graph is itself. For unweighted graphs, adj-matrix

representation is memory efficient (1 bit per entry).

Page 10: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

10

Exercises (pp.530/ 22.1.2)

Give an adjacency-list & adjacency-matrix representations for a complete binary tree on 7 vertices. Assume that all vertices are numbered from 1-7.

1

3

7654

2

1 2 3 0

2 4 5 1 0

3 6 7 1 0

4 2 0

5 2 0

6 3 0

7 3 0

Page 11: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

11

Adj-Matrix repr.

1 2 3 4 5 6 7

1 0 1 1 0 0 0 0

2 1 0 0 1 1 0 0

3 1 0 0 0 0 1 1

4 0 1 0 0 0 0 0

5 0 1 0 0 0 0 0

6 0 0 1 0 0 0 0

7 0 0 1 0 0 0 0

Page 12: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

12

22.1.3 The transpose of a directed graph G=(V,E)

is the graph GT =(V, ET), where ET={(v,u)εVxV: (u,v) εE}. Thus, GT is G with all its edges reversed. Describe efficient algorithms for computing GT from G, for the adj-matrix representation of G. Analyze the running time.

Answer: Take the transpose of the adj-matrix. O(E2)

Page 13: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

13

Breadth-first search BFS is a simple algorithm for searching a graph. Given a graph G=(V,E) and a distinguished source

vertex “s”, bfs systematically explores the edges of G to “discover” every vertex that is reachable from “s”. It computes the distance from “s” to each reachable vertex.

It also produces a “bfs” tree with root “s” that contains all reachable vertices.

For any vertex “v” reachable from “s”, the path in the bfs tree from “s” to “v” corresponds to a “shortest path” from “s” to “v” in G, that is, a path containing the smallest number of edges.

Works for directed and undirected graphs.

Page 14: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

14

Breadth-first search… BFS is so named because it expands the frontier between

discovered and undiscovered vertices uniformly accross the breadth of the frontier. That is, the algorithm discovers all vertices at a distance “k” from “s” before discovering any vertices at distance “k+1”.

For this purpose, bfs colors each vertex with white, gray, or black.

All vertices are white at the beginning. They may become gray and then black.

A vertex is “discovered” the first time it is encountered during the search, at which time it becomes nonwhite.

Gray and black vertices, therefore, have been “discovered”. However, if (u,v)εE, and, then vertex “v” is either gray or black vertex “u” is black; that is, all vertices adjacent to black vertices have been discovered.

Gray ones may have some adjacent white vertices; they represent the frontier between discovered and undiscovered vertices.

Page 15: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

15

Breadth-first search… BFS constructs a tree, starting with the

root “s”. Whenever, a white vertex is discovered in the course of scanning the adjacency list of an already discovered vertex “u”, the vertex “v” and the edge (u,v) is added to the tree. Here, “u” becomes the predecessor (or parent) of “v”.

Since a vertex is discovered at most once, it has at most one parent.

Page 16: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

16

Breadth-first search… BFS assumes that G is represented by an

adjacency list. It also maintains several data structures to represent colors (gray,white, and black), distance, and the predecessor data. color[vertex] (gray, white or black) d[u] 0,1,2,.. and π[u] (NIL, or the predecessor vertex)

Algorithm is given next. Running time of BFS is O(V+E)

Page 17: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

17

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 18: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

18

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 19: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

19

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Print out the vertices on a shortest path from s to v

Page 20: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

20

Depth-first Search (DFS) The strategy is to search “deeper” in the graph

whenever possible. In DFS, edges are explored out of the most recently discovered vertex “v” that still has unexplored edges leaving it.

When all of the “v”s edges have been explored, the search “backtracks” to explore edges leaving the vertex from which “v” was discovered.

This process continues until we have discovered all the vertices that are reachable from the original source vertex.

If any undiscovered vertices remain, then one of them is selected as a new source and the search is repeated from that source.

Page 21: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

21

Depth-first Search (DFS)… Whenever a vertex “v” is discovered

during a scan of the adjacency list of an already discovered vertex “u”, dfs records this event by setting π[v] to u.

Unlike, bfs, predecessor subgraph produced by a dfs may consist of several trees, because the search may be repeated from different sources

Page 22: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

22

Depth-first Search (DFS)… The predecessor subgraph Gπ=(V,E π) where

E π={(π[v],v): v εV and π[v]≠NIL} The predecesor subgraph of a dfs forms a

depth-first forest. Vertices are again colored during the search.

Each vertex is initially white, is grayed when it is discovered in the search, and is blackened when it is finished, that is, when its adjacency list has been examined completely.

Page 23: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

23

Depth-first Search (DFS)… Besides creating a df forest, dfs also timestamps

each vertex. Each vertex v has two timestamps: d[v] records when v is first discovered (gray) and f[v] when the search finishes examining “v”s

adjacency list (and blackens “v”). These timestamps are between 1 and 2|V|. (Because

of discovery and finishing events for each vertex) For every vertex u, d[u]< f[u]

Vertex u is white before time d[u], and gray between time d[u] and time f[u], and black afterwards.

G can be either directed or undirected.

Page 24: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

24

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 25: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

25

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 26: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

26

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 27: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

27

Properties of DFS Running time of DFS is Θ(V+E) DFS reveals valuable info about the structure of

a graph Predecessor subgraph Gπ form a forest of trees.

Discovery and finishing times have parenthesis structure. If we represent the discovery of vertex “u” with a left parenthesis, then the history of discoveries and finishings makes a well-formed expression (properly nested)

Page 28: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

28

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 29: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

29

Topological Sort

Page 30: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

30

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 31: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

31

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 32: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

32

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 33: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

33

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 34: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

34

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 35: Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.

35

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.