VIT Bhopal University: Practical File Fall 2020-2021
VIT Bhopal University: Practical File Fall 2020-2021
University
School of Computing Science & Engineering
Practical File
Fall 2020-2021
Code: CSD-3001
Submitted By:
Kunal Chandra, 19BCY10028
Submitted To:
S. No.
Experiment Page No.
Write the Algorithm and develop a program with menu for various
1 3
operations (push, pop, display, peek) of Stack.
5 Implement Singly Linked List and Doubly Linked List operations. 13,15,17
8 21
Write down the algorithm and program for each of the following.
1. Sequential/Linear Search 21
2. Bubble Sort 22
3. Selection Sort.
4. Insertion Sort. 23
Write the algorithm and Implement the program to find the Optimal order 25
of the parenthesis for the Matrix Chain Multiplication with Minimum cost
12 using Dynamic Programming Strategy.
Implement Dijkstra's and Bellman Ford Algorithms for Single Source all
18 destination pair Shortest Path finding in a weighted Graph 36,37,39
1)
import java.util.*;
class stack //class body starts
{
static Scanner sc=new Scanner(System.in); //Static Input Stream
static int top=-1,n,a[]; // static global variables
else
{
a[++top]=x; //inserting the element into the stack
}
}
void pop()
{
if(isEmpty()==true) //checking if stack is empty
System.out.println("Stack is underflow");
else
System.out.println("Popped: "+a[top--]); //popping the topmost
element from the stack
}
void peek()
{
if(isEmpty()==true)
System.out.println("Nothing to peek");
else
System.out.println("Peeking at: "+a[top]);
}
2.1)
//POSTFIX EVALUATION
import java.util.*;
class postfix // CLASS BODY BEGINS
{
static Scanner sc=new Scanner(System.in); //STATIC INPUT STREAM
static int top=-1,n; //STATIC GLOVBAL VARIABLES
static String a[]; //STRING ARRAY
else
a[++top]=x; //INSERTING THE ELEMENT INTO THE STACK
}
else
return(a[top--]);
}
if(op=="+")
{
r=Integer.parseInt(op2)+Integer.parseInt(op1); //IMPLICIT
STRING TO INTEGER CONVERTION FOR THE PURPOSE OF CALCULATION
}
else if(op=="-")
{
r=Integer.parseInt(op2)-Integer.parseInt(op1);
}
else if(op=="*")
{
r=Integer.parseInt(op2)*Integer.parseInt(op1);
}
else if(op=="/")
{
r=Integer.parseInt(op2)/Integer.parseInt(op1);
}
return (Integer.toString(r)); //AGAIN IMPLICITELY CONVERTING
THE RESULTANT VALUE BACK TO THE STRING TYPE AND RETURNING TO THE STACK
}
obj.push("2");
obj.push("3");
obj.push("1");
obj.push("*"); //FUNCTIONS CALLING
obj.push("+");
obj.push("9");
obj.push("-");
obj.show();
} //MAIN BODY ENDS
} //CLASS BODY ENDS
2.2)
import java.util.*;
class inf_post // CLASS BODY BEGINS
{
static Scanner sc=new Scanner(System.in); //STATIC INPUT STREAM
static int top=-1,n; //STATIC GLOVBAL VARIABLES
static String a[],p=""; //STRING ARRAY
if(top==n-1)
System.out.println("Stack is overflow");
else //A+B*C/(E-F)
{
if(x.equals("("))
a[++top]=x;
else
{ //CHECKS THE
PRECEDENCE OF THE INPUT VALUE WITH THE TOP VALUE OF THE STACK
f=pre(x);
d=Integer.parseInt(f);
s=pre(a[top]);
b=Integer.parseInt(s);
}
}
else if(d>b) //INPUT PRECEDENCE IS GREATER
{
a[++top]=x; //PUSHED THE INPUT ELEMENT
}
}
}
}
else if(x.equals(")")) //AS SOON AS CLOSED PARENTHESIS IS
ENCOUNTERED
{
while(!a[top].equals("("))
{
String t=pop();
if(!t.equals("("))
System.out.print(t); //POP ALL THE ELEMENTS UNTIL OPEN
PARANTHESIS
}
}
else
System.out.print(x);
else
return(a[top--]);
}
else if(p.equals("^"))
r="3";
else
r="-1";
return(r);
}
for(int i=0;i<p.length();i++)
{
q="";
q=Character.toString(p.charAt(i)); //CONVERTING THE
CHARACTER TO STRING
obj.push(q); //CALLING THE PUSH FUNCTION AND PASSING THR
EXTRACTED VALUE
}
while(top!=-1) //LOOP TO PRINT THE REMAINING ELEMENTS
{
if(!a[top].equals("("))
System.out.print(a[top--]);
else
top--;
}
}
}
2.3)
import java.util.*;
class hanoi
{
static int n,s=0;
3)
import java.util.*;
class Queue
{
static Scanner sc=new Scanner(System.in); //static Scanner class
to take input streams
static int n,rear=-1,front=0,a[]; //global variables
void peek()
{
System.out.println("Peeking at: "+a[rear]);
}
System.out.println(obj.size());
obj.show();
obj.dque(); //METHOD CALLING
obj.peek();
obj.show();
}
}
4)
import java.util.*;
class circular_queue
{
static Scanner sc=new Scanner(System.in); //static Scanner class
to take input streams
static int n,rear=0,front=0,a[]; //global variables
void peek()
{
System.out.println("Peeking at: "+a[rear-1]);
}
System.out.println(obj.size());
obj.show();
obj.dque(); //METHOD CALLING
obj.peek();
obj.show();
obj.que(7);
obj.show();
}
}
5)
import java.util.*;
class linked
{
static Scanner sc=new Scanner(System.in);
Node head;
public class Node
{
Node next;
int data;
}
if(head==null)
head=node;
else
{
Node n=head;
while(n.next!=null)
{
n=n.next;
}
n.next=node;
}
}
else
{
Node node=new Node();
node.data=d;
//node.next=null;
Node n=head;
for(int i=0;i<pos-2;i++)
{
n=n.next;
}
node.next=n.next;
n.next=node;
}
}
if(head==null)
System.out.println("nothing to delete: Empty");
else
{
for(int i=0;i<pos-2;i++)
{
n=n.next;
}
n1=n.next;
n.next=n1.next;
System.out.println("deleted element is :"+n1.data);
}
}
5.1)
import java.util.*;
class doubly
{
static Scanner sc=new Scanner(System.in);
static Node head;
else
{
Node n=head;
while(n.next!=null)
{
n=n.next;
}
n.next=node;
node.prev=n;
}
}
node.next=a.next;
node.prev=a;
a.next=node;
if(node.next.prev!=null)
node.next.prev=node;
while(n!=null)
{
System.out.print(n.data+" ");
n=n.next;
}
System.out.println();
}
show();
insert(5,6);
show();
insert_front(0);
show();
insert_before(head.next.next,-1);
show();
insert_after(head.next,9);
show();
}
}
5.2)
import java.util.*;
class linked
{
static Scanner sc=new Scanner(System.in);
Node head;
public class Node
{
Node next;
int data;
}
if(head==null)
head=node;
else
{
Node n=head;
while(n.next!=null)
{
n=n.next;
}
n.next=node;
}
}
else
{
Node node=new Node();
node.data=d;
//node.next=null;
Node n=head;
for(int i=0;i<pos-2;i++)
{
n=n.next;
}
node.next=n.next;
n.next=node;
}
}
if(head==null)
System.out.println("nothing to delete: Empty");
else
{
for(int i=0;i<pos-2;i++)
{
n=n.next;
}
n1=n.next;
n.next=n1.next;
System.out.println("deleted element is :"+n1.data);
}
}
while(n.next!=null)
{
System.out.println(n.data);
n=n.next;
}
System.out.println(n.data);
}
7)
import java.util.*;
class circular
{
static Scanner sc=new Scanner(System.in);
static Node tail;
if(tail==null)
{
tail=node;
tail.next=node;
}
else
{
node.next=tail.next; //changing the next of the current
node to point at the starting node
tail.next=node; //linking the tail node to the
current node
tail=node; //moving the tail to pointat the
current node
}
}
else
{
Node n=tail; //last node of the list
Node curr=n.next; //pointing the current pointer to the
1st node of the list
Node prev=n,next; //previous pointer pointing to the last
node
8)
import java.util.*;
class select_search
{
static Scanner sc=new Scanner(System.in);
static int a[],n;
search(s);
}
}
8.1)
import java.util.*;
class bubble_sort
{
static Scanner sc=new Scanner(System.in);
static int a[],n;
sort(a);
System.out.println("sorted array is: ");
for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
}
}
8.3)
import java.util.*;
class select_sort
{
static Scanner sc=new Scanner(System.in);
static int a[],n;
sort();
System.out.println("sorted array is: ");
for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
}
}
8.4)
import java.util.*;
class insert_sort
{
static Scanner sc=new Scanner(System.in);
static int a[],n;
}
}
sort(a);
System.out.println("sorted array is: ");
for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
}
}
9)
import java.util.*;
class bin_search
{
static Scanner sc=new Scanner(System.in);
static int a[],n;
else if(k>a[mid])
return search(k,mid+1,h);
}
return mid+1;
}
}
10)
11)
class quickSort
{
int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low-1); // index of smaller element
for (int j=low; j<high; j++)
{
if (arr[j] <= pivot)
{
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
return i+1;
}
void sort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
}
}
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
public static void main(String args[])
{
int arr[] = {10, 7, 8, 9, 1, 5};
int n = arr.length;
quickSort ob = new quickSort();
ob.sort(arr, 0, n-1);
System.out.println("sorted array");
printArray(arr);
}
}
12)
Import java.io.*;
class MatrixChainMultiplication
{
static int MatrixChainOrder(int p[], int n)
{
int m[][] = new int[n][n];
int i, j, k, L, q;
for (i = 1; i < n; i++)
m[i][i] = 0;
for (L = 2; L < n; L++) {
for (i = 1; i < n - L + 1; i++) {
j = i + L - 1;
if (j == n)
continue;
m[i][j] = Integer.MAX_VALUE;
for (k = i; k <= j - 1; k++) {
q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
if (q < m[i][j])
m[i][j] = q;
}
}
}
return m[1][n - 1];
}
public static void main(String args[]) {
int arr[] = new int[] { 20, 15, 30, 5, 40 };
int size = arr.length;
System.out.println("Minimum number of multiplications is "
+ MatrixChainOrder(arr, size));
}
}
13)
return true;
}
if (solveNQUtil(board, 0) == false) {
System.out.print("Solution does not exist");
return false;
}
printSolution(board);
return true;
}
14)
import java.util.*;
import java.lang.*;
import java.io.*;
class MST {
// Number of vertices in the graph
private static final int V = 5;
return min_index;
}
15)
import java.util.*;
class GFG
{
static int N = 4;
// Driver code
public static void main(String[] args)
{
//Adjacency matrix for the given graph
int adj[][] = {{0, 10, 15, 20},
{10, 0, 35, 25},
{15, 35, 0, 30},
{20, 25, 30, 0} };
TSP(adj);
System.out.printf("Minimum cost : %d\n", final_res);
System.out.printf("Path Taken : ");
for (int i = 0; i <= N; i++)
{
System.out.printf("%d ", final_path[i]);
}
}
}
16)
import java.util.Arrays;
import java.util.Comparator;
// Greedy approach
public class FractionalKnapSack
{
// function to get maximum value
private static double getMaxValue(int[] wt, int[] val,
int capacity)
{
ItemValue[] iVal = new ItemValue[wt.length];
return totalValue;
}
// Driver code
public static void main(String[] args)
{
int[] wt = { 10, 40, 20, 30 };
int[] val = { 60, 40, 100, 120 };
int capacity = 50;
// Function call
System.out.println("Maximum value we can obtain = "
+ maxValue);
}
}
17)
import java.io.*;
import java.util.*;
// Constructor
Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i=0; i<v; ++i)
adj[i] = new LinkedList();
}
// Function to add an edge into the graph
void addEdge(int v,int w)
{
adj[v].add(w);
}
while (queue.size() != 0)
{
// Dequeue a vertex from queue and print it
s = queue.poll();
System.out.print(s+" ");
// Driver method to
public static void main(String args[])
{
Graph g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
18)
import java.io.*;
import java.util.*;
// Constructor
@SuppressWarnings("unchecked")
Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i=0; i<v; ++i)
adj[i] = new LinkedList();
}
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
g.DFS(2);
}
}
18.1)
import java.util.*;
import java.lang.*;
import java.io.*;
class ShortestPath {
// A utility function to find the vertex with minimum distance
value,
// from the set of vertices not yet included in shortest path tree
static final int V = 9;
int minDistance(int dist[], Boolean sptSet[])
{
// Initialize min value
int min = Integer.MAX_VALUE, min_index = -1;
return min_index;
}
// Driver method
public static void main(String[] args)
{
/* Let us create the example graph discussed above */
int graph[][] = new int[][] { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
ShortestPath t = new ShortestPath();
t.dijkstra(graph, 0);
}
}
18.2)
import java.util.*;
import java.lang.*;
import java.io.*;
int V, E;
Edge edge[];
graph.BellmanFord(graph, 0);
}
}
19)
class BinarySearchTree {
/* Class containing left and right child of current node and key
value*/
class Node {
int key;
Node left, right;
// Root of BST
Node root;
// Constructor
BinarySearchTree() {
root = null;
}
tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
19.1)
class BinarySearchTree
{
/* Class containing left and right child of current node and key
value*/
class Node
{
int key;
Node left, right;
public Node(int item)
{
key = item;
left = right = null;
}
}
// Root of BST
Node root;
// Constructor
BinarySearchTree()
{
root = null;
}
return root;
}
System.out.println("\nDelete 20");
tree.deleteKey(20);
System.out.println("Inorder traversal of the modified tree");
tree.inorder();
System.out.println("\nDelete 30");
tree.deleteKey(30);
System.out.println("Inorder traversal of the modified tree");
tree.inorder();
System.out.println("\nDelete 50");
tree.deleteKey(50);
System.out.println("Inorder traversal of the modified tree");
tree.inorder();
}
}
20)
int j;
txt[] = "AABCCAADDEE";
pat[] = "FAA";
The number of comparisons in best case is O(n).
The worst case of Naive Pattern Searching occurs in following
scenarios.
1) When all characters of the text and pattern are same.
txt[] = "AAAAAAAAAAAAAAAAAA";
pat[] = "AAAAA";
2) Worst case also occurs when only the last character is different.
txt[] = "AAAAAAAAAAAAAAAAAB";
pat[] = "AAAAB";
The number of comparisons in the worst case is O(m*(n-m+1)).
20.1)
/* Driver Code */
public static void main(String[] args)
{
String txt = "GEEKS FOR GEEKS";
String pat = "GEEK";
// A prime number
int q = 101;
// Function Call
search(pat, txt, q);
}
} The average and best-case running time of the
Rabin-Karp algorithm is O(n+m), but its worst-case time is O(nm). Worst
case of Rabin-Karp algorithm occurs when all characters of pattern and
text are same as the hash values of all the substrings of txt[] match
with hash value of pat[]. For example pat[] = “AAA” and txt[] =
“AAAAAAA”.
20.2)
class KMP_String_Matching {
void KMPSearch(String pat, String txt)
{
int M = pat.length();
int N = txt.length();
10)
class MergeSort {
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
// Driver method
public static void main(String args[])
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
System.out.println("Given Array");
printArray(arr);
System.out.println("\nSorted array");
printArray(arr);
}
}