Depth First Search. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

Similar documents
Lecture 20: Minimum Spanning Trees (CLRS 23)

CMPS 2200 Fall Graphs. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

Spanning Trees. BFS, DFS spanning tree Minimum spanning tree. March 28, 2018 Cinda Heeren / Geoffrey Tien 1

Weighted Graphs. Weighted graphs may be either directed or undirected.

CMSC 451: Lecture 4 Bridges and 2-Edge Connectivity Thursday, Sep 7, 2017

5/1/2018. Huffman Coding Trees. Huffman Coding Trees. Huffman Coding Trees. Huffman Coding Trees. Huffman Coding Trees. Huffman Coding Trees

(Minimum) Spanning Trees

The University of Sydney MATH 2009

COMP 250. Lecture 29. graph traversal. Nov. 15/16, 2017

Strongly connected components. Finding strongly-connected components

Lecture II: Minimium Spanning Tree Algorithms

4.1 Interval Scheduling. Chapter 4. Greedy Algorithms. Interval Scheduling: Greedy Algorithms. Interval Scheduling. Interval scheduling.

Divided. diamonds. Mimic the look of facets in a bracelet that s deceptively deep RIGHT-ANGLE WEAVE. designed by Peggy Brinkman Matteliano

Theorem 1. An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices.

Graph Search (6A) Young Won Lim 5/18/18

Improving Union. Implementation. Union-by-size Code. Union-by-Size Find Analysis. Path Compression! Improving Find find(e)

Minimum Spanning Trees (CLRS 23)

Single Source Shortest Paths (with Positive Weights)

CSE 332. Data Structures and Parallelism

CSE 332. Graphs 1: What is a Graph? DFS and BFS. Data Abstractions. CSE 332: Data Abstractions. A Graph is a Thingy... 2

The R-Tree. Yufei Tao. ITEE University of Queensland. INFS4205/7205, Uni of Queensland

CS 103 BFS Alorithm. Mark Redekopp

Applications of trees

Minimum Spanning Trees (CLRS 23)

Cycles and Simple Cycles. Paths and Simple Paths. Trees. Problem: There is No Completely Standard Terminology!

Graph Search Algorithms

In which direction do compass needles always align? Why?

MAT3707. Tutorial letter 201/1/2017 DISCRETE MATHEMATICS: COMBINATORICS. Semester 1. Department of Mathematical Sciences MAT3707/201/1/2017

Graphs Depth First Search

CSE 332. Graphs 1: What is a Graph? DFS and BFS. Data Abstractions. CSE 332: Data Abstractions. A Graph is a Thingy... 2

2 Trees and Their Applications

Outline. 1 Introduction. 2 Min-Cost Spanning Trees. 4 Example

Having a glimpse of some of the possibilities for solutions of linear systems, we move to methods of finding these solutions. The basic idea we shall

Closed Monochromatic Bishops Tours

Outline. Computer Science 331. Computation of Min-Cost Spanning Trees. Costs of Spanning Trees in Weighted Graphs

V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

17 Basic Graph Properties

The Constrained Longest Common Subsequence Problem. Rotem.R and Rotem.H

learning objectives learn what graphs are in mathematical terms learn how to represent graphs in computers learn about typical graph algorithms

CSE 373: More on graphs; DFS and BFS. Michael Lee Wednesday, Feb 14, 2018

23 Minimum Spanning Trees

CS 241 Analysis of Algorithms

SAMPLE CSc 340 EXAM QUESTIONS WITH SOLUTIONS: part 2

Exam 2 Solutions. Jonathan Turner 4/2/2012. CS 542 Advanced Data Structures and Algorithms

Planar convex hulls (I)

Paths. Connectivity. Euler and Hamilton Paths. Planar graphs.

d e c b a d c b a d e c b a a c a d c c e b

18 Basic Graph Properties

16.unified Introduction to Computers and Programming. SOLUTIONS to Examination 4/30/04 9:05am - 10:00am

Platform Controls. 1-1 Joystick Controllers. Boom Up/Down Controller Adjustments

