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

Graph Representation

Basic Graph Algorithms

Chapter 22. Elementary Graph Algorithms

Representations of Graphs

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

Graph representation

Elementary Graph Algorithms

Design and Analysis of Algorithms

Elementary Graph Algorithms

Graph Search. Adnan Aziz

Graph: representation and traversal

DFS & STRONGLY CONNECTED COMPONENTS

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

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

Design and Analysis of Algorithms

Minimum Spanning Trees Ch 23 Traversing graphs

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

CSI 604 Elementary Graph Algorithms

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

Graph Algorithms. Definition

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Introduction to Algorithms. Lecture 11

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

Outlines: Graphs Part-2

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

COT 6405 Introduction to Theory of Algorithms

Strongly Connected Components

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

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

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

Graph Algorithms: Chapters Part 1: Introductory graph concepts

22.3 Depth First Search

Graph implementations :

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

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

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

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

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

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

Announcements. HW3 is graded. Average is 81%

Review: Graph Theory and Representation

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

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

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

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

Unit 2: Algorithmic Graph Theory

Graph: representation and traversal

Chapter 22 Elementary Graph Algorithms

Graph Representations and Traversal

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

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

W4231: Analysis of Algorithms

Graph. Vertex. edge. Directed Graph. Undirected Graph

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

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

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

Topic 12: Elementary Graph Algorithms

Announcements Problem Set 4 is out!

Data Structures and Algorithms. Chapter 7. Graphs

CSC263 Week 8. Larry Zhang.

Data Structures and Algorithms. Werner Nutt

csci 210: Data Structures Graph Traversals

Lecture 10: Strongly Connected Components, Biconnected Graphs

ECE250: Algorithms and Data Structures Elementary Graph Algorithms Part B

Lecture 9 Graph Traversal

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

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

Algorithm Design and Analysis

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

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

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

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

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

Figure 1: A directed graph.

CS 310 Advanced Data Structures and Algorithms

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Algorithm Design and Analysis

22.1 Representations of graphs

ECE250: Algorithms and Data Structures Single Source Shortest Paths Bellman-Ford Algorithm

Sample Solutions to Homework #4

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

Algorithm Design and Analysis

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

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

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

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

Copyright 2000, Kevin Wayne 1

Suggested Study Strategy

Introduction. I Given a weighted, directed graph G =(V, E) with weight function

Graphs: basic concepts and algorithms

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

Plan. CMPSCI 311: Introduction to Algorithms. Recall. Adjacency List Representation. DFS Descriptions. BFS Description

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Transcription:

Computer Science & Engineering 423/823 Design and Analysis of Algorithms Lecture 04 Elementary Graph Algorithms (Chapter 22) Stephen Scott (Adapted from Vinodchandran N. Variyam) sscott@cse.unl.edu

Introduction I Graphs are abstract data types that are applicable to numerous problems I Can capture entities, relationships between them, the degree of the relationship, etc. I This chapter covers basics in graph theory, including representation, and algorithms for basic graph-theoretic problems (some content was covered in review lecture) I We ll build on these later this semester

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

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

BFS Example

BFS Example (2)

BFS Properties I What is the running time? I Hint: How many times will a node be enqueued? I After the end of the algorithm, d[v] = shortest distance from s to v ) Solves unweighted shortest paths I Can print the path from s to v by recursively following [v], [ [v]], etc. I If d[v] ==1, thenv not reachable from s ) Solves reachability

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

DFS(G) 1 for each vertex u 2 V do 2 color[u] =white 3 [u] =nil 4 end 5 time =0 6 for each vertex u 2 V do 7 if color[u] ==white then 8 DFS-Visit(u) 9 10 end

DFS-Visit(u) 1 color[u] =gray 2 time = time +1 3 d[u] =time 4 for each v 2 Adj[u] do 5 if color[v] ==white then 6 [v] =u 7 DFS-Visit(v) 8 9 end 10 color[u] =black 11 f [u] =time = time +1

DFS Example

DFS Example (2)

DFS Properties I Time complexity same as BFS: ( V + E ) I Vertex u is a proper descendant of vertex v in the DF tree i d[v] < d[u] < f [u] < f [v] ) Parenthesis structure: If one prints (u when discovering u and u) when finishing u, thenprintedtextwillbeawell-formedparenthesized sentence

DFS Properties (2) I Classification of edges into groups I A tree edge is one in the depth-first forest I A back edge (u, v) connects a vertex u to its ancestor v in the DF tree (includes self-loops) I A forward edge is a nontree edge connecting a node to one of its DF tree descendants I A cross edge goes between non-ancestral edges within a DF tree or between DF trees I See labels in DFS example I Example use of this property: A graph has a cycle i DFS discovers a back edge (application: deadlock detection) I When DFS first explores an edge (u, v), look at v s color: I color[v] == white implies tree edge I color[v] ==gray implies back edge I 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 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 Algorithm 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 I Thus topological sort is a descending sort of vertices based on DFS finishing times I What is the time complexity? I Why does it work? I 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

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 2 Cuis reachable from v and v is reachable from u What are the SCCs of the above graph?

Component Graph Collapsing edges within each component yields acyclic component graph

Transpose Graph I Algorithm for finding SCCs of G depends on the transpose of G, denoted G T I G T is simply G with edges reversed I Fact: G T and G have same SCCs. Why?

SCC Algorithm 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

SCC Algorithm Example After first round of DFS: Which node is first one to be visited in second DFS?

SCC Algorithm Example (2) After second round of DFS:

SCC Algorithm Analysis I What is its time complexity? I 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 (Lemma 22.14), so the second DFS s tree edges define exactly x s component 3. Now let x 0 be the next node explored in a new component C 0 4. The only edges from C 0 to another component are to nodes in C, sothe DFS tree edges define exactly the component for x 0 5. And so on... I In other words, DFS on G T visits components in order of a topological sort of G s component graph ) First component node of G T visited has no outgoing edges (since in G it has only incoming edges), second only has edges into the first, etc.

Intuition I For algorithm to work, need to start second DFS in component abe I How do we know that some node in abe will have largest finish time? I If first DFS in G starts in abe, then it visits all other reachable components and finishes in abe ) one of {a, b, e} will have largest finish time I If first DFS in G starts in component downstream of abe, thenthat DFS round will not reach abe ) to finish in abe, you have to start there at some point ) you will finish there last (see above)