Chapter 1 Introduction Analysis of Algorithm
Chapter 1 Introduction Analysis of Algorithm
• Complexity of an algorithm refers to the measure of the time that it will need to execute
and get the expected output, and the Space it will need to store all the data.
• Hence these two factors define the efficiency of an algorithm.
Space Factor: Space is measured by counting the maximum memory space required by
the algorithm to run/execute.
Algorithm complexity:
1. Space Complexity:
• The space complexity of an algorithm refers to the amount of memory required by the
algorithm to store the variables and get the result.
To calculate Space Complexity: Determined based on folbeging 2 components:
Fixed Part:
• This refers to the space that is required by the algorithm.
• For example, input variables, output variables, program size, etc.
Variable Part:
• This refers to the space that can be different based on the implementation of the
algorithm. For example, temporary variables, dynamic memory allocation, recursion
stack space, etc.
• Mathematically,
f(n) = Ω (g(n)) there exist positive constants c and n0
such that 0 ≤ c*g(n) ≤ f(n) for all n ≥ n0
f(n) and g(n) are non-negative functions.
• A nondeterministic computer is one that can “guess” the right answer or solution
• The solutions of the NP class are hard to find since they are being solved by a
non-deterministic machine but the solutions are easy to verify.
• Thus, NP can also be thought of as the class of problems “whose solutions can be
verified in polynomial time”
NP-hard class:
• An NP-hard problem is at least as hard as the hardest problem in NP and it is a class
of problems such that every problem in NP reduces to NP-hard.
Features:
• All NP-hard problems are not in NP.
• It takes a long time to check them. This means if a solution for an NP-hard problem is
given then it takes a long time to check whether it is right or not.
• A problem A is in NP-hard if, for every problem L in NP, there exists a polynomial-
time reduction from L to A.
Examples:
• Halting problem.
• Qualified Boolean formulas.
• No Hamiltonian cycle.
NP-complete class:
• A problem is NP-complete if it is both NP and NP-hard.
• NP-complete problems are the hard problems in NP.
Features:
• NP-complete problems are special as any problem in NP class can be transformed or
reduced into NP-complete problems in polynomial time.
• If one could solve an NP-complete problem in polynomial time, then one could also
solve any NP problem in polynomial time.
Example:
• Hamiltonian Cycle.
• Satisfiability.
• Vertex cover.
Complexity Class Characteristic feature
Recurrence relations:
• Recurrence relations are used in algorithms to express the runtime or complexity of
recursive functions and data structures.
• The new card is put into the gap made on the right side by moving certain k cards.
• The master method provides a method for solving recurrence of the form
• The above recurrence describe that the algorithm divides the problem of size n into a
sub-problem, each of size n/b.
• f(n) is the cost of dividing the problem and combining the result of sub-problem.
case 1:
T(n) = O(n logba)
f(n) = O(n logba-e ) for some constant e > 0
For this case, f(n) must be smaller than nlogba
case 2:
T(n) = O(n logba log n) If f(n) = O(n logba )
This case is applied only when the two functions are of same size.
case 3:
T(n) = O(f(n))
f(n) = O(nlogba+e ) for some constant e > 0
and if a f(n/b) <= C f(n) for some constant C < 1 and sufficiently large n
For this case, f(n) must be larger than nlogba and f(n) must be larger and should also
satisfy the "regularity" and i.e. a f(n/b) <= Cf(n).
Note: In each of 3 cases we are comparing the function f(n) with nlogba and solution
to determined by larger of 2 function.
Recursion Tree:
• Recursion Tree is another method for solving the recurrence relations.
• A recursion tree is a tree where each node represents the cost of a certain recursive sub-
problem.
• We sum up the values in each node to get the cost of the entire algorithm.
Steps to Solve Recurrence Relation:
Step 1: Draw a recursion tree based on the given recurrence relation.
Step 2: Determine-
Cost of each level
Total number of levels in the recursion tree
Number of nodes in the last level
Cost of the last level
Step 3: Add cost of all the levels of the recursion tree and simplify the expression so
obtained in terms of asymptotic notation.
• To find the cost function C(n), consider • Each half is now n/2 long and will each
what happens when one splits a given array require n/2 array assignments to merge
(or sub-array) into two halves, sorts them, back together once they themselves are
and then merges the sorted halves back broken in half and their halves sorted.
together again. • This is a total of 2(n/2)=n array
• One will ultimately need n array assignments.
assignments to accomplish the final merge. • This happens again and again, each time
• The computation of the rest of the cost now picking up 4(n/4)=n array assignments,
requires we look at sorting the two halves. and then 8(n/8)=n array assignments, etc.,
until no more merges are necessary.