Академический Документы
Профессиональный Документы
Культура Документы
OF
Technology
PRACTICAL FILE
for
4thSemester
1|Page
1.Write a program for iterative and Recursive Binary Search.
import java.util.Scanner;
class BinarySearchRecursive
input[i] = kb.nextInt();
if (index == -1)
else
2|Page
System.out.printf("%d is found in array at index %d %n", key, index);
kb.close();
private static int binarySearch(int[] array, int start, int end, int target)
{ return -1; }
if (target == array[middle])
{ return middle; }
else if
else
3|Page
4|Page
2. Write a program for Merge Sort.
import java.util.*;
class MergeSort
int mid;
if(low<high)
mid=(low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
int h,i,j,k;
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
5|Page
if(a[h]<=a[j])
b[i]=a[h];
h++;
else
b[i]=a[j];
j++;
i++;
if(h>mid)
for(k=j;k<=high;k++)
b[i]=a[k];
i++;
else
for(k=h;k<=mid;k++)
b[i]=a[k];
6|Page
i++; } }
for(k=low;k<=high;k++) a[k]=b[k];}
public MergeSort()
int num,i;
System.out.println();
System.out.println();
System.out.println("Please Enter THE No. OF ELEMENTS you want to sort[THEN PRESS ENTER]:");
num=new Scanner(System.in).nextInt();
System.out.println();
System.out.println("Now, Please Enter the ("+ num +") nos. [THEN PRESS ENTER]:");
for(i=1;i<=num;i++)
{ a[i]=new Scanner(System.in).nextInt() ; }
merge_sort(1,num);
System.out.println();
System.out.println();
System.out.println();
for(i=1;i<=num;i++)
System.out.print(a[i]+" ");
new MergeSort();
}}
7|Page
8|Page
3. Write a program for Quick Sort.
import java.util.Scanner;
class QuickSort
int temp;
while (i <= j)
i++;
j--;
if (i <= j)
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
9|Page
i++;
j--; } }
if (low < j)
if (i < high)
{ quickSort(arr, i, high); }
int n, i;
n = scan.nextInt();
{ arr[i] = scan.nextInt();
sort(arr);
{ System.out.print(arr[i]+" ");
System.out.println();
}}
10 | P a g e
11 | P a g e
4. Write a program for Strassen’s Matrix Multiplication.
import java.io.*;
class Strassen
int n;
int[][] a, b, c;
n = Integer.parseInt(br.readLine());
a = new int[n][n];
b = new int[n][n];
c = new int[n][n];
a[i][j] = Integer.parseInt(br.readLine());
12 | P a g e
System.out.print("\n\n\5nEnter values for 2nd Matrix:\n");
b[i][j] = Integer.parseInt(br.readLine());
c = strassenMatrixMultiplication(a, b);
printArray(c);
int n = A.length;
int n1 = n+1;
a1 = new int[n1][n1];
b1 = new int[n1][n1];
c1 = new int[n1][n1];
13 | P a g e
for(int j=0; j<n; j++)
a1[i][j] =A[i][j];
b1[i][j] =B[i][j];
c1 = strassenMatrixMultiplication(a1, b1);
result[i][j] =c1[i][j];
return result;
if(n == 1)
else
14 | P a g e
divideArray(A, A11, 0 , 0);
return result;
15 | P a g e
}
int n = A.length;
return result;
int n = A.length;
return result;
public void divideArray(int[][] parent, int[][] child, int iB, int jB)
child[i1][j1] = parent[i2][j2]; } }
16 | P a g e
public void copySubArray(int[][] child, int[][] parent, int iB, int jB)
parent[i2][j2] = child[i1][j1];
int n = array.length;
System.out.println();
System.out.print(array[i][j] + "\t");
System.out.println();
System.out.println();
new Strassen(); } }
17 | P a g e
18 | P a g e
5. Write a program for optimal merge patterns.
#include<iostream>
int main()
cin>>n;
for(i=0;i<n;i++)
cin>>Elements[i];
cost[i] = 0;
i = 0;
k = 0;
i = 2;
while(i < n)
k++;
19 | P a g e
else
i = i + 2;
while(i < n)
k++;
else
i++;
i++;
k++;
for(k=0;k<n-1;k++)
20 | P a g e
cout<<cost[k]<<" ";
length = 0;
for(k=0;k<n-1;k++)
21 | P a g e
22 | P a g e
6. Write a program for Huffman coding.
import java.util.ArrayList;
class Huffmancode
{ if (!(characters.contains(msgChar[i])))
{ characters.add(msgChar[i]);
} }
{ countOfChar[x] = 0; }
if (checker == msgChar[x])
{ countOfChar[i]++; }
}}
23 | P a g e
for (int j = 0; j < countOfChar.length - 1; j++) {
countOfChar[j + 1] = temp;
characters.set(j + 1, tempChar);
} } }
if (root == null) {
root = node;
end = node;
} else {
current = root;
current = current.linker;
24 | P a g e
current.linker = node;
current.linker.linkerBack = current;
end = node;
TreeMaker(root, end);
System.out.println();
inOrder(node);
System.out.println();
preOrder(node);
char checker;
current = node;
checker = messageArray[i];
while (true) {
if (current.left.value.toCharArray()[0] == checker) {
code += "0";
break;
} else {
code += "1";
if (current.right != null) {
if (current.right.value.toCharArray()[0] == characters
.get(countOfChar.length - 1)) {
25 | P a g e
break;
current = current.right;
} else {
break;
codedString += code;
System.out.println();
if (root != null) {
System.out.print(root.value + "->");
preOrder(root.left);
preOrder(root.right);
26 | P a g e
public static void inOrder(Node root) {
if (root != null) {
inOrder(root.left);
System.out.print(root.value + "->");
inOrder(root.right);
} }
+ end.count);
node.left = end.linkerBack;
node.right = end;
end.linkerBack.linkerBack.linker = node;
node.linkerBack = end.linkerBack.linkerBack;
end = node;
end.linker = null;
System.out.print(current.value + "->");
current = current.linker;
System.out.println(current.value);
27 | P a g e
if (root.linker == end) {
node.left = root;
node.right = end;
node.linker = null;
node.linkerBack = null;
System.out.println(node.value);
newRoot = node; }
else
{ TreeMaker(root, end);
} } }
class Node
{ String value;
int count;
Node left;
Node right;
Node linker;
Node linkerBack;
this.value = value;
this.count = count;
this.left = null;
this.right = null;
this.linker = null;
this.linkerBack = null; )}
28 | P a g e
29 | P a g e
7. Write a program for minimum spanning trees using Kruskal’s
algorithm.
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
class KruskalAlgorithm
this.numberOfVertices = numberOfVertices;
30 | P a g e
public void kruskalAlgorithm(int adjacencyMatrix[][])
edge.sourcevertex = source;
edge.destinationvertex = destination;
edge.weight = adjacencyMatrix[source][destination];
adjacencyMatrix[destination][source] = MAX_VALUE;
edges.add(edge);
spanning_tree[edge.sourcevertex][edge.destinationvertex] = edge.weight;
spanning_tree[edge.destinationvertex][edge.sourcevertex] = edge.weight;
if (checkCycle.checkCycle(spanning_tree, edge.sourcevertex))
31 | P a g e
{
spanning_tree[edge.sourcevertex][edge.destinationvertex] = 0;
spanning_tree[edge.destinationvertex][edge.sourcevertex] = 0;
edge.weight = -1;
continue;
visited[edge.sourcevertex] = 1;
visited[edge.destinationvertex] = 1;
if (visited[i] == 0)
finished = false;
break;
} else
finished = true;
if (finished)
break; }
System.out.print("\t" + i);
System.out.println();
32 | P a g e
for (int source = 1; source <= numberOfVertices; source++)
System.out.print(source + "\t");
System.out.print(spanning_tree[source][destination] + "\t");
System.out.println();
int adjacency_matrix[][];
int number_of_vertices;
number_of_vertices = scan.nextInt();
adjacency_matrix[i][j] = scan.nextInt();
33 | P a g e
if (i == j)
adjacency_matrix[i][j] = 0;
continue;
if (adjacency_matrix[i][j] == 0)
adjacency_matrix[i][j] = MAX_VALUE;
kruskalAlgorithm.kruskalAlgorithm(adjacency_matrix);
scan.close();
class Edge
int sourcevertex;
int destinationvertex;
int weight;
@Override
34 | P a g e
public int compare(Edge edge1, Edge edge2)
return -1;
return 1;
return 0;
class CheckCycle
public CheckCycle()
35 | P a g e
{
adjacencyMatrix[sourcevertex][destinationvertex] =
adjacency_matrix[sourcevertex][destinationvertex];
int i = source;
visited[source] = 1;
stack.push(source);
while (!stack.isEmpty())
element = stack.peek();
i = element;
if (stack.contains(i))
cyclepresent = true;
return cyclepresent;
36 | P a g e
stack.push(i);
visited[i] = 1;
adjacencyMatrix[element][i] = 0;
adjacencyMatrix[i][element] = 0;
element = i;
i = 1;
continue;
i++;
stack.pop();
return cyclepresent;
}}
37 | P a g e
38 | P a g e
8. Write a program for minimum spanning trees using Prim’s
algorithm
import java.util.*;
class Prims
int i,j,min=999,num_edges=1,a=1,b=1,minpos_i=1,minpos_j=1;
while(num_edges < n)
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(this.cost[i][j]<min)
if(this.isVisited[i]!=0)
min=this.cost[i][j];
a=minpos_i=i;
b=minpos_j=j;
if(this.isVisited[minpos_i]==0 || this.isVisited[minpos_j]==0)
39 | P a g e
System.out.println("Edge Number \t"+num_edges+"\t from Vertex \t"+a+"\t to Vertex \t"+b+"-
mincost:"+min+" \n");
this.minimum_cost=this.minimum_cost+min;
num_edges=num_edges+1;
this.isVisited[b]=1;
this.cost[a][b]=this.cost[b][a]=999;
int nodes,i,j;
nodes = in.nextInt();
for(i=1;i<=nodes;i++)
for(j=1;j<=nodes;j++)
{ p.cost[i][j]=in.nextInt();
if(p.cost[i][j]==0)
p.cost[i][j]=999;}
p.isVisited[1]=1;
p.calc(nodes); } }
40 | P a g e
41 | P a g e
9. write a program for single sources shortest path algorithm.
import java.util.*;
class Dijkstra
int i,minpos=1,k,c,minimum;
for(i=1;i<=n;i++)
flag[i]=0;
this.distance[i]=this.cost[s][i];
c=2;
while(c<=n)
minimum=99;
for(k=1;k<=n;k++)
42 | P a g e
{
minimum=this.distance[i];
minpos=k;
flag[minpos]=1;
c++;
for(k=1;k<=n;k++)
this.distance[k]=this.distance[minpos]+this.cost[minpos][k];
int nodes,source,i,j;
nodes = in.nextInt();
for(i=1;i<=nodes;i++)
for(j=1;j<=nodes;j++)
43 | P a g e
{
d.cost[i][j]=in.nextInt();
if(d.cost[i][j]==0)
d.cost[i][j]=999;
source=in.nextInt();
d.calc(nodes,source);
System.out.println("The Shortest Path from Source \t"+source+"\t to all other vertices are : \n");
for(i=1;i<=nodes;i++)
if(i!=source)
44 | P a g e
45 | P a g e
10. write a program for Floyd Warshall algorithm.
import java.util.Scanner;
class FloydWarshall
this.numberofvertices = numberofvertices;
distancematrix[source][destination] = adjacencymatrix[source][destination];
46 | P a g e
{
if (distancematrix[source][intermediate] + distancematrix[intermediate][destination]
< distancematrix[source][destination])
distancematrix[source][destination] = distancematrix[source][intermediate]
+ distancematrix[intermediate][destination];
System.out.print("\t" + source);
System.out.println();
System.out.print(source + "\t");
System.out.print(distancematrix[source][destination] + "\t");
System.out.println();
47 | P a g e
}
int adjacency_matrix[][];
int numberofvertices;
numberofvertices = scan.nextInt();
adjacency_matrix[source][destination] = scan.nextInt();
if (source == destination)
adjacency_matrix[source][destination] = 0;
continue;
if (adjacency_matrix[source][destination] == 0)
48 | P a g e
{
adjacency_matrix[source][destination] = INFINITY;
floydwarshall.floydwarshall(adjacency_matrix);
scan.close();
49 | P a g e
50 | P a g e
11. Write a program for traveling salesman problem.
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Stack;
class TSPNearestNeighbour
public TSPNearestNeighbour()
numberOfNodes = adjacencyMatrix[1].length - 1;
visited[1] = 1;
stack.push(1);
51 | P a g e
boolean minFlag = false;
System.out.print(1 + "\t");
while (!stack.isEmpty())
element = stack.peek();
i = 1;
min = Integer.MAX_VALUE;
min = adjacencyMatrix[element][i];
dst = i;
minFlag = true;
i++;
if (minFlag)
visited[dst] = 1;
stack.push(dst);
52 | P a g e
System.out.print(dst + "\t");
minFlag = false;
continue;
stack.pop();
int number_of_nodes;
try
number_of_nodes = scanner.nextInt();
adjacency_matrix[i][j] = scanner.nextInt();
53 | P a g e
}
adjacency_matrix[j][i] = 1;
tspNearestNeighbour.tsp(adjacency_matrix);
scanner.close();
54 | P a g e
55 | P a g e
12. Write a program for Hamiltonian cycle problem.
import java.util.Scanner;
import java.util.Arrays;
class1 HamiltonianCycle
V = g.length;
Arrays.fill(path, -1);
graph = g;
try
path[0] = 0;
pathCount = 1;
solve(0);
System.out.println("No solution");
56 | P a g e
}
catch (Exception e)
System.out.println(e.getMessage());
display();
if (pathCount == V)
return;
if (graph[vertex][v] == 1 )
path[pathCount++] = v;
graph[vertex][v] = 0;
graph[v][vertex] = 0;
57 | P a g e
if (!isPresent(v))
solve(v);
graph[vertex][v] = 1;
graph[v][vertex] = 1;
path[--pathCount] = -1;
if (path[i] == v)
return true;
return false;
System.out.print("\nPath : ");
58 | P a g e
System.out.print(path[i % V] +" ");
System.out.println();
int V = scan.nextInt();
System.out.println("\nEnter matrix\n");
graph[i][j] = scan.nextInt();
hc.findHamiltonianCycle(graph);
}}
59 | P a g e
60 | P a g e