Analysis of Algorithms - Elementary graphs algorithms -

Similar documents
Analysis of Algorithms - Elementary graphs algorithms -

Strongly Connected Components

Examples and applications on SSSP and MST

Searching Linked Lists. Perfect Skip List. Building a Skip List. Skip List Analysis (1) Assume the list is sorted, but is stored in a linked list.

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

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

The second condition says that a node α of the tree has exactly n children if the arity of its label is n.

10. EXTENDING TRACTABILITY

Week 3: Connected Subgraphs

Chapter Finding Small Vertex Covers. Extending the Limits of Tractability. Coping With NP-Completeness. Vertex Cover

Addition of angular momentum

First order differential equation Linear equation; Method of integrating factors

Y 0. Standing Wave Interference between the incident & reflected waves Standing wave. A string with one end fixed on a wall

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

CS 491 G Combinatorial Optimization

Abstract Interpretation: concrete and abstract semantics

Addition of angular momentum

Roadmap. XML Indexing. DataGuide example. DataGuides. Strong DataGuides. Multiple DataGuides for same data. CPS Topics in Database Systems

Graphs and Graph Searches

Homework #3. 1 x. dx. It therefore follows that a sum of the

Association (Part II)

CPSC 665 : An Algorithmist s Toolkit Lecture 4 : 21 Jan Linear Programming

Multiple Short Term Infusion Homework # 5 PHA 5127

CS 361 Meeting 12 10/3/18

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

SPH4U Electric Charges and Electric Fields Mr. LoRusso

That is, we start with a general matrix: And end with a simpler matrix:

PROBLEM SET Problem 1.

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

Minimum Spanning Trees

EECE 301 Signals & Systems Prof. Mark Fowler

The Equitable Dominating Graph

u x v x dx u x v x v x u x dx d u x v x u x v x dx u x v x dx Integration by Parts Formula

Mathematics 1110H Calculus I: Limits, derivatives, and Integrals Trent University, Summer 2018 Solutions to the Actual Final Examination

a 1and x is any real number.

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

CS553 Lecture Register Allocation I 3

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

The pn junction: 2 Current vs Voltage (IV) characteristics

Problem Set 6 Solutions

Additional Math (4047) Paper 2 (100 marks) y x. 2 d. d d

1 Minimum Cut Problem

Slide 1. Slide 2. Slide 3 DIGITAL SIGNAL PROCESSING CLASSIFICATION OF SIGNALS

Thickness and Colorability of Geometric Graphs

Kernels. ffl A kernel K is a function of two objects, for example, two sentence/tree pairs (x1; y1) and (x2; y2)

EEO 401 Digital Signal Processing Prof. Mark Fowler

cycle that does not cross any edges (including its own), then it has at least

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

CS 241 Analysis of Algorithms

Minimum Spanning Trees

From Elimination to Belief Propagation

Propositional Logic. Combinatorial Problem Solving (CPS) Albert Oliveras Enric Rodríguez-Carbonell. May 17, 2018

Final Exam Solutions

On spanning trees and cycles of multicolored point sets with few intersections

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

A RELATIVISTIC LAGRANGIAN FOR MULTIPLE CHARGED POINT-MASSES

64. A Conic Section from Five Elements.

CS 6353 Compiler Construction, Homework #1. 1. Write regular expressions for the following informally described languages:

EXST Regression Techniques Page 1

Chapter 6 Folding. Folding

Self-Adjointness and Its Relationship to Quantum Mechanics. Ronald I. Frank 2016

Basic Polyhedral theory

Alpha and beta decay equation practice

Thomas Whitham Sixth Form

Computing and Communications -- Network Coding

Superposition. Thinning

CPS 616 W2017 MIDTERM SOLUTIONS 1

The van der Waals interaction 1 D. E. Soper 2 University of Oregon 20 April 2012

PHYS ,Fall 05, Term Exam #1, Oct., 12, 2005

EEO 401 Digital Signal Processing Prof. Mark Fowler

ME311 Machine Design

GEOMETRICAL PHENOMENA IN THE PHYSICS OF SUBATOMIC PARTICLES. Eduard N. Klenov* Rostov-on-Don, Russia

On the irreducibility of some polynomials in two variables

Chapter 8: Electron Configurations and Periodicity

