Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 205

Wolkite University

College of Computing
and Informatics

Data Structures & Algorithms


melaku.kore@gmail.com
CHAPTER ONE

INTRODUCTION TO DATA STRUCTURE


Introduction to Data Structure
A program is written in order to solve a problem. A
solution to a problem actually consists of two things:-
A way to organize the data
Sequence of steps to solve the problem. Here, the
former one is known as data structure whereas the
latter one is called algorithm.
Data is organized in a particular fashion for the
computer to be able to use it efficiently & this
structure is called as Data Structure.
Data structure is the structural representation of
logical relationships between elements of data.
Introduction to Data Structure
In other words a data structure is a way of organizing data
items by considering its relationship to each other.

Data structure is an arrangement of data in computer's


memory.

It makes the data quickly available to the processor for


required operations.

It is a structure program used to store ordered data, so


that various operations can be performed on it easily.

Data Structure=Organized Data + Operations


Cont…
Data structure affects the design of both the
structural and functional aspects of a program.

Algorithm + Data Structure = Program

Data structures are the building blocks of a program.


The selection of a particular data structure will help
the programmer to design more efficient programs as
the complexity and volume of the problems solved by
the computer is steadily increasing day by day.
Why is Data Structure important?
 Data structure is important because it is used in
almost every program or software system.

 It helps to write efficient code, structures the code


and solve problems.

 Data can be maintained more easily by encouraging a


better design or implementation.

 Data structure is just a container for the data that is


used to store, manipulate and arrange. It can be
processed by algorithms.
Classifications of Data strictures'

Data structures are generally classified into


primitive and non-primitive data structures.
Cont…
 Primitive data structures: basic data structures and are directly
operated upon by the machine instructions, which is in a
primitive level.
 They are integers, floating point numbers, characters, string
constants, pointers etc. These primitive data structures are the
basis for the discussion of more sophisticated (non-primitive)
data structures.
 Non-primitive data structures: It is a more sophisticated data
structure emphasizing on structuring of a group of homogeneous
(same type) or heterogeneous (different type) data items.
 Array, list, files, linked list, trees and graphs fall in this
category. Based on the structure and arrangement of data,
non-primitive data structures further classified in to linear and
non-linear data structures.
Cont…
1. Linear Data Structures :- the data elements are
arranged in a linear.
Arrays,Linked lists, Stacks and Queues are
examples of linear data structures.

2.Non- Linear Data Structures :-the data elements are


not in sequence that means insertion and deletion
are not possible in a linear manner.

Trees and Graphs examples of non-linear data


structures.
Cont…

Linear Data structure Non-Linear Data structure

Every item is related to Every item is attached with


previous and next item. many other items
Data is arranged in linear Data is not arranged in
sequence sequence
Data items can be Data items cannot be
traversed in a single run. traversed in a single run.
Implementation is easy Implementation is difficult
Introduction to Basic DS
Array
Arrays are most frequently used in programming.
Mathematical problems like matrix, algebra and etc.
can be easily handled by arrays.
An array is a collection of homogeneous data
elements described by a single name.
 Each element of an array is referenced by a
subscripted variable or value, called subscript or index
enclosed in parenthesis.
If an element of an array is referenced by single
subscript, then the array is known as one dimensional
array or linear array and if two subscripts are required
to reference an element, the array is known as two
dimensional arrays and so on.
Basic Operations on Array
 Inserting an element
 Retrieving an element
 Deleting an element
 Appending two arrays
 Getting index of a given element
 Modifying the element of an array
Lab Work (Revision )
Develop a complete C++ program to:-

1. Insert a particular array element on to the existing


array list.

2. Delete an element from the existing array list and


display the rest elements

3. append array elements of array A to array B and store


on third array C.
4. Modify the value of a particular array element.
Stack
 Stack is an ordered collection of items where the
addition of new items and the removal of existing
items always takes place at the same end.

 This end is commonly referred to as the “top.”


 The most recently added item is the one that is in
position to be removed first. This ordering principle
is sometimes called LIFO, last-in first-out.

stack of Books
Queue
 An ordered collection of items where the addition of
new items happens at one end, called the “rear,” and
the removal of existing items occurs at the “front”
end.

• A queue is called a FIFO (First in-First out) data


structure.
Linked List
 A linked list is a data structure that is built from
structures and pointers. It forms a chain of "nodes"
with pointers representing the links of the chain and
holding the entire thing together.
Graph
 Data sometimes contain a relationship between pairs of
elements which is not necessarily hierarchical in nature.

 A graph is composed of a set of vertices and a set of


edges (links) be-tween pairs of vertices.

 For example, suppose airlines flies only between the


cities connected by line.
Tree
 Data frequently contain a hierarchical relationship
various elements.
 The data structure which reflects this relationship is
called tree.
 
Basic Operations of Data structure
 Algorithms manipulate the data that is represented by
different data structures using various operations.
 The following data structure operations play major role in the
processing of data:
 Traversing: - It means to access each data item exactly once
so that it can be processed. For example, to print the names
of all the students in a class.
 Searching: - It is used to find the location of one or more
data items that satisfy the given constraint. Such a data item
may or may not be present in the given collection of data
items. For example, to find the names of all the students who
secured 100 marks in mathematics.
 Inserting: - It is used to add new data items to the given list
of data items. For example, to add the details of a new
student who has recently joined the course.
Cont…
 Deleting: - It means to remove (delete) a particular
data item from the given collection of data items. For
example, to delete the name of a student who has left
the course.
 Sorting: - Data items can be arranged in some order
like ascending order or descending order depending on
the type of application.
 For example, arranging the names of students in a
class in an alphabetical order, or calculating the top
three winners by arranging the participants’ scores in
descending order and then extracting the top three.
 Merging: - Lists of two sorted data items can be
combined to form a single list of sorted data items.
ALGORITHM AND ALGORITHM ANALYSIS
Definition of Algorithm

 The typical definition of algorithm is a formally defined procedure


for performing some calculation.

 A step-by-step procedure, which defines a set of instructions to be


executed in a certain order to get the desired output.

 A finite set of instructions that takes some raw data as input and
transforms it into refined data.

 If a procedure is formally defined, then it can be implemented


using a formal language, and such a language is known as a
programming language.
PROPERTIES OF ALGORITHM
 Not all procedures can be called an algorithm. An algorithm should have the
following characteristics

 Unambiguous − Algorithm should be clear and well defined definition.

 Input − an algorithm should have 0 or more well-defined inputs.

 Output − an algorithm should have 1 or more well-defined outputs, and


should match the desired output.

 Finiteness − Algorithms must terminate after a finite number of steps.

 Feasibility /Efficient− should be feasible with the available resources.

 Independent − an algorithm should have step-by-step directions, which


should be independent of any programming code.

 Correctness- Every step of the algorithm must generate a correct output.


TIME AND SPACE COMPLEXITY
 For any defined problem, there can be N number of solution.
 Similarly for any problem which must be solved using a
program, there can be infinite number of solutions.
 Example, two different algorithms to find square of a
number.
Solution 1.
for i=1 to n
do n = n + n
// when the loop ends n will hold its square
return n
Solution 2.
simply use a mathematical operator * to find the square of
a number
return n*n
Cont…
 Analysing an algorithm means determining the amount of resources
(such as time and memory) needed to execute it.

 Algorithms are generally designed to work with an arbitrary number


of inputs, so the efficiency or complexity of an algorithm is stated in
terms of time and space complexity.

 The time complexity of an algorithm is basically the running time of


a program as a function of the input size.

 Similarly, the space complexity of an algorithm is the amount of


computer memory that is required during the program execution as
a function of the input size.
Cont…
 It is difficult to use actual clock-time as a consistent
measure of an algorithm’s efficiency, because clock-
time can vary based on many things. For example,
 Specific processor speed

 Current processor load

 Specific data for a particular run of the


program

o Input Size

o Input Properties

 Operating Environment
Time Complexity Functions

 Best case: - The best case time complexity of the algorithm is


the function defined by the minimum number of operations
performed, taken across all instances of size n.
 In other words, it’s defined as fastest time to complete, with
optimal inputs chosen.
 For example, the best case for a sorting algorithm would be
data that's already sorted.
 Average case: - The average-case time complexity of the
algorithm is the function defined by an average number of
operations performed, taken across all instances of size n.
 Worst case: - The worst case time complexity of the algorithm
is the function defined by the maximum number of operations
performed, taken across all instances of size n.
TIME–SPACE TRADE-OFF
 The best algorithm to solve a particular problem at hand is no doubt
the one that requires less memory space and takes less time to
complete its execution.
 But in practice, it is not always possible to achieve both of these
objectives.
 There can be more than one algorithm to solve a particular problem.
 One algorithm may require more space but less time to complete its
execution while the other algorithm requires less time space but
takes more time to complete its execution. Hence, there exists a
time–space trade-off among algorithms.
 So, if space is a big constraint, then one might choose a program
that takes less space at the cost of more CPU time.
 On the contrary, if time is a major constraint, then one might choose
a program that takes minimum time to execute at the cost of more
space.
COMPLEXITY ANALYSIS
 Complexity Analysis is the systematic study of the cost of
computation, measured either in time units or in operations
performed, or in the amount of storage space required.

 The time complexity of an algorithm is basically the running time


