0% found this document useful (0 votes)
2 views

II Bsc - Datastructures With Java-112 Copies

Data Structures are methods for organizing and storing data to facilitate efficient operations. They can be classified into primitive (like integers and floats) and compound types (like linked lists, trees, and graphs), with further distinctions between linear and non-linear structures. Abstract Data Types (ADTs) define data types by their operations, emphasizing modularity and ease of debugging in programming.

Uploaded by

balaparameswari
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

II Bsc - Datastructures With Java-112 Copies

Data Structures are methods for organizing and storing data to facilitate efficient operations. They can be classified into primitive (like integers and floats) and compound types (like linked lists, trees, and graphs), with further distinctions between linear and non-linear structures. Abstract Data Types (ADTs) define data types by their operations, emphasizing modularity and ease of debugging in programming.

Uploaded by

balaparameswari
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 84

INTRODUCTION TO DATA STRUCTURES

DATA STRUCTURES
Data Stuctures is a way of collecting and organisinig data in such a way that can perform
operations on these data in an effective way. Data Structures is about rendering data elements in
terms of some relationship, for better organization and storage. For example, we have data
players name “Virat”and age 26, Here “Virat” is of String data type and 26 is of integer data
type.
We can organize this data as a record like player record. Now we can collect and store
player’s records in a file or database as a data structure. For example:”Dhoni”30, “Gambhir”31,
“Sehwag”33.
In simple language, Data Structures are structures programmed to store ordered data, so that
various operations can be performed on it easily.
Basic types of Data Structures:
As we discussed above , anything that can store data can be called as a data structure,
hence Integer, Float;Boolean,Char etc, all data structures. They are known as Primitive Data
Structures.
 Linked List
 Tree
 Graph
 Stack , Queue etc.
All these data structures allow us to perform different operations on data. We select
these data structures based on which type of operation is required. We will look these data
structures in more details in our lessons.
Define a Linear and non linear data structures:
Linear Data Fashion- Linked list is an example of linear data storage or structure. Linked list
store data in an organized a linear fashion. They store data in form of a list.
Non Linear Data Fashion- Tree data structure is an example of a non linear data structure.
A tree has one node called as root node that is the starting point that holds data and links to other
nodes.
Define a linear and non linear data structure.

Linear data structure: A linear data structure traverses the data elements sequentially, in which
only one data element can directly be reached.

Ex:Arrays,Linked,List

Non-Linear data structure: Every data item is attached to several other data items in a
way that is specific for reflecting relationships. The data items are not arranged in a
sequential structure. Ex: Trees, Graphs

Definition of ADT:

 Abstract data type is a setup operation, which are mathematical abstraction.


 An ADT definition is their any mention of how the set of operation is implemented. These
operations are along with their objects such as list,sets and graphs can be viwed as a ADT.
The ADT data types are integer’s real and Boolean variables.
 A data type that is defined entirely by a set of operations is referred to as an ADT.
 These operations provide a representation independent specification.
 An ADT is a combination of interface and implementation.
 The interface defines the logical properties of ADT and edpecially the signature of its
operation.
 The implementation defines the representation of data structure and the algorithms that
implement the operations.

Operation of ADT:

 Creation
 Insertion
 Deletion
 Copy
 Modify
 Search

Advantages:

 It is very much easier to debugging the small routines to larger routines.


 It is very much easier for several peoples to work on a modular program
simultaneously.
 It is easy to modify and change the programs and large routines.

DATA STRUCTURES:
Introduction:
 A data structure is a data object together with the relationships that exist among the
instances and among the individual elements that compose an instance.
 In other words , a data structure defines a way of organizing all data items that consider
not only the item stored but also store the relationship between the elements.
 A data structure represents the logical relationship that exists between the individual
elements of data to carry out certain tasks. A data structure is an actual implementation of
an array.
 For example, an integer as a data structure can be defined as a integer data type stored on
consecutive memory bytes.
 An abstract data (ADT) is a data type in which the members of the data type are unknown
to users of the type.
 Abstraction refers to define new types,hiding the details of implementation.
 The above diagram shows the classification of data structures.
 Primary data structures are the basic data structures that directly operates upon the
machine instructions.
 They have different representation on different Computers.
 All the basic constants ( Integer, Floting Point Numbers, Character Constant, and String
Constant) and pointers are consider as primary data structure.
 They emphasize on grouping same or different data items with relationship between each
data item.
 Compound data Structure can be broadly classified into two types such as static and
dynamic data structures
If a data structure is created using the static memory allocation(data structure formed when
the number of data items are known in advance)it is known as static data structure or fixed
size data structure Example: Arrays, Structures.

Data

Primary Data Secondary Data

Static Data Dynamic Data

Linear Data Non-Linear Data

Linked Stack Queue Trees Graphs


 If a data structure is created using the dynamic memory allocation(data structure formed
when the number of data items are known in advance) it is known as dynamic data
structure or variable size data structure
 Dynamic data structure can be classified into two types such as linear and non linear data
structures.

Linear data structures:


 Linear data structures have the linear relationship between its adjacent elements.
 There are two methods by which we can construct a linear data structure. They are.
Method 1: By using a sequence of memory locations. These are implemented using
arrays.
Method 2: By using pointers or links. These are implemented using linked lists.
The different types of lineat data structures are:

1. Lists:
- The list can be implemented using arrays : these are called linear lists or
ordered lists.
- The list can be implemented using pointers : these are called linked lists.
2. Stacks:
- The stacks can be implemented using arrays.
- The stacks can be implemented using linked lists.
3. Queues:
- The queue can be implemented using arrays.
- The queue can be implemented using linked lists.
Non Linear Data Structures:
 Non linear data structures don’t have a linear relationship between its adjacent elements.
 Each node may point to several nodes. But in linear data structures, each node has a link
which points to another node.
Example: Tree, Graph
 A tree is a non linear data structure that may point to one or more nodes at a time.
 A graph is similar to tree except that it has no relationship between its adjacent elements.
LINEAR LISTS:
 A list is a ordered collection of elements of similar types.
 A linear list is a data object whose instances are of the from (e1,e2,…..en),where n is a
finite natural number.
e1  Element of the list.
n  length of the list.
s  size of the element
 For example : a list of names, list of marks, etc.
Operations of list ADT:
The following operations that are performed on a linear list are:
1. Create a list.
2. Delete a list.
3. Determine whether the list is empty.
4. Determine the length of the list.
5. Find the k th element
6. Search for a given element.
7. Delete the k th element.
8. Insert a new element.
9. Display all elements.
LINKED LIST:
A linked list is a collection of elements called nodes, each of which store two items
called info and link. Info is an element of the list and a link is a pointer to the next
element. The linked list is also called a chain.
The different types of linked lists are:
1. Singly linked list.
2. Doubly liked list.
3. Circularly linked list.

SINGLY LINKED LISTS:


A singly linked list is a linked list in which each node contains only one link pointing
to the next node in the list.

In singly linked list , we can traverse in only one direction.


NODE
HEAD

INFO LINK
In a singly linked list, the first node always pointed by a pointer called HEAD. If the link
of the node points to NULL, then that indicates the end of the list.

Operations of Singly Linked List:


The following operations that can be performed on a singly linked list are:
1. Count the number of elements.
2. Creating a new list.
3. Add an element at the beginning of the list.
4. Add an element at the end of the list.
5. Insert an element at the specified position in the list.
6. Delete an element fron the list.
7. Search X in the list.
8. Display all the elements of the list.
Implementation of list ADT:
The list can be implemented in two ways such as,
 Array implementation of lists
 Linked lists implementation of lists.
Array Implementation of list ADT: The list can be implementation by using array.
Initially, the size of the list is fixed. so that we cannot change the size of the list during
the execution time.
Basic Operations:
1. Creating a new list
2. Inserting a new element into the list
3. Deleting the element from the list.
4. Display the contents of the list.
5. Searching an element from the list.
6. Counting the no of elements of the list.

Algorithm for Basic operations: to write a logics in a notes


Creating a new list:
Inserting a new element at the beginning position of the list:
Inserting a new element at the last position of the list:
Deleting a first element from the list:
Deleting the last element from the list:
Display the contents of the list:
Count the no of elements in the list:
Searching an element from the list:
Linked List Implementation of List ADT:
 Linked list is the most commonly used data structure used to store similar type of data in
