Algorithm-whether the graph is a tree (and check or deep search)

Source: Internet
Author: User

Today did a very interesting problem, although the difficulty is only moderate, but the contents of the involved is quite a lot. Among them, I learned in the inside and check this thing, although not very deep, at least an impression; and deep search, has been, deep search and wide search are my weaknesses, the understanding of this article is based on someone else's blog: lintcode178. Whether the graph valid tree is trees. Let's take a look at the question

Test instructions

Given n nodes, the labels are from 0 to n-1 and give a list of non-edge (give two vertices per edge), write a function to determine whether this ' no direction ' graph is a tree

Examples:

Truefalse.

Precautions:

You can assume that we don't give duplicate edges in the list of edges. The non-lateral edges [0, 1] and [1, 0] are the same side, so they will not appear in the list of edges we give you at the same time. 

1. And check the set

First there are two points:

A. If the number of points minus the number of edges is not equal to 1, this figure is definitely not a tree.

B. If two points of an edge belong to a set, then this figure must not be a tree

1th, we are very easy to judge, but how to judge the second? This requires us to check the set (I am also the first to contact and check the set, just know its model, so may not be said very clearly).

(1). One-dimensional array records the collection of each point

We can first define a one-dimensional array, the default is all initialized to-1, which means: Suppose array temp[], Temp[0] represents 0 this point belongs to the collection, different sets of our different numbers to represent, for example, 0 sets and 1 sets are different, The points of the same set form a tree.

The specific operation:

When we traverse to an edge, we determine whether the two points of the edge appear in the temp array, which is divided into three cases:

A. Two points are not present, that is, two points corresponding to the value of the temp array is equal to 1, then we will be the two points corresponding to the array value of the smallest point of two points (there is no limit here, the largest line, mainly to ensure that they both in the same set).

B. A point has occurred, a point does not appear, then we will not appear at that point corresponding to the array value of the array value to the point that appears, so that two points belong to the same set.

C. Two points have appeared, just belong to a different set, that is, the two points corresponding to the array value is not the same, then our operation is to merge two sets, the specific operation is: Because each of the values inside the collection is the same, Then we update the value of the larger collection to the value of the other collection, for example, there are two sets: {1,1},{2,2}, after the update is: {1,1,1,1}. This ensures that two sets are merged together to form a set.

D. Two points have appeared, but also belong to the same collection, then this figure is certainly not a tree.

Code:

1      Public BooleanValidtree (intNint[] edges) {2         //when the number of points minus sides is not equal to 1 o'clock, certainly not for the tree3         if(N-edges.length! = 1) {4             return false;5         }6         intTemp[] =New int[n];7          for(inti = 0; i < edges.length; i++) {8             intNode1 = edges[i][0];9             intNode2 = edges[i][1];Ten             //when two points belong to the same set, either two points are not in any set, or in the same set One             if(Temp[node1] = =Temp[node2]) { A                 //None of them ever appeared. -                 if(Temp[node1] = =-1) { -TEMP[NODE1] = Temp[node2] =math.min (Node1, node2); the}Else{//both appear and belong to the same collection -                     return false; -                 } -}Else { +                 //when two points are in the collection, but are in a different set, update the collection -                 if(Temp[node1]! =-1 && temp[node2]! =-1) { +                     intMax =Math.max (Temp[node1], temp[node2]); A                     intMin =math.min (Temp[node1], temp[node2]); at                      for(intj = 0; J < N; J + +) { -                         if(Temp[j] = = max) {//update all values of a large collection to Min -TEMP[J] =min; -                         } -                     } -}Else{//when there's only one point in the collection in                     if(Temp[node1]! =-1) { -TEMP[NODE2] =Temp[node1]; to}Else { +TEMP[NODE1] =Temp[node2]; -                     } the                 } *             } $         }Panax Notoginseng         return true; -}

2. Deep Search Traversal

Deep search traversal is the main function here: We look for each point of the parent node, if the two points of the parent node is the same, then this figure is certainly not a tree. There may be some problems with understanding here, let's look at the following scenarios:

(1). Two points appear for the first time.

(2). One point has occurred, and the other point is the first time it appears. ( hint, the picture below is described incorrectly, C's father should be a):

(3). Two points all have fathers, but fathers are not the same, divided into two kinds of situations:

(4). Two points have appeared, and the father is the same, divided into two situations:

Code:

1     Private intDfsintTemp[],intnode) {2         //When this point doesn't have a father, return to this point3         if(Temp[node] = =-1){4             returnnode;5         }6         Else //If there's a father, then continue to traverse the Father .7         {8             returnDfs (temp, temp[node]);9         }Ten     } One      Public BooleanValidtree (intNint[] edges) { A  -         if(N-edges.length! = 1) { -             return false; the         } -         int[]temp =New int[n]; -Arrays.fill (temp,-1); -          for(inti = 0; i < edges.length; i++){ +             //get edges[i][0] points of the Father -             intNode1 = DFS (temp, edges[i][0]); +             //get edges[i][1] points of the Father A             intNode2 = DFS (temp, edges[i][0]); at             //If the father is the same, it must not be a tree. -             if(Node1 = =Node2) { -                 return false; -             } -             //set Node2 's father to Node1 -TEMP[NODE2] =Node1; in         } -         return true; to}

Algorithm-whether the graph is a tree (and check or deep search)

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.