-
-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathGraphValidTree.java
113 lines (98 loc) · 3.75 KB
/
GraphValidTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.leetcode.graphs;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Level: Medium
* Problem Link: https://leetcode.com/problems/graph-valid-tree/
* Problem Description:
* Given n nodes labeled from 0 to n-1 and a list of undirected edges (each edge is a pair of nodes), write a function
* to check whether these edges make up a valid tree.
* <p>
* Example 1:
* Input: n = 5, and edges = [[0,1], [0,2], [0,3], [1,4]]
* Output: true
* <p>
* Example 2:
* Input: n = 5, and edges = [[0,1], [1,2], [2,3], [1,3], [1,4]]
* Output: false
* <p>
* Note: you can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0,1] is the
* same as [1,0] and thus will not appear together in edges.
*
* @author rampatra
* @since 2019-08-05
*/
public class GraphValidTree {
/**
* Checks if the given edges form a valid tree using BFS.
*/
public static boolean isValidTree(int n, int[][] edges) {
List<List<Integer>> adjacencyList = buildAdjacencyList(n, edges);
return isTreeBFS(n, adjacencyList);
}
/**
* Builds the adjacency list from the given edges.
*/
private static List<List<Integer>> buildAdjacencyList(int n, int[][] edges) {
List<List<Integer>> adjacencyList = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
adjacencyList.add(new ArrayList<>());
}
for (int[] edge : edges) {
adjacencyList.get(edge[0]).add(edge[1]);
adjacencyList.get(edge[1]).add(edge[0]); // Since the graph is undirected
}
return adjacencyList;
}
/**
* Uses BFS to check for cycles and disconnected components.
*/
private static boolean isTreeBFS(int n, List<List<Integer>> adjacencyList) {
Set<Integer> visited = new HashSet<>();
Queue<Integer> queue = new LinkedList<>();
queue.offer(0);
visited.add(0);
while (!queue.isEmpty()) {
int node = queue.poll();
for (int neighbor : adjacencyList.get(node)) {
if (!visited.add(neighbor)) { // if 'add' returns false, 'neighbor' is already visited
return false;
}
queue.offer(neighbor);
}
}
return visited.size() == n;
}
/**
* Checks if the given edges form a valid tree using the Union-Find algorithm.
*/
public static boolean isValidTreeUsingUnionFind(int n, int[][] edges) {
int[] parent = new int[n];
Arrays.fill(parent, -1);
for (int[] edge : edges) {
int x = find(parent, edge[0]);
int y = find(parent, edge[1]);
if (x == y) return false; // x and y are in the same set
// Union operation
parent[y] = x;
}
return edges.length == n - 1; // Tree should have exactly n-1 edges
}
/**
* Finds the root of the node 'i' using path compression.
*/
private static int find(int[] parent, int i) {
if (parent[i] == -1) return i;
return find(parent, parent[i]);
}
public static void main(String[] args) {
assertTrue(isValidTree(5, new int[][]{{0, 1}, {0, 2}, {0, 3}, {1, 4}}));
assertFalse(isValidTree(5, new int[][]{{0, 1}, {1, 2}, {2, 3}, {1, 3}, {1, 4}}));
assertFalse(isValidTree(3, new int[][]{{0, 1}, {1, 2}, {2, 0}}));
assertTrue(isValidTreeUsingUnionFind(5, new int[][]{{0, 1}, {0, 2}, {0, 3}, {1, 4}}));
assertFalse(isValidTreeUsingUnionFind(5, new int[][]{{0, 1}, {1, 2}, {2, 3}, {1, 3}, {1, 4}}));
assertFalse(isValidTreeUsingUnionFind(3, new int[][]{{0, 1}, {1, 2}, {2, 0}}));
}
}