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

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

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

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

(Minimum) Spanning Trees

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

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

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

Strongly connected components. Finding strongly-connected components

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

Applications of trees

The University of Sydney MATH 2009

Minimum Spanning Trees (CLRS 23)

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

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

Graph Search Algorithms

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

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

CS 103 BFS Alorithm. Mark Redekopp

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

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

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

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

2 Trees and Their Applications

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 (CLRS 23)

Single Source Shortest Paths (with Positive Weights)

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

Lecture II: Minimium Spanning Tree Algorithms

Planar convex hulls (I)

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

Graphs Depth First Search

17 Basic Graph Properties

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

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

CS 241 Analysis of Algorithms

18 Basic Graph Properties

5/7/13. Part 10. Graphs. Theorem Theorem Graphs Describing Precedence. Outline. Theorem 10-1: The Handshaking Theorem

In which direction do compass needles always align? Why?

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

SAMPLE CSc 340 EXAM QUESTIONS WITH SOLUTIONS: part 2

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

DOCUMENT STATUS: RELEASE

Graphs Breadth First Search

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

23 Minimum Spanning Trees

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

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

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

Graphs. Graphs. Graphs: Basic Terminology. Directed Graphs. Dr Papalaskari 1

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

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

MATERIAL SEE BOM ANGLES = 2 > 2000 DATE MEDIUM FINISH

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

Graph Search Algorithms CSE

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

On Hamiltonian Tetrahedralizations Of Convex Polyhedra

Priority Search Trees - Part I

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

CSCI 104 Graph Representation and Traversals. Mark Redekopp David Kempe

Graphs. CSC 1300 Discrete Structures Villanova University. Villanova CSC Dr Papalaskari

23 Minimum Spanning Trees

MATERIAL SEE BOM ANGLES = 2 FINISH N/A

Round 7: Graphs (part I)

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

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

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

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.

One-Dimensional Computational Topology

A Simple Method for Identifying Compelled Edges in DAGs

Closed Monochromatic Bishops Tours

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

CS200: Graphs. Graphs. Directed Graphs. Graphs/Networks Around Us. What can this represent? Sometimes we want to represent directionality:

Phylogenetic Tree Inferences Using Quartet Splits. Kevin Michael Hathcock. Bachelor of Science Lenoir-Rhyne University 2010

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

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

ECE 407 Computer Aided Design for Electronic Systems. Circuit Modeling and Basic Graph Concepts/Algorithms. Instructor: Maria K. Michael.

Math 166 Week in Review 2 Sections 1.1b, 1.2, 1.3, & 1.4

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

CMSC 451: Lecture 2 Graph Basics Thursday, Aug 31, 2017

QUESTIONS BEGIN HERE!

Shortest Paths in Graphs. Paths in graphs. Shortest paths CS 445. Alon Efrat Slides courtesy of Erik Demaine and Carola Wenk

Math 61 : Discrete Structures Final Exam Instructor: Ciprian Manolescu. You have 180 minutes.

Planar Upward Drawings

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

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

Outlines: Graphs Part-4. Applications of Depth-First Search. Directed Acyclic Graph (DAG) Generic scheduling problem.

DOCUMENT STATUS: MINTP0 E-ST5080, BASE, NO DISPLAY VENDOR: 15.5 INCH MATERIAL SEE BOM FINISH REVISION HISTORY ITEM NO. PART NUMBER DESCRIPTION

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

An Introduction to Clique Minimal Separator Decomposition

CLKOUT CLKOUT VCC CLKOUT RESOUT OSCOUT ALE TEST AD0 66 AD2 INT0 INT0 AD INT1 AD INT2/INTA0 AD5 AD7 AD7 INT AD8 AD8 AD10

Announcements. Not graphs. These are Graphs. Applications of Graphs. Graph Definitions. Graphs & Graph Algorithms. A6 released today: Risk

Solutions for HW11. Exercise 34. (a) Use the recurrence relation t(g) = t(g e) + t(g/e) to count the number of spanning trees of v 1

Sheet Title: Building Renderings M. AS SHOWN Status: A.R.H.P.B. SUBMITTAL August 9, :07 pm

Jonathan Turner Exam 2-10/28/03

COMP108 Algorithmic Foundations

