Round 7: Graphs (part I)

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

CS 241 Analysis of Algorithms

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

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

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

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

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

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

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

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

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

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

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

QUESTIONS BEGIN HERE!

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

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

QUESTIONS BEGIN HERE!

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

Exam 1 Solution. CS 542 Advanced Data Structures and Algorithms 2/14/2013

N=4 L=4. Our first non-linear data structure! A graph G consists of two sets G = {V, E} A set of V vertices, or nodes f

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

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

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

1 Introduction to Modulo 7 Arithmetic

Outline. Binary Tree

Planar Upward Drawings

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

Constructive Geometric Constraint Solving

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

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

Module graph.py. 1 Introduction. 2 Graph basics. 3 Module graph.py. 3.1 Objects. CS 231 Naomi Nishimura

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

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

b. How many ternary words of length 23 with eight 0 s, nine 1 s and six 2 s?

ECE COMBINATIONAL BUILDING BLOCKS - INVEST 13 DECODERS AND ENCODERS

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

Designing A Concrete Arch Bridge

CS September 2018

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

10/30/12. Today. CS/ENGRD 2110 Object- Oriented Programming and Data Structures Fall 2012 Doug James. DFS algorithm. Reachability Algorithms

Problem solving by search

Present state Next state Q + M N

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

Why the Junction Tree Algorithm? The Junction Tree Algorithm. Clique Potential Representation. Overview. Chris Williams 1.

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

Numbering Boundary Nodes

The University of Sydney MATH2969/2069. Graph Theory Tutorial 5 (Week 12) Solutions 2008

Section 10.4 Connectivity (up to paths and isomorphism, not including)

Seven-Segment Display Driver

CS 103 BFS Alorithm. Mark Redekopp

EE1000 Project 4 Digital Volt Meter

Lecture 20: Minimum Spanning Trees (CLRS 23)

Greedy Algorithms, Activity Selection, Minimum Spanning Trees Scribes: Logan Short (2015), Virginia Date: May 18, 2016

Algorithmic and NP-Completeness Aspects of a Total Lict Domination Number of a Graph

Multipoint Alternate Marking method for passive and hybrid performance monitoring

Similarity Search. The Binary Branch Distance. Nikolaus Augsten.

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

12. Traffic engineering

COMPLEXITY OF COUNTING PLANAR TILINGS BY TWO BARS

COMP108 Algorithmic Foundations

Weighted graphs -- reminder. Data Structures LECTURE 15. Shortest paths algorithms. Example: weighted graph. Two basic properties of shortest paths

Organization. Dominators. Control-flow graphs 8/30/2010. Dominators, control-dependence. Dominator relation of CFGs

Scientific Programming. Graphs

1. Determine whether or not the following binary relations are equivalence relations. Be sure to justify your answers.

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

Strongly connected components. Finding strongly-connected components

Nefertiti. Echoes of. Regal components evoke visions of the past MULTIPLE STITCHES. designed by Helena Tang-Lim

Trees as operads. Lecture A formalism of trees

CSE 373: AVL trees. Warmup: Warmup. Interlude: Exploring the balance invariant. AVL Trees: Invariants. AVL tree invariants review

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

Announcements. These are Graphs. This is not a Graph. Graph Definitions. Applications of Graphs. Graphs & Graph Algorithms

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

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

(Minimum) Spanning Trees

OpenMx Matrices and Operators

GREEDY TECHNIQUE. Greedy method vs. Dynamic programming method:

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

Garnir Polynomial and their Properties

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

Graph Contraction and Connectivity

S i m p l i f y i n g A l g e b r a SIMPLIFYING ALGEBRA.

Minimum Spanning Trees

Chapter 18. Minimum Spanning Trees Minimum Spanning Trees. a d. a d. a d. f c

More Foundations. Undirected Graphs. Degree. A Theorem. Graphs, Products, & Relations

Graph Theory. Vertices. Vertices are also known as nodes, points and (in social networks) as actors, agents or players.

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

Solutions to Homework 5

Analysis for Balloon Modeling Structure based on Graph Theory

