CS402 - Lab Manual - ADA
CS402 - Lab Manual - ADA
LAB MANUAL
NAME:
ENROLLMENT NUMBER:
SESSION: 2018-19
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
Vision of Institute:
Initially to seek autonomy and eventually grow the Institute into a renowned University by:
Imparting the best technical and professional education to the students of the
Institute.
Developing all the Departments of the Institute as Centers of Excellence.
Creating the most congenial and cordial environment of Teaching, Learning
and Research in the Institute.
Conceiving world - class Education, Ethics and Employability for students in
global perspective.
Mission of Institute
To explore and ensure the best environment to transform students into creative,
knowledgeable, principled engineers and managers compatible with their abilities in ever-
changing socio-economic and competitive scenario by:
Imparting intensive teaching and training through latest technology
Motivating the teachers for higher learning and innovative research activities
with social services.
Generating maximum opportunities for placement of students in National,
Multi-National companies and nurturing entrepreneurship quality.
Producing highly intellectual citizens through technical education to constitute
an elegant society and meeting social challenges.
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
Vision
Program Objectives
PSO 1- Able to apply the knowledge gained during the course of the program from
Mathematics, Science & Engineering to solve computation task, and ability to
understand, analyze, design and develop computer programs in the areas related to
algorithms, system software, multi-media, web designing, data analytics and networking.
(PO1, PO2, PO3)
PSO 2- Able to comprehend the technological advancements by lifelong learning and usage
of modern tools for analysis and design of application and system software with varying
complexity. (PO4, PO5, PO12)
PSO 3- Able to communicate effectively in oral and written forms with good leadership
quality, team work, managerial skill to work in multi-disciplinary environment, involve in
development of commercially viable projects, demonstrating the practice of professional
ethics for sustainable development of society. (PO6, PO7, PO8, PO9, PO10, PO11)
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
COURSE OUTCOMES
Once the student has successfully completed this course, he/she will be able to:
CO1: Understand & formulate the time-complexity analysis for an algorithm and analysis
and design of divide & conquer problem solving approach for sorting and searching method.
CO2: understand the greedy strategy & apply it on the problems like optimal merge
patterns, minimum spanning tree & single source shortest path algorithm.
CO3: understand the concept of dynamic programming approach to solve problems &
compare difference between dynamic programming & greedy strategy .
CO4: Understand and design parallel algorithms & understand solutions to problems based
on backtracking approach & also branch & bound approach.
CO5: Understand, Design & implement traversals on trees & searches on graphs and NP
completeness
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
Type of Cov
Sn Name of Experiment Uni Exp. Cat. C PO PS ere Sig
o
t O O d n
Dat
e
1 Write a program for Iterative I Design Core 1 1,2,3,4,5,12 1,2
and Recursive Binary Search.
2 Write a program for Merge I Design Core 1 1,2,3,4,5,12 1,2
Sort.
3 Write a program for Quick I Design Core 1 1,2,3,4,5,12 1,2
Sort.
4 Write a program for I Design Core 1 1,2,3,4,5,12 1,2
Strassen’s Matrix
Multiplication.
5 Write a program for optimal II Design Core 2 1,2,3,4,5,12 1,2
merge patterns.
6 Write a program for II Design Core 2 1,2,3,4,5,12 1,2
Huffman coding.
7 Write a program for II Design Core 2 1,2,3,4,5,12 1,2
minimum spanning trees
using Kruskal’s algorithm.
8 Write a program for II Design Core 2 1,2,3,4,5,12 1,2
minimum spanning trees
using Prim’s algorithm.
9 Write a program for single II Design Core 2 1,2,3,4,5,12 1,2
sources shortest path
algorithm.
10 Write a program for Floye- III Design Core 3 1,2,3,4,5,12 1,2
Warshal algorithm.
11 Write a program for IV Design Core 4 1,2,3,4,5,12 1,2
traveling salesman problem.
Type of Cov
Sn Name of Experiment Uni Exp. Cat. C PO PS ere Sig
o
t O O d n
Dat
e
1 Write a program for Iterative I Design Core 1 1,2,3,4,5,12 1,2
and Recursive Binary Search.
2 Write a program for Merge I Design Core 1 1,2,3,4,5,12 1,2
Sort.
3 Write a program for Quick I Design Core 1 1,2,3,4,5,12 1,2
Sort.
4 Write a program for I Design Core 1 1,2,3,4,5,12 1,2
Strassen’s Matrix
Multiplication.
5 Write a program for optimal II Design Core 2 1,2,3,4,5,12 1,2
merge patterns.
6 Write a program for II Design Core 2 1,2,3,4,5,12 1,2
Huffman coding.
7 Write a program for II Design Core 2 1,2,3,4,5,12 1,2
minimum spanning trees
using Kruskal’s algorithm.
8 Write a program for II Design Core 2 1,2,3,4,5,12 1,2
minimum spanning trees
using Prim’s algorithm.
9 Write a program for single II Design Core 2 1,2,3,4,5,12 1,2
sources shortest path
algorithm.
10 Write a program for Floye- III Design Core 3 1,2,3,4,5,12 1,2
Warshal algorithm.
11 Write a program for IV Design Core 4 1,2,3,4,5,12 1,2
traveling salesman problem.
Sr.
No Topics/Sub Topics Date Grade Sign Remark
.
Write a program for Iterative and Recursive
1
Binary Search.
3 Write a program for Merge Sort.
Experiment-01
ABOUT THE EXPERIMENT: Binary search compares the target value to the middle
element of the array. If they are not equal, the half in which the target cannot lie is
eliminated and the search continues on the remaining half, again taking the middle element
to compare to the target value, and repeating this until the target value is found. If the
search ends with the remaining half being empty, the target is not in the array.
if A[midPoint] < x
set lowerBound = midPoint + 1
if A[midPoint] > x
set upperBound = midPoint - 1
if A[midPoint] = x
PROGRAM CODE:
#include <stdio.h>
int main()
{ int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break; }
else
last = middle - 1;
middle = (first + last)/2; }
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
return 0; }
OUTPUT:
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
Experiment-02
ABOUT THE EXPERIMENT: Merge Sort is a Divide and Conquer algorithm. It divides
input array in two halves, calls itself for the two halves and then merges the two sorted
halves. The merge() function is used for merging two halves. The merge(arr, l, m, r) is
key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two
sorted sub-arrays into one.
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
Algorithm: MergeSort(arr[], l, r)
If r > l
1. Find the middle point to divide the array into two halves:
middle m = (l+r)/2
2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)
Program Code:
#include <stdio.h>
#define max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];
void merging(int low, int mid, int high) {
int l1, l2, i;
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++]; }
while(l1 <= mid)
b[i++] = a[l1++];
while(l2 <= high)
b[i++] = a[l2++];
for(i = low; i <= high; i++)
a[i] = b[i]; }
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
Experiment-03
ABOUT THE EXPERIMENT: In quick sort ,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 sub-
arrays.
Algorithm:
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
Program Code:
include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp; } }
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last); } }
int main(){
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0; }
Output:
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
Experiment-04
Program Code:
#include<stdio.h>
int main(){
scanf("%d", &a[i][j]);
scanf("%d", &b[i][j]);
printf("\n");
printf("%d\t", a[i][j]); }
printf("\n");
printf("%d\t", b[i][j]); }
c[0][1] = m3 + m5;
c[1][0] = m2 + m4;
c[1][1] = m1 - m2 + m3 + m6;
printf("\n");
printf("%d\t", c[i][j]); }
return 0; }
12
34
56
78
1 2
3 4
5 6
7 8
19 22
43 50
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
Experiment-05
Algorithm:
Algorithm Tree(n)
Return least(list); }
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
Scanf(“%d”,&n);
for(i=0;i<n;i++)
{printf("\t");
Scanf(a[i]);}
i=0;
k=0;
c[k]=a[i]+a[i+1];
i=2;
while(i<n)
{k++;
if((c[k-1]+a[i])<=(a[i]+a[i+1]))
{c[k]=c[k-1]+a[i];}
else{c[k]=a[i]+a[i+1];
i=i+2;
while(i<n)
{ k++;
if((c[k-1]+a[i])<=(c[k-2]+a[i]))
{c[k]=c[k-1]+a[i];}
else{c[k]=c[k-2]+a[i];}
i++;}}
i++;}
k++;
c[k]=c[k-1]+c[k-2];
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
for(k=0;k<n-1;k++)
{printf(“%d\t”,c[k]";}
l=0;
for(k=0;k<n-1;k++)
{l=l+c[k];}
getch();}
Output:
Enter the sorted elements for optimal merge pattern 20,30, 10, 5 ,30
Experiment-06
ABOUT THE EXPERIMENT: This algorithm is basically a coding technique for encoding
data such encoded data is used in data compression technique.
Algorithm:
Huffman(c)
{q=c;
For(i=1;i<=n-1;i++)
{temp=getnode();
Left[temp]=getmin(q);
right[temp]=getmin(q);
q=left[temp];
b=roght[temp];
f[temp]=a+b;
insert(a,temp);
} return getmin(q);}
Program Code:
#include<string.h>
#include<stdio.h>
{ char ch;
int freq;
}node;
node * heap[100];
int heapSize=0;
{ heapSize++;
heap[heapSize] = element;
{ heap[now] = heap[now/2];
now /= 2;
} heap[now] = element;
}node * DeleteMin()
{ node * minElement,*lastElement;
int child,now;
minElement = heap[1];
lastElement = heap[heapSize--];
{ child = now*2;
{ child++;}
{ heap[now] = heap[child];
} else
{ break; } }
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
heap[now] = lastElement;
return minElement; }
return; }
char leftcode[10],rightcode[10];
strcpy(leftcode,code);
strcpy(rightcode,code);
leftcode[length] = '0';
leftcode[length+1] = '\0';
rightcode[length] = '1';
rightcode[length+1] = '\0';
print(temp->left,leftcode);
print(temp->right,rightcode);
} int main()
heap[0]->freq = 0;
int n ;
scanf("%d",&n);
char ch;
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
int freq,i;
for(i=0;i<n;i++)
{ scanf(" %c",&ch);
scanf("%d",&freq);
Insert(temp);
} if(n==1)
return 0; }
for(i=0;i<n-1 ;i++)
temp -> ch = 0;
Insert(temp);
char code[10];
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
code[0] = '\0';
print(tree,code);
Output:
f: 0
c: 100
d: 101
a: 1100
b: 1101
e: 111
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
Experiment-07
AIM : Write a program for minimum spanning trees using Kruskal’s algorithm.
Algorithm: MST-KRUSKAL(G, w)
A←Ø
do MAKE-SET(v)
do if FIND-SET(u) ≠ FIND-SET(v)
UNION(u, v)
return A
Program Code:
#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];
int find(int);
int uni(int,int);
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
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++)
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();
int find(int i)
while(parent[i])
i=parent[i];
return i;
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
if(i!=j)
parent[j]=i;
return 1;
return 0;
}
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
Experiment-08
AIM : Write a program for minimum spanning trees using Prim’s algorithm
Algorithm: MST-PRIM(G, w, r)
do key[u] ← ∞
π[u] ← NIL
key[r] ← 0
Q ← V [G]
while Q ≠ Ø
do u ← EXTRACT-MIN(Q)
then π[v] ← u
key[v] ← w(u, v)
Program Code:
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
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; }
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)
mincost+=min;
visited[b]=1;}
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
cost[a][b]=cost[b][a]=999;}
getch();}
Output:
Experiment-09
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
ABOUT THE EXPERIMENT: in single source shortest path problem the shortest
distance from a single vertex called source is obtained.
Algorithm:
DIJKSTRA(G,s)
INITIALIZE-SINGLE-SOURCE(G, S)
S←Ø
Q ← V[G]
while Q ≠ Ø
do u ← EXTRACT-MIN(Q)
S ← S U {u}
dist[s] = 0;
dist[v] ← ∞
Program Code:
#include<stdio.h>
#include<conio.h>
#define MAX 10
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
void main(){
int G[MAX][MAX], i, j, n, u;
clrscr();
scanf("%d", &n);
scanf("%d", &G[i][j]);
scanf("%d", &u);
dijikstra(G,n,u);
getch(); }
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];
for(i=0;i< n;i++)
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
{ distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0; }
distance[startnode]=0;
visited[startnode]=1;
count=1;
mindistance=INFINITY;
{ mindistance=distance[i];
nextnode=i; }
visited[nextnode]=1;
if(!visited[i])
{ distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode; }
count++; }
if(i!=startnode)
j=i;
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
do
{ j=pred[j];
while(j!=startnode); } }
Output:
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
Experiment-10
Algorithm:
dist[v][v] ← 0
end if
Program Code:
#include<stdio.h>
int i, j, k,n,dist[10][10];
void floydWarshell ()
} int main()
{ int i,j;
scanf("%d",&n);
printf("\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{ printf("dist[%d][%d]:",i,j);
scanf("%d",&dist[i][j]); }
floydWarshell();
printf (" \n\n shortest distances between every pair of vertices \n");
printf("\n"); }
return 0; }
Output:-
enter no of vertices :4
dist[0][0]:0
dist[0][1]:8
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
dist[0][2]:999
dist[0][3]:1
dist[1][0]:999
dist[1][1]:0
dist[1][2]:1
dist[1][3]:999
dist[2][0]:4
dist[2][1]:999
dist[2][2]:0
dist[2][3]:999
dist[3][0]:999
dist[3][1]:2
dist[3][2]:9
dist[3][3]:0
0 3 4 1
5 0 1 6
4 7 0 5
7 2 3 0
--------------------------------
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
Experiment-11
ABOUT THE EXPERIMENT: If there are N cities and cost of travelling from any city to
any other city is given then we have to obtain the cheapest round trip such that each city is
visited exactly once and then returning to the starting city , complete the tour.
Program Code:
#include<stdio.h>
int ary[10][10],completed[10],n,cost=0;
void takeInput()
{ int i,j;
scanf("%d",&n);
scanf("%d",&ary[i][j]);
completed[i]=0; }
{printf("\n");
printf("\t%d",ary[i][j]); } }
{int i,ncity;
completed[city]=1;
printf("%d--->",city+1);
ncity=least(city);
if(ncity==999)
{ ncity=0;
printf("%d",ncity+1);
cost+=ary[city][ncity];
return; }
mincost(ncity);}
int least(int c)
{ int i,nc=999;
int min=999,kmin;
{ if((ary[c][i]!=0)&&(completed[i]==0))
{ min=ary[i][0]+ary[c][i];
kmin=ary[c][i];
nc=i; } }
if(min!=999)
cost+=kmin;
return nc;}
int main()
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
{ takeInput();
return 0; }
Output
Minimum cost is 7
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
Experiment-12
ABOUT THE EXPERIMENT: given an undirected connected graph and two nodes X and
Y then find a path from X to Y visiting each node in the graph exactly y once.
In the mathematical field of graph theory the Hamiltonian path problem and
the Hamiltonian cycle problem are problems of determining whether a Hamiltonian path (a
path in an undirected or directed graph that visits each vertex exactly once) or
a Hamiltonian cycle exists in a given graph (whether directed or undirected).
Algorithm:
Algo_isValid(v, k)
Begin
return false
return false
End
cycleFound(node k)
Begin
return true
else
return false;
return true
done
return false
End
Program Code:
include<iostream>
#define NODE 5
int graph[NODE][NODE] = {
{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 1},
{0, 1, 1, 1, 0},
}; /* int graph[NODE][NODE] = {
{0, 1, 0, 1, 0},
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 0},
{0, 1, 1, 0, 0},
}; */
int path[NODE];
void displayCycle() {
cout<<"Cycle: ";
cout << path[0] << endl; //print the first vertex again
return false;
for (int i = 0; i < k; i++) //if vertex is already taken, skip that
if (path[i] == v)
return false;
return true; }
bool cycleFound(int k) {
if (graph[path[k-1]][ path[0] ] == 1 )
return true;
else
return false;
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
} for (int v = 1; v < NODE; v++) { //for all vertices except starting point
path[k] = v;
return true;
} } return false; }
bool hamiltonianCycle() {
path[i] = -1;
if ( cycleFound(1) == false ) {
return false; }
displayCycle();
return true; }
int main() {
hamiltonianCycle();
Output
Cycle: 0 1 2 4 3 0
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
Experiment-13
Algorithm:
begin
repeat
DFS g
until solved;
end.
function DepthFirstSearch (n, bound): integer; //if returns next cost bound g
begin
end;
new bound := 1;
b := c(n; ni) + DepthFirstSearch (ni; bound c(n; ni)); //f search deeper g
new bound := min (new bound; b); //f compute next iteration's bound g
end;
iteration's bound g
end;
Program Code:
#include<stdio.h>
#include<conio.h>
int m=0,n=4;
{ int i,j,m=0;
{ if(temp[i][j]!=t[i][j])
m++; }
return m;}
{ int i,j,f=1;
if(a[i][j]!=t[i][j])
f=0;
return f; }
void main()
{ int p,i,j,n=4,a[10][10],t[10][10],temp[10][10],r[10][10];
int m=0,x=0,y=0,d=1000,dmin=0,l=0;
clrscr();
scanf("%d",&a[i][j]);
scanf("%d",&t[i][j]);
printf("%d\t",a[i][j]);
printf("\n"); }
printf("%d\t",t[i][j]);
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
printf("\n"); }
while(!(check(a,t)))
{ l++;
d=1000;
{if(a[i][j]==0)
{x=i;
y=j; } }
temp[i][j]=a[i][j];
if(x!=0)
{ p=temp[x][y];
temp[x][y]=temp[x-1][y];
temp[x-1][y]=p; }
m=cal(temp,t);
dmin=l+m;
if(dmin < d)
{ d=dmin;
r[i][j]=temp[i][j]; }
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
temp[i][j]=a[i][j];
if(x!=n-1)
{ p=temp[x][y];
temp[x][y]=temp[x+1][y];
temp[x+1][y]=p; }
m=cal(temp,t);
dmin=l+m;
if(dmin < d)
{ d=dmin;
r[i][j]=temp[i][j]; }
temp[i][j]=a[i][j];
if(y!=n-1)
{ p=temp[x][y];
temp[x][y]=temp[x][y+1];
temp[x][y+1]=p; }
m=cal(temp,t);
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
dmin=l+m;
if(dmin < d)
{ d=dmin;
r[i][j]=temp[i][j]; }
temp[i][j]=a[i][j];
if(y!=0)
{ p=temp[x][y];
temp[x][y]=temp[x][y-1];
temp[x][y-1]=p;}
m=cal(temp,t);
dmin=l+m;
if(dmin < d)
{ d=dmin;
r[i][j]=temp[i][j]; }
printf("%d\t",r[i][j]);
printf("\n"); }
{ a[i][j]=r[i][j];
temp[i][j]=0; }
getch(); }
1 2 3 4 5 6 0 8 9 10 7 11 13 14 15 12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0
Entered Matrix is :
1 2 3 4
5 6 0 8
9 10 7 11
13 14 15 12
Target Matrix is :
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
1 2 3 4
Gyan Ganga College of Technology, Jabalpur
Computer Science & Engineering
5 6 7 8
9 10 0 11
13 14 15 12
Minimum cost : 4
1 2 3 4
5 6 7 8
9 10 11 0
13 14 15 12
Minimum cost : 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
Minimum cost : 3