261. Graph Valid Tree

Source: Internet
Author: User

Topic:

Given n nodes labeled from 0 n - 1 to and a list of undirected edges (each edge is a pair of nodes), write a funct Ion to check whether these edges make up a valid tree.

For example:

Given n = 5 edges = [[0, 1], [0, 2], [0, 3], [1, 4]] and, return true .

Given n = 5 edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]] and, return false .

Hint:

    1. Given n = 5 edges = [[0, 1], [1, 2], [3, 4]] and, what should your return? Is this case a valid tree? Show more Hint

Note:you can assume that no duplicate edges would appear in edges . Since all edges was undirected, is the same as and thus would not [0, 1] [1, 0] appear together in edges .

Links: http://leetcode.com/problems/graph-valid-tree/

Exercises

Verifies that the input array can form a tree. Read some information, this belongs to the problem of collection. First we define the conditions for satisfying the required solution:

    1. Topic given n node, if you can form a tree, then the number of edge of this tree is n-1, so we start to judge edges.length = = N-1
    2. There can be no loop, that is, this graph must be acyclic, and all edges can only be composed of one connected components at the end.

After judging edges.length = = N-1, we can think of using the Quick-find in Union-find. Constructs an array ID that is long as the number of nodes N, and initializes the array with each element equal to their index. Next traverse the edges matrix of the graph, if edges[i][0] and edges[i][1] have been connected, then this graph exists ring, we return false, otherwise we will connect these two elements-iterate the ID array, the value of which all values are PID of the element values updated to qid.  Returns true after the array traversal has ended. Quadratic time is still very large, so we can continue to optimize this method, followed by weighted Quick Union + Path compression, left two brushes, continue to move forward. The harvest is in contact with the concept of the collection, very happy.

Time Complexity-o (n2), Space complexity-o (n)

 Public classSolution { Public BooleanValidtree (intNint[] edges) {//Dynamic Connectivity        if(N < 0 | | edges = =NULL|| Edges.length! = n-1)            return false; int[] id =New int[n];  for(inti = 0; i < id.length; i++) {Id[i]=i; }                 for(inti = 0; i < edges.length; i++) {            if(!connected (ID, edges[i][0], edges[i][1]) {Union (ID, edges[i][0], edges[i][1]); } Else {                return false; }        }        return true; }        Private BooleanConnectedint[] ID,intPintq) {returnID[P] = =Id[q]; }        Private voidUnionint[] ID,intPintq) {intPID =Id[p]; intqid =Id[q];  for(inti = 0; i < id.length; i++) {            if(Id[i] = =pid) {Id[i]=qid; }        }    }}

Reference:

Https://en.wikipedia.org/wiki/Disjoint-set_data_structure

https://www.youtube.com/watch?v=4SZTsQO9d6k&index=3&list=PLe-ggMe31CTexoNYnMhbHaWhQ0dvcy43t

Https://en.wikipedia.org/wiki/Tree_ (Data_structure)

http://segmentfault.com/a/1190000003791051

http://blog.csdn.net/dm_vincent/article/details/7655764

http://blog.csdn.net/pointbreak1/article/details/48796691

http://nb4799.neu.edu/wordpress/?p=1143

http://algorithmsandme.in/2014/06/graphs-detecting-cycle-in-undirected-graph/

Http://www.cs.nyu.edu/courses/summer04/G22.1170-001/6a-Graphs-More.pdf

Https://www.me.utexas.edu/~bard/IP/Handouts/cycles.pdf

Https://www.cs.princeton.edu/~rs/AlgsDS07/01UnionFind.pdf

Http://www.eecs.wsu.edu/~ananth/CptS223/Lectures/UnionFind.pdf

Https://leetcode.com/discuss/52610/8-10-lines-union-find-dfs-and-bfs

Https://leetcode.com/discuss/52563/ac-java-union-find-solution

Https://leetcode.com/discuss/58600/a-java-solution-with-dfs

Https://leetcode.com/discuss/72645/compressed-weighted-quick-union-solution-in-java-2ms

261. Graph Valid Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.