COMP108 Algorithmic Foundations

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

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

Minimum Spanning Trees

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

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

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

CS 241 Analysis of Algorithms

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Garnir Polynomial and their Properties

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

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

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

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

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

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

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

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

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

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

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

GREEDY TECHNIQUE. Greedy method vs. Dynamic programming method:

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

Problem solving by search

CSI35 Chapter 11 Review

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

Minimum Spanning Trees

12. Traffic engineering

Last time: introduced our first computational model the DFA.

Final Exam Solutions

Planar Upward Drawings

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

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

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

ECE COMBINATIONAL BUILDING BLOCKS - INVEST 13 DECODERS AND ENCODERS

Weighted Matching and Linear Programming

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

Seven-Segment Display Driver

Section 3: Antiderivatives of Formulas

UNCORRECTED SAMPLE PAGES 4-1. Naming fractions KEY IDEAS. 1 Each shape represents ONE whole. a i ii. b i ii

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

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?

Constructive Geometric Constraint Solving

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

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

COMPLEXITY OF COUNTING PLANAR TILINGS BY TWO BARS

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

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 23, April 5, 2016

13. Binary tree, height 4, eight terminal vertices 14. Full binary tree, seven vertices v 7 v13. v 19

Two Graph Algorithms On an Associative Computing Model

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

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

QUESTIONS BEGIN HERE!

Multi-Section Coupled Line Couplers

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

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

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

Ch 1.2: Solutions of Some Differential Equations

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

1 Introduction to Modulo 7 Arithmetic

Instructions for Section 1

DUET WITH DIAMONDS COLOR SHIFTING BRACELET By Leslie Rogalski

Multipoint Alternate Marking method for passive and hybrid performance monitoring

Numbering Boundary Nodes

CS September 2018

DFA (Deterministic Finite Automata) q a

Computational Biology, Phylogenetic Trees. Consensus methods

Chapter 16. 1) is a particular point on the graph of the function. 1. y, where x y 1

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

(Minimum) Spanning Trees

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.

Graph Contraction and Connectivity

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...

Present state Next state Q + M N

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

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

( ) Geometric Operations and Morphing. Geometric Transformation. Forward v.s. Inverse Mapping. I (x,y ) Image Processing - Lesson 4 IDC-CG 1

Formal Concept Analysis

EE1000 Project 4 Digital Volt Meter

Week 3: Connected Subgraphs

Linked-List Implementation. Linked-lists for two sets. Multiple Operations. UNION Implementation. An Application of Disjoint-Set 1/9/2014

Walk Like a Mathematician Learning Task:

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

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

Examples and applications on SSSP and MST

Transcription:

Grdy mthods Prudn Wong http://www.s.liv..uk/~pwong/thing/omp108/01617

Coin Chng Prolm Suppos w hv 3 typs of oins 10p 0p 50p Minimum numr of oins to mk 0.8, 1.0, 1.? Grdy mthod

Lrning outoms Undrstnd wht grdy mthod is Al to pply Kruskl s lgorithm to find minimum spnning tr Al to pply Dijkstr s lgorithm to find singlsour shortst-pths Al to pply grdy lgorithm to find solution for Knpsk prolm 3

Grdy mthods How to grdy? At vry stp, mk th st mov you n mk Kp going until you r don Advntgs Don t nd to py muh ffort t h stp Usully finds solution vry quikly Th solution found is usully not d Possil prolm Th solution found my NOT th st on

Grdy mthods - xmpls Minimum spnning tr Kruskl s lgorithm Singl-sour shortst-pths Dijkstr s lgorithm Both lgorithms find on of th BEST solutions Knpsk prolm grdy lgorithm dos NOT find th BEST solution 5

Kruskl s lgorithm

Minimum Spnning tr (MST) Givn n undirtd onntd grph G Th dgs r llld y wight Spnning tr of G tr ontining ll vrtis in G Minimum spnning tr of G spnning tr of G with minimum wight 7

Exmpls Grph G (dg ll is wight) 3 3 1 d Spnning trs of G 3 3 3 1 d d 1 d MST 8

Id of Kruskl's lgorithm - MST min-wight dg nd min-wight dg trs in forst my mrg until on singl tr formd 9