Planar Upward Drawings

On Hamiltonian Tetrahedralizations Of Convex Polyhedra

An undirected graph G = (V, E) V a set of vertices E a set of unordered edges (v,w) where v, w in V

Minimum Spanning Trees

23 Minimum Spanning Trees

CSC Design and Analysis of Algorithms. Example: Change-Making Problem

Problem solving by search

Grade 7/8 Math Circles March 4/5, Graph Theory I- Solutions

Maximum Matching Width: new characterizations and a fast algorithm for dominating set

, each of which is a tree, and whose roots r 1. , respectively, are children of r. Data Structures & File Management

Minimum Spanning Trees

MATERIAL SEE BOM ANGLES = 2 FINISH N/A

DOCUMENT STATUS: RELEASE

Graphs Breadth First Search

# 1 ' 10 ' 100. Decimal point = 4 hundred. = 6 tens (or sixty) = 5 ones (or five) = 2 tenths. = 7 hundredths.

Face Detection and Recognition. Linear Algebra and Face Recognition. Face Recognition. Face Recognition. Dimension reduction

ECE COMBINATIONAL BUILDING BLOCKS - INVEST 13 DECODERS AND ENCODERS

COMP108 Algorithmic Foundations

Tangram Fractions Overview: Students will analyze standard and nonstandard

Graph Search Algorithms CSE

(4, 2)-choosability of planar graphs with forbidden structures

DOCUMENT STATUS: LA-S5302-XXXXS LA, SSS, TRICEPS EXTENSION VERY

Constructive Geometric Constraint Solving

Minimum Spanning Trees

CSE 373. Graphs 1: Concepts, Depth/Breadth-First Search reading: Weiss Ch. 9. slides created by Marty Stepp


An Introduction to Clique Minimal Separator Decomposition

b.) v d =? Example 2 l = 50 m, D = 1.0 mm, E = 6 V, " = 1.72 #10 $8 % & m, and r = 0.5 % a.) R =? c.) V ab =? a.) R eq =?

1 Introduction to Modulo 7 Arithmetic

CS September 2018

MATERIAL SEE BOM ANGLES = 2 > 2000 DATE MEDIUM FINISH

Dental PBRN Study: Reasons for replacement or repair of dental restorations

CS 461, Lecture 17. Today s Outline. Example Run

A Simple Method for Identifying Compelled Edges in DAGs

Graph Isomorphism. Graphs - II. Cayley s Formula. Planar Graphs. Outline. Is K 5 planar? The number of labeled trees on n nodes is n n-2

Using the Printable Sticker Function. Using the Edit Screen. Computer. Tablet. ScanNCutCanvas

Aquauno Video 6 Plus Page 1

(2) If we multiplied a row of B by λ, then the value is also multiplied by λ(here lambda could be 0). namely

A New Interface to Render Graphs Using Rgraphviz

CHELOURANYAN CALENDAR FOR YEAR 3335 YEAR OF SAI RHAVË

QUESTIONS BEGIN HERE!

12/3/12. Outline. Part 10. Graphs. Circuits. Euler paths/circuits. Euler s bridge problem (Bridges of Konigsberg Problem)

5/9/13. Part 10. Graphs. Outline. Circuits. Introduction Terminology Implementing Graphs

Complete Solutions for MATH 3012 Quiz 2, October 25, 2011, WTT

L.3922 M.C. L.3922 M.C. L.2996 M.C. L.3909 M.C. L.5632 M.C. L M.C. L.5632 M.C. L M.C. DRIVE STAR NORTH STAR NORTH NORTH DRIVE

Preview. Graph. Graph. Graph. Graph Representation. Graph Representation 12/3/2018. Graph Graph Representation Graph Search Algorithms

CS61B Lecture #33. Administrivia: Autograder will run this evening. Today s Readings: Graph Structures: DSIJ, Chapter 12

DOCUMENT STATUS: CORE HEALTH & FITNESS, LLC IL-D2002-XXAAX IP,DUAL ADJUSTIBLE PULLEY MATERIAL SEE BOM FINISH N/A N/A SHEET SIZE: B SCALE: 1:33.

