text
stringlengths
1
7.76k
source
stringlengths
17
81
17. Sorting and its complexity structures is most of the work done? Insertion sorts remove the first or most easily accessible element from 'unsorted' and search through 'sorted' to find its proper place. Selection sorts search through 'unsorted' to find the next element to be appended to 'sorted'. Insertion sort The i...
algorithms and data structures_Page_161_Chunk4701
This book is licensed under a Creative Commons Attribution 3.0 License Exhibit 17.3: Merge sorts exploit order already present. The processor shown at left in Exhibit 17.4 reads two tapes, A and B. Tape A contains runs 1 and 2; tape B contains runs 3 and 4. The processor merges runs 1 and 3 into the single run 1 & 3 on...
algorithms and data structures_Page_162_Chunk4702
17. Sorting and its complexity Exhibit 17.5 Distribution sorts use the radix representation of keys to organize elements in buckets We have now seen the basic ideas on which all sorting algorithms are built. It is more important to understand these ideas than to know dozens of algorithms based on them. To appreciate th...
algorithms and data structures_Page_163_Chunk4703
This book is licensed under a Creative Commons Attribution 3.0 License Exhibit 17.6: Straight insertion propagates a ripple-effect across the sorted part of the array. A[0] := –∞; for i := 2 to n do begin j := i; while A[j] < A[j – 1] do { A[j] :=: A[j – 1]; { exchange } j := j – 1 } end; This straight insertion sort i...
algorithms and data structures_Page_164_Chunk4704
17. Sorting and its complexity The sum in the formula for the number of comparisons reflects the structure of the two nested for loops. The body of the inner loop is executed the same number of times for each of the three cases. Thus this straight selection sort is of time complexity Θ(n2). A lower bound Ω(n · log n) A...
algorithms and data structures_Page_165_Chunk4705
This book is licensed under a Creative Commons Attribution 3.0 License The average number of binary questions needed by a sorting algorithm is equal to the average depth of the leaves of this decision tree. The lemma following this theorem will show that in a binary tree with k leaves the average depth of the leaves is...
algorithms and data structures_Page_166_Chunk4706
17. Sorting and its complexity Exhibit 17.9: Quicksort partitions the array into the "small" elements on the left and the "large" elements on the right. We chose an arbitrary threshold value m to define "small" as ≤ m, and "large" as ≥ m, thus ensuring that any "small element" ≤ any "large element". We partition an arb...
algorithms and data structures_Page_167_Chunk4707
This book is licensed under a Creative Commons Attribution 3.0 License • The average between the smallest and largest element. This requires a separate scan of the entire array in the beginning; thereafter, the average for each subarray can be calculated during the previous partitioning process. The recursive procedure...
algorithms and data structures_Page_168_Chunk4708
17. Sorting and its complexity repeat partition; if j – L < R – i then begin if i < R then { s := s + 1; stack[s].L := i; stack[s].R := R }; R := j end else begin if L < j then { s := s + 1; stack[s].L := L; stack[s].R := j }; L := i end until L ≥ R until s = 0 end; { iqs } After partitioning, 'iqs' pushes the bounds o...
algorithms and data structures_Page_169_Chunk4709
This book is licensed under a Creative Commons Attribution 3.0 License and substitute on the right-hand side to obtain Repeated substitution yields The constant q(1), which measures quicksort's work on a trivially sorted array of length 1, and b, the cost of a single procedure call, do not affect the dominant term n · ...
algorithms and data structures_Page_170_Chunk4710
17. Sorting and its complexity This recurrence relation approximates the recurrence relation discussed in chapter 16 well enough to have the same solution Since ln 4 ≈ 1.386, quicksort's asymptotic behavior in the typical case is only about 40% worse than in the best case, and remains in Θ(n · log n). [Sed 77] is a tho...
algorithms and data structures_Page_171_Chunk4711
This book is licensed under a Creative Commons Attribution 3.0 License Exhibit 17.11: Each copy-merge cycle halves the number of runs and doubles their lengths. Exercise: a merge sort in main memory Consider the following procedure that sorts the array A: const n = … ; var A: array[1 .. n] of integer; … procedure sort ...
algorithms and data structures_Page_172_Chunk4712
17. Sorting and its complexity Exhibit 17.12: Sorting an array by using a divide-and-conquer scheme. (b) The work w(n) performed while sorting n elements satisfies The first term describes the cost of the two recursive calls of 'sort', the term a · n is the cost of merging the two sorted subarrays, and the constant b i...
algorithms and data structures_Page_173_Chunk4713
This book is licensed under a Creative Commons Attribution 3.0 License since w(1) is constant the time complexity of 'sort' is Θ(n · log n). (d) If 'sort' is called recursively for m subarrays of equal size, the cost w'(n) is solving this recursive equation shows that the time complexity does not change [i.e. it is Θ(n...
algorithms and data structures_Page_174_Chunk4714
17. Sorting and its complexity computation changes. For this purpose a discussion of special-purpose sorting networks suffices. The "processors" in a sorting network are merely comparators: Their only function is to compare the values on two input wires and switch them onto two output wires such that the smaller is on ...
algorithms and data structures_Page_175_Chunk4715
This book is licensed under a Creative Commons Attribution 3.0 License usually admit a simpler correctness proof. For this example, we observe that c1, c2, and c3 place the smallest element on the top wire. Similarly, c1, c2, and c4 place the largest on the bottom wire. This leaves the middle two elements on the middle...
algorithms and data structures_Page_176_Chunk4716
17. Sorting and its complexity Exhibit 17.19: Shifting comparators reduces the number of stages. Any number of comparators that are aligned vertically require only a single unit of time. The compressed triangular network has O(n2) comparators, but its time complexity is 2 · n – 1 ∈ O(n). There are networks with better ...
algorithms and data structures_Page_177_Chunk4717
This book is licensed under a Creative Commons Attribution 3.0 License A duplicate is an integer that is contained in both A and B. Problem: How many duplicates are there in A and B? (a) Determine the time complexity of the brute-force algorithm that compares each integer contained in one array to all integers in the o...
algorithms and data structures_Page_178_Chunk4718
This book is licensed under a Creative Commons Attribution 3.0 License Part V: Data structures The tools of bookkeeping When thinking of algorithms we emphasize a dynamic sequence of actions: "Take this and do that, then that, then … ." In human experience, "take" is usually a straightforward operation, whereas "do" me...
algorithms and data structures_Page_179_Chunk4719
This book is licensed under a Creative Commons Attribution 3.0 License 18. What is a data structure? Learning objectives: • data structures for manual use (e.g. edge-notched cards) • general-purpose data structures • abstract data types specify functional properties only • data structures include access and maintenance...
algorithms and data structures_Page_180_Chunk4720
18. What is a data structure? two needles through the pack of cards at the holes E and ~U. EXETER and OMEGA will drop out. In principle it is easy to make this sample database more powerful by including additional attributes, such as "A occurs exactly once", "A occurs exactly twice", "A occurs as the first letter in th...
algorithms and data structures_Page_181_Chunk4721
This book is licensed under a Creative Commons Attribution 3.0 License • The separation of abstract data types, which specify only functional properties, from data structures, which also involve aspects of implementation • The classification of all data structures into three major types: implicit data structures, lists...
algorithms and data structures_Page_182_Chunk4722
18. What is a data structure? computers, except that it incorporates no bounds on the memory size—either in terms of the number of locations or the size of the content of this location. It implies, for example, that a multiplication of two very large numbers requires no more time than 2 · 3 does. This assumption is unr...
algorithms and data structures_Page_183_Chunk4723
This book is licensed under a Creative Commons Attribution 3.0 License 19. Abstract data types Learning objectives: • data abstraction • abstract data types as a tool to describe the functional behavior of data structures • examples of abstract data types: stack, fifo queue, priority queue, dictionary, string Concepts:...
algorithms and data structures_Page_184_Chunk4724
19. Abstract data types the type of each operand. We present the syntax of operations in mathematical function notation, specifying its domain and range. The semantic part attaches a meaning to each operation: what values it produces or what effect it has on its environment. We specify the semantics of abstract data ty...
algorithms and data structures_Page_185_Chunk4725
This book is licensed under a Creative Commons Attribution 3.0 License possible states of a stack, let s = x1 x2 … xk ∈ S be an arbitrary stack state with k elements, and let λ denote the empty state of the stack, corresponding to the null string ∈ X*. Let 'cat' denote string concatenation. Define the functions create:...
algorithms and data structures_Page_186_Chunk4726
19. Abstract data types 'create' with s0, but we choose to make a distinction between the act of creating a new empty stack and the empty state that results from this creation; the latter may recur during normal operation of the stack. Reduced sequences Any s ∈ S is obtained from the empty stack s0 by performing a fini...
algorithms and data structures_Page_187_Chunk4727
This book is licensed under a Creative Commons Attribution 3.0 License not empty(s) ⇒ depth(pop(s)) = depth(s) – 1 not full(s) ⇒ depth(push(s, x)) = depth(s) + 1 full(s) = (depth(s) = m) not full(s) ⇒ top(push(s, x)) = x pop(push(s, x)) = s Variable-length stack A stack implemented as a list may overflow at unpredictab...
algorithms and data structures_Page_188_Chunk4728
19. Abstract data types function top(s: stack): elt; { not to be called if the stack is empty } begin return(s.a[s.d]) end; procedure pop(var s: stack); { not to be called if the stack is empty } begin s.d := s.d – 1 end; Since the function 'depth' is not exported (i.e. not made available to the user of this data type)...
algorithms and data structures_Page_189_Chunk4729
This book is licensed under a Creative Commons Attribution 3.0 License Any f ∈ F is obtained from the empty fifo queue f0 by performing a finite sequence of 'enqueue' and 'dequeue' operations. By axioms (6) and (7) this sequence can be reduced to a sequence consisting of 'enqueue' operations only which also transforms ...
algorithms and data structures_Page_190_Chunk4730
19. Abstract data types ∀p ∈ P,∀x ∈ X: (1) create = p0 (2) empty(p0) = true (3) empty(insert(p, x)) = false (4) min(insert(p0, x)) = x (5) not empty(p) ⇒ min(insert(p, x)) = MIN(x, min(p)) (6) delete(insert(p0, x)) = p0 (7) not empty(p)⇒ delete (insert(p,x))={ pifxminp insertdeletep,xelse Any p ∈ P is obtained f...
algorithms and data structures_Page_191_Chunk4731
This book is licensed under a Creative Commons Attribution 3.0 License ∀d ∈ D,∀x, y ∈ X: (1) create = d0 (2) member(d0, x) = false (3) member(insert(d, x), x) = true (4) x ≠ y ⇒ member(insert(d, y), x) = member(d, x) (5) delete(d0, x) = d0 (6) delete(insert(d, x), x) = delete(d, x) (7) x ≠ y ⇒ delete(insert(d, x), y) =...
algorithms and data structures_Page_192_Chunk4732
19. Abstract data types The successor of x is defined as the smallest of all the elements in the table which are larger than x, or as +∞ if none exists. The predecessor is defined symmetrically: the largest of all the elements in the table that are smaller than x, or –∞. Present a formal specification to describe the b...
algorithms and data structures_Page_193_Chunk4733
This book is licensed under a Creative Commons Attribution 3.0 License tail('abcd') = 'bcd'; length('abcd') = 4; find('abcd', 'b') = 2. (a) Give the axioms that specify the semantics of the abstract data type 'string'. (b) The function hchop: S × X → S returns the substring of a string s beginning with the first occurr...
algorithms and data structures_Page_194_Chunk4734
19. Abstract data types reverse(s0) = s0 s ≠ s0 ⇒ reverse(s) = append(reverse(tail(s)), head(s)) Exercises 1. Implement two stacks iν onε array a[1 .. m] in such a way that neither stack overflows unless the total number of elements in both stacks together is m. The operations 'push', 'top', and 'pop' should run in O(1...
algorithms and data structures_Page_195_Chunk4735
This book is licensed under a Creative Commons Attribution 3.0 License 20. Implicit data structures Learning objectives: • implicit data structures describe relationships among data elements implicitly by formulas and declarations • array storage • band matrices • sparse matrices • Buffers eliminate temporary speed dif...
algorithms and data structures_Page_196_Chunk4736
20. Implicit data structures Array storage A two-dimensional array declared as var A: array[1 .. m, 1 .. n] of elt; is usually written in a rectangular shape: A[1, 1] A[1, 2] … A[1, n] A[2, 1] A[2, 2] … A[2, n] … … … … A[m, 1] A[m, 2] … A[m, n] But it is stored in a linearly addressed memory, typically row by row (as s...
algorithms and data structures_Page_197_Chunk4737
This book is licensed under a Creative Commons Attribution 3.0 License The point is that access to an element A[i, j, …] invokes evaluation of a (linear) formula α(i, j, …) that tells us where to find this element. A high-level programming language hides most of the details of address computation, except when we wish t...
algorithms and data structures_Page_198_Chunk4738
20. Implicit data structures Exercise: band matrices (a) Write a procedure add(p, q: bandm; var r: bandm); which adds two band matrices stored in p and q and stores the result in r. (b) Write a procedure bmv(p: bandm; v: … ; var w: … ); which multiplies a band matrix stored in p with a vector v of length n and stores t...
algorithms and data structures_Page_199_Chunk4739
This book is licensed under a Creative Commons Attribution 3.0 License (d) that access C and return the corresponding matrix elements. (e) Write a procedure that computes A := A · B in place: The entries of A in C are replaced by the entries of the product A · B. You may use a (small) constant number of additional vari...
algorithms and data structures_Page_200_Chunk4740
20. Implicit data structures Exhibit 20.2: A staircase separates two triangular matrices (c) stored in a rectangular array. (graphic does not match) function A(i, j: 1 .. n): real begin if i < j then return(0.0) else return(C[i, j]) end; function B(i, j: 1 .. n): real; begin if i < j then return(0.0) else return(C[n – ...
algorithms and data structures_Page_201_Chunk4741
This book is licensed under a Creative Commons Attribution 3.0 License C[i, j] := x / C[i, i] end; C[i, i] := 1.0 / C[i, i] end end; procedure AinvertedmultB; begin invertA; mult end; Implementation of the fixed-length fifo queue as a circular buffer A fifo queue is needed in situations where two processes interact in ...
algorithms and data structures_Page_202_Chunk4742
20. Implicit data structures Exhibit 20.3: Insertions move the pointer 'in', deletions the pointer 'out' counterclockwise around the array. Notice that the pointers 'in' and 'out' meet both when the buffer gets full and when it gets empty. Clearly, we must be able to distinguish a full buffer from an empty one, so as t...
algorithms and data structures_Page_203_Chunk4743
This book is licensed under a Creative Commons Attribution 3.0 License The producer uses only 'enqueue' and 'full', as it deletes no elements from the circular buffer. The consumer uses only 'front', 'dequeue', and 'empty', as it inserts no elements. The state of the circular buffer is described by its contents and the...
algorithms and data structures_Page_204_Chunk4744
20. Implicit data structures function front(f: fifoqueue): elt; { not to be called if the queue is empty } begin return(f.B[f.out]) end; procedure dequeue(f: fifoqueue); { not to be called if the queue is empty } begin f.out := (f.out + 1) mod m end; Implementation of the fixed-length priority queue as a heap A fixed-l...
algorithms and data structures_Page_205_Chunk4745
This book is licensed under a Creative Commons Attribution 3.0 License Exhibit 20.7: A heap has the structure of an almost complete binary tree. Order: The element assigned to any node is ≤ the elements assigned to any children this node may have (Exhibit 20.8). Exhibit 20.8: The order property implies that the smalles...
algorithms and data structures_Page_206_Chunk4746
20. Implicit data structures Exhibit 20.9: Rebuilding the order property of the tree in Exhibit 20.8 after 1 has been removed and 13 has been moved to the root. Insertion is handled analogously. The structural condition requires that a new node is created on the bottom level at the leftmost empty slot. The new element ...
algorithms and data structures_Page_207_Chunk4747
This book is licensed under a Creative Commons Attribution 3.0 License A binary tree can be implemented in many different ways, but the special class of trees that meets the structural condition stated above has a particularly efficient array implementation. A heap is a binary tree that satisfies the structural and the...
algorithms and data structures_Page_208_Chunk4748
20. Implicit data structures This is more efficient than repeated insertion of a single element into an existing heap. Since the for loop is executed n div 2 times, and n – i ≤ n, the time complexity for creating a heap with n elements is O(n · log n). A more careful analysis shows that the time complexity for creating...
algorithms and data structures_Page_209_Chunk4749
This book is licensed under a Creative Commons Attribution 3.0 License Exercises and programming projects 1. Block-diagonal matrices are composed of smaller matrices that line up along the diagonal and have 0 elements everywhere else, as shown in Exhibit 20.12. Show how to store an arbitrary block-diagonal matrix in a ...
algorithms and data structures_Page_210_Chunk4750
This book is licensed under a Creative Commons Attribution 3.0 License 21. List structures Learning objectives: • static vs dynamic data structures • linear, circular and two-way lists • fifo queue implemented as a linear list • breadth-first and depth-first tree traversal • traversing a binary tree without any auxilia...
algorithms and data structures_Page_211_Chunk4751
21. List structures tail has no successor, its pointer is assigned a predefined value 'nil', which differs from the address of any cell. Access to the list is provided by an external pointer 'head'. If the list is empty, 'head' has the value 'nil'. A cell stores an element xi and a pointer to the successor cell (Exhibi...
algorithms and data structures_Page_212_Chunk4752
This book is licensed under a Creative Commons Attribution 3.0 License In a two-way (or doubly linked) list each cell contains two pointers, one to its successor, the other to its predecessor. The list can be traversed in both directions. Exhibit 21.5 shows a circular two-way list. Exhibit 21.5: A circular two-way or d...
algorithms and data structures_Page_213_Chunk4753
21. List structures The fifo queue implemented as a one-way list It is natural to implement a fifo queue as a one-way linear list, where each element points to the next one "in line". The operation 'dequeue' occurs at the pointer 'head', and 'enqueue' is made fast by having an external pointer 'tail' point to the last ...
algorithms and data structures_Page_214_Chunk4754
This book is licensed under a Creative Commons Attribution 3.0 License level, in order to pursue the most promising one. We do not discuss breadth-first traversal further, we merely suggest the following: Exercise: breadth-first traversal Decide on a representation for trees where each node may have a variable number o...
algorithms and data structures_Page_215_Chunk4755
21. List structures Exhibit 21.8: Straightforward implementation of a binary tree The following procedure 'traverse' implements any or all of the three orders preorder, inorder, and postorder, depending on how the procedures 'visit1', 'visit2', and 'visit3' process the data in the node referenced by the pointer p. The ...
algorithms and data structures_Page_216_Chunk4756
This book is licensed under a Creative Commons Attribution 3.0 License Exhibit 21.9: Coding of a leaf used in procedure TTT procedure TTT; var o, p, q: nptr; begin o := nil; p:= root; while p ≠ nil do begin visit(p); q := p^.L; p^.L := p^.R; { rotate left pointer } p^.R := o; { rotate right pointer } o := p; p := q end...
algorithms and data structures_Page_217_Chunk4757
21. List structures Exhibit 21.11: Tracing procedure TTT while traversing the smallest tree Induction step: If H is true for all n, 0 < n ≤ k, H is also true for k + 1. Proof: Consider a tree T with k + 1 nodes. T consists of a root and k nodes shared among the left and right subtrees of the root. Each of these subtree...
algorithms and data structures_Page_218_Chunk4758
This book is licensed under a Creative Commons Attribution 3.0 License Exhibit 21.12: Trace of procedure TTT, invoking the induction hypothesis Exercise: binary trees Consider a binary tree declared as follows: type nptr = ^node; node = record L, R: nptr end; var root: nptr; (a) If a node has no left or right subtree, ...
algorithms and data structures_Page_219_Chunk4759
21. List structures (b) Write a function nodes(…): integer; that returns the number of nodes, and a function depth(…): integer; that returns the depth of a binary tree. The depth of the root is defined to be 0; the depth of any other node is the depth of its parent increased by 1. The depth of the tree is the maximum d...
algorithms and data structures_Page_220_Chunk4760
This book is licensed under a Creative Commons Attribution 3.0 License to copy a graph given by a pointer p to its origin, and return a pointer to the origin of the copy. Use the field cn, assuming that its initial value is 'nil' in every node of the original graph; set it to 'nil' in every node of the copy. Solution (...
algorithms and data structures_Page_221_Chunk4761
21. List structures (a) Write a program to visit every node in a directed acyclic graph reachable from a pointer called 'root'. You are free to execute procedure 'visit' for each node as often as you like. (b) Write a program similar to (a) where you are required to execute procedure 'visit' exactly once per node. Hint...
algorithms and data structures_Page_222_Chunk4762
This book is licensed under a Creative Commons Attribution 3.0 License Exhibit 21.16: A possible implementation as a list structure. type nptr = ^node; cptr = ^cell; node = record status: boolean; np: nptr; cp: cptr end; cell = record np: nptr; cp: cptr end; var origin: nptr; The pointer 'origin' has the value 'nil' or...
algorithms and data structures_Page_223_Chunk4763
21. List structures Basic operations on binary search trees are most easily implemented as recursive procedures. Consider a tree represented as in the preceding section, with empty subtrees denoted by 'nil'. The following function 'find' searches for an element x in a subtree pointed to by p. It returns a pointer to a ...
algorithms and data structures_Page_224_Chunk4764
This book is licensed under a Creative Commons Attribution 3.0 License Exhibit 21.17: Element x is deleted while preserving its node N. Node N is filled with a new value y, whose old node is easier to delete. A sentinel is again the key to an elegant iterative implementation of binary search trees. In a node with no le...
algorithms and data structures_Page_225_Chunk4765
21. List structures function member(d: dictionary; x: elt): boolean; var p: nptr; begin d^.e := x; { initialize element in sentinel } p := d^.L; { point to root, if it exists } while x ≠ p^.e do if x < p^.e then p := p^.L else { x > p^.e } p := p^.R; return(p ≠ d) end; Procedure 'find' searches for x. If found, p point...
algorithms and data structures_Page_226_Chunk4766
This book is licensed under a Creative Commons Attribution 3.0 License Without any further provisions, a binary search tree can degenerate into a linear list in the worst case. Then the cost for each of the operations would be O(n). What is the expected average cost for the search operation in a randomly generated bina...
algorithms and data structures_Page_227_Chunk4767
21. List structures Exhibit 21.20: Restructuring: worst case In the next two sections we present several classes of balanced trees that meet both conditions: the height- balanced or AVL-trees (G. Adel'son-Vel'skii and E. Landis, 1962) [AL 62] and various multiway trees, such as B- trees [BM 72, Com 79] and their genera...
algorithms and data structures_Page_228_Chunk4768
This book is licensed under a Creative Commons Attribution 3.0 License Exhibit 21.23: Most skewed AVL trees of heights h = 0 through h = 4 The number of nodes in a most-skewed AVL-tree of height h is given by the recurrence relation nh = nh–1 + nh–2 + 1, n0 = 1, n1 = 2. In the section on recurrence relations in the cha...
algorithms and data structures_Page_229_Chunk4769
21. List structures Applying the logarithm results in Therefore, the height of a worst-case AVL-tree with n nodes is about 1.44 · log2 n. Thus the class of AVL-trees satisfies the balance condition, and the 'member' operation can always be performed in time O(log n). We now show that the class of AVL-trees also satisfi...
algorithms and data structures_Page_230_Chunk4770
This book is licensed under a Creative Commons Attribution 3.0 License Rule I1: If the current node has balance condition –, change it to / or \ depending on whether we entered from the node's left or from its right child. If the current node is the root, terminate; if not, continue to follow the path upward. Rule I2: ...
algorithms and data structures_Page_231_Chunk4771
21. List structures Exhibit 21.25: Trace of consecutive insertions and the rebalancings they trigger Deletion An element is deleted as in the case of a binary search tree. Starting at the parent of the deleted node, walk towards the root, passing along the message that the height of the subtree rooted at the current no...
algorithms and data structures_Page_232_Chunk4772
This book is licensed under a Creative Commons Attribution 3.0 License balance condition of the other child of the current node (consider also the mirror images of the following illustrations): (a) X Y Z a b X Y Z b a rotation An appropriate rotation restores the balance of the current node without changing the height ...
algorithms and data structures_Page_233_Chunk4773
21. List structures • All leaves are at the same level • 2 ≤ c(root) ≤ b • For all internal nodes N except the root, a ≤ c(N) ≤ b A node with k children contains k – 1 elements x1 < x2 < … < xk–1 drawn from X; the subtrees corresponding to the k children are denoted by T1, T2, … , Tk. An (a,b)-tree supports "c(N) searc...
algorithms and data structures_Page_234_Chunk4774
This book is licensed under a Creative Commons Attribution 3.0 License worst, the splitting process propagates to the root of the tree, where a new root that contains only the median element is created. (a,b)-trees grow at the root, and this is the reason for allowing the root to have as few as two children. Deletion D...
algorithms and data structures_Page_235_Chunk4775
21. List structures Exhibit 21.28: Node splits propagate towards the root Exhibit 21.29: A deletion is absorbed by borrowing Exhibit 21.30: Another deletion propagates node merges towards the root Exhibit 21.31: Node merges and borrowing combined (2,3)-trees are the special case a = 2, b = 3: each node has two or three...
algorithms and data structures_Page_236_Chunk4776
This book is licensed under a Creative Commons Attribution 3.0 License triggers the merger of the nodes assigned to 3 and 5; this causes an underflow in their parent, which in turn propagates to the root and results in a tree of reduced height (state 5). Exhibit 21.32: Tracing insertions and deletions in a (2,3)-tree A...
algorithms and data structures_Page_237_Chunk4777
21. List structures (b) Write a procedure smallest(var x: integer); which returns the smallest number stored in the tree, and a procedure remove smallest; which deletes it. If the tree is empty both procedures should call a procedure message('tree is empty'); (c) Write a procedure sort; that sorts the numbers stored in...
algorithms and data structures_Page_238_Chunk4778
This book is licensed under a Creative Commons Attribution 3.0 License 22. Address computation Learning objectives: • hashing • perfect hashing • collision resolution methods: separate chaining, coalesced chaining, open addressing (linear probing and double hashing) • deletions degrade performance of a hash table • Per...
algorithms and data structures_Page_239_Chunk4779
22. Address computation Each cell has a capacity of b > 0 elements; b stands for bucket capacity. The number n of elements to be stored is therefore bounded by m · b. Two cases are usefully distinguished, depending on whether the hash table resides on disk or in central memory: 1. Disk or other secondary storage device...
algorithms and data structures_Page_240_Chunk4780
This book is licensed under a Creative Commons Attribution 3.0 License procedure insert(x); begin T[h(x)] := true end; procedure delete(x); begin T[h(x)] := false end; The idea of collision-free address computation can be extended to large key domains through a combination of address computation and list processing tec...
algorithms and data structures_Page_241_Chunk4781
22. Address computation A perfect hash table has to store each element explicitly, not just a bit (present/absent). In the example above, the elements 0, 1, 16, 17, 32, 33, … all map into address 0, but only 17 is present in the table. The access function 'member(x)' is implemented as a single statement: return ((h(x) ...
algorithms and data structures_Page_242_Chunk4782
This book is licensed under a Creative Commons Attribution 3.0 License for handling collisions. We present the three major collision resolution techniques in use: separate chaining, coalesced chaining, and open addressing. The two techniques called chaining call upon list processing techniques to organize overflowing e...
algorithms and data structures_Page_243_Chunk4783
22. Address computation • Memory is partitioned into two separate areas that do not share space: If the overflow area is full, the entire table is full, even if there is still space in the array of home cells. This consideration leads to the next technique. Coalesced chaining The chains that emanate from overflowing bu...
algorithms and data structures_Page_244_Chunk4784
This book is licensed under a Creative Commons Attribution 3.0 License lengthening this block; this in turn increases the probability that future elements will hash into this block. Once this positive feedback loop gets started, the cluster keeps growing. Double hashing is a special type of open addressing designed to ...
algorithms and data structures_Page_245_Chunk4785
22. Address computation return(a) end; Deletion of elements creates problems, as is the case in many types of hash tables. An element to be deleted cannot simply be replaced by 'empty', or else it might break the collision paths of other elements still in the table— recall the basic invariant on which the correctness o...
algorithms and data structures_Page_246_Chunk4786
This book is licensed under a Creative Commons Attribution 3.0 License 1. A Fortran compiler might use a hash table to store the set of identifiers it encounters in a program being compiled. The rules of the language and human habits conspire to make this set a highly biased sample from the set of legal Fortran identif...
algorithms and data structures_Page_247_Chunk4787
22. Address computation ▪ Each bit of an address h(x) should depend on all bits of the key value x. In particular, don't ignore any part of x in computing h(x). Thus h(x) = x mod 213 is suspect, as only the least significant 13 bits of x affect h(x). ▪ Make sure that arithmetic progressions such as Ch1, Ch2, Ch3, … get...
algorithms and data structures_Page_248_Chunk4788
This book is licensed under a Creative Commons Attribution 3.0 License Exhibit 22.5 suggests that a reasonable operating range for a hash table keeps the load factor λ between 0.25 and 0.75. If λ is much smaller, we waste space, if it is larger than 75 per cent, we get into a domain where the performance degrades rapid...
algorithms and data structures_Page_249_Chunk4789
22. Address computation Exhibit 22.6: Address space organized as a binary radix tree. The radix tree shown in Exhibit 22.6 (without the nodes that have been clipped) describes an active address range with addresses {00, 010, 011, 1} that are considered as bit strings or binary numbers. To each active node with address ...
algorithms and data structures_Page_250_Chunk4790
This book is licensed under a Creative Commons Attribution 3.0 License Exhibit 22.8: An overflowing bucket may trigger doubling of the directory. A virtual radix tree: order-preserving extendible hashing Hashing, in the usual sense of the word, destroys structure and thus buys uniformity at the cost of order. Extendibl...
algorithms and data structures_Page_251_Chunk4791
22. Address computation Exhibit 22.9: Relative frequency of words beginning with a given letter in Webster's dictionary. If there is one page of words starting with X for 160 pages of words starting with S, this suggests that if our active address space is partitioned into equally sized intervals, some intervals may be...
algorithms and data structures_Page_252_Chunk4792
This book is licensed under a Creative Commons Attribution 3.0 License (b) Write a procedure initTable; which initializes the entries of the hash table T. (c) Write a function member (name: pair): boolean; which returns for any pair of uppercase letters whether it is stored in T. 3. Consider the hash function h(x) = x ...
algorithms and data structures_Page_253_Chunk4793
This book is licensed under a Creative Commons Attribution 3.0 License 23. Metric data structures Learning objectives: • organizing the embedding space versus organizing its contents • quadtrees and octtrees. grid file. two-disk-access principle • simple geometric objects and their parameter spaces • region queries of ...
algorithms and data structures_Page_254_Chunk4794
23. Metric data structures Radix trees, tries We have encountered binary radix trees, and a possible implementation, in chapter 22 in the section “Extendible hashing”. Radix trees with a branching factor, or fan-out, greater than 2 are ubiquitous. The Dewey decimal classification used in libraries is a radix tree with ...
algorithms and data structures_Page_255_Chunk4795
This book is licensed under a Creative Commons Attribution 3.0 License Exhibit 23.2: A quarter circle digitized on a 16 · 16 grid, and its representation as a 4-level quadtree. A quadtree is the obvious two-dimensional analog of the one-dimensional binary radix tree we have seen. Accordingly, quadtrees are frequently u...
algorithms and data structures_Page_256_Chunk4796
23. Metric data structures 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Notice that the children of any node i are 4 · i + 1, 4 · i + 2, 4 · i + 3, 4 · i + 4. The parent of node i is (i – 1) div 4. This is similar to the address computation used in the heap of “Implicit data structures”, a binary tree where eac...
algorithms and data structures_Page_257_Chunk4797
This book is licensed under a Creative Commons Attribution 3.0 License phenomenon. Central memory is organized in small physical units (a byte, a word) with access times of approximately 1 microsecond, 10–6 second. Disks are organizein large physical blocks (512 bytes to 5kilobytes) with access times ranging from 10 to...
algorithms and data structures_Page_258_Chunk4798
23. Metric data structures Uniformly high memory utilization. Any data structure that adapts its shape to dynamically changing contents is likely to leave "unused holes" in storage space: space that is currently unused, and that cannot conveniently be used for other purposes because it is fragmented. We have encountere...
algorithms and data structures_Page_259_Chunk4799
This book is licensed under a Creative Commons Attribution 3.0 License Exhibit 23.3: Cells of a grid partition adapt their size so that no cell is populated by more than c points. A grid file for this database contains the following components: • Linear scales show how the domain is currently partitioned. • The directo...
algorithms and data structures_Page_260_Chunk4800