Copyright 2000, Kevin Wayne 1

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

Copyright 2000, Kevin Wayne 1

Copyright 2000, Kevin Wayne 1

Chapter Interval Scheduling. Greedy Algorithms. What is a Greedy Algorithm? Interval Scheduling

Greedy Algorithms. Greedy Algorithms. Piyush Kumar

Minimum Spanning Trees

Lecture 20: Minimum Spanning Trees (CLRS 23)

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

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

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

COMP108 Algorithmic Foundations

Minimum Spanning Trees

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

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

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

(Minimum) Spanning Trees

Minimum Spanning Trees (CLRS 23)

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

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

Strongly connected components. Finding strongly-connected components

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

Lecture II: Minimium Spanning Tree Algorithms

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

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

The University of Sydney MATH 2009

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

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

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

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

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

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

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

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

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

Spanning Tree. Preview. Minimum Spanning Tree. Minimum Spanning Tree. Minimum Spanning Tree. Minimum Spanning Tree 10/17/2017.

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

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

Section 3: Antiderivatives of Formulas

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

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

12. Traffic engineering

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

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

CSE303 - Introduction to the Theory of Computing Sample Solutions for Exercises on Finite Automata

CS September 2018

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

minimize c'x subject to subject to subject to

Minimum Spanning Trees (CLRS 23)

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

CS 241 Analysis of Algorithms

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

ECE COMBINATIONAL BUILDING BLOCKS - INVEST 13 DECODERS AND ENCODERS

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

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

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

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

Planar Upward Drawings

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

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

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

Integration Continued. Integration by Parts Solving Definite Integrals: Area Under a Curve Improper Integrals

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

Graph Search Algorithms

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

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

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

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 =?

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

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

Multipoint Alternate Marking method for passive and hybrid performance monitoring

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

Constructive Geometric Constraint Solving

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

DUET WITH DIAMONDS COLOR SHIFTING BRACELET By Leslie Rogalski

In which direction do compass needles always align? Why?

FSA. CmSc 365 Theory of Computation. Finite State Automata and Regular Expressions (Chapter 2, Section 2.3) ALPHABET operations: U, concatenation, *

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

Problem solving by search

EE1000 Project 4 Digital Volt Meter

Single Source Shortest Paths (with Positive Weights)

Applications of trees

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

Algorithm Design and Analysis

Numbering Boundary Nodes

Last time: introduced our first computational model the DFA.

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

a b c cat CAT A B C Aa Bb Cc cat cat Lesson 1 (Part 1) Verbal lesson: Capital Letters Make The Same Sound Lesson 1 (Part 1) continued...

The Mathematics of Harmonic Oscillators

Garnir Polynomial and their Properties

QUESTIONS BEGIN HERE!

1 Introduction to Modulo 7 Arithmetic

Convergence Theorems for Two Iterative Methods. A stationary iterative method for solving the linear system: (1.1)

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

23 Minimum Spanning Trees

(a) v 1. v a. v i. v s. (b)

Register Allocation. Register Allocation. Principle Phases. Principle Phases. Example: Build. Spills 11/14/2012

Algorithm Design and Analysis

2 Trees and Their Applications

Transcription:

Chptr. Intrvl Shulng Gry Algorthms Sls y Kvn Wyn. Copyrght 005 Prson-Ason Wsly. All rghts rsrv. Intrvl Shulng Intrvl Shulng: Gry Algorthms Intrvl shulng. Jo strts t s n fnshs t f. Two os omptl f thy on't ovrlp. Gol: fn mxmum sust of mutully omptl os. Gry tmplt. Consr os n som orr. Tk h o prov t's omptl wth th ons lry tkn. [Erlst strt tm] Consr os n snng orr of strt tm s. f g h 0 3 5 6 7 8 9 0 Tm [Erlst fnsh tm] Consr os n snng orr of fnsh tm f. [Shortst ntrvl] Consr os n snng orr of ntrvl lngth f - s. [Fwst onflts] For h o, ount th numr of onfltng os. Shul n snng orr of onflts. 3 Copyrght 000, Kvn Wyn