Computer Graphics. Viewing & Projections

0.1. Exercise 1: the distances between four points in a graph

Transcription:

CMPS 2200 Fll 2017 Grps Crol Wnk Sls ourtsy o Crls Lsrson wt ns n tons y Crol Wnk 10/23/17 CMPS 2200 Intro. to Alortms 1

Grps Dnton. A rt rp (rp) G = (V, E) s n orr pr onsstn o st V o vrts (snulr: vrtx), st E V V o s. In n unrt rp G = (V, E), t st E onssts o unorr prs o vrts. rt rp 2 1 unrt rp 2 1 3 4 3 4 In tr s, w v E O( V 2 ). Morovr, G s onnt, tn E V 1. 10/23/17 CMPS 2200 Intro. to Alortms 2

Ajny-mtrx rprsntton T jny mtrx o rp G = (V, E), wr V = {1, 2,, n}, s t mtrx A[1.. n, 1.. n] vn y 1 (, j) E, A[, j] = 0 (, j) E. 2 1 3 4 A 1 2 3 4 1 0 1 1 0 2 0 0 1 0 3 0 0 0 0 4 0 0 1 1 ( V 2 ) stor ns rprsntton. 10/23/17 CMPS 2200 Intro. to Alortms 3

Ajny-lst rprsntton An jny lst o vrtx v V s t lst Aj[v] o vrts jnt to v. 2 1 3 4 Aj[1] = {2, 3} Aj[2] = {3} Aj[3] = {} Aj[4] = {3, 4} For unrt rps, Aj[v] = r(v). For rps, Aj[v] = out-r(v). 10/23/17 CMPS 2200 Intro. to Alortms 4

Ajny-lst rprsntton Hnskn Lmm: Evry s ount tw For unrt rps: v V r(v) = 2 E For rps: v V n-r(v) = v V out-r(v) = E jny lsts us ( V + E ) stor sprs rprsntton W usully us ts rprsntton, unlss stt otrws 10/23/17 CMPS 2200 Intro. to Alortms 5

Grp Trvrsl Lt G=(V,E) (rt or unrt) rp, vn n jny lst rprsntton. V = n, E = m A rp trvrsl vsts vry vrtx: Brt-rst sr (BFS) Dpt-rst sr (DFS) 10/23/17 CMPS 2200 Intro. to Alortms 6

Brt-Frst Sr (BFS) BFS(G=(V,E)) Mrk ll vrts n G s unvst // tm=0 Intlz mpty quu Q or vrtx v V o v s unvst vst v // tm++ Q.nquu(v) BFS_tr(G) BFS_tr(G) wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 10/23/17 CMPS 2200 Intro. to Alortms 7

Exmpl o rt-rst sr wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) Q: 10/23/17 CMPS 2200 Intro. to Alortms 8

Exmpl o rt-rst 0 sr 0 Q: wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 10/23/17 CMPS 2200 Intro. to Alortms 9

Exmpl o rt-rst 0 1 sr 2 1 2 Q: wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 10/23/17 CMPS 2200 Intro. to Alortms 10

Exmpl o rt-rst 0 1 sr 2 3 4 2 3 4 Q: wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 10/23/17 CMPS 2200 Intro. to Alortms 11

Exmpl o rt-rst 0 1 sr 2 3 4 3 4 Q: wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 10/23/17 CMPS 2200 Intro. to Alortms 12

Exmpl o rt-rst 0 1 sr 2 3 4 4 Q: wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 10/23/17 CMPS 2200 Intro. to Alortms 13

Exmpl o rt-rst 0 1 sr 2 3 4 5 5 6 Q: wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 6 10/23/17 CMPS 2200 Intro. to Alortms 14

Exmpl o rt-rst 0 1 sr 2 3 4 5 7 Q: 6 7 wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 6 10/23/17 CMPS 2200 Intro. to Alortms 15

0 1 Exmpl o rt-rst sr 2 3 4 Q: 5 7 8 7 8 wl Q s non-mpty o v = Q.quu() or w jnt to v o 6 w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 10/23/17 CMPS 2200 Intro. to Alortms 16

0 1 Exmpl o rt-rst sr 2 3 4 Q: 5 7 8 8 wl Q s non-mpty o v = Q.quu() or w jnt to v o 6 w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 10/23/17 CMPS 2200 Intro. to Alortms 17

