Depth-first Search (DFS)

Similar documents
Introduction to Algorithms

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456

Chapter 23. Minimum Spanning Trees

Introduction to Algorithms

Minimum Spanning Trees

Minimum Spanning Trees My T. UF

Introduction to Algorithms. Lecture 11

Minimum Spanning Trees Ch 23 Traversing graphs

Greedy Algorithms. At each step in the algorithm, one of several choices can be made.

Introduction to Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Week 11: Minimum Spanning trees

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

22.3 Depth First Search

Minimum Spanning Trees

Minimum Spanning Trees

Lecture Notes for Chapter 23: Minimum Spanning Trees

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

CSE 100 Minimum Spanning Trees Prim s and Kruskal

2pt 0em. Computer Science & Engineering 423/823 Design and Analysis of Algorithms. Lecture 04 Minimum-Weight Spanning Trees (Chapter 23)

CS302 - Data Structures using C++

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

Algorithms and Data Structures: Minimum Spanning Trees I and II - Prim s Algorithm. ADS: lects 14 & 15 slide 1

Three Graph Algorithms

Three Graph Algorithms

Shortest Path Algorithms

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

Introduction: (Edge-)Weighted Graph

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

23.2 Minimum Spanning Trees

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

CS Yaxin.Huang.HWM10- Solution

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

Minimum Spanning Tree (undirected graph)

Minimum Spanning Trees

COT 6405 Introduction to Theory of Algorithms

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Shortest path problems

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

COT 6405 Introduction to Theory of Algorithms

Greedy Algorithms and Matroids. Andreas Klappenecker

22 Elementary Graph Algorithms. There are two standard ways to represent a

Minimum Spanning Tree

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 20. Example. Shortest Paths Definitions

6.1 Minimum Spanning Trees

G205 Fundamentals of Computer Engineering. CLASS 21, Mon. Nov Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

Design and Analysis of Algorithms

Greedy Algorithms and Matroids. Andreas Klappenecker

Partha Sarathi Manal

The minimum spanning tree problem

Solutions to relevant spring 2000 exam problems

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Chapter 22. Elementary Graph Algorithms

Context: Weighted, connected, undirected graph, G = (V, E), with w : E R.

Algorithm Design and Analysis

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

22.1 Representations of graphs

Definition: A graph G = (V, E) is called a tree if G is connected and acyclic. The following theorem captures many important facts about trees.

22 Elementary Graph Algorithms. There are two standard ways to represent a

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Minimum Spanning Trees Ch 23 Traversing graphs

Algorithms on Graphs: Part III. Shortest Path Problems. .. Cal Poly CSC 349: Design and Analyis of Algorithms Alexander Dekhtyar..

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

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

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

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

Algorithm Design and Analysis

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Minimum Spanning Trees Outline: MST

Algorithm Design, Anal. & Imp., Homework 4 Solution

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

Data Structures and Algorithms. Werner Nutt

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

COP 4531 Complexity & Analysis of Data Structures & Algorithms

CS521 \ Notes for the Final Exam

Representations of Graphs

Dijkstra s algorithm for shortest paths when no edges have negative weight.

Elementary Graph Algorithms

Minimum spanning trees

CS583 Lecture 09. Jana Kosecka. Graph Algorithms Topological Sort Strongly Connected Component Minimum Spanning Tree

from notes written mostly by Dr. Carla Savage: All Rights Reserved

Minimum Spanning Trees

Unit 2: Algorithmic Graph Theory

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Analysis of Algorithms, I

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

Graph representation

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

Figure 1: A directed graph.

Graph Algorithms. Definition

Dist(Vertex u, Vertex v, Graph preprocessed) return u.dist v.dist

Title. Ferienakademie im Sarntal Course 2 Distance Problems: Theory and Praxis. Nesrine Damak. Fakultät für Informatik TU München. 20.

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

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

Data Structure and Algorithm I Homework #4 Due: 5pm, Sunday, December 12, 2010

Transcription:

Depth-first Search (DFS) DFS Strategy: First follow one path all the way to its end, before we step back to follow the next path. (u.d and u.f are start/finish time for vertex processing) CH08-320201: Algorithms and Data Structures 433

DFS example CH08-320201: Algorithms and Data Structures 434