of a program as a function of the input size.

 Similarly, the space complexity of an algorithm is the amount of


computer memory that is required during the program execution
as a function of the input size.
ALGORITHM EFFICIENCY
 If a function is linear (without any loops or recursions), the
efficiency of that algorithm or the running time of that
algorithm can be given as the number of instructions it
contains.

 If an algorithm contains loops, then the efficiency of that


algorithm may vary depending on the number of loops and
the running time of each loop in the algorithm.
 The following are some of classes of time complexities
used to measure the efficiency of an algorithms.
Classes of time complexities
 Constant, O(1): Constant Time Complexity describes an
algorithm that will always execute in the same time (or space)
regardless of the size of the input data set.
 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.
 Linear, O(n): Time Complexity of a loop is considered as
O(n) if the loop variables is incremented / decremented by a
constant amount.
Eg. for(i=0;i<100;i++) have f(n)=n
statement block;
 However calculating efficiency is not as simple as is shown in
the above example. Consider the loop given below:
for(i=0;i<100;i+=2) have f(n)=n/2
statement block;
Cont…
 Logarithmic, O(Logn) :-Time Complexity of a loop is considered as
O(Logn) if the loop variables is divided / multiplied by a constant
amount. Consider the following example
for(i=1;i<1000;i*=2)
for(i=1000;i>=1;i/=2) have f(n)=log 1000=10
statement block;

 Nested Loops:- In order to analyse nested loops, we need to determine


the number of iterations each loop completes. The total is then
obtained as the product of the number of iterations in the inner loop
and the number of iterations in the outer loop.
 We analyse the efficiency of the algorithm based on whether it is a
linear logarithmic, quadratic, or dependent quadratic nested loop.
 Linear logarithmic loop (n log n)
for(i=0;i<10;i++)
for(j=1; j<10;j*=2) have f(n)=10 log 10
statement block;
Cont…
 Dependent quadratic loop: - In a dependent quadratic loop, the
number of loop is dependent on the outer loop. Consider the code
given below:

for(i=0;i<10;i++)

for(j=0; j<=i;j++)

statement block;

 The inner loop will execute just once in the first iteration, twice in the
second iteration, thrice in the third iteration, so on and so forth.

 can be calculated as 1 + 2 + 3 + ... + 9 + 10 = 55

 Therefore, the efficiency of such a code can be given as

f(n) = n (n + 1)/2 => 10*(10+1)/2=(100+10)/2=55


Analysis Rules
 We assume an arbitrary time unit.
 Execution of one of the following operations takes time 1:
o Assignment Operation
o Single Input/output Operation
o Single Boolean Operations
o Single Arithmetic Operations
o Function Return
 Running time of a selection statement (if, switch) is the time for the
condition evaluation + the maximum of the running times for the
individual clauses in the selection.
 Loops: Running time for a loop is equal to the running time for the
statements inside the loop * number of iterations.
Cont…
 The total running time of a statement inside a group of nested loops is the
running time of the statements multiplied by the product of the sizes of all
the loops.
 Running time of a function call is 1 for setup + the time for any parameter
calculations + the time required for the execution of the function body.
Eg.
 
int count() Time Units to Compute
1 for the assignment statement: int k=0
{ 1 for the output statement.
1 for the input statement.
int k=0; In the for loop:
1 assignment, n+1 tests, and n increments.
cout<<”Enter an integer”; n loops of 2 units for an assignment, and an addition.
1 for the return statement.
cin>>n; -------------------------------------------------------------------
T (n) = 1+1+1+ (1+n+1+n) +2n+1 = 4n+6 = O (n)
for(i=0;i<n;i+) k=k+1; return 0;
}
Example 2 Time Units to Compute
----------------------------
void func() 1 for the first assignment statement: x=0;
{ 1 for the second assignment statement: i=0;
1 for the third assignment statement: j=1;
int x=0; 1 for the output statement.
1 for the input statement.
int i=0;
In the first while loop:-
int j=1; n+1 tests
n loops of 2 units for the two increment (addition)
cout<< “Enter an Integer value”; operations.
cin>>n; while (i<n){ In the second while loop:-
n tests
x++; n-1 increments
--------------------------------------------
i++;
T (n) = 1+1+1+1+1+n+1+2n+n+n-1 = 5n+5 =
} O(n)
while (j<n)
{ j++;
}
}
Example 3
Time Units to Compute
int total (int n)  
{ 1 for the assignment statement: int sum=0
In the for loop:
int sum=0;
1 assignment, (n+1)/2 tests, and n/2 increments.
for (int i=1;i<=n;i+=2) n/2 loops of 2 units for an assignment, and an
sum=sum+i; addition.
1 for the return statement.
return sum;
--------------------------------------------------
} T (n) = 1+ (1+(n+1)/2+n/2) +n/2(1+1)+1
=(4n+7)/2
= O(n)
Example 5
int i=0;
while(i<n)
{
cout<<i;
i++;
}
int j=1;
while(j<=10)
{
cout<<j;
j++;
}
T(n)=1+n+1+n+n+1+11+2(10)
= 3n+34
Cont…
 To determine order of magnitude the following general rules
are applied for time complexity analysis.
 For very large input size lower order terms have insignificant
on the time complexity, so that drop the lower order terms.
eg. if T(n)=n3+n2+6, then its order of magnitude is O(n3)
 For very large input size constant terms have insignificant on
the given time function, so that drop constant values.
eg. T(n)=n2+8 then its order of magnitude is O(n2)
 Running time is the summation of all fragments.
function()
{ int a=2; int b=4;
for (int i=0;i<n;i++) // statements
for(int j=0;j<n;j++)
for(int k=0;k<n;k++) //simple statement
} T (n) =O (1) +O (n) +O (n2) =O (n2)
Cont…
 When there is some condition evaluation we will take the
maximum execution time taken on the fragment given, which is
the worst case.
function()
{
if(somecondition)
for(int i=0;i<n;i++)
// simple statements O(n)
else
for(int j=0;j<n;j++)
for(int k=0;k<n;k++)
O(n2)
//simple statements
O
}
Therefore, from these we will consider the worst case which is O (n2)
ASYMPTOTIC ANALYSIS (Reading Assignment)
 Asymptotic Notations are the expressions that are used to represent
the complexity of an algorithm.

 Asymptotic analysis of an algorithm refers to defining the


mathematical bounding/framing of its run-time performance .

 Asymptotic analysis is input bound i.e., if there's no input to the


algorithm, it is concluded to work in a constant time.

 There are three basic notations used to describe a running time


function. These are:
 Big-Oh Notation (O)
 Big-Omega Notation (Ω)
 Theta Notation (Ө)
Big-Oh Notation
 The notation Ο(n) is the formal way to express the upper bound of an
algorithm's running time.
 It measures the worst case time complexity or the longest amount of
time an algorithm can possibly take to complete .
 It’s only concerned with what happens for very large value of n.
 Formal Definition: f (n) = O (g (n)) if there exist c, no ε ℛ+ such that
for all n≥ no, f (n) ≤c.g (n).
Cont…
Example 1: Show that 4n2 = O(n3).
Solution By definition, we have , 0 ≤ h(n) ≤ cg(n)
Substituting 4n2 as h(n) and n3 as g(n), we get , 0 ≤ 4n2 ≤ cn3
Dividing by n3
0/n3 ≤ 4n2/n3 ≤ cn3/n3
0 ≤ 4/n ≤ c
Note that 4/n → 0 as n → ∞, and 4/n is maximum. Therefore, c=4.
To determine the value of n0,
0 ≤ 4/n0 ≤ 4
0 ≤ 4/4 ≤ n0
There fore, n0 =1
Cont…
Example 2:-Show that 400n3 + 20n2 = O(n3).
Solution By definition, we have 0 ≤ h(n) ≤ cg(n)
Substituting 400n3 + 20n2 as h(n) and n3 as g(n), we get 0 ≤ 400n3 +
20n2 ≤ cn3
Dividing by n3 we get that ,0/n3 ≤ 400n3/n3 + 20n2/n3 ≤ cn3/n3
0 ≤ 400 + 20/n ≤ c
Note that 20/n → 0 as n → ∞, and 20/n is maximum when n = 1.
Therefore,
0 ≤ 400 + 20/1 ≤ c
This means, c = 420 ,To determine the value of n 0,
0 ≤ 400 + 20/n0 ≤ 420
This implies n0 = 1.
Hence, 0 ≤ 400n3 + 20n2 ≤ 420n3 ∀ n ≥ n0=1.
Cont…
Example 3:- Show that f(n)=O(g(n))
f(n)=10n+5 and g(n)=n. To show that f(n) is O(g(n)) we must
show that constants c and k such that
f(n) <=c.g(n) for all n>= n0
Or 10n+5<=c.n for all n>= n0
Try c=15. Then we need to show that 10n+5<=15n
Solving for n we get: 5<5n or 1<=n.
So f(n) =10n+5 <=15.g(n) for all n>=1.
(c=15, n0=1).

 
Exercise
 Show that f(n) is Big Oh of g(n) for the
functions
f(n)=n2+n and g(n)=n2
Big-Omega Notation
 Just as O-notation provides an asymptotic upper bound on a
function, Ω notation provides an asymptotic lower bound.

 It measures the best case time complexity or the best amount


