You are on page 1of 60

ELECTRONICS

AND
TELECOMMUNICATION
ENGINEERING
B.Tech V SEMESTER
Advance Data Structures and
Algorithms

Subject Code – C022535(022)


Syllabus
• UNIT I Linear Data Structures : Introduction - Abstract
Data Types (ADT) – Stack – Queue – Circular Queue - Double
Ended Queue - Applications of stack – Evaluating Arithmetic
Expressions - Other Applications - Applications of Queue -
Linked Lists - Singly Linked List - Circularly Linked List - Doubly
Linked lists – Applications of linked list – Polynomial
Manipulation.
• UNIT II Non - linear Tree Structures : Binary
Tree – expression trees – Binary tree traversals – applications
of trees – Huffman Algorithm - Binary search tree - Balanced
Trees - AVL Tree - B-Tree - Splay Trees – Heap operations- -
Binomial Heaps - Fibonacci Heaps- Hash set.
Syllabus
• UNIT III Graphs: Representation of graph - Graph Traversals - Depth-
first and breadth-first traversal - Applications of graphs - Topological
sort – shortest-path algorithms - Dijkstra‟s algorithm – Bellman-Ford
algorithm – Floyd's Algorithm - minimum spanning tree – Prim's and
Kruskal's algorithms.

• UNIT IV Algorithm and Analysis: Algorithm Analysis – Asymptotic


Notations - Divide and Conquer – Merge Sort – Quick Sort - Binary
Search - Greedy Algorithms – Knapsack Problem – Dynamic
Programming – Optimal Binary Search Tree - Warshall‟s Algorithm
for Finding Transitive Closure.

• UNIT V Advanced Algorithm Design and Analysis: Backtracking – N-


Queen's Problem - Branch and Bound – Assignment Problem - P &
NP problems – NP-complete problems – Approximation algorithms
for NP-hard problems – Traveling salesman problem-Amortized
Analysis.
Text Books
1. Data Structures, Seymour Lipschutz,
Schaum’s Outline Series.
UNIT-I
Linear Data Structures
Data Structures
• A data structure is a particular way of organizing
data in a computer so that it can be used
effectively.

• For example, we can store a list of items having


the same data-type using the array data
structure.
DATA STRUCTURE
• Data structures are used to store data in a
computer in an organized form. In C++
language Different types of data structures
are;
1. Array,
2. Stack,
3. Queue,
4. Linked List,
5. Tree.
Types of Data Structure

1. Array: Array is collection of similar data type,


you can insert and deleted element form array
without following any order.
2. Stack: Stack work on the basis of Last-In-First-
Out (LIFO). Last entered element removed first.
3. Queue: Queue work on the basis of First-In-
First-Out (FIFO). First entered element removed
first.
4. Linked List: Linked list is the collection of
node, Here you can insert and delete data in any
order.
5. Tree: Stores data in a non linear form with
one root node and sub nodes.
Linear Data Structure
• It is a type of data structure where the arrangement of the data
follows a linear trend. The data elements are arranged linearly
such that the element is directly linked to its previous and the
next elements.

Characteristics

1. It is a type of data structure where data is stored and


managed in a linear sequence.
2. Data elements in the sequence are linked to one after the
other.
3. Implementation of the linear structure of data in a computer’s
memory is easy as the data is organized sequentially.
4. Array, queue. Stack, linked list, etc. are examples of this type
of structure.
Abstract Data Types
• The Data Type is basically a type of data that can be
used in different computer program.
• It signifies the type like integer, float etc, the space
like integer will take 4-bytes, character will take 1-
byte of space etc.
• The abstract data type is special kind of data type,
whose behaviour is defined by a set of values and
set of operations.
• The keyword “Abstract” is used as we can use these
data types, we can perform different operations.
• Some examples of ADT are Stack, Queue, List etc
STACK
• Stack is a simple data structure that allows
adding and removing elements in a particular
order.
• Every time an element is added, it goes on the
top of the stack and the only element that can
be removed is the element that is at the top of
the stack, just like a pile of objects.
• It is named stack as it behaves like a real-world
stack, for example – a deck of cards or a pile of
plates, etc.
Basic features of Stack
• Stack is an ordered list of similar data type.
• Stack is a LIFO(Last in First out) structure or we can say FILO(First in Last
out).
• push() function is used to insert new elements into the Stack
and pop() function is used to remove an element from the stack. Both
insertion and removal are allowed at only one end of Stack called Top.
• Stack is said to be in Overflow state when it is completely full and is said to
be in Underflow state if it is completely empty.
Push Operation
The process of putting a new data element onto stack is known as a Push
Operation. Push operation involves a series of steps −
• Step 1 − Checks if the stack is full.
• Step 2 − If the stack is full, produces an error and exit.
• Step 3 − If the stack is not full, increments top to point next
empty space.
• Step 4 − Adds data element to the stack location, where top
is pointing.
• Step 5 − Returns success.
Algorithm for PUSH Operation
• A simple algorithm for Push operation can be
derived as follows −