DFS example CH08-320201: Algorithms and Data Structures 435

DFS example CH08-320201: Algorithms and Data Structures 436

DFS analysis Each vertex and each edge is processed once. Hence, time complexity is Θ( V + E ). CH08-320201: Algorithms and Data Structures 437

Edge types Different edge types for (u,v): Tree edges (solid): v is white. Backward edges (purple): v is gray. Forward edges (orange): v is black and u.d < v.d Cross edges (red): v is black and u.d > v.d. The tree edges form a forest. This is called the depth-first forest. In an undirected graph, we have no cross edges. CH08-320201: Algorithms and Data Structures 438

5.3 Minimum Spanning Tree CH08-320201: Algorithms and Data Structures 439

Problem Given a connected undirected graph G=(V,E) with weight function w: E -> R. Compute a minimum spanning tree (MST), i.e., a tree that connects all vertices with minimum weight Why of interest? One example would be a telecommunications company laying out cables to a neighborhood. CH08-320201: Algorithms and Data Structures 440

Spanning tree CH08-320201: Algorithms and Data Structures 441

MST CH08-320201: Algorithms and Data Structures 442

Optimal substructure Consider an MST T of graph G (other edges not shown). Remove any edge (u,v) є T. Then, T is partioned into subtrees T 1 and T 2. CH08-320201: Algorithms and Data Structures 443

Theorem (a) Subtree T 1 is an MST of graph G 1 = (V 1, E 1 ) with V 1 being the set of all vertices of T 1 and E 1 being the set of all edges є G that connect vertices є V 1. (b) Subtree T 2 is an MST of graph G 2 = (V 2, E 2 ) with V 2 being the set of all vertices of T 2 and E 2 being the set of all edges є G that connect vertices є V 2. Proof (only (a), (b) is analogous): w(t) = w(t 1 ) + w(t 2 ) + w(u,v) Assume S 1 was an MST for G 1 with lower weight than T 1. Then, S = S 1 υ T 2 υ {(u,v)} would be an MST for G with lower weight than T. Contradiction! CH08-320201: Algorithms and Data Structures 444

Greedy choice property Theorem: Let T be the MST of graph G = (V,E) and let A c V. Let (u,v) є E be the edge with least weight connecting A to V \ A. Then, (u,v) є T. CH08-320201: Algorithms and Data Structures 445

Greedy choice property Proof: Suppose (u,v) is not part of T. Then, consider the path from u to v within T. Replace the first edge on this path that connects a vertex in A to a vertex in V \ A with (u,v). This results in an MST with smaller weight. Contradiction! CH08-320201: Algorithms and Data Structures 446

Prim s algorithm Idea: Develop a greedy algorithm that iteratively increases A and, consequently, decreases V\A. Maintain V\A as a min-priority queue Q (min-priority queue analogous to max-priority queue). Key each vertex in Q with the weight of the leastweight edge connecting it to a vertex in A (if no such edge exists, the weight shall be infinity). Then, always add the vertex of V\A with minimal key to A. CH08-320201: Algorithms and Data Structures 447

Min-priority queues Definition (recall): A priority queue is a data structure for maintaining a set S of elements, each with an associated value called a key. Definition (implementation as min-heap): A min-priority queue is a priority queue that supports the following operations: Minimum (S): return element from S with smallest key. [O(1)] Extract-Min (S): remove and return element from S with smallest key. [O(lg n)] Decrease-Key (S,x,k): decrease the value of the key of element x to k, where k is assumed to be smaller or equal than the current key. [O(lg n)] Insert (S,x): add element x to set S. [O(lg n)] CH08-320201: Algorithms and Data Structures 448

Prim s algorithm CH08-320201: Algorithms and Data Structures 449

Prim s algorithm The output is provided by storing predecessors π[v] of each node v. The set {(v, π[v])} forms the MST. CH08-320201: Algorithms and Data Structures 450

Example CH08-320201: Algorithms and Data Structures 451

Example CH08-320201: Algorithms and Data Structures 452

Example CH08-320201: Algorithms and Data Structures 453

Analysis Notation Θ(V) means Θ( V ). CH08-320201: Algorithms and Data Structures 454

Analysis min-heap CH08-320201: Algorithms and Data Structures 455