SlideShare a Scribd company logo
DATA STRUCTURE AND ALGORITHM-
IN23C05
Dr Sabitha Banu
Assistant Professor
PSGR Krishnammal College for Women
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 1
Course Outcomes
•Recall about the concepts of Arrays, Stack, Queue, Link List, Trees and
Graph.
•Understand sorting, searching and hashing algorithm
•Apply the data structures to solve various computing algorithms and
sorting algorithms.
•Analyze lists, queues, stacks, trees and graph according to the needs
of different applications
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 2
UNIT-I
•Introduction to Data Structure
Definition, Basic Terminology, Elementary Data Organization
Types of Data Structures- Linear & Non-Linear Data Structures
Data Structure Operations
•Algorithm Specifications:
Performance Analysis and Measurement (Time and space analysis)
• Abstract Data Types
Advantages of ADT
• Array
Representation of arrays
Types of arrays
Applications of arrays
Sparse matrix and its representation
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 3
Definition of Data Structure
•Logical or mathematical model of the organization of data elements
in the computer memory.
•Algorithms-Specific methods used to access and process the data
elements
•Organizing the data + processing the data from
memory(main+Secondary) collectively –Data Structures
•Basic building blocks of the program
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 4
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 5
Linear Data Structures
•The data is stored in linear data structures sequentially.
•These are rudimentary structures since the elements are stored
one after the other without applying any mathematical
operations.
Examples
•Arrays
•Linked Lists
•Stacks
•Queues
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 6
Non-Linear Data Structures
•Non-Linear data structures store the data in the form of a hierarchy.
•data can be found in multiple levels and are difficult to traverse
through.
Examples
•Tree
•Graph
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 7
Common Operations
✔ Traverse
✔ Search
✔ Insert
✔ Delete
✔ Sort
✔ Merge
✔ Create
✔ Retrieve
✔ Store
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 8
Algorithm
•step-by-step procedure
•Pseudo code
•Algorithm writing is a process and is executed after the problem
domain is well-defined.
•Instructions are applied on raw data –input, solution of the problem is
produced-output
•Example
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 9
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 10
•https://algodaily.com/lessons/algorithm-examples-everyday-life
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 11
Characteristics of an algorithm
•Input value must be given to solve the computational problem
•Algorithms process the data by applying some operations ,desired
output is generated
•Algorithms must terminate after some instructions and reach the
concluding state
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 12
Advantages of Algorithms:
•It is easy to understand.
•An algorithm is a step-wise representation of a solution to a given
problem.
•In Algorithm the problem is broken down into smaller pieces or steps
hence, it is easier for the programmer to convert it into an actual
program.
Disadvantages of Algorithms:
•Writing an algorithm takes a long time so it is time-consuming.
•Understanding complex logic through algorithms can be very difficult.
•Branching and Looping statements are difficult to show in Algorithms
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 13
Flowchart
•A flowchart is a graphical representation of
an algorithm.
•makes use of symbols that are connected
among them to indicate the flow of
information and processing.
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 14
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 15
Analysis of Algorithms
•Provides theoretical estimation for the required resources of an
algorithm to solve a specific computational problem.
•Analysis of algorithms is the determination of the amount of time and
space resources required to execute it.
•Efficiency(CPU, Memory ,Disk, Network )
•Time complexity
•Space complexity
•Worst, Average and Best Cases
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 16
Abstract Datatypes (ADT)
•Is a mathematical tool used to define the concept of data types and
set of operations
•Collection of data values and the functions which operate on these
values
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 17
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 18
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 19
Arrays
•fixed-size sequenced collection of variables belonging to the same data types
and stored in contiguous memory.
•Set of pairs, index or value
•The array has adjacent memory locations to store values.
•convenient structure for representing data
•Two terms to understand the concept of array are Element and Index
✔ Element − Each item stored in an array is called an element.
✔ Index − Each location of an element in an array has a numerical index, which is used to
identify the element.
data_type array_name [array_size];
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 20
• Index starts with 0.
• Array length is 10 which means it can store 10 elements.
• Each element can be accessed via its index(mapping). For example, we can fetch an
element at index 6 as 9.
#
structure ARRAY(value, index)
declare CREATE( ) array
RETRIEVE(array,index) value
STORE(array,index,value) array;
#
Need for Arrays
• number of variables used will increase
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 21
Ordered Lists
• list in which the elements must always be ordered in a particular way
• Also called as Sorted list.
Eg. (SUNDAY ,MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
SATURDAY)
Representation of arrays
✔ One dimensional array
❖ A one-dimensional array is also called a single dimensional array where the elements will be accessed in
sequential order. This type of array will be accessed by the subscript of either a column or row index. eg
a[n] or an
✔ Two dimensional array
❖ When the number of dimensions specified is more than one, then it is called as a
multi-dimensional array. Eg a[3,3] (row x column)
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 22
Eg a[3][4]
• A two-dimensional array will be accessed by using the subscript of row and column
index.
eg a[1][1]
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 23
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 24
1D or one dimensional array-
•A list of items having one variable name with only one subscript
•Computer reserves 5 storage locations
•Int number[5]={35,40,20,57,19}
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 25
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 26
Char name[10]=“well done”;
When declaring a character arrays ,allocate one extra element space
for the null terminator
When a compiler reads a character arrays it terminates with the
additional null character(‘0’)
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 27
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 28
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 29
•Two different types of initialization of arrays
• Compile time initialization
• Run time initialization
Compile time Initialization
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 30
•Run time initialization
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 31
Two Dimensional Array-
•To store a table of values
•C allows us to store the table of values using 2D or two dimensional array
•Two dimensional array can be declared as
•C places each size its own set of brackets.
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 32
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 33
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 34
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 35
Initializing an 2D array –
1. 6.
2. 7.
3.
4.
5.
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 36
Memory Layout-
•The subscripts in the 2d arrays represents the rows and columns
•The elements of the array are stored contiguously in increasing memory
locations.
•Starts to store row wise ,starting from the first row and ending with the last
row, treating each row like a simple array.
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 37
A[3][3]={{10,20,30},{40,50,60},{70,80,
90}};
Multi-dimensional array-
•C allows arrays of 3 or more dimensions.the exact limit is decided by the
compiler
•There is no limit for dimensions but compiler permits only 7-10 dimensions.
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 38
12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 39
Different types of representation
• Square matrix
• Diagonal
• Row
• Zero
• Upper triangular matrix
• Lower triangular matrix
• Scalar matrix
• identity/Unit matrix
Square matrix
• Number of rows=number of columns
Diagonal matrix
• Where diagonal elements are non-zero elements
Row matrix
Matrix consists of one row
Zero matrix
Matrix where all the elements have zero value
Upper& lower triangular matrix
● A square matrix is said to be a lower
triangular matrix if all the elements above its
main diagonal are zero.
● A square matrix is said to be an upper
triangular matrix if all the elements below
the main diagonal are zero.
Scalar matrix
Matrix where all diagonal elements are same
identity/unit matrix
All diagonal elements are 1
Unit-2
Stacks and Queues
• Stack
• Stack Representation & Implementation
• Stack Operations
• Applications of Stack
• Queue
• Queue Representation & Implementation
• Queue Operations
• Types of Queues
Stack
• simple data structure used for storing data
• In a stack, the order in which the data arrives is
important
• Definition
“A stack is an ordered list in which insertion and
deletion are done at one end, called top. The last
element inserted is the first one to be deleted.
Hence, it is called the Last in First out (LIFO) or
First in Last out (FILO) list”
• https://www.youtube.com/watch?v=-qsKQVpGPKs
Operations
• When an element is inserted in a stack, the
concept is called push
• when an element is removed from the stack, the
concept is called pop.
• Trying to pop out an empty stack is called
underflow
• trying to push an element in a full stack is called
overflow
Main stack operations
• Push (int data): Inserts data onto stack.
• int Pop(): Removes and returns the last
inserted element from the stack.
Auxiliary stack operations
• int Top(): Returns the last inserted element without removing
it.
• int Size(): Returns the number of elements stored in the
stack.
• int IsEmptyStack(): Indicates whether any elements are
stored in the stack or not.
• int IsFullStack(): Indicates whether the stack is full or not.
Stack Implementation
• Simple array /Static array based
implementation
• Dynamic /pointer/Linked lists
implementation
Simple Array Implementation
• uses an array
• In the array, elements are added from left to
right and use a variable to keep track of the
index of the top element.
• The array storing the stack elements may
become full. A push operation will then
throw a full stack exception.
• Similarly, if we try deleting an element from
an empty stack it will throw stack empty
exception.
Dynamic /pointer representation
• Do not have size limitations and use space proportional to
the actual number of elements stored in the stack
• Also called as linked representation of stack /linked stack
Linked representation
• Stack is a collection of nodes
• Each is divided into 2 parts
• Info-element ,next-pointer of the
neighbour element
• Top -start pointer of the stack
• If the last node link field contains null /Top=null-bottom of
the stack
•
Multiple Stacks
❖ a single array having more than one stack. The array is divided for
multiple stacks.
❖ If the array represents two stacks ,then two stacks can grow
in the opposite direction
❖ Suppose there is an array STACK[n] divided into two stack STACK A
and STACK B, where n = 10.
➢ STACK A expands from the left to the right, i.e., from 0th
element.
➢ STACK B expands from the right to the left, i.e., from 10th
element.
➢ The combined size of both STACK A and STACK B never
exceeds 10.
Queues
• Linear data structure in which elements can be inserted at
one end(REAR) and deleted from other end (FRONT)
• Elements are processed based on the order of arrival
• First Come First Serve (FCFS)/FIFO (First In First Out)
•
•
• Queue is an integral part of OS
• Process management and multiprogramming environment
Queue representation and
implementation
❖ Two kinds of representation
➢ Static /sequentially /array representation
➢ Dynamic /pointer /linkedlist
representation
Array representation
• A queue is implemented with max elements
array stored from queue[i] to queue[j],Max
• I,j are front(F) and Rear(R)
• Contains two pointer variables
• Front -location of the front element of the queue
• Rear-location of the rear element of the queue
Dynamic /Pointer representation
• Size of the queue is not constant which is called
dynamic
• Also called as Linked representation of queue
• Linked queue
Linked queue
• Implemented as a linked list with two pointer variables
• Front and Rear (pointing to the front and rear nodes of
the queue)
Stack vs Queue
Stack Queue
Can be implemented sequentially and
dynamically
Can be implemented sequentially and
dynamically
Are called LIFO Called FIFO
One entry and exit called Top Separate entry called Rear and separate
exit called Front
No different forms of stacks Different forms of queue circular,priority ,
dequeue
Drawback of queue
• Queue is completely full when rear is at last array
position i.e. (MaxSize -1).
• Now no more elements can be inserted in queue
even if the queue has some empty spaces.
Circular Queue
• Removes the drawback of simple queue
• the last element is connected to the first element,
creating a circular structure.
• This allows for efficient use of
memory.
• FIFO basis
•
• Let’s say the MaxSize of your queue is 5, and the rear pointer has
already reached the end of a queue.
• There is one empty space at the beginning of a queue, which
means that the front pointer is pointing to location 1.
Enqueue(x) Operation
Step 1: Check if the queue is full (Rear + 1 % Maxsize = Front)
Step 2: If the queue is full, there will be an Overflow error
Step 3: Check if the queue is empty, and set both Front and Rear
to 0
Step 4: If Rear = Maxsize - 1 & Front != 0 (rear pointer is at the
end of the queue and front is not at 0th index), then set Rear = 0
Step 5: Otherwise, set Rear = (Rear + 1) % Maxsize
Step 6: Insert the element into the queue (Queue[Rear] = x)
Step 7: Exit
Deque
❖ Double ended queue where insertion and deletion can
be performed in both ends front and rear
❖ Rear is incremented by 1 nd then it is inserted in the
new location
❖ Similarly when the element is deleted front is
incremented by 1
❖ Elements can be inserted in the front
➢ Front is decremented to 1 before insertion and the
element is inserted into the new location
➢ Front is 0 ,overflow occurs
❖ Elements can be deleted from the rear
➢ Rear is decremented by 1 after the deletion
➢ Deleted element returned to the user
Dequeue
Dequeue() Operation
Step 1: Check if the queue is empty (Front = -1 & Rear = -1)
Step 2: If the queue is empty, Underflow error
Step 3: Set Element = Queue[Front]
Step 4: If there is only one element in a queue, set both Front
and Rear to -1 (IF Front = Rear, set Front = Rear = -1)
Step 5: And if Front = Maxsize -1 set Front = 0
Step 6: Otherwise, set Front = Front + 1
Step 7: Exit
Different types of Deque
Input Restricted
Where insertion is restricted to one end (rear end )and
deletion can be done both ends,front & rear
Output Restricted
Where deletion is restricted to one end (front end )and
insertion can be done both ends,front & rear
Circular Dequeue
Implemented in Circular form,last element follows the first
element
Priority Queue
➔ Insertion and deletion is done according to the priorities
assigned
➔ Does not follow any principles FIFO
➔ Elements of higher priorities are processed first than the
elements with the lowest priorities
➔ If both have same priorities then based on the order of
insertion
➔ Used in OS for time sharing property
➔ Insertion of an element is difficult than deletion (finding out
the correct location according to the priority)
➔ Two ways
◆ Sequential /array
◆ dynamic/linked info Priority
no
pointer
Multiqueue
• Multi queue is data structure in which multiple queues are maintained.
• are utilized for process scheduling.
• one dimensional array or multidimensional array to illustrated a
multiple queue.
• Each of queues contains n elements that are mapped to a linear array of m
elements.
Applications of stack
• Factorial of a given number
• Recursion
• Infix to Postfix transformation
Recursion
• a function or method has the ability to call itself to solve the problem.
• Recursion in stack in data structure is when functions call themselves
directly or indirectly.
Factorial of a given number(Recursion)
• First, convert the given expression into special notation.
• Evaluate the expression in this new notation
• three notations to represent an arithmetic expression:
❖ Infix Notation
❖ Prefix Notation
❖ Postfix Notation
Infix
• The infix notation is a convenient way of writing an
expression in which each operator is placed
between the operands.
• Infix expressions can be parenthesized or
unparenthesized depending upon the problem
requirement
• Example A + B, (C - D)
• All these expressions are in infix notation because
the operator comes between the operands.
Prefix Notation
• The prefix notation places the operator before
the operands.
• This notation was introduced by the Polish
mathematician and hence often referred to as
polish notation
• Example: + A B, -CD etc
• All these expressions are in prefix notation
because the operator comes before the
operands.
Postfix Notation
• The postfix notation places the operator after
the operands.
• This notation is just the reverse of Polish
notation and also known as Reverse Polish
notation.
• Example: AB +, CD+, etc.
• All these expressions are in postfix notation
because the operator comes after the operands
Infix Notation
(A*B)
(A+B)/C
(A*B)+(D-C)
example of Converting an infix expression
into a postfix expression
Evaluating a postfix expression
3 + 8 - 9 / 8
= 3 + 8 - 1.125
= 11 - 1.125
= 9.875
3 + 8 - 9 / 8
Sum of n number using Recursion
#include <stdio.h>
int sum(int n);
int main() {
int number, result;
printf("Enter a positive integer: ");
scanf("%d", &number);
result = sum(number);
printf("sum = %d", result);
return 0;
}
int sum(int n) {
if (n != 0)
// sum() function calls itself
return n + sum(n-1);
else
return n;
}
Factorial of given number
#include<stdio.h>
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
void main()
{
int number;
long fact;
printf("Enter a number: ");
scanf("%d", &number);
fact = factorial(number);
printf("Factorial of %d is %ldn",
number, fact);
return 0;
}
Recursion Iteration
When a function calls itself Set of statements executed repeatedly until the condition is
satisfied
Based on base condition Initialization,termination,incrementation,updation ,execution
Top-down approach Bottom up approach
Takes more space to store new set of
variables
Does not take much space and time
Not efficient Efficient
Recursive algorithm can be converted to
iterative version
Iterative cannot be converted to recursive version
Saves the return address Does not save the return address
Uses stack for temporary storage Stacks are not required to implement iterative algorithms
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int top = -1;
/* Check if the stack is empty */
int isempty(){
if(top == -1)
return 1;
else
return 0;
}
/* Check if the stack is full */
int isfull(){
if(top == MAXSIZE)
return 1;
else
return 0;
}
/* Function to return the
topmost element in the stack */
int peek(){
return stack[top];
}
/* Function to delete from the stack
*/
int pop(){
int data;
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
} else {
printf("Could not retrieve data,
Stack is empty.n");
}
}
/* Function to insert into the stack
*/
int push(int data){
if(!isfull()) {
top = top + 1;
stack[top] = data;
} else {
printf("Could not insert data,
Stack is full.n");
}
}
/* Main function */
int main(){
push(44);
push(10);
push(62);
push(123);
push(15);
printf("Element at top of the
stack: %dn" ,peek());
printf("Elements: n");
// print stack data
while(!isempty()) {
int data = pop();
printf("%dn",data);
}
printf("Stack full: %sn" ,
isfull()?"true":"false");
printf("Stack empty: %sn" ,
isempty()?"true":"false");
return 0;
}
UNIT-III
Linked List
• Linked List as Data Structures
• Representation of Linked List
• Operations on Linked List
• Stack as Linked List
• Queue as Linked List
• Doubly Linked List
• Circular List
Linked List as Data Structures
• Collection of similar elements where each element points to the next
element
• Is a linear data structure
• Each node divided into two parts
• Info -contains the info about the items of the list stored and processed
• Next-contains the address of next adjacent node .used to access the next
data item maintained in the other node,maintains the link of the data
• A linked list can grow and shrink its size(dynamic), as per the requirement.
• It does not waste memory space.
• Data items in the list are not maintained in contiguous locations ,may placed
anywhere
• Correct order is maintained by the logical address in the next field of each
node,insertion and deletion is possible
Singly Linked List
• Access the data items of the linked list sequentially
• Random access of items is not allowed ,coz they are
linked through pointers
Represeantion of linked list
• Static /sequential/array representation
• dynamic/pointer/linked representation
Static /sequential/array representation
• Linked list can also be implemented as an array
• Data items maintained in two parallel arrays of the
same sizes
• One array for storing info field of the nodes
• Other array for the addresses
List[n] -number of nodes in the list
List[6].info=16
List[6].field=10
Dynamic /pointer representation
• Array representation is not flexible
• Size of the linkedlist may increase/decrease as per the
requirement when insertion and deletion is done
• Dynamic representation does not have size limitations
• Space proportional to the actual no of elements
• In C language, a linked list can be
implemented using structure
and pointers
• is used to create every node in
the list
• The data field stores the element
and the next is a pointer to store
the address of the next node
• In place of a data type, struct
LinkedList is written before next.
• That's because its a
self-referencing pointer. It
means a pointer that points to
whatever it is a part of. Here next
is a part of a node and it will
point to the next node.
Operations on the linkedlist
• Creation
• Traversal - To access each element of the linked list.
• Insertion - To add/insert a new node to the list.
• Deletion - To remove an existing node from the list.
• Search - To find a node in the list.
• Sort - To sort the nodes.
Create linkedlist
• Creation of the linkedlist
Array implementation-
❖ Number of nodes declared in advance
❖ Elements are entered in the contiguous
memory locations
❖ Placed sequentially in the array
❖ Maintain the list of free nodes
❖ Initially all the nodes are empty then
linked with one another in sequence
❖ next pointer=-1 denotes end of the list
Dynamic implementation
• List is created by creating the nodes of the list as
and when required
• Allocate memory dynamically
• Define the structure of a node and define
start pointer to the node type
struct Node
{
int info;
Node *next;
};
struct node *start;
● In linked lists, a struct is
used to define the structure
of each node in the list.
● Each node typically
contains two parts: the
data and a reference (or
pointer) to the next node in
the sequence.
● The struct allows you to
group these two pieces of
information together.
2. Set start =NULL to indicate the list is initially empty.
start=NULL;
3. Get the free new node from the memory heap assign its
address into pointer ptr.
struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
4. Assign the value to be inserted to the info part of the new
node pointed by ptr
ptr→info=value(first supply the value)
5.Assign NULL to the next part of the new node
ptr→next=start(ptr→next=NULL becoz start=NULL
6.Assign the address of the first node to the start variable
start=ptr
7.Return
int main() {
struct Node* head = NULL;
struct Node* second_node = NULL;
struct Node* third_node = NULL;
// Allocate memory to 3 nodes
head = (struct Node*)malloc(sizeof(struct Node));
second_node = (struct Node*)malloc(sizeof(struct
Node));
third_node = (struct Node*)malloc(sizeof(struct Node));
head->data = 12; //assign data in first node
head->next = second_node; // Link first node with the
second node.
second_node->data = 99; //assign data to second node
second_node->next = third_node; // Link second node
with the third node
third_node->data = 58; //assign data to third node
third_node->next = NULL;}
Traversing a linkedlist
• Is used to visit each node of the list exactly once in
order to access the information stored in the
nodes
• Accessing or printing the values (i.e., data items stored
inside each node) of a singly linked list exactly once
until the end node is reached.
• This accessing and printing is sometimes called “visiting”
the linked list
• Accessing or printing of nodes always takes place one by
one
• The process of visiting each node of the list once to
perform some operation on it is called traversing
1.node creation
2.memory allocation
3.setting head
4.The malloc function in C returns a pointer to the first byte of the allocated
memory block, and its return type is void *.
5.type casting
Insertion of an element into the linked list at
various positions
● Requires a free node in which the information is to be
inserted
● Then the node can be inserted into the linked list
● Pointers are adjusted
● Three possible positions where we can enter a new node
in a linked list –
★ Insertion at beginning
★ Insertion after nth position
★ Insertion at end
Steps :
1. Get the free node from memory pool and assign its address
to the ptr
2. Assign the value ptr→info=value to the info part of the new
node pointed by the ptr
3. If the linked list is empty start=NULL then assign ptr→next =
start and start =ptr
4. Otherwise adjust the pointers according to the place of
insertion.
Insertion at the beginning
• Create a new node
• Assign its data value
• Assign newly created node’s next ptr to current head
reference. So, it points to the previous start node of the
linked list address
• Change the head reference to the new node’s address.
1.
2.
void insertStart (struct Node *head, int data)
{
struct Node *newNode = (struct Node *) malloc
(sizeof (struct Node));
newNode - > data = data;
newNode - > next = *head;
//changing the new head to this freshly entered
node
*head = newNode;
}
Insertion at the end of the list
● Create a new node
● Assign its data value
● Assign its next node to NULL as this will be the last(tail) node
● Check if the list is empty
○ Change the head node to the new node
● If not then traverse till the last node
● Assign the last node’s next pointer to this new node
● Now, the new node has become the last node.
Insertion at the specified position in the
linkedlist
• First ,create a new node named by newnode and put the position
where u want to insert the node.
• Now give the address of the new node in previous node means link
the new node with previous node.
• After this, give the address of current node in new node.Means link
your new node also with current node.
Deletion of an element from the linkedlist
• No longer nodes can be deleted from the list
• Can be performed at various positions like linkedlist
• Necessary to know the position of the node to be
deleted
• Check for the underflow condition (list=NULL)
• Deletion at the beginning
• Deletion at the end
• Deletion at the middle
Deletion at the beginning of the list
• Check for the underflow condition
• First node to be deleted start pointerpoint to the
second node of the list
• After deleting save the value into a variable
• Deleted nodesent to the memory for future use
• free() ,delete()
Deletion at the end of the list
• Traverse the entire list to find the address of the second
last node to adjust the pointers
• next field of the second last node point to the next field
of the last node
• Hence the next field of the second last node will contain
now NULL
• Contains a temporary pointer to hold the address of the
preceding node (node to be deleted)
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = HEAD
Step 3: Repeat Steps 4 and 5 while PTR ->
NEXT!= NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 6: SET PREPTR -> NEXT = NULL
Step 7: FREE PTR
Step 8: EXIT
Representation of linked list
Array Pointer(linkedlist)
Fixed size,cannot grow according to the
need,hard to predict the size every time
Elements can be added indefinitely,grow during the
execution time
Static,fixed size,easy to use Dynamic,does not require to predict in advance
Deletion leaves an empty space (wasting of
memory),shifting of elements is difficult
Elements can be deleted ,can shrink during the
execution time
Insertion and deletion is slow,needs to shift
operation
Insertion and deletion is fast
Memory allocated at compile time ,memory
does not used efficiently
Efficiently utilizes the memory ,bcoz memory is
allotted at runtime
Insertion and deletion at specified position
needs resizing of the array which is not
Insertion and deletion at specified position by
adjusting the pointers ,it is flexible
Random access to the elements Allows sequential access to the elements
Suits smaller list of elements when numbers
of items in the list is known
Larger list where insertion and deletion is done
frequently
Pass through all the elements to reach kth
element
Access directly in a single operation by using the
index value
Stack as a linkedlist
Array implementation
• Size of the stack is known in advance
• Advance declaration of maximum size is
not possible all the time
• Size may increase or decrease according
to the requirement
• Size constantly change as items pushed
and popped
Dynamic representation of a stack:
• Called as linked stack/linked list
• Suits for large records and when elements are
not known in advance
• Stack is represented as linkedlist where each
node contains info,next part
• Top pointer of the stack represented by start
pointer
• If start =NULL shows stack is empty
• If last node=NULL shows the bottom of the stack
Push operation can be performed in the below steps
Step 1 − Checks stack has some space or stack is full.
Step 2 − If the stack has no space then display “overflow” and exit.
Step 3 − If the stack has space then increase top by 1 to point next empty space.
Step 4 − Adds item to the newly stack location, where top is pointing.
Step 5 – PUSH operation performed successfully.
Step 1 − Checks stack has some element or stack is
empty.
Step 2 − If the stack has no element means it is empty
then display “underflow”
Step 3 − If the stack has element some element,
accesses the data element at which top is pointing.
Step 4 − Decreases the value of top by 1.
Step 5 − POP operation performed successfully.
Queue as a linkedlist
• the array implementation can not be used for the
large scale applications where the queues are
implemented.
• One of the alternative of array implementation is
linked list implementation of queue.
• Size of the queue may increase or decrease according
to the requirement as items get inserted and deleted
• Dynamic representation of queue is called linked
queue
• Linked queue -elements size is not know in advance
may increase
Linked queue :
• In a linked queue, each node of the queue consists of two parts i.e. data part
and the link part.
• Each element of the queue points to its immediate next element in the
memory.
• In the linked queue, there are two pointers maintained in the memory i.e.
front pointer and rear pointer.
• The front pointer contains the address of the starting element of the queue
while the rear pointer contains the address of the last element of the queue.
•
• Two pointers for front and rear position of the
queue
• front=null (queue is empty)
• front=rear=null
• Accessing the nodes in the forward direction only
• Cannot traverse backward direction
Doubly Linked list
• a node consists of three parts: node data, pointer to
the next node in sequence (next pointer) , pointer to
the previous node (previous pointer).
• Each node contains 1 info field ,2 link pointers (forward &
backward)
• The pointers points to the adjacent to the current node
• Fowrdpointer points to the next node of the current node
• Backward pointer points to the preceding node of the current
node
• 2 Pointers helps in accessing successor and predecessor nodes
of any arbitrary node
Comparison
No Singly linked list Doubly linked list
1. Can move only in forward
direction
Can move both in forward and backward
direction
2. Cannot reach preceding node
fro any arbitrary node
Can reach any preceding node from any
arbitrary node
3. Insertion and deletion to the
preceding node needs
traversal of links again fro the
first node
Insertion and deletion can be easily performed
here bcoz every pointer has links to the
preceding node
Representation of DLL
• static/array/sequential representation
(implementation)
• dynamic/pointer/linked representation
(implementation)
static/array implementation
• DLL is also a collection of similar data items in an array
• Data items of the linkedlist are maintained in the form
of nodes
• Each node contains 3 parts
• Infor
• Prev
• Next
• List of 12 nodes containing integer type of info,prev,next
• In array representation start value =0 and null value =-1
• If any nodes contains -1 shws there is no successor
/predecessor node
Dynamic /pointer representation
• Do not have size limitations
• Use space proportional to the actual number of
elements in the list
DLL Operations
• Same as SLL (singly linkedlist)
• Creation
• Traversal - To access each element of the linked list.
• Insertion - To add/insert a new node to the list.
• Deletion - To remove an existing node from the list.
• Search - To find a node in the list.
• Sort - To sort the nodes.
Insertion into DLL at various positions
• Requires a freenode in which information can be
inserted
• Node can be inserted into DLL and pointers are
adjusted
•
•
Insert to the end of DLL
Algorithm
○ Step 1: IF PTR = NULL
○ Write OVERFLOW
Go to Step 11
[END OF IF]
○ Step 2: SET NEW_NODE = PTR
○ Step 3: SET PTR = PTR -> NEXT
○ Step 4: SET NEW_NODE -> DATA = VAL
○ Step 5: SET NEW_NODE -> NEXT = NULL
○ Step 6: SET TEMP = START
○ Step 7: Repeat Step 8 while TEMP
-> NEXT != NULL
○ Step 8: SET TEMP = TEMP ->
NEXT
○ [END OF LOOP]Step 9: SET TEMP
-> NEXT = NEW_NODE
○ Step 10C: SET NEW_NODE ->
PREV = TEMP
○ Step 11: EXIT
Insert after in DLL
○ Step 1: IF PTR = NULL
○ Write OVERFLOW
Go to Step 15
[END OF IF]Step 2: SET NEW_NODE =
PTR
○ Step 3: SET PTR = PTR -> NEXT
○ Step 4: SET NEW_NODE -> DATA = VAL
○ Step 5: SET TEMP = START
○ Step 6: SET I = 0
○ Step 7: REPEAT 8 to 10 until I
○ Step 8: SET TEMP = TEMP -> NEXT
○ STEP 9: IF TEMP = NULL
○ STEP 10: WRITE "LESS THAN DESIRED NO. OF
ELEMENTS"
○ GOTO STEP 15
[END OF IF]
[END OF LOOP]Step 11: SET NEW_NODE ->
NEXT = TEMP -> NEXT
○ Step 12: SET NEW_NODE -> PREV = TEMP
○ Step 13 : SET TEMP -> NEXT = NEW_NODE
○ Step 14: SET TEMP -> NEXT -> PREV =
NEW_NODE
○ Step 15: EXIT
Deletion of a node in DLL
• Deletion can be done from the beginning,end, and
at the middle of the list
• Check for the underflow condition (start=NULL)
•
Drawback
• Cannot move directly to the first node after
reaching lastnode of the list
Circular Linked list
• The first and last nodes are linked together
• Next pointer of the last node connects to the first
node instead of containing NULL
• Contains valid addresses without null
• Two types
• Singly circular linked list
• Doubly circular linked list
Singly circular linked list
• the last node of the list contains a pointer to the first
node of the list.
• traverse the circular singly linked list until we reach the
same node where we started.
• The circular singly linked list has no beginning or end.
• No null value is present in the next part of any of the
nodes.
Doubly circular linked list
• has properties of both doubly linked list and circular linked
list
• in which two consecutive elements are linked or connected
by the previous and next pointer and the last node points to
the first node by the next pointer
• also the first node points to the last node by the previous
pointer.
• CLL used to implement circular queue
• Used to maintain the process for execution in the time
sharing OS
• Each process is allotted a timestamp for execution
• Wait for the next turn in a circular queue
• All nodes are connected in the circle so any node can be
accessed from any node
Representation of CLL
• Array representation
• Pointer representation
Array representation
• Data items are maintained in the form of nodes
divided into 3 parts
• 3 parallel arrays of the same sizes
•
Pointer representation
Insertion into circular singly linked list at beginning
Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET TEMP = HEAD
Step 6: Repeat Step 8 while TEMP -> NEXT !=
HEAD
Step 7: SET TEMP = TEMP -> NEXT
[END OF LOOP]
Step 8: SET NEW_NODE -> NEXT = HEAD
Step 9: SET TEMP → NEXT = NEW_NODE
Step 10: SET HEAD = NEW_NODE
Step 11: EXIT
Deletion in circular singly linked list at beginning
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]Step 2: SET PTR = HEAD
Step 3: Repeat Step 4 while PTR → NEXT !=
HEAD
Step 4: SET PTR = PTR → next
[END OF LOOP]
Step 5: SET PTR → NEXT =
HEAD → NEXT
Step 6: FREE HEAD
Step 7: SET HEAD = PTR →
NEXT
Step 8: EXIT
Deleting nodes at given index in the Circular linked list
Step 1 START
Step 2 Store the element to be deleted.
Step 3 Store the position.
Step 4 Initialize counter c=1
Step 5 Check while (c<pos)
Step 6 Count the position of data to be deleted y = x; x = x->link; c++;
Step 7 Use free() to delete y->link = x->link; free(x);
Step 8 STOP
Deletion in Circular singly linked list at the end
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]Step 2: SET PTR = HEAD
Step 3: Repeat Steps 4 and 5 while PTR -> NEXT != HEAD
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]Step 6: SET PREPTR -> NEXT = HEAD
Step 7: FREE PTR
Step 8: EXIT
UNIT-IV
Trees
• Trees: Preliminaries
• Binary Trees
• B-Trees
• Graph
• Graph Terminologies
• Types of Graphs
• Graph Representation
• Hashing
• Hash Functions
● Sorting
● Bubble Sort
● Selection Sort
● Quick Sort
● Heap Sort
● Merge Sort
Tree
• Defined as a finite collection of special data ites called
nodes
• Nodes are arranged in hierarchical order to represent
parent-child relationship
• First node linked with some other nodes in turn linked
with other nodes
• The parent connected to its childrens through edges
Tree is a finite set of data elements called nodes such
that there is a special node called the root of the tree
and the remaining nodes are partitioned into a
number of mutually exclusive subsets which are
themselves trees.The edges that connect the nodes
are called branches , and the number of branches
connected to a particular node is called the degree of
that node .
Binary tree
• Can have maximum two
children
• Left child,right child
• Left child ,right child is also a
subtree
• Binary tree can also be defined as a collection of nodes
• Each node is divided into 3 part containing left child
address,information,right child address
•
Classification of binary trees
• Complete binary tree
• Almost complete binary tree
Complete binary tree
A complete binary tree is a special type of binary tree
where all the levels of the tree are filled completely
except the lowest level nodes which are filled from as
left as possible.
Almost complete binarytree
What Is B-tree?
• A B-tree is a data structure that provides sorted data and allows searches,
sequential access, attachments and removals in sorted order.
• The B-tree is highly capable of storing systems that write large blocks of
data.
• The B-tree simplifies the binary search tree by allowing nodes with more
than two children. Below is a B-tree example.
• B-tree stores data such that each node contains
keys in ascending order.
• Each of these keys has two references to another
two child nodes.
• The left side child node keys are less than the
current keys, and the right side child node keys
are more than the current keys.
• If a single node has “n” number of keys, then it
can have maximum “n+1” child nodes.
B-tree
• is a specialized m-way(multiple children) tree that can be widely
used for disk access.
• A B-Tree of order m can have at most m-1 keys and m children.
• using B tree is its capability to store large number of keys in a single
node and large key values by keeping the height of the tree relatively
small.
• A B tree of order m contains all the properties of an M way tree. In
addition, it contains the following properties.
1. Every node in a B-Tree contains at most m children.
2. Every node in a B-Tree except the root node and the leaf
node contain at least m/2 children.
3. The root nodes must have at least 2 nodes.
4. All leaf nodes must be at the same level.
• It is not necessary that, all the nodes contain the same
number of children but, each node must have m/2 number of
nodes.
• A B tree of order 4 is shown in the following image.
Searching
A. Searching in B Trees is similar to that in Binary search tree. For example, if
we search for an item 49 in the following B Tree. The process will something
like following :
i. Compare item 49 with root node 78. since 49 < 78 hence, move to its left sub-tree.
ii. Since, 40<49<56, traverse right sub-tree of 40.
iii. 49>45, move to right. Compare 49.
iv. match found, return.
B. Searching in a B tree depends upon the height of the tree. The search
algorithm takes O(log n) time to search any element in a B tree.
Inserting
Insertions are done at the leaf node level. The following algorithm needs to be followed in order to
insert an item into B Tree.
1. Traverse the B Tree in order to find the appropriate leaf node at which the node can be inserted.
2. If the leaf node contain less than m-1 keys then insert the element in the increasing order.
3. Else, if the leaf node contains m-1 keys, then follow the following steps.
○ Insert the new element in the increasing order of elements.
○ Split the node into the two nodes at the median.
○ Push the median element upto its parent node.
○ If the parent node also contain m-1 number of keys, then split it too by following the same
steps.
Insert the node 8 into the B Tree of order 5 shown in the following image
8 will be inserted to the right of 5, therefore insert 8
The node, now contain 5 keys which is greater than (5 -1 = 4 ) keys.
Therefore split the node from the median i.e. 8 and push it up to its parent
node shown as follows
Deletion
Deletion is also performed at the leaf nodes. The node which is to be deleted can either be a leaf
node or an internal node. Following algorithm needs to be followed in order to delete a node from
a B tree.
1. Locate the leaf node.
2. If there are more than m/2 keys in the leaf node then delete the desired key from the node.
3. If the leaf node doesn't contain m/2 keys then complete the keys by taking the element from
eight or left sibling.
○ If the left sibling contains more than m/2 elements then push its largest element up to its
parent and move the intervening element down to the node where the key is deleted.
○ If the right sibling contains more than m/2 elements then push its smallest element up to
the parent and move intervening element down to the node where the key is deleted.
1. If neither of the sibling contain more than m/2 elements then create a new leaf
node by joining two leaf nodes and the intervening element of the parent node.
2. If parent is left with less than m/2 nodes then, apply the above process on the
parent too.
If the the node which is to be deleted is an internal node, then replace the node with its
in-order successor or predecessor. Since, successor or predecessor will always be on the
leaf node hence, the process will be similar as the node is being deleted from the leaf
node.
Delete the node 53 from the B Tree of order 5 shown in the following
figure.
53 is present in the right child of element 49. Delete it.
Now, 57 is the only element which is left in the node, the minimum number
of elements that must be present in a B tree of order 5, is 2. it is less than
that, the elements in its left and right sub-tree are also not sufficient
therefore, merge it with the left sibling and intervening element of parent i.e.
49.
Graph
Types of Graph
• Simple graph
• Multigraph
• Directed graph
• Undirected graph
• Weighted graph
• Regular graph
• Complete graph
• Strongly connected graph
• Weakly connected graph
• Isomorphic graph
Simple graph
Does not contain loop or multiple edges
Weighted graph
• A graph in which the weights are assigned to
each edge
Regular Graph
• A graph in which each vertex has the same degree
‘k’
Strongly connected graph
• Where each vertex is connected to every other
vertex with the direction
Weakly connected graph
• A directed graph is weakly connected if at least two
vertices are not connected
Isomorphic graph
Two graphs in which there is a one to one
correspondence between their vertices and between
their edges
● Are the number of vertices in both graphs the same?
Yes, both graphs have 4 vertices.
● Are the number of edges in both graphs the same?
Yes, both graphs have 4 edges.
● Is the degree sequence in both graphs the same? Yes,
each vertex is of degree 2.
● If the vertices in one graph can form a cycle of length
k, can we find the same cycle length in the other
graph? Yes, each graph has a cycle of length 4.
Graph representation
• Sequential representation using adjacency
matrix
• Linked representation using adjacency list or
linked list of neighbours
Sequential representation using adjacency matrix
• use of an adjacency matrix to represent the mapping between vertices and edges of the graph.
• adjacency matrix can be used to represent the undirected graph, directed graph, weighted directed graph,
and weighted undirected graph.
• If adj[i][j] = w, it means that there is an edge exists from vertex i to vertex j with weight w.
• An entry Aij
in the adjacency matrix representation of an undirected graph G will be 1 if an edge exists
between Vi
and Vj
.
• If an Undirected Graph G consists of n vertices, then the adjacency matrix for that graph is n x n, and the
matrix A = [aij] can be defined as -
• aij
= 1 {if there is a path exists from Vi
to Vj
}
• a = 0 {Otherwise}
Linked representation using adjacency list
•
Hashing
• Hashing is a fundamental data structure that efficiently
stores and retrieves data in a way that allows for quick
access.
• It involves mapping data to a specific index in a hash table
using a hash function, enabling fast retrieval of information
based on its key.
• This method is commonly used in databases, caching
systems, and various programming applications to optimize
search and retrieval operations.
• Avoids number of comparisons
• Goes directly where the required data is present
• Searches in constant time ,no matter where the element is
The process of hashing can be broken down into three steps:
○ Input: The data to be hashed is input into the hashing algorithm.
○ Hash Function: The hashing algorithm takes the input data and
applies a mathematical function to generate a fixed-size hash
value. The hash function should be designed so that different
input values produce different hash values, and small changes in
the input produce large changes in the output.
○ Output: The hash value is returned, which is used as an index to
store or retrieve data in a data structure.
• Provides direct access to records thru hash function
• Hash functions are used in hash tables
• HT uses an array to hold ,or reference,the stored
records
• Most useful in case of large records of data are to
be stored
• Commonly used for symbol table
• HT is used to process the hash value generated by
applying some function on the key
• Determines where to store the record
• Hash function must be chosen so that its return the
value is always a valid index for the array
• When hash function maps to the same
location in the hash table for two different
keys -collision
• Eg ( 0012 ,1112 )%100 =12
• Hash function never provides collision is
called a perfect hash function
• Difficult to find perfect function
• HF Minimizes collision by spreading elements
uniformly throughout the array -good hash
function
Characteristics of a good hash function
• Formed using mathematical transformation
• Should be very easy and quick to compute
• Produce relatively random and unique
distribution of values within the hash table
so that it produces as few collisions as
possible
• Must minimize collisions
Hash functions
• Division method
• Mid square method
• Folding method
Division
•Hash function obtained by using the modulo(mod) operator
•The value is divided by some number M(size of the hash table)
and the remainder is used as the hash address for X
Example
Size of Hash Table (m) = 1000 (0 - 999)
Suppose we want to calculate the index of element x, where x =
123789456
index =123789456 mod 1000
= 456
The element x is stored at position 456 in the hash table.
Mid Square method
•Mid-Square(f
m
) hashing is a hashing technique in which unique
keys are generated.
•a seed value is taken and it is squared.
•Then, some digits from the middle are extracted. These
extracted digits form a number which is taken as the new seed.
•This technique can generate keys with high randomness if a big
enough seed value is taken.
•This process is repeated as many times as a key is required.
Folding method
•The key k is partitioned into a number of parts k
1
, k
2
.... k
n
where each part except possibly the last, has the same
number of digits as the required address.
•Then the parts are added together, ignoring the last carry.
•There are two type of folding:
• ØShift –all are added except least bit
• ØBoundary-Alternate pieces are flipped on the boundary.
Boundary folding is indicated by p_i^r
The task is to fold the key 452378912 into a Hash Table of ten spaces (0
through 9).
● It is given that the key, say X is 452378912 and the table size (i.e., M =
10).
● Since it can break X into three parts in any order. Let’s divide it evenly.
● Therefore, a = 452, b = 378, c = 912.
● Now, H(x) = (a + b + c) mod M i.e., H(452378912) =(452 + 378 + 912)
mod 10 = 1742 mod 10 = 2.
● Hence, 452378912 is inserted into the table at address 2.
Sorting
• Retrieval and searching of data in a particular order
becomes easier
• Sorting is classified into
• Internal sorting and
• External Sorting
Internal Sorting
• Elements to be sorted in main memory
• Where the list of all elements is small
so the sorting can be carried in main
memory
External Sorting
• Elements sorted in secondary memory
• Eg disks and tapes
• Applied to large file of records
Sorting algorithms
• Bubble sort
• Selection sort
• Quick sort
• Heap sort
• Merge sort
Bubble sort
• Easy to understand and implement
• Multiple swapping takes place while
passing through the elements
sequentially
• In each pass compare the element with
its successor and interchange if they are
not in proper order
• Interchange is done bubbling up the
elements
• Process is carried on till it is sorted
Time and Space Complexity
• When bubble sort is used on an array already in ascending
order, it requires only one pass through the entire array.
• This is considered the best-case scenario. In practice, though,
this only occurs sometimes, and bubble sort usually
necessitates n(n-1)/2 swaps or comparisons to achieve a
sorted array.
• In terms of space complexity, since only swapped the
elements with one another and never stored
anything, don’t need any extra space to run the
algorithm.
• This is amazing because it means the space
complexity comes out as constant, or O(1).
• This makes it an in-place algorithm that works by
modifying the input directly.
https://ds1-iiith.vlabs.ac.in/exp/bubble-sort/bubble-sort/bsdemo.html
Selection sort
• Search for the smallest element in the list and
interchange it with the first(0th) position
• This process is repeated for the remaining
unsorted portion until the entire list is sorted.
Time Complexity: The time complexity of Selection Sort is O(N2) as
there are two nested loops:
● One loop to select an element of Array one by one = O(N)
● Another loop to compare that element with every other Array
element = O(N)
● Therefore overall complexity = O(N) * O(N) = O(N*N) = O(N2)
Auxiliary Space: O(1) as the only extra memory used is for
temporary variables while swapping two values in Array.
The selection sort never makes more than O(N) swaps and
can be useful when memory writing is costly.
Quick Sort
• Popular and widely used algorithm
• Developed by CAR Hoare in 1962
• Quickest in average case scenario
• Works on divide and conquer strategy
• Larger problems are divided into sub problems and
finding solutions for the sub problems
• Solutions of the smaller subproblems are combined
to obtain the solution of the whole problem
• Selects a key element called pivot
Divide: In Divide, first pick a pivot element. After that, partition or
rearrange the array into two sub-arrays such that each element in the left
sub-array is less than or equal to the pivot element and each element in
the right sub-array is larger than the pivot element.
Conquer: Recursively, sort two subarrays with Quicksort.
function quickSort(arr):
if length(arr) <= 1:
return arr
else:
pivot = selectPivot(arr) // Choose pivot element
left = [elements in arr less than pivot]
right = [elements in arr greater than pivot]
return concatenate(quickSort(left), pivot,
quickSort(right))
Heap Sort
•Heap is a tree-based data structure in which all the tree nodes are in a
particular order, such that the tree satisfies the heap properties
•Heap sort may be regarded as two stage method
• It is converted to heap with the property that the value of each node is at least
as large as the value of its children nodes .root is the largest key in the tree
• The output sequence is generated in decreasing order by successively
outputting the root and restructuring the remaining tree into a heap
•Follow the given steps to solve the problem:
• Build a max heap from the input data.
• At this point, the maximum element is stored at the root of the heap. Replace it
with the last item of the heap followed by reducing the size of the heap by 1.
Finally, heapify the root of the tree.
• Repeat step 2 while the size of the heap is greater than 1.
Sorting heap by repeatedly deleting the heap
Merge Sort
• External sorting
• Divide and conquer strategy
• that works by dividing an array into smaller
subarrays, sorting each subarray, and then
merging the sorted subarrays back together to
form the final sorted array.
UNIT-V
❖ Algorithm Design Techniques:
➢ Greedy Algorithms
➢ Prim’s Algorithm,
➢ Kruskal’s Algorithm.
❖ Divide and Conquer:
➢ Running Time of Divide and conquer algorithms.
❖ Decrease and Conquer
➢ Depth First Search
➢ Breadth First Search.
❖ Backtracking Algorithms
➢ n Queens Problem
➢ Branch and Bound
➢ Traveling Salesman Problem
Algorithm Design Techniques
What is an algorithm design technique?
An algorithm design technique (or “strategy” or
“paradigm”) is a general approach to solving
problems algorithmically that is applicable to a
variety of problems from different areas of
computing.
• they provide guidance for designing
algorithms for new problems, i.e., problems
for which there is no known satisfactory
algorithm.
• algorithms are the cornerstone of computer
science.
• Algorithm design techniques make it possible
to classify algorithms according to an
underlying design idea
• The classification of algorithms is important for several reasons:
Organization: Algorithms can be very complex and by classifying them, it
becomes easier to organize, understand, and compare different algorithms.
Problem Solving: Different problems require different algorithms, and by
having a classification, it can help identify the best algorithm for a particular
problem.
Performance Comparison: By classifying algorithms, it is possible to compare
their performance in terms of time and space complexity, making it easier to
choose the best algorithm for a particular use case.
Reusability: By classifying algorithms, it becomes easier to re-use existing
algorithms for similar problems, thereby reducing development time and
improving efficiency.
Research: Classifying algorithms is essential for research and development in
computer science, as it helps to identify new algorithms and improve existing
ones.
Classification by Design Method
• Greedy Method: In the greedy method, at each step, a decision is made to choose the local
optimum, without thinking about the future consequences.
• Divide and Conquer: The Divide and Conquer strategy involves dividing the problem into
sub-problem, recursively solving them, and then recombining them for the final answer.
• Dynamic Programming: The approach of Dynamic programming is similar to divide and conquer.
The difference is that whenever we have recursive function calls with the same result, instead of
calling them again we try to store the result in a data structure in the form of a table and retrieve the
results from the table. Thus, the overall time complexity is reduced. “Dynamic” means we
dynamically decide, whether to call a function or retrieve values from the table.
• Linear Programming: In Linear Programming, there are inequalities in terms of inputs and
maximizing or minimizing some linear functions of inputs.
• Reduction(Transform and Conquer): In this method, we solve a difficult problem by transforming it into a
known problem for which we have an optimal solution. Basically, the goal is to find a reducing algorithm
whose complexity is not dominated by the resulting reduced algorithms.
• Backtracking: This technique is very useful in solving combinatorial problems that have a single unique
solution. Where we have to find the correct combination of steps that lead to fulfillment of the task.
Such problems have multiple stages and there are multiple options at each stage. This approach is based
on exploring each available option at every stage one-by-one. While exploring an option if a point is
reached that doesn’t seem to lead to the solution, the program control backtracks one step, and starts
exploring the next option. In this way, the program explores all possible course of actions and finds the
route that leads to the solution.
• Branch and Bound: This technique is very useful in solving combinatorial optimization problem that
have multiple solutions and we are interested in find the most optimum solution. In this approach, the
entire solution space is represented in the form of a state space tree. As the program progresses each
state combination is explored, and the previous solution is replaced by new one if it is not the optimal
than the current solution.
There are two approaches for designing an algorithm. these approaches include
1. Top-Down Approach :
2. Bottom-up approach
● Top-Down Approach: In the top-down approach, a large problem is divided
into small sub-problem. and keep repeating the process of decomposing
problems until the complex problem is solved.
● Bottom-up approach: The bottom-up approach is also known as the reverse
of top-down approaches.In approach different, part of a complex program is
solved using a programming language and then this is combined into a
complete program.
Top-Down Approach:
Breaking down a complex problem into smaller, more manageable
sub-problems and solving each sub-problem individually.
Designing a system starting from the highest level of abstraction and moving
towards the lower levels.
Bottom-Up Approach:
Building a system by starting with the individual components and gradually
integrating them to form a larger system.
Solving sub-problems first and then using the solutions to build up to a solution
of a larger problem.
Note: Both approaches have their own advantages and disadvantages and the
choice between them often depends on the specific problem being solved.
Greedy Algorithms
• to solving a problem that selects the most appropriate option based on the
current situation.
• ignores the fact that the current best result may not bring about the overall
optimal result.
• Even if the initial decision was incorrect, the algorithm never reverses it.
• intuitive algorithm can be applied to solve any optimization problem which
requires the maximum or minimum optimum result.
• easy to understand and implement.
a greedy solution only if the problem statement follows two properties
mentioned below:
● Greedy Choice Property: Choosing the best option at each phase can
lead to a global (overall) optimal solution.
● Optimal Substructure: If an optimal solution to the complete
problem contains the optimal solutions to the subproblems, the
problem has an optimal substructure.
Limitations of Greedy Algorithm
Factors listed below are the limitations of a greedy algorithm:
1. The greedy algorithm makes judgments based on the information at
each iteration without considering the broader problem; hence it
does not produce the best answer for every problem.
2. The problematic part for a greedy algorithm is analyzing its accuracy.
Even with the proper solution, it is difficult to demonstrate why it is
accurate.
3. Optimization problems (Dijkstra’s Algorithm) with negative graph
edges cannot be solved using a greedy algorithm.
• The main disadvantage of using a greedy algorithm
is that it may not find the optimal solution to a
problem.
• In other words, it may not produce the best
possible outcome.
• Additionally, greedy algorithms can be very
sensitive to changes in input data — even a small
change can cause the algorithm to produce a
completely different result.
• Finally, greedy algorithms can be difficult to
implement and understand.
Prim's Algorithm
• used to find the minimum spanning tree from a graph.
• Prim's algorithm finds the subset of edges that includes every vertex
of the graph such that the sum of the weights of the edges can be
minimized.
• Prim's algorithm starts with the single node and explores all the
adjacent nodes with all the connecting edges at every step.
• The edges with the minimal weights causing no cycles in the graph
got selected.
Spanning tree - A spanning tree is the subgraph of
an undirected connected graph.
Minimum Spanning tree - Minimum spanning tree
can be defined as the spanning tree in which the
sum of the weights of the edge is minimum. The
weight of the spanning tree is the sum of the
weights given to the edges of the spanning tree.
The first spanning tree has the least weight i.e. 10 and
hence is the minimum spanning tree.
Kruskal Algorithm
• Another minimum spanning tree problem that also always yields an
optimal solution.
• It is named Kruskal’s algorithm after Joseph Kruskal
• Kruskal’s algorithm looks at a minimum spanning tree of a weighted
connected graph G = ⟨V, E⟩ as an acyclic subgraph with |V | − 1
edges for which the sum of the edge weights is the smallest.
• The algorithm begins by sorting the graph’s edges in nondecreasing
order of their weights.
• Then, starting with the empty subgraph, it scans this sorted
list,adding the next edge on the list to the current subgraph if such
an inclusion does not create a cycle and simply skipping the edge
otherwise.
How does Kruskal's algorithm work?
In Kruskal's algorithm, we start from edges with the lowest weight and keep
adding the edges until the goal is reached. The steps to implement Kruskal's
algorithm are listed as follows -
○ First, sort all the edges from low weight to high.
○ Now, take the edge with the lowest weight and add it to the spanning
tree. If the edge to be added creates a cycle, then reject the edge.
○ Continue to add the edges until we reach all vertices, and a minimum
spanning tree is created.
The applications of Kruskal's algorithm are -
○ Kruskal's algorithm can be used to layout electrical wiring
among cities.
○ It can be used to lay down LAN connections.
Step 5 - After that, pick the edge AE with weight 5. Including this edge will
create the cycle, so discard it.
Step 6 - Pick the edge AC with weight 7. Including this edge will create the
cycle, so discard it.
Step 7 - Pick the edge AD with weight 10. Including this edge will also create
the cycle, so discard it.
So, the final minimum spanning tree obtained from the given weighted graph
by using Kruskal's algorithm is -
○ Time Complexity
The time complexity of Kruskal's algorithm is O(E logE) or
O(V logV), where E is the no. of edges, and V is the no. of
vertices.
Running Time of Divide and conquer algorithms.
• The divide and conquer approach as the name
suggests divides the given problem in parts and then
each problem is solved independently.
• When we keep dividing the problem into smaller
parts a moment comes when the problem cannot
be divided further into smaller part, then those
smaller parts are solved and the solution of all those
smaller parts or sub-parts is finally merged to obtain
the solution of the original problem.
Divide and Conquer Algorithm contains the following
steps:
1. Divide: This involves dividing the problem into smaller sub
problem.
2. Conquer: Solving the smaller sub-problems recursively.
3. Combine: Combine the solutions of the sub-problems that are
part of the recursive process to solve the actual problem.
Advantages of Divide and Conquer:
1. This algorithm makes the given problem easier as it divides the given problem into sub-problems
which makes it easier to solve and then solving each of them individually and combining all the
solution of all sub-problem into one to solve the original problem.
2. The algorithm increases the efficiency of other algorithms such as quick sort, merge sort etc.
These algorithms are the application of Divide and Conquer Algorithm.
3. This algorithm is supported by most of the processors especially with shared memory system
where data communication between processors does not need to be pre-programmed.
4. This algorithm uses memory caches in an efficient manner.
Disadvantages of Divide and Conquer:
1. The main problem which arises with this algorithm is the recursion is slow that increases the
time complexity.
2. Another problem which this algorithm is that sometimes it becomes complicated while
solving the problem through this approach and the basic iterative approach seems more
easier.
3. While solving the problems through this approach sometimes there are same sub-problems.
So its best to save the solution to the repeated sub problem.
4. While using this algorithms, make sure there is enough memory allocated to the return stack;
Divide and Conquer Applications:
• Binary Search
• Merge Sort
• Quick Sort
• Strassen’s Matrix multiplication
Time Complexity
The complexity of the divide and conquer algorithm is calculated
using the master theorem which is as follow.
Decrease and Conquer
make the problem smaller by reducing problem at each
step. They can reduce the problem by
● constant amount
● constant factor
● variable factor
• used to solve problems by reducing the size of the input data at each step of the
solution process.
• This technique is similar to divide-and-conquer, in that it breaks down a problem into
smaller subproblems, but the difference is that in decrease-and-conquer, the size of
the input data is reduced at each step.
• The technique is used when it’s easier to solve a smaller version of the problem, and
the solution to the smaller problem can be used to find the solution to the original
problem.
1. binary search, finding the maximum or minimum element in an array, and finding
the closest pair of points in a set of points.
2. The main advantage of decrease-and-conquer is that it often leads to efficient
algorithms, as the size of the input data is reduced at each step, reducing the time
and space complexity of the solution.
Insertion Sort
To sort an array of size N in ascending order iterate
over the array and compare the current element (key)
to its predecessor, if the key element is smaller than
its predecessor, compare it to the elements before.
Move the greater elements one position up to make
space for the swapped element.
1. defines a function named insertion_sort that takes a single
argument arr, which is expected to be a list.
2. calculates the length of the input list arr and assigns it to
the variable n.
3. initiates a loop that iterates over the indices of the list
arr starting from index 1 up to (but not including) n.
4. selects the current element (key) at index i from the list
arr. This element will be compared and inserted into its
correct position in the sorted sublist.
5. initializes a variable j to the index directly before i. j will
be used to traverse the sorted sublist from right to left
to find the correct position for key.
def insertion_sort(arr):
n = len(arr)
for i in range(1, n):
key = arr[i]
j = i - 1
while j >= 0 and arr[j] >
key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
1. starts a while loop that continues as
long as j is greater than or equal to 0
(ensuring we don't go out of bounds
of the list) and the element at index j
is greater than the key value. This loop
moves elements of the sorted sublist
greater than key one position to the
right.
2. Within the while loop, these two lines
shift elements of the sorted sublist to
the right to make space for the key
element. j is decremented to continue
traversing the sorted sublist.
3. nce the correct position for key is
found (either at index j+1 or 0 if j
becomes -1), key is inserted into the
sorted sublist at index j+1.
def insertion_sort(arr):
n = len(arr)
for i in range(1, n):
key = arr[i]
j = i - 1
while j >= 0 and arr[j] >
key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
Breadth First Search
• It is also known as level order traversal.
• The Queue data structure is used for the Breadth First Search
traversal.
• using the BFS algorithm for the traversal in a graph,consider
any node as a root node and explores all the neighboring
nodes.
• Then, it selects the nearest node and explores all the
unexplored nodes.
• While using BFS for traversal, any node in the graph can be
considered as the root node.
• BFS is the most commonly used approach. It is a recursive
algorithm to search all the vertices of a tree or graph data
structure.
• BFS puts every vertex of the graph into two categories - visited
and non-visited.
• It selects a single node in a graph and, after that, visits all the
nodes adjacent to the selected node.
• Breadth-First Search uses a queue data structure to store the node
and mark it as "visited" until it marks all the neighboring vertices
directly related to it.
• The queue operates on the First In First Out (FIFO) principle, so the
How Does the Algorithm Operate?
● Start with the source node.
● Add that node at the front of the queue to the visited list.
● Make a list of the nodes as visited that are close to that
vertex.
● And dequeue the nodes once they are visited.
● Repeat the actions until the queue is empty.
Complexity Of Breadth-First Search Algorithm
• The time complexity of the breadth-first search algorithm can be stated
as O(|V|+|E|) because, in the worst case, it will explore every vertex
and edge.
• The number of vertices in the graph is |V|, while the edges are |E|.
• the space complexity as O(|V|), where |V| is the number of vertices in
the graph, and different data structures are needed to determine which
vertices have already been added to the queue.
BFS applications
• GPS Navigation System
• Broadcasting Network
• Identifying Routes
Depth First Search (DFS)
• It is a recursive algorithm to search all the vertices of a tree data
structure or a graph.
• The depth-first search (DFS) algorithm starts with the initial
node of graph G and goes deeper until we find the goal node or
the node with no children.
• Because of the recursive nature, stack data structure can be
used to implement the DFS algorithm.
• The process of implementing the DFS is similar to the BFS
algorithm
The step by step process to implement the DFS traversal is given as follows -
1. First, create a stack with the total number of vertices in the graph.
2. Now, choose any vertex as the starting point of traversal, and push that vertex
into the stack.
3. After that, push a non-visited vertex (adjacent to the vertex on the top of the
stack) to the top of the stack.
4. Now, repeat steps 3 and 4 until no vertices are left to visit from the vertex on
the stack's top.
5. If no vertex is left, go back and pop a vertex from the stack.
6. Repeat steps 2, 3, and 4 until the stack is empty.
DFS Applications
○ DFS algorithm can be used to implement the topological
sorting.
○ It can be used to find the paths between two vertices.
○ It can also be used to detect cycles in the graph.
○ DFS algorithm is also used for one solution puzzles.
Complexity of Depth-first search algorithm
The time complexity of the DFS algorithm is O(V+E),
where V is the number of vertices and E is the number of
edges in the graph.
The space complexity of the DFS algorithm is O(V).
Backtracking algorithms
• used to solve the problem.
• uses Brute force search to solve the problem,
• to make all the possible solutions and pick out the best
solution from all the desired solutions.
• This rule is also followed in dynamic programming, but
dynamic programming is used for solving optimization
problems.
• In contrast, backtracking is not used in solving optimization
problems.
• Backtracking is used when having multiple solutions, and
require all those solutions.
• It finds a solution by building a solution step by step, increasing levels over time,
using recursive calling.
• A search tree known as the state-space tree is used to find these solutions.
• Each branch in a state-space tree represents a variable, and each level represents a
solution.
• A backtracking algorithm uses the depth-first search method.
• When the algorithm begins to explore the solutions, the abounding function is
applied so that the algorithm can determine whether the proposed solution satisfies
the constraints.
• If it does, it will keep looking. If it does not, the branch is removed, and the algorithm
How Does a Backtracking Algorithm Work?
In any backtracking
algorithm, the algorithm
seeks a path to a feasible
solution that includes some
intermediate checkpoints.
If the checkpoints do not lead
to a viable solution, the
problem can return to the
checkpoints and take another
path to find a solution
Examples
• To Find All Hamiltonian Paths Present in a Graph.
• To Solve the N Queen Problem.
• Maze Solving Problems
To Find All Hamiltonian Paths Present in a Graph.
A Hamiltonian path, also
known as a Hamilton path, is
a graph path connecting two
graph vertices that visit each
vertex exactly once. If a
Hamiltonian way exists with
adjacent endpoints, the
resulting graph cycle is a
Hamiltonian or Hamiltonian
cycle.
To Solve the N Queen Problem
● The problem of placing n queens on the
nxn chessboard so that no two queens
attack each other is known as the
n-queens puzzle.
● Return all distinct solutions to the
n-queens puzzle given an integer n. You
are free to return the answer in any order.
● Each solution has a unique board
configuration for the placement of the
n-queens, where 'Q' and. '' represent a
queen and a space, respectively.
N-Queens problem
• N - Queens problem is to place n - queens in such a manner
on an n x n chessboard that no queens attack each other by
being in the same row, column or diagonal.
• Problem Statement: We need to find out all the possible
arrangements in which N queens can be seated in each row
and each column so that all queens are safe. The queen
moves in 8 directions and can directly attack in these 8
directions only.
• his problem demands us to put 4 queens on 4 X 4 chessboard
in such a way that one queen is present in each row and
column and no queen can attack any other queen directly.
this means no 2 or more queens can be placed in the same
diagonal or row or column.
Let's try to put queens Q1, Q2, Q3, and Q4 in the above
present chessboard. The first queen i.e. Q1 can be put
anywhere on the chessboard as there is no other queen
present on the board and hence no restrictions. Therefore
putting Q1 at position (0,0). So the path so far is| (0,0)|.
When Q1 has been placed there are some places where the next
queens can't be placed to fulfill given conditions. So to put queen Q2
in the second row we have positions - (1,2) and (1,3). Let's put it at
(1,2).The path so far is | (0,0) -> (1,2)|
Now this placement of Q2 blocks all the boxes of row 3 and hence there is no
way to put Q3. If we put it at (2,0) or (2,2), Q1 will attack it, and at (2,1) and
(2,3) Q2 attacks it. Therefore we backtrack from here and revisit the previous
solution by readjusting the position of Q2. So instead of putting it at (1,2), we
put it at (1,3). The path so far is | (0,0) -> (1,3)|
put Q3 at (2,1). Hence, the path so far is | (0,0) -> (1,3) -> (2,1)|.
Now again the same problem occurs, there left no box to place Q4.
There was only 1 way to place Q3 and all placements of Q2 have been
explored, so now we come to Q1 for re-adjustment. We move it from
(0,0) to (0,1). The path so far is | (0,1)|.
put Q2 at (1,0). The path so far is | (0,1) -> (1,0)|.
q3 is put at (2,2). The path so far is | (0,1) -> (1,0) -> (2,2)|.Q
Now again there is no space left for placement of Q4 in row 4. Therefore we
again backtrack and readjust position of Q2 from (1,0) to (1,3).The path so far
is | (0,1) -> (1,3)|.
Q3 is put at (2,0). The path so far is | (0,1) -> (1,0) -> (2,0)|.
put Q4 at (3,2). The path so far is | (0,1) -> (1,0) -> (2,0) -> (3,2)|.
Therefore through backtracking, we reached a solution where 4 queens are
put in each row and column so that no queen is attacking any other on a 4
X 4 chessboard.
N Queen Problem Algorithm
1. create a board of N x N size that stores characters. It will store 'Q' if
the queen has been placed at that position else '.'
2. create a recursive function called "solve" that takes board and
column and all Boards (that stores all the possible arrangements) as
arguments. We will pass the column as 0 so that we can start
exploring the arrangements from column 1.
3. In solve function will go row by row for each column and will check
if that particular cell is safe or not for the placement of the queen,
we will do so with the help of isSafe() function.
4. For each possible cell where the queen is going to be placed, will
first check isSafe() function.
5. If the cell is safe, we put 'Q' in that row and column of the board
and again call the solve function by incrementing the column by 1.
6. Whenever we reach a position where the column becomes
equal to board length, this implies that all the columns and possible
arrangements have been explored, and so we return.
7. Coming on to the boolean isSafe() function, we check if a queen
is already present in that row/ column/upper left diagonal/lower
left diagonal/upper right diagonal /lower right diagonal. If the
queen is present in any of the directions, we return false. Else we
put board[row][col] = 'Q' and return true.
Branch and Bound
• is one of the techniques used for problem solving.
• It is similar to the backtracking since it also uses
the state space tree.
• It is used for solving the optimization problems
and minimization problems.
• given a maximization problem then can convert it
using the Branch and bound technique by simply
converting the problem into a maximization
problem.
• Branch and bound algorithms are used to find the optimal
solution for combinatory, discrete, and general
mathematical optimization problems.
• the branch and bound algorithm extends all the paths it can
follow. That means it visits all the nodes and all the paths to
generate the shortest distance.
Travelling Salesman Problem
given-
● A set of some cities
● Distance between every pair of cities
Travelling Salesman Problem states-
● A salesman has to visit every city exactly once.
● He has to come back to the city from where he starts his journey.
● What is the shortest possible route that the salesman must follow to complete his tour?
Example:
Consider a salesman who needs to visit 4 cities: A, B, C, and D. The distances
between these cities are as follows:
● A to B: 10 units
● A to C: 15 units
● A to D: 20 units
● B to C: 35 units
● B to D: 25 units
● C to D: 30 units
The goal is to find the shortest route that visits each city exactly once and returns
to the starting point.
Steps to Solve TSP:
• Generate Permutations: Generate all possible permutations of
the cities. Each permutation represents a possible route that
the salesman can take.
• Calculate Route Lengths: Calculate the total distance for each
permutation. Add up the distances between consecutive cities
in the permutation and add the distance from the last city back
to the starting city.
• Find the Shortest Route: Identify the permutation with the
shortest total distance. This represents the optimal route for
the salesman.
Applying Steps to the Example:
Generate Permutations:
● Possible permutations: ABCD, ABDC, ACBD, ACDB, ..., DCBA
● There are 4! = 24 possible permutations in total.
Calculate Route Lengths:
● For each permutation, calculate the total distance.
● For example, for the permutation ABCD, the total distance = 10 + 15 + 20 +
30 + 10 = 85 units.
Find the Shortest Route:
● Compare the total distances calculated for each permutation.
● Identify the permutation with the shortest total distance. This represents
the shortest route.
Portions Completed

