Chapter 22 Elementary Graph Algorithms

Similar documents
Graph representation

Design and Analysis of Algorithms

Graph Representation

Elementary Graph Algorithms

Graph: representation and traversal

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

Representations of Graphs

Graph Algorithms: Chapters Part 1: Introductory graph concepts

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

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

Elementary Graph Algorithms

Chapter 22. Elementary Graph Algorithms

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

DFS & STRONGLY CONNECTED COMPONENTS

Topic 12: Elementary Graph Algorithms

Outlines: Graphs Part-2

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

COT 6405 Introduction to Theory of Algorithms

Minimum Spanning Trees Ch 23 Traversing graphs

Basic Graph Algorithms

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

Data Structures and Algorithms. Chapter 7. Graphs

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

Design and Analysis of Algorithms

Unit 2: Algorithmic Graph Theory

22.3 Depth First Search

Announcements. HW3 is graded. Average is 81%

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

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

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

W4231: Analysis of Algorithms

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

Graph Search. Adnan Aziz

Data Structures and Algorithms. Werner Nutt

Strongly Connected Components

Lecture 9 Graph Traversal

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

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

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

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

Algorithm Design and Analysis

(Re)Introduction to Graphs and Some Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms

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

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

CSE 2331/5331. Topic 9: Basic Graph Alg. Representations Basic traversal algorithms Topological sort CSE 2331/5331

ECE250: Algorithms and Data Structures Elementary Graph Algorithms Part A

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

Chapter 22: Elementary Graph Algorithms. Definitions:

CSI 604 Elementary Graph Algorithms

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 5: SCC/BFS

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms

Graph: representation and traversal

Graph Algorithms. Definition

Algorithm Design and Analysis

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

Graph implementations :

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Algorithm Design and Analysis

3.1 Basic Definitions and Applications

TIE Graph algorithms

Graph Representations and Traversal

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

Graph. Vertex. edge. Directed Graph. Undirected Graph

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

Introduction to Algorithms. Lecture 11

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

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

CS 310 Advanced Data Structures and Algorithms

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

22.1 Representations of graphs

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Algorithm Design and Analysis

Cut vertices, Cut Edges and Biconnected components. MTL776 Graph algorithms

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

csci 210: Data Structures Graph Traversals

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

Figure 1: A directed graph.

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

Homework No. 4 Answers

Review: Graph Theory and Representation

- Logic and Algorithms - Graph Algorithms

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

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

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

TIE Graph algorithms

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

CSE 100: GRAPH ALGORITHMS

Inf 2B: Graphs II - Applications of DFS

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

Graph Algorithms. Textbook reading. Chapter 3 Chapter 4. CSci 3110 Graph Algorithms 1/41

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

Outline. Graphs. Divide and Conquer.

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

Inf 2B: Graphs, BFS, DFS

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

CS 206 Introduction to Computer Science II

Graph Algorithms. Andreas Klappenecker

Transcription:

Chapter 22 Elementary Graph Algorithms

Graph Representations Graph G = (V,E) Directed Undirected Adjacency Lists Adjacency Matrix

Graph Representations Adjacency List: Undirected Memory: Adjacency:

Graph Representations Adjacency List: Undirected Memory: O(V+E) Adjacency:

Graph Representations Adjacency List: Undirected Memory: O(V+E) Adjacency: O(V)

Graph Representations Adjacency List: Directed Memory: O(V+E) Adjacency: O(V)

Graph Representations Adjacency Matrix: Undirected Memory: Adjacency:

Graph Representations Adjacency Matrix: Undirected Memory:!(V 2 ) Adjacency:

Graph Representations Adjacency Matrix: Undirected Memory:!(V 2 ) Adjacency:!(1)

Graph Representations Adjacency Matrix: Undirected Memory:!(V 2 ) Adjacency:!(1)

Breadth First Search Idea: Send a wave out from s First hits all vertices 1 away from s From there, hits all vertices 2 from s Etc. Use FIFO Queue Q to maintain wavefront v " Q if and only if the wave has hit v but has not come out of v yet d[v] is distance from s to u

Breadth First Search

BFS: Example r s t u 0 v w x y Q: s

BFS: Example r s t u 1 0 1 v w x y Q: w,r

BFS: Example r s t u 1 0 2 1 2 v w x y Q: r,t,x

BFS: Example r s t u 1 0 2 2 1 2 v w x y Q: t,x,v

BFS: Example r s t u 1 0 2 3 2 1 2 v w x y Q: x,v,u

BFS: Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: v,u,y

BFS: Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: u,y

BFS: Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: y

