261. Graph Valid Tree
261. Graph Valid Tree Three states: visiting , visited, unexpolored In number : 1. 2 0 If visiting node’s nei is also visiting , then cycle // bfs class Solution{ public boolean validTree(int n, int[][] edges){ List<List<Integer>> list = new ArrayList<>(); Queue<Integer> queue = new LinkedList<>(); int[] visited = new int[n]; // default inital value is 0, means unexplored for(int i = 0; i < n; i++){ list.add(new ArrayList<>()); } for(int i = 0; i < edges.length; i++){ int firstNode = edges[i][0]; int secNode = edges[i][1]; list.get(firstNode).add(secNode); list.get(secNode).add(firstNode); } queue.offer(0); while(!queue.isEmpty()){ int cur = queue.poll(); visited[cur] = 1; // means visiting for(Integer nei : list.get(cur)){ if(visited[nei] == 1){ return false; } if(visited[nei] == 0){ visited[nei] = 1; queue.offer(nei); } } visited[cur] = 2; // means visited } // another check for valid tree , is it has only one connected component // if no cycle, after all the nodes should be visited, which is 2 in the int[] visited // if there's still nodes left that is not 2, or, in other words, if its 0, // then we know we have more than one connected components for(int i = 0; i < n; i++){ if(visited[i] == 0){ return false; } } return true; } }
// uf
public class Solution { public boolean validTree(int n, int[][] edges) { // initialize n isolated islands int[] nums = new int[n]; Arrays.fill(nums, -1); // perform union find for (int i = 0; i < edges.length; i++) { int x = find(nums, edges[i][0]); int y = find(nums, edges[i][1]); // if two vertices happen to be in the same set // then there's a cycle if (x == y) return false; // union nums[x] = y; } return edges.length == n - 1; } int find(int nums[], int i) { if (nums[i] == -1) return i; return find(nums, nums[i]); } }
// dfs public class Solution { public boolean validTree(int n, int[][] edges) { // initialize adjacency list List<List<Integer>> adjList = new ArrayList<List<Integer>>(n); // initialize vertices for (int i = 0; i < n; i++) adjList.add(i, new ArrayList<Integer>()); // add edges for (int i = 0; i < edges.length; i++) { int u = edges[i][0], v = edges[i][1]; adjList.get(u).add(v); adjList.get(v).add(u); } boolean[] visited = new boolean[n]; // make sure there's no cycle if (hasCycle(adjList, 0, visited, -1)) return false; // make sure all vertices are connected for (int i = 0; i < n; i++) { if (!visited[i]) return false; } return true; } // check if an undirected graph has cycle started from vertex u boolean hasCycle(List<List<Integer>> adjList, int u, boolean[] visited, int parent) { visited[u] = true; for (int i = 0; i < adjList.get(u).size(); i++) { int v = adjList.get(u).get(i); if ((visited[v] && parent != v) || (!visited[v] && hasCycle(adjList, v, visited, u))) return true; } return false; } }
valid tree: no cycle, one connected component
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.
Example 1:
Input:n = 5, andedges = [[0,1], [0,2], [0,3], [1,4]]Output: true
Example 2:
Input:n = 5,andedges = [[0,1], [1,2], [2,3], [1,3], [1,4]]Output: false
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.
posted on 2018-08-09 17:51 猪猪🐷 阅读(92) 评论(0) 收藏 举报
浙公网安备 33010602011771号