Exmpl o rt-rst 0 1 sr 2 3 4 5 7 8 wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 6 Q: 10/23/17 CMPS 2200 Intro. to Alortms 18

Exmpl o rt-rst Dstn to : 0 1 2 3 4 0 1 sr 2 3 4 Q: 5 7 8 wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 6 10/23/17 CMPS 2200 Intro. to Alortms 19

Brt-Frst Sr (BFS) O(n) O(1) O(n) wtout BFS_tr BFS(G=(V,E)) Mrk ll vrts n G s unvst // tm=0 Intlz mpty quu Q or vrtx v V o v s unvst vst v // tm++ Q.nquu(v) BFS_tr(G) O(m) BFS_tr(G) wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) O((v)) 10/23/17 CMPS 2200 Intro. to Alortms 20

BFS runtm E vrtx s mrk s unvst n t nnn O(n) tm E vrtx s mrk t most on, nquu t most on, n tror quu t most on T tm to pross vrtx s proportonl to t sz o ts jny lst (ts r), sn t rp s vn n jny lst rprsntton O(m) tm Totl runtm s O(n+m) = O( V + E ) 10/23/17 CMPS 2200 Intro. to Alortms 21

Dpt-Frst Sr (DFS) DFS(G=(V,E)) Mrk ll vrts n G s unvst // tm=0 or vrtx v V o v s unvst DFS_r(G,v) DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm 10/23/17 CMPS 2200 Intro. to Alortms 22

Exmpl o pt-rst sr / 0/- : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 23 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm

Exmpl o pt-rst sr / 0/- 1/- : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 24 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm

Exmpl o pt-rst sr / 0/- 1/- 2/- 2/3 : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 25 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm

Exmpl o pt-rst sr / 0/- 1/- 2/3 : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 26 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm

Exmpl o pt-rst sr / 0/- 2/3 : - 1/- 4/- Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 27 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm

Exmpl o pt-rst sr / 0/- 2/3 : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 28 1/- 4/- 5/- DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm

Exmpl o pt-rst sr / 0/- 2/3 : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 29 1/- 4/- 5/- 6/- DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm

Exmpl o pt-rst sr / 0/- 2/3 : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 30 6/- 7/- 7/8 1/- 4/- 5/- DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm

Exmpl o pt-rst sr / 0/- 2/3 : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 31 7/8 1/- 4/- 5/- 6/- 6/9 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm

Exmpl o pt-rst sr / 0/- 2/3 : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 32 1/- 4/- 5/- 7/8 6/9 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm

Exmpl o pt-rst sr / 0/- 2/3 : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 33 5/- 10/- 1/- 4/- 7/8 6/9 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm

Exmpl o pt-rst sr / 0/- 2/3 10/- 11/12 11/- : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 34 5/- 1/- 4/- 7/8 6/9 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm

Exmpl o pt-rst sr / 0/- 2/3 11/12 : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 35 5/- 10/- 10/13 1/- 4/- 7/8 6/9 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm

Exmpl o pt-rst sr / 0/- 2/3 11/12 : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 36 10/13 1/- 4/- 5/- 5/14 7/8 6/9 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm

Exmpl o pt-rst sr / 0/- 2/3 11/12 : - 1/- 4/15 4/- Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 37 10/13 5/14 7/8 6/9 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm

Exmpl o pt-rst sr / 0/- 1/16 1/- 2/3 11/12 : - 4/15 Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 38 10/13 5/14 7/8 6/9 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm

Exmpl o pt-rst sr / 0/17 0/- 1/16 2/3 11/12 : - 4/15 Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 39 10/13 5/14 7/8 6/9 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm

O(1) Dpt-Frst Sr (DFS) O(n) O(n) wtout DFS_r O((v)) wtout rursv ll DFS(G=(V,E)) Mrk ll vrts n G s unvst // tm=0 or vrtx v V o v s unvst DFS_r(G,v) DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm Wt Hnskn Lmm, ll rursv lls r O(m), or totl o O(n + m) runtm 10/23/17 CMPS 2200 Intro. to Alortms 40