of time an algorithm can possibly take to complete.

 Formal Definition: A function f(n) is Ω ( g (n)) if there exist


constants c and no ε ℛ+ such that f(n) >=c. g(n) for all n>=no.
Example 1
Show that 5n2 + 10n = Ω(n2).
Solution By the definition, we can write
cg(n) ≤ f(n)
cn2 ≤ 5n2 + 10n
Dividing by n2
cn2/n2 ≤ 5n2/n2 + 10n/n2
c ≤ 5 + 10/n
Note that 10/n → 0 as n → ∞, and 10/n is maximum when n = 1.
Therefore,
Therefore, 0 ≤ c ≤ 15.
Hence, c = 15
Now to determine the value of n0
15 ≤ 5 + 10/n0
n0=1
Example 2
 Show that 5n2 + 3n+1 = Ω(n2)
C=9, no=1

 Show that n2 + 100n = Ω(n2)


C=101, no=1
Theta Notation
 A function f (n) belongs to the set of Ө(g(n)) if there exist positive
constants c1 and c2 such that it can be sandwiched between c1.g(n)
and c2.g(n), for sufficiently large values of n.

 Formal Definition: A function f (n) is Ө (g(n)) if it is both


O( g(n) ) and Ω ( g(n) ).
 In other words, there exist constants c1, c2, and no >0 such that
c1.g (n) <=f(n)<=c2. g(n) for all n >= no.

 If f(n)= Ө(g(n)), then g(n) is an asymptotically tight bound for f(n).


 In simple terms, f(n)= Ө(g(n)) means that f(n) and g(n) have the
same rate of growth.
Cont…
Example :- If f(n)=2n+1, then f(n) = Q
(n)

According to the definition of Q


notation:-

f(n)<=C1.g(n)……Big O notation
C2.g(n)<=f(n)……Big Ω notation
Therefore,C1.g(n)<=f(n)<=C2.g(n)

C2.n<=2n+1<=C1.n Divide all by n


C2<=2+1/n<=C1 ,here 2+1/n is maximum
when n=1, and as n → ∞ 2+1/n will be
minimum.
So that , C2<=2+1/1<=C1 <=>
C2<=3<=C1
C2 =2 and C1 =3, for all no >=1
Example 2
Show that f(n)=Ө(g(n))
f(n)=10n2+4n+2 , g(n)=n2

o
C1=10, C2= 16 ,n =1
Common Asymptotic Notations
(1) constant
O(log n) logarithmic
O(n) linear
O(n log n) linear logarithmic,
O(n2) quadratic
O(nc) polynomial or algebraic
O(cn); where c>1 exponential
O(n!) factorial
! !
nd
E
NE
O
ER
PT
H A
C
Searching Algorithm
 Searching is an operation or a technique that helps
finds the place of a given element or value in the list.

 Any search is said to be successful or unsuccessful


depending upon whether the element that is being
searched is found or not.

 Some of the standard searching technique that is


being followed in the data structure is listed below:
 Linear Search or Sequential Search
 Binary Search
 Interpolation search
 Jump search
Linear (Sequential Search)
 Linear search, also called as sequential search, is a very
simple method used for searching an array for a particular
value.
 It works by comparing the value to be searched with every
element of the array one by one in a sequence until a match
is found.
 The linear search is the algorithm of choice for short lists,
because it’s simple and requires minimal code to implement.
 The linear search algorithm looks at the first list item to see
whether you are searching for it and, if so, you are finished.
 If not, it looks at the next item and on through each entry in
the list.
 It is not compulsory to arrange an array in any order
Algorithm
 Let A be an array of n elements, A[0],A[1],A[2], ...... A[n-1]. “data” is
the element to be searched.
 Then this algorithm will find the location “loc” of data in A. Set loc = –
1,if the search is unsuccessful.
1. Input an array A of n elements and “data” to be searched and initialise loc
= – 1.
2. Initialise i = 0; and repeat through step 3 if (i < n) by incrementing i by
one
3. If (data = A[i])
(a) loc = i
(b) GOTO step 4
4. If (loc > 0)
(a) Display “data is found and searching is successful”
5. Else
(a) Display “data is not found and searching is unsuccessful”
6. Exit
Time Complexity 
 Time Complexity of the linear search is found by number of
comparisons made in searching a record.

 In the best case, the desired element is present in the first position
of the array, i.e., only one comparison is made.

So f (n) = O(1).

 In the Average case, the desired element is found in the half


position of the array, then f (n) = O[(n + 1)/2].

 But in the worst case the desired element is present in the nth (or
last) position of the array, so n comparisons are made.

 So f (n) = O(n + 1).


Binary Search
 Binary Search is one of the most fundamental and useful algorithms
in Computer Science. It describes the process of searching for a
specific value in an ordered collection.
 Binary search is a popular algorithm for large databases with records
ordered by numerical key. Example candidates include the IRS
database keyed by social security number.
 Searching a particular name from telephone directory
 Searching a particular word from dictionary
 Binary Search is generally composed of 3 main sections:
Pre-processing —Sort if collection is unsorted.
Binary Search — Using a loop or recursion to divide search space
in half after each comparison.
Post-processing — Determine viable candidates in the remaining
space.
Concept of Binary Search
 1. Find the middle element of the array (i.e., n/2 is the middle
element if the array or the sub-array contains n elements).
 2. Compare the middle element with the data to be searched,
then there are following three cases.
 (a) If it is a desired element, then search is successful.
 (b) If it is less than desired data, then search only the first half
of the array, i.e., the elements which come to the left side of the
middle element.
 (c) If it is greater than the desired data, then search only the
second half of the array, i.e., the elements which come to the
right side of the middle element.
 Repeat the same steps until an element is found or exhaust the
search area.
Algorithm
 Let A be an array of n elements A[0],A[1],A[2],...... A[n-1].
 “Data” is an element to be searched. “mid” denotes the middle
location of a segment (or array or sub-array) of the element of A.
 LB and UB is the lower and upper bound of the array which is
under consideration.
Cont…
1. Input an array A of n elements and “data” to be sorted.
2. LB = 0, UB = n-1; mid = int ((LB+UB)/2)
3. Repeat step 4 and 5 while (LB <= UB) and (A[mid] ! = data)
4. If (data < A[mid])
(a) UB = mid–1
5. Else
(a) LB = mid + 1
6. Mid = int ((LB + UB)/2)
7. If (A[mid]== data)
(a) Display “the data found”
8. Else
(a) Display “the data is not found”
9. Exit
Cont…

key = 11

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
list 2 4 7 10 11 45 50 59 60 66 69 70 79

key < 50 mid


[0] [1] [2] [3] [4] [5]
2 4 7 10 11 45

key > 7 mid

[3] [4] [5]


10 11 45

key = 11 mid
Lab Work
1. Implement sequential (linear search) for numeric
data.

2. Implement Binary search using both iterative


algorithm and recursive algorithm.
Sorting Algorithms
.
Why?
 We need to do sorting for the following reasons :
a) By keeping a data file sorted, we can do binary search on
it.
b) Doing certain operations, like matching data in two
different files, become much faster.

 There are various methods for sorting: Bubble sort,


Insertion sort, Selection sort, Radix sort, Merge sort etc.
Bubble Sort
 In bubble sort, each element is compared with its
adjacent element. If the first element is larger than the
second one, then the positions of the elements are
interchanged, otherwise it is not changed.

 Then next element is compared with its adjacent element


and the same process is repeated for all the elements in
the array until we get a sorted array.

 Let A be a linear array of n numbers. Sorting of A means


rearranging the elements of A so that they are in order.
Here we are dealing with ascending order. i.e., A[0] < A[1]
< A[2]< ...... A[n-1].
Cont…
 Suppose the list of numbers A[0], A[1], ………… A[n-1] is an
element of array A.
 The bubble sort algorithm works as follows:
 Step 1: Compare A[0] and A[1] and arrange them in the
( desired) ascending order or descending order, so that
A[0] < A[1] or A[0]>A[1].
 That is if A[0] is greater than A[1] then interchange the
position of data. by swap = A[0]; A[0] = A[1]; A[1] = swap.
Then compare A[1] and A[2] and arrange them so that A[1]
< A[2]. Continue the process until we compare A[N – 2]
with A[N-1].
Cont…
 Step1 contains n – 1 comparisons i.e., the largest
element is “bubbled up” to the nth position or “sinks” to
the nth position. When step 1 is completed A[N-1] will
contain the largest element.

 Step 2: Repeat step 1 with one less comparisons that is,


now stop comparison at A [n – 1] and possibly rearrange
A[N – 2] and A[N – 1] and so on.
Cont…

Bubble sort: beginning of first pass


Cont…
Cont…

Bubble sort: end of First pass


Algorithm
 Let A be a linear array of n numbers. Swap is a temporary
variable for swapping (or interchange) the position of the
numbers.
1. Input n numbers of an array A
2. Initialise i = 0 and repeat through step 4 if (i < n)
3. Initialize j = 0 and repeat through step 4 if (j < n – i – 1)
4. If (A[j] > A[j + 1])
(a) Swap = A[j]
(b) A[j] = A[j + 1]
(c) A[j + 1] = Swap
5. Display the sorted numbers of array A
6. Exit.
Selection Sort
 Selection sort algorithm finds the smallest element of the
