Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Similar documents
Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Representations of Graphs

Chapter 22. Elementary Graph Algorithms

Basic Graph Algorithms

Graph Representation

Elementary Graph Algorithms

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Design and Analysis of Algorithms

Graph representation

DFS & STRONGLY CONNECTED COMPONENTS

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 17. Depth-First Search. DFS (Initialize and Go) Last Time Depth-First Search

Graph: representation and traversal

Graph Algorithms. Definition

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 16. Graph Search Algorithms. Recall BFS

Graph Search. Adnan Aziz

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

CSI 604 Elementary Graph Algorithms

Basic Graph Algorithms (CLRS B.4-B.5, )

Graph implementations :

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

Graph Algorithms: Chapters Part 1: Introductory graph concepts

COT 6405 Introduction to Theory of Algorithms

Elementary Graph Algorithms

Graphs. Graphs. Representation of Graphs. CSE 680 Prof. Roger Crawfis. Two standard ways. Graph G = (V, E)» V = set of vertices

Introduction to Algorithms. Lecture 11

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

Minimum Spanning Trees Ch 23 Traversing graphs

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms

Unit 2: Algorithmic Graph Theory

Design and Analysis of Algorithms

Outlines: Graphs Part-2

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Review: Graph Theory and Representation

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

22.3 Depth First Search

Strongly Connected Components

Proof: if not f[u] < d[v], then u still grey while v is being visited. DFS visit(v) will then terminate before DFS visit(u).

W4231: Analysis of Algorithms

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Announcements. HW3 is graded. Average is 81%

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

Graph: representation and traversal

Chapter 22 Elementary Graph Algorithms

CSCE 750, Fall 2002 Notes 6 Page Graph Problems ffl explore all nodes (breadth first and depth first) ffl find the shortest path from a given s

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution

Lecture 9 Graph Traversal

Figure 1: A directed graph.

Practical Session No. 12 Graphs, BFS, DFS Topological Sort

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Announcements Problem Set 4 is out!

Homework Assignment #3 Graph

Graph. Vertex. edge. Directed Graph. Undirected Graph

CSC263 Week 8. Larry Zhang.

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

Graph Representations and Traversal

Topic 12: Elementary Graph Algorithms

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

csci 210: Data Structures Graph Traversals

Data Structures and Algorithms. Chapter 7. Graphs

Sample Solutions to Homework #4

1 Start with each vertex being its own component. 2 Merge two components into one by choosing the light edge

Algorithm Design and Analysis

Graphs: basic concepts and algorithms

Algorithm Design and Analysis

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

811312A Data Structures and Algorithms, , Exercise 6, solution

Data Structures and Algorithms. Werner Nutt

Slides credited from Hsueh-I Lu, Hsu-Chun Hsiao, & Michael Tsai

Homework No. 4 Answers

Lecture 10: Strongly Connected Components, Biconnected Graphs

Suggested Study Strategy

Konigsberg Bridge Problem

CS 310 Advanced Data Structures and Algorithms

Quiz 2 Practice Problems

22.1 Representations of graphs

Graduate Algorithms CS F-15 Graphs, BFS, & DFS

Final Exam. EECS 2011 Prof. J. Elder - 1 -

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

ECE250: Algorithms and Data Structures Elementary Graph Algorithms Part B

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

Graph Algorithms. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

Elementary Graph Algorithms CSE 6331

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

Transcription:

s of s Computer Science & Engineering 423/823 Design and Analysis of Lecture 03 (Chapter 22) Stephen Scott (Adapted from Vinodchandran N. Variyam) 1 / 29 Spring 2010

s of s s are abstract data types that are applicable to numerous problems Can capture entities, relationships between them, the degree of the relationship, etc. This chapter covers basics in graph theory, including representation, and algorithms for basic graph-theoretic problems We ll build on these later this semester 2 / 29

s s of s A (simple, or undirected) graph G = (V, E) consists of V, a nonempty set of vertices and E a set of unordered pairs of distinct vertices called edges A D B E C V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)} 3 / 29

s (2) s A directed graph (digraph) G = (V, E) consists of V, a nonempty set of vertices and E a set of ordered pairs of distinct vertices called edges of s 4 / 29

s (3) s of s A weighted graph is an undirected or directed graph with the additional property that each edge e has associated with it a real number w(e) called its weight 3-6 3 12 7 0 4 Other variations: multigraphs, pseudographs, etc. 5 / 29