Pipe flow friction, small vs. big pipes

(Upside-Down o Direct Rotation) β - Numbers

5. B To determine all the holes and asymptotes of the equation: y = bdc dced f gbd

The Transfer Function. The Transfer Function. The Transfer Function. The Transfer Function. The Transfer Function. The Transfer Function

ECE602 Exam 1 April 5, You must show ALL of your work for full credit.

Function Spaces. a x 3. (Letting x = 1 =)) a(0) + b + c (1) = 0. Row reducing the matrix. b 1. e 4 3. e 9. >: (x = 1 =)) a(0) + b + c (1) = 0

3 Finite Element Parametric Geometry

MCB137: Physical Biology of the Cell Spring 2017 Homework 6: Ligand binding and the MWC model of allostery (Due 3/23/17)

ON RIGHT(LEFT) DUO PO-SEMIGROUPS. S. K. Lee and K. Y. Park

Sundials and Linear Algebra

SIGNIFICANCE OF SMITH CHART IN ANTENNA TECHNOLOGY

Derangements and Applications

First derivative analysis

DIFFERENTIAL EQUATION

COHORT MBA. Exponential function. MATH review (part2) by Lucian Mitroiu. The LOG and EXP functions. Properties: e e. lim.

Note If the candidate believes that e x = 0 solves to x = 0 or gives an extra solution of x = 0, then withhold the final accuracy mark.

Random Access Techniques: ALOHA (cont.)

MA1506 Tutorial 2 Solutions. Question 1. (1a) 1 ) y x. e x. 1 exp (in general, Integrating factor is. ye dx. So ) (1b) e e. e c.

Indexed Search Tree (Trie)

Data Assimilation 1. Alan O Neill National Centre for Earth Observation UK

Thomas Whitham Sixth Form

Construction of asymmetric orthogonal arrays of strength three via a replacement method

Supplementary Materials

Applied Statistics II - Categorical Data Analysis Data analysis using Genstat - Exercise 2 Logistic regression

The graph of y = x (or y = ) consists of two branches, As x 0, y + ; as x 0, y +. x = 0 is the

Transcription:

Analysis of Algorithms - Elmntary graphs algorithms - Anras Ermahl MRTC (Mälaralns Ral-Tim Rsarch Cntr) anras.rmahl@mh.s Autumn 004 Graphs Graphs ar important mathmatical ntitis in computr scinc an nginring Oftn us to rprsnt iffrnt kins of rlational information Dpnncis btwn ntitis Distanc btwn ntitis Graphs In many applications it is intrsting to sarch or travrs th graph Th last numbr of subway stations to gt from Hässlby to Norsborg? How o I visit all subway stations in an fficint mannr? This an following lcturs will al with algorithms for this, an som applications Rprsnting graphs Thr ar many ways of rprsnting graphs A graph G = (V, E) consists of vrtics (nos) V an gs E 1 5 4 V = {1,,,4,5} E = {(1,),(1,5),(,),(,5),(,4),(4,5)} Th rprsntations iffr in fficincy pning on th algorithm, whthr th graph is ns or spars, an othr factors W will consir two rprsntations: Ajacncy-list rprsntation Ajacncy-matrix rprsntation Rprsnting graphs Rprsnting graphs Unirct graph 1 5 4 Dirct graph 1 4 5 6 1 5 1 5 4 1 4 4 5 5 4 1 Ajacncy-list rprsntation 1 4 5 1 0 1 0 0 1 1 0 1 1 1 0 1 0 1 0 4 0 1 1 0 1 5 1 1 0 1 0 Ajacncy-matrix rprsntation 1 4 5 6 5 4 5 4 6 6 Ajacncy-list rprsntation 1 4 5 6 1 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 1 4 0 1 0 0 0 0 5 0 0 0 1 0 0 6 0 0 0 0 0 1 Ajacncy-matrix rprsntation

