Example: Prior Measures

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

 So usually only R(P) is considered

Space Time Tradeoff:


A space-time trade-off in algorithm is a way of solving a problem in:
 Less time using more memory
 By solving in very little space by spending a long time.

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

Counting Steps Implementation


 Assignment – count 1
 For, while, repeat loops
 Using a variable count
 Using steps per execution of instructions

 Use a variable count to store step count and increment it wherever necessary
 Display count

Computing Step Count from s/e


 Each statement will have an (s/e) steps/execution depending on the type of
statement
 Frequency of each statement is multiplied with the corresponding s/e of that
statement to get the step count for that statement.
 Statement Step counts are added to give the total 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

Solving the Recurrence:


 trsum(n)=2+ trsum(n-1)
 = 2+2+ trsum(n-2) = 2*2+ trsum(n-2) = 2*3+ trsum(n-3) - - - - - - - - - - =2*n + trsum
 = 2*2+ trsum(n-2) = 2*3+ trsum(n-3) - - - - - - - - - - =2*n + trsum(n-n) = 2n+2 n>=0
 = 2*3+ trsum(n-3) - - - - - - - - - - =2*n + trsum(n-n) = 2n+2 n>=0 Comp
 - - - - - - - - - - =2*n + trsum(n-n) = 2n+2 n>=0 Complexity –
 =2*n + trsum(n-n) = 2n+2 n>=0
 = 2n+2 n>=0 Complexity – Representation

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).

Worst Case Complexity:


It is the maximum run time, over all inputs of size n, ignoring effects (a) through (d)
above. That is, we only consider the "number of times the principle activity of that
algorithm is performed".
Worst case complexity W can be defined as
– 𝐖(𝐧) = 𝐦𝐚𝐱{ 𝐓(𝐢) / 𝐢  𝐃𝐧}

Best Case Complexity:


In this case we look at specific instances of input of size n. For example, we might
get best behavior from a sorting algorithm if the input to it is already sorted.
Best Case complexity B can be defined as
– 𝐁(𝐧) = 𝐦𝐢𝐧{ 𝐓(𝐢) / 𝐢  𝐃𝐧}

Average Case Complexity:


The amount of some computational resource (typically time) used by the algorithm,
averaged over all possible inputs.
Average behavior can be defined as
𝐀(𝐧) = ∑_(𝐢 ∈ 𝐃𝐧)▒〖𝐏(𝐢) × 𝐓 (𝐢)〗

Computing Average Case Complexity:


 Two steps
 An understanding of the average nature of the input
 Performing a run-time analysis of the algorithm for different cases
 Difficult to estimate the statistical behavior of the input
Running Time Analysis:
 Running time analysis can be performed by counting the number of statements
executed
 We can count:
 All statements
 Frequently performed statements

Stack
A stack is an abstract data type that holds an ordered, linear sequence of items.

Stack Operations to preform


 Operations
1. Push ()
2. Pop ()
3. Multi Pop (k)

 Speed different
 Push () and Pop () are fast.
 Multi Pop (k) may be slow

 Time of a single operation may vary

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:

You might also like