Data Structures and Algorithms

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

Data Structures and Algorithms

Graph Traversals BFS & DFS 1 CS S-16

Data Structures and Algorithms

Design and Analysis of Algorithms

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

Additional Divide and Conquer Algorithms. Skipping from chapter 4: Quicksort Binary Search Binary Tree Traversal Matrix Multiplication

Elementary Graph Algorithms

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

Introduction to Computer Science II CS S-18 Linked Lists

Strongly Connected Components

Info 2950, Lecture 16

Graph Algorithms (part 3 of CSC 282),

Data Structures and Algorithms

Basic Graph Algorithms

Depth-first search in undirected graphs What parts of the graph are reachable from a given vertex?

Graphs. Data Structures 1 Graphs

CS483 Design and Analysis of Algorithms

ECE 242 Data Structures and Algorithms. Graphs II. Lecture 27. Prof.

Directed Graphs (II) Hwansoo Han

Graph Representation

Data Structures and Algorithms

Strongly Connected Components. Andreas Klappenecker

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

Graph representation

3.1 Basic Definitions and Applications

DFS & STRONGLY CONNECTED COMPONENTS

Solutions to Midterm 2 - Monday, July 11th, 2009

Global Constraints. Combinatorial Problem Solving (CPS) Enric Rodríguez-Carbonell (based on materials by Javier Larrosa) February 22, 2019

Representations of Graphs

Introduction to Computer Science II CS S-20 Linked Lists IV

Algorithms and Data Structures 2014 Exercises and Solutions Week 9

Introduction to Computer Science II CS S-20 Linked Lists III

Data Structure Lecture#23: Graphs (Chapter 11) U Kang Seoul National University

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

Data Structures and Algorithms

Chapter 22 Elementary Graph Algorithms

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

Chapter 22. Elementary Graph Algorithms

Inf 2B: Graphs II - Applications of DFS

Breadth First Search. Graph Traversal. CSE 2011 Winter Application examples. Two common graph traversal algorithms

CS302 - Data Structures using C++

A Linear-time Algorithm for Testing the Truth of Certain Quantified Boolean Formulas by Aspvall, B., Plass, F.M.

Elementary Graph Algorithms CSE 6331

Algorithms CMSC Homework set #2 due January 21, 2015

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

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

Konigsberg Bridge Problem

CS261: Problem Set #2

Data Structures and Algorithms

Mystery Algorithm! ALGORITHM MYSTERY( G = (V,E), start_v ) mark all vertices in V as unvisited mystery( start_v )

Chapter 28 Graphs and Applications. Objectives

CHAPTER 13 GRAPH ALGORITHMS ORD SFO LAX DFW

Definition Example Solution

Graph Algorithms. Definition

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

DFS on Directed Graphs BOS. Outline and Reading ( 6.4) Digraphs. Reachability ( 6.4.1) Directed Acyclic Graphs (DAG s) ( 6.4.4)

ECE4050 Data Structures and Algorithms. Lecture 8: Graphs

Mathematics Masters Examination

6.854J / J Advanced Algorithms Fall 2008

1 Connected components in undirected graphs

Depth-First Search and Template Patterns

Outline. Graphs. Divide and Conquer.

15-451/651: Design & Analysis of Algorithms October 5, 2017 Lecture #11: Depth First Search and Strong Components last changed: October 17, 2017

22.3 Depth First Search

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

Advanced algorithms. topological ordering, minimum spanning tree, Union-Find problem. Jiří Vyskočil, Radek Mařík 2012

Computational Optimization ISE 407. Lecture 19. Dr. Ted Ralphs

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Unit 2: Algorithmic Graph Theory

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

A loose end: binary search

Part IV. Common Reduction Targets

Week 9. CS 400 Programming III

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Total Score /1 /20 /41 /15 /23 Grader

Unweighted Graphs & Algorithms

Strongly connected: A directed graph is strongly connected if every pair of vertices are reachable from each other.

Let G = (V, E) be a graph. If u, v V, then u is adjacent to v if {u, v} E. We also use the notation u v to denote that u is adjacent to v.

Graph Algorithms (part 3 of CSC 282),

Skill Sets Chapter 5 Functions

Data Structures and Algorithms

CSE 101. Algorithm Design and Analysis Miles Jones and Russell Impagliazzo Miles Office 4208 CSE Building

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

Sample Solutions to Homework #4

Directed Graphs BOS Goodrich, Tamassia Directed Graphs 1 ORD JFK SFO DFW LAX MIA

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

Analysis of Algorithms Prof. Karen Daniels

23 DECOMPOSITION OF GRAPHS

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Algorithm design. algorithms, instances, correctness efficiency, order of growth, tractability a paradigm: exhaustive search

Larger K-maps. So far we have only discussed 2 and 3-variable K-maps. We can now create a 4-variable map in the

Recitation Caches and Blocking. 4 March 2019

CS4800: Algorithms & Data Jonathan Ullman

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

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

Preamble. Singly linked lists. Collaboration policy and academic integrity. Getting help

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018

Lecture 16: Directed Graphs

Transcription:

Data Structures an Algorithms CS-0S-9 Connecte Components Davi Galles Department o Computer Science University o San Francisco

9-0: Strongly Connecte Graph Directe Path rom every noe to every other noe Strongly Connecte

9-: Strongly Connecte Graph Directe Path rom every noe to every other noe Strongly Connecte

9-: Connecte Components Subgraph (subset o the vertices) that is strongly connecte.

9-: Connecte Components Subgraph (subset o the vertices) that is strongly connecte.

9-: Connecte Components Subgraph (subset o the vertices) that is strongly connecte.

9-: Connecte Components Subgraph (subset o the vertices) that is strongly connecte.

9-6: Connecte Components Connecte components o the graph are the largest possible strongly connecte subgraphs I we put each vertex in its own component each component woul be (trivially) strongly connecte Those woul not be the connecte components o the graph unless there were no larger connecte subgraphs

9-: Connecte Components Calculating Connecte Components Two vertices v an v are in the same connecte component i an only i: Directe path rom v to v Directe path rom v to v To in connecte components in irecte paths Use DFS

9-8: DFS Revisite We can keep track o the orer in which we visit the elements in a Depth-First Search For any vertex v in a DFS: [v] = Discovery time when the vertex is irst visite [v] = Finishing time when we have inishe with a vertex (an all o its chilren)

9-9: DFS Revisite class Ege { public int neighbor; public int next; } voi DFS(Ege G[], int vertex, boolean Visite[], int [], int []) { Ege tmp; Visite[vertex] = true; [vertex] = time++; or (tmp = G[vertex]; tmp!= null; tmp = tmp.next) { i (!Visite[tmp.neighbor]) DFS(G, tmp.neighbor, Visite); } [vertex] = time++; }

9-0: DFS Revisite To visit every noe in the graph: TraverseDFS(Ege G[]) { int i; boolean Visite = new boolean[g.length]; int = new int[g.length]; int v = new int[g.length]; time = ; or (i=0; i<g.length; i++) Visite[i] = alse; or (i=0; i<g.length; i++) i (!Visite[i]) DFS(G, i, Visite,, ); }

9-: DFS Example

9-: DFS Example

9-: DFS Example

9-: DFS Example

9-: DFS Example

9-6: DFS Example

9-: DFS Example

9-8: DFS Example 6

9-9: DFS Example 6

9-0: DFS Example 8 6

9-: DFS Example 8 9 6

9-: DFS Example 0 8 9 6

9-: DFS Example 0 8 9 6

9-: DFS Example 0 8 9 6

9-: DFS Example 0 8 9 6

9-6: DFS Example 0 8 9 6

9-: DFS Example 0 8 9 6

9-8: DFS Example 0 8 6 9 6

9-9: DFS Example

9-0: DFS Example

9-: DFS Example

9-: DFS Example

9-: DFS Example

9-: DFS Example

9-: DFS Example 6

9-6: DFS Example 6

9-: DFS Example 8 6

9-8: DFS Example 9 8 6

9-9: DFS Example 0 9 8 6

9-0: DFS Example 0 9 8 6

9-: DFS Example 0 9 8 6

9-: DFS Example 0 9 8 6

9-: DFS Example 0 9 8 6

9-: DFS Example 0 9 8 6

9-: DFS Example 0 6 9 8 6

9-6: Using [] & [] Given two vertices v an v, what o we know i [v ] < [v ]?

9-: Using [] & [] Given two vertices v an v, what o we know i [v ] < [v ]? Either: Path rom v to v Start rom v Eventually visit v Finish v Finish v

9-8: Using [] & [] Given two vertices v an v, what o we know i [v ] < [v ]? Either: Path rom v to v No path rom v to v Start rom v Eventually inish v Start rom v Eventually inish v

9-9: Using [] & [] I [v ] < [v ]: Either a path rom v to v, or no path rom v to v I there is a path rom v to v, then there must be a path rom v to v [v ] < [v ] an a path rom v to v v an v are in the same connecte component

9-0: Calculating paths Path rom v to v in G i an only i there is a path rom v to v in G T G T is the transpose o G G with all eges reverse I ater DFS, [v ] < [v ] Run secon DFS on G T, starting rom v, an v an v are in the same DFS spanning tree v an v must be in the same connecte component

9-: Connecte Components Run DFS on G, calculating [] times Compute G T Run DFS on G T examining noes in inverse orer o inishing times rom irst DFS Any noes that are in the same DFS search tree in G T must be in the same connecte component

9-: Connecte Components Eg.

9-: Connecte Components Eg. 0 8 6 9 6

9-: Connecte Components Eg. 0 8 6 9 6

9-: Connecte Components Eg. 0 8 6 9 6

9-6: Connecte Components Eg.

9-: Connecte Components Eg. 0 6 9 8 6

9-8: Connecte Components Eg. 0 6 9 8 6

9-9: Connecte Components Eg. 0 6 9 8 6

9-60: Topological Sort How coul we use DFS to o a Topological Sort? (Hint Use iscover an/or inish times)

9-6: Topological Sort How coul we use DFS to o a Topological Sort? (Hint Use iscover an/or inish times) (What oes it mean i noe x inishe beore noe y?)

9-6: Topological Sort How coul we use DFS to o a Topological Sort? Do DFS, computing inishing times or each vertex As each vertex is inishe, a to ront o a linke list This list is a vali topological sort