Skip to content

Commit b008370

Browse files
committed
backtracking started
1 parent 3e08044 commit b008370

File tree

8 files changed

+172
-173
lines changed

8 files changed

+172
-173
lines changed
Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,63 @@
11
package com.thealgorithm.array;
22

33
public class AGraphIsAValidTree {
4-
private boolean cycleExist = false;
5-
6-
// invoker
7-
public boolean validTree(int n, int[][] edges) {
8-
int[][] graph = new int[n][n];
9-
10-
for (int i = 0; i < edges.length; ++i) {
11-
graph[edges[i][0]][edges[i][1]] = 1;
12-
graph[edges[i][1]][edges[i][0]] = 1;
13-
}
14-
15-
// Start DFS
16-
boolean[] visited = new boolean[n];
17-
dfs(0, -1, visited, graph);
18-
19-
if (cycleExist) {
20-
return false;
21-
}
22-
23-
for (int i = 0; i < n; ++i) {
24-
if (!visited[i]) {
25-
// That means after a single pass DFS
26-
// i-th node was not visited
27-
// which proves that graph is not connected
28-
return false;
29-
}
30-
}
31-
32-
return true;
4+
private boolean cycleExist = false;
5+
6+
// invoker
7+
public boolean validTree(int n, int[][] edges) {
8+
int[][] graph = new int[n][n];
9+
10+
for (int i = 0; i < edges.length; ++i) {
11+
graph[edges[i][0]][edges[i][1]] = 1;
12+
graph[edges[i][1]][edges[i][0]] = 1;
13+
}
14+
15+
// Start DFS
16+
boolean[] visited = new boolean[n];
17+
dfs(0, -1, visited, graph);
18+
19+
if (cycleExist) {
20+
return false;
21+
}
22+
23+
for (int i = 0; i < n; ++i) {
24+
if (!visited[i]) {
25+
// That means after a single pass DFS
26+
// i-th node was not visited
27+
// which proves that graph is not connected
28+
return false;
29+
}
3330
}
3431

35-
void dfs(int node, int parent, boolean[] visited, int[][] graph) {
36-
// System.out.println("- cc : " + node + " - " + parent);
37-
if (!valid(graph, node)) {
38-
return;
39-
}
40-
41-
if (cycleExist) {
42-
return;
43-
}
44-
45-
if (visited[node]) {
46-
//System.out.println("cc : " + node + " - " + parent);
47-
cycleExist = true;
48-
return;
49-
}
50-
51-
visited[node] = true;
52-
53-
for (int nextNode = 0; nextNode < graph.length; ++nextNode) {
54-
if (graph[node][nextNode] == 1 && nextNode != parent) {
55-
dfs(nextNode, node, visited, graph);
56-
}
57-
}
32+
return true;
33+
}
34+
35+
void dfs(int node, int parent, boolean[] visited, int[][] graph) {
36+
// System.out.println("- cc : " + node + " - " + parent);
37+
if (!valid(graph, node)) {
38+
return;
39+
}
40+
41+
if (cycleExist) {
42+
return;
5843
}
5944

60-
boolean valid(int[][] graph, int node) {
61-
return 0 <= node && node < graph.length;
45+
if (visited[node]) {
46+
// System.out.println("cc : " + node + " - " + parent);
47+
cycleExist = true;
48+
return;
6249
}
50+
51+
visited[node] = true;
52+
53+
for (int nextNode = 0; nextNode < graph.length; ++nextNode) {
54+
if (graph[node][nextNode] == 1 && nextNode != parent) {
55+
dfs(nextNode, node, visited, graph);
56+
}
57+
}
58+
}
59+
60+
boolean valid(int[][] graph, int node) {
61+
return 0 <= node && node < graph.length;
62+
}
6363
}

src/main/java/com/thealgorithm/backtracking/CombinationSumI.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import java.util.List;
55

66
public class CombinationSumI {
7+
public static void main(String[] args) {
8+
System.out.println(new CombinationSumI().combinationSum(new int[] {2, 3, 6, 7}, 7));
9+
}
10+
711
public List<List<Integer>> combinationSum(int[] candidates, int target) {
812
List<List<Integer>> result = new ArrayList<>();
913
List<Integer> runningSet = new ArrayList<>();
@@ -30,8 +34,4 @@ void combinationSum(
3034
// exclude
3135
combinationSum(arr, index - 1, target, result, currentSet);
3236
}
33-
34-
public static void main(String[] args) {
35-
System.out.println(new CombinationSumI().combinationSum(new int[] {2, 3, 6, 7}, 7));
36-
}
3737
}

src/main/java/com/thealgorithm/backtracking/PermutationI.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
*/
1111
public class PermutationI {
1212

13+
public static void main(String[] args) {
14+
System.out.println(new PermutationI().permute(new int[] {0}));
15+
System.out.println(new PermutationI().permute(new int[] {1}));
16+
System.out.println(new PermutationI().permute(new int[] {1, 2}));
17+
System.out.println(new PermutationI().permute(new int[] {1, 2, 3}));
18+
}
19+
1320
public List<List<Integer>> permute(int[] nums) {
1421
List<List<Integer>> result = new ArrayList<>();
1522
permute(nums, 0, result);
@@ -34,11 +41,4 @@ void swap(int[] arr, int f, int t) {
3441
arr[f] = arr[t];
3542
arr[t] = x;
3643
}
37-
38-
public static void main(String[] args) {
39-
System.out.println(new PermutationI().permute(new int[] {0}));
40-
System.out.println(new PermutationI().permute(new int[] {1}));
41-
System.out.println(new PermutationI().permute(new int[] {1, 2}));
42-
System.out.println(new PermutationI().permute(new int[] {1, 2, 3}));
43-
}
4444
}

src/main/java/com/thealgorithm/backtracking/Subsets.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import java.util.List;
55

66
public class Subsets {
7+
public static void main(String[] args) {
8+
System.out.println(new Solution().subsets(new int[] {1, 2}));
9+
}
10+
711
static class Solution {
812
public List<List<Integer>> subsets(int[] nums) {
913
int len = nums.length;
@@ -26,8 +30,4 @@ public List<List<Integer>> subsets(int[] nums) {
2630
return result;
2731
}
2832
}
29-
30-
public static void main(String[] args) {
31-
System.out.println(new Solution().subsets(new int[] {1, 2}));
32-
}
3333
}

src/main/java/com/thealgorithm/backtracking/SubsetsII.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
*/
1111
public class SubsetsII {
1212

13+
public static void main(String[] args) {
14+
System.out.println(new SubsetsII().subsetsWithDup(new int[] {1, 2, 2, 1, 1, 1}));
15+
}
16+
1317
public List<List<Integer>> subsetsWithDup(int[] nums) {
1418
int len = nums.length;
1519
int powerLen = 1 << len;
@@ -30,8 +34,4 @@ public List<List<Integer>> subsetsWithDup(int[] nums) {
3034
}
3135
return new ArrayList<>(result);
3236
}
33-
34-
public static void main(String[] args) {
35-
System.out.println(new SubsetsII().subsetsWithDup(new int[] {1, 2, 2, 1, 1, 1}));
36-
}
3737
}

src/main/java/com/thealgorithm/dp/lcs/LongestCommonSubsequence.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.thealgorithm.dp.lcs;
22

33
import com.thealgorithm.commons.Pair;
4-
54
import java.util.HashMap;
65
import java.util.Map;
76

src/main/java/com/thealgorithm/graph/CoursesII.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
import java.util.Map;
88

99
public class CoursesII {
10+
public static void main(String[] args) {
11+
System.out.println(
12+
Arrays.toString(new Solution().findOrder(4, new int[][] {{1, 0}, {2, 0}, {3, 1}, {3, 2}})));
13+
}
14+
1015
static class Solution {
1116
private boolean cycleExists = false;
1217
private List<Integer> orderedList;
@@ -66,9 +71,4 @@ void dfs(Map<Integer, List<Integer>> graph, int courseLen, int node, int[] visit
6671
orderedList.add(node);
6772
}
6873
}
69-
70-
public static void main(String[] args) {
71-
System.out.println(
72-
Arrays.toString(new Solution().findOrder(4, new int[][] {{1, 0}, {2, 0}, {3, 1}, {3, 2}})));
73-
}
7474
}

0 commit comments

Comments
 (0)