Graph Valid Tree

Source: Internet
Author: User

Mainly read this article

http://www.geeksforgeeks.org/union-find/

Summarize the current understanding of the Union-find

Used on DISJOINT-SET data structure, Disjoint-set keeps track of a set of elements partitioned into a number of disjoint (Non-ov erlapping) subsets.

Union-find is an algorithm that performs and useful operations on such a data structure:

1. Find

Determine which subset a particular element is in, this can being used to determin whether and the elements is in the same subse T

2. Union

Join-subsets into a single one

Union-find can is used to check whether a undirected graph contains cycle or not, and this algorithm assumes that graph D OESN ' t contain any self-loops

Say we have a graph as below:

[0, 1]

[1, 2]

[2, 0]

When using Union-find to process the graph, we use a hashmap to store the parent-child relationship.

1) First we have node 0 and node 1

The map doesn ' t contain 0 nor 1, so it returns 0 and 1 themselves (because they is the parent of themselves by now)

And we connect them together by making 0 the parent of 1 or 1 the parent of 0

So put (0->1) in the map

2) and then we have node 1 and Node 2

The map doesn ' t contain 1 nor 2 so it returns 1 for node 1 and returns 2 for node 2

And we connect them together by putting (1->2) in the map

Now we had (0->1), (1->2) in the map

3) for Node 2 and node 0

The parent of Node 2 is 2 itself, and the parent of node 0 is also 2, they equal to all other meaning this they belong to The same subset

Therefore, we found a cycle

In the code, I use an array instead of a hashmap, and fill the array with-1 at first meaning which this is the end, this n Umber is the parent of itself

     Public BooleanValidtree (intNint[] edges) {        if(Edges = =NULL)return false; int[] Parent =New int[n]; Arrays.fill (Parent,-1);  for(inti = 0; i < edges.length; i++){            intx = Find (Parent, edges[i][0]); inty = Find (parent, edges[i][1]); if(x==y)return false; PARENT[X]=y; }                returnEdges.length = = N-1; }        Private intFindint[] Parent,inti) {        if(Parent[i] = =-1)returni; returnFind (parent, parent[i]); }

can also be used on two other lintcode topics.

Find the Weak Connected Component in the Directed Graph

Find the Connected Component in the undirected Graph

The code is similar, in general Union-find is very helpful on the Disjoint-set topic.

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.