Applications: The problem has several applications, for example, to compute periods of maximum net expenses for a design department.

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

DUET WITH DIAMONDS COLOR SHIFTING BRACELET By Leslie Rogalski

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

Decimals DECIMALS.

A Simple Code Generator. Code generation Algorithm. Register and Address Descriptors. Example 3/31/2008. Code Generation

SAMPLE PAGES. Primary. Primary Maths Basics Series THE SUBTRACTION BOOK. A progression of subtraction skills. written by Jillian Cockings

XML and Databases. Outline. Recall: Top-Down Evaluation of Simple Paths. Recall: Top-Down Evaluation of Simple Paths. Sebastian Maneth NICTA and UNSW

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

Transcription:

Roun 7: Grphs (prt I) Tommi Junttil Alto Univrsity Shool o Sin Dprtmnt o Computr Sin CS-A40 Dt Struturs n Alorithms Autumn 207 Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 / 55

Topis: Grphs Rprsntin rphs Brth-irst srh Dpth-irst srh Topoloil sortin Stronly onnt omponnts Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 2 / 55

Mtril in Introution to Alorithms, 3r. (onlin vi Alto li): Stions 22. 22.4 Extrnl links: MIT 6.006 OCW vio on rth-irst srh MIT 6.006 OCW vio on pth-irst srh n topoloil sort Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 3 / 55

Dirnt kins o rphs n ntworks r vrywhr: th prossor o omputr is ntwork o loi ts intrnt soil ntworks mps pnny rphs (ours prrquisits t) t n t Grphs s mthmtil struturs r n importnt strtion or rsonin out suh systms Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 4 / 55

A prt o th intrnt in 2005: (iur: Mtt Britt, Crtiv Commons-lins, sour) Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 5 / 55

Europn E ros: (iur: puli omin, sour) Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 6 / 55

Mthmtil prliminris Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 7 / 55

Unirt Grphs Rp rom th roun on trs An unirt rph is pir (V,E), whr V is (usully init) st o vrtis (lso ll nos), n E is st o s twn th vrtis, i.. st o pirs o orm {u,v} suh tht u,v V n u v Exmpl Consir th rph (V,E) with th vrtis V = {,,,,,,,h}, n th s E = {{,},{,},{,},{,}, {,},{,},{,},{,},{,}}. It is illustrt rphilly on th riht with vrtis rwn s irls n s s lins twn th vrtis Sl-loops, i.. s twn vrtx n itsl, r orin in unirt rphs hr Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 8 / 55

A pth o lnth k rom vrtx v 0 to vrtx v k is squn (v 0,v,...,v k ) o k + vrtis suh tht {v i,v i+ } E or h i = 0,,...k A vrtx v is rhl rom vrtx u i thr is pth rom u to v. Not tht h vrtx is rhl rom itsl s thr is pth o lnth 0 rom th vrtx to itsl A pth is simpl i ll th vrtis in it r istint Th rph is onnt i vry vrtx in it is rhl rom ll th othr vrtis Exmpl Th rph on th riht hs (non-simpl) pth (,,,,,) rom to hs simpl pth (,,) o lnth 2 rom to is onnt ut ltin th {,} woul mk it isonnt Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 9 / 55

A yl is pth (v 0,v,...,v k ) with k 3 n v 0 = v k Exmpl A yl (v 0,v,...,v k ) is simpl i v,v 2,...,v k r istint A rph is yli i it os not hv ny simpl yls Th rph on th lt is not yli s it ontins, mon mny othrs, simpl yl (,,,) Th rph on th riht is yli Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 0 / 55

Dirt Grphs In irt rphs th s hv irtion A irt rph (or irph), is pir (V,E), whr V is (usully init) st o vrtis, n E V V is st o s twn th vrtis: pir (u,v) E nots n rom th vrtx u to th vrtx v. Exmpl Consir th rph (V,E) with th vrtis V = {,,,,,,,h}, n th s E = {(,),(,),(,), (,),(,),(,),(,),(,),(,), (,),(,)}. It is illustrt rphilly on th riht with vrtis rwn s irls n s s lins with irtion rrows Noti th loop rom th vrtx to itsl n th two s twn th vrtis n Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 / 55