More Related Content

Similar to DATA STRUCTURE AND ALGORITHM for beginners

dsa.ppt
dsa.pptdsa.ppt
dsa.ppt
kabhinavin
 
Data Structures - Lecture 1 - Unit 1.pptx
Data Structures  - Lecture 1 - Unit 1.pptxData Structures  - Lecture 1 - Unit 1.pptx
Data Structures - Lecture 1 - Unit 1.pptx
DanielNesaKumarC
 
Data structure ppt
Data structure pptData structure ppt
Data structure ppt
Prof. Dr. K. Adisesha
 
Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptx
GIRISHKUMARBC1
 
Database design for HPC
Database design for HPCDatabase design for HPC
Array 2 hina
Array 2 hina Array 2 hina
Array 2 hina
heena94
 
Array
ArrayArray
Array
Deep Shah
 
Lecture_1_Introduction to Data Structures and Algorithm.pptx
Lecture_1_Introduction to Data Structures and Algorithm.pptxLecture_1_Introduction to Data Structures and Algorithm.pptx
Lecture_1_Introduction to Data Structures and Algorithm.pptx
mueedmughal88
 
data science
data sciencedata science
data science
KamleshParihar12
 
Data Analytics with R and SQL Server
Data Analytics with R and SQL ServerData Analytics with R and SQL Server
Data Analytics with R and SQL Server
Stéphane Fréchette
 
