CPSC 320: Intermediate Algorithm Design and Analysis August 6, 2014 - - PowerPoint PPT Presentation

β–Ά
cpsc 320 intermediate algorithm
SMART_READER_LITE
LIVE PREVIEW

CPSC 320: Intermediate Algorithm Design and Analysis August 6, 2014 - - PowerPoint PPT Presentation

CPSC 320: Intermediate Algorithm Design and Analysis August 6, 2014 1 Schedule Monday: BC Day, no classes Wednesday: Quiz 5 (NP and NP completeness) Review: Amortized Analysis Friday: Survey (bonus marks)


slide-1
SLIDE 1

1

CPSC 320: Intermediate Algorithm Design and Analysis

August 6, 2014

slide-2
SLIDE 2

2

Schedule

  • Monday: BC Day, no classes
  • Wednesday:
  • Quiz 5 (NP and NP completeness)
  • Review: Amortized Analysis
  • Friday:
  • Survey (bonus marks)
  • Review: probably Graph Theory and Dynamic Programming
slide-3
SLIDE 3

3

Amortized Analysis

slide-4
SLIDE 4

4

Amortized cost

  • Although the worst case for one call has a large bound, we are interested in the

total cost of a sequence of calls

  • In other words, 𝑙 calls to a 𝑃 𝑔 π‘œ

function may be better than 𝑃 𝑙 β‹… 𝑔 π‘œ

  • The amortized cost of the function takes the total amount in consideration
slide-5
SLIDE 5

5

Aggregate Method

  • Evaluate a sequence of calls, and calculate the total cost
  • Usually changes in data structure are considered instead of operations
slide-6
SLIDE 6

6

Potential Method

  • Each operation receives a potential cost, which if larger than actual cost can be

β€œsaved” towards future operations

  • Operations that take longer can use β€œsaved” value
  • Total cost of sequence of operations is the sum of potential costs
  • Properties:
  • Assume 𝐸𝑗 is state of algorithm after 𝑗 iterations
  • Ξ¦ 𝐸𝑗 β‰₯ 0 (you cannot use more than what you saved)
  • Ξ¦ 𝐸0 = 0 (you start with no saved cost – usually)
  • 𝑑𝑝𝑑𝑒𝑏𝑛 π‘π‘žπ‘— = Ξ¦ 𝐸𝑗 βˆ’ Ξ¦ πΈπ‘—βˆ’1 + π‘‘π‘π‘‘π‘’π‘ π‘“π‘π‘š(π‘π‘žπ‘—)
  • Alternative notation:
  • 𝑑𝑗 for real cost,

𝑑𝑗 for amortized cost

slide-7
SLIDE 7

7

Garbage Collection

  • Assume a memory management process where:
  • allocating memory takes a constant time 𝑏 in general case
  • there is no freeing process, but memory reference is taken into account
  • if there is no memory left (or memory runs over a limit), unreferenced

memory space is freed

  • each freed element takes constant time 𝑔
  • For simplicity, assume there is trivial way to retrieve unreferenced memory space
slide-8
SLIDE 8

8

Garbage Collection

  • Real cost of allocate:
  • If no garbage collection is needed: 𝑏
  • If garbage collection is needed: 𝑏 + 𝑔𝑠 (𝑠 is number of freeable objects)
  • Amortized cost of allocate:
  • Assuming initially no elements are allocated
  • Each freed element in garbage collection requires a previous allocation, and

every allocation has a single free

  • Sequence of 𝑙 calls to allocate costs less than 𝑏𝑙 + 𝑔𝑙
  • Each allocate can be assumed to have amortized cost 𝑏 + 𝑔 ∈ Θ(1)
slide-9
SLIDE 9

9

Garbage Collection – Potential Method

  • What information in the data structure determines an expensive operation?
  • Allocate is costly if number of elements is large
  • In worst-case, allocate takes time linear to size of memory
  • Potential function: number of allocated words Γ— 𝑔
  • If no GC is needed: 𝑑𝑝𝑑𝑒𝑏𝑛 π‘π‘šπ‘šπ‘π‘‘ = π‘‘π‘π‘‘π‘’π‘ π‘“π‘π‘š π‘π‘šπ‘šπ‘π‘‘ + Ξ” Ξ¦ = 𝑏 + 𝑔
  • If GC is needed: 𝑑𝑝𝑑𝑒𝑏𝑛 π‘π‘šπ‘šπ‘π‘‘ = π‘‘π‘π‘‘π‘’π‘ π‘“π‘π‘š π‘π‘šπ‘šπ‘π‘‘ + Ξ” Ξ¦ = 𝑏 + 𝑔𝑠 + 𝑔 βˆ’ 𝑔𝑠 = 𝑏 + 𝑔
