AA - Unit 1 - Part-I
AA - Unit 1 - Part-I
AA - Unit 1 - Part-I
(PCCO6020T)
Unit 1
Types of algorithms:
The running time of the algorithm is how long it takes the computer to
execute the lines of code to completion, usually measured in milliseconds or
seconds.
Using this method is not the most efficient way of calculating the running
time of an algorithm, cause the running time of an algorithm depends on
The compiler that translates our code into machine code (Clang, GCC, Min
GW).
Consider a model machine
Assigning values to variables.
Making comparisons.
Executing arithmetic operations.
Accessing objects from memory.
Time Complexity:
In the above code “Hello World” is printed only once on the screen.
So, the time complexity is constant: O(1)
1) Loop
16
Amortized Analysis
17
Note that this amortized cost applies to each operation, even when there is
several types of operations in sequence.
How large should a hash
table be?
Goal: Make the table as small as possible, but
large enough so that it won’t overflow (or
otherwise become inefficient).
Problem: What if we don’t know the proper size
in advance?
Solution: Dynamic tables.
IDEA: Whenever the table overflows, “grow” it
by allocating (via malloc or new) a new, larger
table. Move all items from the old table into the
new one, and free the storage for the old table.
Example of a dynamic table
1. INSERT 1
2. INSERT overflow
Example of a dynamic table
1. INSERT 1
2. INSERT overflow
Example of a dynamic table
1. INSERT 1
2. INSERT 2
Example of a dynamic table
1. INSERT 1
2. INSERT 2
3. INSERT overflow
Example of a dynamic table
1. INSERT 1
2. INSERT 2
3. INSERT overflow
Example of a dynamic table
1. INSERT 1
2. INSERT 2
3. INSERT
Example of a dynamic table
1. INSERT 1
2. INSERT 2
3. INSERT 3
4. INSERT 4
Example of a dynamic table
1. INSERT 1
2. INSERT 2
3. INSERT 3
4. INSERT 4
5. INSERT overflow
Example of a dynamic table
1. INSERT 1
2. INSERT 2
3. INSERT 3
4. INSERT 4
5. INSERT overflow
Example of a dynamic table
1. INSERT 1
2. INSERT 2
3. INSERT 3
4. INSERT 4
5. INSERT
Example of a dynamic table
1. INSERT 1
2. INSERT 2
3. INSERT 3
4. INSERT 4
5. INSERT 5
6. INSERT 6
7. INSERT 7
Worst-case analysis
i 1 2 3 4 5 6 7 8 9 10
sizei 1 2 4 4 8 8 8 8 16 16
ci 1 2 3 1 5 1 1 1 9 1
Tighter analysis
i 1 2 3 4 5 6 7 8 9 10
sizei 1 2 4 4 8 8 8 8 16 16
1 1 1 1 1 1 1 1 1 1
ci
1 2 4 8
Tighter analysis (continued)
n
Cost of n insertions c i
i1
lg(n1)
n 2j
j0
3n
(n).
Thus, the average cost of each dynamic-table
operation is (n)/n = (1).
Example for amortized analysis
• Amortized analysis can be used to show that the average cost of an operation is
small , if one averages over a sequence of operations ,even though a single
operation within the sequence might be expensive
• Stack operations:
– PUSH(S,x), O(1)
– POP(S), O(1)
– MULTIPOP(S,k), min(s,k)
•while not STACK-EMPTY(S) and k>0
• do POP(S)
• k=k-1
• Let us consider a sequence of n PUSH, POP, MULTIPOP.
– The worst case cost for MULTIPOP in the sequence is O(n), since the stack
size is at most n.
– thus the cost of the sequence is O(n2). Correct, but not tight.
Aggregate Analysis
• In fact, a sequence of n operations on an
initially empty stack cost at most O(n). Why?
Each object can be POP only once (including in MULTIPOP) for each time
it is PUSHed. #POPs is at most #PUSHs, which is at most n.
log 𝑛 ∞
Thus total #flips is 𝑛
< 𝑛
2𝑖 2𝑖
𝑖=0 𝑖=0
=2n.
Accounting Method
In accounting method, we assign different charges to
different operations. The amount we charge is called
amortized cost
𝐶𝑖 = is the actual cost
𝐶𝑖 =is the amortized cost
Accounting method
• Charge i th operation a fictitious amortized cost
ĉi, where $1 pays for 1 unit of work (i.e., time).
• This fee is consumed to perform the operation.
• Any amount not immediately consumed is stored
in the bank for use by subsequent operations.
• The bank balance must not go negative! We
must ensure that n n
ci cˆi
i1 i1
for all n.
• Thus, the total amortized costs provide an upper
bound on the total true costs.
Accounting analysis of
dynamic tables
Charge an amortized cost of ĉi = $3 for the i th
insertion.
• $1 pays for the immediate insertion.
• $2 is stored for later table doubling.
When the table doubles, $1 pays to move a recent
item, and $1 pays to move an old item.
Accounting analysis
(continued)
Key invariant: Bank balance never drops below 0.
Thus, the sum of the amortized costs provides an
upper bound on the sum of the true costs.
i 1 2 3 4 5 6 7 8 9 10
sizei 1 2 4 4 8 8 8 8 16 16
ci 1 2 3 1 5 1 1 1 9 1
ĉi 2* 3 3 3 3 3 3 3 3 3
banki 1 2 2 4 2 4 6 8 2 4
Potential method
ĉi = ci + (Di) – (Di–1)