memory.
 The element of a linked list are not stored in adjacent memory locations as in arrays.
 It is a linear collection of data elements called nodes, where linear order is implemented
by means of pointers.
 A linked list allocates memory for storing elements and connects element together using
pointers.
 In a linear or single linked list a node is connected to the next node by a single link.
 A node in this type of linked list contains two types of fields.
Data: which holds a list elements.
Next: which stores a link to the next node in the list

Basic Operations:
1. Creating a new list
2. Inserting an element into the list
3. Deleting an element from the list
4. Searching an element from the list
5. Display the contents of the list
6. Counting the no of elements of the list
Algorithm for Basic Operations: Creating a new list:
The creation() is used for creating a new list.
Algorithm:
Inserting a new element into the beginning position of the list:
The insert () operation is used for adding a new element into the list. The
insertion process can be performed at anywhere in the list (Beginning ,last and middle
position of the list
Inserting a new element into the beginning position of the list:
to draw the diagram
Deleting an element from the beginning position of the list:
This operation is used for removing the first element from the list.
Algorithm

Deleting an element from the last position of the list:


to draw the diagram

CURSOR IMPLEMENTATION OF LINKED LISTS


 Suppose we don’t have pointer in c
 If we want to still implement linked list in create an array of structures, which contains
collection of nodes.
 Each array of structures holds a data element and a link (an integer value) which acts as
the pointer to the next element.
 The content to be stored in the next link is the index of the next node.
 The advantage of using linked is to dynamically allocate the memory for the nodes when
needed.
 This is achieved in this case using array of structures, but since the size of the array of
structures is fixed, this type of lists is referred to as static linked lists of reffered to as
cursor implementation of linked lists.
 Since the index values of array structures are used in implementation of static linked list
accessing is fast compared to pointer in dynamic linked list.
 Cursor in the implementation of static linked list means an integer, which indicate the
index position in the array.
 In the cursor list every node consists of two fields such as data field and link field. the
link field is the one which points to the next node.
 The content to be stored in the link field is the index of the next node.
 The array of structures has to be initialized before storing the data,by assigning the link
value of each node by the index of the immediate next node.
 In the cursor list array, the node with the index 0 is used as the header.
 The value 0 in the link field indicates the end of the list(similar to NULL in a dynamic
linked lists).
INDEX DATA LINK
0 1
1 2
2 3
3 4
4 0

CURSOR ALLOCATION IN STATIC LINKED LIST:


Steps to be followed to allocate a cursor.
 To store data for the data field, memory has to be allocated which is always
started from the first node of the cursor list.
 Get the free node from the link of the first node of the cursor list.
 Assign the link of the new node to the link of the first node.
 Assign the link of the new node to zero.
 The next free node is found from the link of the first node of the cursor list.
Cursor De allocation in Static Linked Lists:
steps to be followed to de allocate a cursor in static linked lists
 To remove a node from the list ,memory has to be de allocated.
 Assign the link of the removed node with the link of the first node.
 Assign the link of the first node with the index of the node to be removed.
DOUBLY LINKED LISTS
 The Doubly linked list is a collection of nodes each of which consists of three parts
namely the data part, prev pointer and the next pointer.
 The data part stores the value of the element, the prev pointer has the address of the
previous node and the next pointer has the value of the next node.

NODE

PREV DATA NEXT

 I n a doubly linked list, the head always points to the first node. the prev pointer of the
first node points to NULL and the next pointer of the last node points to NULL.
Operations of Doubly Linked Lists:
The following operations that can be performed on a doubly linked list are,
1. Count the number of elements.
2. Add an element at the beginning of the list.
3. Add an element at the end of the list.
4. Insert an element at the specified position in the list
5. Delete an element from the list.
6. Display all the elements of the list.

Algorithm for basic operations:


Creating a new list by using linked list:
Inserting a new element at the beginning position of the list:
Inserting a new element at the middle position of the list:

Deleting an element from the last position of the list:

Algorithm Deletelast()

Deleting an element from the middle position of the list:


Algorithm Deletemiddle(Head)

To find the no of elements of the list:’


CIRCULARLY LINKED LISTS
 Circular linked list is a linked list which consists of collection of nodes each of which has
two parts, namely the data part and the link part.
 The data part holds the value of the element and the link part has the address of the next
node.
 The last node of list has the link pointing to the first node thus making the circular
traversal possible in the list.
Logical representation of the circularly linked list is:

45
4
5
4
5

APPLICATIONS OF LISTS
 Linked lists from the basis of many data structures.
 Some important application using linked lists are,
 Polynomial Manipulation
 Stack
 Queue

Polynomial Manipulation
 Linked list is generally used to represent and manipulate polynomials.
 Polynomials are expressions containing terms with non zero coefficients and exponents
 For example
P(x)=a0xn+a1xn-1+…………an-1+an
Where
P(X) is a polynomial in X
a0,a1….,an-1,an are constants and n is a positive integer.
 In the linked list representation of polynomials each term / element in the list is referred
as a node.
 Each node containes three fields namely,

 Coefficient Field
 Exponent Field
 Link Field

Co-Efficient Field Exponent Field Link Field


Representation Of Polynomial Node
 The Coefficient and exponent fields store the data of a polynomial.
 the coefficient fields holds the value of the coefficient of a polynomial. The exponent
fields holds the exponent value of the polynomial.
 The link field contains the address of the next term in the polynomial.
 Creating a polynomial using a singly linked list starts with creating a node.
 Sufficient memory has allocated for creating a node.
 The information is stored in the memory allocated by using malloc().
 The getnode () is used for creating a node.
 After allocating memory for the structure of type poly the information for the terms (coef
and expo)has to be read from the users.
 After,we can do all the operations such as insertions and deletions in the polynomial
lists.
UNIT-II
STACK ADT:
 A stack is an ordered collection of items accessed as last-in-first-out order.
 A stack is a list of elements in which insertions and deletions are restricted to one end.
 The end from which the elements are added / removed is referred to as top of the stack.
 Stack is also referred as piles and push down lists.
 The first element placed in the stack will be at the top of the stack. The last element
placed in the stack will be at the top of the stack. The last element added to stack is the
first element to be removed.
 Hence the stacksare referred to as Last-in-first-out-lists.
 A stack is referenced via a pointer to the top elements of the stack referred as top pointer.
 The top pointer keeps track of the top elements in the stack.
 Initially, when the stack is empty, the top pointer has a value zero and when the stack
contains a singly element the top pointer has a value one and so on.

Example:
 Bangles in a lady hand
 Coaches of train
 Plies of notebook
Stacks: A stack is a data structures in which addition of new element or deletion of an existing
elements always takes place at the same end. This end is often known as top of stack. when item
is added to a stack, the operation is called push ,and when an item is removed from the stack the
operation is called pop. Stack is also called as Last-In-First-Out(LIFO)list.
Basic Operations of Stack ADT:

The primitive operations of the stack include PUSH and POP.


PUSH: Allows adding an element at the top of the stack.
A push () operation adds (or) inserts a new element to the stack.
Each time a new element is inserted in the stack, the top pointer is
incremented by one before the element is placed on the stack.’
The general syntax for inserting a new element into the stack.
Push(S,X).
The element ‘x’ is inserted into the stack’s’.
POP: Allows removing an element from the top of the stack.
A pop () operation deleted the top most elements in the stack. Each time, an
element is removed from the stack, the top pointer is decremented by one.
The general syntax for removing an element from the stack.
Pop(S);
Used to delete the top element from the stack.
PEEK: Allows displaying top element of the stack.
A peek () is an operation used to display the element from the top of the
Stack, pointed by the top pointer. The general syntax is used to return the Element
at the top of the stack.
Top(S),
Implementation of the Stack ADT:
A Stack can be implemented in two ways.
 Array Implementation
 Linked list Implementation
Array Implementation of Stack ADT:
 An array is a place holder to store the elements of the stack.
 The size of an array should be fixed.
 An integer variable top can be used to mark the of the stack.
 The notation top=-1 is used for initializing the top variable.
 Top can be made to point to the top element of the stack.
 when a new element is to be pushed then the top can be incremented and the element can
be added.
 In this notation top will be-1 for an empty stack.

Push Operation:
 If the elements are added continuously to stack using the push operation then the stack
grows at one end.
 Initially when the stack is empty the top=-1.The top is a variable which indicates the
position of the topmost element in the stack.

Empty Stack Push(50) Push(40) Push(90)

50 90
40 40
50 50

Algorithm:
AlgorithmPush (S,N,Top,X)
//S->Name of the array,
//N->Size of the array,
//X->New element
Begin
if (Top==N-1)then
Write “Stack overflow”
else
Top<-Top+1
S[Top]<-X
end if
End
Pop Operation:
On deletion of elements the stack shrinks at same end,as the elements at the top get
removed.

pop() pop() pop()

T 40 Top=0
o 50 50 top=-1
p
=
1
Algorithm:
Algorithm Pop(S,Top)
Begin
if(Top1)then
Write “Stack Underflow”
else
X<-S[Top];
Top<-Top-1;
end if
End
 If arrays are used for implementing the stacks, it would be very easy to manage the
stacks.
 However, the problem with an array is that we are required to declare the size of the array
before using it in a program.
 This means the size of the stack of the stack should be fixed. We can decare the array
with a maximum size large enough to manage a stack.
 As result , the stack can grow or shrink within the space reserved for it. The following
program implements the stack using array.

Implementation of Stack ADT using Linked Lists:


 The stack can be implemented by using the singly linked list.
 This type of the representation has more advantage than representing stack using array.
 They are
 It is not necessary to specify the no of elements to be stored in a
stack during its declaration.(memory is allocated dynamically at
run time when an element is added to the stack).
 Insertion and deletion can be handled easily and efficiently.
 Linked list representation of stack can grow and shrink in size
without wasting the memory space depending upon the insertion and deletion that
occurs in the list.
 Initially , when the stack is empty, top points to NULL. when an element is added
using the push operation, top is made to point to the latest element whichever is
added.
Push operation:
 Create a temporary node and store the value of x in the data part of the node.
 Now make link part of temp point to Top and point to Temp.
That will make the new node as the topmost element in the stack.

Algorithm:
Algorithm push()
Begin
if (isfull()==1)
return-1
else
Newptr=Getnode();
Newptr->Data=Value;
Newptr->Next=Topstk;
Topstk<-Newptr
End.

Pop Operation:
The data in the topmost node of the stack is first stored in a variable called item.
Then a temporary pointer is created to point to top. The top is now safely moved to the
next node below it in the stack.
Temp node is deleted and the item is returned.

Algorithm
Algorithm Pop()
Begin
if (isempty()==1)
return-1
else
temp<-Topstk
Topstk<-Topstk->Next
Value<-Temp->data
Releasenode(temp);
End
Note:
The different between the stack and linked list is that insertion and deletion may occur
anywhere in a linked list,but only at the top node in a stack.
APPLICATION OF STACK ADT:
Some of the application of the stack includes
 Towers of Hanoi
 Reversing the String
 Recursion using stack
 Evaluation of Arithmetic Expressions

Towers of Hanoi:
 Towers of Hanoi is a gaming puzzle
 Invented by French mathematician Edouard Lucas in1883.
 The objective of this puzzle is to transfer the entire disks from Tower1 to Tower3 using
Tower2.
 The rules to be followed in moving the disks from tower1 to tower3 using tower2.
 Only one disc can be moved at a time.
 Only the top disc on any tower can be moved to any other tower.
 A large disc cannot be placed on a smaller disc.
 It can be implemented using recursion. To move the largest disc to the bottom of tower3.
 We move the remaining n-1 disks to tower2 and then move the largest disc to tower3.
 This process is continue until to place the entire disc in tower3 in order.
 Since disc are moved from from each tower in a LIFO manner each tower may be
considered as stack.
 The least no of moves required to solve the problem according to our algorithm is given
by O(N)=2N-1.
 The time complexity is measured in no of movements.

Reversing the String:


 Main characteristics of the stack are reversing the order of its contents.
 The tasks can be accomplished by pushing each character until the end of the string.
 Now, the individual characters are popped off from the stack.
 Since the last character pushed into the stack would be the first character to be popped
off from the stack.
 The string will come off in the reverse order.
 Since the individual character are moved from the stack in a LIFO manner, the stack
operation is implemented.
 For Example.,
The input string is Kumar. The output string is ramuk.

Recursion Using Stack:


 Recursion is a process by which a function callsityself repeatedly until some specified
condition has been satisfied.
 When a recursive program is executed the recursive function calls are not executed
immediately.
 They are placed on a stack (LIFO)until the condition that terminates the recursive
function.
 The function calls are then executed in reverse order, as they are popped off the stack

Evaluation of Arithmetic Expression:


 An expression consists of two components namely Operands and Operators.
 operators indicate the operation to be carried out operands.
 operands are the variable and constants.
 There are three ways of representing expression in computers.they are
 infix notation
 Prefix Notation
 Postfix Notation
Infix Notation:
 The normal way of representing mathematical expression is called as infix expression.
 In this form of expressing an arithmetic expression the operator comes in between its
operands.
for Example, (a+b)
The operator ’+’ is written in between the operands “a” and “b”

Advantages:
 It is the mathematical way of representing the expression.
 It’s easier to see visually which operation is done from the first to last.
Prefix Notation:
 Also referred as polish notation.
 It is a way of representing algebraic expression without the use of parenthesis (or) rules
of operator precedence.
 In this form of expressing an arithmetic expression the operator is written before its
operands.
 For example, (+ab)
The operator ‘+’ is written before the operands “a” and “b”.

Postfix Notation:
 Also referred as suffix notation (or)reverse polish notation.
 In this form of expressing an arithmetic expression the operator is written after its
operands.
 For example, (ab+)
The operator ‘+’ is written after the operands”a” and “b”.
Conversion of Notation:
The infix expression is ,
a+b*c+(d*e+f)*g

INFIX STRING STACK POSTFIX STRING


a+b*c+(d*e+f)*g a
+b*c+(d*e+f)*g + a
b*c+(d*e+f)*g + ab
*c+(d*e+f)*g + ab
c+(d*e+f)*g +* abc
+(d*e+f)*g + abc*+
(d*e+f)*g +( abc*+
d*e+f)*g +( abc*+d
*e+f)*g +(* abc*+d
e+f)*g +(* abc*+de
f)*g +(+ abc*+de*f
)*g +(+ abc*+de*f+
*g +* abc*+de*f+
G +* abc*+de*f+g*+_

 Once, the expression is converted into postfix from remove all the parenthesis.
 For example,
The infix expression is,
3+8*4/2-(8-3)
3+8*4/2-83-
3+84*/2-83-
3+84*2/-83-
384*2/+-83-
384*2/+-83—
The postfix expression is,
384*2/+83—

Rules to be followed during infix to prefix conversion:


 Fully, Parenthesize the expression starting from left to right. (During parenthesizing the
operators having higher precedence are the first parenthesized).
 Move the operators one by one to their right such that each operator replaces their
corresponding right parenthesis.
 The part of the expression, which has been converted into postfix, is to be treated as
single operand.
 Once, the expression is converted into postfix from remove all the parenthesis.
3+8*4/2-(8-3)
3+8*4/2--83
3+*84/2--83
3+/*842--83
+3/*842--83
-+3/*842-83
The Prefix expression is,
-+3/*842-83
Evaluation of postfix Expression Using a Stack
Rules for valuating a postfix expression using stack.
 Traverse from left to right of the expression.
 If an operand is encountered push it onto the stack.
 If an operator is encountered pop two elements from the stack evaluate
those operands with that operator and push the result back in the stack.
 When the evaluation of the entire expression is over, the only thing left on
the stack should be the final result.
 If there are zero (or) more than one operands left on the stack either your
program is inconsistent (or) the expression was invalid.
Example:
The postfix expression is

384*2/+83- -
QUEUES
QUEUE
 A queue is a linear data structure.
 a queue is an ordered collection of elements in which are accessed in a first-in-first-out
(FIFO) order.
 In a queue, the element which are inserted at one end deletion are made at another end.
 The end at which the insertions are made is referred as the rear end.
 The end at which the insertions are made is referred as the front end.
 In a queue, the first element inserted will be the first element to be removed.So a queue,
the first element inserted will be the element to be removed. So a queue is referred to as
FIFO List.(First-in-First-out List).

Deletion Insertio n

Rear
Front

Example:
 A Reservation Counter
 Jobs in a printer
 A queue of ready jobs waiting for the processor.

Basic Operations of Queue ADT:


the basic operations that can be done on a queue are
 Enqueue() or insertion()
 Dequeue() or deletion()

Enqueue():
 An enqueue () operation adds a new element in a queue.
 This process is carried out by incrementing the rear end and adding a new element at the
rear end position.
 The syntax for inserting a new element into a queue.
Enqueue (Q,X) or Insert(Q,X)
The element ‘X’ inserted into a queue ‘Q’.
Dequeue():
 A dequeue() operation removes the first element from the queue.
 A dequeue() operation is carried out by incrementing the front end and deleting the first
element at the end position.
 The sytax for removing a first elementfrom the queue.
 Dequeue(Q)(or) Delete(Q)
here, the first element is removed from the queue.
Types of Queue ADT:
There are different types of queue
 Linear Queue
 Circular Queue
 Deque

Linear Queue:
 A queue is referred to as the linear queue.
 The queue has two ends such as front end and rear end.
 The rear end is where we insert the elements and the front end is where we delete the
elements.
 In a linear queue, we can traverse in only one direction.
 In a linear queue if front is in first position, and the rear pointer is in the last position
then the queue is said to be fully occupied.
 Initially, the front and rear ends are at the same position(Initialized to -1)
 When we insert the elements, the rear pointer moves one by one until the last index
position is reached.(the front pointer doesn’t change.)
 When we delete the elements, the front pointer moves one by one until the rear pointer
is reached.(the rear pointer doesn’t change.)
 If the front and rear pointer positions are initialized to -1,then the queue is said to be
empty.

Circular Queue:
 Circular Queue is another form of a linear queue in which the last position is
connected to the first position of the list.
 It is similar to linear queue has two ends such as front and rear ends.
 The rear end is where we insert the element and front end is where we delete the
elements.
 In a circular queue we can traverse in only one direction.
 Initially, Front and rear ends are at the same position.
 when we insert an element , the rear pointer moves one by one until the front end is
reached.(front end doesn’t change.)
 If the next position of the rear is front, then the queue is said to be fully occupied.
 when we delete an element, the front pointer moves one by one until the rear end is
reached.
 If the front end reaches the rear end, then the queue is said to be empty.

10 20
0 78
0

queue=-1 0 1
0 9
6 0

2 a circular array

Representation of Circular Queue

Dequeue:
 Deque means double – Ended Queue.
 It is another form of a queue in which insertion and deletions are made at the both
front and ends of the queue.
 There are two types of dequeue.
 input restricted Dequeue
 output Restricted Dequeue
Input Restricted Deque:
The input restricted deque allows insertions at one end (it can be either front (or) Rear)
Output Restricted Deque:
The output restricted deque allows deletions at one end(it can be either front (or)
Front End
rear)
Insertion
Insertion
Deletion
Deletion
Rear End
Representation of Dequeue

IMPLEMENTATION OF QUEUE ADT:


 A queue can be implemented in two ways .
 Array Implementation of Linear Queue.
 Linked list Implementation Of linear Queue.
ARRAY IMPLEMENTATION OF QUEUE
 Queue and Arrays are ordered collection of elements.
 The number of elements in the array is fixed. But the size of queue is constantly
changed when the elements are enqueued and dequeued.
 The queue is stored in a part of the array, so an array can be declared large
enough to hold the maximum no of elements of the queue.
 During execution of the program, the queue size can be varied within the space
reserved for it.
 All the basic operations performed on the queue by using array.
 The basic operations are,
 Creation
 Enqueue (or) Insertion
 Dequeue (or) Deletion
Operations on a Queue:
 There are two common operations in a queue.
 They are addition of an element to the queue and deletion of an element from the queue
 Two variable front and rear are to point to the ends of the queue.
 The front points to the front end of the queue where deletion takes place ad rear points to
the rear end of the queue, where the addition of elements takes place.
 Initially when the queue is empty, the front and rear is equal to-1.
Creation():

 Creation of a queue requires the declaration of an array and initializing front and rear
wend indicates to -1 respectively.’
Algorithm:
Algorithm CreateQ(Queue Q)
Begin
Q.front1
Q.rear1
End.
Enqueue(X): insertion()
 An element can be added to the only at rear end of the queue.
 Before adding an element in the queue, it is checked whether queue is full.
 If the queue is full, then addition cannot take place. Otherwise, the element is added
to the end of the list at the rear side.

34 3 78 90 56 4 78 90 56 87

f rear front rear


r
Algorithm:
o
n
t
Dequeue(): deletion()
 The dequeue () operation deletes the element from the front of the queue
 Before deleting and element, it is checked if the queue is empty. If not the element
pointed by front is deleted from the queue and front is now made to point to the next
element in the queue.

34 78 90 587 78 50 56 87
6

front rearear front rear

LINKED LIST IMPLEMENTATION OF QUEUE:


 Queue can be represented using a linked list.
 Linked lists do not have any restrictions on the number of elements it can hold.
 Space for the elements in a linked list is allocated dynamically; hence it can grow as long
as there is enough memory available for dynamic allocation.
 The queue represented using limked list would be represented as shown. The front
pointer to the front of the queue and rear pointer to the rear of the queue.

89 499 20 10 N

front rear

 This representation has more advantages than representing queue using arrays.
 The advantages are,
 It is not necessary to specify the no of elements to be stored in a queue
during its declarations
 Insertions and deletions can be handled easily and efficiently.
 Linked list representation of queue can grow and shrink in size without
wasting the memory space depending upon the insertion and deletion that
occurs in the list.
Basic Operations:
The basic operations can be performed on the queue are,
 Enqueue() or insertion()
 Dequeue() or deletion()
Algorithm for Basic Operations:
Enqueue Operation:
In linked list representation of queue, the addition of new element to the queue takes place
at the rear end. It is the normal operation of adding a node at the end of a list.

89 499 20 10 N

front rear

Algorithm:
Dequeue Operation:
The dequeue () operation deletes the first element from the end front end of the
queue. Initially it is checked, if the queue is empty. If it is not empty, then return the
value in the node pointed by front, and moves the front pointer to the next node.
Algorithm:

CIRCULAR QUEUE IMPLEMETATION:


 The circular queue can be representation by using array.
 The size of the circular queue is fixed;we cannot the change the size of the queue during
the execution time.
 The basic operations of the circular queue are enqueue() and dequeue().
Algorithm for Basic Operations:
Enqueue():
The enqueue ()operation is used for adding a new element into the circular queue.
Algorithm:

Dequeue():
The dequeue() operation is used for removing the first element from the circular queue.
Algorithm:

APPLICATION OF QUEUE ADT:


The application of the queue such as,
1. Priority Queue
2. Scheduling Algorithms
Priority Queue
 A Priority queue is a collection of element in which the elements are added to the end and
the high priority element is deleted.
 In a priority queue, each and every element containing the key referred as the priorityfor
the elements.
 The operations performed in a queue are similar to the queue except that the insertion and
deletion element made in it.
 Elements can be inserted in any order, but are arranged in order of their priority value in
the queue.
 The elements are deleted from the queue in the order of their priority.
 The elements with the same priority are given importance and processed accordingly.
 The priority queue can be implemented in the following ways.
 ordered List Implementation of Priority Queue
 Heap Implementation of Priority Queue
Applications of Priority Queue:
1. Modeling of systems.Where the keys might correspond to event times, to be processed
in chronological order.
2. CPU scheduling in computer system, when the keys might correspond to priorities
indicating which processes are to be served first.
3. Numerical computations, where the keys might be computational errors indicating that
the largest should be dealt with first.
Trees
Tree represents the nodes connected by edges. We will discuss binary tree or binary search tree
specifically.

Binary Tree is a special data structure used for data storage purposes. A binary tree has a special
condition that each node can have a maximum of two children. A binary tree has the benefits of
both an ordered array and a linked list as search is as quick as in a sorted array and insertion or
deletion operation are as fast as in linked list.

Important Terms
Following are the important terms with respect to tree.

 Path − Path refers to the sequence of nodes along the edges of a tree.

 Root − The node at the top of the tree is called root. There is only one root per tree and
one path from the root node to any node.

 Parent − Any node except the root node has one edge upward to a node called parent.
 Child − The node below a given node connected by its edge downward is called its child
node.

 Leaf − The node which does not have any child node is called the leaf node.

 Subtree − Subtree represents the descendants of a node.

 Visiting − Visiting refers to checking the value of a node when control is on the node.

 Traversing − Traversing means passing through nodes in a specific order.

 Levels − Level of a node represents the generation of a node. If the root node is at level
0, then its next child node is at level 1, its grandchild is at level 2, and so on.

 keys − Key represents a value of a node based on which a search operation is to be


carried out for a node.

Binary Search Tree Representation


Binary Search tree exhibits a special behavior. A node's left child must have a value less than its
parent's value and the node's right child must have a value greater than its parent value.

We're going to implement tree using node object and connecting them through references.

Tree Node
The code to write a tree node would be similar to what is given below. It has a data part and
references to its left and right child nodes.

struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};

In a tree, all nodes share common construct.

BST Basic Operations


The basic operations that can be performed on a binary search tree data structure, are the
following −
 Insert − Inserts an element in a tree/create a tree.

 Search − Searches an element in a tree.

 Preorder Traversal − Traverses a tree in a pre-order manner.

 Inorder Traversal − Traverses a tree in an in-order manner.

 Postorder Traversal − Traverses a tree in a post-order manner.

We shall learn creating (inserting into) a tree structure and searching a data item in a tree in this
chapter. We shall learn about tree traversing methods in the coming chapter.

Insert Operation
The very first insertion creates the tree. Afterwards, whenever an element is to be inserted, first
locate its proper location. Start searching from the root node, then if the data is less than the key
value, search for the empty location in the left subtree and insert the data. Otherwise, search for
the empty location in the right subtree and insert the data.

Algorithm
If root is NULL
then create root node
return

If root exists then


compare the data with node.data

while until insertion position is located

If data is greater than node.data


goto right subtree
else
goto left subtree

endwhile
insert data

end If

Implementation
The implementation of insert function should look like this −

void insert(int data) {


struct node *tempNode = (struct node*) malloc(sizeof(struct node));
struct node *current;
struct node *parent;

tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;

//if tree is empty, create root node


if(root == NULL) {
root = tempNode;
} else {
current = root;
parent = NULL;

while(1) {
parent = current;

//go to left of the tree


if(data < parent->data) {
current = current->leftChild;
//insert to the left
if(current == NULL) {
parent->leftChild = tempNode;
return;
}
}

//go to right of the tree


else {
current = current->rightChild;

//insert to the right


if(current == NULL) {
parent->rightChild = tempNode;
return;
}
}
}
}
}

Search Operation
Whenever an element is to be searched, start searching from the root node, then if the data is
less than the key value, search for the element in the left subtree. Otherwise, search for the
element in the right subtree. Follow the same algorithm for each node.

Algorithm
If root.data is equal to search.data
return root
else
while data not found
If data is greater than node.data
goto right subtree
else
goto left subtree

If data found
return node

endwhile

return data not found

end if

The implementation of this algorithm should look like this.

struct node* search(int data) {


struct node *current = root;
printf("Visiting elements: ");

while(current->data != data) {
if(current != NULL)
printf("%d ",current->data);

//go to left tree

if(current->data > data) {


current = current->leftChild;
}
//else go to right tree
else {
current = current->rightChild;
}

//not found
if(current == NULL) {
return NULL;
}

return current;
}
}

To know about the implementation of binary search tree data structure, please click here.

 Tree Traversal
Traversal is a process to visit all the nodes of a tree and may print their values too. Because, all
nodes are connected via edges (links) we always start from the root (head) node. That is, we
cannot randomly access a node in a tree. There are three ways which we use to traverse a tree −

 In-order Traversal
 Pre-order Traversal
 Post-order Traversal

Generally, we traverse a tree to search or locate a given item or key in the tree or to print all the
values it contains.

In-order Traversal
In this traversal method, the left subtree is visited first, then the root and later the right sub-tree.
We should always remember that every node may represent a subtree itself.

If a binary tree is traversed in-order, the output will produce sorted key values in an ascending
order.
We start from A, and following in-order traversal, we move to its left subtree B. B is also
traversed in-order. The process goes on until all the nodes are visited. The output of inorder
traversal of this tree will be −

D→B→E→A→F→C→G

Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.

Pre-order Traversal
In this traversal method, the root node is visited first, then the left subtree and finally the right
subtree.
We start from A, and following pre-order traversal, we first visit Aitself and then move to its
left subtree B. B is also traversed pre-order. The process goes on until all the nodes are visited.
The output of pre-order traversal of this tree will be −

A→B→D→E→C→F→G

Algorithm
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.

Post-order Traversal
In this traversal method, the root node is visited last, hence the name. First we traverse the left
subtree, then the right subtree and finally the root node.
We start from A, and following pre-order traversal, we first visit the left subtree B. B is also
traversed post-order. The process goes on until all the nodes are visited. The output of post-
order traversal of this tree will be −

D→E→B→F→G→C→A

Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.
Binary Search Tree
A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned
properties −

 The left sub-tree of a node has a key less than or equal to its parent node's key.

 The right sub-tree of a node has a key greater than or equal to its parent node's key.

Thus, BST divides all its sub-trees into two segments; the left sub-tree and the right sub-tree
and can be defined as −

left_subtree (keys) ≤ node (key) ≤ right_subtree (keys)

Representation
BST is a collection of nodes arranged in a way where they maintain BST properties. Each
node has a key and an associated value. While searching, the desired key is compared to the
keys in BST and if found, the associated value is retrieved.

Following is a pictorial representation of BST –

We observe that the root node key (27) has all less-valued keys on the left sub-tree and
the higher valued keys on the right sub-tree.
Basic Operations
Following are the basic operations of a tree −

 Search − Searches an element in a tree.

 Insert − Inserts an element in a tree.

 Pre-order Traversal − Traverses a tree in a pre-order manner.

 In-order Traversal − Traverses a tree in an in-order manner.

 Post-order Traversal − Traverses a tree in a post-order manner.


Node
Define a node having some data, references to its left and right child nodes.

struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};

Search Operation
Whenever an element is to be searched, start searching from the root node. Then if the data
is less than the key value, search for the element in the left subtree. Otherwise, search for the
element in the right subtree. Follow the same algorithm for each node.

Algorithm
struct node* search(int data){
struct node *current = root;
printf("Visiting elements: ");

while(current->data != data){

if(current != NULL) {
printf("%d ",current->data);

//go to left tree

if(current->data > data){


current = current->leftChild;
}//else go to right tree
else {
current = current->rightChild;
}

//not found
if(current == NULL){
return NULL;
}
}
}
return current;
}

Insert Operation
Whenever an element is to be inserted, first locate its proper location. Start searching from
the root node, then if the data is less than the key value, search for the empty location in the
left subtree and insert the data. Otherwise, search for the empty location in the right subtree
and insert the data.

Algorithm
void insert(int data) {
struct node *tempNode = (struct node*) malloc(sizeof(struct node));
struct node *current;
struct node *parent;

tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;

//if tree is empty


if(root == NULL) {
root = tempNode;
} else {
current = root;
parent = NULL;

while(1) {
parent = current;

//go to left of the tree


if(data < parent->data) {
current = current->leftChild;
//insert to the left

if(current == NULL) {
parent->leftChild = tempNode;
return;
}
}//go to right of the tree
else {
current = current->rightChild;

//insert to the right


if(current == NULL) {
parent->rightChild = tempNode;
return;
}}}}
}
Example

Construct a Binary Search Tree by inserting the following sequence of numbers...

10,12,5,4,20,8,7,15 and 13

Above elements are inserted into a Binary Search Tree as follows...


Threaded Binary Tree
A binary tree is represented using array representation or linked list representation. When a
binary tree is represented using linked list representation, if any node is not having a child we
use NULL pointer in that position. In any binary tree linked list representation, there are more
number of NULL pointer than actual pointers. Generally, in any binary tree linked list
representation, if there are 2N number of reference fields, then N+1 number of reference
fields are filled with NULL ( N+1 are NULL out of 2N ). This NULL pointer does not play
any role except indicating there is no link (no child).A. J. Perlis and C. Thornton have
proposed new binary tree called "Threaded Binary Tree", which make use of NULL pointer
to improve its traversal processes. In threaded binary tree, NULL pointers are replaced by
references to other nodes in the tree, called threads.

Threaded Binary Tree is also a binary tree in which all left child pointers that are
NULL (in Linked list representation) points to its in-order predecessor, and all right
child pointers that are NULL (in Linked list representation) points to its in-order
successor.

If there is no in-order predecessor or in-order successor, then it point to root node.

Consider the following binary tree...

To convert above binary tree into threaded binary tree, first find the in-order traversal of that
tree...

In-order traversal of above binary tree...


H-D-I-B-E-A-F-J-C-G

When we represent above binary tree using linked list representation, nodes H, I, E, F,
J and G left child pointers are NULL. This NULL is replaced by address of its in-order
predecessor, respectively (I to D, E to B, F to A, J to F and G to C), but here the node H does
not have its in-order predecessor, so it points to the root node A. And nodes H, I, E,
J and G right child pointers are NULL. This NULL ponters are replaced by address of its in-
order successor, respectively (H to D, I to B, E to A, and J to C), but here the node G does not
have its in-order successor, so it points to the root node A.

Above example binary tree become as follows after converting into threaded binary tree.

In above figure threaded are indicated with dotted links.


Heap Data Structure
Heap data structure is a specialized binary tree based data structure. Heap is a binary tree
with special characteristics. In a heap data structure, nodes are arranged based on their value.
A heap data structure, some time called as Binary Heap.

There are two types of heap data structures and they are as follows...

1. Max Heap
2. Min Heap

Every heap data structure has the following properties...

Property #1 (Ordering): Nodes must be arranged in a order according to values based on


Max heap or Min heap.

Property #2 (Structural): All levels in a heap must full, except last level and nodes must be
filled from left to right strictly.

Max heap data structure is a specialized full binary tree data structure except last leaf node
can be alone. In a max heap nodes are arranged based on node value.

Max heap is defined as follows...

Max heap is a specialized full binary tree in which every parent node contains greater
or equal value than its child nodes. And last leaf node can be alone.
Example

Above tree is satisfying both Ordering property and Structural property according to the Max
Heap data structure.
Operations on Max Heap

The following operations are performed on a Max heap data structure...

1. Finding Maximum
2. Insertion
3. Deletion

Finding Maximum Value Operation in Max Heap

Finding the node which has maximum value in a max heap is very simple. In max heap, the
root node has the maximum value than all other nodes in the max heap. So, directly we can
display root node value as maximum value in max heap.

Insertion Operation in Max Heap

Insertion Operation in max heap is performed as follows...

 Step 1: Insert the newNode as last leaf from left to right.


 Step 2: Compare newNode value with its Parent node.
 Step 3: If newNode value is greater than its parent, then swap both of them.
 Step 4: Repeat step 2 and step 3 until newNode value is less than its parent nede (or)
newNode reached to root.

Example
Consider the above max heap. Insert a new node with value 85.

 Step 1: Insert the newNode with value 85 as last leaf from left to right. That means
newNode is added as a right child of node with value 75. After adding max heap is as
follows...
 Step 2: Compare newNode value (85) with its Parent node value (75). That
means 85 > 75

 Step 3: Here newNode value (85) is greater than its parent value (75),
then swap both of them. After wsapping, max heap is as follows...
 Step 4: Now, again compare newNode value (85) with its parent nede value (89).

Here, newNode value (85) is smaller than its parent node value (89). So, we stop
insertion process. Finally, max heap after insetion of a new node with value 85 is as
follows...
Deletion Operation in Max Heap

In a max heap, deleting last node is very simple as it is not disturbing max heap properties.

Deleting root node from a max heap is title difficult as it disturbing the max heap properties.
We use the following steps to delete root node from a max heap...

 Step 1: Swap the root node with last node in max heap
 Step 2: Delete last node.
 Step 3: Now, compare root value with its left child value.
 Step 4: If root value is smaller than its left child, then compare left child with
its right sibling. Else goto Step 6
 Step 5: If left child value is larger than its right sibling, then swap root with left
child. otherwise swap root with its right child.
 Step 6: If root value is larger than its left child, then compare root value with
its right childvalue.
 Step 7: If root value is smaller than its right child, then swap root with rith child.
otherwisestop the process.
 Step 8: Repeat the same until root node is fixed at its exact position.

Example
Consider the above max heap. Delete root node (90) from the max heap.
 Step 1: Swap the root node (90) with last node 75 in max heap After swapping
max heap is as follows...

 Step 2: Delete last node. Here node with value 90. After deleting node with value
90 from heap, max heap is as follows...

 Step 3: Compare root node (75) with its left child (89).
Here, root value (75) is smaller than its left child value (89). So, compare left child
(89) with its right sibling (70).

 Step 4: Here, left child value (89) is larger than its right sibling (70), So, swap
root (75) withleft child (89).
 Step 5: Now, again compare 75 with its left child (36).

Here, node with value 75 is larger than its left child. So, we compare node with
value 75 is compared with its right child 85.
 Step 6: Here, node with value 75 is smaller than its right child (85). So, we swap
both of them. After swapping max heap is as follows...

 Step 7: Now, compare node with value 75 with its left child (15).
Here, node with value 75 is larger than its left child (15) and it does not have right
child. So we stop the process.

Finally, max heap after deleting root node (90) is as follows...


Introduction to Graphs
Graph is a non linear data structure, it contains a set of points known as nodes (or vertices)
and set of linkes known as edges (or Arcs) which connets the vertices. A graph is defined as
follows...

Graph is a collection of vertices and arcs which connects vertices in the graph

Graph is a collection of nodes and edges which connects nodes in the graph

Generally, a graph G is represented as G = ( V , E ), where V is set of vertices and E is set of


edges.

Example

The following is a graph with 5 vertices and 6 edges.


This graph G can be defined as G = ( V , E )
Where V = {A,B,C,D,E} and E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.

Graph Terminology
We use the following terms in graph data structure...
Vertex:
A individual data element of a graph is called as Vertex. Vertex is also known as node. In
above example graph, A, B, C, D & E are known as vertices.
Edge :
An edge is a connecting link between two vertices. Edge is also known as Arc. An edge is
represented as (startingVertex, endingVertex). For example, in above graph, the link between
vertices A and B is represented as (A,B). In above example graph, there are 7 edges (i.e.,
(A,B), (A,C), (A,D), (B,D), (B,E), (C,D), (D,E)).

Edges are three types.

1. Undirected Edge - An undirected egde is a bidirectional edge. If there is a undirected


edge between vertices A and B then edge (A , B) is equal to edge (B , A).
2. Directed Edge - A directed egde is a unidirectional edge. If there is a directed edge
between vertices A and B then edge (A , B) is not equal to edge (B , A).
3. Weighted Edge - A weighted egde is an edge with cost on it.

Undirected Graph
A graph with only undirected edges is said to be undirected graph.
Directed Graph
A graph with only directed edges is said to be directed graph.

Mixed Graph
A graph with undirected and directed edges is said to be mixed graph.

End vertices or Endpoints


The two vertices joined by an edge are called the end vertices (or endpoints) of the edge.

Origin
If an edge is directed, its first endpoint is said to be origin of it.

Destination
If an edge is directed, its first endpoint is said to be origin of it and the other endpoint is said
to be the destination of the edge.

Adjacent
If there is an edge between vertices A and B then both A and B are said to be adjacent. In
other words, Two vertices A and B are said to be adjacent if there is an edge whose end
vertices are A and B.

Incident
An edge is said to be incident on a vertex if the vertex is one of the endpoints of that edge.

Outgoing Edge
A directed edge is said to be outgoing edge on its orign vertex.

Incoming Edge
A directed edge is said to be incoming edge on its destination vertex.

Degree
Total number of edges connected to a vertex is said to be degree of that vertex.

Indegree
Total number of incoming edges connected to a vertex is said to be indegree of that vertex.

Outdegree
Total number of outgoing edges connected to a vertex is said to be outdegree of that vertex.

Parallel edges or Multiple edges


If there are two undirected edges to have the same end vertices, and for two directed edges to
have the same origin and the same destination. Such edges are called parallel edges or
multiple edges.
Self-loop
An edge (undirected or directed) is a self-loop if its two endpoints coincide.

Simple Graph
A graph is said to be simple if there are no parallel and self-loop edges.

Path
A path is a sequence of alternating vertices and edges that starts at a vertex and ends at a
vertex such that each edge is incident to its predecessor and successor vertex.

Graph Representations
Graph data structure is represented using following representations...

1. Adjacency Matrix
2. Incidence Matrix
3. Adjacency List

Adjacency Matrix

In this representation, graph can be represented using a matrix of size total number of vertices
by total number of vertices. That means if a graph with 4 vertices can be represented using a
matrix of 4X4 class. In this matrix, rows and columns both represents vertices. This matrix is
filled with either 1 or 0. Here, 1 represents there is an edge from row vertex to column vertex
and 0 represents there is no edge from row vertex to column vertex.

For example, consider the following undirected graph representation...

Directed graph representation...


Incidence Matrix

In this representation, graph can be represented using a matrix of size total number of vertices
by total number of edges. That means if a graph with 4 vertices and 6 edges can be
represented using a matrix of 4X6 class. In this matrix, rows represents vertices and columns
represents edges. This matrix is filled with either 0 or 1 or -1. Here, 0 represents row edge is
not connected to column vertex, 1 represents row edge is connected as outgoing edge to
column vertex and -1 represents row edge is connected as incoming edge to column vertex.

For example, consider the following directed graph representation...

Adjacency List

In this representation, every vertex of graph contains list of its adjacent vertices.

For example, consider the following directed graph representation implemented using linked
list...

This representation can also be implemented using array as follows..


Graph Traversals

Graph traversal is technique used for searching a vertex in a graph. The graph traversal is also
used to decide the order of vertices to be visit in the search process. A graph traversal finds
the edges to be used in the search process without creating loops that means using graph
traversal we visit all vertices of graph without getting into looping path.

There are two graph traversal techniques and they are as follows...

1. DFS (Depth First Search)


2. BFS (Breadth First Search)

DFS (Depth First Search)

DFS traversal of a graph, produces a spanning tree as final result. Spanning Tree is a graph
without any loops. We use Stack data structure with maximum size of total number of
vertices in the graph to implement DFS traversal of a graph.

We use the following steps to implement DFS traversal...

 Step 1: Define a Stack of size total number of vertices in the graph.


 Step 2: Select any vertex as starting point for traversal. Visit that vertex and push it
on to the Stack.
 Step 3: Visit any one of the adjacent vertex of the verex which is at top of the stack
which is not visited and push it on to the stack.
 Step 4: Repeat step 3 until there are no new vertex to be visit from the vertex on top
of the stack.
 Step 5: When there is no new vertex to be visit then use back tracking and pop one
vertex from the stack.
 Step 6: Repeat steps 3, 4 and 5 until stack becomes Empty.
 Step 7: When stack becomes Empty, then produce final spanning tree by removing
unused edges from the graph

Back tracking is coming back to the vertex from which we came to current vertex
Example
BFS (Breadth First Search)

BFS traversal of a graph, produces a spanning tree as final result. Spanning Tree is a graph
without any loops. We use Queue data structure with maximum size of total number of
vertices in the graph to implement BFS traversal of a graph.

We use the following steps to implement BFS traversal...

 Step 1: Define a Queue of size total number of vertices in the graph.


 Step 2: Select any vertex as starting point for traversal. Visit that vertex and insert it
into the Queue.
 Step 3: Visit all the adjacent vertices of the verex which is at front of the Queue
which is not visited and insert them into the Queue.
 Step 4: When there is no new vertex to be visit from the vertex at front of the Queue
then delete that vertex from the Queue.
 Step 5: Repeat step 3 and 4 until queue becomes empty.
 Step 6: When queue becomes Empty, then produce final spanning tree by removing
unused edges from the graph

Example
Spanning Tree

A spanning tree is a subset of Graph G, which has all the vertices covered with minimum
possible number of edges. Hence, a spanning tree does not have cycles and it cannot be
disconnected..

By this definition, we can draw a conclusion that every connected and undirected Graph G
has at least one spanning tree. A disconnected graph does not have any spanning tree, as it
cannot be spanned to all its vertices.

We found three spanning trees off one complete graph. A complete undirected graph can
have maximum nn-2 number of spanning trees, where n is the number of nodes. In the above
addressed example, 33−2 = 3 spanning trees are possible.
General Properties of Spanning Tree

We now understand that one graph can have more than one spanning tree. Following are a
few properties of the spanning tree connected to graph G −

 A connected graph G can have more than one spanning tree.


 All possible spanning trees of graph G, have the same number of edges and vertices.

 The spanning tree does not have any cycle (loops).

 Removing one edge from the spanning tree will make the graph disconnected, i.e. the
spanning tree is minimally connected.

 Adding one edge to the spanning tree will create a circuit or loop, i.e. the spanning
tree is maximally acyclic.

Mathematical Properties of Spanning Tree

 Spanning tree has n-1 edges, where n is the number of nodes (vertices).
 From a complete graph, by removing maximum e - n + 1 edges, we can construct a
spanning tree.

 A complete graph can have maximum nn-2 number of spanning trees.

Thus, we can conclude that spanning trees are a subset of connected Graph G and
disconnected graphs do not have spanning tree.

Application of Spanning Tree

Spanning tree is basically used to find a minimum path to connect all nodes in a graph.
Common application of spanning trees are −

 Civil Network Planning


 Computer Network Routing Protocol

 Cluster Analysis

Let us understand this through a small example. Consider, city network as a huge graph and
now plans to deploy telephone lines in such a way that in minimum lines we can connect to
all city nodes. This is where the spanning tree comes into picture.

Minimum Spanning Tree (MST)

In a weighted graph, a minimum spanning tree is a spanning tree that has minimum weight
than all other spanning trees of the same graph. In real-world situations, this weight can be
measured as distance, congestion, traffic load or any arbitrary value denoted to the edges.
Minimum Spanning-Tree Algorithms
We shall learn about two most important spanning tree algorithms here −
 Kruskal's Algorithm
 Prim's Algorithm
Both are greedy algorithms.
Sorting and Searching Techniques
Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to
arrange data in a particular order. Most common orders are in numerical or lexicographical
order.

The importance of sorting lies in the fact that data searching can be optimized to a very high
level, if data is stored in a sorted manner. Sorting is also used to represent data in more
readable formats. Following are some of the examples of sorting in real-life scenarios −

 Telephone Directory − The telephone directory stores the telephone numbers of


people sorted by their names, so that the names can be searched easily.
 Dictionary − The dictionary stores words in an alphabetical order so that searching of
any word becomes easy.

Selection Sort
Selection Sort algorithm is used to arrange a list of elements in a particular order (Ascending
or Descending). In selection sort, the first element in the list is selected and it is compared
repeatedly with remaining all the elements in the list. If any element is smaller than the
selected element (for Ascending order), then both are swapped. Then we select the element at
second position in the list and it is compared with remaining all elements in the list. If any
element is smaller than the selected element, then both are swapped. This procedure is
repeated till the entire list is sorted.

Step by Step Process

The selection sort algorithm is performed using following steps...

 Step 1: Select the first element of the list (i.e., Element at first position in the list).
 Step 2: Compare the selected element with all other elements in the list.
 Step 3: For every comparision, if any element is smaller than selected element (for
Ascending order), then these two are swapped.
 Step 4: Repeat the same procedure with next position in the list till the entire list is
sorted.

Sorting Logic

Following is the sample code for selection sort...

Example
To sort a unsorted list with 'n' number of elements we need to make ((n-1)+(n-2)+(n-3)+......
+1) = (n (n-1))/2 number of comparisions in the worst case. If the list already sorted, then it
requires 'n' number of comparisions.

Worst Case : O(n2)


Best Case : Ω(n2)
Average Case : Θ(n2)

-----------Write a Selection Sort Program -----------------

 Insertion sort:

Sorting is the process of arranging a list of elements in a particular order (Ascending or


Descending).

Insertion sort algorithm arranges a list of elements in a particular order. In insertion sort
algorithm, every iteration moves an element from unsorted portion to sorted portion until all
the elements are sorted in the list.

The insertion sort algorithm is performed using following steps...

 Step 1: Asume that first element in the list is in sorted portion of the list and
remaining all elements are in unsorted portion.
 Step 2: Consider first element from the unsorted list and insert that element into the
sorted list in order specified.
 Step 3: Repeat the above process until all the elements from the unsorted list are
moved into the sorted list.

Following is the sample code for insertion sort...

Example:
To sort a unsorted list with 'n' number of elements we need to make (1+2+3+......+n-1) = (n
(n-1))/2 number of comparisions in the worst case. If the list already sorted, then it requires
'n' number of comparisions.

Worst Case : O(n2)


Best Case : Ω(n)
Average Case : Θ(n2)

-----------------------------write a program to insertion sort


-------------------------------------------
Bubble Sort
Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based
algorithm in which each pair of adjacent elements is compared and the elements are swapped
if they are not in order. This algorithm is not suitable for large data sets as its average and
worst case complexity are of Ο(n2) where n is the number of items.

How Bubble Sort Works?


We take an unsorted array for our example. Bubble sort takes Ο(n 2) time so we're keeping it
short and precise.

Bubble sort starts with very first two elements, comparing them to check which one is
greater.

In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare
33 with 27.

We find that 27 is smaller than 33 and these two values must be swapped.
The new array should look like this −

Next we compare 33 and 35. We find that both are in already sorted positions.

Then we move to the next two values, 35 and 10.

We know then that 10 is smaller 35. Hence they are not sorted.

We swap these values. We find that we have reached the end of the array. After one iteration,
the array should look like this −

To be precise, we are now showing how an array should look like after each iteration. After
the second iteration, it should look like this −

Notice that after each iteration, at least one value moves at the end.

And when there's no swap required, bubble sorts learns that an array is completely sorted.
Now we should look into some practical aspects of bubble sort.

Algorithm
We assume list is an array of n elements. We further assume that swap function swaps the
values of the given array elements.

begin BubbleSort(list)

for all elements of list


if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for

return list

end BubbleSort

Pseudo code
We observe in algorithm that Bubble Sort compares each pair of array element unless the
whole array is completely sorted in an ascending order. This may cause a few complexity
issues like what if the array needs no more swapping as all the elements are already
ascending.

To ease-out the issue, we use one flag variable swapped which will help us see if any swap
has happened or not. If no swap has occurred, i.e. the array requires no more processing to be
sorted, it will come out of the loop.

Pseudo code of Bubble Sort algorithm can be written as follows −

procedure bubbleSort( list : array of items )

loop = list.count;

for i = 0 to loop-1 do:


swapped = false

for j = 0 to loop-1 do:

/* compare the adjacent elements */


if list[j] > list[j+1] then
/* swap them */
swap( list[j], list[j+1] )
swapped = true
end if

end for

/*if no number was swapped that means


array is sorted now, break the loop.*/

if(not swapped) then


break
end if

end for

end procedure return list

Implementation
One more issue we did not address in our original algorithm and its improvised pseudocode,
is that, after every iteration the highest values settles down at the end of the array. Hence, the
next iteration need not include already sorted elements. For this purpose, in our
implementation, we restrict the inner loop to avoid already sorted values.

---------------------write a program to bubble sor t ----------------

Merge sort:

Merge sort is a sorting technique based on divide and conquer technique. With worst-case
time complexity being Ο(n log n), it is one of the most respected algorithms.

Merge sort first divides the array into equal halves and then combines them in a sorted
manner.
How Merge Sort Works?
To understand merge sort, we take an unsorted array as the following −

We know that merge sort first divides the whole array iteratively into equal halves
unless the atomic values are achieved. We see here that an array of 8 items is divided
into two arrays of size 4.

This does not change the sequence of appearance of items in the original. Now we divide
these two arrays into halves.

We further divide these arrays and we achieve atomic value which can no more be divided.

Now, we combine them in exactly the same manner as they were broken down. Please note
the color codes given to these lists.

We first compare the element for each list and then combine them into another list in a sorted
manner. We see that 14 and 33 are in sorted positions. We compare 27 and 10 and in the
target list of 2 values we put 10 first, followed by 27. We change the order of 19 and 35
whereas 42 and 44 are placed sequentially.

n the next iteration of the combining phase, we compare lists of two data values, and merge
them into a list of found data values placing all in a sorted order.

After the final merging, the list should look like this –
Now we should learn some programming aspects of merge sorting.

Algorithm

Merge sort keeps on dividing the list into equal halves until it can no more be divided. By
definition, if it is only one element in the list, it is sorted. Then, merge sort combines the
smaller sorted lists keeping the new list sorted too.

Step 1 − if it is only one element in the list it is already sorted, return.


Step 2 − divide the list recursively into two halves until it can no more be divided.
Step 3 − merge the smaller lists into new list in sorted order.

Pseudocode

We shall now see the pseudocodes for merge sort functions. As our algorithms point out two
main functions − divide & merge.

Merge sort works with recursion and we shall see our implementation in the same way

-----------------write a program to merge sort---------------

Quick sort:
Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data
into smaller arrays. A large array is partitioned into two arrays one of which holds values
smaller than the specified value, say pivot, based on which the partition is made and another
array holds values greater than the pivot value.

Quick sort partitions an array and then calls itself recursively twice to sort the two resulting
subarrays. This algorithm is quite efficient for large-sized data sets as its average and worst
case complexity are of Ο(nlogn), where n is the number of items.

Partition in Quick Sort


Following animated representation explains how to find the pivot value in an array.
The pivot value divides the list into two parts. And recursively, we find the pivot for each
sub-lists until all lists contains only one element.

Quick Sort Pivot Algorithm

Based on our understanding of partitioning in quick sort, we will now try to write an
algorithm for it, which is as follows.

Step 1 − Choose the highest index value has pivot


Step 2 − Take two variables to point left and right of the list excluding pivot
Step 3 − left points to the low index
Step 4 − right points to the high
Step 5 − while value at left is less than pivot move right
Step 6 − while value at right is greater than pivot move left
Step 7 − if both step 5 and step 6 does not match swap left and right
Step 8 − if left ≥ right, the point where they met is new pivot

Quick Sort Algorithm

Using pivot algorithm recursively, we end up with smaller possible partitions. Each partition
is then processed for quick sort. We define recursive algorithm for quicksort as follows −

Step 1 − Make the right-most index value pivot


Step 2 − partition the array using pivot value
Step 3 − quicksort left partition recursively
Step 4 − quicksort right partition recursively

------------------------ Write a program to quick sort----------------------------


Linear Search

Search is a process of finding a value in a list of values. In other words, searching is the
process of locating given value position in a list of values.

Linear search algorithm finds given element in a list of elements with O(n) time complexity
where n is total number of elements in the list. This search process starts comparing of search
element with the first element in the list. If both are matching then results with element found
otherwise search element is compared with next element in the list. If both are matched, then
the result is "element found". Otherwise, repeat the same with the next element in the list
until search element is compared with last element in the list, if that last element also doesn't
match, then the result is "Element not found in the list". That means, the search element is
compared with element by element in the list.

Linear search is implemented using following steps...

 Step 1: Read the search element from the user


 Step 2: Compare, the search element with the first element in the list.
 Step 3: If both are matching, then display "Given element found!!!" and terminate the
function
 Step 4: If both are not matching, then compare search element with the next element
in the list.
 Step 5: Repeat steps 3 and 4 until the search element is compared with the last
element in the list.
 Step 6: If the last element in the list is also doesn't match, then display "Element not
found!!!" and terminate the function.

Example

Consider the following list of element and search element...

Linear Search
Linear Search program--------------------------------------------------------
Binary Search Algorithm
Search is a process of finding a value in a list of values. In other words, searching is the
process of locating given value position in a list of values.

Binary search algorithm finds given element in a list of elements with O(log n) time
complexity where n is total number of elements in the list. The binary search algorithm can
be used with only sorted list of element. That means, binary search can be used only with
lkist of element which are already arraged in a order. The binary search can not be used for
list of element which are in random order. This search process starts comparing of the search
element with the middle element in the list. If both are matched, then the result is "element
found". Otherwise, we check whether the search element is smaller or larger than the middle
element in the list. If the search element is smaller, then we repeat the same process for left
sublist of the middle element. If the search element is larger, then we repeat the same process
for right sublist of the middle element. We repeat this process until we find the search
element in the list or until we left with a sublist of only one element. And if that element also
doesn't match with the search element, then the result is "Element not found in the list".

Binary search is implemented using following steps...

 Step 1: Read the search element from the user


 Step 2: Find the middle element in the sorted list
 Step 3: Compare, the search element with the middle element in the sorted list.
 Step 4: If both are matching, then display "Given element found!!!" and terminate the
function
 Step 5: If both are not matching, then check whether the search element is smaller or
larger than middle element.
 Step 6: If the search element is smaller than middle element, then repeat steps 2, 3, 4
and 5 for the left sublist of the middle element.
 Step 7: If the search element is larger than middle element, then repeat steps 2, 3, 4
and 5 for the right sublist of the middle element.
 Step 8: Repeat the same process until we find the search element in the list or until
sublist contains only one element.
 Step 9: If that element also doesn't match with the search element, then display
"Element not found in the list!!!" and terminate the function.

Example

Consider the following list of element and search element...


Write a Binary Search program----------------------------------

You might also like