slide-10
SLIDE 10

10

More examples

  • Consider an operation that, when called the π‘œth time, costs π‘œ if π‘œ is a power of 2,

and costs 1 otherwise

  • Aggregate analysis:
  • A sequence of π‘œ operations will cost 1 every time except lg π‘œ + 1 times

(20, 21, … 2 lg π‘œ )

  • π‘œ operations will cost:

𝐷 ≀ π‘œ +

𝑗=0 lg π‘œ

2𝑗 ≀ π‘œ + 2lg π‘œ+1 βˆ’ 1 2 βˆ’ 1 = π‘œ + 2π‘œ βˆ’ 1 ≀ 3π‘œ ∈ 𝑃 π‘œ

  • Each operation has an amortized constant cost
slide-11
SLIDE 11

11

More examples

  • Consider an operation that, when called the π‘œth time, costs π‘œ2 if π‘œ is a power of 2,

and costs 1 otherwise

  • Aggregate analysis:
  • A sequence of π‘œ operations will cost 1 every time except lg π‘œ + 1 times

(20, 21, … 2 lg π‘œ )

  • π‘œ operations will cost:

𝐷 ≀ π‘œ +

𝑗=0 lg π‘œ

22𝑗 ≀ π‘œ + 4lg π‘œ+1 βˆ’ 1 4 βˆ’ 1 = π‘œ + 4π‘œ2 βˆ’ 1 3 ∈ 𝑃 π‘œ2

  • Each operation has an amortized linear cost
slide-12
SLIDE 12

12

Exercise

Georgia Street has many tall buildings, but only some of them have a clear view of Stanley Park. Suppose we are given an array 𝐡 1. . π‘œ that stores the height of π‘œ buildings on a city block, indexed from South to North. Building 𝑗 has a good view of Stanley Park if and only if every building to the North of 𝑗 is shorter than 𝑗. Here is an algorithm that computes which buildings have a good view of Stanley

  • Park. What is the running time of this algorithm?

Algorithm Goodview(A[1 .. n]): Initialize a stack S For i ← 1 to n Do While (S not empty and A[i] > A[Top(S)]) Do Pop(S) Push(S, i) Return S

slide-13
SLIDE 13

13

Good view – Aggregate Analysis

  • Consider each different building
  • How many times is it pushed?
  • How many times is it popped?
  • How many times is Top called?
  • For each building:
  • Push is called exactly once
  • Pop is called no more than once
  • Top is called once for every pop and once for every push
  • So, potentially, each building triggers up to 4 queue operations
  • Since there are π‘œ buildings, this algorithm takes 𝑃(π‘œ) total time
slide-14
SLIDE 14

14

Good view – Potential Method

  • Potential method corresponds to twice the size of the stack
  • Push:

𝑑𝑝𝑑𝑒𝑏𝑛 π‘žπ‘£π‘‘β„Ž = π‘‘π‘π‘‘π‘’π‘ π‘“π‘π‘š π‘žπ‘£π‘‘β„Ž + Ξ” Ξ¦ = 1 + 2 = 3

  • Pop:

𝑑𝑝𝑑𝑒𝑏𝑛 π‘žπ‘π‘ž = π‘‘π‘π‘‘π‘’π‘ π‘“π‘π‘š π‘žπ‘π‘ž + Ξ” Ξ¦ = 1 βˆ’ 2 = βˆ’1

  • Top:

𝑑𝑝𝑑𝑒𝑏𝑛 π‘’π‘π‘ž = π‘‘π‘π‘‘π‘’π‘ π‘“π‘π‘š π‘’π‘π‘ž + Ξ” Ξ¦ = 1 + 0 = 1

  • In each iteration of the for loop, if there are 𝑙 calls to Pop, there are 𝑙 + 1 calls to Top
  • Cost for each iteration: 𝑙 β‹… βˆ’1 + 𝑙 + 1 β‹… 1 + 3 = 4
  • Since the loop iterates π‘œ times, we have a total cost of 4π‘œ ∈ 𝑃 π‘œ