Data Structures Unit V
Data Structures Unit V
UNIT – V
Sorting, Hashing and Graphs
Sorting: Bubble Sort, Selection Sort, Insertion Sort, Heap Sort, Shell Sort and Radix Sort. Performance and
Comparison among the sorting methods. Hashing: Hash Table, Hash Function and its characteristics. Graph:
Basic Terminologies and Representations, Graph traversal algorithms
2 Marks
1. Define Sorting.
A sorting algorithm is an algorithm that puts elements of a list either in ascending or descending order.
Internal sorting:
It takesplace in themainmemory of acomputer
Internal sorting methods are applied for small collections of data (i.e) the entire collection of data to be
sorted is smallenoughthatthesorting can takeplacewithinmainmemory.
External sorting:
Externalsortingmethodsareappliedonlywhenthenumberofdataelementstobesortedistoolarge.
Thesesorting methodsinvolves as much external processing.
4. List out some of the sorting algorithms.
1. Insertion sort
2. Selectionsort
3. Bubble sort
4. Shell sort
5. Merge sort
6. Heap sort
7. Quicksort
8. RadixSort
7. Name the sorting techniques which use Divide and Conquer strategy.
Merge sort
Quick sort
8. What is the best case and average case analysis for Quick sort?
Thetotaltime in best case is : O(n logn)
The total time in worstcase is : Ɵ(n²)
That is, if A is an array then the elements of A are arranged in sorted order (ascending order) in
such a way that, A [0] < A [1] < A [2] < …… < A [N].
External sorting is applied when there is huge data that cannot be stored in computer’s memory.
Hashing is used for storing relatively large amounts of data in a table called a hash table.
Hashing is a technique used to perform insertions, deletions, and finds the element in constant
average time
Hash Table is a data structure in which keys are mapped to array positions by a hash function.
Hash table is usually fixed as M-size, which is larger than the amount of data that we want to store.
Division Method
Multiplication Method
Folding Method
Division method is the most simple method of hashing an integer x. The method divides x
by M and then use the remainder thus obtained.
h(x) = x mod M
Collision occurs when the hash function maps two different keys to same location.
Open Addressing
When collision occurs, use organized method to find next open space.
Maximum number of elements equal to table size.
Chained Addressing – Separate Chaining
32. What are the methods used in Open addressing collision technique?
• Linear Probing
• Quadratic Probing
• Double Hashing
The best sorting algorithm which implements the ‘divide and conquer’ concept.
It first divides the list into two parts by picking an element a ’pivot’.
Then arranges the elements those are smaller than pivot into one sub list and the elements
those are greater than pivot into one sub list by keeping the pivot in its original place.
It is also called as partition exchange sort.
39. What is selection sort ? Why selection sort is better than insertion sort?
The idea of selection sort is rather simple which repeatedly find the next largest (or
smallest) element in the array and move it to its final position in the sorted array.
In insertion sort the elements are inserted at an appropriate place similar to card insertion.
The elements in the list is divided into two parts- sorted and unsorted sub-lists.
In each pass, the first element of unsorted sub-list is picked up and moved into the sorted sub-
list by inserting it in suitable positon.
41. What is shell sort?
The shell sort improves upon bubble sort and insertion sort, by moving out of order
elements more than one position at a time.
It compares the elements that are at a specific distance from each other, and interchanges
them if necessary. The shell sort divides the list into smaller sub lists, and then sorts the sub lists
separately using the insertion sort
Selection sort is sorted by scanning the entire list to find the smallest element and exchange
it with the first element, putting the first element in the final position in the sorted list. Then the scan
starts from the second element to find the smallest among n-1 elements and exchange it with the
second element.
G= (V,E)
46. Define digraph (Nov 13)
If an edge between any two nodes in a graph is directionally oriented, a graph is called as directed
.it is also referred as digraph.
47. Define undirected graph.
If an edge between any two nodes in a graph is not directionally oriented, a graph is called as
undirected .it is also referred as unqualified graph.
A path in a graph is defined as a sequence of distinct vertices each adjacent to the next except
possibly the first vertex and last vertex is different.
A cycle is a path containing atleast three vertices such that the starting and the ending vertices
are the same. The cycles are (1, 2, 3, 1), (1, 2, 3, 4, 5, 3, 1)
A graph is said to be a strongly connected graph, if for every pair of distinct vertices there is a
directed path from every vertex to every other vertex. It is also referred as a complete graph.
A directed graph is said to be a weakly connected graph if any vertex doesn’t have a directed path
to any other vertices.
52. Define a weighted graph.
A graph is said to be a weighted graph if every edge in the graph is assigned some weight or value.
The weight of an edge is a positive value that may be representing the distance between the vertices
or the weights of the edges along the path.
53. How we can represent the graph?
1. adjacent matrix
2. adjacent list
55. What is meant by traversing a graph? State the different ways of traversing a graph. (Nov 12)
In DFS, we don’t have any special vertex, we can start from any vertex.
Let us start from vertex (v), its adjacent vertex is selected and DFS is initialized.
Let us consider the adjacent vertices to V are V1, V2, and V3 …Vk.
Now pick V1 and visit its adjacent vertices then pick V2 and visit the adjacent vertices .to continue
and process till the nodes are visited.
Consider the graph
V1 V2, V3, V8
V2 V4, V5
V4 V2, V8
V8 V4, V5, V1, V6, V7
V5 V2, V8
V6 V3, V8
V3 V6, V7, V1
V7 V8, V3
Is BFS ,an adjacent vertex is selected then visit its adjacent vertices then backtrack the unvisited adjacent
vertex
In BFS ,to visit all the vertices of the start vertex ,then visit the unvisited vertices to those adjacent
vertices
A directed graph G in which the vertices represent tasks or activities and the edges represent activities
to move from one event to another then the task is known as activity on vertex network or AOV-network/pert
network
5 Marks
SORTING
1. BUBBLE SORT
1. The easiest and the most widely used sorting technique among students and engineers is
the bubble sort.
2. This sort is also referred as sinking sort. The idea of bubble sort is to repeatedly move
the smallest element to the lowest index position in the list.
3. To find the smallest element, the bubble sort algorithm begins by comparing the first
element of the list with its next element, and upto the end of the list and interchanges the
two elements if they are not in proper order.
4. In either case, after such a pass the smaller element will be in the lowest index position of
the list.
5. The focus then moves to the next smaller element, and the process is repeated. Swapping
occurs only among successive elements in the list and hence only one element will be
placed in its sorted order each pass.
BUBBLE (ARR,N)
END BUBBLE()
Example :-
//Bubble Sort
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,temp,n,a[200];
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(a[j]>a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
for(i=0;i<n;i++)
printf("\n %d",a[i]);
getch();
return 0; }
OUTPUT:
2. INSERTION SORT
1. The main idea of insertion sort is to consider each element at a time, into the appropriate
position relative to the sequence of previously ordered elements, such that the resulting
sequence is also ordered.
2. The insertion sort can be easily understood if you know to play cards. Imagine that you are
arranging cards after it has been distributed before you in front of the table.
3. As each new cards is taken, it is compared with the cards in hand. The card is inserted in
proper place within the cards in hand, by pushing one position to the left or right. This
procedure proceeds until all the cards are placed in the hand are in order.
Algorithm
INSERT(ARR,N)
#include<stdio.h>
#include<conio.h>
int main()
{ int i,j,key,a[200],n;
clrscr();
printf("\n\n Insertion Sort");
printf("\n\n Enter the number of values you have got: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" Enter the %d value: ",i+1);
scanf("%d",&a[i]);
}
printf("\n\n Sorted List");
for(i=1;i<n;i++)
{
key=a[i];
j=i-1;
while((j>=0)&&(key<a[j]))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=key;
}
for(i=0;i<n;i++)
{
printf("\n %d",a[i]);
}
getch();
return 0;
}
OUTPUT:
3. SELECTION SORT
1. Another easiest method for sorting elements in the list is the selection sort. The main idea
of selection sort is to search for the smallest element in the list.
2. When the element is found, it is swapped with the first element in the list. The second
smallest element in the list is then searched.
3. When the element is found, it is swapped with the second element in the list.
4. The process of searching the next smallest element is repeated, until all the elements in
the list have been sorted in ascending order.
Algorithm
SELECT(ARR,N)
K=J
(END OF IF STRUCTURE)
#include<stdio.h>
#include<conio.h>
int main()
int i,j,k,min,n,a[200];
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
k=i;
min=a[i];
for(j=i+1;j<n;j++)
if(a[j]<min)
min=a[j];
k=j;
}}
a[k]=a[i];
a[i]=min;
printf("\n %d",a[i]);
getch();
return 0;
OUTPUT:
4. SHELL SORT
SHELL(ARR,N)
//Shell Sort
#include<stdio.h>
#include<conio.h>
int j,i,k,m,mid;
for(m = n/2;m>0;m/=2)
for(i=j-m;i>=0;i-=m)
if(a[i+m]>=a[i])
break;
else
mid = a[i];
a[i] = a[i+m];
a[i+m] = mid;
printf("\n\nPass %d:",m);
for(k=0;k<n;k++)
printf("%d\t",a[k]);
int main()
int a[20],i,n;
clrscr();
scanf("%d", &n);
scanf("%d", &a[i]);
for(i=0;i< n;i++)
printf("%d\t",a[i]);
shellsort(a,n);
for(i=0;i< n;i++)
printf("%d\t",a[i]);
getch();
return 0;
}
OUTPUT
5. RADIX SORT
Example :-
// Radix Sort
#include <stdio.h>
#include <conio.h>
int MAX = 100;
int A[100];
int C[100][10]; // Two dimensional array, where each row has ten pockets
int N;
void get_Values()
{
int i=0;
printf("\n\tHow many values you want to sort -->");
scanf("%d",&N);
if(N>MAX)
{
printf("\n\n \t Maximum values Limits %d so try again...",MAX);
get_Values(); // recursive call
}
printf("\n Put %d Values..\n",N);
for(i=0 ; i< N ; i++)
scanf("%d",&A[i]);
}
void RadixSort (int R)
{
int i,j,X,D,m,p;
int Z=0;
D=R/10;
for(i=0;i<N;i++)
{
for(j=0 ; j < 10 ; j++) C[i][j]=-1;
}
for(i=0; i<N ; i++)
{
X=A[i]%R;
m=X/D;
if(m>0)Z=1;
C[i][m]=A[i];
}
p=-1;
for(j=0 ; j < 10 ; j++)
{
for( i=0 ; i < N ; i++)
{
if(C[i][j]!=-1)
{
p++;
A[p]=C[i][j];
}
}
}
if(Z==1)
{
R*=10;
RadixSort(R); // recursive call
}
} // end of RadixSort( ) function
void display()
{
int i;
printf("\n\n\t\t\t Sorted List\n\n");
for(i=0; i<N ; i++)
printf("%d",A[i]);
}
void main()
{
clrscr();
get_Values();
if(N>1) RadixSort (10); // Here 10 is the Radix of Decimal Numbersl
display();
getch();
}
OUTPUT
6. Explain various hashing techniques with suitable example.
Separate chaining:
Chaining
In collision handling method chaining is a concept which introduces an additional field with
data i.e. chain. A separate chain table is maintained for colliding data. When collision occurs then a linked
list(chain) is maintained at the home bucket.
For example:
H(key) = keytoD
where D is the size of table. The hash table will be: Here D = 10.
A chain is maintained for colliding elements. For instance 131 has a home bucket (key) 1.
Similarly keys 21 and 61 demand for home bucket 1. Hence a chain is maintained at index 1. Similarly the
chain at index 4 and 7 is maintained.
7. Explain in detail about Rehashing
Rehashing
Rehashing is a technique in which the table is resized, i.e., the size of table is doubled by
creating a new table. It is preferable if the total size of table is a prime number. There are situations in
which the rehashing is required
Consider we have to insert the elements 37, 90, 55, 22, 17, 49 and 87. The table size is 10 and will use hash
function,
# Now this table is almost full and if we try to insert more elements collisions will occur and eventually
further insertions will fail. Hence we will rehash by doubling the table size.
# The old table size is 10 then we should double this size for new table, that becomes 20. But 20 is not a
prime number, we will prefer to make the table size as 23. And new hash function will be
H(key) = key mod 23
Rehashing Example
Extensible hashing:
When the amount of data is too large to fit in main memory, the previous hashing
techniques will not work properly.
So we use a new technique called extensible hashing. It is one form of dynamic hashing.
Here the
Suppose that we have records with these keys and hash function
Check the first 2 bits(prefix bits) of the bit pattern and then place the key in the corresponding directory’s
bucket.
1. Briefly explain the three common collision strategies in open addressing hashing.
(OR)
• Once a collision takes place, open addressing computes new positions using a probe sequence and
the next record is stored in that position.
• In this technique of collision resolution, all the values are stored in the hash table.
• The process of examining memory locations in the hash table is called probing.
• Open addressing technique can be implemented using:
• Linear probing
• Quadratic probing
• Double hashing.
Linear Probing
The simplest approach to resolve a collision is linear probing. In this technique, if a value
is already stored at location generated by h(k), then the following hash function is used to resolve the
collision.
where, m is the size of the hash table, h’(k) = k mod m and i is the probe number and varies from 0 to
m-1.
Example: Consider a hash table with size = 10. Using linear probing insert the keys 72, 27, 36, 24, 63, 81
and 92 into the table.
0 1 2 3 4 5 6 7 8 9
Step1: Key = 72
= (2) mod 10
=2
Since, T[2] is vacant, insert key 72 at this location
0 1 2 3 4 5 6 7 8 9
Step2: Key = 27
= (7) mod 10
=7
Step3: Key = 36
= (6) mod 10
=6
0 1 2 3 4 5 6 7 8 9
Step4: Key = 24
= (4) mod 10
=4
0 1 2 3 4 5 6 7 8 9
Step5: Key = 63
= (3) mod 10
=3
Since, T[3] is vacant, insert key 63 at this location
0 1 2 3 4 5 6 7 8 9
Step6: Key = 81
= (1) mod 10
=1
0 1 2 3 4 5 6 7 8 9
Step7: Key = 92
= (2) mod 10
=2
Now, T[2] is occupied, so we cannot store the key 92 in T[2]. Therefore, try again for next location. Thus
probe, i = 1, this time.
Key = 92
= (2 + 1) mod 10
=3
Now, T[3] is occupied, so we cannot store the key 92 in T[3]. Therefore, try again for next location. Thus
probe, i = 2, this time.
Key = 92
= (2 + 2) mod 10
=4
Now, T[4] is occupied, so we cannot store the key 92 in T[4]. Therefore, try again for next location. Thus
probe, i = 3, this time.
Key = 92
= (2 + 3) mod 10
=5
0 1 2 3 4 5 6 7 8 9
Quadratic probing
• If quadratic probing is used and the table size is prime , then a new element can always be inserted
if the table is at least half empty.
• If the table is even one more than half full, the insertion could fail [prime].
Double Hashing:
F(i)= i.hash2(x)
here , hash2(x) = R-(x mod R) with R is a prime smaller than table size.
Example:
For first value apply the normal hash function i.e key mod tablesize
hash(89)=89%10=9
hash(18)=18%10=8
Now the key values 89 and 18 are stored at the corresponding location ,when for inserting the 3rd element
49
so apply double hashing technique and Choose “R” a prime number smaller than table size, so we choose
R=7 then
hash2(49)=7-(49%7)= 7 - 0 =7
hash2(58)=7-(58%7)= 7 – 2 =5
hash2(69)=7-(69%7)= 7 – 6 = 1
FIG: Graph G
An undirected graph is that in which, the pair of vertices representing the edges is unordered.
DIRECTED GRAPH
A directed graph is that in which, each edge is an ordered pair of vertices, (i.e.) each edge is
represented by a directed pair.
It is also referred to as digraph.
SUBGRAPH
2 3
4
FIG: Graph G
1
2 3
(a) (b)
2 3
2 3
ADJACENT VERTIC
PATH:
A path from vertex V to the vertex W is a sequence of vertices, each adjacent to the next. The
length of the graph is the number of edges in it.
CONNECTED GRAPH
A graph is said to be connected if there exist a path from any vertex to another vertex.
UNCONNECTED GRAPH
A graph is said to be an unconnected graph if there exist any two unconnected components.
2 3
7 8
. 2
4 5
WEAKLY CONNECTED GRAPH
If there does not exist a directed path from one vertex to another vertex then it is said to be a weakly
connected graph.
4 5
CYCLE
A cycle is a path in which the first and the last vertices are the same.
Eg. 1,3,4,7,2,1
DEGREE:
The number of edges incident on a vertex determines its degree. There are two types of degrees In-degree and
out-degree.
IN-DEGREE of the vertex V is the number of edges for which vertex V is a head.
1. It is connected
1. Adjacency matrix.
2. Adjacency list.
3. Adjacency multi-list.
ADJACENCY MATRIX:
The adjacency matrix A for a graph G = (V,E) with n vertices, is an n* n matrix of bits
,such that A Ij = 1 , if there is an edge from vi to vj and
Aij = 0, if there is no
such edge. The adjacency matrix for
the graph G is,
0111
1011
1101
1110
The space required to represent a graph using its adjacency matrix is n* n bits. About half
this space can be saved in case of an undirected graph, by storing only the upper or lower triangle of
the matrix. From the adjacency matrix, one may readily determine if there an edge connecting any
two vertices I and j. for an undirected graph the degree of any vertices I is its row ∑ n
(j=1) A (I, j).
For a directed graph the row is the out-degree and column sum is the in-degree.
The adjacency matrix is a simple way to represent a graph , but it has 2 disadvantages,
It takes O(n*n) space to represent a graph with n vertices ; even for sparse graphs and
ADJACENCY LIST
In the representation of the n rows of the adjacency matrix are represented as n linked lists. There
is one list for each vertex in G. the nodes in list I represent the vertices that are adjacent from vertex
i. Each node has at least 2 fields: VERTEX and LINK. The VERTEX fields contain the indices of
the vertices adjacent to vertex i. In the case of an undirected graph with n vertices and E edges, this
representation requires n head nodes and 2e list nodes.
Vertex 1
2 3 4 0
Vertex 2 1 3 4 0
1 2 4 0
Vertex 3
Vertex 4 1 2 3 0
The degree of the vertex in an undirected graph may be determined by just counting the number of nodes in its
adjacency list. The total number of edges in G may , therefore be determined in time O(n+e).in the case of
digraph the number of list nodes is only e. the out-degree of any vertex can be determined by counting the
number of nodes in an adjacency list. The total number of edges in G can, therefore be determined in O(n+e).
ADJACENCY MULTILIST:
In the adjacency list representation of an undirected graph each edge (vi, vj) is represented by two entries ,one
on the list for Vi and the other on the list for Vj.
For each edge there will be exactly one node , but this node will be in two list , (i.e) the adjacency list for each
of the two nodes it is incident to .the node structure now becomes
Where M is a one bit mark field that may be used to indicate whether or not the edge has been examined. The
adjacency multi-list diagram is as follow,
V
N1 1 1 2 N2 N4
1 3 N3 N4
N2
V
2
N3 edge(1,4)
N
1 4 0 5
N N
2 3 5 6
N4 edge(2,3)
N
N5 2 4 0 6 edge(2,4)
N6 edge(3,4)
3 4 0 0
V4: N3 N5 N6
GRAPH TRAVERSAL
Given an undirected graph G = (V.E) and a vertex v in V(G) we are interested in visiting all vertices in G that
are reachable from v (that is all vertices connected to v ). We have two ways to do the traversal. They are
DEPTH FIRST SEARCH
BREADTH FIRST SEARCH.
In graphs, we do not have any start vertex or any special vertex singled out to start traversal from. Therefore
the traversal may start from any arbitrary vertex.
We start with say, vertex v. An adjacent vertex is selected and a Depth First search is intimated from it, i.e. let
V1, V2.. Vk are adjacent vertices to vertex v. We may select any vertex from this list. Say, we select V1. Now
all the adjacent vertices to v1 are identified and all of those are visited; next V2 is selected and all its adjacent
vertices visited and so on.
This process continues till are the vertices are visited. It is very much possible that we reach a traversed vertex
second time. Therefore we have to set a flag somewhere to check if the vertex is already visited.
V1
V2
V3
V4 V5 V6 V7
V8
1. Its adjacent vertices are V2, V8, and V3. Let us pick on v2.
8. Its adjacent vertices are V2, V8. Both are already visited therefore, we back track.
9. We had V6 and V7 unvisited in the list of V8. We may visit any. We may visit any. We visit V6.
12. All the adjacent vertices of V7 are already visited, we back track and find that we have visited all the
vertices.
This is not a unique or the only sequence possible using this traversal method.
We may implement the Depth First search by using a stack, pushing all unvisited vertices to the one just
visited and popping the stack to find the next vertex to visit
// Given an undirected graph G = (V.E) with n vertices and an array visited (n) initially set to zero . This
algorithm visits all vertices reachable from v .G and VISITED are global > //VISITED (v) 1
end DFS
COMPUTING TIME
1. In case G is represented by adjacency lists then the vertices w adjacent to v can be determined by
following a chain of links. Since the algorithm DFS would examine each node in the adjacency lists at
most once and there are 2e list nodes. The time to complete the search is O (e).
2. If G is represented by its adjacency matrix, then the time to determine all vertices adjacent to v is O(n).
In DFS we pick on one of the adjacent vertices; visit all of its adjacent vertices and back track to visit the
unvisited adjacent vertices.
In BFS , we first visit all the adjacent vertices of the start vertex and then visit all the unvisited vertices
adjacent to these and so on.
Let us consider the same example, given in figure. We start say, with V1. Its adjacent vertices are V2, V8 ,
V3.
We visit all one by one. We pick on one of these, say V2. The unvisited adjacent vertices to V2 are V4, V5
. we visit both.
We go back to the remaining visited vertices of V1 and pick on one of this, say V3. T The unvisited adjacent
vertices to V3 are V6,V7. There are no more unvisited adjacent vertices of V8, V4, V5, V6 and V7.
V1
V1
V2 V3 V2 V3
V1
V8
(a)
V4 V5 V8
(b)
(c)
V1
V2 V3
V4 V5 V6 V7
V8
We add unvisited vertices adjacent to the one just visited at the rear and read at from to find the next vertex
to visit.
Procedure BFS(v)
//A breadth first search of G is carried out beginning at vertex v. All vertices visited are marked as
VISITED(I) = 1. The graph G and array VISITED are global and VISITED is initialised to 0.//
1. VISITED(v) 1
3. loop
9. call DELETEQ(v,Q)
10. forever
1. Each vertex visited gets into the queue exactly once, so the loop forever is iterated at most n times.
2. If an adjacency matrix is used, then the for loop takes O(n) time for each vertex visited. The total time
is, therefore, O(n2).
3. In case adjacency lists are used the for loop as a total cost of d1+.......... +dn = O(e) where di = degree(vi).
Again, all vertices visited. Together with all edges incident to from a connected component of G.
3. What isgraphtraversal? Whatareitstypes? Explain(April 2011, April 2012, April
2013) Graph Traversal
Some applications require the graph to be traversed . This means that starting from
some designated vertex the graph is walked around in a symmetric way such that
vertex is visited only once. Two algorithms are commonly used:
1. Depth-First Search
2. Breadth-First Search
Applications of DFS
3. If the first element on the Stack is a goal node g , return succeed and stop otherwise.
4. POP and expand the first element and place the children at the front of the Stack.
5. Go back to step 2
Step 1 Step 2
Step 3 Step 4
Step 5 Step 6
Step 7 Step 8
Step 9 Step 10
Applications of BFS
V1
(d)
FIG: Breadth First Search
Thus the sequence so generated is V1,V2, V8, V3,V4, V5,V6, V7. Here we need a queue instead of a
stack to implement it.
We add unvisited vertices adjacent to the one just visited at the rear and read at from to find the next
vertex to visit.
1. VISITED(v) 1
2. Initialise Q to be empty //Q is a queue//
3. loop
4. for all vertices w adjacent to v do
5. if VISITED(w) = 0 //add w to queue//
6. then [call ADDQ(w, Q); VISITED(w) 1] //mark w as VISITED//
7. end
8. if Q is empty thenreturn
9. call DELETEQ(v,Q)
10. forever
11. end BFS
Computing Time
1. Each vertex visited gets into the queue exactly once, so the loop forever is iterated at most n
times.
2. If an adjacency matrix is used, then the for loop takes O(n) time for each vertex visited. The
4. What is minimum Spanning tree? Explain Kruskal’s algorithm with the graph.(Nov 2012) Spanning Tree
When the graph G is connected, a depth first search starting at any vertex, visits all the vertices in G.
In this case the edges of G are partitioned into two sets T (for tree edges) and B (for back edges), where T
is the set of edges used or traversed during the search and B the set of remaining edges.
Since, the links selected will form a tree. We are going to find the spanning tree of a given graph
withminimum cost. The cost of a spanning tree is the sum of the costs of the edges in the tree.
The minimum cost edge is that connects vertex A and B. let us choose that Fig.(a)
Fig. (a)
Now we have, Vertices that may be
added,
The least cost edge is AC therefore we choose AC. Now we have Fig. (b),
Fig. b
AF is the minimum cost edge therefore we add it to the partial tree we have
Fig.: (d)
The only vertex left is G and we have the minimum cost edge that connects it to the tree constructed so for is
BG. Therefore add it and the costs 9+3+7+5+4+8+36 and is given by the following figure,
Fig. (e)
ALGORITHM
1. T<-0
Initially, E is the set of all edges in G. In this set, we are going to (I) determining an edge with minimum cost (in
line 3)
(ii) Delete that edge (line 4)
This can be done efficiently if the edges in E are maintained as a sorted sequential list.
In order to be able to perform steps 5&6 efficiently, the vertices in G should be grouped together in
such way that one may easily determine of the vertices V and W are already connected by the earlier selection
of edges.
In case they are, then the edge (V,W) is to be discarded. If theyare not then (V,W) is to be added to T.
Our possible grouping is to place all vertices in the same connected components of T into the set. Then two
vertices V,W are connected in t if they are in the same set.
Here for example when the edge (B,F) is to be considered, the sets would be {A,B,C,D,F},{E},{F}.vertices B
and F are in the same set and so the edge (B,F) is rejected. The next edge to be considered is (C, E).since vertices
C and E are in different sets the edge is accepted. This edge connects the two components {A, B, C, D, F} and
{E} together and so these two sets should be joined to obtain the set representing the new component.
5. Explain Prim’s Algorithmindetail and findthe Minimum Spanning Tree forthebelow graph.
Spanning tree:
the adjacency matrix of G, then the matrix A+ having the property A+ (i,j) = 1 if there is a path of length > 0 from
i to j and 0 otherwise is called the transitive closure matrix of G. The matrix A* with the property A* (i,j) = 1
if there is a path of length 0 from i to j and 0 otherwise is the reflexive transitive closure matrix of G.
on the diagonal. A+(i,i) = 1 iff there a cycle of length > 1 containing vertex i while A*(i,i) is always one as there
is a path of length 0 from i to i. If we use algorithm ALL_COSTS with COST(i,j) = 1 if <i,j> is an edge in
G and
COST(i,j) = + if <i,j> is not in G, then we can easily obtain A+ from the final matrix A by letting A+ (i,j) = 1
iff A (i,j) < +∞. A* can be obtained from A+ by setting all diagonal elements equal 1. The total time is O(n3).
Some simplification is achieved by slightly modifying the algorithm. In this modification the computation of
line 9 of ALL_COSTS becomes A(i,j) A (i,j) or (A(i,k) and A(k,)) and COST(i,j) is just the adjacencymatrix of
G. With this modification, A need only be a bit matrix and then the final matrix A will be A+.
A graph structure can be extended by assigning a weight to each edge of the graph. Graphs with weights,
or weighted graphs, are used to represent structures in which pairwise connections have some numerical
values. For example if a graph represents a road network, the weights could represent the length of each road.
A digraph with weighted edges in the context of graph theory is called a network.
Networks have manyuses in the practical side of graph theory, network analysis (for example, to model
and analyze traffic networks). Within network analysis, the definition of the term "network" varies, and may
often refer to a simple graph.
Model of www: The model of world wide web (www) can be represented by a graph (directed) wherein
nodes denote the documents, papers, articles, etc. and the edges represent the outgoing hyperlinks between
them as shown in
Figure
Coloring of maps: Coloring of maps is an interesting problem wherein it is desired that a map has to be
colored in such a fashion that no two adjacent countries or regions have the same color. The constraint is to
use minimum number of colors. A map can be represented as a graph wherein a node represents a region and
an edge between two regions denote that the two regions are adjacent.
8. Discuss in detail Dijikstra’s algorithm and Find the shortest path for the below graph using the same
Algorithm.
Running Performance:
On array – O | V ² |
On binary heap – O ( E + V log
V) On Fibonacci heap – O (log
V) Where E – edges, V –
vertices
RESULT: e -> a = 9
e -> b = 6
e -> c = 6
e->d= 4
Sri Manakula Vinayagar Engineering College, Puducherry
Important Program
1. SHORTEST PATH ALGORITHM - FLOYD’S ALGORITHM
#include<stdio.h>
void all_path();
int cost[20][20],n;
void main()
int i,j;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
all_path();
getch();
void all_path()
72 Data Structures
Sri Manakula Vinayagar Engineering College, Puducherry
{
int i,j,k,a[20][20];
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
a[i][j]=cost[i][j];
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(a[i][j]<(a[i][k]+a[k][j]))
a[i][j]=a[i][j];
else
a[i][j]=(a[i][k]+a[k][j]);
73 Data Structures
Sri Manakula Vinayagar Engineering College, Puducherry
}
printf("STEP:%d\n",k);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
printf(" %d",a[i][j]);
printf("\n");
#include <stdio.h>
#include <conio.h>
int i,u,count,w,flag[10],min;
for(i=1;i<=n;i++)
74 Data Structures
Sri Manakula Vinayagar Engineering College, Puducherry
flag[i]=0,dist[i]=cost[v][i];
count=2;
while(count<=n)
min=99;
for(w=1;w<=n;w++)
min=dist[w],u=w;
flag[u]=1;
count++;
for(w=1;w<=n;w++)
dist[w]=dist[u]+cost[u][w];
void main()
int n,v,i,j,cost[10][10],dist[10];
clrscr();
scanf("%d",&n);
75 Data Structures
Sri Manakula Vinayagar Engineering College, Puducherry
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=infinity;
scanf("%d",&v);
dij(n,v,cost,dist);
for(i=1;i<=n;i++)
if(i!=v)
printf("%d->%d,cost=%d\n",v,i,dist[i]);
getch();
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
76 Data Structures
Sri Manakula Vinayagar Engineering College, Puducherry
int find(int);
int uni(int,int);
void main()
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
while(ne<n)
for(i=1,min=999;i<=n;i++)
77 Data Structures
Sri Manakula Vinayagar Engineering College, Puducherry
for(j=1;j<=n;j++)
if(cost[i][j]<min)
min=cost[i][j];
a=u=i;
b=v=j;
u=find(u);
v=find(v);
if(uni(u,v))
mincost +=min;
cost[a][b]=cost[b][a]=999;
getch();
78 Data Structures
Sri Manakula Vinayagar Engineering College, Puducherry
int find(int i)
while(parent[i])
i=parent[i];
return i;
if(i!=j)
parent[j]=i;
return 1;
}return 0;
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
void main()
clrscr();
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
visited[1]=1;
printf("\n");
while(ne<n)
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0)
min=cost[i][j];
a=u=i;
b=v=j;
if(visited[u]==0 || visited[v]==0)
80 Data Structures
Sri Manakula Vinayagar Engineering College, Puducherry
mincost+=min;
visited[b]=1;
cost[a][b]=cost[b][a]=999;
getch();
#include<conio.h>
int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;
void bfs(int v)
for(i=1;i<=n;i++)
q[++r]=i;
if(f<=r)
visited[q[f]]=1;
bfs(q[f++]);
81 Data Structures
Sri Manakula Vinayagar Engineering College, Puducherry
}
void main()
int v;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
q[i]=0;
visited[i]=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
scanf("%d",&v);
bfs(v);
for(i=1;i<=n;i++)
82 Data Structures
Sri Manakula Vinayagar Engineering College, Puducherry
if(visited[i])
printf("%d\t",i);
else
getch();
#include<conio.h>
int a[20][20],reach[20],n;
void dfs(int v)
int i;
reach[v]=1;
for(i=1;i<=n;i++)
printf("\n %d->%d",v,i);
dfs(i);
void main()
int i,j,count=0;
83 Data Structures
Sri Manakula Vinayagar Engineering College, Puducherry
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
reach[i]=0;
for(j=1;j<=n;j++)
a[i][j]=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
dfs(1);
printf("\n");
for(i=1;i<=n;i++)
if(reach[i])
count++;
if(count==n)
84 Data Structures
Sri Manakula Vinayagar Engineering College, Puducherry
else
getch();
struct Node
int data;
Node(int data)
this->data = data;
if (node == NULL)
return;
printPostorder(node->left);
printPostorder(node->right);
85 Data Structures
Sri Manakula Vinayagar Engineering College, Puducherry
// now deal with the node
if (node == NULL)
return;
printInorder(node->left);
printInorder(node->right);
if (node == NULL)
return;
86 Data Structures
Sri Manakula Vinayagar Engineering College, Puducherry
cout << node->data << " ";
printPreorder(node->left);
printPreorder(node->right);
int main()
printPreorder(root);
printInorder(root);
printPostorder(root);
87 Data Structures
Sri Manakula Vinayagar Engineering College, Puducherry
return 0;
Output:
88 Data Structures