import java.io.*; import java.lang.*; import java.util.*; class Allpairshortestpath { final static int INF = 99999, V = 4; void floydWarshall(int dist[][]) { int i, j, k; for (k = 0; k < V; k++) { for (i = 0; i < V; i++) { for (j = 0; j < V; j++) { if (dist[i][k] + dist[k][j]< dist[i][j])dist[i][j]= dist[i][k] + dist[k][j]; } } } printSolution(dist); } void printSolution(int dist[][]) { System.out.println( "The following matrix shows the shortest "+ "distances between every pair of vertices"); for (int i = 0; i < V; ++i) { for (int j = 0; j < V; ++j) { if (dist[i][j] == INF) System.out.print("INF "); else System.out.print(dist[i][j] + " "); } System.out.println(); } } public static void main(String[] args) { int graph[][] = { { 0, 5, INF, 10 }, { INF, 0, 3, INF }, { INF, INF, 0, 1 }, { INF, INF, INF, 0 } }; Allpairshortestpath a = new Allpairshortestpath(); a.floydWarshall(graph); } } import java.util.*; public class TSP { public static void main(String[] args) { int ans = Integer.MAX_VALUE; int dp[][] = new int[16][4]; for(int i = 0; i < (1<<n); i++) { for(int j = 0; j < n; j++) { dp[i][j] = -1; }} int shortestDistance = travellingSalesman(1, 0, ans, dp); System.out.println("The shortest distance to visit all the cities is " + shortestDistance); } static int[][]distance = {{0, 20, 42, 25},{20, 0 , 30, 34}, {42, 30, 0, 10}, {25, 34, 10, 0}}; static int n = 4; static int allCityVisited = (1<<n)-1; public static int travellingSalesman(int bitMask, int currCity, int ans, int[][] dp) { if(bitMask == allCityVisited) { return distance[currCity][0];} if(dp[bitMask][currCity] != -1) { return dp[bitMask][currCity]; } for(int city = 0; city < n; city++) { if((bitMask&(1<<city)) == 0) { bitMask = bitMask | (1<<city); int distAns = distance[currCity][city] + travellingSalesman(bitMask, city,ans,dp); ans = Math.min(ans,distAns); } } return dp[bitMask][currCity] = ans; } } import java.lang.*; class NQueen { static final int N = 4; static void printSolution(int board[][]) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) System.out.print(" " + board[i][j]+ " "); System.out.println(); } } static boolean isSafe(int board[][], int row, int col) { int i, j; for (i = 0; i < col; i++) if (board[row][i] == 1) return false; for (i = row, j = col; i >= 0 && j >= 0; i--, j--) if (board[i][j] == 1) return false; for (i = row, j = col; j >= 0 && i < N; i++, j--) if (board[i][j] == 1) return false; return true; } public static boolean solveNQueen(int board[][], int col) { if (col >= N) return true; for (int i = 0; i < N; i++) { if (isSafe(board, i, col)) { board[i][col] = 1; if (solveNQueen(board, col + 1)) return true; board[i][col] = 0; } } return false; } public static void main(String args[]) { int board[][] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } }; if (!solveNQueen(board, 0)) { System.out.print("Solution does not exist"); return; } printSolution(board); } } import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.*; class Sumofsubsets{ public static void findSubsets(Set<List<Integer>> allSubsets, List<Integer> nums, int sum) { if (nums.size() == 0) { return; } int currentSum = 0; for (Integer num : nums) { currentSum += num; } if (currentSum == sum) { allSubsets.add(new ArrayList<>(nums)); } for (int i = 0; i < nums.size(); i++) { final List<Integer> subset = new ArrayList<>(nums); subset.remove(i); findSubsets(allSubsets, subset, sum); } } public static void main(String[] args) { final Integer array[]; Scanner s=new Scanner(System.in); System.out.println("Enter size of the array:"); int n=s.nextInt(); array=new Integer[n]; System.out.println("Enter array elements"); for(int i=0;i<n;i++) { array[i]=s.nextInt(); } System.out.println("Enter Target sum:"); int sum=s.nextInt(); final HashSet<List<Integer>> allSubsets = new HashSet<>(); findSubsets(allSubsets, Arrays.asList(array), sum); System.out.println("The sum of subsets is:"); System.out.println(allSubsets); } } import java.util.Scanner; import java.util.Arrays; public class Ham { private int V, pathCount; private int[] path; private int[][] graph; public void findHamiltonianCycle(int[][] g) { V = g.length; path = new int[V]; Arrays.fill(path, -1); graph = g; try { path[0] = 0; pathCount = 1; solve(0); System.out.println("No solution"); } catch (Exception e) { System.out.println(e.getMessage()); display(); } } public void solve(int vertex) throws Exception { if (graph[vertex][0] == 1 && pathCount == V) throw new Exception("Solution found"); if (pathCount == V) return; for (int v = 0; v < V; v++) { if (graph[vertex][v] == 1 ) { path[pathCount++] = v; graph[vertex][v] = 0; graph[v][vertex] = 0; if (!isPresent(v)) solve(v); graph[vertex][v] = 1; graph[v][vertex] = 1; path[--pathCount] = -1; } } } public boolean isPresent(int v) { for (int i = 0; i < pathCount - 1; i++) if (path[i] == v) return true; return false; } public void display() { System.out.print("\nPath : "); for (int i = 0; i <= V; i++) System.out.print(path[i % V] +" "); System.out.println(); } public static void main (String[] args) { Scanner scan = new Scanner(System.in); System.out.println("HamiltonianCycle Algorithm Test\n"); Ham hc = new Ham(); System.out.println("Enter number of vertices\n"); int V = scan.nextInt(); System.out.println("\nEnter matrix\n"); int[][] graph = new int[V][V]; for (int i = 0; i < V; i++) for (int j = 0; j < V; j++) graph[i][j] = scan.nextInt(); hc.findHamiltonianCycle(graph); } }