Th out-r o vrtx is th numr o s rom it whil th in-r is th numr o s to it A pth o lnht k rom vrtx u to th vrtx u is squn (v 0,v,...,v k ) o k + vrtis suh tht v 0 = u, v k = u n (v i,v i+ ) E or ll i = 0,,...,k A pth is simpl i ll vrtis in it istint A pth (v 0,v,...,v k ) is yl i k n v 0 = v k Exmpl In th rph on th riht, th in-r o is 2 n th out-r is som simpl pths r (), (,,), n (,,) th yls r (,), (,,,), (,,,), (,,) Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 2 / 55

Dirt Ayli Grphs (DAGs) A irt yli rph (DAG) is irt rph tht os not ontin ny yls Suh rphs ris in mny pplitions, s.. this wikipi p Exmpl DAGs ris nturlly in mny tim- n orr-rlt rs whr somthin hs to on or somthin ls n on wth pnts trousrs lt jkt ti unrshirt shirt soks shos In suh sitution yl woul mn tht som thins nnot on t ll As toy xmpl, pnny rph twn lothins or rssin up is shown on th riht ht Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 3 / 55

Rprsnttions or rphs Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 4 / 55

In orr to rson out rphs with lorithms, on hs to rprsnt thm in mmory An unirt rph n hv t most n(n ) n irph t most n 2 2 s A rph is sprs i its numr o s is smll ompr to this mximum numr o possil s, othrwis it is ns Sprsity is not n xtly in onpt Th two ommon rprsnttion mthos r: jny mtris, whih r minly us or ns rphs, n jny lists, th most ommon rprsnttion In oth o ths rprsnttions, th vrtis r usully ssum to th irst intrs {0,,...,n } so tht inxin is sy I mor omplx vrtx nms suh s strins r to us, on n mintin on mp rom ths to smll intrs so tht intrs n us insi th rph lorithm Th xmpls low us symoli nms suh s or ttr humn rility Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 5 / 55

Ajny mtris Th jny mtrix o irt rph G = ({0,,...,n },E) is n n inry mtrix A = ( u,v ) suh tht th ntry u,v is i (u,v) E n 0 othrwis Th mtrix is sy to stor in row-y-row it-vtor o lnth n 2 (n rry with n2 32 32-it intrs); th ntry u,v n oun t th inx un + v in th it-vtor Exmpl: A irt rph n its jny mtrix rprsnttion 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 As it-vtor th mtrix is 00000000000000000000000000000000000000 Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 6 / 55

Th jny mtrix o n unirt rph G = ({0,,...,n },E) is simply th uppr riht hl o th mtrix With 0 u < v < n, th ntry u,v is i {u,v} E n 0 othrwis I th mtrix is prsnt s row-y-row it-vtor, thn th ntry u,v n oun in th it un u(u+) 2 + (v u ) Exmpl: An unirt rph n its jny mtrix rprsnttion 0 0 0 0 0 0 0 0 0 0 0 0 As it vtor th mtrix is 000000000000 n th it or th {,} n oun t th inx 2 7 2(2+) + (6 2 ) = 4 3 + 3 = 4 unr th ) 2 mppin 0 2 3 4 5 6 Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 7 / 55

Som nlysis: Goo or ns (n smllish) rps Ain or rmovin n tks onstnt tim Ain nw vrtx is xpnsiv Grphs with mny vrtis tk lot o mmory: th jny mtrix or irt rph with million vrtis woul tk ovr 00 iyts o mmory Trvrsin th nihours o vrtx not optiml i th r o th vrtx is low ompr to th numr n o ll vrtis (hv to o throuh ll th O(n) yts mkin th row) Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 8 / 55

Ajny lists Eh vrtx is ssoit to list o its nihours Nturlly, n rry or ynmi rry n us inst o th list s wll Exmpl: A irt rph n its jny list rprsnttion Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 9 / 55