Intrvl Shulng: Gry Algorthms Intrvl Shulng: Gry Algorthm Gry tmplt. Consr os n som orr. Tk h o prov t's omptl wth th ons lry tkn. Gry lgorthm. Consr os n nrsng orr of fnsh tm. Tk h o prov t's omptl wth th ons lry tkn. rks rlst strt tm rks shortst ntrvl rks fwst onflts Sort os y fnsh tms so tht f f... f n. os slt A φ for = to n { f (o omptl wth A) A A {} } rturn A Implmntton. O(n log n). Rmmr o * tht ws lst to A. Jo s omptl wth A f s f *. 5 6 Intrvl Shulng: Anlyss Intrvl Shulng: Anlyss Thorm. Gry lgorthm s optml. Thorm. Gry lgorthm s optml. Pf. (y ontrton) Assum gry s not optml, n lt's s wht hppns. Lt,,... k not st of os slt y gry. Lt,,... m not st of os n th optml soluton wth =, =,..., r = r for th lrgst possl vlu of r. Pf. (y ontrton) Assum gry s not optml, n lt's s wht hppns. Lt,,... k not st of os slt y gry. Lt,,... m not st of os n th optml soluton wth =, =,..., r = r for th lrgst possl vlu of r. o r+ fnshs for r+ o r+ fnshs for r+ Gry: r r+ Gry: r r+ OPT: r r+... OPT: r r+... why not rpl o r+ wth o r+? Proof y nuton tht for h r >= th gr lgorthm stys h of th optml lgorthm soluton stll fsl n optml, ut ontrts mxmlty of r. 7 8 Copyrght 000, Kvn Wyn

Intrvl Prttonng. Intrvl Prttonng Intrvl prttonng. Ltur strts t s n fnshs t f. Gol: fn mnmum numr of lssrooms to shul ll lturs so tht no two our t th sm tm n th sm room. Ex: Ths shul uss lssrooms to shul 0 lturs. g h f 9 9:30 0 0:30 :30 :30 :30 :30 3 3:30 :30 Tm 0 Intrvl Prttonng Intrvl Prttonng: Lowr Boun on Optml Soluton Intrvl prttonng. Ltur strts t s n fnshs t f. Gol: fn mnmum numr of lssrooms to shul ll lturs so tht no two our t th sm tm n th sm room. Ex: Ths shul uss only 3. Df. Th pth of st of opn ntrvls s th mxmum numr tht ontn ny gvn tm. Ky osrvton. Numr of lssrooms n pth. Ex: Dpth of shul low = 3 shul low s optml.,, ll ontn 9:30 Q. Dos thr lwys xst shul qul to pth of ntrvls? f f g g h h 9 9:30 0 0:30 :30 :30 :30 :30 3 3:30 :30 Tm 9 9:30 0 0:30 :30 :30 :30 :30 3 3:30 :30 Tm Copyrght 000, Kvn Wyn 3

Intrvl Prttonng: Gry Algorthm Intrvl Prttonng: Gry Anlyss Gry lgorthm. Consr lturs n nrsng orr of strt tm: ssgn ltur to ny omptl lssroom. Osrvton. Gry lgorthm nvr shuls two nomptl lturs n th sm lssroom. Sort ntrvls y strtng tm so tht s s... s n. 0 numr of llot lssrooms for = to n { f (ltur s omptl wth som lssroom k) shul ltur n lssroom k ls llot nw lssroom + shul ltur n lssroom + + } Thorm. Gry lgorthm s optml. Pf. Lt = numr of lssrooms tht th gry lgorthm llots. Clssroom s opn us w n to shul o, sy, tht s nomptl wth ll - othr lssrooms. Sn w sort y strt tm, ll ths nomptlts r us y lturs tht strt no ltr thn s. Thus, w hv lturs ovrlppng t tm s + ε. Ky osrvton ll shuls us lssrooms. Implmntton. O(n log n). For h lssroom k, mntn th fnsh tm of th lst o. Kp th lssrooms n prorty quu. 3 Shulng to Mnmzng Ltnss. Shulng to Mnmz Ltnss Mnmzng ltnss prolm. Sngl rsour prosss on o t tm. Jo rqurs t unts of prossng tm n s u t tm. If strts t tm s, t fnshs t tm f = s + t. Ltnss: = mx { 0, f - }. Gol: shul ll os to mnmz mxmum ltnss L = mx. Ex: t 3 3 5 3 6 6 8 9 9 5 ltnss = ltnss = 0 mx ltnss = 6 3 = 9 = 8 6 = 5 = 6 5 = = 9 0 3 5 6 7 8 9 0 3 5 6 Copyrght 000, Kvn Wyn

