2. Text Books
• Suggested Textbooks:
1. Fundamentals of Data Structures, E.Horowitz and
S.Sahni,1977
2. Data Structures and Algorithms, alfred V.Aho, John
E.Hooperoft, Jeffrey D.Ullman
3. COURSE OBJECTIVES
1. To impart the basic concepts of data structures and algorithms.
2. To introduce various searching and sorting techniques.
3. To demonstrate operations of linear and non-linear data structure.
4. To develop an application using suitable data structure.
4. COURSE OUTCOMES
After completion of the course, the student will be able to
1. Understand the specifications for writing algorithms and analyze its
performance.
2. Analyze basic concepts of data structures, computation complexity.
3. Understand linear data structures, various sorting, searching
techniques.
4. Apply various operations on linear and non-linear data structures.
5. Identify appropriate and efficient data structure to implement a
given problem.
5. UNIT-1
Basic Terminologies & Introduction to algorithm and Data
Organization:
• Algorithm Specification
• Recursion
• Performance Analysis
• Asymptotic Notation
• The Big-O, Omega and Theta notation
• Programming style
• Refinement of coding-time-space trade off
• Testing and Data abstraction
6. What is an algorithm?
An algorithm is a sequence of unambiguous instructions
for solving a problem, i.e., for obtaining a required
output for any legitimate input in a finite amount of
time.
“computer”
problem
algorithm
input output
Algorithmic solution : a way to teach the computer
Notion of algorithm
7. An Algorithm must satisfy the following criteria
(features).
1) Input
2) Output
3) Definiteness
4) Finiteness
5) Effectiveness
8. 1)Input- Zero or more quantities externally supplied.
2)Output- At least one quantity is produced.
3)Definiteness- Each instruction is clear and
unambiguous.
4)Finiteness- If we trace out the instructions of an
algorithm, then for all cases the algorithm terminates
after a finite number of steps.
5)Effectiveness- Every step of algorithm should be
feasible.
9. Basic Issues Related to Algorithms
• How to design algorithms
– Understanding the problem (instances, ranges)
– Ascertaining the capabilities of a computational device
(RAM and parallel machines, speed and amount of memory)
– Choosing between exact and approximate algorithms
– Deciding the correct data structure
– Algorithm design techniques
• How to express algorithms (Specification)
– Methods of specifying an algorithm (free language, pseudo
code, flowchart)
10. Basic Issues Related to Algorithms
• Proving correctness (Verification)
– Algorithm’s Proof
• Analyzing an algorithm (Analysis)
Efficiency, theoretical and empirical analysis, optimality
• Coding an algorithm (Implementation)
11. Algorithm Design Techniques/Strategies
Greedy Approach
Dynamic Programming
Back Tracking
Branch and Bound
Brute Force Technique
Divide and Conquer
New Microsoft Word Document.docx
12. Pseudo code for expressing Algorithms
Algorithm is broadly divided into 2 sections.
1. Algorithm heading- It consists of name of algorithm,
problem description, input and output.
Ex:- Algorithm name(v1,v2,……,vn)
In heading section we should write the following things:-
//Problem Description:
//Input:
//Output:
13. 2. Algorithm body- It consists of logical body of the
program by making use of various programming constructs
and assignment statements.
• The Body of an algorithm is written, in which various
programming constructs like if, for, while and some
assignment statements.
• The compound statements should be enclosed with in {
and } brackets.
• Single line comments are written using // as beginning of
comment.
14. • The Identifier should begin by letter and not by digit. An
identifier can be a combination of alphanumeric string.
• Assignment operator
Ex:- Variable Expression
• There are other types of operators like Boolean(true , false),
Logical(and , or , not) and Relational(<,<=,>,>=,=).
• Array indices [ , ] initial index is 0.
• Inputting and outputting can be done using read and write.
Ex:- write(“WELCOME”);
read(val);
15. • The conditional statements if-then and if-then-else.
If(condition) then statement
If(condition) then statement else statement
We can use brackets also { and }.
• While statement
While(condition) do
{ Statement 1;
Statement 2;
….…
Statement n;
}
16. • For statement
For variable value1 to valuen do (step i)
{
Statement 1
Statement 2
……..
Statement n
}
• Repeat-Until statement
repeat
Statement 1
Statement 2
……..
Statement n
until(condition)
17. • Break statement is used to exit from inner loop.
• Return statement is used to return control from one
point to another.
Examples:-
1. Write an algorithm to find sum of n numbers.
2. Write an algorithm to check the given number is
even or odd.
3. Write an algorithm to find factorial of a number.
4. Write an algorithm to sort the given list of
elements.
18. Algorithm sum()
// Prob Desc: This alg is used to find sum of n numbers
// input: n value
// output: sum of n numbers
{
Write(“enter n”);
Read(n);
i0;
Repeat
{
sumsum+I;
ii+1;
}
Until(i<=n)
Write(“sum of n numbers is”);
Write(sum);
}
19. Performance Analysis
• The efficiency of an algorithm can be decided by
measuring the performance of an algorithm. we can
measure the performance of an algorithm by
computing two factors.
1. Amount of TIME required by an algorithm to
execute. (Time Complexity)
2. Amount of STORAGE required by an algorithm.
(Space Complexity)
20. Space Complexity
• The Space Complexity can be defined as amount of memory
required by an algorithm to run.
• To compute Space Complexity we use two factors:-
• Constant and Instance Characteristics.
• The space requirement S(p) can be given as:
S(p) = C + Sp
Where C is constant i.e fixed part it denotes the space of inputs
and outputs.
This space is an amount of space taken by instruction, variables
and identifiers.
21. • Sp is a space depends upon instance characteristics.
• This is a variable part whose space requirement depends on
particular problem instance.
Ex:- Addition of two numbers.
S(p) = C Ө(Sp) = 0
Ex:- Addition of array elements.
S(p) >= (n+3)
Ex:- Addition of array elements.(Recursive)
S(p) >= 3(n+1)
The internal stack used for recursion includes space for formal
parameters, local variables and return address.
22. Time Complexity
• The time complexity of an algorithm is the amount of
computer time required by an algorithm to run to completion.
• Executing time depends on many factors:-
System load
Number of other programs running
Instruction set used
Speed of underlying hardware
• Therefore the time complexity is in terms of frequency count.
• Frequency count is a count denoting number of times of
execution of a statement.
23. • Measuring an input size
• Measuring running time
T(n)= C(n) Cop
- Where T(n) is running time of basic operation
- Cop is Time taken by the basic operation to execute
- C(n) is number of times the operation needs to be
executed
• Order of Growth:- Measuring the performance of an
algorithm in relation with the input size n is called Order
of Growth.
24. • Example the order of growth for varying input
size of n
logn n nlogn n*n POW(2,n)
0 1 0 1 2
1 2 2 4 4
2 4 8 16 16
3 8 24 64 256
4 16 64 256 65,536
5 32 160 1024 4,294,967,296
25. Asymptotic Notations
• To choose the best algorithm, we need to check efficiency of
each algorithm.
• The efficiency can be measured by computing time
complexity of each algorithm.
• Asymptotic notation is a shorthand way to represent the time
complexity.
• Using asymptotic notations we can give time complexity as
“Fastest possible”, “Slowest Possible” or “Average Time”.
• Various notations such as ,ϴ and used are called
Asymptotic notations.
26. 1.Big oh Notation
• The Big oh notation is denoted by “”.
• It is a method of representing the upper bound of algorithms
running time.
• Using Big oh notation we can give longest amount of time taken by
the algorithm to complete.
• Definition:- f(n)=O(g(n))
Let f(n) and g(n) be two non negative functions.
Let n0 and constant c are two integers such that no denotes some
value of input and n>=n0
similarly c is some constant such that c>0
we can write F(n)<=c*g(n) for all n>=n0
27. • Then F(n) is Big oh of g(n). It is also denoted as F(n) Є
O(g(n)). In other words F(n) is <=g(n) is multiple of
some constant c.
28. • Ex:- Consider the function F(n)=2n+2 and g(n)=n2
then we have to find some constant c so that
f(n)<=c*g(n).
if n=1 then f(n) > g(n)
if n=2 then f(n) > g(n)
if n=3 then f(n) < g(n)
• Thus always upper bound of existing time is obtained
by Big oh notation.
29. 2.Omega Notation
• Omega notation is denoted by “”.
• This notation is used to represent the Lower Bound of
algorithm’s running time.
• Using Omega notation we can denote shortest amount of
time taken by algorithm.
Definition:-
• A function f(n) is said to be in (g(n)) if f(n) is bounded below
by some positive constant multiple of g(n) such that
• F(n) > = c*g(n) for all n>=n0
• It is denoted as f(n) Є (g(n))
31. 3.Theta Notation
• The theta notation is denoted by “ϴ”.
• By this method the running time is between upper bound and
lower bound.
• Definition:-
• Let f(n) and g(n) be two non negative functions. There are two
positive constants namely C1 and C2 such that
C1*g(n)<=f(n)<=C2*g(n) for all n, n>=n0
f(n) Є ϴ (g(n))
32. Ex:- f(n)=2n+8 and g(n)=7n where n>=2
Similarly f(n)=2n+8 and g(n)=7n
i.e 5n < 2n+8 < 7n for n>=2
33. Performance Analysis
1. O(1): Time complexity of a function (or set of statements) is
considered as O(1) if it doesn’t contain loop, recursion and
call to any other non-constant time function.
2. O(n): Time Complexity of a loop is considered as O(n) if the
loop variables is incremented / decremented by a constant
amount.
3. O(Logn):Time Complexity of a loop is considered as O(Logn) if
the loop variables is divided / multiplied by a constant
amount.
34. 4) O(nc): Time complexity of nested loops is equal to the
number of times the innermost statement is executed.
For example the algorithm addition of two matrices
contains two loops then we have O(n2) time complexity.
5) O(LogLogn): Time Complexity of a loop is considered as
O(LogLogn) if the loop variables is reduced / increased
exponentially by a constant amount.
35. Analysis of Algorithms
• We can have three cases to analyze an algorithm:
1) Worst Case
2) Average Case
3) Best Case
Ex:- Linear Search
int search(int arr[], int n, int x)
{
int i;
for (i=0; i<n; i++)
{
if (arr[i] == x)
return i;
}
return -1;
}
36. • If we use the ϴ notation for time complexity then
• Worst Case is ϴ(n).
• Average Case is
∑(i=1 to n+1)(ϴ(i))
-----------------------
n+1
• i.e Θ(n)
• Best Case is ϴ(1)
37. Binary Search
• Worst Case O(n) for an unsorted array and O(logn)
for sorted array.
• Best Case is O(1) (if the key element is equal to the
first element of the given array).
• Average Case O(logn)
38. int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l)/2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid-1, x);
return binarySearch(arr, mid+1, r, x);
}
return -1;
}
40. Time Complexities of all Sorting Algorithms
Best Average Worst
Selection Sort Ω(n^2) θ(n^2) O(n^2)
Bubble Sort Ω(n) θ(n^2) O(n^2)
Insertion Sort Ω(n) θ(n^2) O(n^2)
Heap Sort Ω(n log(n)) θ(n log(n)) O(n log(n))
Quick Sort Ω(n log(n)) θ(n log(n)) O(n^2)
Merge Sort Ω(n log(n)) θ(n log(n)) O(n log(n))
41. • Programming Style
1. Clarity and simplicity of Expression: The programs should be
designed in such a manner so that the objectives of the program is
clear.
2. Naming: In a program, you are required to name the module,
processes, and variable, and so on. Care should be taken that the
naming style should not be cryptic and non-representative.
For Example: a = 3.14 * r * r
area_of_circle = 3.14 * radius * radius;
42. • Programming Style
3. Spaces: Style related to white space is commonly used to enhance
readability. For instance, compare the following syntactically
equivalent examples of C code:
int i;
for(i=0;i<10;++i){
printf("%d",i*i+i);
}
versus
int i;
for (i = 0; i < 10; ++i) {
printf("%d", i * i + i);
}
43. • Programming Style
4. Tabs: tabs work fine provided they are used consistently, restricted to
logical indentation, and not used for alignment.
int ix;
long sum;
5. Control Constructs: It is desirable that as much as a possible single entry
and single exit constructs used.
6. Indentation: Indentation styles assist in identifying control flow and
blocks of code. In some programming languages, indentation is used to
delimit logical blocks of code; correct indentation in these cases is more
than a matter of style. In other languages, indentation and white space do
not affect function, although logical and consistent indentation makes code
more readable.
44. 7. Nesting: Deep nesting of loops and conditions greatly harm the
static and dynamic behaviour of a program. It also becomes difficult
to understand the program logic, so it is desirable to avoid deep
nesting.
8. Module size: The module size should be uniform. The size of the
module should not be too big or too small.
49. Testing:-
• Technically Testing is a process to check if the application is
working same as it was supposed to do, and not working as it was
not supposed to do.
• Main objective of Testing is to find bugs and errors in an
application which get missed during the unit testing by the
developer.
• A unit is the smallest testable part of any software. It usually has
one or a few inputs and usually a single output.
• A unit may be an individual program, function, procedure, etc
50. • Benefits:
• Unit testing increases confidence in changing / maintaining code.
If good unit tests are written and if they are run every time any
code is changed, we will be able to promptly catch any defects
introduced due to the change. Also, if codes are already made less
interdependent to make unit testing possible, the unintended
impact of changes to any code is less.
• Codes are more reusable. In order to make unit testing possible,
codes need to be modular. This means that codes are easier to
reuse.
• Debugging is easy. When a test fails, only the latest changes need
to be debugged.
51. • Data abstraction
• Data Abstraction is the property by virtue of which only the
essential details are displayed to the user.
• The trivial or the non-essentials units are not displayed to the user.
Ex: A car is viewed as a car rather than its individual components.
• Data Abstraction may also be defined as the process of identifying
only the required characteristics of an object ignoring the
irrelevant details.
• The properties and behaviors of an object differentiate it from
other objects of similar type and also help in classifying/grouping
the objects.
52. • Consider a real-life example of a man driving a car.
• The man only knows that pressing the accelerators will increase
the speed of car or applying brakes will stop the car, but he does
not know about how on pressing the accelerator the speed is
actually increasing, he does not know about the inner mechanism
of the car or the implementation of the accelerator, brakes, etc in
the car. This is what abstraction is.
• Advantages of Abstraction
• It reduces the complexity of viewing the things.
• Avoids code duplication and increases reusability.
• Helps to increase security of an application or program as only
important details are provided to the user.