Union-find Algorithm Introduction

Source: Internet
Author: User
Tags data structures

Article reprinted from: http://blog.csdn.net/dm_vincent/article/details/7655764

This paper mainly introduces an algorithm to solve a kind of problem of dynamic connectivity, and uses a data structure called Union-find.

For more information, refer to section 1.5 of the algorithms book, which is actually based on a review of it.

More of the text gives some conclusions, and I try to give some idea of the process, that is, why to use this method, and not other methods. I think this might make more sense, compared to writing down some conclusions.


About dynamic Connectivity

Let's look at a picture to see what dynamic connectivity is:

Suppose we enter a set of pairs of integers, i.e. (4, 3) (3, 8), and so on, each pair of integers represents that two points/sites are connected. So with the continuous input of data, the connectivity of the entire graph will change, from the above image can be clearly found this point. Also, for points/sites that are already connected, ignore them directly, such as (8, 9) in the image above.


Application Scenarios for dynamic connectivity: Network connection judgment:

If the two integers in each pair represent a network node, then the pair is used to indicate that the two nodes need to be connected. The dynamic connectivity diagram for all pairs will reduce the need for cabling as little as possible, since the two nodes that have been connected will be ignored directly. Variable name equivalence (similar to the concept of pointers):

In a program, you can declare multiple references to point to the same object, and at this point you can determine which references actually point to the same object by establishing a dynamic connectivity graph for the references and actual objects declared in the program.

Model The problem:

When modeling the problem, we should try to figure out what problems need to be solved. Because the data structures and algorithms selected in the model are obviously different depending on the problem, the problem that we need to solve in terms of dynamic connectivity is: give two nodes, determine if they are connected, if they are connected, do not need to give a specific path to give two nodes, to determine whether they are connected, if connected, Need to give a specific path

In terms of the above two problems, although only can give the specific path of the difference, but this difference leads to the choice of algorithms, this article mainly introduces the first case, that is, do not need to give a specific path to the Union-find algorithm, and the second case can use the DFS-based algorithm.

Modeling ideas:

The simplest and most intuitive assumption is that for all nodes that are connected, we can think of them as belonging to a group, so that disconnected nodes are bound to belong to different groups. With the pair input, we need to first determine whether the input two nodes are connected. How to judge it. According to the above assumptions, we can determine the group they belong to, and then see if the two groups are the same, if the same, then the two nodes are connected, and vice versa. For simplicity, we represent all the nodes as integers, that is, an integer representation of n nodes using 0 to N-1. And before processing the input pair, each node must be isolated, that is, they belong to different groups, you can use an array to represent this layer of relationship, the index of the array is the integer representation of the node, and the corresponding value is the group number of the node. The array can be initialized to:

[Java] view plain copy print? for (int i = 0; i < size; i++) id[i] = i;


That is, for node I, its group number is also I.

After initialization, there are several possible operations on the dynamic connectivity diagram: The group that the query node belongs to

The value of the corresponding position of the array is the group number to determine whether two nodes belong to the same group

Get the group number of two nodes respectively, and then determine whether the group number is equal to connect two nodes so that they belong to the same group

Get the group number of two nodes respectively, the group number is the same as the end of the operation, not at the same time, the group number of one node is replaced by the group number of the other node gets the number of groups

Initialized to the number of nodes, then decrements 1 after each successful connection of two nodes


Api

We can design the appropriate API:


Note that you use integers to represent nodes, and if you need to use other data types to represent nodes, such as strings, you can use a hash table to map the string to the integer type needed here.

Analyzing the above API, the methods connected and union both rely on find,connected to call two find methods on two parameters, and the Union will need to determine whether the connection is connected before the Union is actually executed, and this is two times the Find method is called. So we need to design the implementation of the Find method as efficiently as possible. So there is the following Quick-find implementation.


Quick-findalgorithm: [Java]   View plain copy print? public class uf   {       private int[] id; //  access to component id  (site indexed)         private int count; // number of components        Public uf (int n)        {            // Initialize component id array.            count = N;           id  = new int[N];           for  (int i  = 0; i < n; i++)                 id[i] = i;       }       public 

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.