Exmpl: A irt rph n its jny list rprsnttion in mor ompt orm Th vrtis r now {0,,...,6} Th nihours o vrtx u n oun in th surry s[strt[u],strt[u+]-] or s[strt[u],s.lnth-] or th lst vrtx u = 6 4 2 6 strt 0 0 2 2 4 3 7 4 8 5 9 6 0 0 3 s 0 4 2 3 5 4 0 5 3 6 6 7 6 8 5 9 0 0 3 5 In th rprsnttion mtho ov, th two rrys ontin V + E intrs Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 20 / 55

Anlysis: Compt or sprs rphs: mmory us is O( V + E ) inst o O( V 2 ) Ain nw vrtx is n mortiz onstnt-tim oprtion whn ynmi rrys r us Ain n rmovin s linr in th r o th vrtis involv in th worst s whn th list/rry pr vrtx rprsnttion o th irst xmpl is us Itrtin th nihours o vrtx is sy Mor ommonly us s rphs in mny pplitions tn to oth lr n sprs Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 2 / 55

Rprsntin vrtx ttriuts In mny rph lorithms, w n to ssoit som t ttriuts to vrtis Thr r multipl wys to hiv this ut s/whn th vrtis r {0,,...,n }, th sist wy is to hv n rry o n lmnts holin th ttriut(s) From now on, w ssum tht ssin n sttin vrtx ttriut n on in onstnt tim Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 22 / 55

Brth-irst srh Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 23 / 55

An iint trvrsl lorithm tht ins, or th ivn sour vrtx s, ll th vrtis rhl rom s, n shortst simpl pth rom s to h rhl vrtx Th i is to strt rom th sour vrtx s n trvrs its outoin s. Th nihour vrtis tht hv not yt n visit r put in irst-in-irst-out quu. Eh vrtx is tkn out o th quu n its nihours r thn visit in th sm mnnr n put t th n o th quu i not yt visit. Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 24 / 55

At h point, vrtx is olor whit i it hs not yt n oun rhl rom s ry i it hs n oun rhl rom s ut not ll its outoin s hv n trvrs lk i it hs n oun rhl rom s n ll its outoin s hv n trvrs In ition, h vrtx t is ssoit with istn ttriut tht tlls th lnth o th shortst pth rom s to t, n prssor ttriut tht stors th nihour rom whih shortst pth th sour to it ws prou Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 25 / 55

In psuoo Brth-irst-srh(G, s): or h vrtx v V \ {s}: // Initiliz olors, istns n prssors v.olor WHITE v.ist v.pr NIL Q /0 // Q is FIFO-quu s.olor GRAY // Initiliz th sour vrtx s to ry s.ist 0 s.pr NIL nquu(q, s) whil Q is not mpty: u quu(q) // Tk th rlist ry vrtx rom th quu or h v with {u,v} E: // Trvrs h rom it i v.olor = WHITE: // Th nihour v hs not yt n visit? v.olor GRAY // Color it ry v.ist u.ist + // Stor th istn v.pr u // An th pth rom th sour nquu(q, v) // Put it t th n o th quu u.olor := BLACK // All s rom u trvrs Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 26 / 55

Exmpl Brth-irst srh on rph with two onnt omponnts. Th sour vrtx is Eh nnott rph low shows th olors, istns, n BFS-tr s in th innin o h whil loop n t th n o th lorithm (th lst rph). Th nihours r in th quu in n ritrry orr. 2 2 0 0 0 0 2 2 2 3 3 0 0 0 Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 27 / 55

For irt rphs on just rpls th lin or h v with {u,v} E with or h v with (u,v) E Exmpl Brth-irst srh on irt rph Th nihours r in th quu in n ritrry orr. 2 0 0 0 0 2 2 2 3 3 0 0 0 Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 28 / 55

At th n o th lorithm, vrtx is lk i it is rhl rom th sour vrtx is whit i it is not rhl rom th sour Furthrmor, or h lk vrtx th istn ttriut ivs th lnth o th shortst pth(s) rom th sour to th vrtx ollowin th prssor s prous shortst pth to th sour vrtx Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 29 / 55

