Union-Find Problem. Using Arrays And Chains



Similar documents
Motivation Suppose we have a database of people We want to gure out who is related to whom Initially, we only have a list of people, and information a

Full and Complete Binary Trees


The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Union-Find Algorithms. network connectivity quick find quick union improvements applications

CS711008Z Algorithm Design and Analysis

Complexity of Union-Split-Find Problems. Katherine Jane Lai

Algorithms and Data Structures

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Analysis of Algorithms I: Binary Search Trees

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Cpt S 223. School of EECS, WSU

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Lecture 2 February 12, 2003

Union-find with deletions

Worst-Case Analysis of Set Union Algorithms

Physical Data Organization

An Evaluation of Self-adjusting Binary Search Tree Techniques

Binary Search Trees CMPSC 122

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

Data Structure with C

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Mathematical Induction. Lecture 10-11

Converting a Number from Decimal to Binary

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Binary Heaps. CSE 373 Data Structures

How To Create A Tree From A Tree In Runtime (For A Tree)

Binary Search Trees 3/20/14

Merkle Hash Trees for Distributed Audit Logs

Binary Heap Algorithms

Analysis of Algorithms, I

Introduction Advantages and Disadvantages Algorithm TIME COMPLEXITY. Splay Tree. Cheruku Ravi Teja. November 14, 2011

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers

6.852: Distributed Algorithms Fall, Class 2

Binary Trees and Huffman Encoding Binary Search Trees

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

From Last Time: Remove (Delete) Operation

IE 680 Special Topics in Production Systems: Networks, Routing and Logistics*

Why Use Binary Trees?

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Persistent Data Structures

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Lecture 4: Balanced Binary Search Trees

Algorithms and Data Structures Written Exam Proposed SOLUTION

The LCA Problem Revisited

6 March Array Implementation of Binary Trees

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Analysis of Binary Search algorithm and Selection Sort algorithm

Parallelization: Binary Tree Traversal

Binary Search Trees (BST)

DESIGN AND ANALYSIS OF ALGORITHMS

Ordered Lists and Binary Trees

CS473 - Algorithms I

How To Prove The Dirichlet Unit Theorem

Lecture 1: Course overview, circuits, and formulas

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. !-approximation algorithm.

Approximation Algorithms

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Data Structures Fibonacci Heaps, Amortized Analysis

OPTIMAL BINARY SEARCH TREES

Classifying Large Data Sets Using SVMs with Hierarchical Clusters. Presented by :Limou Wang

1/1 7/4 2/2 12/7 10/30 12/25

CSC2420 Fall 2012: Algorithm Design, Analysis and Theory

V. Adamchik 1. Graph Theory. Victor Adamchik. Fall of 2005

Data Structure [Question Bank]

Binary Trees (1) Outline and Required Reading: Binary Trees ( 6.3) Data Structures for Representing Trees ( 6.4)

A Catalogue of the Steiner Triple Systems of Order 19

Algorithms Chapter 12 Binary Search Trees

Gambling and Data Compression

Turing Degrees and Definability of the Jump. Theodore A. Slaman. University of California, Berkeley. CJuly, 2005

The Goldberg Rao Algorithm for the Maximum Flow Problem

Classification/Decision Trees (II)

Persistent Data Structures and Planar Point Location

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006

Scheduling Home Health Care with Separating Benders Cuts in Decision Diagrams

Binary Search Trees. Ric Glassey

Data Structures. Algorithm Performance and Big O Analysis

Why? A central concept in Computer Science. Algorithms are ubiquitous.

recursion, O(n), linked lists 6/14

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. #-approximation algorithm.

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Data Structures and Algorithms

Complete Redundancy Detection in Firewalls

Hash-based Digital Signature Schemes

Introduction to Automata Theory. Reading: Chapter 1

Symbol Tables. Introduction

Persistent Binary Search Trees

Zabin Visram Room CS115 CS126 Searching. Binary Search

Transcription:

Union-Find Problem Given a set {,,, n} of n elements. Initially each element is in a different set. ƒ {}, {},, {n} An intermixed sequence of union and find operations is performed. A union operation combines two sets into one. ƒ Each of the n elements is in exactly one set at any time. A find operation identifies the set that contains a particular element. Using Arrays And Chains See Section. for applications as well as for solutions that use arrays and chains. Best time complexity obtained in Section. is O(n + u log u + f), where u and f are, respectively, the number of union and find operations that are done. Using a tree (not a binary tree) to represent a set, the time complexity becomes almost O(n + f) (assuming at least n/ union operations).

