Greedy Practice Problems

download Greedy Practice Problems

of 3

Transcript of Greedy Practice Problems

  • 7/22/2019 Greedy Practice Problems

    1/3

    C S 4 4 1 T 5 3 9 T : A d v a n c e d A l g o r i t h m s F a l l S e m e s t e r , 2 0 0 1

    P r a c t i c e P r o b l e m s o n G r e e d y A l g o r i t h m s

    S e p t e m b e r 4 , 2 0 0 1

    B e l o w a r e a s e t o f t h r e e p r a c t i c e p r o b l e m s o n d e s i g n i n g a n d p r o v i n g t h e c o r r e c t n e s s o f

    g r e e d y a l g o r i t h m s . F o r t h o s e o f y o u w h o f e e l l i k e y o u n e e d u s t o g u i d e y o u t h r o u g h s o m e

    a d d i t i o n a l p r o b l e m s t h a t y o u r s t t r y t o s o l v e o n y o u r o w n , t h e s e p r o b l e m s w i l l s e r v e t h a t

    p u r p o s e . I f a n y o n e w o u l d l i k e a h e l p s e s s i o n w h e r e I g u i d e y o u t h r o u g h t h e p r o c e s s o f s o l v i n g

    t h e s e p r o b l e m s , p l e a s e l e t m e k n o w .

    T h e f r o n t p a g e h a s t h e p r o b l e m s a n d t h e r e s t g i v e s t h e s o l u t i o n s .

    Y o u c a n u s e t h e s e s o l u t i o n s a s a g u i d e a s t o h o w y o u s h o u l d w r i t e - u p

    y o u r s o l u t i o n s . T h e s e p r o b l e m s w i l l b e M U C H m o r e v a l u a b l e t o y o u

    i f y o u r s t s o l v e t h e m a n d t h e n c h e c k t h e s o l u t i o n s .

    P r a c t i c e P r o b l e m s

    1 . G i v e n a s e t f x

    1

    x

    2

    x

    n

    g o f p o i n t s o n t h e r e a l l i n e , d e t e r m i n e t h e s m a l l e s t

    s e t o f u n i t - l e n g t h c l o s e d i n t e r v a l s e . g . t h e i n t e r v a l 1 . 2 5 , 2 . 2 5 i n c l u d e s a l l x

    i

    s u c h t h a t

    1 2 5 x

    i

    2 2 5 t h a t c o n t a i n s a l l o f t h e p o i n t s .

    G i v e t h e m o s t e c i e n t a l g o r i t h m y o u c a n t o s o l v e t h i s p r o b l e m , p r o v e i t i s c o r r e c t a n d

    a n a l y z e t h e t i m e c o m p l e x i t y .

    2 . S u p p o s e y o u w e r e t o d r i v e f r o m S t . L o u i s t o D e n v e r a l o n g I - 7 0 . Y o u r g a s t a n k , w h e n

    f u l l , h o l d s e n o u g h g a s t o t r a v e l m m i l e s , a n d y o u h a v e a m a p t h a t g i v e s d i s t a n c e s

    b e t w e e n g a s s t a t i o n s a l o n g t h e r o u t e . L e t d

    1

    d

    2

    d

    n

    b e t h e l o c a t i o n s o f a l l

    t h e g a s s t a t i o n s a l o n g t h e r o u t e w h e r e d

    i

    i s t h e d i s t a n c e f r o m S t . L o u i s t o t h e g a s

    s t a t i o n . Y o u c a n a s s u m e t h a t t h e d i s t a n c e b e t w e e n n e i g h b o r i n g g a s s t a t i o n s i s a t m o s t

    m m i l e s .

    Y o u r g o a l i s t o m a k e a s f e w g a s s t o p s a s p o s s i b l e a l o n g t h e w a y . G i v e t h e m o s t e c i e n t

    a l g o r i t h m y o u c a n n d t o d e t e r m i n e a t w h i c h g a s s t a t i o n s y o u s h o u l d s t o p a n d p r o v e

    t h a t y o u r s t r a t e g y y i e l d s a n o p t i m a l s o l u t i o n . B e s u r e t o g i v e t h e t i m e c o m p l e x i t y o f

    y o u r a l g o r i t h m a s a f u n c t i o n o f n

    3 . Y o u a r e g i v e n n e v e n t s w h e r e e a c h t a k e s o n e u n i t o f t i m e . E v e n t i w i l l p r o v i d e a p r o t

    o f g

    i

    d o l l a r s g

    i

    0 i f s t a r t e d a t o r b e f o r e t i m e t

    i

    w h e r e t

    i

    i s a n a r b i t r a r y r e a l n u m b e r .

    N o t e : I f a n e v e n t i s n o t s t a r t e d b y t

    i

    t h e n t h e r e i s n o b e n e t i n s c h e d u l i n g i t a t a l l .

    A l l e v e n t s c a n s t a r t a s e a r l y a s t i m e 0 .

    G i v e n t h e m o s t e c i e n t a l g o r i t h m y o u c a n t o n d a s c h e d u l e t h a t m a x i m i z e s t h e p r o t .

    1

  • 7/22/2019 Greedy Practice Problems

    2/3

    S o l u t i o n s s o l v e t h e p r o b l e m s b e f o r e r e a d i n g t h i s

    1 . T h e g r e e d y a l g o r i t h m w e u s e i s t o p l a c e t h e r s t i n t e r v a l a t x

    1

    ; x

    1

    + 1 , r e m o v e a l l

    p o i n t s i n x

    1

    ; x

    1

    + 1 a n d t h e n r e p e a t t h i s p r o c e s s o n t h e r e m a i n i n g p o i n t s .

    C l e a r l y t h e a b o v e i s a n O n a l g o r i t h m . W e n o w p r o v e i t i s c o r r e c t .

    G r e e d y C h o i c e P r o p e r t y : L e t S b e a n o p t i m a l s o l u t i o n . S u p p o s e S p l a c e s i t s

    l e f t m o s t i n t e r v a l a t x ; x + 1 . B y d e n i t i o n o f o u r g r e e d y c h o i c e x x

    1

    s i n c e i t p u t s

    t h e r s t p o i n t a s f a r r i g h t a s p o s s i b l e w h i l e s t i l l c o v e r i n g x

    1

    . L e t S

    0

    b e t h e s c h e d u l e d

    o b t a i n e d b y s t a r t i n g w i t h S a n d r e p l a c i n g x ; x + 1 b y x

    1

    ; x

    1

    + 1 . W e n o w a r g u e

    t h a t a l l p o i n t s c o n t a i n e d i n x ; x + 1 a r e c o v e r e d b y x

    1

    ; x

    1

    + 1 . T h e r e g i o n c o v e r e d

    b y x ; x + 1 w h i c h i s n o t c o v e r e d b y x

    1

    ; x

    1

    + 1 i s x ; x

    1

    w h i c h i s t h e p o i n t s f r o m x

    u p u n t i l x

    1

    b u t n o t i n c l u d i n g x

    1

    . H o w e v e r , s i n c e x

    1

    i s t h e l e f t m o s t p o i n t t h e r e a r e

    n o p o i n t s i n t h i s r e g i o n . T h e r e c o u l d b e a d d i t i o n a l p o i n t s c o v e r e d b y x + 1 ; x

    1

    + 1

    t h a t a r e n o t c o v e r e d i n x ; x + 1 b u t t h a t d o e s n o t a e c t t h e v a l i d i t y o f S

    0

    . H e n c e

    S

    0

    i s a v a l i d s o l u t i o n w i t h t h e s a m e n u m b e r o f p o i n t s a s S a n d h e n c e S

    0

    i s a n o p t i m a l

    s o l u t i o n .

    O p t i m a l S u b s t r u c t u r e P r o p e r t y : L e t P b e t h e o r i g i n a l p r o b l e m w i t h a n o p t i m a l

    s o l u t i o n S . A f t e r i n c l u d i n g t h e i n t e r v a l x

    1

    ; x

    1

    + 1 , t h e s u b p r o b l e m P

    0

    i s t o n d a n

    s o l u t i o n f o r c o v e r i n g t h e p o i n t s t o t h e r i g h t o f x

    1

    + 1 . L e t S

    0

    b e a n o p t i m a l s o l u t i o n

    t o P

    0

    . S i n c e , c o s t S = c o s t S

    0

    + 1 , c l e a r l y a n o p t i m a l s o l u t i o n t o P i n c l u d e s w i t h i n

    i t a n o p t i m a l s o l u t i o n t o P

    0

    2 . T h e g r e e d y a l g o r i t h m w e u s e i s t o g o a s f a r a s p o s s i b l e b e f o r e s t o p p i n g f o r g a s . L e t c

    i

    b e t h e c i t y w i t h d i s t a n c e d

    i

    f r o m S t . L o u i s . H e r e i s t h e p s e u d o - c o d e .

    S = ;

    l a s t = 0

    f o r i = 1 t o n

    i f d

    i

    , l a s t m

    S = S f c

    i , 1

    g

    l a s t = t

    i , 1

    C l e a r l y t h e a b o v e i s a n O n a l g o r i t h m . W e n o w p r o v e i t i s c o r r e c t .

    G r e e d y C h o i c e P r o p e r t y : L e t S b e a n o p t i m a l s o l u t i o n . S u p p o s e t h a t i t s s e q u e n c e

    o f s t o p s i s s

    1

    ; s

    2

    ; : : : ; s

    k

    w h e r e s

    i

    i s t h e s t o p c o r r e s p o n d i n g t o d i s t a n c e t

    i

    . S u p p o s e t h a t

    g i s t h e r s t s t o p m a d e b y t h e a b o v e g r e e d y a l g o r i t h m . W e n o w s h o w t h a t t h e r e i s

    a n o p t i m a l s o l u t i o n w i t h a r s t s t o p a t g I f s

    1

    = g t h e n S i s s u c h a s o l u t i o n . N o w

    s u p p o s e t h a t s

    1

    6= g . S i n c e t h e g r e e d y a l g o r i t h m s t o p s a t t h e l a t e s t p o s s i b l e c i t y t h e n

    i t f o l l o w s t h a t s

    1

    i s b e f o r e g . W e n o w a r g u e t h a t S

    0

    = h g ; s

    2

    ; s

    3

    ; : : : ; s

    k

    i i s a n o p t i m a l

    s o l u t i o n . F i r s t n o t e t h a t S

    0

    = S . S e c o n d , w e a r g u e t h a t S

    0

    i s l e g a l i . e . y o u n e v e r

    r u n o u t o f g a s . B y d e n i t i o n o f t h e g r e e d y c h o i c e y o u c a n r e a c h g . F i n a l l y , s i n c e S

    i s o p t i m a l a n d t h e d i s t a n c e b e t w e e n g a n d s

    2

    i s n o m o r e t h a n t h e d i s t a n c e b e t w e e n s

    1

    a n d s

    2

    , t h e r e i s e n o u g h g a s t o g e t f r o m g t o s

    2

    . T h e r e s t o f S

    0

    i s l i k e S a n d t h u s l e g a l .

    O p t i m a l S u b s t r u c t u r e P r o p e r t y : L e t P b e t h e o r i g i n a l p r o b l e m w i t h a n o p t i m a l

    s o l u t i o n S . T h e n a f t e r s t o p p i n g a t t h e s t a t i o n g a t d i s t a n c e d

    i

    t h e s u b p r o b l e m P

    0

    t h a t

    r e m a i n s i s g i v e n b y d

    i + 1

    ; : : : ; d

    n

    i . e . y o u s t a r t a t t h e c u r r e n t c i t y i n s t e a d o f S t . L o u i s .

    2

  • 7/22/2019 Greedy Practice Problems

    3/3

    L e t S

    0

    b e a n o p t i m a l s o l u t i o n t o P

    0

    . S i n c e , c o s t S = c o s t S

    0

    + 1 , c l e a r l y a n o p t i m a l

    s o l u t i o n t o P i n c l u d e s w i t h i n i t a n o p t i m a l s o l u t i o n t o P

    0

    3 . W e r s t a r g u e t h a t t h e r e a l w a y s e x i s t s a n o p t i m a l s o l u t i o n i n w h i c h a l l o f t h e e v e n t s

    s t a r t a t i n t e g r a l t i m e s . T a k e a n o p t i m a l s o l u t i o n S | y o u c a n a l w a y s h a v e t h e r s t

    j o b i n S s t a r t a t t i m e 0 , t h e s e c o n d s t a r t a t t i m e 1 , a n d s o o n . H e n c e , i n a n y o p t i m a l

    s o l u t i o n , e v e n t i w i l l s t a r t a t o r b e f o r e t i m e b t

    i

    c

    T h i s o b s e r v a t i o n l e a d s t o t h e f o l l o w i n g g r e e d y a l g o r i t h m . F i r s t , w e s o r t t h e j o b s a c -

    c o r d i n g t o b t

    i

    c s o r t e d f r o m l a r g e s t t o s m a l l e s t . L e t t i m e t b e t h e c u r r e n t t i m e b e i n g

    c o n s i d e r e d w h e r e i n i t i a l l y t = b t

    1

    c . A l l j o b s i w h e r e b t

    i

    c = t a r e i n s e r t e d i n t o a

    p r i o r i t y q u e u e w i t h t h e p r o t g

    i

    u s e d a s t h e k e y . A n e x t r a c t M a x i s p e r f o r m e d t o s e l e c t

    t h e j o b t o r u n a t t i m e t . T h e n t i s d e c r e m e n t e d a n d t h e p r o c e s s i s c o n t i n u e d . C l e a r l y

    t h e t i m e c o m p l e x i t y i s O n l o g n . T h e s o r t t a k e s O n l o g n a n d t h e r e a r e a t m o s t n

    i n s e r t a n d e x t r a c t M a x o p e r a t i o n s p e r f o r m e d o n t h e p r i o r i t y q u e u e , e a c h w h i c h t a k e s

    O l o g n t i m e .

    W e n o w p r o v e t h a t t h i s a l g o r i t h m i s c o r r e c t b y s h o w i n g t h a t t h e g r e e d y c h o i c e a n d

    o p t i m a l s u b s t r u c t u r e p r o p e r t i e s h o l d .

    G r e e d y C h o i c e P r o p e r t y : C o n s i d e r a n o p t i m a l s o l u t i o n S i n w h i c h x + 1 e v e n t s

    a r e s c h e d u l e d a t t i m e s 0 , 1 , . . . , x . L e t e v e n t k b e t h e l a s t j o b r u n i n S . T h e g r e e d y

    s c h e d u l e w i l l r u n e v e n t 1 l a s t a t t i m e b t

    1

    c . F r o m t h e g r e e d y c h o i c e p r o p e r t y w e k n o w

    t h a t b t

    1

    c b t

    k

    c . W e c o n s i d e r t h e f o l l o w i n g c a s e s :

    C a s e 1 : b t

    1

    c = b t

    k

    c . B y o u r g r e e d y c h o i c e , w e k n o w t h a t g

    1

    g

    k

    . I f e v e n t 1 i s n o t

    i n S t h e n w e c a n j u s t r e p l a c e e v e n t k b y e v e n t 1 . T h e r e s u l t i n g s o l u t i o n S

    0

    i s a t

    l e a s t a s g o o d a s S s i n c e g

    1

    g

    k

    . T h e o t h e r p o s s i b i l i t y i s t h a t e v e n t 1 i s i n S a t

    a n e a r l i e r t i m e . S i n c e b t

    1

    c = b t

    k

    c , w e c a n s w i t c h t h e t i m e s i n w h i c h t h e y r u n t o

    c r e a t e a s c h e d u l e S

    0

    w h i c h h a s t h e s a m e p r o t a s S a n d i s h e n c e o p t i m a l .

    C a s e 2 : b t

    k

    c b t

    1

    c . I n t h i s c a s e , S d o e s n o t r u n a n y e v e n t a t t i m e b t

    1

    c s i n c e j o b k

    w a s i t s l a s t j o b . I f e v e n t 1 i s n o t i n S , t h e w e c o u l d a d d i t t o S c o n t r a d i c t i n g

    t h e o p t i m a l i t y o f S . I f e v e n t 1 i s i n S w e c a n r u n i t i n s t e a d a t t i m e b t

    1

    c c r e a t i n g

    a s c h e d u l e S

    0

    t h a t m a k e s t h e g r e e d y c h o i c e a n d h a s t h e s a m e p r o t a s S a n d i s

    h e n c e a l s o o p t i m a l .

    O p t i m a l S u b s t r u c t u r e P r o p e r t y : L e t P b e t h e o r i g i n a l p r o b l e m o f s c h e d u l i n g

    e v e n t s 1 ; : : : ; n w i t h a n o p t i m a l s o l u t i o n S . G i v e n t h a t e v e n t 1 i s s c h e d u l e d r s t w e

    a r e l e f t w i t h t h e s u b p r o b l e m P

    0

    o f s c h e d u l i n g e v e n t s 2 ; : : : ; n . L e t S

    0

    b e a n o p t i m a l

    s o l u t i o n t o P

    0

    . C l e a r l y p r o t S = p r o t S

    0

    + g

    1

    a n d h e n c e a n o p t i m a l s o l u t i o n f o r P

    i n c l u d e s w i t h i n i t a n o p t i m a l s o l u t i o n t o P

    0

    3