of s s of s Adjacency List Adjacency Matrix Two common ways of representing a graph: Adjacency list and adjacency matrix Let G = (V, E) be a graph with n vertices and m edges 6 / 29

Adjacency List s of s Adjacency List Adjacency Matrix a d For each vertex v V, store a list of vertices adjacent to v For weighted graphs, add information to each node How much is space required for storage? c b e a b c d e b c d a e a d c a c e b c d 7 / 29

Adjacency Matrix s of s Adjacency List Adjacency Matrix 8 / 29 a d Use an n n matrix M, where M(i, j) = 1 if (i, j) is an edge, 0 otherwise If G weighted, store weights in the matrix, using for non-edges How much is space required for storage? c b e a 0 a b 1 c 1 d 1 e 0 b 1 0 0 0 1 c 1 0 0 1 1 d 1 0 1 0 1 e 0 1 1 1 0

Breadth-First (BFS) s of s Breadth-First Depth-First Given a graph G = (V, E) (directed or undirected) and a source node s V, BFS systematically visits every vertex that is reachable from s Uses a queue data structure to search in a breadth-first manner Creates a structure called a BFS tree such that for each vertex v V, the distance (number of edges) from s to v in tree is the shortest path in G Initialize each node s color to white As a node is visited, color it to gray ( in queue), then black ( finished) 9 / 29

BFS Algorithm s of s Breadth-First Depth-First 10 / 29 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 for each vertex u V \ {s} do color[u] = white d[u] = π[u] = nil end color[s] = gray d[s] = 0 π[s] = nil Q = Enqueue(Q, s) while Q do u = Dequeue(Q) for each v Adj[u] do if color[v] == white then color[v] = gray d[v] = d[u] + 1 π[v] = u Enqueue(Q, v) end end color[u] = black end

BFS Example 22.2 Breadth-first search s33 s of s Breadth-First Depth-First o o o ffi 0 ffirffiffiffifl r22 ffi!gffifi]$ffifrl zzj o o n ffi tl fffi:$ffiiflffiml 222 flffiffiffiflffifr ZJJ 11 / 29 l:ilfql*l o l::ld::dj;ii:;l o ffi

o BFS Example (2) ffirffiffiffifl r22 o fffi:$ffiiflffim 222 o ffi!gffifi]$ffifrl zzj n flffiffiffiflffifr ZJJ s of s o l:ilfql*l l::ld::dj;ii:;l JJ o ffi 3 Breadth-First Depth-First oa 12 / 29 Figure 22.3 The operation of BFS on an undirected graph. Tree edges are shown shaded as they are produced by BFS. Within each vertex u is shown dful. The queue Q is shown at the beginning of each iteration of the while loop of lines 10-18. Vertex distances are shown next to vertices in the queu:

BFS Properties s of s Breadth-First Depth-First What is the running time? Hint: How many times will a node be enqueued? After the end of the algorithm, d[v] = shortest distance from s to v Solves unweighted shortest paths Can print the path from s to v by recursively following π[v], π[π[v]], etc. If d[v] ==, then v not reachable from s Solves reachability 13 / 29

Depth-First (DFS) s of s Breadth-First Depth-First Another graph traversal algorithm Unlike BFS, this one follows a path as deep as possible before backtracking Where BFS is queue-like, DFS is stack-like Tracks both discovery time and finishing time of each node, which will come in handy later 14 / 29

DFS Algorithm s of s Breadth-First Depth-First 1 2 3 4 5 6 7 8 9 10 for each vertex u V do color[u] = white π[u] = nil end time = 0 for each vertex u V do if color[u] == white then DFS-Visit(u) end end Algorithm 2: DFS(G) 15 / 29

DFS Algorithm (2) s of s Breadth-First Depth-First 16 / 29 1 2 3 4 5 6 7 8 9 10 11 color[u] = gray time = time + 1 d[u] = time for each v Adj[u] do if color[v] == white then π[v] = u DFS-Visit(v) end end color[u] = black f[u] = time = time + 1 Algorithm 3: DFS-Visit(u)