data structure
data structuredata structure
data structure
hashim102
 
data structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptxdata structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptx
coc7987515756
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm.
Abdul salam
 
datastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptxdatastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptx
DEEPAK948083
 
project on data structures and algorithm
project on data structures and algorithmproject on data structures and algorithm
project on data structures and algorithm
AnujKumar566766
 
K Means Clustering Algorithm for Partitioning Data Sets Evaluated From Horizo...
K Means Clustering Algorithm for Partitioning Data Sets Evaluated From Horizo...K Means Clustering Algorithm for Partitioning Data Sets Evaluated From Horizo...
K Means Clustering Algorithm for Partitioning Data Sets Evaluated From Horizo...
IOSR Journals
 
12000121037.pdf
12000121037.pdf12000121037.pdf
12000121037.pdf
AVIWORLD1
 
ML SFCSE.pptx
ML SFCSE.pptxML SFCSE.pptx
ML SFCSE.pptx
NIKHILGR3
 
Data structures cs301 power point slides lecture 01
Data structures   cs301 power point slides lecture 01Data structures   cs301 power point slides lecture 01
Data structures cs301 power point slides lecture 01
shaziabibi5
 
Introduction to DS.pptx
Introduction to DS.pptxIntroduction to DS.pptx
Introduction to DS.pptx
OnkarModhave
 