BFS: Example r s t u 1 0 2 3 2 1 2 3 v w x y Q:

Breadth First Search Analysis:

Breadth First Search Analysis: O(V) because every vertex is enqueued at most once. O(E) because every edge is dequeued at most once and we examine (u,v) only when u is dequeued. Therefore, every edge is examined at least once when directed, at least twice when undirected. Time = O(V+E)

Depth First Search Idea: Methodically explore every edge As soon as we discover a vertex, we explore it Use the stack to search the graph d[v] is the discovery time f [v] is the finishing time Every vertex has a color - white = undiscovered - grey = discovered, but not finished - black = finished (we have discovered all vertices reachable by it) Time is a global variable

Depth First Search

DFS: Example u v w 1/ x y z

DFS: Example u v w 1/ 2/ x y z

DFS: Example u v w 1/ 2/ 3/ x y z

DFS: Example u v w 1/ 2/ 4/ 3/ x y z

DFS: Example u v w 1/ 2/ 4/5 3/ x y z

DFS: Example u v w 1/ 2/ 4/5 3/6 x y z

DFS: Example u v w 1/ 2/7 4/5 3/6 x y z

DFS: Example u v w 1/8 2/7 4/5 3/6 x y z

DFS: Example u v w 1/8 2/7 9/ 4/5 3/6 x y z

DFS: Example u v w 1/8 2/7 9/ 4/5 3/6 10/ x y z

DFS: Example u v w 1/8 2/7 9/ 4/5 3/6 10/11 x y z

DFS: Example u v w 1/8 2/7 9/12 4/5 3/6 10/11 x y z

Depth First Search Analysis:

Depth First Search Analysis: Time =!(V+E)! because it is guaranteed to examine each vertex and edge

Shortest Path: BFS Extension: Use #[u] to store the predecessor variable of u Algorithm: PRINT-PATH(G,s,v) if v = s then print s else if #[v] = NIL then print no path from s to v exits else PRINT-PATH(G,s,![v]) print v

Web Crawling Google: Web can be considered a large graph Vertices = web pages Edges = hyperlinks How do you index all the available pages? BFS using the links Why not DFS?

Parenthesis Structure: DFS u v w 1/8 2/7 9/12 4/5 3/6 10/11 x y z

Parenthesis Structure: DFS u v w z y x 1 (u 2 (v 3 (y 4 (x 5 x) 6 y) 7 v) 8 u) 9 (w 10 (z 11 z) 12 w)

Finding Cycles If G contains a back edge, it is cyclic Algorithm: When exploring all vertices u adjacent to v check the color of u If u is black, then there is a cycle

Topological Sort: DFS Used to find precedence in a DAG (no cycles) Good for modeling processes that have a partial order a > b and b > c therefore a > c But may have a and b such that neither a > b nor b > c It makes a total order from from a partial order

Topological Sort: Goalie Example socks shorts T-shirt batting glove hose chest pad pants skates leg pads sweater mask catch glove blocker

Topological Sort Algorithm: Perform a DFS Topological order is finish times in decreasing order When a vertex is finished, push it onto the front of a list Time =!(V+E)

Topological Sort: Goalie Example 25/26 15/24 socks shorts 16/23 hose 17/22 pants 18/21 skates 19/20 leg pads 7/14 T-shirt 8/13 chest pad 9/12 sweater 10/11 mask 2/5 catch glove 3/4 blocker 1/6 batting glove

Topological Sort: Goalie Example 26 socks 24 shorts 23 hose(r) 22 pants 21 skates 20 leg pads 14 t-shirt 13 chest pad 12 sweater 11 mask 6 batting glove 5 catch glove 4 blocker

Topological Sort: Volume Rendering

Topological Sort: Volume Rendering

Topological Sort: Volume Rendering 6 7 5 B 3 2 4 1 A p A < B p

Topological Sort: Volume Rendering Idea: Define ordering relations by looking at shared faces. A B C E D F B < A A < C B < E C < E C < D E < F D < F Viewing direction

Strongly Connected Components Definition: A SCC of G is a maximal set of vertices C $ V such that for all u, v " C, both u! v and v! u

Strongly Connected Components Algorithm: Call DFS(G) to compute finishing times f[u] for each vertex u Compute G T (!(V+E)) Call DFS(G T ), but in the main loop of DFS, consider the vertices oin order of decreasing f[u] (as computed in step 1) Output the vertices of each tree in the depth-first forest of step 3 as a seperate SCC

Strongly Connected Components: Example

Miscellaneous Finding paths through mazes Routing wires on circuit boards Others?