DFS Example cv> 77 n1doq3 suqttos1y qdotg trztuaualg s of s Breadth-First Depth-First (8.) ( a/y? Y,,:#7Y #@@ #@@ 5@ (q) G) {er Lix (c) (3) /ix 'rxz{xz{r 17 / 29 ({) (

(8.) ( DFS Example (2) (q) a/y? Y,,:#7Y #@@ #@@ 5@ G) {er Lix (c) (3) /ix 'rxz{xz{r s of s Breadth-First Depth-First (ttr) { @ il ffir @ ({) ( 18 / 29 'eurr1,{-renocsrp dq peduretseurl ere secrue^.se'pe ro pre.nrog 'ssorc'{c?q erb '{eqt reqleqa. 'urqsrug/eurr1 ol Sulprocce d ro 'J 'g pelewl ere se8pe ee4uon pellssp ro (se3pe.(esr,vrreqlo) eerl ere deql g) pepeqs reqrre se u,r\oqs er' reql,urry1roap eq1 dq peroldxe se8pe sy 'qder8 ere pelcenp e uo SdC urqlfogp qcrees_lsrg_q1dep eq1 yo sseraoro eql p.77 e;rtrfl1g

DFS Properties s of s Breadth-First Depth-First Time complexity same as BFS: Θ( V + E ) Vertex u is a proper descendant of vertex v in the DF tree iff d[v] < d[u] < f[u] < f[v] Parenthesis structure: If one prints (u when discovering u and u) when finishing u, then printed text will be a well-formed parenthesized sentence 19 / 29

DFS Properties (2) s of s Breadth-First Depth-First 20 / 29 Classification of edges into groups A tree edge is one in the depth-first forest A back edge (u, v) connects a vertex u to its ancestor v in the DF tree (includes self-loops) A forward edge is a nontree edge connecting a node to one of its DF tree descendants A cross edge goes between non-ancestral edges within a DF tree or between DF trees See labels in DFS example Example use of this property: A graph has a cycle iff DFS discovers a back edge (application: deadlock detection) When DFS first explores an edge (u, v), look at v s color: color[v] == white implies tree edge color[v] == gray implies back edge color[v] == black implies forward or cross edge

Application: Topological Sort A directed acyclic graph (dag) can represent precedences: an edge (x, y) implies that event/activity x must occur before y s of s Topological Sort Strongly Connected Components 21 / 29

Application: Topological Sort (2) s of s A topological sort of a dag G is an linear ordering of its vertices such that if G contains an edge (u, v), then u appears before v in the ordering Topological Sort Strongly Connected Components 22 / 29

Topological Sort Algorithm s of s Topological Sort Strongly Connected Components 1 Call DFS algorithm on dag G 2 As each vertex is finished, insert it to the front of a linked list 3 Return the linked list of vertices Thus topological sort is a descending sort of vertices based on DFS finishing times Why does it work? When a node is finished, it has no unexplored outgoing edges; i.e. all its descendant nodes are already finished and inserted at later spot in final sort 23 / 29

Application: Strongly Connected Components Given a directed graph G = (V, E), a strongly connected component (SCC) of G is a maximal set of vertices C V such that for every pair of vertices u, v C u is reachable from v and v is reachable from u s of s Topological Sort Strongly Connected Components 24 / 29 What are the SCCs of the above graph?

Transpose s of s Our algorithm for finding SCCs of G depends on the transpose of G, denoted G T G T is simply G with edges reversed Fact: G T and G have same SCCs. Why? Topological Sort Strongly Connected Components 25 / 29

SCC Algorithm s of s Topological Sort Strongly Connected Components 1 Call DFS algorithm on G 2 Compute G T 3 Call DFS algorithm on G T, looping through vertices in order of decreasing finishing times from first DFS call 4 Each DFS tree in second DFS run is an SCC in G 26 / 29

SCC Algorithm Example After first round of DFS: s of s Topological Sort Strongly Connected Components Which node is first one to be visited in second DFS? 27 / 29

SCC Algorithm Example (2) After second round of DFS: s of s Topological Sort Strongly Connected Components 28 / 29

SCC Algorithm Analysis s of s Topological Sort Strongly Connected Components What is its time complexity? How does it work? 1 Let x be node with highest finishing time in first DFS 2 In G T, x s component C has no edges to any other component, so the second DFS s tree edges define exactly x s component 3 Now let x be the next node explored in a new component C 4 The only edges from C to another component are to nodes in C, so the DFS tree edges define exactly the component for x 5 And so on... 29 / 29