A Set As A Tree S = {,,,,,, } Some possible tree representations: Result Of A Find Operation find(i) is to identify the set that contains element i. In most applications of the union-find problem, the user does not provide set identifiers. The requirement is that find(i) and find(j) return the same value iff elements i and j are in the same set. find(i) will return the element that is in the tree root.

Strategy For find(i) Start at the node that represents element i and climb up the tree until the root is reached. Return the element in the root. To climb the tree, each node must have a parent pointer. Trees With Parent Pointers 8 3 6 0 0 6

Possible Node Structure Use nodes that have two fields: element and parent. ƒ Use an array table[] such that table[i] is a pointer to the node whose element is i. ƒ To do a find(i) operation, start at the node given by table[i] and follow parent fields until a node whose parent field is null is reached. ƒ Return element in this root node. Example table[] 0 0 (Only some table entries are shown.)

Better Representation Use an integer array parent[] such that parent[i] is the element that is the parent of element i. parent[] 0 0 0 Union Operation union(i,j) ƒ i and j are the roots of two different trees, i!= j. To unite the trees, make one tree a subtree of the other. ƒ parent[j] = i

Union Example 8 3 6 0 0 6 union(,) The Find Method public int find(int theelement) { while (parent[theelement]!= 0) theelement = parent[theelement]; // move up return theelement; }

The Union Method public void union(int roota, int rootb) {parent[rootb] = roota;} O() Time Complexity Of union()

Time Complexity of find() Tree height may equal number of elements in tree. ƒ union(,), union(3,), union(,3), union(,) 3 So complexity is O(u). u Unions and f Find Operations O(u + uf) = O(uf) Time to initialize parent[i] = 0 for all i is O(n). Total time is O(n + uf). Worse than solution of Section.! Back to the drawing board.

Smart Union Strategies 8 3 6 0 0 6 union(,) Which tree should become a subtree of the other? Height Rule Make tree with smaller height a subtree of the other tree. Break ties arbitrarily. union(,) 8 3 6 0 0 6

Weight Rule Make tree with fewer number of elements a subtree of the other tree. Break ties arbitrarily. 8 3 6 union(,) 0 0 6 Implementation Root of each tree must record either its height or the number of elements in the tree. When a union is done using the height rule, the height increases only when two trees of equal height are united. When the weight rule is used, the weight of the new tree is the sum of the weights of the trees that are united.

Height Of A Tree Suppose we start with single element trees and perform unions using either the height or the weight rule. The height of a tree with p elements is at most floor (log p) +. Proof is by induction on p. See text. Sprucing Up The Find Method 8 3 6 a b d c f g e 0 0 6 a, b, c, d, e, f, and g are subtrees find() Do additional work to make future finds easier.

Path Compaction Make all nodes on find path point to tree root. find() 8 3 6 a b d c f g e 0 0 6 a, b, c, d, e, f, and g are subtrees Makes two passes up the tree. Path Splitting Nodes on find path point to former grandparent. find() 8 3 6 a b d c f g e 0 0 6 a, b, c, d, e, f, and g are subtrees Makes only one pass up the tree.

Path Halving Parent pointer in every other node on find path is changed to former grandparent. find() 8 3 6 a b d c f g e 0 0 6 a, b, c, d, e, f, and g are subtrees Changes half as many pointers. Time Complexity Ackermann s function. ƒ A(i,j) = j, i = and j >= ƒ A(i,j) = A(i-,), i >= and j = ƒ A(i,j) = A(i-,A(i,j-)), i, j >= Inverse of Ackermann s function. ƒ alpha(p,q) = min{z>= A(z, p/q) > log q}, p >= q >=

Time Complexity Ackermann s function grows very rapidly as i and j are increased. ƒ A(,) = 6,36 The inverse function grows very slowly. ƒ alpha(p,q) < until q = A(,) ƒ A(,) = A(,6) >>>> A(,) In the analysis of the union-find problem, q is the number, n, of elements; p = n + f; and u >= n/. For all practical purposes, alpha(p,q) <. Time Complexity Theorem. [Tarjan and Van Leeuwen] Let T(f,u) be the maximum time required to process any intermixed sequence of f finds and u unions. Assume that u >= n/. a*(n + f*alpha(f+n, n)) <= T(f,u) <= b*(n + f*alpha(f+n, n)) where a and b are constants. These bounds apply when we start with singleton sets and use either the weight or height rule for unions and any one of the path compression methods for a find.