Present state Next state Q + M N

Transcription:

Dprtmnt o Computr Sn n Ennrn Cns Unvrsty o Hon Kon

W v lry lrn rt rst sr (BFS). Toy, w wll suss ts sstr vrson : t pt rst sr (DFS) lortm. Our susson wll on n ous on rt rps, us t xtnson to unrt rps s strtorwr. DFS s surprsnly powrul lortm, n solvs svrl lss prolms lntly. In ts ltur, w wll s on su prolm ttn wtr t nput rp ontns yls.

Pts n Cyls Lt G = (V, E) rt rp. Rll: A pt n G s squn o s (v 1, v 2 ), (v 2, v 3 ),..., (v l, v l+1 ), or som ntr l 1. W my lso not t pt s v 1 v 2... v l+1. W now n: A pt v 1 v 2... v l+1 s ll yl v l+1 = v 1.

Exmpl A yl:. Anotr on:.

Drt Ayl/Cyl Grps I rt rp ontns no yls, w sy tt t s rt yl rp (DAG). Otrws, G s yl. Exmpl Cyl DAG

T Cyl Dtton Prolm Lt G = (V, E) rt rp. Dtrmn wtr t s DAG.

Nxt, w wll sr t pt rst sr (DFS) lortm to solv t prolm n O( V + E ) tm, w s optml (us ny lortm must t lst s vry vrtx n vry on n t worst s). Just lk BFS, t DFS lortm lso outputs tr, ll t DFS-tr. Ts tr ontns vtl normton out t nput rp tt llows us to wtr t nput rp s DAG.

DFS At t nnn, olor ll vrts n t rp wt. An rt n mpty DFS tr T. Crt stk S. Pk n rtrry vrtx v. Pus v nto S, n olor t ry (w mns n t stk ). Mk v t root o T.

Exmpl Suppos tt w strt rom. DFS tr S = ().

DFS Rpt t ollown untl S s mpty. 1 Lt v t vrtx tt urrntly tops t stk S (o not rmov v rom S). 2 Dos v stll v wt out-nor? 2.1 I ys: lt t u. Pus u nto S, n olor u ry. Mk u l o v n t DFS-tr T. 2.2 I no, pop v rom S, n olor v lk (mnn v s on). I tr r stll wt vrts, rpt t ov y rstrtn rom n rtrry wt vrtx v, rtn nw DFS-tr root t v. DFS vs lk xplorn t w on lk t tm, s w wll s nxt.

Runnn Exmpl Top o stk:, w s wt out-nors,. Suppos w ss rst. Pus nto S. DFS tr S = (, ).

Runnn Exmpl Atr pusn nto S: DFS tr S = (,, ).

Runnn Exmpl Now tops t stk. It s wt out-nors n. Suppos w vst rst. Pus nto S. DFS tr S = (,,, ).

Runnn Exmpl Atr pusn nto S: DFS tr S = (,,,, ).

Runnn Exmpl Suppos w vst wt out-nor o rst. Pus nto S DFS tr S = (,,,,, ).

Runnn Exmpl Atr pusn nto S: DFS tr S = (,,,,,, ).

Runnn Exmpl s no wt out-nors. So pop t rom S, n olor t lk. Smlrly, s no wt out-nors. Pop t rom S, n olor t lk. DFS tr S = (,,,, ).

Runnn Exmpl Now tops t stk n. It stll s wt out-nor. So, pus nto S. DFS tr S = (,,,,, ).

Runnn Exmpl Atr poppn,,,,, : DFS tr S = ().

Runnn Exmpl Now tr s stll wt vrtx. So w prorm notr DFS strtn rom. DFS orst S = ().

Runnn Exmpl Pop. T n. DFS orst S = (). Not tt w v rt DFS-orst, w onssts o 2 DFS-trs.

