• No results found

COMP50001: Algorithm Design & Analysis

N/A
N/A
Protected

Academic year: 2022

Share "COMP50001: Algorithm Design & Analysis"

Copied!
9
0
0

Loading.... (view fulltext now)

Full text

(1)

COMP50001: Algorithm Design & Analysis

Sheet 4 (Week 5)

Exercise 4.1

dataDeque a = Deque [a] [a]

instanceList Deque where toList :: Deque a ! [a]

toList (Deque xs sy) = xs ++ reverse sy fromList xs = Deque ys (reverse zs)

where (ys, zs) = splitAt (length xs ‘div‘ 2) xs tail :: Deque a ! Deque a

tail (Deque [ ] [ ]) =error"tail: empty list"

tail (Deque [ ] sy) = empty

tail (Deque [x] sy) = fromList (reverse sy) tail (Deque (x : xs) sy) = Deque xs sy cons :: a ! Deque a ! Deque a cons x (Deque xs [ ]) = Deque [x] xs cons x (Deque xs sy) = Deque (x : xs) sy snoc :: Deque a ! a ! Deque a snoc (Deque [ ] sv) x = Deque sv [x]

snoc (Deque us sv) x = Deque us (x : sv)

1. Give the time complexity of the following reverse in terms of the length of a deque:

reverse :: Deque a!Deque a reverse=fromList reverse toList

2. Implement a reverse for deques that runs in O(1)time.

Exercise 4.2

Supposing xs0:: Deque a is the empty deque, show that the amor- tised complexity of each operation in the following sequence is O(1):

xs0op0 xs1op1

xs2op2

. . .opn 1 xsn

where each opi 2 {tail, snoc, cons}.

Exercise 4.3

snoc0:: Deque a ! a ! Deque a snoc0(Deque us sv) v = Deque us (v : sv) tail0:: Deque a ! Deque a

tail0(Deque [ ] [ ]) = error"tail: empty list"

tail0(Deque [ ] sv) = Deque (tail (reverse sv)) [ ] tail0(Deque us sv) = Deque (tail us) sv init0:: Deque a ! Deque a

init0(Deque [ ] [ ]) = error"init: empty list"

init0(Deque us [ ]) = Deque [ ] (tail (reverse us)) init0(Deque us sv) = Deque us (tail sv)

Suppose no invariants are imposed on Deque and snoc and tail are alternatively defined as snoc0 and tail0.

1. Prove that the amortised complexity of each operation in a se- quence of snoc0 and tail0 is still O(1).

2. Suppose that the sequence additionally contains operation init0. Determine whether the amortised complexity is still O(1).

Exercise 4.4

Consider the following alternative representation of Deque:

dataDeque0 a=Deque0Int[a] [a]

with an invariant n = length us+length sv for any Deque0 n us sv.

Define cons and++for this representation as follows:

instanceList Deque0where toList :: Deque0a ! [a]

toList (Deque0n us sv) = us ++ reverse sv fromList :: [a] ! Deque0a

fromList xs = Deque0n us sv wheren =length xs

(us, vs) = splitAt (n ‘div‘ 2) xs sv =reverse vs

cons :: a!Deque0 a!Deque0 a

cons u(Deque0n us sv) =Deque0 (n+1) (u : us)sv (++):: Deque0 a!Deque0 a!Deque0 a

Deque0n us sv++Deque0 n0 us0 sv0

|n<n0 =Deque0(n+n0) (us++reverse sv++us0)sv0

|otherwise=Deque0(n+n0)us(sv0++reverse us0++sv)

1. Give the worst-case complexity of xs++ys in terms of length xs and length ys.

(2)

comp50001: algorithm design & analysis 2

2. Consider a sequence of operations D0op0

D1op1 D2op2

. . . Dn 1opn 1 Dn

where each Diis a multiset of deques and D0 = ∆. Each opi is only one of the following forms:

(a) xsi=empty, and in this case

