GREEDY TECHNIQUE. Greedy method vs. Dynamic programming method:

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

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

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

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

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

COMP108 Algorithmic Foundations

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Constructive Geometric Constraint Solving

12. Traffic engineering

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

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

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

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

1 Introduction to Modulo 7 Arithmetic

Planar Upward Drawings

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

Minimum Spanning Trees

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

ECE COMBINATIONAL BUILDING BLOCKS - INVEST 13 DECODERS AND ENCODERS

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

Problem solving by search

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

NP-Completeness. CS3230 (Algorithm) Traveling Salesperson Problem. What s the Big Deal? Given a Problem. What s the Big Deal? What s the Big Deal?

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

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

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

Similarity Search. The Binary Branch Distance. Nikolaus Augsten.

Computational Biology, Phylogenetic Trees. Consensus methods

(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

CSI35 Chapter 11 Review

Compression. Compression. Compression. This part of the course... Ifi, UiO Norsk Regnesentral Vårsemester 2005 Wolfgang Leister

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

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

Outline. Binary Tree

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

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

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

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

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

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

QUESTIONS BEGIN HERE!

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

The Plan. Honey, I Shrunk the Data. Why Compress. Data Compression Concepts. Braille Example. Braille. x y xˆ

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

QUESTIONS BEGIN HERE!

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

Garnir Polynomial and their Properties

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

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

LEO VAN IERSEL TU DELFT

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

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

CS September 2018

Multipoint Alternate Marking method for passive and hybrid performance monitoring

COMPLEXITY OF COUNTING PLANAR TILINGS BY TWO BARS

A 4-state solution to the Firing Squad Synchronization Problem based on hybrid rule 60 and 102 cellular automata

CS553 Lecture Register Allocation 1

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

Graph Contraction and Connectivity

EE1000 Project 4 Digital Volt Meter

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

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

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

Seven-Segment Display Driver

DUET WITH DIAMONDS COLOR SHIFTING BRACELET By Leslie Rogalski

Trees as operads. Lecture A formalism of trees

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

Module 2 Motion Instructions

Register Allocation. How to assign variables to finitely many registers? What to do when it can t be done? How to do so efficiently?

Lecture 20: Minimum Spanning Trees (CLRS 23)

MULTIPLE-LEVEL LOGIC OPTIMIZATION II

Round 7: Graphs (part I)

New challenges on Independent Gate FinFET Transistor Network Generation

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

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

CS553 Lecture Register Allocation I 3

Jonathan Turner Exam 2-10/28/03

Solutions to Homework 5

Scientific Programming. Graphs

Lecture 6: Coding theory

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

RAM Model. I/O Model. Real Machine Example: Nehalem : Algorithms in the Real World 4/9/13

CS 103 BFS Alorithm. Mark Redekopp

Transcription:

Dinition: GREEDY TECHNIQUE Gry thniqu is gnrl lgorithm sign strtgy, uilt on ollowing lmnts: onigurtions: irnt hois, vlus to in ojtiv untion: som onigurtions to ithr mximiz or minimiz Th mtho: Applil to optimiztion prolms ONLY Construts solution through squn o stps Eh stp xpns prtilly onstrut solution so r, until omplt solution to th prolm is rh. On h stp, th hoi m must Fsil: it hs to stisy th prolm s onstrints Lolly optiml: it hs to th st lol hoi mong ll sil hois vill on tht stp Irrvol: On m, it nnot hng on susqunt stps o th lgorithm NOTE: Gry mtho works st whn ppli to prolms with th gryhoi proprty A glollyoptiml solution n lwys oun y sris o lol improvmnts rom strting onigurtion. Gry mtho vs. Dynmi progrmming mtho: LIKE ynmi progrmming, gry mtho solvs optimiztion prolms. LIKE ynmi progrmming, gry mtho prolms xhiit optiml sustrutur UNLIKE ynmi progrmming, gry mtho prolms xhiit th gry hoi proprty vois ktring. Applitions o th Gry Strtgy: Optiml solutions: Chng mking Minimum Spnning Tr (MST) Singlsour shortst pths Humn os Approximtions: Trvling Slsmn Prolm (TSP) Frtionl Knpsk prolm

Dinition: Spnning Tr Spnning tr is onnt yli sugrph (tr) o th givn grph (G) tht inlus ll o G s vrtis Exmpl: Consir th ollowing grph Th spnning trs or th ov grph r s ollows: Wight (T ) = 8 Wight (T ) = Wight (T ) = 9 Minimum Spnning Tr (MST) Dinition: MST o wight, onnt grph G is in s: A spnning tr o G with minimum totl wight. Exmpl: Consir th xmpl o spnning tr: For th givn grph thr r thr possil spnning trs. Among thm th spnning tr with th minimum wight is th MST or th givn grph Qustion: Why n t w us BRUTE FORCE mtho in onstruting MST? Answr: I w us Brut or mtho Exhustiv srh pproh hs to ppli. Two srious ostls :. Th numr o spnning trs grows xponntilly with grph siz.. Gnrting ll spnning trs or th givn grph is not sy.

MST Applitions: Ntwork sign. Tlphon, ltril, hyruli, TV l, omputr, ro Approximtion lgorithms or NPhr prolms. Trvling slsprson prolm, Stinr tr Clustr nlysis. Ruing t storg in squning mino is in protin Lrning slint turs or rltim vriition Auto onig protool or Ethrnt riging to voi yls in ntwork, t Prim s Algorithm to in minimum spnning tr Som usul initions: Fring g: An g whih hs on vrtx is in prtilly onstrut tr Ti n th othr is not. Unsn g: An g with oth vrtis not in Ti Algorithm: ALGORITHM Prim (G) //Prim s lgorithm or onstruting MST //Input: A wight onnt grph G = { V, E } //Output: ET th st o gs omposing MST o G // th st o tr vrtis n initiliz with ny vrtx V T { v 0 } E T Ø or i to V o rturn E T Fin minimumwight g * = (v*, u*) mong ll th gs (v, u) suh tht v is in V T n u is in V V T V T V T U { u*} E T E T U { *}

Th mtho: STEP : Strt with tr, T 0, onsisting o on vrtx STEP : Grow tr on vrtx/g t tim Construt sris o xpning sutrs T, T, T n. At h stg onstrut T i + rom T i y ing th minimum wight g onnting vrtx in tr (Ti) to on vrtx not yt in tr, hoos rom ring gs (this is th gry stp!) Algorithm stops whn ll vrtis r inlu Exmpl: Apply Prim s lgorithm or th ollowing grph to in MST. 8 Solution: Tr vrtis Rmining vrtis Grph (, ) (, ) (, ) (, ) (, ) (, ) (, ) (, ) (, ) (, ) (, ) (, ) (, ) (, ) (, )

(, ) (, ) (, ) (, ) (, ) (, ) Algorithm stops sin ll vrtis r inlu. Th wight o th minimum spnning tr is Eiiny: Eiiny o Prim s lgorithm is s on t strutur us to stor priority quu. Unorr rry: Eiiny: Θ(n ) Binry hp: Eiiny: Θ(m log n) Minhp: For grph with n nos n m gs: Eiiny: (n + m) log n Conlusion: Prim s lgorithm is vrtx s lgorithm Prim s lgorithm Ns priority quu or loting th nrst vrtx. Th hoi o priority quu mttrs in Prim implmnttion. o Arry optiml or ns grphs o Binry hp ttr or sprs grphs o Fioni hp st in thory, ut not in prti

Algorithm: ALGORITHM Kruskl (G) Kruskl s Algorithm to in minimum spnning tr //Kruskl s lgorithm or onstruting MST //Input: A wight onnt grph G = { V, E } //Output: ET th st o gs omposing MST o G Sort E in sning orr o th g wights // initiliz th st o tr gs n its siz E T Ø g_ountr 0 //initiliz th numr o pross gs k 0 whil g_ountr < V k k + i E T U { i k } is yli E T E T U { i k } g_ountr g_ountr + rturn E T Th mtho: STEP : Sort th gs y inrsing wight STEP : Strt with orst hving V numr o trs. STEP : Numr o trs r ru y ONE t vry inlusion o n g At h stg: Among th gs whih r not yt inlu, slt th on with minimum wight AND whih os not orm yl. th g will ru th numr o trs y on y omining two trs o th orst Algorithm stops whn V gs r inlu in th MST i. : whn th numr o trs in th orst is ru to ONE.

Exmpl: Apply Kruskl s lgorithm or th ollowing grph to in MST. Solution: Th list o gs is: Eg Wight 8 Sort th gs in sning orr: Eg Wight 8 8 Eg Wight Insrtion sttus Insrtion orr YES Eg Wight Insrtion sttus Insrtion orr YES 7

Eg Wight Insrtion sttus Insrtion orr YES Eg Wight Insrtion sttus Insrtion orr YES Eg Wight Insrtion sttus NO Insrtion orr Eg Wight Insrtion sttus NO Insrtion orr Eg Wight Insrtion sttus Insrtion orr YES Algorithm stops s V gs r inlu in th MST 8

Eiiny: Eiiny o Kruskl s lgorithm is s on th tim n or sorting th g wights o givn grph. With n iint sorting lgorithm: Eiiny: Θ( E log E ) Conlusion: Kruskl s lgorithm is n g s lgorithm Prim s lgorithm with hp is str thn Kruskl s lgorithm. Som usul initions: Dijkstr s Algorithm to in Singl Sour Shortst Pths Shortst Pth Prolm: Givn onnt irt grph G with nonngtiv wights on th gs n root vrtx r, in or h vrtx x, irt pth P (x) rom r to x so tht th sum o th wights on th gs in th pth P (x) is s smll s possil. Algorithm By Duth omputr sintist Esgr Dijkstr in 99. Solvs th singlsour shortst pth prolm or grph with nonngtiv g wights. This lgorithm is otn us in routing. E.g.: Dijkstr's lgorithm is usully th working prinipl hin linkstt routing protools ALGORITHM Dijkstr(G, s) //Input: Wight onnt grph G n sour vrtx s //Output: Th lngth Dv o shortst pth rom s to v n its pnultimt vrtx Pv or vry vrtx v in V //initiliz vrtx priority in th priority quu Initiliz (Q) or vry vrtx v in V o D v ; P v null // Pv, th prnt o v insrt(q, v, D v ) //initiliz vrtx priority in priority quu s 0 //upt priority o s with s, mking s, th minimum Drs(Q, s, s ) V T 9

or i 0 to V o u* DltMin(Q) //xpning th tr, hoosing th lolly st vrtx V T V T U {u*} or vry vrtx u in V V T tht is jnt to u* o i Du* + w (u*, u) < Du Du Du + w (u*, u); Pu u* Drs(Q, u, Du) Th mtho Dijkstr s lgorithm solvs th singl sour shortst pth prolm in stgs. Stg : A gry lgorithm omputs th shortst istn rom sour to ll othr nos in th grph n svs in t strutur. Stg : Uss th t strutur or ining shortst pth rom sour to ny vrtx v. At h stp, n or h vrtx x, kp trk o istn D(x) n irt pth P(x) rom root to vrtx x o lngth D(x). Sn irst rom th root n tk initil pths P( r, x ) = ( r, x ) with D(x) = w( rx ) whn rx is n g, D(x) = whn rx is not n g. For h tmporry vrtx y istint rom x, st D(y) = min{ D(y), D(x) + w(xy) } Exmpl: Apply Dijkstr s lgorithm to in Singl sour shortst pths with vrtx s th sour. 8 Solution: Lngth Dv o shortst pth rom sour (s) to othr vrtis v n Pnultimt vrtx Pv or vry vrtx v in V: 0

D = 0, P = null D =, P = null D =, P = null D =, P = null D =, P = null D =, P = null Tr vrtis (, 0 ) Rmining vrtis (, ) (, ) (, ) (, ) (, ) Distn & Pth vrtx D = 0 P = D = P = [, ] D = P = null D = P = null D = P = [, ] D = P = [, ] Grph (, ) (, ) (, ) (, + ) (, ) (, ) (, ) (, + ) (, ) (, ) (, 0 ) (, ) D = 0 P = D = P = [, ] D = P = [,,] D = P = null D = P = [, ] D = P = [, ] D = 0 P = D = P = [, ] D = P = [,,] D=0 P = [,,,] D = P = [, ] D = P = [, ] D = 0 P = D = P = [, ] D = P = [,,] D=0 P = [,,,] D = P = [, ] D = P = [, ] (, ) (, 0 ) D = 0 P = D = P = [, ] D = P = [,,] D=0 P = [,,,] D = P = [, ] D = P = [, ] (, 0) Algorithm stops sin no gs to sn

Conlusion: Dosn t work with ngtiv wights Applil to oth unirt n irt grphs Us unorr rry to stor th priority quu: Eiiny = Θ(n ) Us minhp to stor th priority quu: Eiiny = O(m log n) Som usul initions: Humn Trs Co wor: Enoing txt tht ompriss n hrtrs rom som lpht y ssigning to h o th txt s hrtrs som squn o its. This its squn is ll o wor Fix lngth noing: Assigns to h hrtr it string o th sm lngth. Vril lngth noing: Assigns o wors o irnt lngths to irnt hrtrs. Prolm: How n w tll how mny its o n no txt rprsnt ith hrtr? W n us prix r os Prix r o: In Prix r o, no owor is prix o owor o nothr hrtr. Binry prix o : o Th hrtrs r ssoit with th lvs o inry tr. o All lt gs r ll 0 o All right gs r ll o Cowor o hrtr is otin y roring th lls on th simpl pth rom th root to th hrtr s l. o Sin, thr is no simpl pth to l tht ontinus to nothr l, no owor n prix o nothr owor Humn lgorithm: Construts inry prix o tr By Dvi A Humn in 9. Humn s lgorithm hivs t omprssion y ining th st vril lngth inry noing shm or th symols tht our in th il to omprss. Humn oing uss rqunis o th symols in th string to uil vril rt prix o o Eh symol is mpp to inry string o Mor rqunt symols hv shortr os o No o is prix o nothr o Humn Cos or Dt Comprssion hivs 090% Comprssion

Constrution: Stp : Initiliz n onno trs n ll thm with th hrtrs o th lpht. Ror th rquny o h hrtr in its tr s root to init th tr s wight. (Mor gnrlly th wight o tr will qul to th sum o th rqunis in th tr s lvs) Stp : Rpt th ollowing oprtion until singl tr is otin. Fin two trs with smllst wight. Mk thm th lt n right sutr o nw tr n ror th sum o thir wights in th root o th nw tr s its wight Exmpl: Construt Humn o or th ollowing t: Chrtr A B C D proility 0. 0. 0. 0. 0. Eno th txt ABACABAD using th o. Do th txt whos noing is 00000000 Solution: A 0. B 0. C 0. D 0. 0. B 0. D 0. 0. C 0. A 0. C 0. A 0. 0. B D 0. 0. 0.

0. 0. A 0. B 0. D 0. 0. C 0. 0. A 0. 0. 0. B 0. D 0. 0. C 0..0 0 A 0. 0 B 0. 0. 0 D 0. 0. 0 0. 0. C 0. Algorithm STOPS s SINGLE TREE is otin Co wors r: Chrtr A B C D proility 0. 0. 0. 0. 0. Co wor 0 00 0 0 Eno txt or ABACABAD using th o wors: 000000000 Do txt or no txt 00000000 is: BADADA Comput omprssion rtio: Bits pr hrtr = Cowor lngth * Frquny = ( * 0. ) + ( * 0.) + ( * 0. ) + ( * 0. ) + ( * 0. ) =.0 Comprssion rtio is = (.0 )/. 00% =.%