Download August 26 - CSE@IIT Delhi

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CSL 356: Analysis and Design of
Algorithms
Ragesh Jaiswal
CSE, IIT Delhi
Techniques
 Greedy Algorithms
 Divide and Conquer
Digression: Amortized Analysis of Data Structures
 Dynamic Programming
 Network Flows
Amortized Analysis of Data
Structures
Amortized analysis of Data Structures
 Suppose you have a data structure such that the worst-case
time for performing one of the operations is O(n).
 On the other hand, starting with an empty structure, the
total time required to do a sequence of n operations is O(n).
 Would you use this data structure?
Amortized analysis of Data Structures
 Suppose you have a data structure such that the worst-case
time for performing one of the operations is O(n).
 On the other hand, starting with an empty structure, the
total time required to do a sequence of n operations is O(n).
 Would you use this data structure?
 Example: If this data structure is used within some algorithm.
Amortized analysis of Data Structures
 A data structure has T(n) amortized running time of its
operations, if every time n operations are performed in any
order the total running time is O(n*T(n)).
 General definition: Consider a data structure that supports k
operations op1,…,opk. These operations have amortized
running time t1(.),…,tk(.) if for any sequence of n
operations consisting of n1 operations of type op1, n2
operations of type op2, …, nk operations of type nk, the
total time is at most
n1*t1(n1) + n2*t2(n2) + … + nk*tk(nk)
Amortized analysis: Examples
Stack implementation
Amortized Analysis: Examples
 Problem: Give an implementation of an integer stack using
an array.
 Heuristic:
 Maintain a variable top storing the last inserted element
in the array A.
 Pop(): return the integer at A[top] and decrement the
pointer.
 Push(x): If A is not full then store x at A[top+1], else
create a new array B of double the size, copy all elements
of A in the new array, and then store x at B[top+1].
Amortized Analysis: Examples
 Problem: Give an implementation of an integer stack using
an array.
 What is the amortized running time of n operations?
 Pop operations account for O(n) time.
 How much does copying cost?
Amortized Analysis: Examples
 Problem: Give an implementation of an integer stack using
an array.
 What is the amortized running time of n operations?
 Pop operations account for O(n) time.
 How much does copying cost?
 O(n)
 So, the amortized running time of n operations is O(1).
Amortized analysis: Examples
Queue with two stacks
Amortized Analysis: Examples
 Problem: Give an implementation of a queue using two
stacks.
A
B
Amortized Analysis: Examples
 Problem: Give an implementation of a queue using two
stacks.
x3
x2
x1
A
B
Amortized Analysis: Examples
 Problem: Give an implementation of a queue using two
stacks.
 Heuristic:
 Enqueue(x): Push x in A
 Dequeue(): If B is not empty, then Pop from
B, else move all elements from A to B and
then Pop.
 What is the amortized running time of n
Enqueue/Dequeue operations?
x1
x2
x3
A
B
Amortized Analysis: Examples
 Problem: Give an implementation of a queue using two
stacks.
 Heuristic:
 Enqueue(x): Push x in A
 Dequeue(): If B is not empty, then Pop from
B, else move all elements from A to B and
then Pop.
 What is the amortized running time of n
Enqueue/Dequeue operations?
 O(1) since there are at most O(1)
(push/pop) operations per item.
x1
x2
x3
A
B
Amortized analysis
Piggy Bank Analysis
Amortized analysis: Potential function
 We maintain a piggy bank that initially has 0 money.
 Each time we perform a simple operation, we have to pay the
machine Rs.1.
 We take out money from the piggy bank to pay for all the
operations.
 The total running time is the total amount of money that is
paid to the machine.
Amortized analysis: Problem 1 using
piggy bank
 Each time there is a Push operation we deposit Rs. 3 to our
piggy bank.
 Each time there is a pop operation, we deposit Rs. 1 to out
piggy bank.
 Claim: There will be enough money in the bank to pay for all
the operations.
 So, the total time for performing n operations is 4n.
Amortized analysis: Problem 2 using
piggy bank
 Each time there is a Push operation we deposit Rs. 4 to our
piggy bank.
 Claim: There will be enough money in the bank to pay for all
the operations.
 So, the total time for performing n operations is O(n).
End
Problems to think about:
1. We implement a binary counter by maintaining a linked list
where each node contains a single bit of the counter. When the
counter is incremented by 1, then we traverse through the list
and make appropriate changes. What is the amortized running
time of n increment operations?