"Algorithm C language Implementation" ———— fast-Find algorithm (Quick-find algorithm)

Source: Internet
Author: User

The algorithm is based on an integer array, when p and Q are connected only if the p and Q elements are equal. initially, the value of element I in the array is i,0<=i<n, in order to implement the merging of P and Q, we iterate over the array and change the value of all elements named P to Q. We can also choose another way to change all the elements named Q to P.

The program reads a non-negative integer pair sequence that is less than n from the standard input (to P-q, "connect the object Beta to Q"), and the output is not connected to the input pair. An array ID is used in the program, each element represents an object and has the following properties, when and only when P and Q are connected, id[p] and id[q] want to wait. For simplicity, define n as a compile-time constant. On the other hand, you can also get it from the input and dynamically assign it an array of IDs.

/*@file Quickfind.c@brief uses fast lookup algorithms to solve small-scale connectivity problems*/#include<stdio.h>#defineN 1000intMainvoid){    intI, p, Q, t; intId[n]; //Initializes the initial values of the elements in the collection of objects     for(i =0; i < N; i++) Id[i] =i; //loop read-in integer pairs     while(scanf_s ("%d-%d", &p, &q) = =2)    {        //if the object p is connected to Q, the next pair of integers is read from the standard input        if(Id[p] = = Id[q])Continue; //if the values of id[p] and id[q] are not equal, then P-Q is the new pair//All elements that are equal to the value of the id[p] element are connected to the Q         for(t = id[p], i =0; i < N; i++)        {            if(Id[i] = =t) id[i]=Id[q]; }        //because P-q is new, so output this toprintf"New pair:%d-%d\n", p, q); }    return 0;}

I don't know what this is for, it's just a traversal, a value assignment.

"Algorithm C language Implementation" ———— fast-Find algorithm (Quick-find algorithm)

Related Article

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.