Algorithms and data structures with applications to graphics and geometry
All A I' I rrL v g S I 3 Ii p 1 p I 1, : I$',i,.d]' ]1, I 1 1' ALGORITHMS AND DATA STRUCTURES With Applications to Graphics and Geometry JURG NIEVERGELT ETH Zurich KLAUS H. HINRICHS University of Miinster PRENTICE HALL, Englewood Cliffs, New Jersey 07632 Library of Congress Cataloging-in-Publication Data NIEVERGELT, JURG. Algorithms and data structures: with applications to graphics and geometry / Jurg Nievergelt, Klaus Hinrichs. p. cm. Includes bibliographical references and indexes. ISBN 0-13-489428-6 1. Computer algorithms. 2. Computer structures (Computer science) 3. Computer graphics. 1. Hinrichs, Klaus. 11. Title. QA76.9.A43N54 1993 005.1- - dc2O 92-4687 CIP Acquisitions editor: Marcia Horton Prepress buyer: Linda Behrens Production editor: Jennifer Wenzel Manufacturing buyer: Dave Dickey Copy editor: Barbara Zeiders Supplements editor: Alice Dworkin Cover designer: Miguel Ortiz Editorial assistant: Dolores Mars = 1993 by Prentice-Hall, Inc. A Simon & Schuster Company I= Englewood Cliffs, New Jersey 07632 The author and publisher of this book have used their best efforts in preparing this book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The author and publisher make no warranty of any kind, expressed or implied, with regard to these programs or the documentation contained in this book. The author and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs. All rights reserved. No part of this book may be reproduced, in any form or by any means, without permission in writing from the publisher. Printed in the United States of America 10 9 8 7 6 5 4 3 2 1 ISBN 0-13-489428-6 Prentice-Hall International (UK) Limited, London Prentice-Hall of Australia Pty. Limited, Sydney Prentice-Hall Canada Inc., Toronto Prentice-Hall Hispanoamericana, S.A., Mexico Prentice-Hall of India Private Limited, New Delhi Prentice-Hall of Japan, Inc., Tokyo Simon & Schuster Asia Pte. Ltd., Singapore Editora Prentice-Hall do Brasil, Ltda., Rio de Janeiro for Tess and Katharina Contents PREFACE Part I Programming environments for motion, graphics, and geometry 1 REDUCING A TASK TO GIVEN PRIMITIVES: PROGRAMMING MOTION 1.1 A robot car, its capabilities, and the task to be performed 3 1.2 Wall-following algorithm described informally 5 1.3 Algorithm specified in a high-level language 6 1.4 Algorithm programmed in the robot's language 6 1.5 The robot's program optimized 7 2 GRAPHICS PRIMITIVES AND ENVIRONMENTS 2.1 2.2 2.3 Turtle graphics: A basic environment 9 QuickDraw: A graphics toolbox 12 A graphics frame program 15 xiii 1 3 9 V 2.4 Example of a graphics routine: Polyline input 18 3 ALGORITHM ANIMATION 20 3.1 Computer-driven visualization: Characteristics and techniques 20 3.2 Example: The convex hull of points in the plane 22 3.3 A gallery of algorithm snapshots 24 Part II Programming concepts: Beyond notation 31 4 ALGORITHMS AND PROGRAMS AS LITERATURE: SUBSTANCE AND FORM 32 4.1 Programming in the large versus programming in the small 32 4.2 Documentation versus literature: Is it meant to be read? 33 4.3 Pascal and its dialects: Lingua franca of computer science 39 5 DIVIDE-AND-CONQUER AND RECURSION 44 5.1 An algorithmic principle 44 5.2 Divide-and-conquer expressed as a diagram: Merge sort 45 5.3 Recursively defined trees 46 5.4 Recursive tree traversal 48 5.5 Recursion versus iteration: The Tower of Hanoi 49 5.6 The flag of Alfanumerica: An algorithmic novel on iteration and recursion 52 6 SYNTAX 54 6.1 Syntax and semantics 54 6.2 Grammars and their representation: Syntax diagrams and EBNF 55 6.3 Example: Syntax of simple expressions 57 vi Contents 6.4 An overly simple syntax for simple expressions 59 6.5 Parenthesis-free notation for arithmetic expressions 60 7 SYNTAX ANALYSIS 63 7.1 The role of syntax analysis 63 7.2 Syntax analysis of parenthesis-free expressions by counting 64 7.3 Analysis by recursive descent 66 7.4 Turning syntax diagrams into a parser 66 Part III Objects, algorithms, programs 69 8 TRUTH VALUES, THE DATA TYPE 'SET', AND BIT ACROBATICS 71 8.1 Bits and boolean functions 71 8.2 Swapping and crossovers: The versatile exclusive-or 72 8.3 The bit sum or "population count" 73 9 ORDERED SETS 81 9.1 Sequential search 82 9.2 Binary search 83 9.3 In-place permutation 86 10 STRINGS 93 10.1 Recognizing a pattern consisting of a single string 93 10.2 Recognizing a set of strings: A finite-state-machine interpreter 94 11 MATRICES AND GRAPHS: TRANSITIVE CLOSURE 99 11.1 Paths in a graph 99 11.2 Boolean matrix multiplication 100 11.3 Warshall's algorithm 102 11.4 Minimum spanning tree in a graph 104 Contents vii 12 INTEGERS 107 12.1 Operations on integers 107 12.2 The Euclidean algorithm 110 12.3 The prime number sieve of Eratosthenes 111 12.4 Large integers 112 12.5 Modular number systems: The poor man's large integers 113 12.6 Random numbers 116 13 REALS 119 13.1 Floating point numbers 119 13.2 Some dangers 121 13.3 Homer's method 123 13.4 Bisection 124 13.5 Newton's method for computing the square root 125 14 STRAIGHT LINES AND CIRCLES 129 14.1 Intersection 129 14.2 Clipping 132 14.3 Drawing digitized lines 134 14.4 The riddle of the braiding straight lines 137 14.5 Digitized circles 142 Part IV Complexity of problems and algorithms 147 15 COMPUTABILITY AND COMPLEXITY 148 15.1 Models of computation: The ultimate RISC 148 15.2 Almost nothing is computable 152 15.3 The halting problem is undecidable 153 15.4 Computable, yet unknown 154 15.5 Multiplication of complex numbers 156 vill Contents 15.6 Complexity of matrix multiplication 157 16 THE MATHEMATICS OF ALGORITHM ANALYSIS 161 16.1 Growth rates and orders of magnitude 161 16.2 Asymptotics 163 16.3 Summation formulas 164 16.4 Recurrence relations 166 16.5 Asymptotic performance of divide-and-conquer algorithms 169 16.6 Permutations 170 16.7 Trees 171 17 SORTING AND ITS COMPLEXITY 174 17.1 What is sorting? How difficult is it? 174 17.2 Types of sorting algorithms 177 17.3 Simple sorting algorithms that work in time 0(n 2 ) 180 17.4 A lower bound Q(n logn) 181 17.5 Quicksort 183 17.6 Analysis for three cases: best, "typical," and worst 186 17.7 Merging and merge sorts 188 17.8 Is it possible to sort in linear time? 191 17.9 Sorting networks 192 Part V Data structures 197 18 WHAT IS A DATA STRUCTURE? 199 18.1 Data structures old and new 199 18.2 The range of data structures studied 201 18.3 Performance criteria and measures 202 19 ABSTRACT DATA TYPES 204 19.1 Concepts: What and why? 204 Contents ix Contents 19.2 Stack 206 19.3 First-in-first-out queue 210 19.4 Priority queue 212 19.5 Dictionary 213 20 IMPLICIT DATA STRUCTURES 218 20.1 What is an implicit data structure? 218 20.2 Array storage 219 20.3 Implementation of the fixed-length fifo queue as a circular buffer 224 20.4 Implementation of the fixed-length priority queue as a heap 227 20.5 Heapsort 232 21 LIST STRUCTURES 234 21.1 Lists, memory management, pointer variables 234 21.2 The fifo queue implemented as a one-way list 237 21.3 Tree traversal 238 21.4 Binary search trees 248 21.5 Balanced trees: General definition 252 21.6 Height-balanced trees 254 21.7 Multiway trees 260 22 ADDRESS COMPUTATION 267 22.1 Concepts and terminology 267 22.2 The special case of small key domains 269 22.3 The special case of perfect hashing: Table contents known a priori 269 22.4 Conventional hash tables: Collision resolution 271 22.5 Choice of hash function: Randomization 276 22.6 Performance analysis 278 22.7 Extendible hashing 279 X [...]... the complementary primitive 'if not touch goto #', we can simplify the flow of the program at the left as shown on the right side 2 3 [ wall to left-rear } left if touch goto 4 goto 6 I 2 [ wall to right-front } 4 5 6 7 right goto 2 forward goto I 4 5 6 7 £ wall to left-rear] left if not touch goto 6 { wall to right-front } right goto 2 forward goto I Reducing a Task to Given Primitives: Programming Motion... Exercises and examples are sprinkled throughout the text, some with solutions, some without A generic exercise and programming project that applies to almost any topic treated in this book is to program and animate the execution of some algorithm on a sample data configuration To acquaint the student early with the necessary concepts and techniques, Part I is dedicated to graphics programming, and Chapter... many books on data structures which emphasize lists and comparative search techniques, we attach equal importance to address computation and metric data structures that partition space according to predefined grids Part VI, "Interaction Between Algorithms and Data Structures: Case Studies in Geometric Computation" has a threefold goal First, we want to show convincingly that the data structures of... forever; 4 5 6 7 left if touch goto 4 goto 6 right goto 2 forward goto I left I I wall to left-rear wall to left-front -I 4 - C right wail to right-front wall to left-front forward l Figure 1.6 wall to left-rear wall to left-front Robot motions as predicate transformers 1.5 THE ROBOT'S PROGRAM OPTIMIZED In designing a program it is best to follow simple, general ideas, and to decide on details in... commands: left right forward goto # if touch goto # Turn 90 degrees counterclockwise Turn 90 degrees clockwise Move one step, to the next grid point in front of you Send program control to the label # If you are touching a wall to your front, send program control to the label # A program for the robot is a sequence of commands with distinct labels The labels serve merely to identify the commands and. .. Execution begins with the first command and proceeds to successive commands in the order in which they appear, except when flow of control is redirected by either of the goto commands Example The following program moves the robot forward until it bumps into a wall: I 2 3 4 if touch goto 4 forward goto 1 { there is no command here; just a label } In developing programs for the robot, we feel free to use any... choice of topics In PartI, "Programming Environments for Motion, Graphics, and Geometry" , we assume that the reader has worked with one or two computers, operating systems, and languages, and may now have to master a new programming environment that supports graphics We use this opportunity to reinforce the idea that programming is the reduction of a conceived solution to given primitive operations, and. .. any specific environment We begin by defining a toy environment designed to set the tone and highlight simple concepts necessary for programming motion, geometry, and graphics Next, we ask the questions: What programming environment would we like to have, what are we likely to find, and what can we do to narrow the gap? Recognizing the fact that most of today's programs are interactive but that programming... Starting with an intuitive introduction to computability and its refinement, the notion of complexity, we survey the mathematical tools commonly used in algorithm analysis We illustrate the use of these tools by presenting classical results on sorting, an important topic in its own right Part V, "Data Structures" , introduces the concept of abstractdata type as a tool to separate functional specification and. .. problems and techniques similar to those of the first half, but does not assume explicit knowledge of precisely those topics treated in the first half Thus a traditional course on data structures for students who have taken one or two introductory courses in computer science might be based on Chapters 16 to 26, with Chapters I to 15 serving as a source of exercises, programming projects, and applications . Cataloging-in-Publication Data NIEVERGELT, JURG. Algorithms and data structures: with applications to graphics and geometry / Jurg Nievergelt, Klaus Hinrichs. p. cm. Includes bibliographical references and. 3 Ii p 1 p I 1, : I$',i,.d]' ]1, I 1 1' ALGORITHMS AND DATA STRUCTURES With Applications to Graphics and Geometry JURG NIEVERGELT ETH Zurich KLAUS H. HINRICHS University. Data structures old and new 199 18.2 The range of data structures studied 201 18.3 Performance criteria and measures 202 19 ABSTRACT DATA TYPES 204 19.1 Concepts: What and