begin procedure PUSH: stack, data


if stack is full Print ‘OVERFLOW’ and return null

end if
top ← top + 1
stack[top] ← data
end procedure
POP Operation
Accessing the content while removing it from the stack, is known as a Pop
Operation. A Pop operation may involve the following steps −
• Step 1 − Checks if the stack is empty.
• Step 2 − If the stack is empty, produces an error and exit.
• Step 3 − If the stack is not empty, accesses the data element at
which top is pointing.
• Step 4 − Decreases the value of top by 1.
• Step 5 − Returns success.
• Algorithm for Pop Operation
• A simple algorithm for Pop operation can be derived
as follows −

begin procedure POP: stack


if stack is empty then Print ‘UNDERFLOW’ and return null
endif
data ← stack[top]
top ← top - 1
return data
end procedure
Write a C++ program to push an element onto Stack and POP an element from STACK.

#include <iostream.h>
void main()
#include<conio.h>
{
int val;
int stack[100], n=100, top=-1;
void push(int val)
{ cout<<"\nEnter value to be pushed:\n";
if(top>=n-1) cin>>val;
cout<<"Stack Overflow"; push(val);
else display();
{ cout<<"Enter another value to be pushed:\n";
top++; cin>>val;
stack[top]=val; push(val);
} display();
} pop();
void pop() display();
{ getch();
if(top<=-1) }
cout<<"Stack Underflow\n”;
else
{
cout<<"\nThe popped element is "<< stack[top] ;
top--;
}
}
void display()
{
if(top>=0)
{
cout<<"\nStack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<"\n ";
}
else
cout<<"Stack is empty";
}
QUEUE
• Queue is an abstract data type which can be implemented as a linear or
circular list. It has a front and rear.
• Queue is linear data structure, in which the first element is inserted from
one end called the REAR(also called tail), and the removal of existing
element takes place from the other end called as FRONT(also
called head).
• The Queues are also called as FIFO (First-In-First-Out) lists, Since the first
element in the list will be removed first.
• Queue Representation :
• The following diagram given below tries to explain queue representation
as data structure −
• This makes queue as FIFO(First in First Out) data structure, which
means that element inserted first will be removed first.
• Which is exactly how queue system works in real world. If you go to
a ticket counter to buy movie tickets, and are first in the queue, then
you will be the first one to get the tickets. Right? Same is the case
with Queue data structure. Data inserted first, will leave the queue
first.
• The process to add an element into queue is called Enqueue and the
process of removal of an element from queue is called Dequeue.
• The condition FRONT = NULL will indicate that
Queue is empty.
• Whenever the element is deleted from the queue
the value of the FRONT is increased by one.
FRONT = FRONT +1;
• Similarly whenever any element is added to the
queue then the REAR value is increased by one.
REAR = REAR +1;
• IF FRONT = REAR=NULL then Queue is empty.
• If FRONT = REAR!=NULL
Then only one element is present in the Queue.
• There are four types of Queue:
1. Simple Queue
2. Circular Queue
3. Priority Queue
4. Dequeue (Double Ended Queue)
1. Simple Queue
• Simple queue is a linear list which defines the
simple operation of queue in which insertion
occurs at the rear end of the list and deletion
occurs at the front of the list.
2. Circular Queue
• In a circular queue, all nodes are treated as circular.
Last node is connected back to the first node.
• Circular queue is also called as Ring Buffer.
• It is an abstract data type.
• Circular queue contains a collection of data which
allows insertion of data at the end of the queue and
deletion of data at the beginning of the queue.
3. Priority Queue
• Each node in the list contains three items of information: an
information field INFO, a priority number PRN and a link number LINK.
• Priority queue contains data items which have some preset priority.
While removing an element from a priority queue, the data item with
the highest priority is removed first.
• In a priority queue, insertion is performed in the order of arrival and
deletion is performed based on the priority.
• A node X precedes a node Y in the list when
– Node X has the higher priority than Y OR
• when both have the same priority but X was added to the list before Y.
4. Dequeue (Double Ended Queue)
Dequeue is also known as Double Ended Queue.
It is a linear list in which elements can be added
or deleted at both the ends of the queue.
Enqueue Operation
• Queues maintain two data pointers, front and rear. Therefore, its operations
are comparatively difficult to implement than that of stacks.
• The following steps should be taken to enqueue (insert) data into a queue −
• Step 1 − Check if the queue is full.
• Step 2 − If the queue is full, produce overflow error and exit.
• Step 3 − If the queue is not full, increment rear pointer to point the next
empty space.
• Step 4 − Add data element to the queue location, where the rear is pointing.
• Step 5 − return success.
Enqueue Algorithm
Dequeue Operation
Accessing data from the queue is a process of two tasks − access the data
where front is pointing and remove the data after access. The following steps are
taken to perform dequeue operation −
• Step 1 − Check if the queue is empty.
• Step 2 − If the queue is empty, produce underflow error and exit.
• Step 3 − If the queue is not empty, access the data where front is pointing.
• Step 4 − Increment front pointer to point to the next available data element.
• Step 5 − Return success.
Dequeue Algorithm
What do you mean by UNDERFLOW and OVERFLOW in
CIRCULAR QUEUE?
• Circular Queue is a linear data structure in which
the operations are performed based on FIFO
(First In First Out) principle and the last position
is connected back to the first position to make a
circle. It is also called ‘Ring Buffer’.
• If Circular Queue is full then we can not insert an
element into Circular Queue. If value of REAR
variable is greater then or equal to SIZE – 1(here
8-1=7) and value of FRONT variable is equal to 0
then we can not insert an element into Circular
Queue. This condition is known as “Overflow”.
• In order to delete an element from Circular
Queue first we have to check weather Circular
Queue is empty or not. If Circular Queue is
empty then we cannot delete an element from
Circular Queue. This condition is known
as “Underflow”.
LINKED LIST
• Linked list is a one way list, which is a linear collection of data elements, called nodes,
where the linear order is given by means of pointers.
• Each node is divided is into two parts : The first part contains the data, and the second part
, called the link field or nextpointer field contains the address of the next node in the list.
• A linked list is a linear data structure, in which the elements are not stored at contiguous
memory locations.
• The elements in a linked list are linked using pointers as shown in the below image:
• In simple words, a linked list consists of nodes where each node contains a data field and a
reference(link) to the next node in the list.
• The null pointer denoted in the diagram,
signals the end of the list.
• The linked list also contains a list pointer
variable – called START or HEAD which
contains the address of the first node in the
list;
• Hence there is an arrow drawn from the START
or HEAD to the first node.
• When START contains null then there is no
elements in the list and the list is known as
empty list.
Number Patient Next
1 KIRK
2
3 DEAN
START
4 MAXWELL
5 ADAM
6
7 LANE
8 GREEN
9 Samuels
10
11 Fields
12 Nelson
Representation Of Linked List in the
Memory
• List requires two linear arrays
1. LINK[PTR]
2. INFO[PTR]
• INFO[PTR]contains the information part of the
list.
• LINK[PTR] contains the nextpointer field of the
LIST.
S.No INFO LINK