Kruskl s lgorithm - MST 8 7 d 9 11 1 i 7 8 10 h 1 g f Arrng dgs from smllst to lrgst wight (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 10

Kruskl s lgorithm - MST 8 11 h 7 8 7 i 1 g d 1 f 9 10 (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 Choos th minimum wight dg (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 11

Kruskl s lgorithm - MST 8 7 d 9 11 1 i 7 8 10 h 1 g f Choos th nxt minimum wight dg (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 1

Kruskl s lgorithm - MST 8 11 h 7 8 7 i 1 g d 1 f 9 10 (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 Continu s long s no yl forms (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 13

Kruskl s lgorithm - MST 8 11 h 7 8 7 i 1 g d 1 f 9 10 (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 Continu s long s no yl forms (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 1

Kruskl s lgorithm - MST 8 11 h 7 8 7 i 1 g d 1 f 9 10 (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 Continu s long s no yl forms (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 15

Kruskl s lgorithm - MST 8 11 h 7 8 7 i 1 g d 1 f 9 10 (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 Continu s long s no yl forms (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 16

Kruskl s lgorithm - MST 8 11 h 7 8 i 7 1 g 1 (h,i) nnot inludd, othrwis, yl is formd d f 9 10 (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 17

Kruskl s lgorithm - MST 8 7 d 9 11 1 i 7 8 10 h 1 g f Choos th nxt minimum wight dg (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 18

Kruskl s lgorithm - MST 8 11 h 7 8 i 7 1 g 1 (,h) nnot inludd, othrwis, yl is formd d f 9 10 (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 19

Kruskl s lgorithm - MST 8 7 d 9 11 1 i 7 8 10 h 1 g f Choos th nxt minimum wight dg (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 0

Kruskl s lgorithm - MST 8 11 h 7 8 i 7 1 g 1 (f,) nnot inludd, othrwis, yl is formd d f 9 10 (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 1

Kruskl s lgorithm - MST 8 11 h 7 8 i 7 1 g (,h) nnot inludd, othrwis, yl is formd 1 d f 9 10 (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn

Kruskl s lgorithm - MST 8 11 h 7 8 i 7 1 g 1 (d,f) nnot inludd, othrwis, yl is formd d f 9 10 (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 3

Kruskl s lgorithm - MST (h,g) 1 8 7 d (i,) 9 (g,f) 11 1 (,) i 7 (,f) 8 10 (,d) 7 h 1 g f (h,i) 7 (,) 8 MST is found whn ll dgs r xmind (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn

Kruskl s lgorithm - MST Kruskl s lgorithm is grdy in th sns tht it lwys ttmpt to slt th smllst wight dg to inludd in th MST 5

Exris Find MST for this grph 10 3 6 3 10 d 6 f 5 ordr of (dgs) sltion: 6

Psudo od // Givn n undirtd onntd grph G=(V,E) T = nd E = E whil E do gin nd pik n dg in E with minimum wight if dding to T dos not form yl thn dd to T, i.., T = T { } rmov from E', i.., E = E \ { } Tim omplxity? Cn tstd y mrking vrtis 7

Dijkstr s lgorithm

Singl-sour shortst-pths Considr (un)dirtd onntd grph G Th dgs r llld y wight Givn prtiulr vrtx lld th sour Find shortst pths from th sour to ll othr vrtis (shortst pth mns th totl wight of th pth is th smllst) 9

Exmpl Dirtd Grph G (dg ll is wight) is sour vrtx 5 5 d 5 5 5 5 d thik lins: shortst pth dottd lins: not in shortst pth 30

Singl-sour shortst pths vs MST Shortst pths from 5 5 5 Wht is th diffrn twn MST nd shortst pths from? 5 5 d 5 d MST 31

Algorithms for shortst pths Algorithms thr r mny lgorithms to solv this prolm, on of thm is Dijkstr s lgorithm, whih ssums th wights of dgs r non-ngtiv 3

Id of Dijkstr s lgorithm sour hoos th dg lding to vrtx s.t. ost of pth to sour is min Mind tht th dg ddd is NOT nssrily th minimum-ost on 33

Dijkstr s lgorithm Input: A dirtd onntd wightd grph G nd sour vrtx s Output: For vry vrtx v in G, find th shortst pth from s to v Dijkstr s lgorithm runs in itrtions: in th i-th itrtion, th vrtx whih is th i-th losst to s is found, for vry rmining vrtis, th urrnt shortst pth to s found so fr (this shortst pth will updtd s th lgorithm runs) 3

Dijkstr s lgorithm Suppos vrtx is th sour, w now show how Dijkstr s lgorithm works 9 15 1 d 5 0 30 18 11 16 f 6 6 h 19 k 35

Dijkstr s lgorithm Evry vrtx v kps lls: (1) th wight of th urrnt shortst pth from ; () th vrtx lding to v on tht pth, initilly s (, -) 9 15 (, -) 1 d (, -) 5 (, -) 0 30 (, -) 18 11 16 f (, -) 6 6 (, -) h 19 k (, -) 36

(15, (, -) ) (, (9, ) -) (1, (, -) ) Dijkstr s lgorithm For vry nighor u of, updt th wight to th wight of (,u) nd th lding vrtx to. Choos from,, d th on with th smllst suh wight. hosn 9 15 nw vlus 1 d 5 0 30 ing onsidrd (, -) 18 shortst pth 11 16 f (, -) 6 6 (, -) h 19 k (, -) 37

Dijkstr s lgorithm For vry un-hosn nighor of vrtx, updt th wight nd lding vrtx. Choos from ALL un-hosn vrtis (i..,, d, h) th on with smllst wight. 9 nw vlus hosn 15 (15, ) 1 d (9, ) (1, ) 5 0 30 ing onsidrd (, -) 18 shortst pth 11 16 f (, -) 6 6 (33, (, -) ) h 19 k (, -) 38

Dijkstr s lgorithm If nw pth with smllst wight is disovrd,.g., for vrtis, h, th wight is updtd. Othrwis, lik vrtx d, no updt. Choos mong d,, h. 9 15 hosn (15, ) nw vlus 1 d (9, ) (1, ) 5 0 30 ing onsidrd 18 (, (,-) ) shortst pth 11 16 f (, -) 6 6 (33, (3, ) ) h 19 k (, -) 39

Dijkstr s lgorithm Rpt th produr. Aftr d is hosn, th wight of nd k is updtd. Choos mong, h, k. Nxt vrtx hosn is h. 9 15 (15, ) nw vlus 1 d (9, ) (1, ) 5 0 30 ing onsidrd 18 (, (35, ) d) shortst pth 11 16 hosn (3, ) f (, -) 6 6 h 19 k (59, (, -) d) 0

Dijkstr s lgorithm Aftr h is hosn, th wight of nd k is updtd gin. Choos mong, k. Nxt vrtx hosn is. (9, ) 9 (1, ) 1 30 15 5 0 (15, ) d nw vlus ing onsidrd 18 hosn (35, (3, d) h) shortst pth f 11 (, -) 16 (3, ) h 6 19 6 k (51, (59,d) h) 1

Dijkstr s lgorithm Aftr is hosn, th wight of f nd k is updtd gin. Choos mong f, k. Nxt vrtx hosn is f. (9, ) 9 (1, ) 1 30 15 5 0 (15, ) d nw vlus ing onsidrd 18 (3, h) shortst pth hosn f 11 (5, (, -) ) 16 (3, ) h 6 19 6 k (51, (50, h) )

Dijkstr s lgorithm Aftr f is hosn, it is NOT nssry to updt th wight of k. Th finl vrtx hosn is k. (9, ) 9 (1, ) 1 30 15 5 0 (15, ) d nw vlus ing onsidrd 18 (3, h) shortst pth f 11 (5, ) 16 hosn (3, ) h 6 19 6 k (50, ) 3

Dijkstr s lgorithm At this point, ll vrtis r hosn, nd th shortst pth from to vry vrtx is disovrd. (9, ) 9 (1, ) 1 30 15 5 0 (15, ) d nw vlus ing onsidrd 18 (3, h) shortst pth f 11 (5, ) 16 (3, ) h 6 19 6 k (50, )

Exris Shortst pths from (,-) 10 3 (,-) 6 3 10 d (,-) 6 f (,-) (,-) 5 ordr of (dgs) sltion: Compr th solution with slid #6 5

Dijkstr s lgorithm To dsri th lgorithm using psudo od, w giv som nottions Eh vrtx v is llld with two lls: numri ll d(v) indits th lngth of th shortst pth from th sour to v found so fr nothr ll p(v) indits nxt-to-lst vrtx on suh pth, i.., th vrtx immditly for v on tht shortst pth 6

Psudo od // Givn grph G=(V,E) nd sour vrtx s for vry vrtx v in th grph do Tim omplxity? st d(v) = nd p(v) = null st d(s) = 0 nd V T = whil V \ V T do // thr is still som vrtx lft gin hoos th vrtx u in V \ V T with minimum d(u) st V T = V T { u } for vry vrtx v in V \ V T tht is nighor of u do nd if d(u) + w(u,v) < d(v) thn // shortr pth is found st d(v) = d(u) + w(u,v) nd p(v) = u 7

Dos Grdy lgorithm lwys rturn th st solution?

Knpsk Prolm Input: Givn n itms with wights w 1, w,, w n nd vlus v 1, v,, v n, nd knpsk with pity W. Output: Find th most vlul sust of itms tht n fit into th knpsk Applition: A trnsport pln is to dlivr th most vlul st of itms to rmot lotion without xding its pity 9

Exmpl 1 pity = 50 w = 10 v = 60 w = 0 v = 100 w = 30 v = 10 itm 1 itm itm 3 knpsk totl totl sust wight vlu φ 0 0 {1} 10 60 {} 0 100 {3} 30 10 {1,} 30 160 {1,3} 0 180 {,3} 50 0 {1,,3} 60 N/A 50

Grdy pproh pity = 50 w = 10 v = 60 w = 0 v = 100 w = 30 v = 10 itm 1 itm itm 3 knpsk Grdy: pik th itm with th nxt lrgst vlu if totl wight pity. Rsult: itm 3 is tkn, totl vlu = 10, totl wight = 30 itm is tkn, totl vlu = 0, totl wight = 50 itm 1 nnot tkn Tim omplxity? Dos this lwys work? 51

Exmpl pity = 10 w = 7 v = w = 3 v = 1 w = v = 0 w = 5 v = 5 itm 1 itm itm 3 itm knpsk totl totl sust wight vlu φ 0 0 {1} 7 {} 3 1 {3} 0 {} 5 5 {1,} 10 5 {1,3} 11 N/A {1,} 1 N/A totl totl sust wight vlu {,3} 7 5 {,} 8 37 {3,} 9 65 {1,,3} 1 N/A {1,,} 15 N/A {1,3,} 16 N/A {,3,} 1 N/A {1,,3,} 19 N/A 5

Grdy pproh pity = 10 w = 7 v = w = 3 v = 1 w = v = 0 w = 5 v = 5 itm 1 itm itm 3 itm knpsk Grdy: pik th itm with th nxt lrgst vlu if totl wight pity. Rsult: itm 1 is tkn, totl vlu =, totl wight = 7 itm 3 nnot tkn itm nnot tkn itm is tkn, totl vlu = 5, totl wight = 10 not th st!! 53

Grdy pproh v/w = 6 v/w = v/w = 10 v/w = 5 pity = 10 w = 7 v = w = 3 v = 1 w = v = 0 w = 5 v = 5 itm 1 itm itm 3 itm knpsk Grdy : pik th itm with th nxt lrgst (vlu/wight) if totl wight pity. Rsult: itm 3 is tkn, totl vlu = 0, totl wight = itm 1 nnot tkn itm is tkn, totl vlu = 65, totl wight = 9 itm nnot tkn Work for Eg 1? 5

Grdy pproh v/w = 6 v/w=5 v/w = pity = 50 w = 10 v = 60 w = 0 v = 100 w = 30 v = 10 itm 1 itm itm 3 knpsk Grdy: pik th itm with th nxt lrgst (vlu/wight) if totl wight pity. Rsult: itm 1 is tkn, totl vlu = 60, totl wight = 10 itm is tkn, totl vlu = 160, totl wight = 30 itm 3 nnot tkn Not th st!! 55

Lsson Lrnd: Grdy lgorithm dos NOTlwys rturn th st solution