DFS lsston / 0/17 1/16 2/3 11/12 10/23/17 CMPS 2200 Intro. to Alortms 41 10/13 5/14 7/8 6/9 4/15 E u v s : tr, t s prt o t pt-rst orst. k, u onnts to n nstor v n ptrst tr. It ols (u)>(v) n (u)<(v). orwr, t onnts u to snnt v n pt-rst tr. It ols (u)<(v). ross, t s ny otr. It ols (u)>(v) n (u)>(v).

Pts, Cyls, Conntvty Lt G=(V,E) rt (or unrt) rp A pt rom v 1 to v k n G s squn o vrts v 1, v 2,,v k su tt (v,v {+1} ) E (or {v,v {+1} } E G s unrt) or ll {1,,k-1}. A pt s smpl ll vrts n t pt r stnt. A pt v 1, v 2,,v k orms yl v 1 =v k. A rp wt no yls s yl. An unrt yl rp s ll tr. (Trs o not v to v root vrtx sp.) A rt yl rp s DAG. (A DAG n v unrt yls t rton o t s s not onsr.) An unrt rp s onnt vry pr o vrts s onnt y pt. A rt rp s stronly onnt or vry pr u,v V tr s pt rom u to v n tr s pt rom v to u. T (stronly) onnt omponnts o rp r t quvln lsss o vrts unr ts rlty rlton. 10/23/17 CMPS 2200 Intro. to Alortms 42

DAG Torm Torm: A rt rp G s yl pt-rst sr o G yls no k s. Proo: : Suppos tr s k (u,v). Tn y nton o k tr woul yl. : Suppos G ontns yl. Lt v t rst vrtx to sovr n, n lt u t prn vrtx n. v s n nstor o u n t pt-rst orst, n (u,v) s k. v u 10/23/17 CMPS 2200 Intro. to Alortms 43

Topolol Sort Topololly sort t vrts o rt yl rp (DAG): Dtrmn : V {1, 2,, V } su tt (u, v) E (u) < (v). 1 4 7 8 2 3 5 6 9 1 2 3 4 5 6 7 8 9 10/23/17 CMPS 2200 Intro. to Alortms 44

Topolol Sort Alortm Stor vrts wt n-r 0 n quu Q. Wl Q s not mpty Dquu vrtx v, n v t t nxt numr Drs n-r o ll jnt vrts y 1 Enquu ll vrts wt n-r 0 Q: 0 0 2 1 10,,,,,,, 3 5 0 7 10/23/17 CMPS 2200 Intro. to Alortms 45 4, 0 21 6 10 9 20 1 1 1 0 8 3 1 20

Topolol Sort Runtm Runtm: O( V + E ) us vry s tou on, n vry vrtx s nquu n quu xtly on 10/23/17 CMPS 2200 Intro. to Alortms 46

DFS-Bs Topolol Sort Alortm Cll DFS on t rt yl rp G=(V,E) Fns tm or vry vrtx Rvrs t ns tms (st ns tm oms t lowst ns tm, ) Vl unton : V {1, 2,, V } su tt (u, v) E (u) < (v) Runtm: O( V + E ) 10/23/17 CMPS 2200 Intro. to Alortms 47

DFS-Bs Topolol Sort Run DFS: 1/12 2/11 3 /6 4 /5 13/18 14/17 15/16 7/10 8 /9 Rvrs ns tms: 4 5 8 9 1 2 3 6 7 10/23/17 CMPS 2200 Intro. to Alortms 48

DFS-Bs Top. Sort Corrtnss N to sow tt or ny (u, v) E ols (v) < (u). (sn w onsr rvrs ns tms) Consr xplorn (u, v) n DFS: v nnot vst n unns (n n n nstor n t pt rst tr), sn tn (u,v) woul k (w y t DAG lmm nnot ppn). I v s not n vst yt, t oms snnt o u, n n (v)<(u). (tr ) I v s n ns, (v) s n st, n u s stll n xplor, n (u)>(v) (orwr, ross ). 10/23/17 CMPS 2200 Intro. to Alortms 49

Topolol Sort Runtm Runtm: O( V + E ) us vry s tou on, n vry vrtx s nquu n quu xtly on DFS-s lortm: O( V + E ) 10/23/17 CMPS 2200 Intro. to Alortms 50