├── README.md ├── print all K-element subset of n objects.java ├── Finding all subset (using Backtracking).java ├── Generate Subsets of a given set S whose sum is given K.java ├── Finding all paths in a graph(using Backtracking).java ├── Find all permutation (Using Backtracking).java └── Construct all the derangement of n items.java /README.md: -------------------------------------------------------------------------------- 1 | # Algorithm-Backtracking- 2 | -------------------------------------------------------------------------------- /print all K-element subset of n objects.java: -------------------------------------------------------------------------------- 1 | public class _Q3_2 2 | { 3 | public static void combinationUtil(int arr[], int n, int r, int index, int data[], int i) 4 | { 5 | if (index == r) 6 | { 7 | for (int j = 0; j < r; j++) 8 | { 9 | System.out.print(data[j] + " "); 10 | } 11 | System.out.println(""); 12 | return; 13 | } 14 | 15 | if (i >= n) 16 | return; 17 | 18 | data[index] = arr[i]; 19 | 20 | combinationUtil(arr, n, r, index + 1,data, i + 1); 21 | 22 | combinationUtil(arr, n, r, index, data, i + 1); 23 | } 24 | 25 | public static void printCombination(int arr[], int n, int r) 26 | { 27 | int data[] = new int[r]; 28 | 29 | combinationUtil(arr, n, r, 0, data, 0); 30 | } 31 | 32 | public static void main(String[] args) 33 | { 34 | int arr[] = {1,2,3,4}; 35 | int r = 2; 36 | int n = arr.length; 37 | printCombination(arr, n, r); 38 | } 39 | } 40 | 41 | //Output 42 | //1 2 43 | //1 3 44 | //1 4 45 | //2 3 46 | //2 4 47 | //3 4 48 | -------------------------------------------------------------------------------- /Finding all subset (using Backtracking).java: -------------------------------------------------------------------------------- 1 | public class SUBSET 2 | { 3 | // Display subset value 4 | public void display(int[] result, int n) 5 | { 6 | for (int i = 0; i < n; ++i) 7 | { 8 | System.out.print(" " + result[i]); 9 | } 10 | System.out.print("\n"); 11 | } 12 | 13 | // Find subset elements 14 | public void allSubset(int[] arr, int[] result, int i, int j, int n) 15 | { 16 | if (i == n) 17 | { 18 | display(result, j); 19 | return; 20 | } 21 | // Get element 22 | result[j] = arr[i]; 23 | // Through by recursion find next subset 24 | allSubset(arr, result, i + 1, j + 1, n); 25 | allSubset(arr, result, i + 1, j, n); 26 | } 27 | 28 | // Handles the request to find all subsets 29 | public void findSubsets(int[] arr, int n) 30 | { 31 | if (n <= 0) 32 | { 33 | return; 34 | } 35 | // Used to collect subset element 36 | int[] result = new int[n]; 37 | allSubset(arr, result, 0, 0, n); 38 | } 39 | 40 | public static void main(String[] args) 41 | { 42 | SUBSET task = new SUBSET(); 43 | int[] arr = { 1, 2, 3, 4 }; 44 | // Get the size 45 | int n = arr.length; 46 | task.findSubsets(arr, n); 47 | } 48 | } 49 | 50 | 51 | //Output - 52 | //1 2 3 4 53 | //1 2 3 54 | //1 2 4 55 | //1 2 56 | //1 3 4 57 | //1 3 58 | //1 4 59 | //1 60 | //2 3 4 61 | //2 3 62 | //2 4 63 | //2 64 | //3 4 65 | //3 66 | //4 67 | -------------------------------------------------------------------------------- /Generate Subsets of a given set S whose sum is given K.java: -------------------------------------------------------------------------------- 1 | public class _Q1_2 2 | { 3 | public static void generate_subsets(int a[],int k,int n,int value) 4 | { 5 | backtrack(a,0,n,value); 6 | } 7 | 8 | static boolean finished=false; 9 | 10 | public static void backtrack(int a[],int k,int n,int value) 11 | { 12 | int c[]=new int[n]; 13 | 14 | if(is_a_solution(a,k,n)) 15 | { 16 | process_solution(a,k,value); 17 | } 18 | 19 | else 20 | { 21 | k=k+1; 22 | 23 | int p=construct_candidate(a,k,n,c,value); 24 | 25 | for(int i=0;i[] adjList; 8 | 9 | public all_paths(int vertices) { 10 | this.v = vertices; 11 | initAdjList(); 12 | } 13 | 14 | private void initAdjList() { 15 | adjList = new ArrayList[v]; 16 | 17 | for (int i = 0; i < v; i++) { 18 | adjList[i] = new ArrayList<>(); 19 | } 20 | } 21 | 22 | public void addEdge(int u, int v) { 23 | adjList[u].add(v); 24 | } 25 | 26 | public void is_a_solution(int s, int d) { 27 | boolean[] isVisited = new boolean[v]; 28 | ArrayList pathList = new ArrayList<>(); 29 | pathList.add(s); 30 | construct_candidate(s, d, isVisited, pathList); 31 | } 32 | 33 | private void construct_candidate(Integer u, Integer d, boolean[] isVisited, List localPathList) { 34 | 35 | if (u.equals(d)) { 36 | System.out.println(localPathList); 37 | return; 38 | } 39 | isVisited[u] = true; 40 | for (Integer i : adjList[u]) { 41 | if (!isVisited[i]) { 42 | localPathList.add(i); 43 | construct_candidate(i, d, isVisited, localPathList); 44 | localPathList.remove(i); 45 | } 46 | } 47 | isVisited[u] = false; 48 | } 49 | 50 | public static void main(String[] args) { 51 | all_paths g = new all_paths(4); 52 | g.addEdge(0, 1); 53 | g.addEdge(0, 2); 54 | g.addEdge(0, 3); 55 | g.addEdge(2, 0); 56 | g.addEdge(2, 1); 57 | g.addEdge(1, 3); 58 | 59 | int s = 2; 60 | int d = 3; 61 | 62 | System.out.println("Following are all different paths from " + s + " to " + d); 63 | g.is_a_solution(s, d); 64 | } 65 | } 66 | 67 | //Output 68 | //Following are all different paths from 2 to 3 69 | //[2, 0, 1, 3] 70 | //[2, 0, 3] 71 | //[2, 1, 3] 72 | -------------------------------------------------------------------------------- /Find all permutation (Using Backtracking).java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Arrays; 3 | 4 | public class permutation 5 | { 6 | static void swap(int nums[], int l, int i) 7 | { 8 | int temp = nums[l]; 9 | nums[l] = nums[i]; 10 | nums[i] = temp; 11 | } 12 | 13 | // permutations 14 | static void permutations(ArrayList res, 15 | int[] nums, int l, int h) 16 | { 17 | // Base case 18 | if (l == h) { 19 | res.add(Arrays.copyOf(nums, nums.length)); 20 | return; 21 | } 22 | 23 | for (int i = l; i <= h; i++) { 24 | 25 | swap(nums, l, i); 26 | 27 | permutations(res, nums, l + 1, h); 28 | 29 | // Backtracking 30 | swap(nums, l, i); 31 | } 32 | } 33 | 34 | static ArrayList permute(int[] nums) 35 | { 36 | ArrayList res = new ArrayList(); 37 | int x = nums.length - 1; 38 | 39 | permutations(res, nums, 0, x); 40 | return res; 41 | } 42 | 43 | public static void main(String[] args) 44 | { 45 | int[] nums = { 3,6,7,9 }; 46 | ArrayList res = permute(nums); 47 | 48 | for (int[] x : res) { 49 | for (int y : x) { 50 | System.out.print(y + " "); 51 | } 52 | System.out.println(); 53 | } 54 | } 55 | } 56 | 57 | //Output- 58 | //3 6 7 9 59 | //3 6 9 7 60 | //3 7 6 9 61 | //3 7 9 6 62 | //3 9 7 6 63 | //3 9 6 7 64 | //6 3 7 9 65 | //6 3 9 7 66 | //6 7 3 9 67 | //6 7 9 3 68 | //6 9 7 3 69 | //6 9 3 7 70 | //7 6 3 9 71 | //7 6 9 3 72 | //7 3 6 9 73 | //7 3 9 6 74 | //7 9 3 6 75 | //7 9 6 3 76 | //9 6 7 3 77 | //9 6 3 7 78 | //9 7 6 3 79 | //9 7 3 6 80 | //9 3 7 6 81 | //9 3 6 7 82 | -------------------------------------------------------------------------------- /Construct all the derangement of n items.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class _Q2_2 4 | { 5 | 6 | public static void main(String[] args) 7 | { 8 | System.out.println("derangements for n = 4\n"); 9 | for (Object d : (ArrayList)(construct_candidate(4, false)[0])) 10 | { 11 | System.out.println(Arrays.toString((int[])d)); 12 | } 13 | 14 | 15 | } 16 | 17 | static Object[] construct_candidate(int n, boolean countOnly) 18 | { 19 | int[] seq = iota(n); 20 | int[] ori = Arrays.copyOf(seq, n); 21 | long tot = fact(n); 22 | 23 | List all = new ArrayList(); 24 | int cnt = n == 0 ? 1 : 0; 25 | 26 | while (--tot > 0) 27 | { 28 | int j = n - 2; 29 | while (seq[j] > seq[j + 1]) 30 | { 31 | j--; 32 | } 33 | int k = n - 1; 34 | while (seq[j] > seq[k]) 35 | { 36 | k--; 37 | } 38 | swap(seq, k, j); 39 | 40 | int r = n - 1; 41 | int s = j + 1; 42 | while (r > s) 43 | { 44 | swap(seq, s, r); 45 | r--; 46 | s++; 47 | } 48 | 49 | j = 0; 50 | while (j < n && seq[j] != ori[j]) 51 | { 52 | j++; 53 | } 54 | if (j == n) 55 | { 56 | if (countOnly) 57 | { 58 | cnt++; 59 | } 60 | else 61 | { 62 | all.add(Arrays.copyOf(seq, n)); 63 | } 64 | } 65 | } 66 | return new Object[]{all, cnt}; 67 | } 68 | 69 | static long fact(long n) 70 | { 71 | long result = 1; 72 | for (long i = 2; i <= n; i++) 73 | { 74 | result *= i; 75 | } 76 | return result; 77 | } 78 | 79 | static void swap(int[] arr, int lhs, int rhs) 80 | { 81 | int tmp = arr[lhs]; 82 | arr[lhs] = arr[rhs]; 83 | arr[rhs] = tmp; 84 | } 85 | 86 | static int[] iota(int n) 87 | { 88 | if (n < 0) 89 | { 90 | throw new IllegalArgumentException("iota cannot accept < 0"); 91 | } 92 | int[] r = new int[n]; 93 | for (int i = 0; i < n; i++) 94 | { 95 | r[i] = i; 96 | } 97 | return r; 98 | } 99 | } 100 | --------------------------------------------------------------------------------