Run-tim nlysis in th worst s: Eh vrtx is in th quu on Eh is onsir twi or unirt rphs; on or h n-point whn it turns rom ry to lk on or irt rphs whn th sour n-point turns rom ry to lk Enquu n quu r onstnt tim oprtions th lorithm runs in tim O( V + E ) On unirt rphs, inin th onnt omponnts is sy with BFS Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 30 / 55

Dpth-irst srh Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 3 / 55

Anothr systmi mtho or trvrsin rphs Fins ll th vrtis tht r rhl rom vrtx Dos not in th shortst pths or smllst istn rom sour vrtx But rvls som strutur o th rph tht n us in othr lorithm Th i (si vrsion irst): Strt visitin vrtis rom th sour vrtx Rursivly visit th whit nihours o th urrnt vrtx Whn strtin th visit, olor th vrtx ry Whn ll nihours hv n visit, olor th vrtx lk Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 32 / 55

In psuo-o Dpth-irst-srh(G, s): or h vrtx v V : v.olor WHITE v.pr NIL visit(u): u.olor GRAY or h v with {u,v} E: i v.olor = WHITE: v.pr u visit(v) u.olor BLACK visit(s) Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 33 / 55

Exmpl Dpth-irst srh on n unirt rph Th iurs low show th sitution t th innin o h ll to visit (th visit no is shown in lu) n t th n o th lorithm (th lst iur) Th nihours r in th quu in n ritrry orr Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 34 / 55

Ain, or irt rphs on just rpls th lin or h v with {u,v} E with or h v with (u,v) E Exmpl Dpth-irst srh on irt rph Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 35 / 55

Worst s run-tim nlysis: Eh vrtx is visit on (it must whit to visit n immitly turns ry whn visit) Whil vrtx is visit, h rom it is trvrs th lorithm runs in tim O( V + E ) Th (rvrs) prssor links iv pth, not nssrily shortst on, rom th sour vrtx to h vrtx rhl rom th sour Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 36 / 55

An xtn vrsion Lt us xtn th si vrsion y tthin to h vrtx two visit timstmp ttriuts: th strt timstmp tlls th tim on whih visitin th vrtx strt (th vrtx ws olor to ry), n th inish timstmp tlls th tim whn visitin th vrtx ws omplt (th vrtx ws olor to lk) Th timstmps r snin intrs strtin rom In ition to th timstmps, th lorithm is xtn so tht thr is no sinl sour vrtx or th srh ut nw sour vrtis r tkn (ritrrily or with som strty) or susrhs until ll th vrtis hv n visit Th lorithm now os not in th vrtis rhl rom sinl sour vrtx ut rvls som strutur o th rph Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 37 / 55

In psuo-o Dpth-irst-srh(G = (V, E)): or h vrtx v V : v.olor WHITE v.pr NIL tim 0 visit(u): // Innr rursiv untion or trvrsin vrtis tim tim + u.strt = tim u.olor GRAY or h v with {u,v} E: // (u,v) E or irt rphs i v.olor = WHITE: v.pr u visit(v) u.olor BLACK tim tim + u.inish = tim or h vrtx u V : i u.olor = WHITE: visit(u) Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 38 / 55

Exmpl Consir th rph on th riht Th susrhs (lls to th visit-untion in th lst lin o th min prour) strt rom th vrtis in lphtil orr hr Th s rom th vrtis r trvrs in n ritrry orr Th s rom th prssor vrtis r hihliht in lu A irt rph: 3/6 2/9 /0 /6 y z s t x w v u 4/5 7/8 2/3 4/5 Th sm rwn irntly: /0 /6 s t Th pir / nxt to vrtx tlls th strt n inish timstmp o th vrtx 2/9 z v 2/3 u 4/5 3/6 y w 7/8 4/5 x Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 39 / 55