Similar to DATA STRUCTURE AND ALGORITHM for beginners (20)

dsa.ppt
dsa.pptdsa.ppt
dsa.ppt
 
Data Structures - Lecture 1 - Unit 1.pptx
Data Structures  - Lecture 1 - Unit 1.pptxData Structures  - Lecture 1 - Unit 1.pptx
Data Structures - Lecture 1 - Unit 1.pptx
 
Data structure ppt
Data structure pptData structure ppt
Data structure ppt
 
Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptx
 
Database design for HPC
Database design for HPCDatabase design for HPC
Database design for HPC
 
Array 2 hina
Array 2 hina Array 2 hina
Array 2 hina
 
Array
ArrayArray
Array
 
Lecture_1_Introduction to Data Structures and Algorithm.pptx
Lecture_1_Introduction to Data Structures and Algorithm.pptxLecture_1_Introduction to Data Structures and Algorithm.pptx
Lecture_1_Introduction to Data Structures and Algorithm.pptx
 
data science
data sciencedata science
data science
 
Data Analytics with R and SQL Server
Data Analytics with R and SQL ServerData Analytics with R and SQL Server
Data Analytics with R and SQL Server
 
data structure
data structuredata structure
data structure
 
data structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptxdata structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptx
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm.
 
datastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptxdatastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptx
 
