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

Slides:



Advertisements
Similar presentations
Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Advertisements

Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
CS 473Lecture 141 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture eight Dr. Hamdy M. Mousa.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Breadth First Search. Two standard ways to represent a graph –Adjacency lists, –Adjacency Matrix Applicable to directed and undirected graphs. Adjacency.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
1 Graph Programming Gordon College. 2 Graph Basics A graph G = (V, E) –V = set of vertices, E = set of edges –Dense graph: |E|  |V| 2 ; Sparse graph:
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
Shortest Path Problems
1 Data Structures DFS, Topological Sort Dana Shapira.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Graph Algorithms: Part 1
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
Tirgul 11 BFS,DFS review Properties Use. Breadth-First-Search(BFS) The BFS algorithm executes a breadth search over the graph. The search starts at a.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
Lecture 10 Graph Algorithms. Definitions Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). An edge can connect two.
Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Graph Algorithms Introduction to Algorithms Graph Algorithms CSE 680 Prof. Roger Crawfis Partially from io.uwinnipeg.ca/~ychen2.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
1 Depth-First Search Idea: –Starting at a node, follow a path all the way until you cannot move any further –Then backtrack and try another branch –Do.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Elementary Graph Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
Graph Algorithms Searching. Review: Graphs ● A graph G = (V, E) ■ V = set of vertices, E = set of edges ■ Dense graph: |E|  |V| 2 ; Sparse graph: |E|
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
David Luebke 1 1/6/2016 CS 332: Algorithms Graph Algorithms.
Graph. Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices.
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
Chapter 05 Introduction to Graph And Search Algorithms.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
G RAPH A LGORITHMS Dr. Tanzima Hashem Assistant Professor CSE, BUET.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
Introduction to Algorithms
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Finding Shortest Paths
Advanced Algorithms Analysis and Design
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

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

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 Applications Computer Networks Traffic flow Airport/Flight Systems Transportation problem Scheduling problems

5 Graph Representations

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

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 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 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 Exercises (pp.530/ ) Give an adjacency-list & adjacency-matrix representations for a complete binary tree on 7 vertices. Assume that all vertices are numbered from

11 Adj-Matrix repr

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 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 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 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 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 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

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

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 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 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 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 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 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

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

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

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 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

29 Topological Sort

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

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

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

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

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

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