SNU IDB Lab. Ch7. Linear Lists – Simulated Pointers © copyright 2006 SNU IDB Lab.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Stacks, Queues, and Linked Lists
DATA STRUCTURES USING C++ Chapter 5
CHP-5 LinkedList.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Hash-Based Indexes Chapter 11.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Hash-Based Indexes Chapter 11.
Hashing as a Dictionary Implementation
List Representation - Chain Fall 2010 CSE, POSTECH.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
1 Hash-Based Indexes Yanlei Diao UMass Amherst Feb 22, 2006 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
File System Implementation
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Lecture 4 Abstract Data Types (ADT). Usual Data Types Integer, Double, Boolean You do not know the implementation of these. There are some operations.
Linked lists and memory allocation Prof. Noah Snavely CS1114
1.1 CAS CS 460/660 Introduction to Database Systems File Organization Slides from UC Berkeley.
Lecture 4 Abstract Data Types (ADT). Usual Data Types Integer, Double, Boolean You do not know the implementation of these. There are some operations.
Tree-Structured Indexes. Range Searches ``Find all students with gpa > 3.0’’ –If data is in sorted file, do binary search to find first such student,
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
CPSC 404, Laks V.S. Lakshmanan1 Tree-Structured Indexes BTrees -- ISAM Chapter 10 – Ramakrishnan & Gehrke (Sections )
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
CSC 211 Data Structures Lecture 13
Object-Oriented Programming in C++
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Hashing as a Dictionary Implementation Chapter 19.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
1.1 CS220 Database Systems Indexing: Hashing Slides courtesy G. Kollios Boston University via UC Berkeley.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Indexed Sequential Access Method.
CE Operating Systems Lecture 17 File systems – interface and implementation.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures A bank is a place where they lend you an umbrella in fair weather and ask for it.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
The Class Chain. next (datatype ChainNode) element (datatype Object) Use ChainNode abcde null firstNode size = number of elements.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
SNU IDB Lab. Ch6. Linear Lists – Linked Representation © copyright 2006 SNU IDB Lab.
Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
1 Tree-Structured Indexes Chapter Introduction  As for any index, 3 alternatives for data entries k* :  Data record with key value k   Choice.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Database Applications (15-415) DBMS Internals- Part III Lecture 13, March 06, 2016 Mohammad Hammoud.
Memory Management.
Module 11: File Structure
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Lectures linked lists Chapter 6 of textbook
The Class ArrayLinearList
Data Structures Interview / VIVA Questions and Answers
Concepts of programming languages
Database Applications (15-415) DBMS Internals- Part III Lecture 15, March 11, 2018 Mohammad Hammoud.
Simulated Pointers.
Simulated Pointers.
Indexing 4/11/2019.
Data Structures & Algorithms
Presentation transcript:

SNU IDB Lab. Ch7. Linear Lists – Simulated Pointers © copyright 2006 SNU IDB Lab

SNU IDB Lab. Data Structures 2 Bird’s-Eye View Ch.5 ~ Ch.7: Linear List Ch. 5 – array representation Ch. 6 – linked representation Ch. 7 – simulated pointer representation Simulated Pointers Memory Management Comparison with Garbage Collection Simulated Chains Memory Managed Chains Application: Union-Find Problem ※ In succeeding chapters - matrices, stacks, queues, dictionaries, priority queues Java’s linear list classes java.util.ArrayList Java.util.Vector java.util.LinkedList

SNU IDB Lab. Data Structures 3 Bird’s-Eye View Simulated-pointer representation What if we want to have linked structures on disk User-defined pointers instead of Java references Simulated pointers are represented by integers rather than by Java references To use simulated pointers Must implement our own memory management scheme Our own AllocateNode(), DeallocateNode()

SNU IDB Lab. Data Structures 4 Table of Contents Simulated Pointers Memory Management Comparison with Garbage Collection Simulated Chains Memory Managed Chains Application

SNU IDB Lab. Data Structures 5 The Need for Simulated Pointers Memory allocation via the Java new method Automatically reclaimed by garbage collection (GC) Java references are internal memory addresses and not disk addresses Java references (pointers) cannot be used for Disk storage management Data structure backup External data structures Solution Simulated pointers User defined memory allocation and deallocation

SNU IDB Lab. Data Structures 6 Disk Storage Management Operating System’s disk management Disk is partitioned into blocks of a predetermined size So called “block size” (say 32KB) These blocks are linked together from a chain Each chain entry is made in the file allocation table (FAT) The address of each disk block is different from main memory address So, Java references cannot be used Instead, simulated pointers make easy the process