Ajacncy-list Ajacncy-list G = (V,E) rprsnt by an array Aj of V lists, on pr vrtx Aj[u] contains pointrs to (ID s of) all vrtics ajacnt to u Not ajacnt is iffrnt whn th graph is irct or unirct Dirct: sum of lngth of all ajacncy lists is E Unirct: sum of lngth of all ajacncy lists is E Mmory rquir: Θ(V + E) 1 4 Oftn fficint rprsntation if G is spars 5 6 5 E not too big compar with V Easily aapt to wight graphs Stor wight w on g in ajacncy list 4 5 6 4 6 Ajacncy-list rprsntation Ajacncy-matrix Ajacncy-matrix rprsntation: G = (V,E) rprsnt by a boolan matrix A, whr: Siz O( V ) (inpnnt of E ), fficint whn E = Θ( V ) (an graph siz is small) 1 4 5 Ajacncy matrix symmtric whn graph is unirct, thn 1 0 1 0 0 1 suffics to stor on half 1 0 1 1 1 (still siz O( V ), though) 0 1 0 1 0 Unwight graph can b stor using on bit pr ntry 4 0 1 1 0 1 5 1 1 0 1 0 Easily aapt to wight graphs Ajacncy-matrix Stor wight w for g (u,v) rprsntation at ntry in row u an column v Travrsing graphs Most basic algorithms on graphs will b applications of graph travrsal Printing or valiating ach g/vrtx Copying a graph or convrting btwn rprsntations Counting th numbr of gs/vrtics Intifying connct componnts Fining paths btwn two vrtics, or cycls W will invstigat som classical graph travrsal algorithms in tail Travrsing graphs Important: Efficincy an corrctnss Efficincy: Don t loop or visit vrtics rpatly Corrctnss: Don t miss any vrtx W mark vrtics as w travrs th graph 1 1. Uniscovr (whit), th initial stat, bfor w v sn it. Discovr (gry), w v sn th vrtx but not all of its incint gs. Finish (black), all incint gs hav bn visit. Orr of xploration Th orr in which w xplor vrtics pns on th containr us for storing iscovr but not finish vrtics Two typ of containrs frquntly us: Quu: las to so call brath-first sarch Stack: las to so call pth-first sarch W will invstigat ths two graph travrsal algorithms in mor tail Brath-first sarch Simpl algorithm for sarching a graph Input: A irct/unirct graph with sourc s Output: Th shortst istanc from s [u] = shortst istanc of no u to s p[u] = prcssor of no u in shortst path to s Rprsnt by a brath-first tr Only visits vrtics rachabl from s Running tim: Θ(V+E) Linar tim with rspct to ajacncy list

Brath-first sarch Givn graph G an sourc vrtx s, fin all vrtics rachabl from s by a brath-first sarch from s (whr s is sn as th root in a tr spanning G) Brath-first mans all vrtics at pth k from s ar visit bfor thos at pth k + 1 ar Th algorithm yils th following rsults: Th istanc (lngth of shortst path) from s to any othr rachabl vrtx u ([u]) A s-root brath-first tr that consists of th shortst paths from s to all othr vrtics Works on both irct an unirct graphs Th BFS algorithm s is put in quu Q Each vrtx is st to hav color WHITE, istanc an no prcssor Starting no s is st to hav color GREY, istanc 0 an no prcssor As long as Q not mpty w quu on no u Procss no u is st to BLACK Invstigat all non-visit ajacnt nos to u W st ach such no as GREY, u as its prcssor, an its istanc to 1+u s istanc Exampl: Brath-first sarch Aftr start no s has bn a to Q 0 Q s 0 Exampl: Brath-first sarch Aftr s has bn procss an its nighbours r an w hav bn a 1 0 Q w r 1 1 1 Exampl: Brath-first sarch Aftr no w has bn procss an its nighbours t an x hav bn a 1 0 Q r t x 1 1 Exampl: Brath-first sarch Aftr no r has bn procss an its nighbour v has bn a 1 0 Q t x 1 v

Exampl: Brath-first sarch Aftr no t has bn procss an its nighbour u has bn a 1 0 Q x v u 1 Exampl: Brath-first sarch Aftr no x has bn procss an its nighbour y has bn a 1 0 Q v u y 1 Exampl: Brath-first sarch Aftr no v has bn procss r s t u 1 0 Q u y 1 v w x y Exampl: Brath-first sarch Aftr no u has bn procss r s t u 1 0 Q y 1 v w x y Exampl: Brath-first sarch Aftr no y has bn procss r s t u 1 0 Q «1 v w x y Analysis of BFS Taks O( 1 ) tim What about th outr whil-loop? First loop taks O( V ) tim What about th innr for-loop?

