Geeks Union-Find Algorithm Union By Rank and Path Compression graph ring Algorithm

Source: Internet
Author: User

It is also an algorithm for finding whether a graph has loops. However, this algorithm is awesome and can achieve O (lgn) time efficiency when constructing a tree. N indicates the number of vertices.

The reason is that the height of the tree is reduced as needed, also known as the Path compression. The name is very advanced, but it is not hard to understand. Simply put, every time you look for a node, all nodes in this path are assigned to the root node as the path.


Not pointed out in the original article:

Because compression is required, you must note that the parent node of all vertices cannot be initialized as in the simple and practical Union Find algorithm before-1, it should be to initialize the parent nodes of all nodes as themselves (self-breeding themselves ?), Then we can easily return this and the node in recursion.

Of course, it is also possible to initialize to-1, but it is not difficult to handle additional code.


Finally, you can refer to the original article: http://www.geeksforgeeks.org/union-find-algorithm-set-2-union-by-rank/


# Pragma once # include <iostream> class UnionFindUnionByRank {struct Edge {int src, des ;}; struct Graph {int V, E; Edge * edges; Graph (int v, int e): V (v), E (e) {edges = new Edge [e];} ~ Graph () {if (edges) delete [] edges ;}}; struct subSet {int parent, rank ;}; int find (subSet * subs, int I) {// because compression is required,-1 cannot be used as the root flag of if (subs [I]. parent! = I) {// Union by rank: attach smaller rank tree to high rank tree. it is so simple, but very hard to create it totally by ourself, so it's good to stand on the shoulder of the giant. subs [I]. parent = find (subs, subs [I]. parent);} return subs [I]. parent; // because if-1 is used as the root flag, I will be returned here, which will not achieve the compression effect. Instead, parent should be returned, and the layer-by-layer recursive back to the previous layer .} Void unionTwo (subSet * subs, int x, int y) {int xroot = find (subs, x); int yroot = find (subs, y ); if (subs [xroot]. rank <subs [yroot]. rank) {subs [xroot]. parent = yroot;} else if (subs [xroot]. rank> subs [yroot]. rank) {subs [yroot]. parent = xroot;} else {// only need to increment its rank when ther are equal ranksubs [yroot]. parent = xroot; subs [xroot]. rank ++ ;}} bool isCycle (Graph * gra) {subSet * subs = new subSet [gra-> V]; for (int I = 0; I <gra-> V; I ++) {subs [I]. parent = I; // parent cannot be initialized to-1 subs [I]. rank = 0 ;}for (int e = 0; e <gra-> E; e ++) {int x = find (subs, gra-> edges [e]. src); int y = find (subs, gra-> edges [e]. des); if (x = y) return true; unionTwo (subs, x, y);} return false;} public: UnionFindUnionByRank () {int V = 3, E = 3; struct Graph * graph = new Graph (V, E); // add edge 0-1graph-> edges [0]. src = 0; graph-> edges [0]. des = 1; // add edge 1-2graph-> edges [1]. src = 1; graph-> edges [1]. des = 2; // add edge 0-2graph-> edges [2]. src = 0; graph-> edges [2]. des = 2; if (isCycle (graph) printf ("Union By Rank found graph contains cycle \ n "); elseprintf ("Union By Rank found graph doesn't contain cycle \ n ");}};



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.