Download presentation

Presentation is loading. Please wait.

Published byZoie Spalding Modified over 2 years ago

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

2
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”.

3
3 More definitions… A path in a graph is a sequence of vertices w 1, w 2, …,w N such that (w i,w i+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 w 1 = w N 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.

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

5
5 Graph Representations

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

7
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).

8
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=(a ij ) s.t Figures 22.1(c) and 22.2(c) Memory requirements Θ(V 2 )

9
9 Transpose of Adj-Matrix Transpose of a matrix A=(a ij ) is A T =(a ij 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).

10
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 765 4 2 1230 24510 36710 420 520 630 730

11
11 Adj-Matrix repr. 1234567 10110000 21001100 31000011 40100000 50100000 60010000 70010000

12
12 22.1.3 The transpose of a directed graph G=(V,E) is the graph G T =(V, E T ), where E T ={(v,u)εVxV: (u,v) εE}. Thus, G T is G with all its edges reversed. Describe efficient algorithms for computing G T from G, for the adj-matrix representation of G. Analyze the running time. Answer: Take the transpose of the adj- matrix. O(E 2 )

13
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.

14
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.

15
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.

16
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)

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

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

19
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

20
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.

21
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

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.

23
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.

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

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

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

27
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)

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

29
29 Topological Sort

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

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

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

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

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

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

Similar presentations

OK

ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture eight Dr. Hamdy M. Mousa.

ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture eight Dr. Hamdy M. Mousa.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on akbar's administration Ppt on ganga action plan download Ppt on biodiversity and evolution Ppt on electronic product design Download ppt on floods in india Ppt on second law of thermodynamics biology Ppt on complete c language Ppt on emotional intelligence and management Pdf to ppt online Ppt online compressor mechanic school