Disjoint-set and optimization

Disjoint-set

Outline

nature

  • A tree structure
  • Disjoint-set algorithm does not support splitting a set of

element

  • Representatives yuan
    • Elements of the collection, used to represent the set of
    • All elements in a set to represent organized into a tree structure is the root element
  • parent[x]
    • For each element, parent [x] x parent node point in the tree structure. If x is a root node, so that the parent [x] = x

operating

  • MakeSet
    • Initialization disjoint-set
    • Setting a representative of
    •   function MakeSet(x) // 参数 => 选定的代表元
        x.parent := x
      
  • Find
    • Determining which subset of elements belonging to return a set of metadata representative of the element belongs
    • Method => can continue to move upward in the tree structure along a parent [x], until reaching the root node
    • Determine whether the two belong to the same set of elements, whether just look the same to their representatives yuan
    •   function Find(x) // 参数 => 带查找的元素
        	if x.parent == x // 到达根节点
        		return x
        	else
        		return Find(x.parent)
      
  • Union
    • Into the two subsets and the same collection
    •   function Union(x, y)
        	xRoot := Find(x)
        	yRoot := Find(y)
        	xRoot.parent := yRoot
      

Disjoint-set Uuion optimization

  • And that set the most basic method to check for the array representation
  • Performance inferior to the list method, because the tree could create a serious imbalance, certain branches depth too high
    • Balance optimization
      • By rank merger
        • Always will be a small tree connected to a larger tree

        • Rank: Depth

        • Affect the running time is the tree depth, add smaller trees will not increase to the same rank on rank unless they have deeper roots of the tree

        • Defined as a single element rank 0, when combined with two rank is r trees, their rank r + 1

        • Worst case time complexity of O (logN)

        •   function MakeSet(x) // 初始化
                x.parent := x
                x.rank := 0 // 并查集树结构每个节点包含秩信息
          
        •   function Union(x, y) // 合并
          	  xRoot := Find(x)
          	  yRoot := Find(y)
          	  if xRoot == yRoot
                return
          	  // x和y不在同一个集合,合并他们。
          	  if xRoot.rank < yRoot.rank
                xRoot.parent := yRoot
          	  else if xRoot.rank > yRoot.rank
          	      yRoot.parent := xRoot
          	  else
          	      yRoot.parent := xRoot
          	      xRoot.rank := xRoot.rank + 1
          
      • Path compression
        • The method of performing a tree structure when the flat "lookup"
        • Each node on the path can be connected directly to the root
        • Find recursively through the tree, each node of the changed reference to the root of the tree to give a more flat, after the acceleration of the operation node directly or indirectly references
        •   function Find(x)
            	if x.parent != x
            		x.parent := Find(x.parent) // 将每个节点引用到根节点
            	return x.parent
          
        • Progressive optimal algorithm: Fredman and Saks explains the average time O (N) in 1989 can get any disjoint-set

The main operation

  • The above operations are assumed independent elements respectively belonging to a separate set in
  • Merge two disjoint sets
    • Setting a parent array, x represents the number of parents
    • Merge two disjoint sets which way is to find a collection of the ancestor, the ancestor of another set of a father to it
    •   void Union(int x, int y) {
        	xRoot = Find(x); // 找到始祖
        	yRoot = Find(y); // 找到始祖
        	if (xRoot != yRoot)
        		parent[xRoot] = yRoot; // x -> y
        }
      
  • Analyzing two elements belong to the same set
    • Looking ancestor, the ancestor of the comparison are the same
    •   bool same(int x, int y) {
        	return Find(x) == Find(y); // 比较始祖是否相同
        }
      

And search optimization set

  • Path compression
    •   int getfather(int v) {
        	if (parent[v] == v) // 根节点
        		return v; // 返回始祖
        	else {
        		parent[v] = getfather(parent[v]); // 路径压缩【递归】
        		return parent[v]; // 返回并查集根节点
        	}
        }
      
  • Rank merger
    • The minimum depth of the deepest collection of elements where the collection will be merged into the element at which
    •   void judge(int x, int y) {
        	// 寻找始祖
        	xRoot = Find(x);
        	yRoot = Find(y);
        	if (rank[xRoot] > rank[yRoot])
        		parent[yRoot] = xRoot;
        	else {
        		parent[xRoot] = yRoot; // 以yRoot为始祖
        		if (rank[xRoot] == rank[yRoot])
        			++rank[yRoot]; // 重要的是始祖的rank,只修改始祖的,其他的不用管
        	}
        }
      
    • initialization
      memset(rank, 0, sizeof(int)*rank_size);
      

Time and space complexity

  • time complexity
    • Path compression simultaneously, by rank (Rank) combined optimization procedure average time complexity of each operation is O (N)
  • Space complexity
    • O (N) N is the number of elements

Guess you like

Origin www.cnblogs.com/XyLee/p/12562762.html