1. ADDITIONALLY ATTEND ONLINE Courses - Simple Snippets
https://simplesnippets.tech/courses
DATA STRUCTURE AND ALGORITHM
TEXTBOOK
Obtained from http://ethioprogramming.net
2. i
Contents
Chapter One: Topic One................................................................................................................................1
Data Structures and Algorithms....................................................................................................................1
A program .................................................................................................................................................1
Chapter One: Topic Two ...........................................................................................................................2
Abstract Data Types..................................................................................................................................2
Chapter One: Topic Three.........................................................................................................................3
Data Structure...........................................................................................................................................3
Chapter One: Topic Four...........................................................................................................................4
Algorithm ..................................................................................................................................................4
Chapter One: Topic Five............................................................................................................................5
Properties of Algorithms...........................................................................................................................5
Chapter Two: Topic One ...............................................................................................................................8
Algorithm analysis.........................................................................................................................................8
Chapter Two: Topic Two .........................................................................................................................10
Empirical Algorithm Analysis ..................................................................................................................10
Chapter Two: Topic Three.......................................................................................................................11
Theoretical Algorithm Analysis ...............................................................................................................11
Chapter Two: Topic Four.........................................................................................................................17
Formal Approach to Analysis ..................................................................................................................17
Chapter Two: Topic Five..........................................................................................................................19
Categories of Algorithm Analysis............................................................................................................19
Chapter Two: Topic Six............................................................................................................................21
Order of Magnitude................................................................................................................................21
Chapter Two: Topic Seven ......................................................................................................................25
Rules to estimate Big Oh of a given function..........................................................................................25
Chapter Three: Topic One...........................................................................................................................28
Inroduction to Sorting and Searching.........................................................................................................28
Chapter Three: Topic Two.......................................................................................................................29
Sequential Searching (Linear) .................................................................................................................29
3. ii
Chapter Three: Topic Three ....................................................................................................................31
Chapter Three: Topic Four ......................................................................................................................34
Simple Sorting Algorithms ......................................................................................................................34
Chapter Three: Topic Five.......................................................................................................................34
Simple sort Algorithm:............................................................................................................................34
Chapter Three: Topic Six.........................................................................................................................38
Bubble sort Algorithm:............................................................................................................................38
Chapter Three: Topic Seven....................................................................................................................41
Selection Sort Algorithm.........................................................................................................................41
Chapter Three: Topic Eight .....................................................................................................................45
Insertion Sort Algorithm: ........................................................................................................................45
Chapter Four: Topic One.............................................................................................................................50
Data Structure and its applications.............................................................................................................50
Chapter Four: Topic Two.........................................................................................................................53
Linked List ...............................................................................................................................................53
Chapter Four: Topic Three ......................................................................................................................56
Singly Linked List.....................................................................................................................................56
Chapter Four: Topic Four ........................................................................................................................58
Adding a node to the end of a singly linked list......................................................................................58
Chapter Four: Topic Five.........................................................................................................................58
Adding a node to the front of a singly linked list....................................................................................58
Chapter Four: Topic Six...........................................................................................................................59
Adding a node to the right of a specific value in a singly linked list .......................................................59
Chapter Four: Topic Seven......................................................................................................................60
Adding a node to the left of a specific value in a singly linked list .........................................................60
Chapter Four: Topic Eight .......................................................................................................................61
Deleting a node from the front of a singly linked list .............................................................................61
Chapter Four: Topic Nine........................................................................................................................61
Deleting a node from the end of a singly linked list ...............................................................................61
Chapter Four: Topic Ten..........................................................................................................................63
Deleting a node of specific data of a singly linked list ............................................................................63
Chapter Four: Topic Eleven.....................................................................................................................65
4. iii
Display in a forward manner in a singly linked list..................................................................................65
Chapter Four: Topic Twelve....................................................................................................................65
Chapter Four: Topic Thirteen..................................................................................................................67
Adding a node to the end of a doubly linked list....................................................................................67
Chapter Four: Topic Fourteen.................................................................................................................67
Adding a node to the front of a doubly linked list..................................................................................67
Chapter Four: Topic Fifteen ....................................................................................................................69
Adding a node to the left of a specific data in a doubly linked list.........................................................69
Chapter Four: Topic Sixteen....................................................................................................................71
Adding a node to the right of a specific data in a doubly linked list.......................................................71
Chapter Four: Topic Seventeen...............................................................................................................73
Deleting a node from the end of a doubly linked list .............................................................................73
Chapter Four: Topic Eighteen .................................................................................................................73
Deleting a node from the front of a doubly linked list ...........................................................................73
Chapter Four: Topic Nineteen.................................................................................................................75
Deleting any node using the search data from a doubly linked list........................................................75
Chapter Four: Topic Twenty....................................................................................................................75
Display the node from the doubly linked list in a forward manner........................................................75
Chapter Four: Topic Twenty One............................................................................................................77
Display the node from the doubly linked list in a backward manner.....................................................77
Chapter Four: Topic Twenty Two............................................................................................................77
Circular linked lists..................................................................................................................................77
Chapter Four: Topic Twenty Three .........................................................................................................79
Adding a node to the end of a Circular Singly linked list ........................................................................79
Chapter Four: Topic Twenty Four ...........................................................................................................79
Adding a node to the front of a Circular Singly linked list ......................................................................80
Chapter Four: Topic Twenty Five ............................................................................................................80
Adding a node to the left of a specific data in a Circular Singly linked list .............................................81
Chapter Four: Topic TWENTY SIX............................................................................................................83
Adding a node to the right of a specific data in a Circular Singly linked list...........................................83
Chapter Four: Topic TWENTY SEVEN ......................................................................................................83
Deleting a node from the end of a Circular Singly linked list..................................................................84
5. iv
Chapter Four: Topic TWENTY EIGHT.......................................................................................................84
Deleting a node from the front of a Circular Singly linked list................................................................84
Chapter Four: Topic TWENTY NINE.........................................................................................................86
Deleting any node using the search data from a Circular Singly linked list............................................86
Chapter Four: Topic THIRTY....................................................................................................................88
Display the node from the Circular Singly linked list in a forward manner............................................88
Chapter Five: Topic One..............................................................................................................................88
Abstract Data Type Queue..........................................................................................................................88
Chapter Five: Topic Two..........................................................................................................................91
Array Implementation of Queue.............................................................................................................91
Chapter Five: Topic Three.......................................................................................................................95
enqueue an element...............................................................................................................................95
Array Implementation of queue in C++ ..................................................................................................95
Chapter Five: Topic Four.........................................................................................................................95
dequeue an element...............................................................................................................................95
Chapter Five: Topic Five..........................................................................................................................97
display next element to be dequeued....................................................................................................97
Chapter Five: Topic Six............................................................................................................................97
display all elements in the queue in a forward manner.........................................................................97
Chapter Five: Topic Seven.......................................................................................................................99
display all elements in the queue in a backward manner ......................................................................99
Chapter Five: Topic Eight ........................................................................................................................99
Array of Structs Implementation of a queue enqueue an element........................................................99
Chapter Five: Topic Nine.......................................................................................................................101
dequeue an element in an Array of Structs..........................................................................................101
Chapter Five: Topic Ten ........................................................................................................................101
display next element to be dequeued in an Array of Structs ...............................................................101
Chapter Five: Topic Eleven....................................................................................................................103
display all elements in the queue in a forward manner in an array of structs.....................................103
Chapter Five: Topic Twelve...................................................................................................................103
display all elements in the queue in a backward manner in an array of structs..................................103
Chapter Five: Topic Thirteen.................................................................................................................105
6. v
Linked List Implementation of a queue ................................................................................................105
Chapter Five: Topic Fourteen................................................................................................................105
Linked List Implementation in C++........................................................................................................105
Chapter Five: Topic Fifteen...................................................................................................................107
enqueue an element from a doubly linked list.....................................................................................107
Chapter Five: Topic Sixteen...................................................................................................................107
dequeue an element from a doubly linked list.....................................................................................107
Chapter Five: Topic Sixteen...................................................................................................................109
dequeue an element from a doubly linked list.....................................................................................109
Chapter Five: Topic Sixteen...................................................................................................................109
dequeue an element from a doubly linked list.....................................................................................109
Chapter Five: Topic Seventeen .............................................................................................................111
display next element to be dequeued from a doubly linked list ..........................................................111
Chapter Five: Topic Eighteen ................................................................................................................111
display all elements in the queue in a forward manner in a doubly linked list....................................111
Chapter Five: Topic Nineteen ...............................................................................................................113
display all elements in the queue in a backward manner in a doubly linked list .................................113
Chapter Five: Topic Twenty ..................................................................................................................113
Priority Queue.......................................................................................................................................113
Chapter Five: Topic Twenty One...........................................................................................................115
Demerging Queues ...............................................................................................................................115
Chapter Five: Topic Twenty Two...........................................................................................................115
Merging Queues....................................................................................................................................115
Chapter Five: Topic Twenty Three ........................................................................................................117
Application of Queues...........................................................................................................................117
Chapter Six: Topic One..............................................................................................................................117
Stack..........................................................................................................................................................117
Chapter Six: Topic Two..........................................................................................................................121
Infix to Postfix Conversion ....................................................................................................................121
Chapter Six: Topic Three.......................................................................................................................124
Postfix Evaluation Psuedocode.............................................................................................................124
Chapter Six: Topic Four.........................................................................................................................124
7. vi
Chapter Six: Topic Five..........................................................................................................................126
Chapter Six: Topic Six............................................................................................................................126
pop an element from the top of the stack in an array .........................................................................126
Chapter Six: Topic Seven.......................................................................................................................128
display the content of the top element in the stack in an array ..........................................................128
Chapter Six: Topic Eight ........................................................................................................................128
display the content of all elements in the stack forward manner in an array......................................128
Chapter Six: Topic Nine.........................................................................................................................130
display the content of all elements in the stack back manner in an array...........................................130
Chapter Six: Topic Ten...........................................................................................................................130
Array of Structs Implementation of a stack..........................................................................................130
Chapter Six: Topic Eleven......................................................................................................................132
push an element to the top of the stack ..............................................................................................132
Chapter Six: Topic Twelve.....................................................................................................................133
pop an element from the top of the stack............................................................................................133
Chapter Six: Topic Thirteen...................................................................................................................133
display the content of the top element in the stack.............................................................................133
Chapter Six: Topic Fourteen..................................................................................................................135
display the content of all elements in the stack in a forward manner.................................................135
Chapter Six: Topic Fifteen .....................................................................................................................135
display the content of all elements in the stack in a backward manner ..............................................135
Chapter Six: Topic Sixteen.....................................................................................................................137
Linked list Implementation of a stack...................................................................................................137
Chapter Six: Topic Seventeen ...............................................................................................................137
push an element to the top of the stack in a linked list .......................................................................137
Chapter Six: Topic Eighteen ..................................................................................................................139
pop an element from the top of the stack using a linked list...............................................................139
Chapter Six: Topic Nineteen..................................................................................................................139
display the content of the top element in the stack using a linked list................................................139
Chapter Six: Topic Twenty.....................................................................................................................141
display the content of all elements in the stack in a forward manner in a linked list..........................141
Chapter Six: Topic Twenty One.............................................................................................................141
8. vii
display the content of all elements in the stack in a backward manner in a linked list .......................141
Chapter Seven: Topic One.........................................................................................................................143
Introduction to Data structure..................................................................................................................143
Chapter Seven: Topic Two ....................................................................................................................143
Tree Terminologies ...............................................................................................................................143
Chapter Seven: Topic Three..................................................................................................................147
Binary search tree (ordered binary tree)..............................................................................................147
Chapter Seven: Topic Four....................................................................................................................150
Insertion of data into a BST...................................................................................................................150
Chapter Seven: Topic Five.....................................................................................................................153
Traversing (Visiting) in a BST.................................................................................................................153
Chapter Seven: Topic Six.......................................................................................................................153
Application of binary tree traversal......................................................................................................153
Chapter Seven: Topic Seven..................................................................................................................155
Implementation of the Preorder traversals..........................................................................................155
Chapter Seven: Topic Eight...................................................................................................................155
Implementation of the Inorder traversals............................................................................................155
Chapter Seven: Topic Nine....................................................................................................................157
Implementation of the Postorder traversals ........................................................................................157
Chapter Seven: Topic Ten .....................................................................................................................157
Searching a data in BST.........................................................................................................................157
Chapter Seven: Topic Eleven.................................................................................................................159
Finding Minimum value in a Binary Search Tree ..................................................................................159
Chapter Seven: Topic Twelve................................................................................................................161
Finding Maximum value in a Binary Search Tree..................................................................................161
Chapter Seven: Topic Thirteen..............................................................................................................162
Deleting from a BST ..............................................................................................................................162
Chapter Eight: Topic One..........................................................................................................................174
Graph ........................................................................................................................................................174
Chapter Eight: Topic Two......................................................................................................................176
Directed and Undirected Graph............................................................................................................176
Chapter Eight: Topic Three ...................................................................................................................178
9. viii
Sparse and dense graphs ......................................................................................................................178
Chapter Eight: Topic Four .....................................................................................................................178
Weighted Graph and Unweighted Graph.............................................................................................178
Chapter Eight: Topic Five ......................................................................................................................180
Breadth-first search ..............................................................................................................................180
Chapter Eight: Topic Six ........................................................................................................................182
The algorithm works on both directed and undirected graphs............................................................182
Chapter Eight: Topic Seven...................................................................................................................184
Shortest paths.......................................................................................................................................184
Chapter Eight: Topic Eight.....................................................................................................................184
Implementation of Breadth-first search...............................................................................................184
Chapter Eight: Topic Nine .....................................................................................................................186
Enqueue an Item into a graph of the BFS .............................................................................................186
Chapter Eight: Topic Ten.......................................................................................................................186
Chapter Eight: Topic Eleven..................................................................................................................186
Chapter Eight: Topic Twelve .................................................................................................................186
Create a graph BFS................................................................................................................................186
Chapter Eight: Topic Thirteen...............................................................................................................189
Chapter Eight: Topic Fourteen..............................................................................................................191
Depth first search..................................................................................................................................191
Chapter Eight: Topic Fifteen .................................................................................................................193
pseudocode of depth-first-search algorithm........................................................................................193
Chapter Eight: Topic Sixteen.................................................................................................................196
Implementation of Depth First Search Algorithm ................................................................................196
Chapter Eight: Topic Seventeen............................................................................................................198
Pushing an Item into a graph of the DFS ..............................................................................................198
Chapter Eight: Topic Eighteen...............................................................................................................198
Popping an item from the graph of DFS ...............................................................................................198
Chapter Eight: Topic Nineteen..............................................................................................................198
Check if the Graph of a Depth First Search is Empty ............................................................................198
Chapter Eight: Topic Twenty.................................................................................................................200
Creating a Depth First Search Graph ....................................................................................................200
10. ix
Chapter Eight: Topic Twenty One .........................................................................................................202
Traversing in a Graph of Depth First Search.........................................................................................202
11. 1
Chapter One: Topic One
Data Structures and Algorithms
A program
A set of instruction which 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
The way data are organized in a computers memory is said to be Data
Structure. The sequence of computational steps to solve a problem is
said to be an Algorithm.
Therefore, a program is Data structures plus Algorithm.
Data structures are used to model the world or part of the world.
How?
The value held by a data structure represents some specific
characteristic of the world.
The characteristic being modeled restricts the possible values
held by a data structure and the operations to be performed on
the data structure.
The first step to solve the problem is obtaining ones own abstract
view, or model, of the problem. This process of modeling is
called abstraction. The model defines an abstract view to the problem.
The model should only focus on problem related stuff.
Abstraction
Abstraction is a process of classifying characteristics as relevant and
irrelevant for the particular purpose at hand and ignoring the irrelevant
ones.
Example: model students of BDU.
12. 2
Relevant:
Char Name[15];
Char ID[11];
Char Dept[20];
int Age, year;
Non relevant:
float hieght, weight;
Using the model above, a programmer tries to define the properties of
the problem. These properties include The data which are affected and
The operations that are involved in the problem An entity with the
properties just described is called an abstract data type (ADT)
Chapter One: Topic Two
Abstract Data Types
Abstract Data Types Consists of data to be stored and operations
supported on them. Abstract Data Types is a specification that
describes a data set and the operation on that data.
The ADT specifies:
What data is stored.
What operations can be done on the data. However, it does not specify
how to store or how to implement the operation. It is also independent
of any programming language.
Example:
ADT employees of an organization:
This ADT stores employees with their relevant attributes and
discarding irrelevant attributes.
13. 3
Relevant:- Name, ID, Sex, Age, Salary, Dept, Address
Non Relevant :- weight, color, height
This ADT supports hiring, firing, retiring, … operations.
Chapter One: Topic Three
Data Structure
In Contrast a data structure is a language construct that the
programmer has defined in order to implement an abstract data type.
What is the purpose of data structures in programs?
Data structures are used to model a problem.
Example:
struct Student_Record
{
char name[20];
char ID_NO[10];
char Department[10];
int age;
};
Attributes of each variable:
Name: Textual label.
Address: Location in memory.
Scope: Visibility in statements of a program.
Type: Set of values that can be stored + set of operations that can be
performed.
14. 4
Size: The amount of storage required to represent the variable.
Life time: The time interval during execution of a program while the
variable exists.
Chapter One: Topic Four
Algorithm
Algorithm Is a concise specification of an operation for solving a
problem. Algorithm is a well-defined computational procedure that
takes some value or a set of values as input and produces some value
or a set of values as output:
Inputs ==> Algorithm ==> Outputs
An algorithm is a specification of a behavioral process. It consists of a
finite set of instructions that govern behavior step-by-step. It is part of
what constitutes a data structure Data structures model the static part
of the world. They are unchanging while the world is changing. In
order to model the dynamic part of the world we need to work with
algorithms. Algorithms are the dynamic part of a program’s world
model. An algorithm transforms data structures from one state to
another state.
What is the purpose of algorithms in programs?
Take values as input:
Example: cin>>age;
Change the values held by data structures:
Example: age=age+1;
Change the organization of the data structure:
Example: Sort students by name
Produce outputs:
Example: Display student’s information
15. 5
The quality of a data structure is related to its ability to successfully
model the characteristics of the world (problem). Similarly, the quality
of an algorithm is related to its ability to successfully simulate the
changes in the world. However, the quality of data structure and
algorithms is determined by their ability to work together well.
Generally speaking, correct data structures lead to simple and efficient
algorithms. And correct algorithms lead to accurate and efficient data
structures.
Chapter One: Topic Five
Properties of Algorithms
Finiteness: Algorithm must complete after a finite number of steps.
Algorithm should have a finite number of steps.
Finite:
1. int i=0;
2. while(i<10)
3. {
4. cout << i ;
5. i++;
6. }
Infinite:
1. int i=0;
2. while(true)
3. {
4. cout << "Hello World";
5. }
16. 6
Definiteness (Absence of ambiguity): Each step must be clearly
defined, having one and only one interpretation. At each point in
computation, one should be able to tell exactly what happens next.
Sequential: Each step must have a uniquely defined preceding and
succeeding step. The first step (start step) and last step (halt step)
must be clearly noted.
Feasibility: It must be possible to perform each instruction. Each
instruction should have possibility to be executed.
1) for(int i=0; i<0; i++)
{
cout<< i; // there is no possibility that this statement to be executed
because the condition is false from the beginning.
}
2) if(5>7)
{
cout<<“hello”; // not executed because it is always false.
}
Correctness: It must compute correct answer for all possible legal
inputs. The output should be as expected and required and correct.
Language Independence: It must not depend on any one
programming language.
Completeness: It must solve the problem completely.
Effectiveness: Doing the right thing. It should yield the correct result
all the time for all of the possible cases.
Efficiency: It must solve with the least amount of computational
resources such as time and space. Producing an output as per the
requirement within the given resources (constraints).
17. 7
Example: Write a program that takes a number and displays the square
of the number.
1)
1. int x;
2. cin>>x;
3. cout <
2)
1. int x,y;
2. cin>>x;
3. y=x*x*x;
4. cout <
Example: Write a program that takes two numbers and displays the
sum of the two.
Program a:
1. cin>>a;
2. cin>>b;
3. sum = a + b;
4. cout << sum;
Program b:
1. cin>>a;
2. cin>>b;
3. a= a + b;
4. cout << a;
Program c (the most efficient):
18. 8
1. cin>>a;
2. cin>>b;
3. cout << a + b;
All are effective but with different efficiencies.
Input/output: There must be a specified number of input values, and
one or more result values. Zero or more inputs and one or more
outputs.
Precision: The result should always be the same if the algorithm is
given identical input.
Simplicity: A good general rule is that each step should carry out one
logical step. What is simple to one processor may not be simple to
another.
Levels of abstraction: Used to organize the ideas expressed in
algorithms. It is also used to hide the details of a given activity and
refer to just a name for those details. The simple (detailed) instructions
are hidden inside modules. Well-designed algorithms are organized in
terms of levels of abstraction.
Chapter Two: Topic One
Algorithm analysis
Algorithm analysis refers to the process of determining how much
computing time and storage that algorithms will require. In other
words, it’s a process of predicting the resource requirement of
algorithms in a given environment.
In order to solve a problem, there are many possible algorithms. One
has to be able to choose the best algorithm for the problem at hand
using some scientific method. To classify some data structures and
algorithms as good, we need precise ways of analyzing them in terms
of resource requirement.
19. 9
The main resources are:
Running Time
Memory Usage
Communication Bandwidth
Note: Running time is the most important since computational time is
the most precious resource in most problem domains.
There are two approaches to measure the efficiency of algorithms:
1.Empirical
2. Theoretical
We will see each in the next two consecutive sections.
20. 10
Chapter Two: Topic Two
Empirical Algorithm Analysis
it works based on the total running time of the program. It uses actual system clock time.
Example:
t1(Initial time before the program starts)
for(int i=0; i<=10; i++)
cout<<i;
t2 (final time after the execution of the program is finished)
Running time taken by the above algorithm (TotalTime) = t2-t1;
It is difficult to determine efficiency of algorithms using this approach, because clock-time can
vary based on many factors.
For example:
a) Processor speed of the computer
1.78GHz ==> 10s
2.12GHz ==> 15s
b) Current processor load
Only the work 10s
With printing 15s With printing &
browsing the internet >15s
c) Specific data for a particular run of the program
Input size
Input properties
t1
for(int i=0; i<=n; i++)
21. 11
cout<<i;
t2
T=t2-t1;
For n=100, T>=0.5s
n=1000, T>0.5s d)
Operating System
Multitasking Vs Single tasking
Internal structure
Chapter Two: Topic Three
Theoretical Algorithm Analysis
Determining the quantity of resources required using mathematical
concept. Analyze an algorithm according to the number of basic
operations (time units) required, rather than according to an absolute
amount of time involved.
We use theoretical approach to determine the efficiency of algorithm
because:
The number of operation will not vary under different conditions.
It helps us to have a meaningful measure that permits
comparison of algorithms independent of operating platform.
It helps to determine the complexity of algorithm.
Complexity Analysis
Complexity Analysis is the systematic study of the cost of
computation, measured either in:
Time units
Operations performed, or
The amount of storage space required.
22. 12
Two important ways to characterize the effectiveness of an algorithm
are its Space Complexity and Time Complexity.
Time Complexity: Determine the approximate amount of time
(number of operations) required to solve a problem of size n. The
limiting behavior of time complexity as size increases is called
the Asymptotic Time Complexity.
Space Complexity: Determine the approximate memory required to
solve a problem of size n. The limiting behavior of space complexity as
size increases is called the Asymptotic Space Complexity.
Asymptotic Complexity of an algorithm determines the size of
problems that can be solved by the algorithm.
Factors affecting the running time of a program:
CPU type (80286, 80386, 80486, Pentium I---IV)
Memory used
Computer used
Programming Language C (fastest), C++ (faster), Java (fast) C is
relatively faster than Java, because C is relatively nearer to
Machine language, so, Java takes relatively larger amount of time
for interpreting/translation to machine code.
Algorithm used
Input size
Note: Important factors for this course are Input size and
Algorithm used.
Complexity analysis involves two distinct phases:
Algorithm Analysis
Order of Magnitude Analysis
Algorithm Analysis: Analysis of the algorithm or data structure to
produce a function T(n) that describes the algorithm in terms of the
operations performed in order to measure the complexity of the
algorithm.
Order of Magnitude Analysis: Analysis of the function T (n) to
determine the general complexity category to which it belongs. There
23. 13
is no generally accepted set of rules for algorithm analysis. However,
an exact count of operations is commonly used. To count the number
of operations we can use the following Analysis Rule.
Analysis Rules:
1. Assume an arbitrary time unit.
2. Execution of one of the following operations takes time 1 unit:
Assignment Operation Example: i=0;
Single Input/Output Operation Example: cin>>a; cout<<“hello”;
Single Boolean Operations Example: i>=10
Single Arithmetic Operations Example: a+b;
Function Return Example: return sum;
3. Running time of a selection statement (if, switch) is the time for the
condition evaluation plus the maximum of the running times for the
individual clauses in the selection.
Example: int x;
int sum=0;
if(a>b)
{
sum= a+b;
cout<<sum;
}
else
{
cout<<b;
}
24. 14
T(n) = 1 +1+max(3,1) = 5
4. Loop statements: The running time for the statements inside the
loop * number of iterations + time for setup(1) + time for checking
(number of iteration + 1) + time for update (number of iteration). The
total running time of statements inside a group of nested loops is the
running time of the statements * the product of the sizes of all the
loops.For nested loops, analyze inside out. Always assume that the
loop executes the maximum number of iterations possible. (Why?)
Because we are interested in the worst case complexity.
5. Function call: 1 for setup + the time for any parameter calculations
+ the time required for the execution of the function body.
Examples: 1)
int k=0,n;
cout<<“Enter an integer”;
cin>>n for(int i=0;i<n; i++)
k++;
T(n)= 3+1+n+1+n+n=3n+5 2)
int i=0;
while(i<n)
{
cout<<i; i++;
}
int j=1;
while(j<=10)
{
25. 15
cout<<j; j++;
}
T(n)=1+n+1+n+n+1+11+2(10) = 3n+34 3)
int k=0;
for(int i=1 ; i<=n; i++)
for( int j=1; j<=n; j++)
k++;
T(n)=1+1+(n+1)+n+n(1+(n+1)+n+n) = 2n+3+n(3n+2) = 2n+3+3n2
+2n =
3n2
+4n+3 4).
int sum=0;
for(i=1;i<=n;i++))
sum=sum+i;
T(n)=1+1+(n+1)+n+(1+1)n =3+4n=O(n) 5).
int counter()
{
int a=0;
cout<<”Enter a number”;
cin>>n;
for(i=0;i<n;i++) a=a+1;
return 0;
}
T(n)=1+1+1+(1+n+1+n)+2n+1 =4n+6=O(n) 6).
26. 16
void func( )
{
int x=0;
int i=0;
int j=1;
cout<<”Enter a number”;
cin>>n;
while(i<n){ i=i+1;
}
while(j<n)
{
j=j+1;
}
}
T(n)=1+1+1+1+1+n+1+2n+n+2(n-1) = 6+4n+2n-2 =4+6n=O(n) 7).
int sum(int n)
{
int s=0;
for(int i=1;i<=n;i++)
s=s+(i*i*i*i);
return s;
}
27. 17
T(n)=1+(1+n+1+n+5n)+1 =7n+4=O(n) 8).
int sum=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
sum++;
T(n)=1+1+(n+1)+n+n*(1+(n+1)+n+n) =3+2n+n2
+2n+2n2
=3+2n+3n2
+2n
=3n2
+4n+3=O(n2)
Chapter Two: Topic Four
Formal Approach to Analysis
In the above examples we have seen that analyzing Loop statements is
so complex. It can be simplified by using some formal approach in
which case we can ignore initializations, loop controls, and updates.
Simple Loops: Formally, for loop can be translated to a summation.
The index and bounds of the summation are the same as the index
and bounds of the for loop. Suppose we count the number of additions
that are done. There is 1 addition per iteration of the loop, hence n
additions in total.
Nested Loops: Formally, nested for loops translate into multiple
summations, one for each For loop.
28. 18
Consecutive Statements: Formally, add the running times of the
separate blocks of your code.
Conditionals: (Formally take maximum) Example:
Recursive: Formally -Usually difficult to analyze.
Example:
Factorial:
1. long factorial(int n)
29. 19
2. {
3. if(n<=1)
4. return 1;
5. else
6. return n*factorial(n-1);
7. }
T(n)=1+T(n-1)=2+T(n-2)=3+T(n-3)=------- =n-1 (counting the number of
multiplication)
Chapter Two: Topic Five
Categories of Algorithm Analysis
Algorithms may be examined under different situations to correctly
determine their efficiency for accurate comparison.
Best Case Analysis
Best case analysis assumes the input data are arranged in the most
advantageous order for the algorithm. It also takes the smallest
possible set of inputs and causes execution of the fewest number of
statements. Moreover,it computes the lower bound of T(n), where T(n)
is the complexity function.
Examples: For sorting algorithm
If the list is already sorted (data are arranged in the required
order).
For searching algorithm If the desired item is located at first
accessed position.
Worst Case Analysis
Worst case analysis assumes the input data are arranged in the most
disadvantageous order for the algorithm. Takes the worst possible set
of inputs. Causes execution of the largest number of statements.
30. 20
Computes the upper bound of T(n) where T(n) is the complexity
function.
Example:
While sorting, if the list is in opposite order.
While searching, if the desired item is located at the last position
or is missing.
Examples:
For sorting algorithms, If the list is in opposite order.
For searching algorithms, If the desired item is located at the last
position or is missing.
Worst case analysis is the most common analysis because, it provides
the upper bound for all input (even for bad ones).
Average case analysis is often difficult to determine and define. If
situations are in their best case, no need to develop algorithms
because data arrangements are in the best situation.
Best case analysis can not be used to estimate complexity. We are
interested in the worst case time since it provides a bound for all
input-this is called the “Big-Oh” estimate.
Average Case Analysis
Determine the average of the running time overall permutation of
input data.
Takes an average set of inputs.
It also assumes random input size.
It causes average number of executions.
Computes the optimal bound of T(n) where T(n) is the complexity
function.
Sometimes average cases are as bad as worst cases and as good
as best cases.
Examples:
31. 21
For sorting algorithms, While sorting, considering any arrangement
(order of input data).
For searching algorithms While searching, if the desired item is located
at any location or is missing.
Chapter Two: Topic Six
Order of Magnitude
Order of Magnitude refers to the rate at which the storage or time
grows as a function of problem size. It is expressed in terms of its
relationship to some known functions. This type of analysis is
called Asymptotic analysis.
Asymptotic Notations
Asymptotic Analysis is concerned with how the running time of an
algorithm increases with the size of the input in the limit, as the size
of the input increases without bound!
Asymptotic Analysis makes use of O (Big-Oh) , Ω (Big-Omega), θ
(Theta), o (little-o), ω (little-omega) - notations in performance analysis
and characterizing the complexity of an algorithm.
Note: The complexity of an algorithm is a numerical function of the
size of the problem (instance or input size).
Types of Asymptotic Notations
Big-Oh Notation
Definition: We say f(n)=O(g(n)), if there are positive constants no and
c, such that to the right of no, the value of f(n) always lies on or below
c.g(n). As n increases f(n) grows no faster than g(n). It’s only
concerned with what happens for very large values of n. It describes
the worst case analysis. Gives an upper bound for a function to within
a constant factor.
32. 22
O-Notations are used to represent the amount of time an algorithm
takes on the worst possible set of inputs, “Worst-Case”.
Question-1: f(n)=10n+5 and g(n)=n. Show that f(n) is O(g(n)). To show
that f(n) is O(g(n)), we must show that there exist constants c and k
such that f(n)<=c.g(n) for all n>=k. 10n+5<=c.n ==> for all n>=k
let c=15, then show that 10n+5<=15n 5<=5n or 1<=n So,
f(n)=10n+5<=15.g(n) for all n>=1 (c=15, k=1),
there exist two constants that satisfy the above constraints.
Question-2: f(n)=3n2
+4n+1. Show that f(n)=O(n2
). 4n<=4n2
for all n>=1
and 1<=n2
for all n>=1 3n2
+4n+1<=3n2
+4n2
+n2
for all n>=1 <=8n2
for
all n>=1
So, we have shown that f(n)<=8n2 for all n>=1.
Therefore, f(n) is O(n2), (c=8, k=1), there exist two constants that
satisfy the constraints.
Big-Omega (Ω)-Notation (Lower bound)
33. 23
Definition: We write f(n)= Ω(g(n)) if there are positive constants no and
c such that to the right of no the value of f(n) always lies on or above
c.g(n).
As n increases f(n) grows no slower than g(n). It describes the best
case analysis. Used to represent the amount of time the algorithm
takes on the smallest possible set of inputs-“Best case”.
Example: Find g(n) such that f(n) = Ω(g(n)) for f(n)=3n+5 g(n) = √n,
c=1, k=1. f(n)=3n+5=Ω(√n)
Big-Omega (Ω)-Notation (Lower bound)
3. Theta Notation (θ-Notation) (Optimal bound)
Definition: We say f(n)= θ(g(n)) if there exist positive constants no,
c1
and c2
such that to the right of no, the value of f(n) always lies
between c1
.g(n) and c2
.g(n) inclusive, i.e., c1
.g(n)<=f(n)<=c2
.g(n), for all
n>=no.
34. 24
As n increases f(n) grows as fast as g(n). It describes the average case
analysis. To represent the amount of time the algorithm takes on an
average set of inputs- “Average case”.
Example: Find g(n) such that f(n) = Θ(g(n)) for f(n)=2n2
+3 ==> n2
≤
2n2
≤ 3n2
==> c1=1, c2=3 and no=1 ==>f(n) = Θ(g(n)). Theta Notation
(Θ-Notation) (Optimal bound)
4. Little-oh (small-oh) Notation
Definition: We say f(n)=o(g(n)), if there are positive constants no and c
such that to the right of no, the value of f(n) lies below c.g(n).
As n increases, g(n) grows strictly faster than f(n). It describes the
worst case analysis. Denotes an upper bound that is not asymptotically
tight. Big O-Notation denotes an upper bound that may or may not be
asymptotically tight.
Example: Find g(n) such that f(n) = o(g(n)) for f(n) = n2
n2
<2n2
, for all n>1, ==> k=1, c=2,
35. 25
g(n)=n2
n2
< n3
, g(n) = n3
, f(n)=o(n3
)
n2< n4
, g(n) =n4
, f(n)=o(n4
)
5. Little-Omega (ω) notation
Definition: We write f(n)=ω(g(n)), if there are positive constants no and
c such that to the right of no, the value of f(n) always lies above c.g(n).
As n increases f(n) grows strictly faster than g(n). It describes the best
case analysis and denotes a lower bound that is not asymptotically
tight.
Big Ω-Notation denotes a lower bound that may or may not be
asymptotically tight.
Example: Find g(n) such that f(n)=ω(g(n)) for f(n)=n2
+3 g(n)=n,
Since n2
> n, c=1, k=2. g(n)=√n,
Since n2
> √n, c=1, k=2, can also be solution.
Chapter Two: Topic Seven
Rules to estimate Big Oh of a given function
Pick the highest order. Ignore the coefficient. Example: T(n)=3n + 5
==> O(n)
T(n)=3n2
+4n+2 ==> O(n2
)
Some known functions encountered when analyzing algorithms.
(Complexity category for Big-Oh).
Rule 1: If T1(n)=O(f(n)) and T2(n)=O(g(n)), then
T1(n)+T2(n)=max(O(f(n)),O(g(n))), T1(n)*T2(n)=O(f(n)*g(n))
Rule 2: If T(n) is a polynomial of degree k, then T(n)=θ(nk
).
36. 26
Rule 3: logk
n=O(n) for any constant k. This tells us that logarithms
grow very slowly.
We can always determine the relative growth rates of two functions f(n)
and g(n) by computing lim n->infinity f(n)/g(n). The limit can have four
possible values.
The limit is 0: This means that f(n)=o(g(n)).
The limit is c≠0: This means that f(n)=θ(g(n)).
The limit is infinity: This means that g(n)=o(f(n)).
The limit oscillates: This means that there is no relation between
f(n) and g(n).
Example:
n3
grows faster than n2
, so we can say that n2
=O(n3
) or n3
=Ω(n2
).
f(n)=n2
and g(n)=2n2
grow at the same rate, so both f(n)=O(g(n)) and
f(n)=Ω(g(n)) are true.
If f(n)=2n2
, f(n)=O(n4
), f(n)=O(n3
), and f(n)=O(n2
) are all correct, but the
last option is the best answer.
T(n)
Complexity Category
functions F(n)
Big-O
c, c is constant 1 C=O(1)
10logn + 5 logn T(n)=O(logn)
√n +2 √n T(n)=O(√n)
5n+3 n T(n)=O(n)
3nlogn+5n+2 nlogn T(n)=O(nlogn)
10n2
+nlogn+1 n2
T(n)=O(n2
)
5n3
+ 2n2
+ 5 n3
T(n)=O(n3
)
2n
+n5
+n+1 2n
T(n)=O(2n
)
7n!+2n
+n2
+1 n! T(n)=O(n!)
8nn
+2n
+n2
+3 nn
T(n)=O(nn
)
37. 27
Arrangement of common functions by growth rate. List of typical
growth rates.
Function Name
c Constant
log N Logarithmic
log2 N Log-squared
N Linear
N log N Log-Linear
N2
Quadratic
N3
Cubic
2N
Exponential
38. 28
The order of the body statements of a given algorithm is very
important in determining Big-Oh of the algorithm.
Example: Find Big-Oh of the following algorithm.
1)
for( int i=1;i<=n; i++)
sum=sum + i;
T(n)=2*n=2n=O(n).
2)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
k++;
T(n)=1*n*n=n2
= O(n2
).
Chapter Three: Topic One
Inroduction to Sorting and Searching
Why do we study sorting and searching algorithms?
These algorithms are the most common and useful tasks operated by
computer system. Computers spend a lot of time searching and
sorting.
1. Simple Searching algorithms
Searching:- is a process of finding an element in a list of items or
determining that the item is not in the list. To keep things simple, we
shall deal with a list of numbers. A search method looks for a key,
arrives by parameter. By convention, the method will return the index
of the element corresponding to the key or, if unsuccessful, the value -
1.
39. 29
There are two simple searching algorithms:
Sequential Search, and
Binary Search
Chapter Three: Topic Two
Sequential Searching (Linear)
The most natural way of searching an item. Easy to understand and
implement.
Algorithm:
In a linear search, we start with top (beginning) of the list, and
compare the element at top with the key.
If we have a match, the search terminates and the index number
is returned.
If not, we go on the next element in the list.
If we reach the end of the list without finding a match, we return
-1.
Implementation:
Assume the size of the list is n.
1. #include <iostream>
2. int LinearSearch(int list[ ], int key);
3. using namespace std;
4. int main()
5. {
6. int list[] = {5, 3, 7, 4, 6};
7. int k = 4;
8. int i = LinearSearch(list, k);
9. if(i==-1)
40. 30
10. cout << "the search item is not found" << endl;
11. else
12. cout << "The value is found at index position " << i << endl;
13. return 0;
14. }
15.
16. int LinearSearch(int list[ ], int key)
17. {
18. int index=-1;
19. for(int i=0; i < n; i++)
20. {
21. if(list[i]==key)
22. {
23. index=i;
24. break;
25. }
26. }
27. return index;
28. }
Complexity Analysis:
Big-Oh of sequential searching: How many comparisons are made in
the worst case ?
n O(n).
41. 31
Chapter Three: Topic Three
Binary Searching
It assumes the data is sorted it also uses divide and conquer strategy
(approach).
Algorithm:
In a binary search, we look for the key in the middle of the list. If
we get a match, the search is over.
If the key is greater than the element in the middle of the list, we
make the top (upper) half the list to search.
If the key is smaller, we make the bottom (lower) half the list to
search.
Repeat the above steps (I,II and III) until one element remains.
If this element matches return the index of the element, else
return -1 index. (-1 shows that the key is not in the list).
Implementation:
1. #include <iostream>
2. int BinarySearch(int list[ ], int key);
3. using namespace std;
4. int main()
5. {
6. int list[] = {15, 23, 47, 54, 76};
7. int k = 54;
8. int i = BinarySearch(list, k);
9. if(i==-1)
10. cout << "the search item is not found" << endl;
11. else
12. cout << "The value is found at index position " << i << endl;
44. 34
Chapter Three: Topic Four
Simple Sorting Algorithms
Sorting: is a process of reordering a list of items in either increasing or decreasing order. Ordering
a list of items is fundamental problem of computer science. Sorting is the most important
operation performed by computers. Sorting is the first step in more complex algorithms.
Two basic properties of sorting algorithms:
In-place:
It is possible to sort very large lists without the need to allocate additional working storage.
Stable:
If two elements that are equal remain in the same relative position after sorting is completed.
Two classes of sorting algorithms:
O(n2):
Includes the bubble, insertion, and selection sorting algorithms.
O(nlog n):
Includes the heap, merge, and quick sorting algorithms.
Simple sorting algorithms include:
Simple sorting
Bubble Sorting
Selection Sorting
Insertion Sorting
Chapter Three: Topic Five
Simple sort Algorithm:
In simple sort algorithm the first element is compared with the second,
third and all subsequent elements.
45. 35
If any one of the other elements is less than the current first element
then the first element is swapped with that element.
Eventually, after the last element of the list is considered and swapped,
then the first element has the smallest element in the list.
The above steps are repeated with the second, third and all
subsequent elements.
Implementation:
1. #include <iostream>
2. using namespace std;
3. void SimpleSort(int list[]);
4. int list[] = {5, 3, 7, 4, 6};
5. int main()
6. {
7. cout << "The values before sorting are: n";
8. for(int i = 0; i < 5; i++)
9. cout << list[i] << " ";
10. SimpleSort(list);
11. cout << endl;
12. cout << "The values after sorting are: n";
13. for(int i = 0; i < 5; i++)
14. cout << list[i] << " ";
15. return 0;
16. }
17. void SimpleSort(int list[])
18. {
47. 37
Analysis: O(?)
1st pass-----==> (n-1) comparisons
2nd pass----==> (n-2) comparisons
.
.
.
(n-1)th pass---==> 1 comparison
T(n)=1+2+3+4+-------+(n-2)+(n-1) = (n*(n-1))/2 =n2/2-n/2 =O(n2)
Complexity Analysis: Analysis involves number of comparisons and
swaps.
How many comparisons? 1+2+3+…+(n-1)= O(n2)
How many swaps? 1+2+3+…+(n-1)= O(n2)
Example: Suppose we have 32 unsorted data.
a). How many comparisons are made by sequential search in the worst-
case? ==> Number of comparisons =32.
b). How many comparisons are made by binary search in the worst-
case? (Assuming simple sorting). ==> Number of comparisons =
Number of comparisons for sorting + Number of comparisons for
binary search = (n*(n-1))/2 + logn = 32/2(32-1)+ log 32 =16*31 + 5
c). How many comparisons are made by binary search in the worst-
case if data is found to be already sorted? ==> Number of comparisons
= log2 32 = 5.
48. 38
Chapter Three: Topic Six
Bubble sort Algorithm:
Compare each element (except the last one) with its neighbor to the
right.
If they are out of order, swap them. This puts the largest element at
the very end The last element is now in the correct and final place.
Compare each element (except the last two) with its neighbor to the
right.
If they are out of order, swap them. This puts the second largest
element before last. The last two elements are now in their correct and
final places.
Compare each element (except the last three) with its neighbor to the
right.
Continue as above until you have no unsorted elements on the left.
It is the oldest, simplest, and slowest sort in use. It works by
comparing each item in a list with an item next to it, and swap them if
required. This causes the larger values to “bubble” to the end of the
list while smaller values to “sink” towards the beginning of the list.
In general case, bubble sort has O(n2)level of complexity.
Advantage: Simplicity and ease of implementation.
Disadvantage: Horribly inefficient.
50. 40
Implementation:
1. #include <iostream>
2. using namespace std;
3. void BubbleSort(int list[]);
4. int list[] = {5, 3, 7, 4, 6};
5. int main()
6. {
7. cout << "The values before sorting are: n";
8. for(int i = 0; i < 5; i++)
9. cout << list[i] << " ";
10. BubbleSort(list);
11. cout << endl;
12. cout << "The values after sorting are: n";
13. for(int i = 0; i < 5; i++)
14. cout << list[i] << " ";
15. return 0;
51. 41
16. }
17. void BubbleSort(int list[ ])
18. {
19. int temp;
20. for (int i=n-2; i>=0; i--)
21. {
22. for(int j=0;j<=i; j++)
23. if (list[j] > list[j+1])
24. {
25. temp=list[j];
26. list[j]=list[j+1];
27. list[j+1]=temp;
28. }
29. }
30. }
Complexity Analysis:
Analysis involves number of comparisons and swaps.
How many comparisons? 1+2+3+…+(n-1)= O(n2)
How many swaps? 1+2+3+…+(n-1)= O(n2)
Chapter Three: Topic Seven
Selection Sort Algorithm
The selection sort algorithm is in many ways similar to simple
sort algorithms. The idea of algorithm is quite simple.
52. 42
Array is imaginary divided into two parts - sorted one and
unsorted one.
At the beginning, sorted part is empty, while unsorted one
contains whole array.
At every step, algorithm finds minimal element in the unsorted
part and adds it to the end of the sorted one.
When unsorted part becomes empty, algorithm stops.
Works by selecting the smallest unsorted item remaining in the
list, and then swapping it with the item in the next position to be
filled.
Similar to the more efficient insertion sort.
It yields a 60% performance improvement over the bubble sort.
Advantage: Simple and easy to implement.
Disadvantage: Inefficient for larger lists.
53. 43
Implementation:
1. #include <iostream>
2. using namespace std;
3. void selectionSort(int list[]);
4. int list[] = {5, 3, 7, 4, 6};
5. int main()
6. {
7. cout << "The values before sorting are: n";
54. 44
8. for(int i = 0; i < 5; i++)
9. cout << list[i] << " ";
10. selectionSort(list);
11. cout << endl;
12. cout << "The values after sorting are: n";
13. for(int i = 0; i < 5; i++)
14. cout << list[i] << " ";
15. return 0;
16. }
17.
18. void selectionSort(int list[ ] )
19. {
20. int minIndex, temp;
21. for (int i = 0; i <= n - 2; i++)
22. {
23. minIndex = i;
24. for (int j = i + 1; j <= n-1; j++)
25. if (list[j] < list[minIndex])
26. minIndex = j;
27. if (minIndex != i)
28. {
29. temp = list[i];
30. list[i] = list[minIndex];
31. list[minIndex] = temp;
55. 45
32. }
33. }
34. }
Complexity Analysis
Selection sort stops, when unsorted part becomes empty. As we know,
on every step number of unsorted elements decreased by one.
Therefore, selection sort makes n-1 steps (n is number of elements in
array) of outer loop, before stop.
Every step of outer loop requires finding minimum in unsorted part.
Summing up, (n - 1) + (n - 2) + ... + 1, results in O(n2) number of
comparisons.
Number of swaps may vary from zero (in case of sorted array) to n-1
(in case array was sorted in reversed order), which results in O(n)
number of swaps.
Overall algorithm complexity is O(n2).
Fact, that selection sort requires n-1 number of swaps at most, makes
it very efficient in situations, when write operation is significantly more
expensive, than read operation.
Chapter Three: Topic Eight
Insertion Sort Algorithm:
Insertion sort algorithm somewhat resembles Selection Sort and
Bubble sort.
Array is imaginary divided into two parts - sorted one and unsorted
one.
At the beginning, sorted part contains first element of the array and
unsorted one contains the rest.
56. 46
At every step, algorithm takes first element in the unsorted part and
inserts it to the right place of the sorted one.
When unsorted part becomes empty, algorithm stops.
Using binary search
It is reasonable to use binary search algorithm to find a proper place
for insertion. This variant of the insertion sort is called binary
insertion sort. After position for insertion is found, algorithm shifts
the part of the array and inserts the element. Insertion sort works by
inserting item into its proper place in the list.
Insertion sort is simply like playing cards: To sort the cards in your
hand, you extract a card, shift the remaining cards and then insert the
extracted card in the correct place. This process is repeated until all
the cards are in the correct sequence. Is over twice as fast as the
bubble sort and is just as easy to implement as the selection sort.
Advantage: Relatively simple and easy to implement.
Disadvantage: Inefficient for large lists.
60. 50
33. }
Complexity Analysis
The complexity of insertion sorting is O(n) at best case of an already
sorted array and
O(n2) at worst case, regardless of the method of insertion.
Number of comparisons may vary depending on the insertion
algorithm. O(n2) for shifting or swapping methods.
O(nlogn) for binary insertion sort.
Chapter Four: Topic One
Data Structure and its applications
There are two broad types of data structure based on their memory
allocation:
Static Data Structures
Dynamic Data Structure
Static Data Structures
Static Data Structures Are data structures that are defined & allocated
before execution, thus the size cannot be changed during time of
execution. Example: Array implementation of ADTs.
Dynamic Data Structure
Dynamic Data Structure Are data structure that can grow and shrink in
size or permits discarding of unwanted memory during execution
time.
Example: Linked list implementation of ADTs.
Structure
61. 51
Structure is a collection of data items and the data items can be of
different data type. The data item of structure is called member of the
structure.
Declaration of structure
Structure is defined using the struct keyword.
struct name
{
data type1 member 1;
data type2 member 2
.
.
.
data type n member n;
};
Example :
struct student
{
string name;
int age;
string Dept;
};
The struct keyword creates a new user defined data type that is used
to declare variable of an aggregated data type.
62. 52
Accessing Members of Structure Variables
The Dot operator (.): to access data members of structure
variables.
The Arrow operator (->): to access data members of pointer
variables pointing to the structure.
Example:
struct student stud;
struct student *studptr;
cout<<stud.name;
OR
cout<name;
Self-Referential Structures
Structures can hold pointers to instances of themselves.
Example:
struct student
{
char name[20];
int age;
char Dept[20];
struct student *next;
};
63. 53
Chapter Four: Topic Two
Linked List
Linked List is self-referential structure. It is a collection of elements
called nodes, each of which stores two types of fields. Data items and
a pointer to next node in the case of singly linked list and pointer to
previous node in the case of doubly linked list.
The data field: holds the actual elements on the list.
The pointer field: contains the address of the next and/or previous
node in the list.
Adding a node to the list
Steps
Allocate a new node.
Set the node data values and make new node point to NULL.
Make old last node’s next pointer point to the new node.
*Make the new last node’s prev pointer point to the old last node.
(This is only for Double Linked list).
Traversing through the list
To Move Forward:
Set a pointer to point to the same thing as the start (head) pointer.
If the pointer points to NULL, display the message “list is empty" and
stop.
Otherwise, move to the next node by making the pointer point to the
same thing as the next pointer of the node it is currently indicating.
To Move Forward: (Double linked list)
Set a pointer to point to the same thing as the start pointer.
64. 54
If the pointer points to NULL, display the message “list is empty" and
stop.
Set a new pointer and assign the same value as start pointer and move
forward until you find the node before the one we are considering at
the moment.
To Move Backward: (Double linked list)
Set a pointer to point to the same thing as the end (tail) pointer.
If the pointer points to NULL, display the message “list is empty" and
stop.
Otherwise, move back to the previous node by making the pointer
point to the same thing as the prev pointer of the node it is currently
indicating.
Display the content of list
Steps:
Set a temporary pointer to point to the same thing as the start
pointer.
If the pointer points to NULL, display the message "End of list"
and stop.
Otherwise, display the data values of the node pointed to by the
start pointer.
Make the temporary pointer point to the same thing as the next
pointer of the node it is currently indicating.
Jump back to step 2.
Insert at the front (beginning)
Allocate a new node.
Insert new element values.
Make the next pointer of the new node point to old head (start).
Update head (start) to point to the new node.
65. 55
Inserting at the End
Steps
Allocate a new node.
Set the node data values and make the next pointer of the new node
point to NULL.
Make old last node’s next pointer point to the new node.
Update end to point to the new node.
Insertion in the middle
66. 56
Steps:
Create a new Node
Set the node data Values
Break pointer connection
Re-connect the pointers
Types of Linked List
Single linked lists:
Doubly linked lists
Circular linked lists
Chapter Four: Topic Three
Singly Linked List
A singly linked list can be represented by a diagram like shown blow:
Start (Head): Special pointer that points to the first node of a linked
list, so that we can keep track of the linked list. The last node should
points to NULL to show that it is the last link in the chain (in the linked
list).
67. 57
According to the above example in the figure, it is the singly linked list
which has four nodes in it, each with a link to the next node in the
series (in the linked list).
C++ implementation of singly linked list:
1. struct node
2. {
3. int data;
4. node *next;
5. };
6. node *head = NULL;
Let us consider the above structure definition to perform the upcoming
linked list operations.
Operations of singly Linked List
Adding a node to the end of a singly linked list
Adding a node to the left of a specific data in a singly linked list
Adding a node to the right of a specific data in a singly linked list
Deleting a node from the end of a singly linked list
Deleting a node from the front of a singly linked list
Deleting any node using the search data from a singly linked list
Display the node from the singly linked list in a forward manner
68. 58
Chapter Four: Topic Four
Adding a node to the end of a singly linked list
1. void insert_end(int x)
2. {
3. node *temp=new node;
4. temp->data=x;
5. temp->next=NULL;
6. if(head==NULL)
7. head = temp;
8. else
9. {
10. node *temp2 = head;
11. while(temp2->next!=NULL)
12. {
13. temp2 = temp2->next;
14. }
15. temp2->next = temp;
16. }
17. }
Chapter Four: Topic Five
Adding a node to the front of a singly linked list
1. void insert_front(int x)
2. {
69. 59
3. node *temp=new node;
4. temp->data=x;
5. temp->next=NULL;
6. if(head==NULL)
7. head = temp;
8. else
9. {
10. temp->next = head;
11. head = temp;
12. }
13. }
Chapter Four: Topic Six
Adding a node to the right of a specific value in a singly linked list
1. void insert_right_y(int x, int y)
2. {
3. node *temp=new node;
4. temp->data=x;
5. temp->next=NULL;
6. if(head==NULL)
7. head = temp;
8. else
9. {
10. node *temp2 = head;
11. while(temp2->data!=y)
70. 60
12. {
13. temp2 = temp2->next;
14. }
15. temp->next = temp2->next;
16. temp2->next = temp;
17. }
18. }
Chapter Four: Topic Seven
Adding a node to the left of a specific value in a singly linked list
1. void insert_left_y(int x, int y)
2. {
3. node *temp=new node;
4. temp->data=x;
5. temp->next=NULL;
6. if(head==NULL)
7. head = temp;
8. else
9. {
10. node *temp2 = head;
11. node *temp3;
12. while(temp2->data!=y)
13. {
14. temp3 = temp2;
15. temp2 = temp2->next;
71. 61
16. }
17. temp->next = temp3->next;
18. temp3->next = temp;
19. }
20. }
Chapter Four: Topic Eight
Deleting a node from the front of a singly linked list
1. void delete_front()
2. {
3. node *temp;
4. if(head==NULL)
5. cout <<"No data insiden";
6. else
7. {
8. temp = head;
9. head = head->next;
10. delete temp;
11. }
12. }
Chapter Four: Topic Nine
Deleting a node from the end of a singly linked list
1. void delete_end()
2. {
75. 65
Chapter Four: Topic Eleven
Display in a forward manner in a singly linked list
1. void display()
2. {
3. node *temp;
4. if(head==NULL)
5. cout << "No data insiden";
6. else
7. {
8. temp = head;
9. while(temp!=NULL)
10. {
11. cout <<temp->data << endl;
12. temp = temp->next;
13. }
14. }
15. }
Chapter Four: Topic Twelve
Doubly linked lists
Each node points not only to Successor node (Next node), but also to
Predecessor node (Previous node).
There are two NULL: at the first and last nodes in the linked list.
Advantage: given a node, it is easy to visit its predecessor (previous)
node. It is convenient to traverse linked lists Forwards and Backwards.
76. 66
Operations of Doubly Linked List
Adding a node to the end of a doubly linked list
Adding a node to the front of a doubly linked list
Adding a node to the left of a specific data in a doubly linked list
Adding a node to the right of a specific data in a doubly linked list
Deleting a node from the end of a doubly linked list
Deleting a node from the front of a doubly linked list
Deleting any node using the search data from a doubly linked list
Display the node from the doubly linked list in a forward manner
Display the node from the doubly linked list in a backward manner
1. struct node
2. {
3. int data;
4. node *prev;
5. node *next;
6. };
7. node *head = NULL, *tail = NULL;
77. 67
Chapter Four: Topic Thirteen
Adding a node to the end of a doubly linked list
1. void insert_end(int x)
2. {
3. node* temp = new node;
4. temp->data = x;
5. temp->next = NULL;
6. temp->prev = NULL;
7. if (head == NULL)
8. head = tail = temp;
9. else {
10. tail->next = temp;
11. temp->prev = tail;
12. tail = temp;
13. }
14. }
Chapter Four: Topic Fourteen
Adding a node to the front of a doubly linked list
1. void insert_front(int x)
2. {
3. node* temp = new node;
87. 77
Chapter Four: Topic Twenty One
Display the node from the doubly linked list in a backward manner
1. void display_backward()
2. {
3. node *temp;
4. if(tail==NULL)
5. cout <<"No data insiden";
6. else
7. {
8. temp = tail;
9. while(temp!=NULL)
10. {
11. cout << temp->data << endl;
12. temp = temp->prev;
13. }
14. }
15. }
Chapter Four: Topic Twenty Two
Circular linked lists
Circular linked lists: The last node points to the first node of the list.
88. 78
How do we know when we have finished traversing the list? (Hint: check
if the pointer of the current node is equal to the Start (head) pointer).
Operations of Circular Singly Linked List
Adding a node to the end of a Circular singly linked list
Adding a node to the left of a specific data in a Circular singly
linked list
Adding a node to the right of a specific data in a Circular singly
linked list
Deleting a node from the end of a Circular singly linked list
Deleting a node from the front of a Circular singly linked list
Deleting any node using the search data from a Circular singly
linked list
Display the node from the Circular singly linked list in a forward
manner
1. struct node
2. {
3. int data;
4. node *next;
5. };
6. node *head = NULL;
89. 79
Chapter Four: Topic Twenty Three
Adding a node to the end of a Circular Singly linked list
1. void insert_end(int x)
2. {
3. node *temp = new node;
4. temp->data = x;
5. temp->next = temp;
6. if(head==NULL)
7. head = temp;
8. else
9. {
10. node *temp2 = head;
11. while(temp2->next!=head)
12. {
13. temp2 = temp2->next;
14. }
15. temp->next = head;
16. temp2->next = temp;
17. }
18. }
Chapter Four: Topic Twenty Four
90. 80
Adding a node to the front of a Circular Singly linked list
1. void insert_front(int x)
2. {
3. node *temp = new node;
4. temp->data = x;
5. temp->next = temp;
6. if(head==NULL)
7. head = temp;
8. else
9. {
10. node *temp2 = head;
11. while(temp2->next!=head)
12. {
13. temp2 = temp2->next;
14. }
15. temp->next = head;
16. head = temp;
17. temp2->next = temp;
18. }
19. }
Chapter Four: Topic Twenty Five
91. 81
Adding a node to the left of a specific data in a Circular Singly linked list
1. void insert_left_y(int x, int y)
2. {
3. node *temp=new node;
4. temp->data=x;
5. temp->next=temp;
6. if(head==NULL)
7. head = temp;
8. else
9. if(head->data==y)
10. {
11. node *temp2 = head;
12. while(temp2->next!=head)
13. {
14. temp2 = temp2->next;
15. }
16. temp->next = head;
17. head = temp;
18. temp2->next = temp;
19. }
20. else
21. {
98. 88
Chapter Four: Topic THIRTY
Display the node from the Circular Singly linked list in a forward manner
1. void display()
2. {
3. node *temp;
4. if(head==NULL)
5. cout <<"No data insiden";
6. else
7. {
8. temp = head;
9. while(temp->next!=head)
10. {
11. cout << temp->data << endl;
12. temp = temp->next;
13. }
14. cout << temp->data << endl;
15. }
16. }
Chapter Five: Topic One
Abstract Data Type Queue
Many times, we use a list in a way where we always add to the end, and
99. 89
always remove from the front. The first element put into the list will be
the first element we take out of the list, First-In, First-Out ("FIFO")
Queue: a more restricted List with the following constraints:
Elements are stored by order of insertion from front to back.
Items can only be added to the back of the queue.
Only the front element can be accessed or removed.
Goal: every operation on a queue should be O(1).
Operations on a queue
Offer or enqueue: add an element to the back.
Remove or dequeue: remove and return the element at the front.
peek: return (but not remove) front element: peek on an empty queue
returns null.
Other operations: isEmpty, size
Queue features
ORDERING: maintains order elements were added (new elements are
added to the end by default). O
PERATIONS:
Add element to end of list ('offer' or 'enqueue').
Remove element from beginning of list ('remove' or 'dequeue')
100. 90
examine element at beginning of list ('peek').
Clear all elements. is empty, get size.
All of these operations are efficient! O(1).
The Queue Operations
A queue is like a line of people waiting for a bank teller. The queue has a
front and a rear.
Queue Operations
New people must enter the queue at the rear. it is usually called an
enqueue operation.
When an item is taken from the queue, it always comes from the front. it
is usually called a dequeue operation.
101. 91
Chapter Five: Topic Two
Array Implementation of Queue
A queue can be implemented with an array, as shown here.
For example, this queue contains the integers 4 (at the front), 8 and 6 (at
the rear).
The easiest implementation also keeps track of the number of items in
the queue (Queue Size) and the index of the first element (at the front of
the queue), the last element (at the rear).
102. 92
A Dequeue Operation When an element leaves the queue, size is
decremented, and first changes, too.
An Enqueue Operation When an element enters the queue, size is
incremented, and last changes, too.
103. 93
Circular Array Implementation of Queue There is special behaviour at
the end of the array. For example, suppose we want to add a new
element to this queue, where the last index is [5]:
104. 94
An Enqueue Operation
The new element goes at the front of the array (if that spot isn‟t already
used):
The Operations to be performed by a Stack is listed as follows:
enqueue an element
dequeue an element
display next element to be dequeued
display all elements in the queue in a forward manner
105. 95
Chapter Five: Topic Three
enqueue an element
Array Implementation of queue in C++
int f = - 1, r = - 1, q[50], n = 50;
enqueue an element
1. void enqueue()
2. {
3. int val;
4. if (r == n - 1)
5. cout <<"Queue Overflow"<
6. else
7. {
8. if (f == - 1)
9. f = 0;
10. cout <<"Insert the element in queue : "<
11. cin>>val;
12. r++;
13. q[r] = val;
14. }
15. }
Chapter Five: Topic Four
dequeue an element
107. 97
Chapter Five: Topic Five
display next element to be dequeued
1. void display_next_element()
2. {
3. if (f == - 1)
4. cout << "Queue is empty" << endl;
5. else
6. {
7. cout << q[f]<<" ";
8. cout << endl;
9. }
10. }
Chapter Five: Topic Six
display all elements in the queue in a forward manner
1. void display_forward()
2. {
3. if (f == - 1)
4. cout << "Queue is empty" << endl;
5. else
6. {
7. cout << "Queue elements are : ";
108. 98
8. for (int i = f; i <= r; i++)
9. cout << q[i] <<" ";
10. cout << endl;
11. }
12. }
109. 99
Chapter Five: Topic Seven
display all elements in the queue in a backward manner
1. void display_backward()
2. {
3. if (r == - 1)
4. cout << "Queue is empty" << endl;
5. else
6. {
7. cout << "Queue elements are : ";
8. for (int i = r; i >= f; i--)
9. cout << q[i]<< " ";
10. cout << endl;
11. }
12. }
Chapter Five: Topic Eight
Array of Structs Implementation of a queue enqueue an element
1. struct node
2. {
3. string name;
4. int id;
5. };
110. 100
6. node q[50];
7. int f = - 1, r = - 1, n = 50;
enqueue an element
1. void enqueue()
2. {
3. int id;
4. string name;
5. if (r == n - 1)
6. cout << "Queue Overflow" << endl;
7. else
8. {
9. if (f == - 1)
10. f = 0;
11. cout << "Insert the name in queue : " << endl;
12. cin>>name;
13. cout << "Insert the id in queue : " << endl;
14. cin>>id;
15. r++;
16. q[r].name = name;
17. q[r].id = id;
18. }
19. }
111. 101
Chapter Five: Topic Nine
dequeue an element in an Array of Structs
1. void dequeue()
2. {
3. if (f == - 1 || f > r)
4. {
5. cout << "Queue Underflow ";
6. return ;
7. }
8. else
9. {
10. cout << "name deleted from queue is : " << q[f].name
<< endl;
11. cout << "ID number deleted from queue is : " << q[f].id
<< endl;
12. f++;
13. }
14. }
Chapter Five: Topic Ten
display next element to be dequeued in an Array of Structs
1. void display_next()
2. {