array and interchanges it with the element in the first
position of the array.
 Then it finds the second smallest element from the
remaining elements in the array and places it in the
second position of the array and so on.
 Let A be a linear array of ‘n’ numbers, A [0], A [1], A
[2],...... A [n-1].
 Step 1: Find the smallest element in the array of n
numbers A[0], A[1], ...... A[n-1].
 Let LOC is the location of the smallest number in the
array. Then interchange A[LOC] and A[0]by swap =
A[LOC]; A[LOC] = A[0]; A[0] = Swap.
Cont…
 Step 2: Find the second smallest number in the sub list of
n – 1 elements A [1] A [2]...... A [n – 1] (first element is
already sorted).
 Now we concentrate on the rest of the elements in the
array.
 Again A [LOC] is the smallest element in the remaining
array and LOC the corresponding location then
interchange A [LOC] and A [1].Now A [0] and A [1] is
sorted, since A [0] less than or equal to A [1].
 Step 3: Repeat the process by reducing one element each
from the array
Cont…
Algorithm
1. Input n numbers of an array A
2. Initialize i = 0 and repeat through step5 if (i < n-1)
(a) min = a[i]
(b) loc = i
3.Initialize j = i + 1 and repeat through step 4 if (j < n – 1)
4. if (a[j] < min)
(a) min = a[j]
(b) loc = j
5. if (loc ! = i)
(a) swap = a[i]
(b) a[i] = a[loc]
(c) a[loc] = swap
6. Display “the sorted numbers of array A”
7. Exit
3. INSERTION SORT
 Insertion sort algorithm sorts a set of values by inserting
values into an existing sorted file.

 Compare the second element with first, if the first element is


greater than second, place it before the first one, otherwise
place is just after the first one.

 Compare the third value with second. If the third value is


greater than the second value then place it just after the
second. Otherwise place the second value to the third place.

 And compare third value with the first value. If the third
value is greater than the first value place the third value to
second place, otherwise place the first value to second place.
 And place the third value to first place and so on.
Cont…
 Let A be a linear array of n numbers A [0], A [1], A [2], ......
A[n-1].
 The algorithm scan the array A from A [0] to A [n-1], by
inserting each element A[k], into the proper position of the
previously sorted sub list. A [0], A [1], A [2], ...... A [k – 1]
 Step 1: As the single element A [0] by itself is sorted array.
 Step 2:A [1] is inserted either before or after A [0] by
comparing it so that A[0], A[1] is sorted array.
 Step 3: A [2] is inserted into the proper place in A [0], A [1],
that is A [2] will be compared with A [0] and A [1] and
placed before A [0], between A [0] and A [1], or after A [1]
so that A [0], A [1], A [2] is a sorted array.
Cont…
 Step 4: A [3] is inserted in to a proper place in A [0], A [1],
A [2] by comparing it; so that A [0], A [1], A [2], A [3] is a
sorted array.

 Step 5: Repeat the process by inserting the element in the


proper place in array.

 Step n : A [n-1] is inserted into its proper place in an array


A [0], A [1], A [2], ...... A [n –1] so that A [0], A [1], A
[2], ...... ,A [n-1] is a sorted array.
Cont…
Cont…
Cont…
Algorithm
 Let A be a linear array of n numbers A [0], A [1], A [2],..,A [n-1]...
 Temp be a temporary variable to interchange the two values.
1. Input an array A of n numbers
2. Initialize i = 1 to n repeat through steps 4 by incrementing i by one.
(a) Temp = A [i],
(b) j = i – 1
3. Repeat the step 3 if (Temp < A[j] and (j >= 0))
(a) A [j+1] = A [j]
(b) j = j-1
4. A [j +1] = Temp
5. Exit
CHAPTER THREE

LINKED LIST
Review on Pointers
 When a variable is declared, a specific block of memory
within the computer is allocated to hold the value of that
variable.

 The size of the allocated block depends on the data type.

 In C++, every value is stored somewhere in memory and can


therefore be identified with that address. Such addresses are
called pointers.

 A pointer is a variable which stores the address of another


variable.
Cont…
 The only difference between pointer variable and regular
variable is the data they hold.
 A pointer is simply the address of an object in memory.
 Generally, objects can be accessed in two ways: directly by
their symbolic name, or indirectly through a pointer.
 We will use terms rvalue and lvalue for the value and the
address of the variable, respectively.
 The rvalue appears on the right side of the assignment
statement while lvalue on the left.
 Therefore, writing 10 = k; is illegal.
 If we write,
int x, y;
x = 10;
y = x; then, we have two integer variables x and y.
Declaring Pointers
 Like all variables, pointers must be declared before they can
be used to store an address.
 When you declare a pointer variable, C++ requires also
specifying the type of variable that’s pointed to.
 The general syntax of declaring pointer variables can be
given as below.
Syntax: data_ type * pointer_name ;
 Here, data_type is the data type of the value that the
pointer will point to. For example,
int *pnum;
char *pch;
float *pfnum;

 In each of the above statements, a pointer variable is


declared to point to a variable of the specified data type.
Pointer Operators
 C++ includes two built-in operators for working with pointers:
 The address-of operator (&) is written before a variable name (or any
expression to which you could assign a value) and returns the address of that
variable.
 The dereference operator (*) is written before a pointer expression and
returns the actual value to which the pointer points.

 Suppose, for example, that you have declared and initialized the following
variables:
double x = 2.5;
double *px = &x;

 At this point, the variable px points to the variable x, and the expression *px
is synonymous with the variable x.
Assigning values to pointers
 Assigning values to pointers is similar to that of assigning values to normal
variable, except that the variable is pointer variable.
int main()
Output
{ The address stored in numAddr is 0012FEC8
int *numAddr; The value pointed to by numAddr is 22
The address now stored in numAddr is
int miles, dist;
0012FEBC
dist = 158; The value now pointed to by numAddr is 158
miles = 22;
numAddr = &miles;
cout << "The address stored in numAddr is " << numAddr << endl;
cout << "The value pointed to by numAddr is " << *numAddr << "\n\n";
numAddr = &dist; // now store the address of dist in numAddr
cout << "The address now stored in numAddr is " << numAddr << endl;
cout << "The value now pointed to by numAddr is " << *numAddr << endl;
return 0;
}
Pointer Arithmetic
 As we know pointer stores the address of another
variable, memory location we can perform the following
operations.
 Pointer increment ,p++ or ++p
 Pointer decrement, p- - or --p
 Constant addition, p+k
 Constant subtraction , p-k
Pointer Increment/Decrement
 If p is a pointer then ++p,p++,p– and --p is possible with pointer.
 Pointer increment, p++ or ++p does not mean adding 1 to the pointer but
adding the size of that data type to the pointer p the same for p-- or--p
eg int x,*p,*q;
short int y;
p=&x;
q =&y;
++q;
p++;

 Here if both p and q has an address of 1000 after the statement p=&x
then p++ will be 1004 which is incrementing the size of (x) and ++q will
be 1002 ;
Null Pointers
 A pointer variable is a pointer to a variable of some data type.
 However, in some cases, we may prefer to have a null pointer which
is a special pointer value and does not point to any value.
 This means that a null pointer does not point to any valid memory
address.
 To declare a null pointer, you may use the predefined constant
NULL
int *ptr = NULL;
 You can always check whether a given pointer variable stores the
address of some variable or contains NULL by writing,
if (ptr == NULL)
{
Statement block;
}
Introduction to Linked List
 Array is a linear collection of data elements in which the
elements are stored in consecutive memory locations.
 While declaring arrays, we have to specify the size of the array,
which will restrict the number of elements that the array can
store.
 For example, if we declare an array as int marks[10], then the
array can store a maximum of 10 data elements but not more
than that.
 But what if we are not sure of the number of elements in
advance?
 Moreover, to make efficient use of memory, the elements must
be stored randomly at any location rather than in consecutive
locations.
Cont…
 So, there must be a data structure that removes the restrictions on the
maximum number of elements and the storage condition to write
efficient programs.

 Linked list is a data structure that is free from the aforementioned


restrictions.

 A linked list does not store its elements in consecutive memory


locations and the user can add any number of elements to it.

 However, unlike an array, a linked list does not allow random access
of data.

 Elements in a linked list can be accessed only in a sequential manner.


Basic terminologies
 A linked list, in simple terms, is a linear collection of data
elements.
 These data elements are called nodes.
 Linked list is a data structure which in turn can be used to
implement other data structures as stacks, queues, and their
variations.
 A linked list can be perceived as a train or a sequence of
nodes in which each node contains one or more data fields
and a pointer to the next node.
START

1 2 3 4 5 6 7 X
Cont…
 Every node contains two parts, an integer and a pointer to the
next node.
Info Link
 The left part of the node which contains data may include a
simple data type, an array, or a structure.

 The right part of the node contains a pointer to the next node
(or address of the next node in sequence).

 The last node will have no next node connected to it, so it will
store a special value called NULL.

 In the above fig, the NULL pointer is represented by X ie


address part of 7.
Cont…
 Since in a linked list, every node contains a pointer to another
node which is of the same type, it is also called a self-
referential data type.

 Linked lists contain a pointer variable START that stores the


address of the first node in the list.

 If START = NULL, then the linked list is empty and contains