Th s omin rom th prssor vrtis orm pth-irst orst Formlly, it is th su-rph G pr = (V,E pr ), whr E pr = {(u.pr,u) u V n u.pr NIL} Th pth-irst orst n onsist o svrl pth-irst trs root t th vrtx irst visit in h susrh Th pth-irst orst is not nssrily uniqu or th rph ut pns on trvrsl orr o th pth-irst srh Consir th tim intrvl [u.strt, u.inish] orm y th timstmps ssoit to h vrtx For th intrvls o two isjoint vrtis u n v on o th ollowin hols: [u.strt,u.inish] n [v.strt,v.inish] o not intrst h othr [u.strt,u.inish] is ully ontin in [v.strt,v.inish] [v.strt, v.inish] is ully ontin in [u.strt, u.inish] Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 40 / 55

Exmpl Consir th rph on th riht Th s in th pth-irst orst r hihliht in lu A irt rph: 3/6 2/9 y z /0 /6 s t Th vrtis z, y, x n w n rh rom s in th pth-irst orst n thir tim intrvls r ully ontin in th intrvl o s Th intrvls o th vrtis t, v n u lonin to irnt pth-irst tr o not ovrlp with th intrvl o s x w v 4/5 7/8 2/3 Th sm rwn in irnt wy: /0 /6 s t 2/9 z v 2/3 u 4/5 u 4/5 3/6 y w 7/8 4/5 x Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 4 / 55

Assum tht pth-irst srh hs n prorm in irt rph Th s o th rph n lssii orin to th srh s ollows: Tr s r th s lonin to th pth-irst orst Bk s r th s tht l rom vrtx to som nstor vrtx in th sm pth-irst tr (this inlus loop-s) Forwr s r th s tht l rom vrtx to som propr snnt vrtx in th sm pth-irst tr Cross s r th rst o th s For unirt rphs k n orwr s r usully onsir to th sm thin, n thr r no ross s Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 42 / 55

Exmpl Consir th irt rph n pth-irst srh shown on th riht A irt rph: 3/6 2/9 y z /0 /6 s t Th tr s r hihliht in lu Th k s r: (x,z) n (u,t) Th orwr s r: (s,w) x w v 4/5 7/8 2/3 Th sm rwn in irnt wy: /0 /6 s t u 4/5 Th ross s r: (u,v), (v,s), (v,w) n (w,x) 2/9 z v 2/3 u 4/5 3/6 y w 7/8 4/5 x Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 43 / 55

Som o th typs r sy to tt urin th pth-irst srh I th lorithm is in th untion invotion visit(u) n onsirs th (u,v) E, thn tht is tr i v is whit k i v is ry, n ithr orwr or ross i v is lk A irt rph is yli i n only i (ny) pth-irst orst os not ontin k s th yliity o irt rph n tt in tim O( V + E ) Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 44 / 55

Topoloil sort Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 45 / 55

A topoloil sort, or topoloil orrin, or irt yli rph (V,E) is linr orrin o its vrtis V suh tht or h (u,v) E it hols tht u pprs or v in th orrin Nturlly, irt rphs with yls o not hv suh linr orrins n nnot topoloilly sort Exmpl Rll th DAG rom prvious xmpl shown on th riht pnts trousrs ti unrshirt shirt ht Two topoloil sorts or th DAG r: wth lt soks jkt shos pnts,unrshirt,trousrs,shirt,lt,ti,soks,shos,wth,jkt,ht 2 wth,pnts,soks,trousrs,shos,lt,unrshirt,ht,shirt,ti,jkt Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 46 / 55

A topoloil sort n otin sily with pth-irst srh: Prorm pth-irst srh on th rph ut whn vrtx is inish (olor lk), it in th ront o link list t th n, rturn th list Th ky i or th orrtnss o this lorithm is: in DAGs, whn vrtx is olor lk, ll th vrtis rhl rom it r lk n thus ltr in th prou topoloil orrin In DAGs suh rhl vrtis nnot ry us tht woul mn tht th rph hs yl Topoloil sort n on in tim O( V + E ) s th lorithm ov is simpl vrint o DFS Othr lorithms lso xists, s.. this wikipi p Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 47 / 55