Analysis of BFS Outr loop (whil): Can w boun th tim until Q bcoms mpty? Only whit vrtics ar nquu, an thy ar always gray whn nquu ï A vrtx can b nquu at most onc On vrtx quu for vry lap ï Outr whil loop xcut O( V ) tims Innr loop (for): Analysis of BFS Th ajacncy list of a vrtx is scann at most onc, an ach lmnt in th list is accss only onc Thus, th total tim in innr loop is proportional to sum of lngths of all ajacncy lists fl O( E ) Whol loop nst fl O( V ) + O( E ) = O( V + E ) Analysis of BFS Analysis of BFS Stp 5..9 runs in O(1) tim Whol loop nst (stp 10..18) runs in O( V + E ) tim First loop (stp 1..4) runs in O( V ) tim Total running tim for BFS is: O( V ) + O(1) + O( V + E ) = O( V + E ) tim Dpth-first sarch Simpl algorithm which sarchs pr in th graph whnvr possibl Input: A irct/unirct graph. Output: Dp-first forst (compos of pth-first trs) Each vrtx u is tim-stamp: iscovr [u] an finish f[u]. Egs { tr, back, forwar or cross g }. Visits all vrtics Running tim: Θ(V+E) Linar tim with rspct to ajacncy list. Dpth-first sarch Sarchs graph by rcursivly xploring th vrtics in th ajacncy list All vrtics rachabl from a vrtx in th ajacncy list ar rcursivly sarch bfor nxt vrtx in th list is xplor DFS constructs a pth- first forst that contains all vrtics in th graph BFS only buils a tr of vrtics rachabl from som givn root vrtx Th DFS algorithm [u] is timstamp whn vrtx u first iscovr f[u] is timstamp whn sarch of u s ajacncy list is complt No u gts tim whn first iscovr No u gts tim whn visiting finish All vrtics ar visit onc All vrtics rachabl an from u an not yt visit gts visit by rcursiv call

Dpth-first sarch: vrtx classification Tim-stamp vrtics whn thy ar iscovr/finish: [u]: whn iscovr Th vrtx colors ar quivalnt to th following cass: Whit: th initial stat, bfor w v sn th vrtx Gray: w v sn th vrtx but not all of its incint gs f[u]: whn finish Black: all incint gs hav bn visit Dpth-first sarch: g classification Egs (u,v) ar classifi accoring to th following four cass: Tr-g (T) Eg to whit vrtx Back-g (B) Eg to gry vrtx Forwar-g (F) Eg to black vrtx [u] < [v] Cross-g (C): Eg to black vrtx [u] > [v] Forwar-gs (F) ar non-tr gs (u,v) conncting a vrtx u to a scnant v in a pth-first tr F T T B T C Tr-gs (T) ar gs in th pth-first forst Back-gs (B) ar non-tr gs conncting a vrtx u to an ancstor v in a pth-first tr Cross-gs (C) ar all othr non-tr gs Dpth-first sarch xampl Dpth-first sarch xampl S: { } S: {a} Dpth-first sarch xampl Dpth-first sarch xampl S: {a, } S: {a,, f}

Dpth-first sarch xampl Dpth-first sarch xampl S: {a, } S: {a,, g} Dpth-first sarch xampl Dpth-first sarch xampl S: {a, } S: {a} Dpth-first sarch xampl Dpth-first sarch xampl S: {a, b} S: {a, b, }

Dpth-first sarch xampl Dpth-first sarch xampl S: {a, b} S: {a} Dpth-first sarch xampl Dpth-first sarch xampl S: { } S: {c} Dpth-first sarch xampl Dpth-first sarch xampl S: {c, h} S: {c}