no nodes.
REPRESENTATION OF LINKED LIST
In C++, we can implement a linked list using the following code:
struct node
{
int data;
node *next;
};
In order to form a linked list, we need a structure called node
which has two fields, DATA and NEXT.
DATA will store the information part and NEXT will store the
address of the next node in sequence.
Cont…
 In this example, START = 1, so the first data is stored at address 1, which is H.
The corresponding NEXT stores the address of the next node, which is 4. So,
we will look at address 4 to fetch the next data item.
 The second data element obtained from address 4 is E. Again, we see the
corresponding NEXT to go to the next node.
 From the entry in the NEXT, we get the next address, that is 7, and fetch L as
the data.
 When we traverse DATA and NEXT in this manner, we finally see that the
linked list in the above example stores characters that when put together form
the word HELLO.

START
1 Data Next
1 H 4
2
3
4 E 7
5
6
7 L 8
8 L 1
9
1 O –1
ADVANTAGES AND DISADVANTAGES
Linked list have many advantages and some of them are:
 Linked list are dynamic data structure. That is, they can grow or shrink
during the execution of a program.
 Efficient memory utilization: In linked list (or dynamic) representation,
memory is not pre-allocated. Memory is allocated whenever it is
required. And it is deallocated (or removed) when it is not needed.
 Insertion and deletion are easier and efficient. Linked list provides
flexibility in inserting a data item at a specified position and deletion of
a data item from the given position.
 Many complex applications can be easily carried out with linked list.
Linked list has following disadvantages
 More memory: to store an integer number, a node with integer data and
address
field is allocated. That is more memory space is needed.
 An element can not be accessed randomly
OPERATION ON LINKED LIST
 Creation operation is used to create a linked list. Once a linked list is
created with one node, insertion operation can be used to add more
elements in a node.
 Insertion operation is used to insert a new node at any specified
location in the linked list. A new node may be inserted.
• At the beginning of the linked list
• At the end of the linked list
• At any specified position in between in a linked list
 Deletion operation is used to delete an item (or node) from the linked
list. A node may be deleted from the
• Beginning of a linked list
• End of a linked list
• Specified location of the linked list
Cont…
 Traversing is the process of going through all the nodes from
one end to another end of a linked list.
 In a singly linked list we can visit from left to right, forward
traversing, and nodes only.
 But in doubly linked list forward and backward traversing is
possible.
 Concatenation is the process of appending the second list to the
end of the first list.
 Consider a list A having n nodes and B with m nodes. Then the
operation concatenation will place the 1st node of B in the
(n+1)th node in A. After concatenation A will contain ( n+m)
nodes
TYPES OF LINKED LIST
 Singly linked list
 Doubly linked list
 Circular linked list
Singly linked list
 A singly linked list is the simplest type of linked list in
which every node contains some data and a pointer to the
next node of the same data type.
 By saying that the node contains a pointer to the next node,
we mean that the node stores the address of the next node in
sequence.
 A singly linked list allows traversal of data only in one way.
Operations on Singly Linked list
1. Inserting a new node in a Linked List
 four cases
Case 1: The new node is inserted at the beginning.
Case 2: The new node is inserted at the end.
Case 3: The new node is inserted after a given node.
Case 4: The new node is inserted before a given node.

Suppose START is the first position in linked list. Let DATA be


the element to be inserted in the new node and LINK is a pointer
which holds the address of the next node in sequence. POS is the
position where the new node is to be inserted. TEMP is a
temporary pointer to hold the node address.
Case 1: Insert a Node at the beginning
1. Input DATA to be inserted
2. Create a NewNode
3. NewNode → DATA = DATA
4. If (SATRT equal to NULL )
(a) NewNode → Link = NULL
(b) Go to step 6
5. Else
(a) NewNode → Link = START
6. START = NewNode
7. Exit
Case 2: Insert a Node at the end
Algorithm
1. Input DATA to be inserted
2. Create a NewNode
3. NewNode → DATA = DATA
4. NewNode → Next = NULL
5. If (SATRT equal to NULL )
a) START = NewNode and Go to step 7
6. Else
a) TEMP = START
b) While (TEMP → Next not equal to NULL)
(i) TEMP = TEMP → Next
c) TEMP → Next = NewNode
7. Exit
Cont…
2. Deleting a Node from a Linked List
 Deletion of a node from the linked list can be:-
Case 1: The first node is deleted.
Case 2: The last node is deleted.
Case 3: The node at a specified position.
Case 1: Deleting the First Node from a Linked List
 Algorithm
Step 1: IF START = NULL
a) Write UNDERFLOW
b) Go to Step 5
Step 2: If (Start->next eqaul to NULL)
a) PTR=Start;
b) Start=NULL
Step 3: a) SET PTR = START
b) SET START = START-> NEXT
Step 4: FREE PTR
Step 5: EXIT
Case 2:-Deleting the Last Node from a Linked List
 Algorithm
Step 1: If START = NULL
a) Write the list is empty
b) Go to Step 9
Step 2: If (Start->next eqaul to NULL)
a) PTR=Start;
b) Start=NULL
Step 3: SET PTR = START
Step 4: Repeat Steps 4 and 5 while PTR->NEXT! = NULL
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR ->NEXT
Step 7: SET PREPTR ->NEXT = NULL
Step 8: FREE PTR
Step 9: EXIT
3. Traversing Linked list nodes

 Traversing a linked list means accessing the nodes of the list in order
to perform some processing on them.
Algorithm
1. If (START is equal to NULL)
(a) Display “The list is Empty”
(b) Exit
2. Initialize TEMP = START
3. Repeat the step 4 and 5 until (TEMP == NULL)
4. Display “TEMP → DATA”
5. TEMP = TEMP → Next
6. Exit
 
4. Counting Linked List nodes
 To count nodes in the linked lists ,traverse each and every node of the list and
while traversing every individual node, we will increment the counter by 1.
 Once we reach NULL, that is, when all the nodes of the linked list have been
traversed, the final value of the counter will be displayed.
Algorithm
Step 1: [INITIALIZE] SET COUNT =0
Step 2: [INITIALIZE] SET PTR = START
Step 3: Repeat Steps 4 and 5 while PTR! = NULL
Step 4: SET COUNT = +1
Step 5: SET PTR = PTR-> NEXT
[END OF LOOP]
Step 6: Write COUNT
Step 7: EXIT
5. Searching for a value in a Linked List
 Searching means finding whether a given value is present in the
information part of the node or not.
 If it is present, the algorithm returns the address of the node that
contains the value.
Algorithm
1. Input the DATA to be searched
2. Initialize TEMP = START; POS =1;
3. Repeat the step 4, 5 and 6 until (TEMP is equal to NULL)
4. If (TEMP → DATA is equal to DATA)
(a) Display “The data is found at POS”
(b) Exit
5. TEMP = TEMP → Next
6. POS = POS+1
7. If (TEMP is equal to NULL)
(a) Display “The data is not found in the list”
8. Exit
Doubly Linked List
 A doubly linked list is one in which all nodes are linked together by
multiple links which help in accessing both the successor (next) and
predecessor (previous) node for any arbitrary node within the list.
 Every nodes in the doubly linked list has three fields: Left_Pointer,
Right _Pointer and DATA.
 

 Doubly Linked List is a variation of Linked list in which navigation is


possible in both ways, either forward or backward easily as compared
to Single Linked List.
 LPoint will point to the node in the left side (or previous node) that is
LPoint will hold the address of the previous node.
 RPoint will point to the node in the right side (or next node) that is it
hold the address of the next node.
 Data will store the information of the node
Representation of Doubly Linked List (DLL)
 In C++, the structure of a doubly linked list can be given as,
struct node
{
node *prev;
int data;
node *next;
};
 The PREV field of the first node and the NEXT field of the last node will
contain NULL.
 The PREV field is used to store the address of the preceding node, which
enables us to traverse the list in the backward direction.
Creating Doubly Linked Lists
 The nodes for a doubly linked list would be defined as follows:
struct node{
char name[20];
node *nxt; // Pointer to next node
node *prv; // Pointer to previous node
};
node *head=NULL;
node *tail=NULL;
node *current;
current = new node;
current->name = “helen";
Cont…
 In this example, START = 1, so the first START

data is stored at address 1, which is H. 1 DATA PREV NEXT

 Since this is the first node, it has no 1 H NULL 3

previous node and hence stores NULL


2
or –1 in the PREV field. We will
traverse the list until we reach a position 3 E 1 6

where the NEXT entry contains –1 or 4


NULL. This denotes the end of the
linked list. 5

 When we traverse the DATA and NEXT 6 L 3 7

in this manner, we will finally see that 7 L 6 9

the linked list in the above example


8
stores characters that when put together
form the word HELLO. 9 O 7 NULL
Operations on DLL
Inserting a new node in a doubly linked list
There are four cases:-

Case 1: The new node is inserted at the beginning

Case 2: The new node is inserted at the end

Case 3: The new node is inserted after a given node

Case 4: The new node is inserted before a given node


