II Bsc - Datastructures With Java-112 Copies
II Bsc - Datastructures With Java-112 Copies
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:
Operation of ADT:
Creation
Insertion
Deletion
Copy
Modify
Search
Advantages:
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
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.
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.
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
NODE
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 Deletelast()
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
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:
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.
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.
T 40 Top=0
o 50 50 top=-1
p
=
1
Algorithm:
Algorithm Pop(S,Top)
Begin
if(Top1)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.
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.
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
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—
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.
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
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
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.front1
Q.rear1
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
34 78 90 587 78 50 56 87
6
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:
Dequeue():
The dequeue() operation is used for removing the first element from the circular queue.
Algorithm:
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.
Visiting − Visiting refers to checking the value of a node when control is on the node.
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.
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;
};
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
endwhile
insert data
end If
Implementation
The implementation of insert function should look like this −
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
while(1) {
parent = current;
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
end if
while(current->data != data) {
if(current != NULL)
printf("%d ",current->data);
//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 −
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.
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 −
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);
//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;
while(1) {
parent = current;
if(current == NULL) {
parent->leftChild = tempNode;
return;
}
}//go to right of the tree
else {
current = current->rightChild;
10,12,5,4,20,8,7,15 and 13
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.
To convert above binary tree into threaded binary tree, first find the in-order traversal of that
tree...
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.
There are two types of heap data structures and they are as follows...
1. Max Heap
2. 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 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
1. Finding Maximum
2. Insertion
3. Deletion
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.
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.
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
Example
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)).
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.
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.
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.
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.
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...
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...
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.
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.
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 −
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.
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.
Thus, we can conclude that spanning trees are a subset of connected Graph G and
disconnected graphs do not have 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 −
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.
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 −
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 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
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.
Insertion sort:
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.
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.
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.
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.
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)
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.
loop = list.count;
end for
end for
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.
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.
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
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.
Based on our understanding of partitioning in quick sort, we will now try to write an
algorithm for it, which is as follows.
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 −
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.
Example
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".
Example