Dpth-first sarch xampl 1 16 First loop taks O( V ) tim How many tims will DFS-VISIT b call? How many tims will this loop run? Rcursiv call S: { } Rsult of DFS run is a forst! Prcnc graph for how to gt rss unrwar watch socks Analysis of DFS Running tim of DFS: First loop in DFS taks Θ( V ) tim (ach vrtx visit xactly onc) In scon loop with th rcursiv calls, w obsrv that DFS visit will b call xactly onc on ach vrtx For ach vrtx v whr DFS-VISIT(v) is call, th loop in DFS-VISIT is call Aj[v] tims Sinc vv Aj[v] = Θ( E ) th total cost of scon loop in DFS will b Θ( E ) fl Running tim of DFS is Θ( V + E ) What is an actual schul for gtting rss? Topological sort A linar orr of all nos in th jackt graph G such that if G contains an g (u,v) thn u appars bfor v in th orring Topological sort is only possibl if th graph is acyclic Exampl application: Dirct Acyclic Graph (DAG) rprsnts prcnc rlations btwn tasks or vnts Eg btwn tasks if first task must b prform bfor scon task Thn a topological sort givs a possibl schul of th tasks on a singl rsourc On prcnc graph might hav svral possibl schuls pants blt shirt ti shos Topological sort: algorithm W can us DFS to gt a topological sort Informal scription: Call DFS(G) to comput finishing tims f[u] for all vrtics u Put nos into list so thy ar stor in crasing orr w.r.t. finishing tim A irct way is to first us DFS(G) an thn sort w.r.t. finishing tim f[u] This yils tim Θ( V + E )+ O( V lg V ) = O( V lg V + E ) But asy to moify DFS to comput th sort list on th fly : Just insrt ach vrtx into list immiatly whn finish As no asymptotic complxity to DFS: still Θ( V + E ) 11/16 1/15 6/7 Exampl: Topological sort unrwar pants blt shirt ti jackt 1/8 /5 /4 socks 17/18 shos 1/14 watch Prcnc graph for how to gt rss socks shos watch unrwar pants blt shirt ti jackt 17/18 11/16 1/15 1/14 9/10 1/8 6/7 /5 /4 9/10 DFS algorithm givs th shown [u] an f[u] Evnts sort accoring to thir finishing tim = topological sort

Strongly connct componnts A strongly- connct componnt (SCC) of irct graph G = (V,E) is a maximal subst of vrtics C Œ V such that for vry pair of vrtics u an v in C both vrtics ar rachabl from ach othr E A C B Dirct graph D Strongly connct componnts: {A,B,C,E} an {D} Graph Transpos Th transpos of a irct graph G=(V,E) is th graph G T = (V,E T ) such that G T ={(v,u) V x V : (u,v) E } Thus, G T is G with all gs rvrs Both graphs contains th sam nos Both graphs has th sam SCCs G T can b crat in O(V + E) tim 1 4 5 Original graph G 6 1 4 5 Transpos graph G T 6 Fining SCCs W can us algorithms for DFS an graph transposal for fining SCCs of graph G Stps: 1. Call DFS(G) to comput finishing tims f[u] for ach vrtx u in G. Comput transpos graph G T. Call DFS(G T ), but in th main loop, consir th vrtics in crasing f[u] (as comput in lin 1) 4. Each pth-first tr foun in G T forms a SCC in G Original graph G Fining SCCs Fining SCCs 1. Call DFS(G) to comput finishing tim f[u] for ach vrtx u 1/14 11/16 1/15 /4 1/10 8/9 Sort nos on thir finishing tim: ï b,,a,c,,g,f,h /7 5/6 Fining SCCs. Comput G T from original graph

Fining SCCs. Call DFS(G T ), but in th main loop, consir th vrtics in crasing f[u] (as comput in lin 1 ï b,,a,c,,g,f,h) Dp- first- trs foun: {b,a,}, {c,}, {g,f}, {h} Fining SCCs 4. Each pth- first tr foun in G T forms a SCC in G: {b,a,}, {c,}, {g,f}, {h} 5. Collaps all SCC to on singl no. Th rsult is th acyclic G SCC graph Each vrtx is on SCC ab fg c h Th visits of nos in th scon DFS corrspons to visiting th vrtics in G SCC in topological sort orr Analysis of SCC algorithm 1. Call DFS(G) to comput finishing tims f[u] for ach vrtx u in G ï Θ(V + E). Comput transpos graph G T ï Θ(V + E). Call DFS(G T ), but in th main loop, consir th vrtics in crasing f[u] (as comput in lin 1) ï Θ(V + E) 4. Collaps ach pth-first tr foun in G T to a SCC ï Θ(V + E) Ovrall total tim: Θ(V + E) Th En!