Posts

Showing posts from September, 2022

3

 import java.util.Scanner; class exception  { public static void main(String[] args)   { int a, b, result; Scanner input = new Scanner(System.in); System.out.println("Input two integers"); a = input.nextInt(); b = input.nextInt(); try   { result = a / b; System.out.println("Result = " + result); } catch (ArithmeticException e)   { System.out.println("Exception caught: Division by zero."); } } } //////////////////////////////////////////////////// import java.util.Random; class SquareThread implements Runnable   { int x; SquareThread(int x)   { this.x = x; } public void run()   { System.out.println("Thread Name:Square Thread and Square of " + x + "  is: " + x * x); }  } class CubeThread implements Runnable   { int x; CubeThread(int x)  { this.x = x; } public void run()   { System.out.println("Thread Name:Cube Thread and Cube of " + x + " is: "  + x * x * x); }  }  class...

12

 import java.util.Scanner; public class Hamiltonian  { boolean found = false; int G[][];// = new int[n + 1][n + 1]; int x[];// = new int[n + 1]; int n; public static void main(String args[])  { Hamiltonian hamiltonian = new Hamiltonian(); hamiltonian.getData(); System.out.println("\nSolution:"); hamiltonian.HamiltonianMethod(2); hamiltonian.printNoSlnPossible(); } public void printNoSlnPossible()  { if (found == false) System.out.println("No Solution possible!"); } public void getData()  { Scanner scanner = new Scanner(System.in); System.out.println("\t\t\t\tHamiltonian Cycle"); System.out.print("\nEnter the number of the vertices: "); // int n; n = scanner.nextInt(); G = new int[n + 1][n + 1]; x = new int[n + 1]; System.out.print("\nIf edge between the following vertices enter 1 else 0:\n"); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++)   { if ((i != j) && (i < j))   { System.out.print(i + " and "...

11

 import java.util.Scanner; public class SumOfsubset  { final static int MAX = 10; static int n; static int S[]; static int soln[]; static int d; public static void main(String args[])  { S = new int[MAX]; soln = new int[MAX]; int sum = 0; Scanner scanner = new Scanner(System.in); System.out.println("Enter number of elements: "); n = scanner.nextInt(); System.out.println("Enter the set in increasing order: "); for (int i = 1; i <= n; i++) S[i] = scanner.nextInt(); System.out.println("Enter the max. subset value(d): "); d = scanner.nextInt(); for (int i = 1; i <= n; i++) sum = sum + S[i]; if (sum < d || S[1] > d) System.out.println("No Subset possible"); else SumofSub(0, 0, sum); scanner.close(); } static void SumofSub(int i, int weight, int total)   { if (promising(i, weight, total) == true) if (weight == d)   { for (int j = 1; j <= i; j++)  { if (soln[j] == 1) System.out.print(S[j] + " "); } System.out.println(); }  else ...

10

 import java.util.Scanner; public class FloydsClass { static final int MAX = 20; // max. size of cost matrix static int a[][]; // cost matrix static int n; // actual matrix size public static void main(String args[])  { a = new int[MAX][MAX]; ReadMatrix(); Floyds(); // find all pairs shortest path PrintMatrix(); } static void ReadMatrix()  { System.out.println("Enter the number of vertices\n"); Scanner scanner = new Scanner(System.in); n = scanner.nextInt(); System.out.println("Enter the Cost Matrix (999 for infinity) \n"); for (int i = 1; i <= n; i++)  { for (int j = 1; j <= n; j++)  { a[i][j] = scanner.nextInt(); } } scanner.close(); } static void Floyds()  { for (int k = 1; k <= n; k++)  { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if ((a[i][k] + a[k][j]) < a[i][j]) a[i][j] = a[i][k] + a[k][j]; } } static void PrintMatrix()  { System.out.println("The All Pair Shortest Path Matrix is:\n"); for(int i=1; i<=n; i...

9

 import java.util.Scanner; public class PrimsClass  { final static int MAX = 20; static int n; // No. of vertices of G static int cost[][]; // Cost matrix static Scanner scan = new Scanner(System.in); public static void main(String[] args)  { ReadMatrix(); Prims(); } static void ReadMatrix() { int i, j; cost = new int[MAX][MAX]; System.out.println("\n Enter the number of nodes:"); n = scan.nextInt(); System.out.println("\n Enter the adjacency matrix:\n"); for (i = 1; i <= n; i++) for (j = 1; j <= n; j++)  { cost[i][j] = scan.nextInt(); if (cost[i][j] == 0) cost[i][j] = 999; } } static void Prims()  { int visited[] = new int[10]; int ne = 1, i, j, min, a = 0, b = 0, u = 0, v = 0; int mincost = 0; visited[1] = 1; 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;  } ...

8

 import java.util.Scanner; public class KruskalsClass  { final static int MAX = 20; static int n; // No. of vertices of G static int cost[][]; // Cost matrix static Scanner scan = new Scanner(System.in); public static void main(String[] args)  { ReadMatrix(); Kruskals(); } static void ReadMatrix()  { int i, j; cost = new int[MAX][MAX]; System.out.println("Implementation of Kruskal's algorithm"); System.out.println("Enter the no. of vertices"); n = scan.nextInt(); System.out.println("Enter the cost adjacency matrix"); for (i = 1; i <= n; i++)   { for (j = 1; j <= n; j++)  { cost[i][j] = scan.nextInt(); if (cost[i][j] == 0) cost[i][j] = 999; } } } static void Kruskals()  { int a = 0, b = 0, u = 0, v = 0, i, j, ne = 1, min, mincost = 0; System.out.println("The edges of Minimum Cost Spanning Tree are"); while (ne < n)  { for (i = 1, min = 999; i <= n; i++)  { for (j = 1; j <= n; j++)  { if (cost[i][j] < min)  { mi...

7

 import java.util.*; public class DijkstrasClass  { final static int MAX = 20; final static int infinity = 9999; static int n; // No. of vertices of G static int a[][]; // Cost matrix static Scanner scan = new Scanner(System.in); public static void main(String[] args)   { ReadMatrix(); int s = 0; // starting vertex System.out.println("Enter starting vertex: "); s = scan.nextInt(); Dijkstras(s); // find shortest path  } static void ReadMatrix()   { a = new int[MAX][MAX]; System.out.println("Enter the number of vertices:"); n = scan.nextInt(); System.out.println("Enter the cost adjacency matrix:"); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) a[i][j] = scan.nextInt();  } static void Dijkstras(int s)   { int S[] = new int[MAX]; int d[] = new int[MAX]; int u, v; int i; for (i = 1; i <= n; i++)  { S[i] = 0; d[i] = a[s][i]; } S[s] = 1; d[s] = 1; i = 2; while (i <= n)  { u = Extract_Min(S, d); S[u] = 1; i++; f...

6(A,B)

 import java.util.Scanner; public class KnapsackDP  {  static final int MAX = 20; // max. no. of objects  static int w[]; // weights 0 to n-1  static int p[]; // profits 0 to n-1  static int n; // no. of objects  static int M; // capacity of Knapsack  static int V[][]; // DP solution process - table  static int Keep[][]; // to get objects in optimal solution public static void main(String args[])   { w = new int[MAX]; p = new int[MAX]; V = new int [MAX][MAX]; Keep = new int[MAX][MAX]; int optsoln; ReadObjects(); for (int i = 0; i <= M; i++) V[0][i] = 0; for (int i = 0; i <= n; i++) V[i][0] = 0; optsoln = Knapsack(); System.out.println("Optimal solution = " + optsoln);  } static int Knapsack()   { int r; // remaining Knapsack capacity for (int i = 1; i <= n; i++) for (int j = 0; j <= M; j++) if ((w[i] <= j) && (p[i] + V[i - 1][j - w[i]] > V[i - 1][j]))   { V[i][j] = p[i] + V[i - 1][j...

5

 import java.util.Random; import java.util.Scanner; public class MergeSort2   { static final int MAX = 10005; static int[] a = new int[MAX]; public static void main(String[] args)   { Scanner input = new Scanner(System.in); System.out.print("Enter Max array size: "); int n = input.nextInt(); Random random = new Random(); System.out.println("Enter the array elements: "); for (int i = 0; i < n; i++)  // a[i] = input.nextInt(); // for keyboard entry a[i] = random.nextInt(1000); // generate random numbers  long startTime = System.nanoTime(); MergeSortAlgorithm(0, n - 1); long stopTime = System.nanoTime(); long elapsedTime = stopTime - startTime; System.out.println("Time Complexity (ms) for n = " +  n + " is : " + (double) elapsedTime / 1000000);  System.out.println("Sorted Array (Merge Sort):"); for (int i = 0; i < n; i++) System.out.print(a[i] + " "); input.close();  } public static void MergeSortAlgorithm(int low, int high...