Mnmzng Ltnss: Gry Algorthms Mnmzng Ltnss: Gry Algorthms Gry tmplt. Consr os n som orr. Gry tmplt. Consr os n som orr. [Shortst prossng tm frst] Consr os n snng orr of prossng tm t. [Erlst ln frst] Consr os n snng orr of ln. [Shortst prossng tm frst] Consr os n snng orr of prossng tm t. t 00 0 0 ountrxmpl [Smllst slk] Consr os n snng orr of slk - t. [Smllst slk] Consr os n snng orr of slk - t. t 0 0 ountrxmpl 7 8 Mnmzng Ltnss: Gry Algorthm Mnmzng Ltnss: No Il Tm Gry lgorthm. Erlst ln frst. Osrvton. Thr xsts n optml shul wth no l tm. Sort n os y ln so tht n = = 6 0 3 5 6 = 7 8 9 0 t 0 for = to n Assgn o to ntrvl [t, t + t ] s t, f t + t t t + t output ntrvls [s, f ] = = 6 = 0 3 5 6 7 8 9 0 Osrvton. Th gry shul hs no l tm. mx ltnss = = 6 = 8 3 = 9 = 9 5 = 6 = 5 0 3 5 6 7 8 9 0 3 5 9 0 Copyrght 000, Kvn Wyn 5

Mnmzng Ltnss: Invrsons Mnmzng Ltnss: Invrsons Df. An nvrson n shul S s pr of os n suh tht: < ut shul for. nvrson Df. An nvrson n shul S s pr of os n suh tht: < ut shul for. nvrson f for swp for swp ftr swp Osrvton. Gry shul hs no nvrsons. Osrvton. If shul (wth no l tm) hs n nvrson, t hs on wth pr of nvrt os shul onsutvly. Clm. Swppng two nt, nvrt os rus th numr of nvrsons y on n os not nrs th mx ltnss. Pf. Lt th ltnss for th swp, n lt ' t ftrwrs. ' k = k for ll k, f' ' If o s lt: # = = f # f f (fnton) ( fnshs t tm f ) ( < ) (fnton) Mnmzng Ltnss: Anlyss of Gry Algorthm Gry Anlyss Strtgs Thorm. Gry shul S s optml. Pf. Dfn S* to n optml shul tht hs th fwst numr of nvrsons, n lt's s wht hppns. Cn ssum S* hs no l tm. If S* hs no nvrsons, thn S = S*. If S* hs n nvrson, lt - n nt nvrson. swppng n os not nrs th mxmum ltnss n strtly rss th numr of nvrsons ths ontrts fnton of S* Gry lgorthm stys h. Show tht ftr h stp of th gry lgorthm, ts soluton s t lst s goo s ny othr lgorthm's. Exhng rgumnt. Grully trnsform ny soluton to th on foun y th gry lgorthm wthout hurtng ts qulty. Struturl. Dsovr smpl "struturl" oun ssrtng tht vry possl soluton must hv rtn vlu. Thn show tht your lgorthm lwys hvs ths oun. 3 Copyrght 000, Kvn Wyn 6

Optml Offln Chng.3 Optml Chng Chng. Ch wth pty to stor k tms. Squn of m tm rqusts,,, m. Ch ht: tm lry n h whn rqust. Ch mss: tm not lry n h whn rqust: must rng rqust tm nto h, n vt som xstng tm, f full. Gol. Evton shul tht mnmzs numr of h msss. Ex: k =, ntl h =, rqusts:,,,,,,,. Optml vton shul: h msss. rqusts h 6 Optml Offln Chng: Frthst-In-Futur Ru Evton Shuls Frthst-n-futur. Evt tm n th h tht s not rqust untl frthst n th futur. Df. A ru shul s shul tht only nsrts n tm nto th h n stp n whh tht tm s rqust. urrnt h: f futur qurs: g f f g h... h mss t ths on Thorm. [Blly, 960s] FF s optml vton shul. Pf. Algorthm n thorm r ntutv; proof s sutl. Intuton. Cn trnsform n unru shul nto ru on wth no mor h msss. x x n unru shul ru shul 7 8 Copyrght 000, Kvn Wyn 7

Frthst-In-Futur: Anlyss Thorm. FF s optml vton lgorthm. Pf. (y nuton on numr or rqusts ). Shortst Pths n Grph Invrnt: Thr xsts n optml ru shul S tht mks th sm vton shul s S FF through th frst + rqusts. Lt S ru shul tht stsfs nvrnt through rqusts. W prou S' tht stsfs nvrnt ftr + rqusts. Consr (+) st rqust = +. Sn S n S FF hv gr up untl now, thy hv th sm h ontnts for rqust +. Cs : ( s lry n th h). S' = S stsfs nvrnt. Cs : ( s not n th h n S n S FF vt th sm lmnt). S' = S stsfs nvrnt. shortst pth from Prnton CS prtmnt to Enstn's hous 30 Shortst Pth Prolm Dkstr's Algorthm Shortst pth ntwork. Drt grph G = (V, E). Sour s, stnton t. Lngth = lngth of g. Shortst pth prolm: fn shortst rt pth from s to t. Dkstr's lgorthm. Mntn st of xplor nos S for whh w hv trmn th shortst pth stn (u) from s to u. Intlz S = { s }, (s) = 0. Rptly hoos unxplor no v whh mnmzs π ( v) = mn = ( u, v) : u S ( u) +, ost of pth = sum of g osts n pth v to S, n st (v) = π(v). shortst pth to som u n xplor prt, follow y sngl g (u, v) s 9 5 5 6 0 30 5 3 8 6 6 6 3 9 Cost of pth s--3-5-t = 9 + 3 + + 6 = 8. S s (u) u v 7 t 36 37 Copyrght 000, Kvn Wyn 8

