Functional Data Structures

Total Page:16

File Type:pdf, Size:1020Kb

Functional Data Structures Functional Data Structures Tobias Nipkow February 20, 2021 Abstract A collection of verified functional data structures. The emphasis is on conciseness of algorithms and succinctness of proofs, more in the style of a textbook than a library of efficient algorithms. For more details see [13]. Contents 1 Sorting 4 2 Creating Almost Complete Trees 13 3 Three-Way Comparison 19 4 Lists Sorted wrt < 20 5 List Insertion and Deletion 21 6 Specifications of Set ADT 24 7 Unbalanced Tree Implementation of Set 26 8 Association List Update and Deletion 29 9 Specifications of Map ADT 32 10 Unbalanced Tree Implementation of Map 34 11 Augmented Tree (Tree2) 36 12 Function isin for Tree2 37 13 Interval Trees 37 14 AVL Tree Implementation of Sets 44 1 15 Function lookup for Tree2 54 16 AVL Tree Implementation of Maps 55 17 AVL Tree with Balance Factors (1) 59 18 AVL Tree with Balance Factors (2) 63 19 Height-Balanced Trees 68 20 Red-Black Trees 77 21 Red-Black Tree Implementation of Sets 78 22 Alternative Deletion in Red-Black Trees 85 23 Red-Black Tree Implementation of Maps 89 24 2-3 Trees 91 25 2-3 Tree Implementation of Sets 92 26 2-3 Tree Implementation of Maps 101 27 2-3 Tree from List 105 28 2-3-4 Trees 108 29 2-3-4 Tree Implementation of Sets 109 30 2-3-4 Tree Implementation of Maps 122 31 1-2 Brother Tree Implementation of Sets 126 32 1-2 Brother Tree Implementation of Maps 139 33 AA Tree Implementation of Sets 144 34 AA Tree Implementation of Maps 156 35 Join-Based Implementation of Sets 161 36 Join-Based Implementation of Sets via RBTs 169 37 Braun Trees 176 38 Arrays via Braun Trees 180 2 39 Tries via Functions 195 40 Tries via Search Trees 197 41 Binary Tries and Patricia Tries 200 42 Queue Specification 206 43 Queue Implementation via 2 Lists 207 44 Priority Queue Specifications 209 45 Heaps 210 46 Leftist Heap 212 47 Binomial Heap 217 48 Time functions for various standard library operations 232 49 The Median-of-Medians Selection Algorithm 233 50 Bibliographic Notes 258 3 1 Sorting theory Sorting imports Complex Main HOL−Library:Multiset begin hide const List:insort declare Let def [simp] 1.1 Insertion Sort fun insort :: 0a::linorder ) 0a list ) 0a list where insort x [] = [x] j insort x (y#ys) = (if x ≤ y then x#y#ys else y#(insort x ys)) fun isort :: 0a::linorder list ) 0a list where isort [] = [] j isort (x#xs) = insort x (isort xs) 1.1.1 Functional Correctness lemma mset insort: mset (insort x xs) = f#x#g + mset xs apply(induction xs) apply auto done lemma mset isort: mset (isort xs) = mset xs apply(induction xs) apply simp apply (simp add: mset insort) done lemma set insort: set (insort x xs) = fxg [ set xs by(simp add: mset insort flip: set mset mset) lemma sorted insort: sorted (insort a xs) = sorted xs apply(induction xs) apply(auto simp add: set insort) done lemma sorted isort: sorted (isort xs) 4 apply(induction xs) apply(auto simp: sorted insort) done 1.1.2 Time Complexity We count the number of function calls. insort x [] = [x] insort x (y#ys) = (if x ≤ y then x#y#ys else y#(insort x ys)) fun T insort :: 0a::linorder ) 0a list ) nat where T insort x [] = 1 j T insort x (y#ys) = (if x ≤ y then 0 else T insort x ys) + 1 isort [] = [] isort (x#xs) = insort x (isort xs) fun T isort :: 0a::linorder list ) nat where T isort [] = 1 j T isort (x#xs) = T isort xs + T insort x (isort xs) + 1 lemma T insort length: T insort x xs ≤ length xs + 1 apply(induction xs) apply auto done lemma length insort: length (insort x xs) = length xs + 1 apply(induction xs) apply auto done lemma length isort: length (isort xs) = length xs apply(induction xs) apply (auto simp: length insort) done lemma T isort length: T isort xs ≤ (length xs + 1 ) ^ 2 proof(induction xs) case Nil show ?case by simp next case (Cons x xs) have T isort (x#xs) = T isort xs + T insort x (isort xs) + 1 by simp also have ::: ≤ (length xs + 1 ) ^ 2 + T insort x (isort xs) + 1 using Cons:IH by simp 5 also have ::: ≤ (length xs + 1 ) ^ 2 + length xs + 1 + 1 using T insort length[of x isort xs] by (simp add: length isort) also have ::: ≤ (length(x#xs) + 1 ) ^ 2 by (simp add: power2 eq square) finally show ?case . qed 1.2 Merge Sort fun merge :: 0a::linorder list ) 0a list ) 0a list where merge [] ys = ys j merge xs [] = xs j merge (x#xs)(y#ys) = (if x ≤ y then x # merge xs (y#ys) else y # merge (x#xs) ys) fun msort :: 0a::linorder list ) 0a list where msort xs = (let n = length xs in if n ≤ 1 then xs else merge (msort (take (n div 2 ) xs)) (msort (drop (n div 2 ) xs))) declare msort:simps [simp del] 1.2.1 Functional Correctness lemma mset merge: mset(merge xs ys) = mset xs + mset ys by(induction xs ys rule: merge:induct) auto lemma mset msort: mset (msort xs) = mset xs proof(induction xs rule: msort:induct) case (1 xs) let ?n = length xs let ?ys = take (?n div 2 ) xs let ?zs = drop (?n div 2 ) xs show ?case proof cases assume ?n ≤ 1 thus ?thesis by(simp add: msort:simps[of xs]) next assume : ?n ≤ 1 hence mset (msort xs) = mset (msort ?ys) + mset (msort ?zs) by(simp add: msort:simps[of xs] mset merge) also have ::: = mset ?ys + mset ?zs using h: ?n ≤ 1 i by(simp add: 1 :IH ) also have ::: = mset (?ys @ ?zs) by (simp del: append take drop id) 6 also have ::: = mset xs by simp finally show ?thesis . qed qed Via the previous lemma or directly: lemma set merge: set(merge xs ys) = set xs [ set ys by (metis mset merge set mset mset set mset union) lemma set(merge xs ys) = set xs [ set ys by(induction xs ys rule: merge:induct)(auto) lemma sorted merge: sorted (merge xs ys) ! (sorted xs ^ sorted ys) by(induction xs ys rule: merge:induct)(auto simp: set merge) lemma sorted msort: sorted (msort xs) proof(induction xs rule: msort:induct) case (1 xs) let ?n = length xs show ?case proof cases assume ?n ≤ 1 thus ?thesis by(simp add: msort:simps[of xs] sorted01 ) next assume : ?n ≤ 1 thus ?thesis using 1 :IH by(simp add: sorted merge msort:simps[of xs]) qed qed 1.2.2 Time Complexity We only count the number of comparisons between list elements. fun C merge :: 0a::linorder list ) 0a list ) nat where C merge [] ys = 0 j C merge xs [] = 0 j C merge (x#xs)(y#ys) = 1 + (if x ≤ y then C merge xs (y#ys) else C merge (x#xs) ys) lemma C merge ub: C merge xs ys ≤ length xs + length ys by (induction xs ys rule: C merge:induct) auto fun C msort :: 0a::linorder list ) nat where C msort xs = 7 (let n = length xs; ys = take (n div 2 ) xs; zs = drop (n div 2 ) xs in if n ≤ 1 then 0 else C msort ys + C msort zs + C merge (msort ys)(msort zs)) declare C msort:simps [simp del] lemma length merge: length(merge xs ys) = length xs + length ys apply (induction xs ys rule: merge:induct) apply auto done lemma length msort: length(msort xs) = length xs proof (induction xs rule: msort:induct) case (1 xs) show ?case by (auto simp: msort:simps [of xs] 1 length merge) qed Why structured proof? To have the name "xs" to specialize msort.simps with xs to ensure that msort.simps cannot be used recursively. Also works without this precaution, but that is just luck. lemma C msort le: length xs = 2^k =) C msort xs ≤ k ∗ 2^k proof(induction k arbitrary: xs) case 0 thus ?case by (simp add: C msort:simps) next case (Suc k) let ?n = length xs let ?ys = take (?n div 2 ) xs let ?zs = drop (?n div 2 ) xs show ?case proof (cases ?n ≤ 1 ) case True thus ?thesis by(simp add: C msort:simps) next case False have C msort(xs) = C msort ?ys + C msort ?zs + C merge (msort ?ys)(msort ?zs) by (simp add: C msort:simps msort:simps) also have ::: ≤ C msort ?ys + C msort ?zs + length ?ys + length ?zs using C merge ub[of msort ?ys msort ?zs] length msort[of ?ys] length msort[of ?zs] by arith 8 also have ::: ≤ k ∗ 2^k + C msort ?zs + length ?ys + length ?zs using Suc:IH [of ?ys] Suc:prems by simp also have ::: ≤ k ∗ 2^k + k ∗ 2^k + length ?ys + length ?zs using Suc:IH [of ?zs] Suc:prems by simp also have ::: = 2 ∗ k ∗ 2^k + 2 ∗ 2 ^ k using Suc:prems by simp finally show ?thesis by simp qed qed lemma C msort log: length xs = 2^k =) C msort xs ≤ length xs ∗ log 2 (length xs) using C msort le[of xs k] apply (simp add: log nat power algebra simps) by (metis (mono tags) numeral power eq of nat cancel iff of nat le iff of nat mult) 1.3 Bottom-Up Merge Sort fun merge adj :: ( 0a::linorder) list list ) 0a list list where merge adj [] = [] j merge adj [xs] = [xs] j merge adj (xs # ys # zss) = merge xs ys # merge adj zss For the termination proof of merge all below.
Recommended publications
  • Curriculum for Second Year of Computer Engineering (2019 Course) (With Effect from 2020-21)
    Faculty of Science and Technology Savitribai Phule Pune University Maharashtra, India Curriculum for Second Year of Computer Engineering (2019 Course) (With effect from 2020-21) www.unipune.ac.in Savitribai Phule Pune University Savitribai Phule Pune University Bachelor of Computer Engineering Program Outcomes (PO) Learners are expected to know and be able to– PO1 Engineering Apply the knowledge of mathematics, science, Engineering fundamentals, knowledge and an Engineering specialization to the solution of complex Engineering problems PO2 Problem analysis Identify, formulate, review research literature, and analyze complex Engineering problems reaching substantiated conclusions using first principles of mathematics natural sciences, and Engineering sciences PO3 Design / Development Design solutions for complex Engineering problems and design system of Solutions components or processes that meet the specified needs with appropriate consideration for the public health and safety, and the cultural, societal, and Environmental considerations PO4 Conduct Use research-based knowledge and research methods including design of Investigations of experiments, analysis and interpretation of data, and synthesis of the Complex Problems information to provide valid conclusions. PO5 Modern Tool Usage Create, select, and apply appropriate techniques, resources, and modern Engineering and IT tools including prediction and modeling to complex Engineering activities with an understanding of the limitations PO6 The Engineer and Apply reasoning informed by
    [Show full text]
  • Full Functional Verification of Linked Data Structures
    Full Functional Verification of Linked Data Structures Karen Zee Viktor Kuncak Martin C. Rinard MIT CSAIL, Cambridge, MA, USA EPFL, I&C, Lausanne, Switzerland MIT CSAIL, Cambridge, MA, USA ∗ [email protected] viktor.kuncak@epfl.ch [email protected] Abstract 1. Introduction We present the first verification of full functional correctness for Linked data structures such as lists, trees, graphs, and hash tables a range of linked data structure implementations, including muta- are pervasive in modern software systems. But because of phenom- ble lists, trees, graphs, and hash tables. Specifically, we present the ena such as aliasing and indirection, it has been a challenge to de- use of the Jahob verification system to verify formal specifications, velop automated reasoning systems that are capable of proving im- written in classical higher-order logic, that completely capture the portant correctness properties of such data structures. desired behavior of the Java data structure implementations (with the exception of properties involving execution time and/or mem- 1.1 Background ory consumption). Given that the desired correctness properties in- In principle, standard specification and verification approaches clude intractable constructs such as quantifiers, transitive closure, should work for linked data structure implementations. But in prac- and lambda abstraction, it is a challenge to successfully prove the tice, many of the desired correctness properties involve logical generated verification conditions. constructs such transitive closure and
    [Show full text]
  • CSI33 Data Structures
    Outline CSI33 Data Structures Department of Mathematics and Computer Science Bronx Community College December 2, 2015 CSI33 Data Structures Outline Outline 1 Chapter 13: Heaps, Balances Trees and Hash Tables Hash Tables CSI33 Data Structures Chapter 13: Heaps, Balances Trees and Hash TablesHash Tables Outline 1 Chapter 13: Heaps, Balances Trees and Hash Tables Hash Tables CSI33 Data Structures Chapter 13: Heaps, Balances Trees and Hash TablesHash Tables Python Dictionaries Various names are given to the abstract data type we know as a dictionary in Python: Hash (The languages Perl and Ruby use this terminology; implementation is a hash table). Map (Microsoft Foundation Classes C/C++ Library; because it maps keys to values). Dictionary (Python, Smalltalk; lets you "look up" a value for a key). Association List (LISP|everything in LISP is a list, but this type is implemented by hash table). Associative Array (This is the technical name for such a structure because it looks like an array whose 'indexes' in square brackets are key values). CSI33 Data Structures Chapter 13: Heaps, Balances Trees and Hash TablesHash Tables Python Dictionaries In Python, a dictionary associates a value (item of data) with a unique key (to identify and access the data). The implementation strategy is the same in any language that uses associative arrays. Representing the relation between key and value is a hash table. CSI33 Data Structures Chapter 13: Heaps, Balances Trees and Hash TablesHash Tables Python Dictionaries The Python implementation uses a hash table since it has the most efficient performance for insertion, deletion, and lookup operations. The running times of all these operations are better than any we have seen so far.
    [Show full text]
  • CIS 120 Midterm I October 13, 2017 Name (Printed): Pennkey (Penn
    CIS 120 Midterm I October 13, 2017 Name (printed): PennKey (penn login id): My signature below certifies that I have complied with the University of Pennsylvania’s Code of Academic Integrity in completing this examination. Signature: Date: • Do not begin the exam until you are told it is time to do so. • Make sure that your username (a.k.a. PennKey, e.g. stevez) is written clearly at the bottom of every page. • There are 100 total points. The exam lasts 50 minutes. Do not spend too much time on any one question. • Be sure to recheck all of your answers. • The last page of the exam can be used as scratch space. By default, we will ignore anything you write on this page. If you write something that you want us to grade, make sure you mark it clearly as an answer to a problem and write a clear note on the page with that problem telling us to look at the scratch page. • Good luck! 1 1. Binary Search Trees (16 points total) This problem concerns buggy implementations of the insert and delete functions for bi- nary search trees, the correct versions of which are shown in Appendix A. First: At most one of the lines of code contains a compile-time (i.e. typechecking) error. If there is a compile-time error, explain what the error is and one way to fix it. If there is no compile-time error, say “No Error”. Second: even after the compile-time error (if any) is fixed, the code is still buggy—for some inputs the function works correctly and produces the correct BST, and for other inputs, the function produces an incorrect tree.
    [Show full text]
  • Verifying Linked Data Structure Implementations
    Verifying Linked Data Structure Implementations Karen Zee Viktor Kuncak Martin Rinard MIT CSAIL EPFL I&C MIT CSAIL Cambridge, MA Lausanne, Switzerland Cambridge, MA [email protected] viktor.kuncak@epfl.ch [email protected] Abstract equivalent conjunction of properties. By processing each conjunct separately, Jahob can use different provers to es- The Jahob program verification system leverages state of tablish different parts of the proof obligations. This is the art automated theorem provers, shape analysis, and de- possible thanks to formula approximation techniques[10] cision procedures to check that programs conform to their that create equivalent or semantically stronger formulas ac- specifications. By combining a rich specification language cepted by the specialized decision procedures. with a diverse collection of verification technologies, Jahob This paper presents our experience using the Jahob sys- makes it possible to verify complex properties of programs tem to obtain full correctness proofs for a collection of that manipulate linked data structures. We present our re- linked data structure implementations. Unlike systems that sults using Jahob to achieve full functional verification of a are designed to verify partial correctness properties, Jahob collection of linked data structures. verifies the full functional correctness of the data structure implementation. Not only do our results show that such full functional verification is feasible, the verified data struc- 1 Introduction tures and algorithms provide concrete examples that can help developers better understand how to achieve similar results in future efforts. Linked data structures such as lists, trees, graphs, and hash tables are pervasive in modern software systems. But because of phenomena such as aliasing and indirection, it 2 Example has been a challenge to develop automated reasoning sys- tems that are capable of proving important correctness prop- In this section we use a verified association list to demon- erties of such data structures.
    [Show full text]
  • CSCI 2041: Data Types in Ocaml
    CSCI 2041: Data Types in OCaml Chris Kauffman Last Updated: Thu Oct 18 22:43:05 CDT 2018 1 Logistics Assignment 3 multimanager Reading I Manage multiple lists I OCaml System Manual: I Records to track lists/undo Ch 1.4, 1.5, 1.7 I option to deal with editing I Practical OCaml: Ch 5 I Higher-order funcs for easy bulk operations Goals I Post tomorrow I Tuples I Due in 2 weeks I Records I Algebraic / Variant Types Next week First-class / Higher Order Functions 2 Overview of Aggregate Data Structures / Types in OCaml I Despite being an older functional language, OCaml has a wealth of aggregate data types I The table below describe some of these with some characteristics I We have discussed Lists and Arrays at some length I We will now discuss the others Elements Typical Access Mutable Example Lists Homoegenous Index/PatMatch No [1;2;3] Array Homoegenous Index Yes [|1;2;3|] Tuples Heterogeneous PatMatch No (1,"two",3.0) Records Heterogeneous Field/PatMatch No/Yes {name="Sam"; age=21} Variant Not Applicable PatMatch No type letter = A | B | C; Note: data types can be nested and combined in any way I Array of Lists, List of Tuples I Record with list and tuple fields I Tuple of list and Record I Variant with List and Record or Array and Tuple 3 Tuples # let int_pair = (1,2);; val int_pair : int * int = (1, 2) I Potentially mixed data # let mixed_triple = (1,"two",3.0);; I Commas separate elements val mixed_triple : int * string * float = (1, "two", 3.) I Tuples: pairs, triples, quadruples, quintuples, etc.
    [Show full text]
  • Simple Balanced Binary Search Trees
    Simple Balanced Binary Search Trees Prabhakar Ragde Cheriton School of Computer Science University of Waterloo Waterloo, Ontario, Canada [email protected] Efficient implementations of sets and maps (dictionaries) are important in computer science, and bal- anced binary search trees are the basis of the best practical implementations. Pedagogically, however, they are often quite complicated, especially with respect to deletion. I present complete code (with justification and analysis not previously available in the literature) for a purely-functional implemen- tation based on AA trees, which is the simplest treatment of the subject of which I am aware. 1 Introduction Trees are a fundamental data structure, introduced early in most computer science curricula. They are easily motivated by the need to store data that is naturally tree-structured (family trees, structured doc- uments, arithmetic expressions, and programs). We also expose students to the idea that we can impose tree structure on data that is not naturally so, in order to implement efficient manipulation algorithms. The typical first example is the binary search tree. However, they are problematic. Naive insertion and deletion are easy to present in a first course using a functional language (usually the topic is delayed to a second course if an imperative language is used), but in the worst case, this im- plementation degenerates to a list, with linear running time for all operations. The solution is to balance the tree during operations, so that a tree with n nodes has height O(logn). There are many different ways of doing this, but most are too complicated to present this early in the curriculum, so they are usually deferred to a later course on algorithms and data structures, leaving a frustrating gap.
    [Show full text]
  • Alexandria Manual
    alexandria Manual draft version Alexandria software and associated documentation are in the public domain: Authors dedicate this work to public domain, for the benefit of the public at large and to the detriment of the authors' heirs and successors. Authors intends this dedication to be an overt act of relinquishment in perpetuity of all present and future rights under copyright law, whether vested or contingent, in the work. Authors understands that such relinquishment of all rights includes the relinquishment of all rights to enforce (by lawsuit or otherwise) those copyrights in the work. Authors recognize that, once placed in the public domain, the work may be freely reproduced, distributed, transmitted, used, modified, built upon, or oth- erwise exploited by anyone for any purpose, commercial or non-commercial, and in any way, including by methods that have not yet been invented or conceived. In those legislations where public domain dedications are not recognized or possible, Alexan- dria is distributed under the following terms and conditions: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTIC- ULAR PURPOSE AND NONINFRINGEMENT.
    [Show full text]
  • Wrapped Kd Trees Handed
    CMSC 420: Spring 2021 Programming Assignment 2: Wrapped k-d Trees Handed out: Tue, Apr 6. Due: Wed, Apr 21, 11pm. (Submission via Gradescope.) Overview: In this assignment you will implement a variant of the kd-tree data structure, called a wrapped kd-tree (or WKDTree) to store a set of points in 2-dimensional space. This data structure will support insertion, deletion, and a number of geometric queries, some of which will be used later in Part 3 of the programming assignment. The data structure will be templated with the point type, which is any class that implements the Java interface LabeledPoint2D, as described in the file LabeledPoint2D.java from the provided skeleton code. A labeled point is a 2-dimensional point (Point2D from the skeleton code) that supports an additional function getLabel(). This returns a string associated with the point. In our case, the points will be the airports from our earlier projects, and the labels will be the 3-letter airport codes. The associated point (represented as a Point2D) can be extracted using the function getPoint2D(). The individual coordinates (which are floats) can be extracted directly using the functions getX() and getY(), or get(i), where i = 0 for x and i = 1 for y. Your wrapped kd-tree will be templated with one type, which we will call LPoint (for \labeled point"). For example, your file WKDTree will contain the following public class: public class WKDTree<LPoint extends LabeledPoint2D> { ... } Wrapped kd-Tree: Recall that a kd-tree is a data structure based on a hierarchical decomposition of space, using axis-orthogonal splits.
    [Show full text]
  • Slides-Data-Structures.Pdf
    Data structures ● Organize your data to support various queries using little time an space Example: Inventory Want to support SEARCH INSERT DELETE ● Given n elements A[1..n] ● Support SEARCH(A,x) := is x in A? ● Trivial solution: scan A. Takes time Θ(n) ● Best possible given A, x. ● What if we are first given A, are allowed to preprocess it, can we then answer SEARCH queries faster? ● How would you preprocess A? ● Given n elements A[1..n] ● Support SEARCH(A,x) := is x in A? ● Preprocess step: Sort A. Takes time O(n log n), Space O(n) ● SEARCH(A[1..n],x) := /* Binary search */ If n = 1 then return YES if A[1] = x, and NO otherwise else if A[n/2] ≤ x then return SEARCH(A[n/2..n]) else return SEARCH(A[1..n/2]) ● Time T(n) = ? ● Given n elements A[1..n] ● Support SEARCH(A,x) := is x in A? ● Preprocess step: Sort A. Takes time O(n log n), Space O(n) ● SEARCH(A[1..n],x) := /* Binary search */ If n = 1 then return YES if A[1] = x, and NO otherwise else if A[n/2] ≤ x then return SEARCH(A[n/2..n]) else return SEARCH(A[1..n/2]) ● Time T(n) = O(log n). ● Given n elements A[1..n] each ≤ k, can you do faster? ● Support SEARCH(A,x) := is x in A? ● DIRECTADDRESS: Initialize S[1..k] to 0 ● Preprocess step: For (i = 1 to n) S[A[i]] = 1 ● T(n) = O(n), Space O(k) ● SEARCH(A,x) = ? ● Given n elements A[1..n] each ≤ k, can you do faster? ● Support SEARCH(A,x) := is x in A? ● DIRECTADDRESS: Initialize S[1..k] to 0 ● Preprocess step: For (i = 1 to n) S[A[i]] = 1 ● T(n) = O(n), Space O(k) ● SEARCH(A,x) = return S[x] ● T(n) = O(1) ● Dynamic problems: ● Want to support SEARCH, INSERT, DELETE ● Support SEARCH(A,x) := is x in A? ● If numbers are small, ≤ k Preprocess: Initialize S to 0.
    [Show full text]
  • TAL Recognition in O(M(N2)) Time
    TAL Recognition in O(M(n2)) Time Sanguthevar Rajasekaran Dept. of CISE, Univ. of Florida raj~cis.ufl.edu Shibu Yooseph Dept. of CIS, Univ. of Pennsylvania [email protected] Abstract time needed to multiply two k x k boolean matri- ces. The best known value for M(k) is O(n 2"3vs) We propose an O(M(n2)) time algorithm (Coppersmith, Winograd, 1990). Though our algo- for the recognition of Tree Adjoining Lan- rithm is similar in flavor to those of Graham, Har- guages (TALs), where n is the size of the rison, & Ruzzo (1976), and Valiant (1975) (which input string and M(k) is the time needed were Mgorithms proposed for recognition of Con- to multiply two k x k boolean matrices. text Pree Languages (CFLs)), there are crucial dif- Tree Adjoining Grammars (TAGs) are for- ferences. As such, the techniques of (Graham, et al., malisms suitable for natural language pro- 1976) and (Valiant, 1975) do not seem to extend to cessing and have received enormous atten- TALs (Satta, 1993). tion in the past among not only natural language processing researchers but also al- gorithms designers. The first polynomial 2 Tree Adjoining Grammars time algorithm for TAL parsing was pro- A Tree Adjoining Grammar (TAG) consists of a posed in 1986 and had a run time of O(n6). quintuple (N, ~ U {~}, I, A, S), where Quite recently, an O(n 3 M(n)) algorithm N is a finite set of has been proposed. The algorithm pre- nonterminal symbols, sented in this paper improves the run time is a finite set of terminal symbols disjoint from of the recent result using an entirely differ- N, ent approach.
    [Show full text]
  • Appendix a Answers to Some of the Exercises
    Appendix A Answers to Some of the Exercises Answers for Chapter 2 2. Let f (x) be the number of a 's minus the number of b 's in string x. A string is in the language defined by the grammar just when • f(x) = 1 and • for every proper prefix u of x, f( u) < 1. 3. Let the string be given as array X [O .. N -1] oflength N. We use the notation X ! i, (pronounced: X drop i) for 0:::; i :::; N, to denote the tail of length N - i of array X, that is, the sequence from index ion. (Or: X from which the first i elements have been dropped.) We write L for the language denoted by the grammar. Lk is the language consisting of the catenation of k strings from L. We are asked to write a program for computing the boolean value X E L. This may be written as X ! 0 E L1. The invariant that we choose is obtained by generalizing constants 0 and 1 to variables i and k: P : (X E L = X ! i ELk) /\ 0:::; i :::; N /\ 0:::; k We use the following properties: (0) if Xli] = a, i < N, k > 0 then X ! i E Lk = X ! i + 1 E Lk-1 (1) if Xli] = b, i < N, k ~ 0 then X ! i E Lk = X ! i + 1 E Lk+1 (2) X ! N E Lk = (k = 0) because X ! N = 10 and 10 E Lk for k = 0 only (3) X ! i E LO = (i = N) because (X ! i = 10) = (i = N) and LO = {€} The program is {N ~ O} k := Ij i:= OJ {P} *[ k =1= 0/\ i =1= N -+ [ Xli] = a -+ k := k - 1 404 Appendix A.
    [Show full text]