Tm Anlyss DFS n mplmnt ntly s ollows. Stor G n t jny lst ormt. For vry vrtx v, rmmr t out-nor to xplor nxt. O( V + E ) stk oprtons. Us n rry to rmmr t olors o ll vrts. Hn, t totl runnn tm s O( V + E ).

Rll tt w s rlr tt t DFS-tr (wll, prps DFSorst) nos vlul normton out t nput rp. Nxt, w wll mk ts pont sp, n solv t tton prolm.

E Clsston Suppos tt w v lry ult DFS-orst T. Lt (u, v) n n G (rmmr tt t s rt rom u to v). It n lss nto 1 Forwr : u s propr nstor o v n DFS-tr o T. 2 Bkwr : u s snnt o v n DFS-tr o T. 3 Cross : I ntr o t ov ppls.

Exmpl DFS orst Forwr s: (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ). Bkwr : (, ). Cross s: (, ), (, ), (, ).

Atr t DFS-orst T s n otn, w n trmn typ o (u, v) n onstnt tm. All tt s rqur s to umnt t DFS lortm sltly y rmmrn wn vrtx ntrs n lvs t stk.

Aumntn DFS Sltly Mntn ountr, w s ntlly 0. Evry tm pus or pop s prorm on t stk, w nrmnt y 1. For vry vrtx v, n: Its sovry tm -tm(v) to t vlu o rt tr v s pus nto t stk. Its ns tm -tm(v) to t vlu o rt tr v s popp rom t stk. Dn I (v) = [-tm(v), -tm(v)]. It s strtorwr to otn I (v) or ll v V y pyn O( V ) xtr tm on top o DFS s runnn tm. (Tnk: Wy?)

Exmpl DFS orst I () = [1, 16] I () = [2, 15] I () = [3, 14] I () = [4, 13] I () = [5, 12] I ( ) = [6, 9] I () = [7, 8] I () = [10, 11] I () = [17, 18]

Prntss Torm Torm: All t ollown r tru: I u s propr nstor o v n DFS-tr o T, tn I (u) ontns I (v). I u s propr snnt o v n DFS-tr o T, tn I (u) s ontn n I (v). Otrws, I (u) n I (v) r sjont. Proo: Follows rtly rom t rst-n-lst-out proprty o t stk.

Cyl Torm Torm: Lt T n rtrry DFS-orst. G ontns yl n only tr s kwr wt rspt to T. Proo: T -rton s ovous. Provn t only- rton s mor nvolv, n wll on ltr.

Cyl Dtton Equpp wt t yl torm, w know tt w n tt wtr G s yl sly tr vn otn DFS-orst T : For vry (u, v), trmn wtr t s kwr n O(1) tm. I no kwr s r oun, G to DAG; otrws, G s t lst yl. Only O( E ) xtr tm s n. W now onlu tt t yl tton prolm n solv n O( V + E ) tm.

It rmns to prov t yl torm. W wll rst prov notr mportnt torm, n tn stls t yl torm s orollry.

Wt Pt Torm Torm: Lt u vrtx n G. Consr t momnt wn u s pus nto t stk n t DFS lortm. Tn, vrtx v oms propr snnt o u n t DFS-orst n only t ollown s tru: W n o rom u to v y trvln only on wt vrts. Proo: Wll lt s n xrs (wt soluton prov). Prs rntly, t torm sys tt t sr t u wll t stuk only tr sovrn ll t vrts tt n stll sovr.

Exmpl Consr t momnt n our prvous xmpl wn just ntr t stk. S = (,,,, ). DFS tr W n s tt n r,, y oppn on only wt vrts. Tror,,, r ll propr snnts o n t DFS-orst; n s no otr snnts.

Provn t Only-I Drton o t Cyl Torm W wll now prov tt G s yl, tn tr must kwr n t DFS-orst. Suppos tt t yl s v 1 v 2... v l v 1. Lt v, or som [1, l], t vrtx n t yl tt s t rst to ntr t stk. Tn, y t wt pt torm, ll t otr vrts n t yl must propr snnts o v n t DFS-orst. Ts mns tt t pontn to v n t yl s k. W tus v omplt t wol proo o t yl torm.