3 O 6 • START = 9
4 T 0
• INFO[9]=N LINK[9]=3
• INFO[3]=O LINK[3]=6
5 • INFO[6]=Y LINK[6]=7
• INFO[7]=X LINK[7]=4
6 Y 7
• INFO[4]=T LINK[4]=0
7 X 4

9 N 3
TRAVERSING A LINKED LIST
• Let LIST be a LINKED LIST in the memory stored
in the linear arrays INFO and LINK and START is
pointing to the first element in the LIST,NULL is
indicating the end of the LIST.
• PTR is the pointer which points to the current
node.
• LINK[PTR] points to the next node to be
processed.
PTR :=LINK[PTR]
Above statement moves the pointer to the next
node in the LIST.
1. Initialize PTR or START.
2. Process INFO[PTR]
(Information at the first Node)
3. PTR := LINK[PTR] (Update PTR so that PTR points to the second node).
4. again process INFO[PTR](The information at second node).
5. Again update PTR by
PTR := LINK[PTR]
And process INFO[PTR] and so on…. Until
PTR = NULL,
which indicates the end of the LIST.
SEARCHING A LINKED LIST
Insertion into a linked list
In the insert operation three pointer fields are changes as follows

1. The next pointer field of node A now points


to the new node N, to which AVAIL previously
pointed.
• AVAIL now points to the second node in the free pool, to
which node N previously pointed.
• The next pointer field of node N now points to node B, to
which node A previously pointed.
ALL of the Insertion Algorithm will include following steps:
1. Checking if space is available in the AVAIL list.
if AVAIL = NULL , then no free space hence ‘OVERFLOW’.
2. Removing the first node from the AVAIL list.
NEW := AVAIL
AVAIL : = LINK[AVAIL]
3. Copying new information into the new node.
INFO[NEW] : = ITEM
Write an algorithm to add an element at first position
Inserting an element at the beginning of the List
INSFIRST(INFO,LINK,START,AVAIL,ITEM)
This algorithm inserts ITEM as the first node in the list.
– [OVERFLOW?]
If AVAIL = NULL, then Write : OVERFLOW, and Exit.
– [Remove the first node from the AVAIL List.]
Set NEW := AVAIL and AVAIL:=LINK[AVAIL].
– Set INFO [NEW] := ITEM. [Copies new data into new node]
– Set LINK[NEW] := START.[new node now points to the original first node.]
– Set START := NEW. [Changes START so it points to the new node]
– Exit.
Inserting After a given node
• Let N be the new node(its location is NEW).
• If LOC=NULL, then N is inserted as the first node in the LIST.
• Suppose we want to insert the node N after node A and before node B.
• Then we make node N point to node B(which was originally after A).
• The above task is completed by following equations
• LINK[NEW] := LINK[LOC]
• And we make node A point to the new node N by
LINK[LOC] := NEW
ALGORITHM
INSLOC(INFO,LINK,START,AVAIL,LOC,ITEM)
This algorithm inserts ITEM so that ITEM follows the
node with location LOC or inserts ITEM as the first node
when LOC = NULL.
Deleting from the Linked List
All of deletion algorithms will return the memory space
of the deleted node N to the beginning of the AVAIL list.
Following pair of equations will perform the above task.
LINK[LOC]:=AVAIL
and
AVAIL:=LOC.
Deleting from the Linked List

DEL(INFO,LINK,START,AVAIL,LOC,LOCP)
This algorithm deletes the node N with location LOC.LOCP is the
location of the node which precedes N or ,when N is the first node,
LOCP=NULL.

If LOCP=NULL, then:
Set START:=LINK[START]. [Deletes the first node.]
Else :
Set LINK[LOCP] := LINK[LOC]. [Deletes node N]
[End of If Structure]
[Return Deleted node to the AVAIL List]

Set LINK[LOC]:=AVAIL and AVAIL:=LOC.


Exit.
Circular linked list
• A circular linked list is a variation of a linked list in
which the last node points to the first node,
completing a full circle of nodes. In other words, this
variation of the linked list doesn't have a null element
at the end
Singly Linked List as Circular
In singly linked list, the next pointer of the last node
points to the first node.
Memory Representation of circular linked list:
Doubly linked list
• Doubly linked list is a complex type of linked list in
which a node contains a pointer to the previous as
well as the next node in the sequence.
• Therefore, in a 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).
• A sample node in a doubly linked list is shown in the
figure.
Memory Representation of a doubly linked list

You might also like