Case 1: The new node is inserted at the beginning
Step 1: Input the data to be stored on the linked list
Step 2: Create a new node, store the address of this node in temp.
temp = new node;
Step3: temp->prev=null;
Step4: if there is node before, ie if head=null
a) temp->next=null;
b) tail=temp;
Step 5: Else
a) head->prev=temp;
b) temp->next=head;
Step 6: head=temp;
Step 7: Exit
Case 2: The new node is inserted at the end
Step 1: Input data to be inserted
Step 2: Create a new node, store the address of this node in temp
temp = new node;
temp->next = null;
Step 3: If no node before ie, head==null
a) temp->prev=null;
b) head=temp
c) tail=temp;
Step 4: Else a) SET temp1 = head;
b) while (temp1->next != null)
i) temp1 = temp1->next;
Step 5: temp->prev = temp1;
Step 6: temp1->next = temp;
Step 7: tail=temp
Step 8: Exit
Deleting a node from a doubly linked list

Like that of singly linked list the following cases will be


applied to delete in doubly linked list

Case 1: The first node is deleted

Case 2: The last node is deleted

Case 3: The node after a given node is deleted

Case 4: The node before a given node is deleted


Case 1: Deleting from Beginning

Step 1: If head = null


a) List is empty
b) Go to Step 5
Step 2: If the linked list have only one node (head -> next == null)
ptr = head
head = null
tail=head

Step 3: Else
a) ptr = head
b) head=head->next
c) head->prev=null
d) ptr->next=null
Step 4: free(ptr);
Step 5: Exit
Case 1: Deleting from End
Step 1: If head =null
a) Write the list empty
b) Go to Step 8
Step 2: If (head->next==null)
a) SET PTR=head
b) head = null
c) tail=null
Step 3: Else
a) ptr = head
b) ptr2=ptr->prev
Step 4 while (ptr->next! =null) do the following
a) ptr2=ptr
b) ptr = ptr->next
Step 6: a) ptr ->prev = null
b) ptr2->next=null
c) tail=ptr2
Step 7: free(ptr)
Step 8: Exit
Forward and backward Traversal
A) Forward Traversal
1. If (head is equal to null)
(a) Display “The list is Empty”
(b) Exit
2. Initialize temp=head
3. Repeat the step 4 and 5 until (temp == null)
4. Display “temp → DATA”
5. temp = temp→ Next
6. Exit
 
B) Backward Traversal
1. If (tail is equal to null)
(a) Display “The list is Empty”
(b) Exit
2. Initialize temp=tail
3. Repeat the step 4 and 5 until (temp == null)
4. Display “temp → DATA”
5. temp = temp→ Prev
6. Exit
CHAPTER FOUR

STACK AND QUEUE


Stack ADT
 What is a Stack?
A stack is a data structure of ordered items such
that items can be inserted and removed only at
one end.
 What can we do with a stack?
 push - place an item on the stack
 peek - Look at the item on top of the stack, but
do not remove it.
 pop- Look at the item on top of the stack and
remove it
Stack Analogy
Implementation of Stack
Stack can be implemented in two ways:
1. Static implementation (using arrays)
2. Dynamic implementation (using pointers)
Static implementation uses arrays to create stack. Static
implementation using arrays is a very simple technique
but is not a flexible way, as the size of the stack has to be
declared during the program design, because after that,
the size cannot be varied (i.e. Increased or decreased).
Moreover static implementation is not an efficient
method when resource optimization is concerned (i.e.,
memory utilization).
For example a stack is implemented with array size 50.
Cont…
 That is before the stack operation begins, memory is
allocated for the array of size 50.

 Now if there are only few elements (say 30) to be stored


in the stack, then rest of the statically allocated memory
(in this case 20) will be wasted, on the other hand if
there are more number of elements to be stored in the
stack (say 60) then we cannot change the size array to
increase its capacity.

 The above said limitations can be overcome by


dynamically implementing (is also called linked list
representation) the stack using pointers.
Stack using Array
 Suppose STACK[SIZE] is a one dimensional
array for implementing the stack, which will
hold the data items. TOP is the pointer that
points to the top most element of the stack.
Let DATA is the data item to be pushed or
popped.
Algorithm for push
1. If TOP = SIZE – 1, then:
(a) Display “The stack is in overflow condition”
(b) Exit
2. TOP = TOP + 1
3. STACK [TOP] = ITEM
4. Exit
Algorithm for pop
1. If TOP < 0, then
(a) Display “The Stack is empty”
(b) Exit
2. Else remove the Top most element
3. DATA = STACK[TOP]
4. TOP = TOP – 1
5. Exit
 Problem:
 What happens if we try to pop an item off the
stack when the stack is empty?
 What happen if we try to push an item on to
the stack when the stack is full?
Stack using Linked List
 In a linked stack, every node has two part one that stores
data and another that stores the address of the next node.
 The START pointer of the linked list is used as TOP, all
insertions and deletions are done at the node pointed by
TOP.
 If TOP = NULL, then it indicates that the stack is
empty.
Push Operation
 To insert an element with value 9 to the previous list , we first
check if TOP=NULL
 If this is the case, then we allocate memory for a new node,
store the value in its DATA part and NULL in its NEXT part.
The new node will then be called TOP.
 However, if TOP! =NULL, then we insert the new node at the
beginning of the linked stack and name this new node as TOP.
Push Operation Algorithm
Suppose TOP is a pointer, which is pointing towards the
topmost element of the stack. TOP is NULL when the stack is
empty. DATA is the data item to be pushed.
1. Input the DATA to be pushed
2. Create a New Node
3. NewNode → DATA = DATA
4. NewNode → Next = TOP
5. TOP = NewNode
6. Exit
Pop operation
 The pop operation is used to delete the topmost
element from a stack.
 However, before deleting the value, we must first
check if TOP=NULL, because if this is the case, then
it means that the stack is empty and no more
deletions can be done.
 If an attempt is made to delete a value from a stack
that is already empty, an UNDERFLOW message is
printed.
Pop operation Algorithm
Suppose TOP is a pointer, which is pointing towards the topmost
element of the stack. TOP is NULL when the stack is empty.
TEMP is pointer variable to hold any nodes address. DATA is the
information on the node which is just deleted.
1. if (TOP is equal to NULL)
(a) Display “The stack is empty”
2. Else
(a) TEMP = TOP
(b) Display “The popped element TOP → DATA”
(c) TOP = TOP → Next
(d) TEMP → Next = NULL
(e) Free the TEMP node
3. Exit
Applications of Stack
 Reversing a string
 System start up and shutdown
 Function calling
 Undo mechanism in editor
 Internet web browsers searching for visited site
 Expression evaluation
 Implementation of DFS
Reversing a List
 A list of numbers can be reversed by reading each
number from an array starting from the first index and
pushing it on a stack.

 Once all the numbers have been read, the numbers can
be popped one at a time and then stored in the array
starting from the first index.
 Evaluation of Arithmetic Expressions
Algebraic Expression
An algebraic expression is a legal combination of
operands and the operators.
Operand is the quantity (unit of data) on which a
mathematical operation is performed. Operand may be a
variable like x, y, z or a constant like 5, 4,0,9,1 etc.
Operator is a symbol which signifies a mathematical or
logical operation between the operands. Example of
familiar operators include +,-,*, /, ^
An example of expression as x+y*z.
Infix, Postfix and Prefix Expressions
 INFIX: the expressions in which operands surround the
operator, e.g. x+y, 6*3 etc this way of writing the
expressions is called infix notation.
 POSTFIX: Postfix notation are also Known as Reverse
Polish Notation (RPN).
 They are different from the infix and prefix notations in the
sense that in the postfix notation, operator comes after the
operands, e.g. xy+,xyz+* etc.
 PREFIX: Prefix notation also Known as Polish notation.
 In the prefix notation, operator comes before the operands,
e.g. +xy, *+xyz etc.
Advantages of using postfix notation
 Human beings are quite used to work with mathematical
expressions in infix notation, which is rather complex.
 One has to remember a set of nontrivial rules while using this
notation and it must be applied to expressions in order to
determine the final value.
 These rules include precedence, BODMAS, and associativity.
 Using infix notation, one cannot tell the order in which
operators should be applied.
 Whenever an infix expression consists of more than one
operator, the precedence rules (BODMAS) should be applied
to decide which operator (and operand associated with that
operator) is evaluated first.
Cont…
 But in a postfix expression operands appear before the operator, so there
is no need for operator precedence and other rules.
Notation Conversions
 Let A + B * C be the given expression, which is an infix notation. To
calculate this expression for values 4, 3, 7 for A, B, C respectively we
must follow certain rule (called BODMAS in general mathematics) in
order to have the right result.
For example: A + B * C = 4 + 3 * 7 = 7 * 7 = 49
 The answer is not correct; multiplication is to be done before the
addition, because multiplication has higher precedence over addition.
 An expression is calculated according to the operator’s precedence not
the order as they look like.
 The error in the above calculation occurred, since there were no braces
to define the precedence of the operators.
Converting Infix to Postfix Expression

 The method of converting infix expression A + B *


C to postfix form is:
A+B*C Infix Form
A + (B * C) Parenthesized expression
A + (B C *) Convert the multiplication
A (B C *) + Convert the addition
ABC*+ Postfix form
Cont…
 The rules to be remembered during infix to postfix
