Professional Documents
Culture Documents
Example: Prior Measures
Example: Prior Measures
Example: Prior Measures
Example
Store Employee information
Solutions
Array
Linked list
Which locates a particular employee faster?
Which provides better utilization of memory?
Which is easy to code?
Which is easier to test?
Prior Measures
Instruction count:
The total number of instructions that get executed for a particular
task, algorithm, workload, or program is referred to the instruction count.
Basic operations:
The operation contributing the most to the total running time of an algorithm.
It is typically the most time consuming operation in the algorithm’s innermost
loop.
Examples:
Key comparison operation; arithmetic operation (division being the most time-
consuming, followed by multiplication)
Size of input.
Initialization instructions
Loops.
Number of passes of the loops
Count the basic operations/steps
Basic Steps
Search
Basic operation is compare x and y
Matrix multiplication
Basic operation is multiplication of real numbers
Time complexity can also be expressed in terms of the basic steps performed.
Expressed as a function of instance characteristics
Input size
Output size etc.
Steps in Algorithm
Step is a meaningful segment of program or computational unit that is independent
of the instance characteristics
Example
10 or 100 additions can be one step
200 multiplications can be another step
but not n additions
Use a variable count to store step count and increment it wherever necessary
Display count
Computing Complexity:
X = tsum(n-1)
tsum(n)= { 2 2+ tsum(n-1) if n=0 if n>0
Known as a recurrence relation.
tsum(n)=2+ tsum(n-1)
= 2+2+ tsum(n-2)
= 2*2+ tsum(n-2)
= 2*3+ tsum(n-3)
----------
=2*n + tsum(n-n)
= 2n+2 n>=0
Complexity – Representation
Complexity of an algorithm is a measure of the amount of time and/or space
required by an algorithm for an input of a given size (n).
Stack
A stack is an abstract data type that holds an ordered, linear sequence of items.
Speed different
Push () and Pop () are fast.
Multi Pop (k) may be slow
Methods
i. Aggregate analysis:
The total amount of time needed for the n operations is computed and divided
by n
ii. Accounting:
Operations are assigned an amortized cost. Objects of the data structure are
assigned a credit
iii. Potential:
The prepaid work is represented as “potential” energy that can be released to
pay for future operations
Aggregate analysis
n operations take T(n) time
Amortized cost of an operation is 𝐓(𝐧)/𝐧
Stack Example:
Aggregate analysis
Push and Simple Pop - 𝐎(𝟏)
Multipop- O(min(stacksize, k)) for k elements
Maximum 𝐧 𝐩𝐨𝐩 for 𝐧 𝐩𝐮𝐬𝐡
A sequence of 𝐧 𝐏𝐮𝐬𝐡 and Pop operations has a cost of 𝟐 ∗ 𝐧 = 𝐎(𝐧)
Amortized cost of each operation is 𝐎(𝐧)/𝐧 = 𝐎(𝟏)
Stack Example: