Professional Documents
Culture Documents
New Spanning Tree
New Spanning Tree
AIM :
To write the program for implementation of Mergesort and Quicksort-Analysis
ALGORITHM:
Step 2: Import the necessary header files to get input by using array.
Step 3: Create a class as MergeSort and function as merge in that check the condition
if(list.length<=1) satisfies then return list.
Step 4: Declaration of function merge need to invoke and define by checking the loop with if
condition then print array copy.
Step 5: Create a main function as public static void main(String args[]) throws Exception.
Step 6: Introduce an object as Scanner scan = new Scanner( System.in ); and get an input through
BufferedReader.
Step 7: Print as “Enter number of integer elements” and “Elements after sorting”
Step 8: Sorting need to be carried out based on the rule of Merge sort and Quick sort.
package mergesort;
import java.util.Scanner;
public class MergeSort
{
public static void sort(int[] a, int low, int high)
{
int N = high - low;
if (N <= 1)
return;
int mid = low + N/2;
sort(a, low, mid);
sort(a, mid, high);
int[] temp = new int[N];
int i = low, j = mid;
for (int k = 0; k < N; k++)
{
if (i == mid)
temp[k] = a[j++];
else if (j == high)
temp[k] = a[i++];
else if (a[j]<a[i])
temp[k] = a[j++];
else
temp[k] = a[i++];
}
for (int k = 0; k < N; k++)
a[low + k] = temp[k];
}
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in );
System.out.println("Merge Sort Test\n");
int n, i;
System.out.println("Enter number of integer elements");
n = scan.nextInt();
int arr[] = new int[ n ];
System.out.println("\nEnter "+ n +" integer elements");
for (i = 0; i < n; i++)
arr[i] = scan.nextInt();
sort(arr, 0, n);
System.out.println("\nElements after sorting ");
for (i = 0; i < n; i++)
System.out.print(arr[i]+" ");
System.out.println();
}
}
OUTPUT:
Merge Sort Test
package quicksort;
import java.util.Scanner;
public class QuickSort
{
public static void sort(int[] arr)
{
quickSort(arr, 0, arr.length - 1);
}
public static void quickSort(int arr[], int low, int high)
{
int i = low, j = high;
int temp;
int pivot = arr[(low + high) / 2];
while (i <= j)
{
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
if (low < j)
quickSort(arr, low, j);
if (i < high)
quickSort(arr, i, high);
}
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in );
System.out.println("Quick Sort Test\n");
int n, i;
System.out.println("Enter number of integer elements");
n = scan.nextInt();
int arr[] = new int[ n ];
System.out.println("\nEnter "+ n +" integer elements");
for (i = 0; i < n; i++)
arr[i] = scan.nextInt();
sort(arr);
System.out.println("\nElements after sorting ");
for (i = 0; i < n; i++)
System.out.print(arr[i]+" ");
System.out.println();
}
}
OUTPUT:
Quick Sort Test
RESULT:
Thus the program for the implementation of Mergesort and Quicksort has been
executed and output is verified.
EX.NO: 2 IMPLEMENTATION OF A BINARY SEARCH TREE
DATE:
AIM :
To write the program for implementation of a binary search tree.
ALGORITHM:
Step 2: Import the necessary header files to get input by using array.
Step 5: Create a main function as public static void main(String args[]) throws Exception.
Step 6: Introduce an object as BST bst= new BST(); and get an input through BufferedReader.
Step 7: Print as “Binary Search Tree Test”and “Binary Search Tree Operations”.
RESULT:
Thus the program for the implementation of a binary search tree has been executed and
output is verified.
EX.NO: 3 RED-BLACK TREE IMPLEMENTATION
DATE:
AIM :
To write the program for implementation of a red-black tree .
ALGORITHM:
Step 2: Import the necessary header files to get input by using array.
Step 5: Create a main function as public static void main(String args[]) throws Exception.
Step 7: Print as “Red Black Tree Test”and “Red Black Tree Operations”.
RESULT:
Thus the program for the implementation of red-black tree has been executed and
output is verified.
EX.NO: 4 HEAP IMPLEMENTATION
DATE:
AIM:
To write a program for heap implementation.
ALGORITHM:
Step 2: Import the necessary header files to get input by using array.
Step 3: Create a class heap implimentation function in that check the condition
satisfies the list.
Step 4: Declaration of heap function need to invoke and define by checking the loop with if
condition then print array copy.
Step 5: Create a main function as public static void main(String args[]) throws Exception.
Step 6: Print as “Please enter the list of elements,one element per line”and “write 'STOP' when
list is completed”
RESULT: Thus the program for heap implementation has been executed and output is verified.
EX.NO: 5 FIBONACCI HEAP IMPLEMENTATION
DATE:
AIM:
To write a program for the fibonacci heap implementation.
ALGORITHM:
Step 2: Import the necessary header files to get input by using array.
Step 4: Create a main function as public static void main(String args[]) throws Exception.
Step 6: Print as “Please enter the list of elements,one element per line”and “write 'STOP' when
list is completed”
FibonacciHeap Operations
1. insert element
2. check empty
3. clear
1
Enter element
24
Heap = 24
Do you want to continue (Type y or n)
y
FibonacciHeap Operations
1. insert element
2. check empty
3. clear
1
Enter element
6
Heap = 6 24
Do you want to continue (Type y or n)
y
FibonacciHeap Operations
1. insert element
2. check empty
3. clear
1
Enter element
28
Heap = 6 28 24
Do you want to continue (Type y or n)
y
FibonacciHeap Operations
1. insert element
2. check empty
3. clear
1
Enter element
14
Heap = 6 14 28 24
Do you want to continue (Type y or n)
y
FibonacciHeap Operations
1. insert element
2. check empty
3. clear
1
Enter element
63
Heap = 6 63 14 28 24
Do you want to continue (Type y or n)
y
FibonacciHeap Operations
1. insert element
2. check empty
3. clear
2
Empty status = false
Heap = 6 63 14 28 24
Do you want to continue (Type y or n)
y
FibonacciHeap Operations
1. insert element
2. check empty
3. clear
2
Empty status = true
Heap = Empty
Do you want to continue (Type y or n)
n
RESULT: Thus the program for the fibonacci heap implementation has been executed and output
is verified.
EX.NO: 6 GRAPH TRAVERSALS
DATE:
AIM:
To write a program for the implementation of graph traversals
ALGORITHM:
Step 2: Import the necessary header files to get input by using array.
Step 4: Create a main function as public static void main(String args[]) throws Exception.
Step 6: Print as “Please enter the list of elements,one element per line”and “write 'STOP' when
list is completed”
RESULT: Thus the program for the implementation of graph traversals has been executed and
output is verified.
EX.NO: 7 SPANNING TREE IMPLEMENTATION
DATE:
AIM:
To write a program for the implementation of spanning tree.\
ALGORITHM:
Step 2: Import the necessary header files to get input by using array.
Step 3: Create a class spanning tree function in that check the condition
satisfies the list.
Step 4: Declaration of function prims need to invoke and define by checking the loop with if
condition then print array copy.
Step 5: Create a main function as public static void main(String args[]) throws Exception.
Step 6: Print as “Please enter the list of elements,one element per line”and “write 'STOP' when
list is completed”
Step 7: Sorting need to be carried out based on the rule of prims algorithm.
unsettled.add(source);
distances[source] = 0;
while (!unsettled.isEmpty())
{
evaluationNode = getNodeWithMinimumDistanceFromUnsettled();
unsettled.remove(evaluationNode);
settled.add(evaluationNode);
evaluateNeighbours(evaluationNode);
}
}
private int getNodeWithMinimumDistanceFromUnsettled()
{
int min ;
int node = 0;
RESULT: Thus the program for the implementation of spanning tree has been executed and
output is verified.
EX.NO: 8 SHORTEST PATH ALGORITHM (DIJIKSTRA’S
ALGORITHM,BELLMAN FORD ALGORITHM)
DATE:
AIM:
To write a program for the implementation of shortest path algorithm (dijikstra’s
algorithm,bellman ford algorithm)
ALGORITHM:
Step 2: Import the necessary header files to get input by using array.
Step 4: Declaration of function merge need to invoke and define by checking the loop with if
condition then print array copy.
Step 5: Create a main function as public static void main(String args[]) throws Exception.
Step 7: Print as “Please enter the list of elements,one element per line”and “write 'STOP' when
list is completed”
Step 8: Sorting need to be carried out based on the rule of shortest path algorithm.
import java.util.Scanner;
$javac BellmanFord.java
$java BellmanFord
Enter the number of vertices
6
Enter the adjacency matrix
0 4 0 0 -1 0
0 0 -1 0 -2 0
000000
000000
0 0 0 -5 0 3
000000
distance of source 1 to 1 is 0
distance of source 1 to 2 is 4
distance of source 1 to 3 is 3
distance of source 1 to 4 is -6
distance of source 1 to 5 is -1
distance of source 1 to 6 is 2
RESULT: Thus the program for the implementation of shortest path algorithm (dijikstra’s
algorithm,bellman ford algorithm) has been executed and output is verified.
EX.NO: 9 IMPLEMENTATION OF MATRIX CHAIN MULTIPLICATION
DATE:
AIM:
To write a program for the implementation of matrix chain multiplication
ALGORITHM:
Step 2: Import the necessary header files to get input by using array.
Step 4: Declaration of function need to invoke and define by checking the loop with if condition
then print array copy.
Step 5: Create a main function as public static void main(String args[]) throws Exception.
Step 6: Print as “Please enter the list of elements,one element per line”and “write 'STOP' when
list is completed”
import java.util.Scanner;
public class OptimalParanthesizationUsingDP
{
private int[][] m;
private int[][] s;
private int n;
public OptimalParanthesizationUsingDP(int[] p)
{
n = p.length - 1;
m = new int[n + 1][n + 1];
s = new int[n + 1][n + 1];
matrixChainOrder(p);
}
$ javac OptimalParanthesizationUsingDP.java
$ java OptimalParanthesizationUsingDP
Enter the array p[], which represents the chain of matrices such that the ith matrix Ai is of
dimension p[i-1] x p[i]
Enter the total length:
5
Enter the dimensions:
24521
Matrices are of order:
A1-->2x4
A2-->4x5
A3-->5x2
A4-->2x1
(A[1](A[2](A[3]A[4])))
RESULT: Thus the program for the implementation of matrix chain multiplication has been
executed and output is verified.
EX.NO: 10 Activity Selection and Huffman Coding Implementation.
DATE:
AIM:
To write a program for the implementation of Huffman coding.
ALGORITHM:
Step 2: Import the necessary header files to get input by using array.
Step 4: Declaration of function need to invoke and define by checking the loop with if condition
then print array copy.
Step 5: Create a main function as public static void main(String args[]) throws Exception.
Step 6: Print as “Please enter the list of elements,one element per line”and “write 'STOP' when
list is completed”
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Comparator;
class HuffmanNode
{
int data;
char c;
HuffmanNode left;
HuffmanNode right;
}
class MyComparator implements Comparator<HuffmanNode> {
public int compare(HuffmanNode x, HuffmanNode y)
{
return x.data - y.data;
}
}
public class Huffman
{
public static void printCode(HuffmanNode root, String s)
{
if (root.left == null&& root.right == null&& Character.isLetter(root.c))
{
System.out.println(root.c + ":" + s);
return;
}
printCode(root.left, s + "0");
printCode(root.right, s + "1");
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int n = 6;
char[] charArray = { 'a', 'b', 'c', 'd', 'e', 'f' };
int[] charfreq = { 5, 9, 12, 13, 16, 45 };
PriorityQueue<HuffmanNode> q = new PriorityQueue<HuffmanNode>(n, new
MyComparator());
for (int i = 0; i < n; i++)
{
HuffmanNode hn = new HuffmanNode();
hn.c = charArray[i];
hn.data = charfreq[i];
hn.left = null;
hn.right = null;
q.add(hn);
}
HuffmanNode root = null;
while (q.size() > 1) {
HuffmanNode x = q.peek();
q.poll();
HuffmanNode y = q.peek();
q.poll();
HuffmanNode f = new HuffmanNode();
f.data = x.data + y.data;
f.c = '-';
f.left = x;
f.right = y;
root = f;
q.add(f);
}
printCode(root, "");
}
}
Output:
f: 0
c: 100
d: 101
a: 1100
b: 1101
e: 111
RESULT: Thus the program for the activity selection and huffman coding implementation.