Posts

Showing posts from November, 2024

simple merge sort

 import java.util.Arrays; public class MergeSort {     // Merge sort function     public static void mergeSort(int[] array, int left, int right) {         if (left < right) {             // Find the middle point             int mid = (left + right) / 2;             // Recursively sort first and second halves             mergeSort(array, left, mid);             mergeSort(array, mid + 1, right);             // Merge the sorted halves             merge(array, left, mid, right);         }     }     // Merge function to merge two sorted halves     public static void merge(int[] array, int left, int mid, int right) {         // Sizes of two subarrays to be merged   ...

q3part b

  import java . util . Random ; public class TSPMutation {     public static void main( String [] args ) {         // Example cities (0, 1, 2, 3, 4)         int [] chromosome = { 0 , 1 , 2 , 3 , 4 }; // A possible solution (tour)                 System .out.println( "Original Chromosome:" );         printChromosome(chromosome);                 // Apply Mutation (Swap two cities in the path)         mutate(chromosome);                 System .out.println( "Mutated Chromosome:" );         printChromosome(chromosome);     }     // Mutation function: Swap two cities' positions     public static void mutate( int [] chromosome ) {         Random rand = new Random();           ...

MergeSort

  import java . util . Random ; public class MergesortExample {     public static void main( String [] args ) {         int N = 500 ; // Number of elements to sort         // Initialize an array of random integers         int [] arr = new int [N];         Random rand = new Random();         for ( int i = 0 ; i < N; i++) {             arr[i] = rand.nextInt( 1000 ); // Random integers between 0 and 999         }         // Displaying the unsorted array (optional)         System .out.println( "Unsorted array:" );         printArray(arr);         // Start measuring time         long startTime = System .nanoTime();         // Call the mergesort method         mergesort(arr, 0...

quicksort 1

 import java.util.Random; public class QuicksortExample {     public static void main(String[] args) {         int N = 500; // Number of elements to sort         // Initialize an array of random integers         int[] arr = new int[N];         Random rand = new Random();         for (int i = 0; i < N; i++) {             arr[i] = rand.nextInt(1000); // Random integers between 0 and 999         }         // Displaying the unsorted array (optional)         System.out.println("Unsorted array:");         printArray(arr);         // Start measuring time         long startTime = System.nanoTime();         // Call the quicksort method         quicksort(arr, 0, N - 1);       ...

multithread matrix

  public class MatrixMultiplicationMultithreaded {     public static void main( String [] args ) {         int N = 500 ; // Matrix size (N x N)                 // Initializing matrices A and B         int [][] A = new int [N][N];         int [][] B = new int [N][N];         int [][] C = new int [N][N]; // Result matrix         // Randomly initializing matrices A and B         initializeMatrix(A);         initializeMatrix(B);         // Multithreaded matrix multiplication         long startTime = System .nanoTime();         multiplyMatricesMultithreaded(A, B, C, N);         long endTime = System .nanoTime();                 long duration = (endTime - startTime); // ...

dinning phillosoper

  import java . util . concurrent .*; class DiningPhilosophers {     static final int NUM_PHILOSOPHERS = 5 ;         // Forks are shared resources, each philosopher will access them.     static Semaphore [] forks = new Semaphore [NUM_PHILOSOPHERS];         // Philosopher threads     static Philosopher [] philosophers = new Philosopher [NUM_PHILOSOPHERS];     public static void main( String [] args ) {         // Initialize the forks (semaphores)         for ( int i = 0 ; i < NUM_PHILOSOPHERS; i++) {             forks[i] = new Semaphore( 1 ); // 1 means each fork is initially available         }         // Create philosopher threads         for ( int i = 0 ; i < NUM_PHILOSOPHERS; i++) {             philosophers[i] = new Ph...

tsp genetic

  import java . util .*; class TravellingSalesmanGenetic {     // Constants     static final int POPULATION_SIZE = 100 ; // Size of population     static final int NUM_GENERATIONS = 1000 ; // Number of generations     static final double MUTATION_RATE = 0.05 ; // Mutation rate     // Cities' coordinates (e.g., a small example for TSP)     static final int [][] cities = {         { 0 , 0 }, { 1 , 3 }, { 4 , 3 }, { 6 , 1 }, { 3 , 2 }, { 5 , 4 }, { 7 , 8 }     };     public static void main( String [] args ) {         TravellingSalesmanGenetic tsp = new TravellingSalesmanGenetic();         tsp.solveTSP(cities);     }     // Solve TSP using Genetic Algorithm     public void solveTSP( int [][] cities ) {         List < int []> population = initializePopulation( cities .leng...

tsp bb

  import java . util . Arrays ; import java.util. PriorityQueue ; class TravellingSalesman {     static class Node implements Comparable < Node > {         int level;         int [] path;         int bound;         int cost;         Node( int level , int [] path , int bound , int cost ) {             this .level = level ;             this .path = Arrays .copyOf( path , path .length);             this .bound = bound ;             this .cost = cost ;         }         @ Override         public int compareTo( Node o ) {             return Integer .compare( this .bound, o .bound);         }     }       ...

nqueens

  import java . util . ArrayList ; import java.util. List ; public class NQueens {         public static List < List < String >> solveNQueens( int n ) {         List < List < String >> solutions = new ArrayList<>();         char [][] board = new char [ n ][ n ];                 for ( int i = 0 ; i < n ; i++) {             for ( int j = 0 ; j < n ; j++) {                 board[i][j] = '.' ;             }         }         placeQueens(solutions, board, 0 , n );         return solutions;     }         private static void placeQueens( List < List < String >> solutions , char [][] board , int row , int n ) {        ...

knasack

 knapsack import java.util. ArrayList ; import java.util. List ; public class KnapsackDP {         public static int knapsack( int [] values , int [] weights , int capacity ) {         int n = values .length;         int [][] dp = new int [n + 1 ][ capacity + 1 ];         // Build dp table         for ( int i = 1 ; i <= n; i++) {             for ( int w = 1 ; w <= capacity ; w++) {                 if ( weights [i - 1 ] <= w) {                     dp[i][w] = Math .max(dp[i - 1 ][w], dp[i - 1 ][w - weights [i - 1 ]] + values [i - 1 ]);                 } else {                     dp[i][w] = dp[i - 1 ][w];              ...