project on data structures and algorithm
project on data structures and algorithmproject on data structures and algorithm
project on data structures and algorithm
 
K Means Clustering Algorithm for Partitioning Data Sets Evaluated From Horizo...
K Means Clustering Algorithm for Partitioning Data Sets Evaluated From Horizo...K Means Clustering Algorithm for Partitioning Data Sets Evaluated From Horizo...
K Means Clustering Algorithm for Partitioning Data Sets Evaluated From Horizo...
 
12000121037.pdf
12000121037.pdf12000121037.pdf
12000121037.pdf
 
ML SFCSE.pptx
ML SFCSE.pptxML SFCSE.pptx
ML SFCSE.pptx
 
Data structures cs301 power point slides lecture 01
Data structures   cs301 power point slides lecture 01Data structures   cs301 power point slides lecture 01
Data structures cs301 power point slides lecture 01
 
Introduction to DS.pptx
Introduction to DS.pptxIntroduction to DS.pptx
Introduction to DS.pptx
 

Recently uploaded

How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 

Recently uploaded (20)

How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 

DATA STRUCTURE AND ALGORITHM for beginners

  • 1. DATA STRUCTURE AND ALGORITHM- IN23C05 Dr Sabitha Banu Assistant Professor PSGR Krishnammal College for Women 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 1
  • 2. Course Outcomes •Recall about the concepts of Arrays, Stack, Queue, Link List, Trees and Graph. •Understand sorting, searching and hashing algorithm •Apply the data structures to solve various computing algorithms and sorting algorithms. •Analyze lists, queues, stacks, trees and graph according to the needs of different applications 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 2
  • 3. UNIT-I •Introduction to Data Structure Definition, Basic Terminology, Elementary Data Organization Types of Data Structures- Linear & Non-Linear Data Structures Data Structure Operations •Algorithm Specifications: Performance Analysis and Measurement (Time and space analysis) • Abstract Data Types Advantages of ADT • Array Representation of arrays Types of arrays Applications of arrays Sparse matrix and its representation 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 3
  • 4. Definition of Data Structure •Logical or mathematical model of the organization of data elements in the computer memory. •Algorithms-Specific methods used to access and process the data elements •Organizing the data + processing the data from memory(main+Secondary) collectively –Data Structures •Basic building blocks of the program 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 4
  • 5. 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 5
  • 6. Linear Data Structures •The data is stored in linear data structures sequentially. •These are rudimentary structures since the elements are stored one after the other without applying any mathematical operations. Examples •Arrays •Linked Lists •Stacks •Queues 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 6
  • 7. Non-Linear Data Structures •Non-Linear data structures store the data in the form of a hierarchy. •data can be found in multiple levels and are difficult to traverse through. Examples •Tree •Graph 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 7
  • 8. Common Operations ✔ Traverse ✔ Search ✔ Insert ✔ Delete ✔ Sort ✔ Merge ✔ Create ✔ Retrieve ✔ Store 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 8
  • 9. Algorithm •step-by-step procedure •Pseudo code •Algorithm writing is a process and is executed after the problem domain is well-defined. •Instructions are applied on raw data –input, solution of the problem is produced-output •Example 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 9
  • 10. 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 10
  • 12. Characteristics of an algorithm •Input value must be given to solve the computational problem •Algorithms process the data by applying some operations ,desired output is generated •Algorithms must terminate after some instructions and reach the concluding state 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 12
  • 13. Advantages of Algorithms: •It is easy to understand. •An algorithm is a step-wise representation of a solution to a given problem. •In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program. Disadvantages of Algorithms: •Writing an algorithm takes a long time so it is time-consuming. •Understanding complex logic through algorithms can be very difficult. •Branching and Looping statements are difficult to show in Algorithms 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 13
  • 14. Flowchart •A flowchart is a graphical representation of an algorithm. •makes use of symbols that are connected among them to indicate the flow of information and processing. 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 14
  • 15. 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 15
  • 16. Analysis of Algorithms •Provides theoretical estimation for the required resources of an algorithm to solve a specific computational problem. •Analysis of algorithms is the determination of the amount of time and space resources required to execute it. •Efficiency(CPU, Memory ,Disk, Network ) •Time complexity •Space complexity •Worst, Average and Best Cases 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 16
  • 17. Abstract Datatypes (ADT) •Is a mathematical tool used to define the concept of data types and set of operations •Collection of data values and the functions which operate on these values 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 17
  • 18. 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 18
  • 19. 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 19
  • 20. Arrays •fixed-size sequenced collection of variables belonging to the same data types and stored in contiguous memory. •Set of pairs, index or value •The array has adjacent memory locations to store values. •convenient structure for representing data •Two terms to understand the concept of array are Element and Index ✔ Element − Each item stored in an array is called an element. ✔ Index − Each location of an element in an array has a numerical index, which is used to identify the element. data_type array_name [array_size]; 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 20
  • 21. • Index starts with 0. • Array length is 10 which means it can store 10 elements. • Each element can be accessed via its index(mapping). For example, we can fetch an element at index 6 as 9. # structure ARRAY(value, index) declare CREATE( ) array RETRIEVE(array,index) value STORE(array,index,value) array; # Need for Arrays • number of variables used will increase 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 21
  • 22. Ordered Lists • list in which the elements must always be ordered in a particular way • Also called as Sorted list. Eg. (SUNDAY ,MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY) Representation of arrays ✔ One dimensional array ❖ A one-dimensional array is also called a single dimensional array where the elements will be accessed in sequential order. This type of array will be accessed by the subscript of either a column or row index. eg a[n] or an ✔ Two dimensional array ❖ When the number of dimensions specified is more than one, then it is called as a multi-dimensional array. Eg a[3,3] (row x column) 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 22
  • 23. Eg a[3][4] • A two-dimensional array will be accessed by using the subscript of row and column index. eg a[1][1] 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 23
  • 24. 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 24
  • 25. 1D or one dimensional array- •A list of items having one variable name with only one subscript •Computer reserves 5 storage locations •Int number[5]={35,40,20,57,19} 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 25
  • 26. 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 26 Char name[10]=“well done”; When declaring a character arrays ,allocate one extra element space for the null terminator When a compiler reads a character arrays it terminates with the additional null character(‘0’)
  • 27. 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 27
  • 28. 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 28
  • 29. 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 29
  • 30. •Two different types of initialization of arrays • Compile time initialization • Run time initialization Compile time Initialization 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 30
  • 31. •Run time initialization 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 31
  • 32. Two Dimensional Array- •To store a table of values •C allows us to store the table of values using 2D or two dimensional array •Two dimensional array can be declared as •C places each size its own set of brackets. 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 32
  • 33. 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 33
  • 34. 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 34
  • 35. 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 35
  • 36. Initializing an 2D array – 1. 6. 2. 7. 3. 4. 5. 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 36
  • 37. Memory Layout- •The subscripts in the 2d arrays represents the rows and columns •The elements of the array are stored contiguously in increasing memory locations. •Starts to store row wise ,starting from the first row and ending with the last row, treating each row like a simple array. 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 37 A[3][3]={{10,20,30},{40,50,60},{70,80, 90}};
  • 38. Multi-dimensional array- •C allows arrays of 3 or more dimensions.the exact limit is decided by the compiler •There is no limit for dimensions but compiler permits only 7-10 dimensions. 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 38
  • 39. 12-12-2023 Dr Sabitha Banu,Assistant Professor ,PSGRKCW 39
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46. Different types of representation • Square matrix • Diagonal • Row • Zero • Upper triangular matrix • Lower triangular matrix • Scalar matrix • identity/Unit matrix
  • 47. Square matrix • Number of rows=number of columns
  • 48. Diagonal matrix • Where diagonal elements are non-zero elements
  • 50. Zero matrix Matrix where all the elements have zero value
  • 51. Upper& lower triangular matrix ● A square matrix is said to be a lower triangular matrix if all the elements above its main diagonal are zero. ● A square matrix is said to be an upper triangular matrix if all the elements below the main diagonal are zero.
  • 52. Scalar matrix Matrix where all diagonal elements are same
  • 54. Unit-2 Stacks and Queues • Stack • Stack Representation & Implementation • Stack Operations • Applications of Stack • Queue • Queue Representation & Implementation • Queue Operations • Types of Queues
  • 55. Stack • simple data structure used for storing data • In a stack, the order in which the data arrives is important • Definition “A stack is an ordered list in which insertion and deletion are done at one end, called top. The last element inserted is the first one to be deleted. Hence, it is called the Last in First out (LIFO) or First in Last out (FILO) list” • https://www.youtube.com/watch?v=-qsKQVpGPKs
  • 56.
  • 57. Operations • When an element is inserted in a stack, the concept is called push • when an element is removed from the stack, the concept is called pop. • Trying to pop out an empty stack is called underflow • trying to push an element in a full stack is called overflow
  • 58.
  • 59. Main stack operations • Push (int data): Inserts data onto stack. • int Pop(): Removes and returns the last inserted element from the stack.
  • 60. Auxiliary stack operations • int Top(): Returns the last inserted element without removing it. • int Size(): Returns the number of elements stored in the stack. • int IsEmptyStack(): Indicates whether any elements are stored in the stack or not. • int IsFullStack(): Indicates whether the stack is full or not.
  • 61. Stack Implementation • Simple array /Static array based implementation • Dynamic /pointer/Linked lists implementation
  • 62. Simple Array Implementation • uses an array • In the array, elements are added from left to right and use a variable to keep track of the index of the top element.
  • 63. • The array storing the stack elements may become full. A push operation will then throw a full stack exception. • Similarly, if we try deleting an element from an empty stack it will throw stack empty exception.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69. Dynamic /pointer representation • Do not have size limitations and use space proportional to the actual number of elements stored in the stack • Also called as linked representation of stack /linked stack Linked representation • Stack is a collection of nodes • Each is divided into 2 parts • Info-element ,next-pointer of the neighbour element
  • 70. • Top -start pointer of the stack • If the last node link field contains null /Top=null-bottom of the stack •
  • 71.
  • 72. Multiple Stacks ❖ a single array having more than one stack. The array is divided for multiple stacks. ❖ If the array represents two stacks ,then two stacks can grow in the opposite direction
  • 73. ❖ Suppose there is an array STACK[n] divided into two stack STACK A and STACK B, where n = 10. ➢ STACK A expands from the left to the right, i.e., from 0th element. ➢ STACK B expands from the right to the left, i.e., from 10th element. ➢ The combined size of both STACK A and STACK B never exceeds 10.
  • 74.
  • 75. Queues • Linear data structure in which elements can be inserted at one end(REAR) and deleted from other end (FRONT) • Elements are processed based on the order of arrival • First Come First Serve (FCFS)/FIFO (First In First Out) • •
  • 76. • Queue is an integral part of OS • Process management and multiprogramming environment
  • 77. Queue representation and implementation ❖ Two kinds of representation ➢ Static /sequentially /array representation ➢ Dynamic /pointer /linkedlist representation
  • 78. Array representation • A queue is implemented with max elements array stored from queue[i] to queue[j],Max • I,j are front(F) and Rear(R) • Contains two pointer variables • Front -location of the front element of the queue • Rear-location of the rear element of the queue
  • 79.
  • 80. Dynamic /Pointer representation • Size of the queue is not constant which is called dynamic • Also called as Linked representation of queue • Linked queue Linked queue • Implemented as a linked list with two pointer variables • Front and Rear (pointing to the front and rear nodes of the queue)
  • 81.
  • 82.
  • 83.
  • 84. Stack vs Queue Stack Queue Can be implemented sequentially and dynamically Can be implemented sequentially and dynamically Are called LIFO Called FIFO One entry and exit called Top Separate entry called Rear and separate exit called Front No different forms of stacks Different forms of queue circular,priority , dequeue
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94. Drawback of queue • Queue is completely full when rear is at last array position i.e. (MaxSize -1). • Now no more elements can be inserted in queue even if the queue has some empty spaces.
  • 95.
  • 96. Circular Queue • Removes the drawback of simple queue • the last element is connected to the first element, creating a circular structure. • This allows for efficient use of memory. • FIFO basis •
  • 97.
  • 98.
  • 99. • Let’s say the MaxSize of your queue is 5, and the rear pointer has already reached the end of a queue. • There is one empty space at the beginning of a queue, which means that the front pointer is pointing to location 1.
  • 100.
  • 101.
  • 102. Enqueue(x) Operation Step 1: Check if the queue is full (Rear + 1 % Maxsize = Front) Step 2: If the queue is full, there will be an Overflow error Step 3: Check if the queue is empty, and set both Front and Rear to 0 Step 4: If Rear = Maxsize - 1 & Front != 0 (rear pointer is at the end of the queue and front is not at 0th index), then set Rear = 0 Step 5: Otherwise, set Rear = (Rear + 1) % Maxsize Step 6: Insert the element into the queue (Queue[Rear] = x) Step 7: Exit
  • 103.
  • 104. Deque ❖ Double ended queue where insertion and deletion can be performed in both ends front and rear ❖ Rear is incremented by 1 nd then it is inserted in the new location ❖ Similarly when the element is deleted front is incremented by 1 ❖ Elements can be inserted in the front ➢ Front is decremented to 1 before insertion and the element is inserted into the new location ➢ Front is 0 ,overflow occurs ❖ Elements can be deleted from the rear ➢ Rear is decremented by 1 after the deletion ➢ Deleted element returned to the user
  • 106. Dequeue() Operation Step 1: Check if the queue is empty (Front = -1 & Rear = -1) Step 2: If the queue is empty, Underflow error Step 3: Set Element = Queue[Front] Step 4: If there is only one element in a queue, set both Front and Rear to -1 (IF Front = Rear, set Front = Rear = -1) Step 5: And if Front = Maxsize -1 set Front = 0 Step 6: Otherwise, set Front = Front + 1 Step 7: Exit
  • 107.
  • 108. Different types of Deque Input Restricted Where insertion is restricted to one end (rear end )and deletion can be done both ends,front & rear Output Restricted Where deletion is restricted to one end (front end )and insertion can be done both ends,front & rear Circular Dequeue Implemented in Circular form,last element follows the first element
  • 109.
  • 110. Priority Queue ➔ Insertion and deletion is done according to the priorities assigned ➔ Does not follow any principles FIFO ➔ Elements of higher priorities are processed first than the elements with the lowest priorities ➔ If both have same priorities then based on the order of insertion ➔ Used in OS for time sharing property ➔ Insertion of an element is difficult than deletion (finding out the correct location according to the priority) ➔ Two ways ◆ Sequential /array ◆ dynamic/linked info Priority no pointer
  • 111.
  • 112. Multiqueue • Multi queue is data structure in which multiple queues are maintained. • are utilized for process scheduling. • one dimensional array or multidimensional array to illustrated a multiple queue. • Each of queues contains n elements that are mapped to a linear array of m elements.
  • 113. Applications of stack • Factorial of a given number • Recursion • Infix to Postfix transformation
  • 114. Recursion • a function or method has the ability to call itself to solve the problem. • Recursion in stack in data structure is when functions call themselves directly or indirectly.
  • 115. Factorial of a given number(Recursion)
  • 116.
  • 117.
  • 118.
  • 119. • First, convert the given expression into special notation. • Evaluate the expression in this new notation • three notations to represent an arithmetic expression: ❖ Infix Notation ❖ Prefix Notation ❖ Postfix Notation
  • 120. Infix • The infix notation is a convenient way of writing an expression in which each operator is placed between the operands. • Infix expressions can be parenthesized or unparenthesized depending upon the problem requirement • Example A + B, (C - D) • All these expressions are in infix notation because the operator comes between the operands.
  • 121. Prefix Notation • The prefix notation places the operator before the operands. • This notation was introduced by the Polish mathematician and hence often referred to as polish notation • Example: + A B, -CD etc • All these expressions are in prefix notation because the operator comes before the operands.
  • 122. Postfix Notation • The postfix notation places the operator after the operands. • This notation is just the reverse of Polish notation and also known as Reverse Polish notation. • Example: AB +, CD+, etc. • All these expressions are in postfix notation because the operator comes after the operands
  • 124. example of Converting an infix expression into a postfix expression
  • 125.
  • 126.
  • 127.
  • 128.
  • 129. Evaluating a postfix expression 3 + 8 - 9 / 8 = 3 + 8 - 1.125 = 11 - 1.125 = 9.875
  • 130.
  • 131. 3 + 8 - 9 / 8
  • 132. Sum of n number using Recursion #include <stdio.h> int sum(int n); int main() { int number, result; printf("Enter a positive integer: "); scanf("%d", &number); result = sum(number); printf("sum = %d", result); return 0; } int sum(int n) { if (n != 0) // sum() function calls itself return n + sum(n-1); else return n; }
  • 133.
  • 134. Factorial of given number #include<stdio.h> long factorial(int n) { if (n == 0) return 1; else return(n * factorial(n-1)); } void main() { int number; long fact; printf("Enter a number: "); scanf("%d", &number); fact = factorial(number); printf("Factorial of %d is %ldn", number, fact); return 0; }
  • 135.
  • 136. Recursion Iteration When a function calls itself Set of statements executed repeatedly until the condition is satisfied Based on base condition Initialization,termination,incrementation,updation ,execution Top-down approach Bottom up approach Takes more space to store new set of variables Does not take much space and time Not efficient Efficient Recursive algorithm can be converted to iterative version Iterative cannot be converted to recursive version Saves the return address Does not save the return address Uses stack for temporary storage Stacks are not required to implement iterative algorithms
  • 137. #include <stdio.h> int MAXSIZE = 8; int stack[8]; int top = -1; /* Check if the stack is empty */ int isempty(){ if(top == -1) return 1; else return 0; } /* Check if the stack is full */ int isfull(){ if(top == MAXSIZE) return 1; else return 0; } /* Function to return the topmost element in the stack */ int peek(){ return stack[top]; }
  • 138. /* Function to delete from the stack */ int pop(){ int data; if(!isempty()) { data = stack[top]; top = top - 1; return data; } else { printf("Could not retrieve data, Stack is empty.n"); } } /* Function to insert into the stack */ int push(int data){ if(!isfull()) { top = top + 1; stack[top] = data; } else { printf("Could not insert data, Stack is full.n"); } }
  • 139. /* Main function */ int main(){ push(44); push(10); push(62); push(123); push(15); printf("Element at top of the stack: %dn" ,peek()); printf("Elements: n"); // print stack data while(!isempty()) { int data = pop(); printf("%dn",data); } printf("Stack full: %sn" , isfull()?"true":"false"); printf("Stack empty: %sn" , isempty()?"true":"false"); return 0; }
  • 140. UNIT-III Linked List • Linked List as Data Structures • Representation of Linked List • Operations on Linked List • Stack as Linked List • Queue as Linked List • Doubly Linked List • Circular List
  • 141. Linked List as Data Structures • Collection of similar elements where each element points to the next element • Is a linear data structure • Each node divided into two parts • Info -contains the info about the items of the list stored and processed • Next-contains the address of next adjacent node .used to access the next data item maintained in the other node,maintains the link of the data • A linked list can grow and shrink its size(dynamic), as per the requirement. • It does not waste memory space. • Data items in the list are not maintained in contiguous locations ,may placed anywhere • Correct order is maintained by the logical address in the next field of each node,insertion and deletion is possible
  • 143. • Access the data items of the linked list sequentially • Random access of items is not allowed ,coz they are linked through pointers
  • 144.
  • 145. Represeantion of linked list • Static /sequential/array representation • dynamic/pointer/linked representation
  • 146. Static /sequential/array representation • Linked list can also be implemented as an array • Data items maintained in two parallel arrays of the same sizes • One array for storing info field of the nodes • Other array for the addresses
  • 147.
  • 148.
  • 149. List[n] -number of nodes in the list List[6].info=16 List[6].field=10
  • 150. Dynamic /pointer representation • Array representation is not flexible • Size of the linkedlist may increase/decrease as per the requirement when insertion and deletion is done • Dynamic representation does not have size limitations • Space proportional to the actual no of elements
  • 151. • In C language, a linked list can be implemented using structure and pointers • is used to create every node in the list • The data field stores the element and the next is a pointer to store the address of the next node • In place of a data type, struct LinkedList is written before next. • That's because its a self-referencing pointer. It means a pointer that points to whatever it is a part of. Here next is a part of a node and it will point to the next node.
  • 152.
  • 153. Operations on the linkedlist • Creation • Traversal - To access each element of the linked list. • Insertion - To add/insert a new node to the list. • Deletion - To remove an existing node from the list. • Search - To find a node in the list. • Sort - To sort the nodes.
  • 154. Create linkedlist • Creation of the linkedlist Array implementation- ❖ Number of nodes declared in advance ❖ Elements are entered in the contiguous memory locations ❖ Placed sequentially in the array ❖ Maintain the list of free nodes ❖ Initially all the nodes are empty then linked with one another in sequence ❖ next pointer=-1 denotes end of the list
  • 155.
  • 156. Dynamic implementation • List is created by creating the nodes of the list as and when required • Allocate memory dynamically
  • 157.
  • 158. • Define the structure of a node and define start pointer to the node type struct Node { int info; Node *next; }; struct node *start; ● In linked lists, a struct is used to define the structure of each node in the list. ● Each node typically contains two parts: the data and a reference (or pointer) to the next node in the sequence. ● The struct allows you to group these two pieces of information together.
  • 159. 2. Set start =NULL to indicate the list is initially empty. start=NULL; 3. Get the free new node from the memory heap assign its address into pointer ptr. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 4. Assign the value to be inserted to the info part of the new node pointed by ptr ptr→info=value(first supply the value) 5.Assign NULL to the next part of the new node ptr→next=start(ptr→next=NULL becoz start=NULL 6.Assign the address of the first node to the start variable start=ptr 7.Return
  • 160. int main() { struct Node* head = NULL; struct Node* second_node = NULL; struct Node* third_node = NULL; // Allocate memory to 3 nodes head = (struct Node*)malloc(sizeof(struct Node)); second_node = (struct Node*)malloc(sizeof(struct Node)); third_node = (struct Node*)malloc(sizeof(struct Node)); head->data = 12; //assign data in first node head->next = second_node; // Link first node with the second node. second_node->data = 99; //assign data to second node second_node->next = third_node; // Link second node with the third node third_node->data = 58; //assign data to third node third_node->next = NULL;}
  • 161. Traversing a linkedlist • Is used to visit each node of the list exactly once in order to access the information stored in the nodes • Accessing or printing the values (i.e., data items stored inside each node) of a singly linked list exactly once until the end node is reached. • This accessing and printing is sometimes called “visiting” the linked list • Accessing or printing of nodes always takes place one by one • The process of visiting each node of the list once to perform some operation on it is called traversing
  • 162.
  • 163.
  • 164. 1.node creation 2.memory allocation 3.setting head 4.The malloc function in C returns a pointer to the first byte of the allocated memory block, and its return type is void *. 5.type casting
  • 165. Insertion of an element into the linked list at various positions ● Requires a free node in which the information is to be inserted ● Then the node can be inserted into the linked list ● Pointers are adjusted ● Three possible positions where we can enter a new node in a linked list – ★ Insertion at beginning ★ Insertion after nth position ★ Insertion at end
  • 166. Steps : 1. Get the free node from memory pool and assign its address to the ptr 2. Assign the value ptr→info=value to the info part of the new node pointed by the ptr 3. If the linked list is empty start=NULL then assign ptr→next = start and start =ptr
  • 167. 4. Otherwise adjust the pointers according to the place of insertion.
  • 168.
  • 169.
  • 170. Insertion at the beginning • Create a new node • Assign its data value • Assign newly created node’s next ptr to current head reference. So, it points to the previous start node of the linked list address • Change the head reference to the new node’s address.
  • 171. 1. 2.
  • 172.
  • 173.
  • 174.
  • 175. void insertStart (struct Node *head, int data) { struct Node *newNode = (struct Node *) malloc (sizeof (struct Node)); newNode - > data = data; newNode - > next = *head; //changing the new head to this freshly entered node *head = newNode; }
  • 176.
  • 177. Insertion at the end of the list ● Create a new node ● Assign its data value ● Assign its next node to NULL as this will be the last(tail) node ● Check if the list is empty ○ Change the head node to the new node ● If not then traverse till the last node ● Assign the last node’s next pointer to this new node ● Now, the new node has become the last node.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183. Insertion at the specified position in the linkedlist • First ,create a new node named by newnode and put the position where u want to insert the node. • Now give the address of the new node in previous node means link the new node with previous node. • After this, give the address of current node in new node.Means link your new node also with current node.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189. Deletion of an element from the linkedlist • No longer nodes can be deleted from the list • Can be performed at various positions like linkedlist • Necessary to know the position of the node to be deleted • Check for the underflow condition (list=NULL) • Deletion at the beginning • Deletion at the end • Deletion at the middle
  • 190. Deletion at the beginning of the list • Check for the underflow condition • First node to be deleted start pointerpoint to the second node of the list • After deleting save the value into a variable • Deleted nodesent to the memory for future use • free() ,delete()
  • 191.
  • 192.
  • 193.
  • 194.
  • 195. Deletion at the end of the list • Traverse the entire list to find the address of the second last node to adjust the pointers • next field of the second last node point to the next field of the last node • Hence the next field of the second last node will contain now NULL • Contains a temporary pointer to hold the address of the preceding node (node to be deleted)
  • 196. Step 1: IF HEAD = NULL Write UNDERFLOW Go to Step 8 [END OF IF] Step 2: SET PTR = HEAD Step 3: Repeat Steps 4 and 5 while PTR -> NEXT!= NULL Step 4: SET PREPTR = PTR Step 5: SET PTR = PTR -> NEXT [END OF LOOP] Step 6: SET PREPTR -> NEXT = NULL Step 7: FREE PTR Step 8: EXIT
  • 197.
  • 198.
  • 199.
  • 200.
  • 201. Representation of linked list Array Pointer(linkedlist) Fixed size,cannot grow according to the need,hard to predict the size every time Elements can be added indefinitely,grow during the execution time Static,fixed size,easy to use Dynamic,does not require to predict in advance Deletion leaves an empty space (wasting of memory),shifting of elements is difficult Elements can be deleted ,can shrink during the execution time Insertion and deletion is slow,needs to shift operation Insertion and deletion is fast Memory allocated at compile time ,memory does not used efficiently Efficiently utilizes the memory ,bcoz memory is allotted at runtime Insertion and deletion at specified position needs resizing of the array which is not Insertion and deletion at specified position by adjusting the pointers ,it is flexible
  • 202. Random access to the elements Allows sequential access to the elements Suits smaller list of elements when numbers of items in the list is known Larger list where insertion and deletion is done frequently Pass through all the elements to reach kth element Access directly in a single operation by using the index value
  • 203. Stack as a linkedlist Array implementation • Size of the stack is known in advance • Advance declaration of maximum size is not possible all the time • Size may increase or decrease according to the requirement • Size constantly change as items pushed and popped
  • 204. Dynamic representation of a stack: • Called as linked stack/linked list • Suits for large records and when elements are not known in advance • Stack is represented as linkedlist where each node contains info,next part
  • 205. • Top pointer of the stack represented by start pointer • If start =NULL shows stack is empty • If last node=NULL shows the bottom of the stack
  • 206.
  • 207.
  • 208. Push operation can be performed in the below steps Step 1 − Checks stack has some space or stack is full. Step 2 − If the stack has no space then display “overflow” and exit. Step 3 − If the stack has space then increase top by 1 to point next empty space. Step 4 − Adds item to the newly stack location, where top is pointing. Step 5 – PUSH operation performed successfully.
  • 209.
  • 210.
  • 211.
  • 212. Step 1 − Checks stack has some element or stack is empty. Step 2 − If the stack has no element means it is empty then display “underflow” Step 3 − If the stack has element some element, accesses the data element at which top is pointing. Step 4 − Decreases the value of top by 1. Step 5 − POP operation performed successfully.
  • 213. Queue as a linkedlist • the array implementation can not be used for the large scale applications where the queues are implemented. • One of the alternative of array implementation is linked list implementation of queue. • Size of the queue may increase or decrease according to the requirement as items get inserted and deleted • Dynamic representation of queue is called linked queue • Linked queue -elements size is not know in advance may increase
  • 214. Linked queue : • In a linked queue, each node of the queue consists of two parts i.e. data part and the link part. • Each element of the queue points to its immediate next element in the memory. • In the linked queue, there are two pointers maintained in the memory i.e. front pointer and rear pointer. • The front pointer contains the address of the starting element of the queue while the rear pointer contains the address of the last element of the queue.
  • 215.
  • 216. • Two pointers for front and rear position of the queue • front=null (queue is empty) • front=rear=null
  • 217.
  • 218.
  • 219.
  • 220.
  • 221. • Accessing the nodes in the forward direction only • Cannot traverse backward direction Doubly Linked list • a node consists of three parts: node data, pointer to the next node in sequence (next pointer) , pointer to the previous node (previous pointer).
  • 222.
  • 223. • Each node contains 1 info field ,2 link pointers (forward & backward) • The pointers points to the adjacent to the current node • Fowrdpointer points to the next node of the current node • Backward pointer points to the preceding node of the current node • 2 Pointers helps in accessing successor and predecessor nodes of any arbitrary node
  • 224.
  • 225. Comparison No Singly linked list Doubly linked list 1. Can move only in forward direction Can move both in forward and backward direction 2. Cannot reach preceding node fro any arbitrary node Can reach any preceding node from any arbitrary node 3. Insertion and deletion to the preceding node needs traversal of links again fro the first node Insertion and deletion can be easily performed here bcoz every pointer has links to the preceding node
  • 226. Representation of DLL • static/array/sequential representation (implementation) • dynamic/pointer/linked representation (implementation)
  • 227. static/array implementation • DLL is also a collection of similar data items in an array • Data items of the linkedlist are maintained in the form of nodes • Each node contains 3 parts • Infor • Prev • Next
  • 228.
  • 229.
  • 230. • List of 12 nodes containing integer type of info,prev,next • In array representation start value =0 and null value =-1 • If any nodes contains -1 shws there is no successor /predecessor node
  • 231.
  • 232. Dynamic /pointer representation • Do not have size limitations • Use space proportional to the actual number of elements in the list
  • 233.
  • 234.
  • 235. DLL Operations • Same as SLL (singly linkedlist) • Creation • Traversal - To access each element of the linked list. • Insertion - To add/insert a new node to the list. • Deletion - To remove an existing node from the list. • Search - To find a node in the list. • Sort - To sort the nodes.
  • 236. Insertion into DLL at various positions • Requires a freenode in which information can be inserted • Node can be inserted into DLL and pointers are adjusted •
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242. Insert to the end of DLL Algorithm ○ Step 1: IF PTR = NULL ○ Write OVERFLOW Go to Step 11 [END OF IF] ○ Step 2: SET NEW_NODE = PTR ○ Step 3: SET PTR = PTR -> NEXT ○ Step 4: SET NEW_NODE -> DATA = VAL ○ Step 5: SET NEW_NODE -> NEXT = NULL ○ Step 6: SET TEMP = START ○ Step 7: Repeat Step 8 while TEMP -> NEXT != NULL ○ Step 8: SET TEMP = TEMP -> NEXT ○ [END OF LOOP]Step 9: SET TEMP -> NEXT = NEW_NODE ○ Step 10C: SET NEW_NODE -> PREV = TEMP ○ Step 11: EXIT
  • 243.
  • 244.
  • 245.
  • 246. Insert after in DLL ○ Step 1: IF PTR = NULL ○ Write OVERFLOW Go to Step 15 [END OF IF]Step 2: SET NEW_NODE = PTR ○ Step 3: SET PTR = PTR -> NEXT ○ Step 4: SET NEW_NODE -> DATA = VAL ○ Step 5: SET TEMP = START ○ Step 6: SET I = 0 ○ Step 7: REPEAT 8 to 10 until I ○ Step 8: SET TEMP = TEMP -> NEXT ○ STEP 9: IF TEMP = NULL ○ STEP 10: WRITE "LESS THAN DESIRED NO. OF ELEMENTS" ○ GOTO STEP 15 [END OF IF] [END OF LOOP]Step 11: SET NEW_NODE -> NEXT = TEMP -> NEXT ○ Step 12: SET NEW_NODE -> PREV = TEMP ○ Step 13 : SET TEMP -> NEXT = NEW_NODE ○ Step 14: SET TEMP -> NEXT -> PREV = NEW_NODE ○ Step 15: EXIT
  • 247.
  • 248. Deletion of a node in DLL • Deletion can be done from the beginning,end, and at the middle of the list • Check for the underflow condition (start=NULL) •
  • 249.
  • 250. Drawback • Cannot move directly to the first node after reaching lastnode of the list
  • 251. Circular Linked list • The first and last nodes are linked together • Next pointer of the last node connects to the first node instead of containing NULL • Contains valid addresses without null • Two types • Singly circular linked list • Doubly circular linked list
  • 252. Singly circular linked list • the last node of the list contains a pointer to the first node of the list. • traverse the circular singly linked list until we reach the same node where we started. • The circular singly linked list has no beginning or end. • No null value is present in the next part of any of the nodes.
  • 253. Doubly circular linked list • has properties of both doubly linked list and circular linked list • in which two consecutive elements are linked or connected by the previous and next pointer and the last node points to the first node by the next pointer • also the first node points to the last node by the previous pointer.
  • 254. • CLL used to implement circular queue • Used to maintain the process for execution in the time sharing OS • Each process is allotted a timestamp for execution • Wait for the next turn in a circular queue • All nodes are connected in the circle so any node can be accessed from any node
  • 255.
  • 256.
  • 257.
  • 258. Representation of CLL • Array representation • Pointer representation
  • 259. Array representation • Data items are maintained in the form of nodes divided into 3 parts • 3 parallel arrays of the same sizes •
  • 260.
  • 262.
  • 263. Insertion into circular singly linked list at beginning Step 1: IF PTR = NULL Write OVERFLOW Go to Step 11 [END OF IF] Step 2: SET NEW_NODE = PTR Step 3: SET PTR = PTR -> NEXT Step 4: SET NEW_NODE -> DATA = VAL Step 5: SET TEMP = HEAD Step 6: Repeat Step 8 while TEMP -> NEXT != HEAD Step 7: SET TEMP = TEMP -> NEXT [END OF LOOP] Step 8: SET NEW_NODE -> NEXT = HEAD Step 9: SET TEMP → NEXT = NEW_NODE Step 10: SET HEAD = NEW_NODE Step 11: EXIT
  • 264.
  • 265.
  • 266.
  • 267.
  • 268.
  • 269.
  • 270. Deletion in circular singly linked list at beginning Step 1: IF HEAD = NULL Write UNDERFLOW Go to Step 8 [END OF IF]Step 2: SET PTR = HEAD Step 3: Repeat Step 4 while PTR → NEXT != HEAD Step 4: SET PTR = PTR → next [END OF LOOP] Step 5: SET PTR → NEXT = HEAD → NEXT Step 6: FREE HEAD Step 7: SET HEAD = PTR → NEXT Step 8: EXIT
  • 271.
  • 272. Deleting nodes at given index in the Circular linked list
  • 273. Step 1 START Step 2 Store the element to be deleted. Step 3 Store the position. Step 4 Initialize counter c=1 Step 5 Check while (c<pos) Step 6 Count the position of data to be deleted y = x; x = x->link; c++; Step 7 Use free() to delete y->link = x->link; free(x); Step 8 STOP
  • 274.
  • 275. Deletion in Circular singly linked list at the end Step 1: IF HEAD = NULL Write UNDERFLOW Go to Step 8 [END OF IF]Step 2: SET PTR = HEAD Step 3: Repeat Steps 4 and 5 while PTR -> NEXT != HEAD Step 4: SET PREPTR = PTR Step 5: SET PTR = PTR -> NEXT [END OF LOOP]Step 6: SET PREPTR -> NEXT = HEAD Step 7: FREE PTR Step 8: EXIT
  • 276.
  • 277. UNIT-IV Trees • Trees: Preliminaries • Binary Trees • B-Trees • Graph • Graph Terminologies • Types of Graphs • Graph Representation • Hashing • Hash Functions ● Sorting ● Bubble Sort ● Selection Sort ● Quick Sort ● Heap Sort ● Merge Sort
  • 278.
  • 279. Tree • Defined as a finite collection of special data ites called nodes • Nodes are arranged in hierarchical order to represent parent-child relationship • First node linked with some other nodes in turn linked with other nodes • The parent connected to its childrens through edges
  • 280.
  • 281. Tree is a finite set of data elements called nodes such that there is a special node called the root of the tree and the remaining nodes are partitioned into a number of mutually exclusive subsets which are themselves trees.The edges that connect the nodes are called branches , and the number of branches connected to a particular node is called the degree of that node .
  • 282.
  • 283.
  • 284. Binary tree • Can have maximum two children • Left child,right child • Left child ,right child is also a subtree
  • 285.
  • 286. • Binary tree can also be defined as a collection of nodes • Each node is divided into 3 part containing left child address,information,right child address
  • 287.
  • 288. Classification of binary trees • Complete binary tree • Almost complete binary tree
  • 289. Complete binary tree A complete binary tree is a special type of binary tree where all the levels of the tree are filled completely except the lowest level nodes which are filled from as left as possible.
  • 290.
  • 292. What Is B-tree? • A B-tree is a data structure that provides sorted data and allows searches, sequential access, attachments and removals in sorted order. • The B-tree is highly capable of storing systems that write large blocks of data. • The B-tree simplifies the binary search tree by allowing nodes with more than two children. Below is a B-tree example.
  • 293. • B-tree stores data such that each node contains keys in ascending order. • Each of these keys has two references to another two child nodes. • The left side child node keys are less than the current keys, and the right side child node keys are more than the current keys. • If a single node has “n” number of keys, then it can have maximum “n+1” child nodes.
  • 294.
  • 295. B-tree • is a specialized m-way(multiple children) tree that can be widely used for disk access. • A B-Tree of order m can have at most m-1 keys and m children. • using B tree is its capability to store large number of keys in a single node and large key values by keeping the height of the tree relatively small. • A B tree of order m contains all the properties of an M way tree. In addition, it contains the following properties. 1. Every node in a B-Tree contains at most m children. 2. Every node in a B-Tree except the root node and the leaf node contain at least m/2 children. 3. The root nodes must have at least 2 nodes. 4. All leaf nodes must be at the same level.
  • 296. • It is not necessary that, all the nodes contain the same number of children but, each node must have m/2 number of nodes. • A B tree of order 4 is shown in the following image.
  • 297. Searching A. Searching in B Trees is similar to that in Binary search tree. For example, if we search for an item 49 in the following B Tree. The process will something like following : i. Compare item 49 with root node 78. since 49 < 78 hence, move to its left sub-tree. ii. Since, 40<49<56, traverse right sub-tree of 40. iii. 49>45, move to right. Compare 49. iv. match found, return. B. Searching in a B tree depends upon the height of the tree. The search algorithm takes O(log n) time to search any element in a B tree.
  • 298.
  • 299. Inserting Insertions are done at the leaf node level. The following algorithm needs to be followed in order to insert an item into B Tree. 1. Traverse the B Tree in order to find the appropriate leaf node at which the node can be inserted. 2. If the leaf node contain less than m-1 keys then insert the element in the increasing order. 3. Else, if the leaf node contains m-1 keys, then follow the following steps. ○ Insert the new element in the increasing order of elements. ○ Split the node into the two nodes at the median. ○ Push the median element upto its parent node. ○ If the parent node also contain m-1 number of keys, then split it too by following the same steps.
  • 300. Insert the node 8 into the B Tree of order 5 shown in the following image
  • 301. 8 will be inserted to the right of 5, therefore insert 8
  • 302. The node, now contain 5 keys which is greater than (5 -1 = 4 ) keys. Therefore split the node from the median i.e. 8 and push it up to its parent node shown as follows
  • 303. Deletion Deletion is also performed at the leaf nodes. The node which is to be deleted can either be a leaf node or an internal node. Following algorithm needs to be followed in order to delete a node from a B tree. 1. Locate the leaf node. 2. If there are more than m/2 keys in the leaf node then delete the desired key from the node. 3. If the leaf node doesn't contain m/2 keys then complete the keys by taking the element from eight or left sibling. ○ If the left sibling contains more than m/2 elements then push its largest element up to its parent and move the intervening element down to the node where the key is deleted. ○ If the right sibling contains more than m/2 elements then push its smallest element up to the parent and move intervening element down to the node where the key is deleted.
  • 304. 1. If neither of the sibling contain more than m/2 elements then create a new leaf node by joining two leaf nodes and the intervening element of the parent node. 2. If parent is left with less than m/2 nodes then, apply the above process on the parent too. If the the node which is to be deleted is an internal node, then replace the node with its in-order successor or predecessor. Since, successor or predecessor will always be on the leaf node hence, the process will be similar as the node is being deleted from the leaf node.
  • 305. Delete the node 53 from the B Tree of order 5 shown in the following figure.
  • 306. 53 is present in the right child of element 49. Delete it.
  • 307. Now, 57 is the only element which is left in the node, the minimum number of elements that must be present in a B tree of order 5, is 2. it is less than that, the elements in its left and right sub-tree are also not sufficient therefore, merge it with the left sibling and intervening element of parent i.e. 49.
  • 308.
  • 309. Graph
  • 310.
  • 311.
  • 312.
  • 313.
  • 314.
  • 315. Types of Graph • Simple graph • Multigraph • Directed graph • Undirected graph • Weighted graph • Regular graph • Complete graph • Strongly connected graph • Weakly connected graph • Isomorphic graph
  • 316. Simple graph Does not contain loop or multiple edges
  • 317.
  • 318.
  • 319. Weighted graph • A graph in which the weights are assigned to each edge
  • 320. Regular Graph • A graph in which each vertex has the same degree ‘k’
  • 321.
  • 322. Strongly connected graph • Where each vertex is connected to every other vertex with the direction
  • 323. Weakly connected graph • A directed graph is weakly connected if at least two vertices are not connected
  • 324. Isomorphic graph Two graphs in which there is a one to one correspondence between their vertices and between their edges
  • 325. ● Are the number of vertices in both graphs the same? Yes, both graphs have 4 vertices. ● Are the number of edges in both graphs the same? Yes, both graphs have 4 edges. ● Is the degree sequence in both graphs the same? Yes, each vertex is of degree 2. ● If the vertices in one graph can form a cycle of length k, can we find the same cycle length in the other graph? Yes, each graph has a cycle of length 4.
  • 326.
  • 327.
  • 328. Graph representation • Sequential representation using adjacency matrix • Linked representation using adjacency list or linked list of neighbours
  • 329. Sequential representation using adjacency matrix • use of an adjacency matrix to represent the mapping between vertices and edges of the graph. • adjacency matrix can be used to represent the undirected graph, directed graph, weighted directed graph, and weighted undirected graph. • If adj[i][j] = w, it means that there is an edge exists from vertex i to vertex j with weight w. • An entry Aij in the adjacency matrix representation of an undirected graph G will be 1 if an edge exists between Vi and Vj . • If an Undirected Graph G consists of n vertices, then the adjacency matrix for that graph is n x n, and the matrix A = [aij] can be defined as - • aij = 1 {if there is a path exists from Vi to Vj } • a = 0 {Otherwise}
  • 330.
  • 331.
  • 332.
  • 333.
  • 334. Linked representation using adjacency list •
  • 335.
  • 336.
  • 337.
  • 338. Hashing • Hashing is a fundamental data structure that efficiently stores and retrieves data in a way that allows for quick access. • It involves mapping data to a specific index in a hash table using a hash function, enabling fast retrieval of information based on its key. • This method is commonly used in databases, caching systems, and various programming applications to optimize search and retrieval operations. • Avoids number of comparisons • Goes directly where the required data is present • Searches in constant time ,no matter where the element is
  • 339. The process of hashing can be broken down into three steps: ○ Input: The data to be hashed is input into the hashing algorithm. ○ Hash Function: The hashing algorithm takes the input data and applies a mathematical function to generate a fixed-size hash value. The hash function should be designed so that different input values produce different hash values, and small changes in the input produce large changes in the output. ○ Output: The hash value is returned, which is used as an index to store or retrieve data in a data structure.
  • 340.
  • 341. • Provides direct access to records thru hash function • Hash functions are used in hash tables • HT uses an array to hold ,or reference,the stored records • Most useful in case of large records of data are to be stored • Commonly used for symbol table • HT is used to process the hash value generated by applying some function on the key • Determines where to store the record • Hash function must be chosen so that its return the value is always a valid index for the array
  • 342. • When hash function maps to the same location in the hash table for two different keys -collision • Eg ( 0012 ,1112 )%100 =12 • Hash function never provides collision is called a perfect hash function • Difficult to find perfect function • HF Minimizes collision by spreading elements uniformly throughout the array -good hash function
  • 343. Characteristics of a good hash function • Formed using mathematical transformation • Should be very easy and quick to compute • Produce relatively random and unique distribution of values within the hash table so that it produces as few collisions as possible • Must minimize collisions
  • 344. Hash functions • Division method • Mid square method • Folding method
  • 345. Division •Hash function obtained by using the modulo(mod) operator •The value is divided by some number M(size of the hash table) and the remainder is used as the hash address for X Example Size of Hash Table (m) = 1000 (0 - 999) Suppose we want to calculate the index of element x, where x = 123789456 index =123789456 mod 1000 = 456 The element x is stored at position 456 in the hash table.
  • 346. Mid Square method •Mid-Square(f m ) hashing is a hashing technique in which unique keys are generated. •a seed value is taken and it is squared. •Then, some digits from the middle are extracted. These extracted digits form a number which is taken as the new seed. •This technique can generate keys with high randomness if a big enough seed value is taken. •This process is repeated as many times as a key is required.
  • 347.
  • 348.
  • 349. Folding method •The key k is partitioned into a number of parts k 1 , k 2 .... k n where each part except possibly the last, has the same number of digits as the required address. •Then the parts are added together, ignoring the last carry. •There are two type of folding: • ØShift –all are added except least bit • ØBoundary-Alternate pieces are flipped on the boundary. Boundary folding is indicated by p_i^r
  • 350.
  • 351. The task is to fold the key 452378912 into a Hash Table of ten spaces (0 through 9). ● It is given that the key, say X is 452378912 and the table size (i.e., M = 10). ● Since it can break X into three parts in any order. Let’s divide it evenly. ● Therefore, a = 452, b = 378, c = 912. ● Now, H(x) = (a + b + c) mod M i.e., H(452378912) =(452 + 378 + 912) mod 10 = 1742 mod 10 = 2. ● Hence, 452378912 is inserted into the table at address 2.
  • 352. Sorting • Retrieval and searching of data in a particular order becomes easier • Sorting is classified into • Internal sorting and • External Sorting
  • 353. Internal Sorting • Elements to be sorted in main memory • Where the list of all elements is small so the sorting can be carried in main memory
  • 354. External Sorting • Elements sorted in secondary memory • Eg disks and tapes • Applied to large file of records
  • 355. Sorting algorithms • Bubble sort • Selection sort • Quick sort • Heap sort • Merge sort
  • 356. Bubble sort • Easy to understand and implement • Multiple swapping takes place while passing through the elements sequentially • In each pass compare the element with its successor and interchange if they are not in proper order • Interchange is done bubbling up the elements • Process is carried on till it is sorted
  • 357.
  • 358.
  • 359.
  • 360. Time and Space Complexity • When bubble sort is used on an array already in ascending order, it requires only one pass through the entire array. • This is considered the best-case scenario. In practice, though, this only occurs sometimes, and bubble sort usually necessitates n(n-1)/2 swaps or comparisons to achieve a sorted array.
  • 361. • In terms of space complexity, since only swapped the elements with one another and never stored anything, don’t need any extra space to run the algorithm. • This is amazing because it means the space complexity comes out as constant, or O(1). • This makes it an in-place algorithm that works by modifying the input directly.
  • 362.
  • 363.
  • 365. Selection sort • Search for the smallest element in the list and interchange it with the first(0th) position • This process is repeated for the remaining unsorted portion until the entire list is sorted.
  • 366.
  • 367. Time Complexity: The time complexity of Selection Sort is O(N2) as there are two nested loops: ● One loop to select an element of Array one by one = O(N) ● Another loop to compare that element with every other Array element = O(N) ● Therefore overall complexity = O(N) * O(N) = O(N*N) = O(N2)
  • 368. Auxiliary Space: O(1) as the only extra memory used is for temporary variables while swapping two values in Array. The selection sort never makes more than O(N) swaps and can be useful when memory writing is costly.
  • 369. Quick Sort • Popular and widely used algorithm • Developed by CAR Hoare in 1962 • Quickest in average case scenario • Works on divide and conquer strategy • Larger problems are divided into sub problems and finding solutions for the sub problems • Solutions of the smaller subproblems are combined to obtain the solution of the whole problem • Selects a key element called pivot
  • 370. Divide: In Divide, first pick a pivot element. After that, partition or rearrange the array into two sub-arrays such that each element in the left sub-array is less than or equal to the pivot element and each element in the right sub-array is larger than the pivot element. Conquer: Recursively, sort two subarrays with Quicksort.
  • 371. function quickSort(arr): if length(arr) <= 1: return arr else: pivot = selectPivot(arr) // Choose pivot element left = [elements in arr less than pivot] right = [elements in arr greater than pivot] return concatenate(quickSort(left), pivot, quickSort(right))
  • 372.
  • 373. Heap Sort •Heap is a tree-based data structure in which all the tree nodes are in a particular order, such that the tree satisfies the heap properties •Heap sort may be regarded as two stage method • It is converted to heap with the property that the value of each node is at least as large as the value of its children nodes .root is the largest key in the tree • The output sequence is generated in decreasing order by successively outputting the root and restructuring the remaining tree into a heap •Follow the given steps to solve the problem: • Build a max heap from the input data. • At this point, the maximum element is stored at the root of the heap. Replace it with the last item of the heap followed by reducing the size of the heap by 1. Finally, heapify the root of the tree. • Repeat step 2 while the size of the heap is greater than 1.
  • 374.
  • 375.
  • 376.
  • 377.
  • 378.
  • 379.
  • 380.
  • 381. Sorting heap by repeatedly deleting the heap
  • 382.
  • 383.
  • 384.
  • 385.
  • 386.
  • 387.
  • 388.
  • 389. Merge Sort • External sorting • Divide and conquer strategy • that works by dividing an array into smaller subarrays, sorting each subarray, and then merging the sorted subarrays back together to form the final sorted array.
  • 390.
  • 391.
  • 392.
  • 393.
  • 394.
  • 395.
  • 396. UNIT-V ❖ Algorithm Design Techniques: ➢ Greedy Algorithms ➢ Prim’s Algorithm, ➢ Kruskal’s Algorithm. ❖ Divide and Conquer: ➢ Running Time of Divide and conquer algorithms. ❖ Decrease and Conquer ➢ Depth First Search ➢ Breadth First Search. ❖ Backtracking Algorithms ➢ n Queens Problem ➢ Branch and Bound ➢ Traveling Salesman Problem
  • 397. Algorithm Design Techniques What is an algorithm design technique? An algorithm design technique (or “strategy” or “paradigm”) is a general approach to solving problems algorithmically that is applicable to a variety of problems from different areas of computing.
  • 398. • they provide guidance for designing algorithms for new problems, i.e., problems for which there is no known satisfactory algorithm. • algorithms are the cornerstone of computer science. • Algorithm design techniques make it possible to classify algorithms according to an underlying design idea
  • 399. • The classification of algorithms is important for several reasons: Organization: Algorithms can be very complex and by classifying them, it becomes easier to organize, understand, and compare different algorithms. Problem Solving: Different problems require different algorithms, and by having a classification, it can help identify the best algorithm for a particular problem. Performance Comparison: By classifying algorithms, it is possible to compare their performance in terms of time and space complexity, making it easier to choose the best algorithm for a particular use case. Reusability: By classifying algorithms, it becomes easier to re-use existing algorithms for similar problems, thereby reducing development time and improving efficiency. Research: Classifying algorithms is essential for research and development in computer science, as it helps to identify new algorithms and improve existing ones.
  • 400. Classification by Design Method • Greedy Method: In the greedy method, at each step, a decision is made to choose the local optimum, without thinking about the future consequences. • Divide and Conquer: The Divide and Conquer strategy involves dividing the problem into sub-problem, recursively solving them, and then recombining them for the final answer. • Dynamic Programming: The approach of Dynamic programming is similar to divide and conquer. The difference is that whenever we have recursive function calls with the same result, instead of calling them again we try to store the result in a data structure in the form of a table and retrieve the results from the table. Thus, the overall time complexity is reduced. “Dynamic” means we dynamically decide, whether to call a function or retrieve values from the table. • Linear Programming: In Linear Programming, there are inequalities in terms of inputs and maximizing or minimizing some linear functions of inputs.
  • 401. • Reduction(Transform and Conquer): In this method, we solve a difficult problem by transforming it into a known problem for which we have an optimal solution. Basically, the goal is to find a reducing algorithm whose complexity is not dominated by the resulting reduced algorithms. • Backtracking: This technique is very useful in solving combinatorial problems that have a single unique solution. Where we have to find the correct combination of steps that lead to fulfillment of the task. Such problems have multiple stages and there are multiple options at each stage. This approach is based on exploring each available option at every stage one-by-one. While exploring an option if a point is reached that doesn’t seem to lead to the solution, the program control backtracks one step, and starts exploring the next option. In this way, the program explores all possible course of actions and finds the route that leads to the solution. • Branch and Bound: This technique is very useful in solving combinatorial optimization problem that have multiple solutions and we are interested in find the most optimum solution. In this approach, the entire solution space is represented in the form of a state space tree. As the program progresses each state combination is explored, and the previous solution is replaced by new one if it is not the optimal than the current solution.
  • 402. There are two approaches for designing an algorithm. these approaches include 1. Top-Down Approach : 2. Bottom-up approach ● Top-Down Approach: In the top-down approach, a large problem is divided into small sub-problem. and keep repeating the process of decomposing problems until the complex problem is solved. ● Bottom-up approach: The bottom-up approach is also known as the reverse of top-down approaches.In approach different, part of a complex program is solved using a programming language and then this is combined into a complete program.
  • 403. Top-Down Approach: Breaking down a complex problem into smaller, more manageable sub-problems and solving each sub-problem individually. Designing a system starting from the highest level of abstraction and moving towards the lower levels. Bottom-Up Approach: Building a system by starting with the individual components and gradually integrating them to form a larger system. Solving sub-problems first and then using the solutions to build up to a solution of a larger problem. Note: Both approaches have their own advantages and disadvantages and the choice between them often depends on the specific problem being solved.
  • 404. Greedy Algorithms • to solving a problem that selects the most appropriate option based on the current situation. • ignores the fact that the current best result may not bring about the overall optimal result. • Even if the initial decision was incorrect, the algorithm never reverses it. • intuitive algorithm can be applied to solve any optimization problem which requires the maximum or minimum optimum result. • easy to understand and implement.
  • 405. a greedy solution only if the problem statement follows two properties mentioned below: ● Greedy Choice Property: Choosing the best option at each phase can lead to a global (overall) optimal solution. ● Optimal Substructure: If an optimal solution to the complete problem contains the optimal solutions to the subproblems, the problem has an optimal substructure.
  • 406.
  • 407.
  • 408. Limitations of Greedy Algorithm Factors listed below are the limitations of a greedy algorithm: 1. The greedy algorithm makes judgments based on the information at each iteration without considering the broader problem; hence it does not produce the best answer for every problem. 2. The problematic part for a greedy algorithm is analyzing its accuracy. Even with the proper solution, it is difficult to demonstrate why it is accurate. 3. Optimization problems (Dijkstra’s Algorithm) with negative graph edges cannot be solved using a greedy algorithm.
  • 409. • The main disadvantage of using a greedy algorithm is that it may not find the optimal solution to a problem. • In other words, it may not produce the best possible outcome. • Additionally, greedy algorithms can be very sensitive to changes in input data — even a small change can cause the algorithm to produce a completely different result. • Finally, greedy algorithms can be difficult to implement and understand.
  • 410. Prim's Algorithm • used to find the minimum spanning tree from a graph. • Prim's algorithm finds the subset of edges that includes every vertex of the graph such that the sum of the weights of the edges can be minimized. • Prim's algorithm starts with the single node and explores all the adjacent nodes with all the connecting edges at every step. • The edges with the minimal weights causing no cycles in the graph got selected.
  • 411. Spanning tree - A spanning tree is the subgraph of an undirected connected graph. Minimum Spanning tree - Minimum spanning tree can be defined as the spanning tree in which the sum of the weights of the edge is minimum. The weight of the spanning tree is the sum of the weights given to the edges of the spanning tree.
  • 412.
  • 413.
  • 414. The first spanning tree has the least weight i.e. 10 and hence is the minimum spanning tree.
  • 415. Kruskal Algorithm • Another minimum spanning tree problem that also always yields an optimal solution. • It is named Kruskal’s algorithm after Joseph Kruskal • Kruskal’s algorithm looks at a minimum spanning tree of a weighted connected graph G = ⟨V, E⟩ as an acyclic subgraph with |V | − 1 edges for which the sum of the edge weights is the smallest. • The algorithm begins by sorting the graph’s edges in nondecreasing order of their weights. • Then, starting with the empty subgraph, it scans this sorted list,adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise.
  • 416. How does Kruskal's algorithm work? In Kruskal's algorithm, we start from edges with the lowest weight and keep adding the edges until the goal is reached. The steps to implement Kruskal's algorithm are listed as follows - ○ First, sort all the edges from low weight to high. ○ Now, take the edge with the lowest weight and add it to the spanning tree. If the edge to be added creates a cycle, then reject the edge. ○ Continue to add the edges until we reach all vertices, and a minimum spanning tree is created.
  • 417. The applications of Kruskal's algorithm are - ○ Kruskal's algorithm can be used to layout electrical wiring among cities. ○ It can be used to lay down LAN connections.
  • 418.
  • 419.
  • 420.
  • 421.
  • 422.
  • 423. Step 5 - After that, pick the edge AE with weight 5. Including this edge will create the cycle, so discard it. Step 6 - Pick the edge AC with weight 7. Including this edge will create the cycle, so discard it. Step 7 - Pick the edge AD with weight 10. Including this edge will also create the cycle, so discard it. So, the final minimum spanning tree obtained from the given weighted graph by using Kruskal's algorithm is -
  • 424.
  • 425. ○ Time Complexity The time complexity of Kruskal's algorithm is O(E logE) or O(V logV), where E is the no. of edges, and V is the no. of vertices.
  • 426. Running Time of Divide and conquer algorithms. • The divide and conquer approach as the name suggests divides the given problem in parts and then each problem is solved independently. • When we keep dividing the problem into smaller parts a moment comes when the problem cannot be divided further into smaller part, then those smaller parts are solved and the solution of all those smaller parts or sub-parts is finally merged to obtain the solution of the original problem.
  • 427.
  • 428. Divide and Conquer Algorithm contains the following steps: 1. Divide: This involves dividing the problem into smaller sub problem. 2. Conquer: Solving the smaller sub-problems recursively. 3. Combine: Combine the solutions of the sub-problems that are part of the recursive process to solve the actual problem.
  • 429. Advantages of Divide and Conquer: 1. This algorithm makes the given problem easier as it divides the given problem into sub-problems which makes it easier to solve and then solving each of them individually and combining all the solution of all sub-problem into one to solve the original problem. 2. The algorithm increases the efficiency of other algorithms such as quick sort, merge sort etc. These algorithms are the application of Divide and Conquer Algorithm. 3. This algorithm is supported by most of the processors especially with shared memory system where data communication between processors does not need to be pre-programmed. 4. This algorithm uses memory caches in an efficient manner.
  • 430. Disadvantages of Divide and Conquer: 1. The main problem which arises with this algorithm is the recursion is slow that increases the time complexity. 2. Another problem which this algorithm is that sometimes it becomes complicated while solving the problem through this approach and the basic iterative approach seems more easier. 3. While solving the problems through this approach sometimes there are same sub-problems. So its best to save the solution to the repeated sub problem. 4. While using this algorithms, make sure there is enough memory allocated to the return stack;
  • 431. Divide and Conquer Applications: • Binary Search • Merge Sort • Quick Sort • Strassen’s Matrix multiplication
  • 432. Time Complexity The complexity of the divide and conquer algorithm is calculated using the master theorem which is as follow.
  • 433. Decrease and Conquer make the problem smaller by reducing problem at each step. They can reduce the problem by ● constant amount ● constant factor ● variable factor
  • 434. • used to solve problems by reducing the size of the input data at each step of the solution process. • This technique is similar to divide-and-conquer, in that it breaks down a problem into smaller subproblems, but the difference is that in decrease-and-conquer, the size of the input data is reduced at each step. • The technique is used when it’s easier to solve a smaller version of the problem, and the solution to the smaller problem can be used to find the solution to the original problem. 1. binary search, finding the maximum or minimum element in an array, and finding the closest pair of points in a set of points. 2. The main advantage of decrease-and-conquer is that it often leads to efficient algorithms, as the size of the input data is reduced at each step, reducing the time and space complexity of the solution.
  • 435. Insertion Sort To sort an array of size N in ascending order iterate over the array and compare the current element (key) to its predecessor, if the key element is smaller than its predecessor, compare it to the elements before. Move the greater elements one position up to make space for the swapped element.
  • 436.
  • 437.
  • 438.
  • 439. 1. defines a function named insertion_sort that takes a single argument arr, which is expected to be a list. 2. calculates the length of the input list arr and assigns it to the variable n. 3. initiates a loop that iterates over the indices of the list arr starting from index 1 up to (but not including) n. 4. selects the current element (key) at index i from the list arr. This element will be compared and inserted into its correct position in the sorted sublist. 5. initializes a variable j to the index directly before i. j will be used to traverse the sorted sublist from right to left to find the correct position for key. def insertion_sort(arr): n = len(arr) for i in range(1, n): key = arr[i] j = i - 1 while j >= 0 and arr[j] > key: arr[j + 1] = arr[j] j -= 1 arr[j + 1] = key
  • 440. 1. starts a while loop that continues as long as j is greater than or equal to 0 (ensuring we don't go out of bounds of the list) and the element at index j is greater than the key value. This loop moves elements of the sorted sublist greater than key one position to the right. 2. Within the while loop, these two lines shift elements of the sorted sublist to the right to make space for the key element. j is decremented to continue traversing the sorted sublist. 3. nce the correct position for key is found (either at index j+1 or 0 if j becomes -1), key is inserted into the sorted sublist at index j+1. def insertion_sort(arr): n = len(arr) for i in range(1, n): key = arr[i] j = i - 1 while j >= 0 and arr[j] > key: arr[j + 1] = arr[j] j -= 1 arr[j + 1] = key
  • 441. Breadth First Search • It is also known as level order traversal. • The Queue data structure is used for the Breadth First Search traversal. • using the BFS algorithm for the traversal in a graph,consider any node as a root node and explores all the neighboring nodes. • Then, it selects the nearest node and explores all the unexplored nodes. • While using BFS for traversal, any node in the graph can be considered as the root node.
  • 442. • BFS is the most commonly used approach. It is a recursive algorithm to search all the vertices of a tree or graph data structure. • BFS puts every vertex of the graph into two categories - visited and non-visited. • It selects a single node in a graph and, after that, visits all the nodes adjacent to the selected node. • Breadth-First Search uses a queue data structure to store the node and mark it as "visited" until it marks all the neighboring vertices directly related to it. • The queue operates on the First In First Out (FIFO) principle, so the
  • 443.
  • 444. How Does the Algorithm Operate? ● Start with the source node. ● Add that node at the front of the queue to the visited list. ● Make a list of the nodes as visited that are close to that vertex. ● And dequeue the nodes once they are visited. ● Repeat the actions until the queue is empty.
  • 445.
  • 446.
  • 447.
  • 448.
  • 449. Complexity Of Breadth-First Search Algorithm • The time complexity of the breadth-first search algorithm can be stated as O(|V|+|E|) because, in the worst case, it will explore every vertex and edge. • The number of vertices in the graph is |V|, while the edges are |E|. • the space complexity as O(|V|), where |V| is the number of vertices in the graph, and different data structures are needed to determine which vertices have already been added to the queue.
  • 450.
  • 451. BFS applications • GPS Navigation System • Broadcasting Network • Identifying Routes
  • 452. Depth First Search (DFS) • It is a recursive algorithm to search all the vertices of a tree data structure or a graph. • The depth-first search (DFS) algorithm starts with the initial node of graph G and goes deeper until we find the goal node or the node with no children. • Because of the recursive nature, stack data structure can be used to implement the DFS algorithm. • The process of implementing the DFS is similar to the BFS algorithm
  • 453. The step by step process to implement the DFS traversal is given as follows - 1. First, create a stack with the total number of vertices in the graph. 2. Now, choose any vertex as the starting point of traversal, and push that vertex into the stack. 3. After that, push a non-visited vertex (adjacent to the vertex on the top of the stack) to the top of the stack. 4. Now, repeat steps 3 and 4 until no vertices are left to visit from the vertex on the stack's top. 5. If no vertex is left, go back and pop a vertex from the stack. 6. Repeat steps 2, 3, and 4 until the stack is empty.
  • 454.
  • 455. DFS Applications ○ DFS algorithm can be used to implement the topological sorting. ○ It can be used to find the paths between two vertices. ○ It can also be used to detect cycles in the graph. ○ DFS algorithm is also used for one solution puzzles.
  • 456.
  • 457.
  • 458.
  • 459.
  • 460.
  • 461.
  • 462.
  • 463.
  • 464.
  • 465.
  • 466. Complexity of Depth-first search algorithm The time complexity of the DFS algorithm is O(V+E), where V is the number of vertices and E is the number of edges in the graph. The space complexity of the DFS algorithm is O(V).
  • 467.
  • 468. Backtracking algorithms • used to solve the problem. • uses Brute force search to solve the problem, • to make all the possible solutions and pick out the best solution from all the desired solutions. • This rule is also followed in dynamic programming, but dynamic programming is used for solving optimization problems. • In contrast, backtracking is not used in solving optimization problems. • Backtracking is used when having multiple solutions, and require all those solutions.
  • 469.
  • 470. • It finds a solution by building a solution step by step, increasing levels over time, using recursive calling. • A search tree known as the state-space tree is used to find these solutions. • Each branch in a state-space tree represents a variable, and each level represents a solution. • A backtracking algorithm uses the depth-first search method. • When the algorithm begins to explore the solutions, the abounding function is applied so that the algorithm can determine whether the proposed solution satisfies the constraints. • If it does, it will keep looking. If it does not, the branch is removed, and the algorithm
  • 471. How Does a Backtracking Algorithm Work? In any backtracking algorithm, the algorithm seeks a path to a feasible solution that includes some intermediate checkpoints. If the checkpoints do not lead to a viable solution, the problem can return to the checkpoints and take another path to find a solution
  • 472. Examples • To Find All Hamiltonian Paths Present in a Graph. • To Solve the N Queen Problem. • Maze Solving Problems
  • 473. To Find All Hamiltonian Paths Present in a Graph. A Hamiltonian path, also known as a Hamilton path, is a graph path connecting two graph vertices that visit each vertex exactly once. If a Hamiltonian way exists with adjacent endpoints, the resulting graph cycle is a Hamiltonian or Hamiltonian cycle.
  • 474. To Solve the N Queen Problem ● The problem of placing n queens on the nxn chessboard so that no two queens attack each other is known as the n-queens puzzle. ● Return all distinct solutions to the n-queens puzzle given an integer n. You are free to return the answer in any order. ● Each solution has a unique board configuration for the placement of the n-queens, where 'Q' and. '' represent a queen and a space, respectively.
  • 475. N-Queens problem • N - Queens problem is to place n - queens in such a manner on an n x n chessboard that no queens attack each other by being in the same row, column or diagonal. • Problem Statement: We need to find out all the possible arrangements in which N queens can be seated in each row and each column so that all queens are safe. The queen moves in 8 directions and can directly attack in these 8 directions only. • his problem demands us to put 4 queens on 4 X 4 chessboard in such a way that one queen is present in each row and column and no queen can attack any other queen directly. this means no 2 or more queens can be placed in the same diagonal or row or column.
  • 476.
  • 477.
  • 478. Let's try to put queens Q1, Q2, Q3, and Q4 in the above present chessboard. The first queen i.e. Q1 can be put anywhere on the chessboard as there is no other queen present on the board and hence no restrictions. Therefore putting Q1 at position (0,0). So the path so far is| (0,0)|.
  • 479. When Q1 has been placed there are some places where the next queens can't be placed to fulfill given conditions. So to put queen Q2 in the second row we have positions - (1,2) and (1,3). Let's put it at (1,2).The path so far is | (0,0) -> (1,2)|
  • 480. Now this placement of Q2 blocks all the boxes of row 3 and hence there is no way to put Q3. If we put it at (2,0) or (2,2), Q1 will attack it, and at (2,1) and (2,3) Q2 attacks it. Therefore we backtrack from here and revisit the previous solution by readjusting the position of Q2. So instead of putting it at (1,2), we put it at (1,3). The path so far is | (0,0) -> (1,3)|
  • 481. put Q3 at (2,1). Hence, the path so far is | (0,0) -> (1,3) -> (2,1)|.
  • 482. Now again the same problem occurs, there left no box to place Q4. There was only 1 way to place Q3 and all placements of Q2 have been explored, so now we come to Q1 for re-adjustment. We move it from (0,0) to (0,1). The path so far is | (0,1)|.
  • 483. put Q2 at (1,0). The path so far is | (0,1) -> (1,0)|.
  • 484. q3 is put at (2,2). The path so far is | (0,1) -> (1,0) -> (2,2)|.Q
  • 485. Now again there is no space left for placement of Q4 in row 4. Therefore we again backtrack and readjust position of Q2 from (1,0) to (1,3).The path so far is | (0,1) -> (1,3)|.
  • 486. Q3 is put at (2,0). The path so far is | (0,1) -> (1,0) -> (2,0)|.
  • 487. put Q4 at (3,2). The path so far is | (0,1) -> (1,0) -> (2,0) -> (3,2)|.
  • 488. Therefore through backtracking, we reached a solution where 4 queens are put in each row and column so that no queen is attacking any other on a 4 X 4 chessboard.
  • 489. N Queen Problem Algorithm 1. create a board of N x N size that stores characters. It will store 'Q' if the queen has been placed at that position else '.' 2. create a recursive function called "solve" that takes board and column and all Boards (that stores all the possible arrangements) as arguments. We will pass the column as 0 so that we can start exploring the arrangements from column 1. 3. In solve function will go row by row for each column and will check if that particular cell is safe or not for the placement of the queen, we will do so with the help of isSafe() function. 4. For each possible cell where the queen is going to be placed, will first check isSafe() function.
  • 490. 5. If the cell is safe, we put 'Q' in that row and column of the board and again call the solve function by incrementing the column by 1. 6. Whenever we reach a position where the column becomes equal to board length, this implies that all the columns and possible arrangements have been explored, and so we return. 7. Coming on to the boolean isSafe() function, we check if a queen is already present in that row/ column/upper left diagonal/lower left diagonal/upper right diagonal /lower right diagonal. If the queen is present in any of the directions, we return false. Else we put board[row][col] = 'Q' and return true.
  • 491. Branch and Bound • is one of the techniques used for problem solving. • It is similar to the backtracking since it also uses the state space tree. • It is used for solving the optimization problems and minimization problems. • given a maximization problem then can convert it using the Branch and bound technique by simply converting the problem into a maximization problem.
  • 492. • Branch and bound algorithms are used to find the optimal solution for combinatory, discrete, and general mathematical optimization problems. • the branch and bound algorithm extends all the paths it can follow. That means it visits all the nodes and all the paths to generate the shortest distance.
  • 493.
  • 494. Travelling Salesman Problem given- ● A set of some cities ● Distance between every pair of cities Travelling Salesman Problem states- ● A salesman has to visit every city exactly once. ● He has to come back to the city from where he starts his journey. ● What is the shortest possible route that the salesman must follow to complete his tour?
  • 495. Example: Consider a salesman who needs to visit 4 cities: A, B, C, and D. The distances between these cities are as follows: ● A to B: 10 units ● A to C: 15 units ● A to D: 20 units ● B to C: 35 units ● B to D: 25 units ● C to D: 30 units The goal is to find the shortest route that visits each city exactly once and returns to the starting point.
  • 496. Steps to Solve TSP: • Generate Permutations: Generate all possible permutations of the cities. Each permutation represents a possible route that the salesman can take. • Calculate Route Lengths: Calculate the total distance for each permutation. Add up the distances between consecutive cities in the permutation and add the distance from the last city back to the starting city. • Find the Shortest Route: Identify the permutation with the shortest total distance. This represents the optimal route for the salesman.
  • 497. Applying Steps to the Example: Generate Permutations: ● Possible permutations: ABCD, ABDC, ACBD, ACDB, ..., DCBA ● There are 4! = 24 possible permutations in total. Calculate Route Lengths: ● For each permutation, calculate the total distance. ● For example, for the permutation ABCD, the total distance = 10 + 15 + 20 + 30 + 10 = 85 units. Find the Shortest Route: ● Compare the total distances calculated for each permutation. ● Identify the permutation with the shortest total distance. This represents the shortest route.