conversion are:
1. Parenthesize the expression starting from left to light.
2.During parenthesizing the expression, the operands
associated with operator having higher precedence are first
parenthesized.
For example in the above expression B * C is
parenthesized first before A + B.
3. The sub-expression (part of expression), which has been
converted into postfix, is to be treated as single operand.
4. Once the expression is converted to postfix form, remove
the parenthesis.
Algorithm
1. Push “(” onto stack, and add“)” to the end of P.
2. Scan P from left to right and repeat Steps 3 to 6 for each element of P until the
stack is empty.
3. If an operand is encountered, add it to Q.
4. If a left parenthesis is encountered, push it onto stack.
5. If an operator ⊗ is encountered, then:
a) Repeatedly pop from stack and add P each operator (on the top of stack),
which has the same precedence as, or higher precedence than ⊗.
b) Add ⊗ to stack.
6. If a right parenthesis is encountered, then:
a) Repeatedly pop from stack and add to P (on the top of stack until a left
parenthesis is encountered.
b) Remove the left parenthesis.
7. Exit.
Note. Special character ⊗ is used to symbolize any operator in P.
Example
 Consider the following arithmetic infix expression P
a) P=A+(B/C-(D*E^F)+G)*H
b) P=A – (B / C + (D % E * F) / G)* H
c) P= 2*3/(2-1)+5*3

Answer:
a) I= A B C / D E F ^ * - G + H * +
b) I= A B C / D E F *%G /+H
c) I= 23*21-/53*+
Queue
 Data structure in which the elements are added at one end,
called the rear, and deleted from the other end, called the
front
 A queue is a First In First Out data structure
 As in a stack, the middle elements of the queue are
inaccessible.
 The basic operations that can be performed on queue are
1. Insert (or add) an element to the queue (en-queue)
2. Delete (or remove) an element from a queue (de-queue)
Real Life example of Queue
  A queue of people at ticket-window
 Telephone waiting call
 Vehicles on toll-tax bridge
Applications of Queue
 Round Robin scheduling
 Job scheduling (FIFO)
 Printer server routines (in drivers) are designed using queue
 All types of customer service software (like Railway/Air tic
reservation) are designed using queue to give proper service
the customers.
Algorithms of Queue Operations Using Array
Let Q be the array of some specified size say SIZE
INSERTING AN ELEMENT INTO THE QUEUE
1. Initialize front=-1 rear = –1
2. Input the value to be inserted and assign to variable “data”
3. If (rear = SIZE-1)
(a) Display “Queue overflow”
(b) Exit
4. Else
if(Rear==-1)
front=rear=0;
else
rear = rear +1
5. Q[rear] = data
6. Exit
Cont..
DELETING AN ELEMENT FROM QUEUE
1. If (front==-1) or (front>rear)
(a) Display “The queue is empty”
(c) Exit
2. Else
(a) data = Q[front]
3. front = front +1
4. Exit
Algorithms of Queue Operations Using Linked List
 ALGORITHM FOR PUSHING AN ELEMENT TO A QUEUE
REAR is a pointer in queue where the new elements are added.
FRONT is a pointer, which is pointing to the queue where the
elements are popped. DATA is an element to be pushed.
1. Input the DATA element to be pushed
2. Create a New Node
3. NewNode → DATA = DATA
4. NewNode → Next = NULL why??
5. If(REAR not equal to NULL)
(a) REAR → next = NewNode;
6. REAR =NewNode;
7. Exit
ALGORITHM FOR POPPING AN ELEMENT FROM A QUEUE
REAR is a pointer in queue where the new elements are added.
FRONT is a pointer, which is pointing to the queue where the
elements are popped. DATA is an element popped from the queue.
1. If (FRONT is equal to NULL)
(a) Display “The Queue is empty”
2. Else
(a) Display “The popped element is FRONT → DATA”
(b) If(FRONT is not equal to REAR) what does it mean?
(i) FRONT = FRONT → Next why??
(c) Else
(d) FRONT = NULL;
3. Exit
Circular Queue
 Suppose a queue Q has maximum size 5, say 5 elements
pushed and 2 elements popped.

 Now if we attempt to add more elements, even though 2


queue cells are free, the elements cannot be pushed.

 Because in a queue, elements are always inserted at the


rear end and hence rear points to last location of the
queue array Q[4].

 That is queue is full (overflow condition) though it is


empty. This limitation can be overcome if we use circular
queue.
Cont…
 In circular queues the elements Q[0],Q[1],Q[2] .... Q[n – 1] is
represented in a circular fashion with Q[0] following Q[n-1].
 A circular queue is one in which the insertion of a new
element is done at the very first location of the queue if the
last location at the queue is full.
 Suppose Q is a queue array of 6 elements. Push and pop
operation can be performed on circular.
 The following figures will illustrate the same.

A circular queue after inserting 18, 7, 42, 67.


Cont…
.

A circular queue after popping 18, 7

 After inserting an element at last location Q[5], the next


element will be inserted at the very first location (i.e., Q[0])
that is circular queue is one in which the first element comes
just after the last element.

A circular queue after pushing 30, 47, 14


Cont….
 At any time the position of the element to be inserted
will be calculated by the relation
Rear = (Rear + 1) % SIZE
 After deleting an element from circular queue the
position of the front end is calculated by the relation
Front= (Front + 1) % SIZE
 After locating the position of the new element to be
inserted, rear, compare it with front.
 If (rear = front), the queue is full and cannot be
inserted anymore.
Algorithm
Inserting an element to circular Queue
1. Initialize FRONT = – 1; REAR = -1
2. If ((Front equal to 0 AND Rear equal to MAX-1) OR (Front equal to rear +1))
(a) Display “Queue is full”
(b) Exit
4. Else
(a) Input the value to be inserted and assign to variable “DATA”
5. If (FRONT is equal to – 1)
FRONT =0
6. Else
Rear=(Rear+1)%SIZE
7. Q[REAR] = DATA
8. Repeat steps 2 to 5 if we want to insert more elements
9. Exit
Cont…
Deleting an element from a circular queue
1. If (FRONT is equal to – 1)
(a) Display “Queue is empty”
(b) Exit
2. Else
(a) DATA = Q[FRONT]
3. If (REAR is equal to FRONT)
(a) FRONT = –1
(b) REAR = –1
4. Else
(a)FRONT = (FRONT +1) %SIZE
(b)5. Exit
DEQUES
 A deque is a homogeneous list in which elements can be added or
inserted (called enque operation) and deleted or removed from
both the ends (which is called dequeue operation).
 We can add a new element at the rear or front end and also we
can remove an element from both front and rear end. Hence it is
called Double Ended Queue.

There are two types of deque depending upon the restriction to perform insertion
or deletion operations at the two ends.
1. Input restricted deque
2. Output restricted deque
Cont…
 An input restricted deque is a deque, which allows
insertion at only one end, rear end, but allows deletion
at both ends, rear and front end of the lists.
 An output-restricted deque is a deque, which allows
deletion at only one end, front end, but allows insertion
at both ends, rear and front ends, of the lists.

The possible operation performed on deque is


1. Add an element at the rear end
2. Add an element at the front end
3. Delete an element from the front end
4. Delete an element from the rear end
Only 1st, 3rd and 4th operations are performed by input-
restricted deque and 1st, 2nd and 3rd operations are
performed by output-restricted deque.
Algorithm
INSERT AN ELEMENT AT THE RIGHT SIDE OF THE DE-QUEUE
1. Input the DATA to be inserted
2. If ((Front == 0 && Rear == MAX–1) || (Front== Rear + 1))
(a) Display “Queue Overflow”
(b) Exit
3. If (Front== –1)
(a) Front = 0
(b) Rear = 0
4. Else
(i) Rear =( Rear+1)%SIZE
5. Q[Rear] = DATA
6. Exit
INSERT AN ELEMENT AT THE LEFT SIDE OF THE DE-QUEUE

1. Input the DATA to be inserted


2. If ((Front == 0 && Rear == MAX–1) || (Front== Rear +1))
(a) Display “Queue Overflow”
(b) Exit
3. If (Front == – 1)
(a) Front = 0
(b) Rear = 0
4. Else
(a) if (Front == 0)
(i) Front = MAX – 1
(b) else
(i) Front = Front – 1
5. Q[Font]=DATA
6.Exit
DELETE AN ELEMENT FROM THE RIGHT SIDE OF THE DE-
QUEUE
1.If (Front == – 1)
(a) Display Queue Underflow
(b) Exit
2. DATA = Q [Rear]
3. If (Front == Rear)
(a) Front = – 1
(b) Rear = – 1
4. Else
(a) if(Rear == 0)
(i) Rear = MAX-1
(b) else
(i) Rear = Rear -1
5. Exit
DELETE AN ELEMENT FROM THE LEFT SIDE OF THE DE-QUEUE

