Download presentation
Presentation is loading. Please wait.
1
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 2 Jan Maluszynski - HT 20062.1 TDDB56 – DALGOPT-D Algorithms and optimization TDDB57 – DALG-C Data Structures and Algorithms Jan Maluszynski, IDA, 2006 http://www.ida.liu.se/~TDDB56 janma @ ida.liu.se
2
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 2 Jan Maluszynski - HT 20062.2 Analysis of Algorithms... How to? An algorithm should work for (input) data of any size. (Example TableLookUp: input size is the size of the table.) Show the resource (time/memory) used as an increasing function of input size. Focus on the worst case performance! Ignore constant factors analysis should be machine-independent; more powerful cpu speed-up by constant factors. Study scalability / asymptotic behaviour for large problem sizes: ignore lower-order terms, focus on dominating terms.
3
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 2 Jan Maluszynski - HT 20062.3 Estimating execution time for iterative programs
4
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 2 Jan Maluszynski - HT 20062.4 Analysis... How to in practice? What is the worst-case problem instance? What is the worst case time? What is the ”complexity” for this function?
5
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 2 Jan Maluszynski - HT 20062.5 Different techniques: Issue: growth rate of... Memory (stack, allocated data structures) Execution time Situation to analyze: worst case, best case, expected case, amortized: a sequence of calls of the algorithm Techniques: Algebraically, (count iterations, see last slide) Recursive algorithms (solve recurrence relations) Probabilistic analysis (figure out average case behaviour)
6
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 2 Jan Maluszynski - HT 20062.6 Example 2 of ”algebraic” analysis...
7
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 2 Jan Maluszynski - HT 20062.7 Example: Independent Nested loops...
8
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 2 Jan Maluszynski - HT 20062.8 Example: Dependent Nested Loops
9
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 2 Jan Maluszynski - HT 20062.9 Example: Dependent Nested Loops (cont.) Att ta på tavlan:...även vid krånliga indexgränser blir det något liknande... i
10
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 2 Jan Maluszynski - HT 20062.10 Analysis of Recursive Program (1)
11
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 2 Jan Maluszynski - HT 20062.11 Analysis of Recursive Programs...
12
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 2 Jan Maluszynski - HT 20062.12 Towers of Hanoi Task: Move pile of bricks to an empty pole Limitations: Smaller bricks on top Move one brick at a time
13
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 2 Jan Maluszynski - HT 20062.13 Towers of Hanoi.... As stated earlier: Formulate an equation T(1)=... T(2)=... Unroll a few times, get hypothesis for T(n)=... Prove the hypothesis!
14
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 2 Jan Maluszynski - HT 20062.14 Towers of Hanoi – run on black board! The experiment... The hypothesis: The proof: Therefore: Hanoi O ( 2 n )
15
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 2 Jan Maluszynski - HT 20062.15 Average analysis Reconsider TableSearch(): sequential search through a table Input argument: one of the table elements, assume it is chosen with equal probability for all elements. Expected search time: Time to find the element when it was in the n:th place
16
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 2 Jan Maluszynski - HT 20062.16 Amortized Analysis Example: dynamic array implementation (e.g., flexible string buffer representation) Init(): a 1 element empty array is created Append(c): Character c is appended at the end...if buffer is full: –allocate a new 2 times as big –Copy data to the new one CurrSize: 8 Limit: 8 fffubul CurrSize: 4 Limit: 8 ffub
17
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 2 Jan Maluszynski - HT 20062.17 Amortized Analysis (cont.) What is the time complexity of Append(c) ? Most cases, O(1) When we re-allocate –Copying of n data elementsO(n) –Insertion of the new elementO(1)
18
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 2 Jan Maluszynski - HT 20062.18 Amortized Analysis – charge ahead Goodrich/Tamassia page 229-230 Dynamic Vector – reallocate on overflow: –Reallocate a new vector with double size (cost: 0#) –Copy all items from old vector –Insert the item that caused the overflow Actual ”cost” to insert/copy a value: 1# Charge user 3# for each call to insertLast Start with empty vector:
19
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 2 Jan Maluszynski - HT 20062.19 Amortized Analysis – example insertLast(”A”) overflow, allocate 1, insert 1 insertLast(”B”) overflow, allocate 2, copy 1, insert 1 insertLast(”C”) overflow, allocate 4, copy 2, insert 1 insertLast(”D”) no overflow, insert 1 insertLast(”E”) overflow, allocate 8, copy 4, insert 1 insertLast(”FGH”) no overflow, insert 1 x 3 A 2# A 1# B 2# A 1# B 0# C 2#... A 1# B 0# C 2# D 2# A 1# B 0# C 0# D 0# E 2#... A 1# B 0# C 0# D 0# E 2# F 2# G 2# H 2# Start with empty vector Allocation is free, out of 3# charged, we still have 2# Use 1# to copy A. Inserting B will leave 2# Here we have used the # in B to copy ”AB”...
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.