Exmpl Rll th DAG rom prvious xmpl shown on th riht Th timstmps prou y n ritrry DFS on it r lso shown [9,20] pnts unrshirt [,0] [,6] trousrs [5,6] ti shirt [2,7] ht [8,9] wth [2,3] lt soks [7,8] [2,22] jkt [3,4] shos [4,5] Th topoloil sort prou y th DFS is wth,pnts,soks,trousrs,shos,lt,unrshirt,ht,shirt,ti,jkt Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 48 / 55

Stronly Connt Componnts Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 49 / 55

Finin stronly onnt omponnts o irt rphs is n nothr lorithm s on pth-irst srh For irt rph G = (V,E), stronly onnt omponnt (SCC) is mximl sust C V suh tht or ll vrtis u,v C it hols tht v is rhl rom u n u is rhl rom v Exmpl Thus h yl o th irt rph is insi n SCC Vrtis tht r not in ny yl orm thir own unit omponnts Consir th rph on th riht y z s t Its stronly onnt omponnts r {s}, {t,u}, {v}, n {w,x,y,z} x w v u Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 50 / 55

Th omponnt rph G SCC = (V SCC,E SCC ) o irt rph G is th irt rph whr th vrtis V SCC r th onnt omponnts o G, n thr is n (C,C ) V SCC rom omponnt vrtx C to irnt omponnt vrtx C i (u,v) is n in G or som u C n v C Th omponnt rph is yli. Why? Exmpl Consir in th rph in th prvious xmpl Its omponnt rph is shown on th riht {w, x, y, z} {s} {v} {t, u} Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 5 / 55

Computin SCCs Thr r svrl linr, i. O( V + E ), tim lorithms or omputin th SCCs o rph G = (V,E), s.. wikipi rtil Th ollowin prsnts vrint o Kosrju s lorithm It uss pth-irst srh twi: Prorm pth-irst srh on th oriinl rph to otin th visit timstmps 2 Mk th trnspos G T o th rph G = (V,E) y rvrsin th irtions o th s: G T = (V,E T ), whr E T = {(u,v) (v,u) E} 3 Prorm pth-irst srh on th trnspos rph G T ut strt th susrhs rom th vrtis in th rvrs orr o th inish timstmps prou y th irst pth-irst srh Th SCCs r th nos in th pth-irst srh orsts prou y th son pth-irst srh Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 52 / 55

Consir th rph on th riht Its stronly onnt omponnts r {s}, {t,u}, {v}, n {w,x,y,z} 3/6 2/9 y z /0 /6 s t Also shown r th timstmps n tr s otin y prormin pth-irst srh on th rph x 4/5 w 7/8 v 2/3 u 4/5 Th trnspos o th rph is shown on th riht y z s t Noti tht th SCCs r th sm An th omponnt rph is in ut trnspos x w v u Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 53 / 55

Th rph(s) on th riht show th rsult o pplyin pth-irst srh on th trnspos rph in th rvrs orr o th inish timstmps o th irst DFS Th susrh strtin rom t ins th l omponnt {t, u} Th susrh rom v isovrs th unit omponnt {v} Th susrh rom s isovrs th unit omponnt {s} Th susrh rom z isovrs th root omponnt {w,x,y,z} DFS on th trnspos rph /2 9/6 7/8 /4 y z s t x w v u 3/4 0/5 5/6 2/3 Th sm rwn in irnt orr: /4 5/6 7/8 9/6 t v s z u x 2/3 3/4 y /2 0/5 w Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 54 / 55

Why os this lorithm work? Rouh skth (s th ook or th proos): Th irst pth-irst srh ivs, in th inish timstmps, rvrs topoloil sortin or th SCCs in th omponnt rph Th omponnt rph o th trnspos rph is th trnspos o th omponnt rph o th oriinl rph Prormin th son DFS on th trnspos rph in th rvrs topoloil orr ivn y th irst DFS irst ins th omponnts t hiht 0 in th trnspos omponnt rph thn ins th omponnts t hiht in th trnspos omponnt rph (it os not o to th omponnts t hiht 0 s thy r lry visit) n so on Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 55 / 55