SNU IDB Lab. Data Structures 7 Data Structure Backup What if you want to work on a chain of student grades next week? Serialization: the process of writing every element of the data structure in some sequential order into a file Deserialization: read back the serialized version from the file and reconstruct the chain in memory Saving Java references during serialization is useless for deserialization Instead, simulated pointers make easy the process So called, Persistent Data Structure

SNU IDB Lab. Data Structures 8 External Data Structures Data structures with pointers for the data on a disk B+ tree index (will soon be covered) Leaf nodes of B+ tree are pointing the records on a disk Java references cannot be used Instead, simulated pointers make easy the process

SNU IDB Lab. Data Structures 9 Simulating Pointers How to simulate pointers in internal memory? first implement linked lists using an array of nodes and simulate Java references by integers that are indexes into this array Useful for backup and recovery of data structure To backup, we need merely back up the contents of each node as it appears from left to right in the node array To recover, we read back the node contents in left-to-right in the node array Each array position has an element field and a next field (type int) 14 caedb next element

SNU IDB Lab. Data Structures 10 Node Representation ChainNode ’s next: java reference SimulatedNode ’s next: int type class SimulatedNode { // package visible data members int next; Object element; // package visible constructors SimulatedNode() { }; SimulatedNode(int next) {this.next = next;} } next element

SNU IDB Lab. Data Structures 11 How It All Looks? 14 caedb firstNode = 4 freeNode = 1 next element Initially, the nodes in the available space were all empty nodes Allocate nodes & store “a b c d e” Free nodes are members of a linked list In-use nodes are also members of a linked list

SNU IDB Lab. Data Structures 12 Table of Contents Simulated Pointers Memory Management Comparison with Garbage Collection Simulated Chains Memory Managed Chains Application

SNU IDB Lab. Data Structures 13 Memory Management using SP Memory management Create a collection of nodes (a storage pool): node[0: numOfNodes-1] Allocate a node as needed: allocateNode() Reclaim nodes that are no longer in use: deallocateNode() In our simple memory management scheme, nodes that are not in use are kept in a user-defined storage pool Memory management with different sizes is rather complex

SNU IDB Lab. Data Structures 14 Storage Pool (same size nodes) Maintain a chain of free nodes (user-defined storage pool) In the beginning, all nodes are free Allocate a free node from the front of chain Add node that is freed (deallocated) to the front of chain null firstNode 1234 null firstNode = 0

SNU IDB Lab. Data Structures 15 The Class SimulatedSpace1 /** memory management for simulated pointer classes */ package dataStructures; import utilities.*; public class SimulatedSpace1 { // data members private int firstNode; SimulatedNode [] node; // package visible constructor and other methods here } class SimulatedNode { int next; Object element; }

SNU IDB Lab. Data Structures 16 Constructor of SimulatedSpace1 ** creating the available space list: user-defined storage pool public SimulatedSpace1(int numberOfNodes) { node = new SimulatedNode [numberOfNodes]; // array declaration for (int i = 0; i < numberOfNodes - 1; i++) node[i] = new SimulatedNode(i + 1); // array initialization // last node of array and chain node[numberOfNodes - 1] = new SimulatedNode(-1); // firstNode has the default initial value 0 } ** Remember: SimulatedNode(int next) {this.next = next;}

SNU IDB Lab. Data Structures 17 Array in Java Primitive type: declaration & initialization at once node = new int [10] VS Complex type: declaration & initialization separately node = new SimulatedNode[10]; for (int i = 0; i < 9; i++) node[i] = new SimulatedNode(i+1);

SNU IDB Lab. Data Structures 18 Allocate a Node using SP: O(n) public int allocateNode (Object element, int next) {// Allocate a free node and set its fields. if (firstNode == -1) { // if no more free nodes in the available space list // create and line new nodes (doubling): O(n) } int i = firstNode; // allocate first node firstNode = node[i].next; // firstNode points to next free node node[i].element = element; // set its fields node[i].next = next; return i; // return the sp of new node } null firstNode

SNU IDB Lab. Data Structures 19 Free a Node using SP: O(1) public void deallocateNode (int i) { // free node i. // make the node i first node on free space list node[i].next = firstNode; firstNode = i; // remove element reference so that the space // (the referenced “ ABC ” ) can be garbage collected: node[i].element = null; }

SNU IDB Lab. Data Structures 20 The class SimuatedSpace2 -- first1: a list of free nodes not been used yet -- first2: a list of free nodes at least once public int allocateNode (Object element, int next) {// Allocate a free node and set its fields. if (first2 == -1) { // 2nd list is empty if (first1 == node.length) { // code for doubling number of node } node[first1] = new SimulatedNode(); // lazy initialization node[first1].element = element; node[first1].next = next; return first1++; } int i = first2; // allocate first node of 2 nd chain first2 = node[i].next; node[i].element = element; node[i].next = next; return i; }

SNU IDB Lab. Data Structures 21 Facts of Simulated Pointers Bulk deallocation in O(1) time Free a chain of nodes when first node f and last node e of chain are known Node[e].next = firstnode; firstNode = f; Don’t use simulated pointers Unless you see a clear advantage of simulated pointers over Java references If you deal with only in-memory stuff

SNU IDB Lab. Data Structures 22 Table of Contents Simulated Pointers Memory Management Comparison with Garbage Collection Simulated Chains Memory Managed Chains Application – Union-Find Problem

SNU IDB Lab. Data Structures 23 Garbage Collection (GC) User’s DeallocateNode vs. System’s Garbage Collection GC: The system determines which nodes/memory are not in use and returns these nodes (this memory) to the pool of free storage Periodic & Automatic Invocations This is done in two or three steps Mark nodes that are not in use Compact free spaces (optional) Move free nodes to storage pool

SNU IDB Lab. Data Structures 24 GC Step 1: Marking There is a mark-bit for each node Unmark all nodes (set all mark-bits “false”) Marking: Start at firstNode and mark all nodes reachable from firstNode by setting the mark-bit “true” Repeat marking for all reference variables caedb firstNode

SNU IDB Lab. Data Structures 25 GC Step 2: Compaction (optional) Move all marked nodes (i.e., nodes in use) to one end of memory, and update all pointers as necessary cbedb firstNode aed Free Memory

SNU IDB Lab. Data Structures 26 GC Step 3: Restoring Free Memory The storage pool is also a linked list Free nodes are linked with the storage pool If the reusable nodes can be found by scanning memory for unmarked nodes  return those nodes to the storage pool Otherwise (cannot find reusable nodes)  need to put a new single free block into the storage pool

SNU IDB Lab. Data Structures 27 Facts of GC Due to automatic GC, programmers doesn’t have to worry about freeing nodes as they become free However, for garbage collection to be effective, we must set reference variables to null when the object being referenced is no longer needed (still the programmer’s responsibility! ) Application may run faster when run on computers that have more memory because GC does not need to be invoked frequently Sometimes GC wins, sometimes deallocateNode wins depending upon the characteristics of application and the size of given memory

SNU IDB Lab. Data Structures 28 Alternatives to Garbage Collection malloc()/free() at C language new()/delete() at C++ language new()/GC at Java “GC” Automatic Time to free node is proportional to the memory size “delete()” and “free()” By manual Time to free node is proportional to number of nodes being freed

SNU IDB Lab. Data Structures 29 Table of Contents Simulated Pointers Memory Management Comparison with Garbage Collection Simulated Chains Memory Managed Chains Application

SNU IDB Lab. Data Structures 30 Simulated Chains (Linear List with SP) So far, we concerned only the storage pool management with simulated pointers SimulatedSpace Now, we move to LinearList that use the simulated space S for storing and retrieving the data elements S is declared as a static data member (like global variable) So, all simulated chains share the same simulated space Linear List implementations (n = num of elements, k = value of index) FastArrayLinearList (Array) Get(): O(1), Remove(): O(n-k), Add(): O(n-k) Chain or Simulated Chain (Linked list) Java based & Simulated pointers Get(): O(k), Remove(): O(k), Add(): O(k)

SNU IDB Lab. Data Structures 31 Performance OperationSimulatedChainFastArrayLinearListChain get199s5.6ms157s best-case inserts767ms31.2ms304ms average-case inserts176s5.8s115s worst-case inserts197s11.8s157s best-case removes20ms9ms13ms average removes228s5.7s149s worst-case removes199s11.7s157s

SNU IDB Lab. Data Structures 32 The Class SimulatedChain public class SimulatedChain implements LinearList { // data members private int firstNode; protected int size; public static SimulatedSpace1 S = new SimulatedSpace(10); // all simulated chains share S //constructors public SimulatedChain (int initialCapacity) { firstNode = -1; // size has the default initial value 0 } Suppose: x = new SimulatedChain(10) 12…9 null firstNode = -1 size = 0 S[0] s[1] s[9] x

SNU IDB Lab. Data Structures 33 The method indexOf() public int indexOf(Object elm) { // search the chain for elm; int currentNode = firstNode; int index = 0; // index of currentNode; while (currentNode != -1 && !S.node[currentNode].element.equals(elem) ) { currentNode = S.node[currentNode].next; // move to next node index++; } // make sure we found matching element if (currentNode == -1) return -1; // no such element else return index; }

SNU IDB Lab. Data Structures 34 Table of Contents Simulated Pointers Memory Management Comparison with Garbage Collection Simulated Chains Memory Managed Chains Application

SNU IDB Lab. Data Structures 35 Memory Managed Chains (1) May improve the performance of the class Chain (chap 6) without actually using simulated pointers by adopting allocateNode/deallocateNode Dynamic memory allocation methods such as new usually take a lot more time than memory allocation methods such as allocateNode Suppose add and remove operations are done in a mixed manner and always less than 50 list elements are in the list “new” is invoked times in the original Chain class However, if we use allocateNode/deallocateNode times of allocateNode() and deallocateNode() would be needed But Only 50 calls to new()

SNU IDB Lab. Data Structures 36 Memory Managed Chain (2) Even though we do not implement the simulatedChain class, the idea of buffering free nodes is useful for the class Chain (chapter 6)! Modify the class Chain Add a static data member of type ChainNode : first free node Add a static method deallocateNode : insert a node at the front of the free node chain Add a static method allocateNode : allocates a node from the free node chain (or may call new) Modify Chain.remove : use deallocateNode Modify Chain.add : invoke allocateNode rather than new

SNU IDB Lab. Data Structures 37 Table of Contents Simulated Pointers Memory Management Comparison with Garbage Collection Simulated Chains Memory Managed Chains Application Union-Find solution using Simulated Pointers

SNU IDB Lab. Data Structures 38 Equivalence Classes The relation R is an equivalence relation if and only if the following conditions are true: (a, a) ∈ R for all a ∈ U (reflexive) (a, b) ∈ R iff (b, a) ∈ R (symmetric) (a, b) ∈ R and (b, c) ∈ R  (a, c) ∈ R (transitive) Two elements are equivalent if R is an equivalence relation and (a, b) ∈ R Equivalence class A maximal set of equivalent elements

SNU IDB Lab. Data Structures 39 Equivalent Classes: Example Suppose R = {(1, 11), (7, 11), (2, 12), (12, 8), (11,12), (3, 13), (4,13), (13, 14), (14, 9), (5, 14), (6, 10) } and n = 14 For simplicity omit reflexive and transitive pairs (1, 11) and (7, 11) can be combined to (1, 7, 11) Final three equivalent classes {1, 2, 7, 8, 11, 12} {3, 4, 5, 9, 13, 14} {6, 10} Internally [class1, (1, 11)] [class2, (7, 11)] [class3, (2, 120)]….. Looks like simulated node with simulated pointers

SNU IDB Lab. Data Structures 40 Equivalence Class Problem Determine the equivalence classes The offline equivalence class problem Given n elements and Given a relation R We are to determine the equivalence classes Can be solved easily with various ways The online equivalence class problem (namely, the Union-Find problem) R is built incrementally by online inputs Begin with n elements, each in a separate equiv class Process a sequence of the operations combine(a, b) : combine an equiv class A and an equiv Class B find(theElement) : find a class having theElement

SNU IDB Lab. Data Structures 41 Combine and Find Operation find(theElement) Determine the class that currently contains “theElement” element combine(a,b) Combine the two equivalence classes that contain elements a and b into a single class Is equivalent to classA = find(a); classB = find(b); if (classA != classB) union(classA, classB);

SNU IDB Lab. Data Structures 42 Union-Find Problem Example Edge processedCollection of disjoint sets initial sets (b, c) (e, g) (a, c) (h, i) (a, b) (e, f) (c, d) {a} {b} {c} {d} {e} {f} {g} {h} {i} {j} {a} {b, c} {d} {e} {f} {g} {h} {i} {j} {a} {b, c} {d} {e, g} {f} {h} {i} {j} {a, b, c} {d} {e, g} {f} {h} {i} {j} {a, b, c} {d} {e, g} {f} {h, i} {j} {a, b, c} {d} {e, f, g} {h, i} {j} {a, b, c, d} {e, f, g} {h, i} {j} a c b d ef g h i j We are given set of elements and build up equivalence classes. At each step, sets are build by find and union operations.

SNU IDB Lab. Data Structures 43 Equiv Class Applications – Circuit-wiring Problem (1) Electrically equivalent pins Connected by a wire or there is a sequence of pins connected by wires Find a Net Maximal set of electrically equivalent pins

SNU IDB Lab. Data Structures 44 Equiv Class Applications – Circuit-wiring Problem (2) Each wire may be described by the two pins that it connects Given a set of wires: {(1,11), (7,11),…, (6,10)} Internally [class1, (1, 11)] [class2, (7, 11)] … [class10,(6,10)] Looks like simulated node with simulated pointers Nets: {1, 2, 7, 8, 11, 12}, {3, 4, 5, 9, 13, 14}, {6, 10}

SNU IDB Lab. Data Structures 45 Equiv Class Applications - Circuit-wiring Problem (3) The Offline net finding problem Given the pins and wires Determine the nets Modeled by the offline equivalent problem with each pin (as a member of U) and each wire (as a member of R) The Online net finding problem Begin with a collection of pins and no wires Perform a sequence of operations of the form Add a wire “one-by-one” to connect pins a and b Find the net that contains pin a

SNU IDB Lab. Data Structures 46 The 1 st Union-Find solution By array equivClass[] equivClass[i] is the class that currently contains element i Inputs to Union operations are equivClass values Initialize & Union  O(n), Find  O(1) Given n elements: 1 initialization, u unions, and f finds  O(n + u*n + f)

SNU IDB Lab. Data Structures 47 The Union-Find solution using Arrays ebcad abcde equivClass[] combine(c,e) combine(b,e) combine(a,d) Classes with one element each ‘e’ belongs to the class of “c”  generates {c,e} Generates {b, c, e} and {a, d} ※ index 0 is not used ‘{c,e}’ belongs to the class of “b”  generates {b, c, e}

SNU IDB Lab. Data Structures 48 The 1st Union-Find Solution (1) public class UnionFindFirstSolution { static int [] equivClass; static int n; // number of elements // initialize numberOfElements classes with one element each static void initialize(int numberOfElements) { n = numberOfElements; equivClass = new int [n + 1]; for (int e = 1; e <= n; e++) equivClass[e] = e; } // continued

SNU IDB Lab. Data Structures 49 The 1 st Union-Find Solution (2) // unite the classes classA and classB static void union (int classA, int classB) { // assume classA != classB for (int k = 1; k <= n; k++) if (equivClass[k] == classB) equivClass[k] = classA; } // find the class that contains theElement static int find (int theElement) { return equivClass[theElement]; }

SNU IDB Lab. Data Structures 50 The 2nd Union-Find Solution Reduce the time complexity of the union operation by keeping a chain for each equivalence class In array, full scan is required for changing a class We can find all elements in a given equivalence class by jumping up and down the chain Size and Next data members are needed Next is a simulated pointer equivClass(0)equivClass(1)equivClass(9) size next

SNU IDB Lab. Data Structures 51 The 2 nd Union-Find Solution using Chains ecbad abcde Initial State combine(b,e) combine(c,e) combine(a,d) equivClass size next

SNU IDB Lab. Data Structures 52 The 2 nd UFS: The Class EquivNode class EquivNode { int equivClass; // element class identifier int size; // size of class int next; // simulated pointer to next element in class // constuctor EquivNode (int theClas, int theSize) { equivClass = theClass; size = theSize; // next has the default value 0 }

SNU IDB Lab. Data Structures 53 The Class UnionFindSecondSolution (1) public class UnionFindSecondSolution { static EquivNode [] node;// array of nodes static int n; // number of elements // initialize numberOfElements classes with one element each static void initialize(int numberOfElements) { n = numberOfElements; equivClass = new EquivNode[n + 1]; for (int e = 1; e<=n; e++) // node[e] is initialized so that its equivClass is e node[e] = new EquivNode(e, 1); } // continued

SNU IDB Lab. Data Structures 54 The Class UnionFindSecondSolution (2) static void union (int classA, int classB) { // assume classA != classB, make classA smaller class if (node[classA].size > node[classB].size) { // swap classA and classB int t = classA; classA = classB; classB = t; } int k; for (k = classA; node[k].next != 0; k = node[k].next) node[k].equivClass = classB; // insert chain classA after first node in chain classB and update new chain size node[classB].size += node[classA].size; node[k].next = node[classB].next; node[classB].next = classA; } static int find (int theElement) { return node[theElement].equivClass; }

SNU IDB Lab. Data Structures 55 Summary (1) Simulated-pointer representation What if we want to have linked structures on disk User-defined pointers instead of Java references Simulated pointers are represented by integers rather than by Java references To use simulated pointers Must implement our own memory management scheme Our own AllocateNode(), DeallocateNode()

SNU IDB Lab. Data Structures 56 Summary (2) Simulated Pointers Memory Management Comparison with Garbage Collection Simulated Chains Memory Managed Chains Application: Union-Find Problem