Dkstr's Algorthm Dkstr's Algorthm: Proof of Corrtnss Dkstr's lgorthm. Mntn st of xplor nos S for whh w hv trmn th shortst pth stn (u) from s to u. Intlz S = { s }, (s) = 0. Rptly hoos unxplor no v whh mnmzs π ( v) = mn = ( u, v) : u S v to S, n st (v) = π(v). ( u) +, shortst pth to som u n xplor prt, follow y sngl g (u, v) Invrnt. For h no u S, (u) s th lngth of th shortst s-u pth. Pf. (y nuton on S ) Bs s: S = s trvl. Inutv hypothss: Assum tru for S = k. Lt v nxt no to S, n lt u-v th hosn g. Th shortst s-u pth plus (u, v) s n s-v pth of lngth π(v). Consr ny s-v pth P. W'll s tht t's no shortr thn π(v). Lt x-y th frst g n P tht lvs S, n lt P' th supth to x. P s lry too long s soon s t lvs S. P' x P y S s (u) u v (P) (P') + (x,y) (x) + (x, y) π(y) π(v) s S u v nonngtv wghts nutv hypothss fn of π(y) Dkstr hos v nst of y 38 39 Dkstr's Algorthm: Implmntton For h unxplor no, xpltly mntn π(v) = mn (u) +. = (u,v) : u S Nxt no to xplor = no wth mnmum π(v). Whn xplorng v, for h nnt g = (v, w), upt π(w) = mn { π(w), π(v)+ }. Extr Sls Effnt mplmntton. Mntn prorty quu of unxplor nos, prortz y π(v). Prorty Quu PQ Oprton Insrt ExtrtMn ChngKy Dkstr n n m Arry n n Bnry hp log n log n log n -wy Hp log n log n log n F hp log n IsEmpty n Totl n m log n m log m/n n m + n log n Invul ops r mortz ouns 0 Copyrght 000, Kvn Wyn 9

Con Chngng Con Chngng Gol. Gvn urrny nomntons:, 5, 0, 5, 00, vs mtho to py mount to ustomr usng fwst numr of ons. Ex: 3. Gr s goo. Gr s rght. Gr works. Gr lrfs, uts through, n pturs th ssn of th volutonry sprt. - Goron Gko (Mhl Dougls) Cshr's lgorthm. At h trton, on of th lrgst vlu tht os not tk us pst th mount to p. Ex: $.89. 3 Con-Chngng: Gry Algorthm Con-Chngng: Anlyss of Gry Algorthm Cshr's lgorthm. At h trton, on of th lrgst vlu tht os not tk us pst th mount to p. Sort ons nomntons y vlu: < < < n. ons slt S φ whl (x 0) { lt k lrgst ntgr suh tht k x f (k = 0) rturn "no soluton foun" x x - k S S {k} } rturn S Thorm. Gr s optml for U.S. ong:, 5, 0, 5, 00. Pf. (y nuton on x) Consr optml wy to hng k x < k+ : gry tks on k. W lm tht ny optml soluton must lso tk on k. f not, t ns nough ons of typ,, k- to up to x tl low nts no optml soluton n o ths Prolm rus to on-hngng x - k nts, whh, y nuton, s optmlly solv y gry lgorthm. k k All optml solutons must stsfy Mx vlu of ons,,, k- n ny OPT P - 5 N 3 0 N + D + 5 = 9 Q. Is shr's lgorthm optml? 5 5 00 Q 3 no lmt 0 + = 75 + = 99 5 Copyrght 000, Kvn Wyn 0

/7/ Con-Chngng: Anlyss of Gry Algorthm Osrvton. Gry lgorthm s su-optml for US postl nomntons:, 0,, 3, 70, 00, 350, 5, 500. Countrxmpl. 0. Gry: 00, 3,,,,,,. Optml: 70, 70. 6 Copyrght 000, Kvn Wyn