1. If (Front == – 1)
(a) Display “Queue Underflow”
(b) Exit
2. DATA = Q [Front
3. If(Front == Rear)
(a) Front = – 1
(b) Rear = – 1
4. Else
(i) Front = (Front +1)%SIZE
5. Exit
 Tree.pptx
A directed graph G is defined as an ordered pair (V, E) where, V is a set of
vertices and the ordered pairs in E are called edges on V.
A directed graph can be represented geometrically as a set of marked points
(called vertices) V with a set of arrows (called edges) E between pairs of points
(or vertex or nodes) so that there is at most one arrow from one vertex to
another vertex.
Application of Graph
1. Social network graphs:- Graphs that represent who knows
whom, who communicates with whom, who influences whom
or other relationships in social structures.
2. Transportation networks :- In road networks vertices are
intersections and edges are the road segments between them,
and for public transportation networks vertices are stops and
edges are the links between them.
3. Document link graphs:- The best known example is the link
graph of the web, where each web page is a vertex, and each
hyperlink a directed edge.
4. Network packet traffic graphs :-Vertices are IP (Internet
protocol) addresses and edges are the packets that flow
between them.
5. Neural networks :-Vertices represent neurons and edges the
synapses between them
Basic Terminologies

Vertex A individual data element of a graph is called as


Vertex. Vertex is also known as node. In above example
graph, V1, V2, V3, V4 ,V5 and V6 E are known as vertices
Edge An edge is a connecting link between two vertices. Edge
is also known as Arc. An edge is represented as
(startingVertex, endingVertex). E = {e1, e2, e3, e4, e5, e6} E
= {(v1, v2) (v2, v3) (v1, v3) (v3, v4),(v3, v5) (v5, v6)}.
Edges are three types
Undirected Edge - An undirected edge is a bidirectional edge. If there is
only one path between two vertices, then the edge is undirected edge.
Directed Edge - A directed edge is a unidirectional edge. If there is a
directed edge between two vertices then edge directed edge.
Weighted Edge - A weighted edge is an edge with cost on it.
Cont…
 Directed Graph A graph with only directed edges is said to
be directed graph.
 Undirected Graph A graph with only undirected edges is
said to be undirected graph.
 Mixed Graph A graph with undirected and directed edges is
said to be mixed graph.
 End vertices or End points The two vertices joined by an
edge are called the end vertices (or endpoints) of the edge.
 Origin If an edge is directed, its first endpoint is said to be
origin of it.
 Destination If an edge is directed, its first endpoint is said to
be origin of it and the other end point is said to be the
destination of the edge.
Cont…
 Adjacent If there is an edge between vertices A and B then both
A and B are said to be adjacent. In other words, two vertices A
and B are said to be adjacent if there is an edge whose end
vertices are A and B.
 Outgoing Edge A directed edge is said to be outgoing edge on
its origin vertex.
 Incoming Edge A directed edge is said to be incoming edge on
its destination vertex.
 Degree Total number of edges connected to a vertex is said to
be degree of that vertex.
 Indegree Total number of incoming edges connected to a vertex
is said to be indegree of that vertex.
 Outdegree Total number of outgoing edges connected to a
vertex is said to be outdegree of that vertex
Cont…
 A graph G is said to be weighted graph if every edge and/or
vertices in the graph is assigned with some weight or value. A
weighted graph can be defined as G = (V, E, We, Wv) where V
is the set of vertices, E is the set at edges and We is a weights of
the edges whose domain is E and Wv is a weight to the vertices
whose domain is V. Consider a graph.

An undirected graph is said to be connected if there exist a path


from any vertex to any other vertex. Otherwise it is said to be
disconnected.
Cont…
 A graph G is said to complete (or fully connected or strongly
connected) if there is a path from every vertex to every other
vertex. Let a and b are two vertices in the directed graph, then
it is a complete graph if there is a path from a to b as well as a
path from b to a. A complete graph with n vertices will have n
(n – 1)/2 edges.
 There are two standard ways of maintaining a graph G in the
memory of a computer.
1. Sequential representation of a graph using adjacent
2. Linked representation of a graph using linked list
ADJACENCY MATRIX REPRESENTATION
 Considered a directed graph below where all the vertices are numbered,
(1, 2, 3, 4....etc.)

The adjacency matrix A of a directed


graph G = (V, E) can be with the following
conditions
Aij = 1 {if there is an edge from Vi to Vj or
if the edge (i, j) is member of E. }
Aij = 0 {if there is no edge from Vi to Vj}
Cont…

The adjacency matrix A of an undirected graph G = (V, E)


can be represented with the following conditions
Aij = 1 {if there is an edge from Vi to Vj or if the edge (i, j) is
member of E }
Aij = 0 {if there is no edge from Vi to Vj or the edge i, j, is
not a member of E }
Cont…
Cont…
 The adjacency matrix A for a directed weighted graph G = (V,
E, We ) can be represented as
Aij = Wij { if there is an edge from Vi to Vj then represent its
weight Wij.}
Aij = – 1 { if there is no edge from Vi to Vj}
Cont…
 Weighted graph can be represented using linked list
by storing the corresponding weight along with the
terminal vertex of the edge.
TRAVERSING A GRAPH
 Many application of graph requires a structured system to
examine the vertices and edges of a graph G.

 That is a graph traversal, which means visiting all the nodes


of the graph. There are two graph traversal methods.

(a) Breadth First Search (BFS)


(b) Depth First Search (DFS)

BFS follows the principle of Queue where as DFS follows


the principle of Stack
BREADTH FIRST SEARCH
 Given an input graph G = (V, E) and a source vertex S, from
where the searching starts.

 The breadth first search systematically traverse the edges of G


to explore every vertex that is reachable from S.

 Then we examine all the vertices neighbor to source vertex S.


 Then we traverse all the neighbors of the neighbors of source
vertex S and so on.
 A queue is used to keep track of the progress of traversing the
neighbor nodes.
Cont…

 Suppose the source vertex is a. Then following steps will illustrate the
BFS.
Step 1: Initially push a (the source vertex) to the queue.
F=0,R=0

Step 2: Pop (or remove) the front element A from the queue (by
incrementing front = front +1) and display it. Then push (or add) the
neighboring vertices of a to the queue, (by incrementing Rear = Rear
+1) if it is not in queue.
Cont…
F=1 R=2

Step 3: Pop the front element b from the queue and display
it. Then add the neighboring vertices of b to the queue, if
it is not in queue.
F=2 R=4

One of the neighboring element a of b is present in the


queue, So a is not added to queue.

Step 4: Remove the front element c and display it. Add the
neighboring vertices of c, if it is not present in queue.
Cont…
F=3 R=6

One of the neighboring elements a of c is present in the queue.


So a is not added.
Step 5: Remove the front element d, and add the neighboring
vertex if it is not present in queue
F=4 R=7

Step 6: Remove the front element e, and add the neighboring


vertex if it is not present in queue.
F=5 R=9
Cont…
Cont…
ALGORITHM
1. Input the vertices of the graph and its edges G = (V, E)
2. Input the source vertex and assign it to the variable S.
3. Add or push the source vertex to the queue.
4. Repeat the steps 5 and 6 until the queue is empty (i.e., front >
rear)
5. Pop the front element of the queue and display it as visited.
6. Push the vertices, which is neighbor to just, popped element,
if it is not in the queue and displayed (i.e., not visited).
7. Exit.
DEPTH FIRST SEARCH

 The depth first search (DFS), as its name suggest, is to


search deeper in the graph, when ever possible.
 Given an input graph G = (V, E) and a source vertex S,
from where the searching starts.
 First we visit the starting node. Then we travel through each
node along a path, which begins at S.
 That is we visit a neighbor vertex of S and again a neighbor
of a neighbor of S, and so on.
 The implementation of BFS is almost same except a stack is
used instead of the queue.
Cont…
Cont…
Cont…
Cont…
Cont…
ALGORITHM
1. Input the vertices and edges of the graph G = (V, E).
2. Input the source vertex and assign it to the variable S.
3. Push the source vertex to the stack.
4. Repeat the steps 5 and 6 until the stack is empty.
5. Pop the top element of the stack and display it.
6. Push the vertices which is neighbor to just popped
element, if it is not in the queue and displayed (ie; not
visited).
7. Exit.
MINIMUM SPANNING TREE
 Spanning tree (ST) for a graph G = (V, E) is a sub graph
G1 = (V1, E1) of G contains all the vertices of G and
n-1 edges. A graph can have more than 1 ST.
1. The vertex set V1 is same as that at graph G.
2. The edge set E1 is a subset of G.
3. And there is no cycle.
 If a graph G is not a connected graph, then it cannot
have any spanning tree.
 A graph G with n vertices then the MST will have (n – 1)
edges, assuming that the graph is connected.
 A minimum spanning tree (MST) for a weighted graph is
a spanning tree with minimum weight.
Cont…
 That is all the vertices in the weighted graph will be
connected with minimum edge with minimum weights.
 The left side tree is MST of the right side graph
Kruskal’s Algorithm
 Kruskal’s Algorithm builds the spanning tree by adding
edges one by one into a growing spanning tree.
 Kruskal's algorithm follows greedy approach as in each
iteration it finds an edge which has least weight and add
it to the growing spanning tree.
 If there are several edges with the same minimum cost,
select any one of them) and add it to the spanning tree.
Eg.
Cont…
Cont…

• Why we are not allowed to pick the edge with weight 4 ?

• The cost for MST of the given graph is 1+2+3+5=11


Prim’s Algorithm
 Prim’s Algorithm also use Greedy approach to find
the minimum spanning tree.
 In Prim’s Algorithm we grow the spanning tree from
a starting position.
 Unlike an edge in Kruskal's, we add vertex to the
growing spanning tree in Prim's.
Eg
Cont…
Lab Work

 Implement Graph Traversal methods and MST algorithms

 Algorithms are available on the book

You might also like