Di+1=Di[ {xsi},

(b) xsi =cons x xsj where xsj 2Di, and in this case Di+1= (Di\ {xsj}) [ {xsi},

(c) xsi=xsj++xsk where xsj, xsk2Diand i6=j, and in this case Di+1= (Di\ {xsj, xsk}) [ {xsi}.

Prove that each operation has amortised complexity O(log2n) for sufficiently large n where n is the length of the sequence of operations.

Exercise 4.5

typeBinary = [Digit]

dataDigit = O | I inc :: Binary ! Binary inc [ ] = [I]

inc (O : bs) = I : bs inc (I : bs) = O : (inc bs)

Define dec :: Binary ! Binary that decrements a binary number discussed in the lecture and show that the amortised complexity of repeated applications of dec is O(1). Determine if the amortised complexity of each operation in a sequence opi where 06i< n and opi 2 {inc, dec}is still O(1).

Exercise 4.6

dataTree a = Tip

| Leaf a

| Node Int (Tree a) (Tree a)

size (Tip) =0

size (Leaf ) =1 size (Node n ) =n

Given is the data type Tree which is an instance of List. The tree t :: Tree a is balanced iff t = Tip, t = Leaf a, or t = Node n l r with balanced subtrees l and r such that size l=size r.

Show that(!!):: Tree a ! Int ! a takes O(log2n)time for balanced binary trees using a recurrence relation, where n is the number of elements in the tree.

Exercise 4.7

Consider the definition of RAList a.

newtypeRAList a = RAList [Tree a]

instanceList RAList where fromList :: [a] ! RAList a fromList xs = foldr cons empty xs toList :: RAList a ! [a]

toList (RAList ts) = (concat map toList) ts (!!) :: RAList a ! Int ! a

RAList (t : ts) !! k

| k < size t = t !! k

| otherwise = RAList ts !! (k size t)

1. Letting head = (!!0), give the best-case and worst-case time complexities of head xs where xs :: RAList a.

2. Implement tail :: RAList a ! RAList a such that the amortised complexity of a sequence of tail is O(1).

3. Determine if the amortised complexity of a sequence of oper- ations where each operation is either tail or cons is still O(1). (Hint: compare with inc and dec for binary numbers.)

multi bet bag

sit

list C T

bag 2 S

(3)

Ts Tes Tu

Xs De que us su where

us 4 reverse su

I

II.tn Xh i Xezi

ykntreven segue us su Degas Sr us

Cop Isi A op xs SCxso sexists

(4)

I s Segue us su lengths r

cons Xi on Xu Leinitfinit

dec Binay Binay

doc IT

dec Iihs O bs

dee CO bs I dec be C duc lbs t t t

where t length take while 6 0 bs

Adee Cbs 2

(5)

S Cbs b

where

b length filter 6 0 bs

de 0,0 09 0 I 0,90 0,0 I

d

II I I I I O 00 0 0 0 I

ColeeChs E Adee lbs t sCbs S Cbs

t t t t 2 t b b

wheel

b b t t t

1

(6)

TCO 1

n t 1 t T f

a t t t t t t

K TIE

I 1

n E 2k

log n E k

I

(7)

g

Tcu Joynt T E

E

Tn Toga E Ollogu

Nothing

do 1 x Nowt AM 1 7

HAHAHA

worst case

nothing

n

Nothing

(8)

dec Binay Binay

doc IT

dec I b s O bs

die CO bs I dec be

a 2 4

o o I 4

I É 2

tail RA List a RA List a

tail

whir

split RAL it as CTnee a RALista

gout RAGA ET Ct RALit CD

split CRAW Just t its

Ct Racist Noth ts

(9)

split IRA List Nothing A

It Racist Hits

where

Node t t RA Litts

Split CRA Litts

tin

Coons x x sj D 1

i

T.is sso

References

Related documents

In addition, the case for greater international labour mobility could be much better articulated – partly by highlighting the existing evidence on the benefits for both receiving

If the Paper Thickness Lever setting does not match the paper being used, paper feeding and printing may not work properly..

1 Spatial planning of public charging points using multi-dimensional analysis of early adopters of electric vehicles for a city region..

July 2010 research stage, the fourth coder (of Russian and Kyrgyz language print media) completed coding. The content coding is now ready for comparative analysis. The case

When a multi-active seller offers price p M , then the uncertainty about the buyer leads in cultural terms to differences in the valuation of the bargaining process with the

High-frequency sampling campaigns and analyses were conducted in this study to investigate temporal variations of river water chemistry and the impacts of climate variability on CO

I therefor expect the public servants in Anglo-Saxon and Nordic countries to show higher levels of openness to experience, and lower levels of conscientiousness than in Germanic

Structural Analysis Online Quiz Questions Surveying Online Quiz Questions Surveying Problems Quiz questions Switchgear Protections Online Quiz