DS - Module I
DS - Module I
Module I
• System Life Cycle
• Algorithms
• Performance Analysis
o Space Complexity
o Time Complexity
• Asymptotic Notation
• Complexity Calculation of Simple Algorithms
• Good programmers regard large scale computer programs as systems that contain many
complex interacting parts. (Systems: Large Scale Computer Programs.)
• As systems, these programs undergo a development process called System life cycle.( SLC
: Development Process of Programs)
• The system life cycle is a series of stages that are worked through during the development
of a new information system.
• A lot of time and money can be wasted if a system is developed that doesn’t work properly
or do exactly what is required of it.
5 Phases of system life cycle
1. Requirements phase
2. Analysis phase
3. Design phase
4. Refinement and coding phase
5. Verification phase
[Link] phase
• All programming projects begin with a set of specifications that defines the purpose of
that program.
• Requirements describe the information that the programmers are given (input) and the
results (output) that must be produced.
• Frequently the initial specifications are defined vaguely and we must develop rigorous
input and output descriptions that include all cases.
[Link] phase
• In this phase the problem is break down into manageable pieces.
• Generate diagrams that are used to design the system
• Several alternate solutions to the programming problem are developed and compared
during this phase
• There are two approaches to analysis:- (4 marks)
i. Bottom-up approach
ii. Top-down approach
Bottom up approach
➢ Bottom-up approach is an older, unstructured strategy that places an early emphasis on
coding fine points.
➢ Since the programmer does not have a master plan for the project, the resulting program
frequently has many loosely connected, error ridden segments.
Module I CS201 – Data Structures(KTU)
[Link] Phase
• In this phase we choose representations for data objects and write algorithms for each operation
on them.
• Data objects representation can determine the efficiency of the algorithm related to it. So we
should write algorithms that are independent of data objects first.
• Frequently we realize that we could have created a much better system. (May be we realize that
one of our alternate design is superior than this). If our original design is good, it can absorb
changes easily.
Correctness of proof
• Program can be proven correctly using proof(like mathematical theorem)
• Proof are very time consuming and difficult for develop for large program
• Scheduling constraints presents the development of complete set of proofs for a
larger system
• However selecting the algorithm that have been proven correct can reduce the
number of errors
Module I CS201 – Data Structures(KTU)
Testing
• Testing can be done only after coding
• Testing require work code and set of test data
• Test data should be chosen carefully so that it include all possible scenarios
• Good test data should verify that every piece of code correctly
• Eg: If our program contain a switch statement, our test data should lie chosen so that
we can check each case within switch statement
Error removal
• If not done properly ,the correctness of proof and system test will indicate erroneous
code
• Removal of errors depend on design and code
• While debugging large undocumented programs each corrected error possibly
generate several new errors
• Debugging a well-documented program that is divided into autonomous units that
interact through parameters is far easier.
• This is especially true if each unit is tested separately and then integrated into a
system
PSEUDO CODE
Example : Consider the linear search in an array, the algorithm can be written as below;
From the above example we can see that pseudocode act as a bridge between algorithm and
actual program in a specific language.
RECURSIVE ALGORITHMS
• When we have more than one algorithm to solve a problem, we need to select the best
one. Performance analysis helps us to select the best algorithm from multiple algorithms
to solve a problem.
• Performance analysis means analysis of performance of an algorithm before execution
Module I CS201 – Data Structures(KTU)
Fixed Part:
Example 1
Algorithm Sum(A,n)
{
s=0
for i=0 to n-1 do
s = s + A[i] return s
}
Example 2
➢ Space required for one recursive call includes the space for parameters, space for local
variables and space for return address. Because for each recursive call, this much
Module I CS201 – Data Structures(KTU)
Example 1
Algorithm RSum(a,n)
{
if(n<=0)
return 0;
else
return a[n] + RSum(a,n-1)
}
➢ Here the recursion goes from n down to 0, making n+1 recursive calls.
➢ Space required for on recursive call
= Space for parameters + Space for local variables + Space for return address
➢ Space for parameters: Two parameters
i. a :1(Here we assume that only the reference to the array is passed
in a recursive call , not the entire array, so memory requirement is
taken as 1 unit)
ii. n:1
➢ Space for local variables: No local variables
➢ Space for return address: 1
Example 2
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)
➢ Time Complexity
❖ Note that the amount of computing represented by one program step may be
different from that represented by another step. So, for example, we may count
a simple assignment statement of the form a = 2 as one step and also count a
more complex statement such as a = 2*b+3=^c/d~e+f/g/a/b/c as one step. The
only requirement is that the time required to execute each statement that is
counted as one step be independent of the instance characteristics(instance
characteristics are input , output etc..)
i. To construct a step count table we first determine the step count for each
statement. We call this the steps/execution, or s/e for short.
ii. Next we figure out the number of times that each statement is executed. We
call this the frequency. The frequency of a nonexecutable statement is zero.
iii. Multiplying s/e by the frequency, gives us the total steps for each statement.
Module I CS201 – Data Structures(KTU)
iv. Summing these totals, gives us the step count for the entire function
➢ Rules
o Comments and Declaration-step count=0
o Return and Assignment- step count=1
o Ignore constant multiplier
o Nested loops: The total running time of a statement inside a group of nested loop
is the running time of statement multiplied by the product of the size of all the
loops
for (i=0;i<n;i++)
for (j=0;j<n;j++)
k++
TC→ O(n2 )
o Consecutive statements: Maximum is one that counts
Eg: for (i=0;i<n;i++) → O(n)
a[i]=0;
for (i=0;i<n;i++) →O(n2 )
for (j=0;j<n;j++)
a[i]+= a[j]+i+j
➢ Examples
Eg [Link] the time and space complexity of the algorithm to find sum of elements in an array
Time Complexity = 2n + 3
Eg2: Find the time and space complexity of matrix addition algorithm
Space Complexity = Space for parameters and Space for local variables
m→1 n→1 a[]→mn b[]→mn c[]→mn i→1 j→1
Space complexity = 3mn + 4
• Eg3: Find the time and space complexity of recursive sum algorithm
Time complexity
Space complexity :
in these cases, no of times the for statement executed can be computed as;
ie, the no of iterations the loop body will execute is Log2 (n)+1. And the for statement will execute one
more time(in that time, condition will became false and loop terminates)
❖ Same logic can be used to find the time complexity of following algorithm
BEST CASE, WORST CASE AND AVERAGE CASE COMPLEXITY (Explain with example:5 marks)
• In certain case we cannot find the exact value of frequency count. In this case we have 3 types
offrequency counts
o Best Case : It is the minimum number of steps that can be executed for a given parameter
o Worst Case: It is the maximum number of steps that can be executed for a given parameter
o Average Case: It is the average number of steps that can be executed for a given parameter
• Eg: Linear Search
o Best Case: Search data will be in the first location of the array.
o Worst Case: Search data does not exist in the array
o Average Case: Search data is in the middle of the array.
• The function f(n) = O(g(n)) if there exists 2 positive constants c and n0 such that
0 ≤ f(n) ≤ c g(n) for all n ≥ n0
f(n)=computing time of some algorithm
g(n)=general function
▪ It is the measure of longest amount of time taken by an algorithm (Worst case).
▪ It is asymptotically tight upper bound
Eg:f(n)= 2n2 + n + 3
g(n)= n2
c=3
2. Omega (Ω)
▪ The function f(n) = Ω (g(n)) if there exists 2 positive constant c and n0 such that
f(n) ≥ c g(n) ≥ 0 for all n ≥ n0
▪ It is the measure of smallest amount of time taken by an algorithm (Best case).
▪ It is asymptotically tight lower bound
Eg:f(n)= 2n2 + n + 3
g(n)= n2
Module I CS201 – Data Structures(KTU)
c=2
•
3. Theta (Ɵ)
▪ The function f(n) = Ɵ (g(n)) if there exists 3 positive constants c1, c2 and n0 such that
0 ≤ c1 g(n) ≤ f(n) ≤ c2 g(n) for all n ≥ n0
▪ It is the measure of average amount of time taken by an algorithm (Average case).
▪ Eg:f(n)= 2n2 + n + 3
g(n)= n2
c1=2
c2=3
4. Little Oh (o)
▪ The function f(n) = o(g(n)) if for any positive constant c>0, there exists a constant n0>0 such
that 0 ≤ f(n) < c g(n) for all n ≥ n0
▪ It is asymptotically loose upper bound
Where g(n)!=0
g(n) becomes arbitrarily large relative to f(n) as n approaches infinity
5. Little Omega (ω)
▪ The function f(n) = ω(g(n)) if for any positive constant c>0, there exists a constant n0>0 such
that f(n) > c g(n) ≥ 0 for all n ≥ n0
▪ It is asymptotically loose lower bound
Where f(n)!=0
f(n) becomes arbitrarily large relative to g(n) as n approaches infinity
O(1) < O(log n) < O(n) < O(n log n) < O(nk) < O(2n) < O(n!)
Module I CS201 – Data Structures(KTU)
if flag=0 then
Print “Search data not found”
else
Print “Search_data found at index “ mid
}
19 CS KTU Lectures
Module I CS201 – Data Structures(KTU)
o
Answer:
o Case 1 complexity=O(n)
o Case 2 complexity=O(n2)
o The overall complexity = O(n2)
20 CS KTU Lectures