Algorithmique, Graphes Et Programmation Dynamique Notes De Cours Rapport De Travaux Pratiques

Total Page:16

File Type:pdf, Size:1020Kb

Algorithmique, Graphes Et Programmation Dynamique Notes De Cours Rapport De Travaux Pratiques Algorithmique, graphes et programmation dynamique Notes de Cours Rapport de Travaux Pratiques Laurent Canet Le 2 juillet 2003 Table des mati`eres I IN202 - Algorithmique 6 1 Syst`emeformel de preuve de programme de O'Hare 8 1.1 R`egleset axiomes . 8 1.2 Construction de programmes sur invariant . 9 2 Probl`emesde recherches 11 2.1 Remarques sur l'´evaluation de complexit´ed'un programme . 11 2.2 Recherche dichotomique . 11 2.3 Recherche s´equentielle . 13 2.4 Recherche arri`ere . 14 3 Problemes de tris 16 3.1 Slowsort . 16 3.2 Quicksort . 17 3.3 Complexit´eminimum d'un algorithme de tri . 21 4 Approche diviser pour regn´er 23 4.1 Diviser pour r´egner . 23 4.2 Limites de l'approche diviser pour r`egner. 24 5 Complexit´edes algorithmes 25 5.1 Expression du temps de calcul . 25 5.2 Notations . 26 5.3 Calculs courants dans le calcul des complexit´es. 27 5.4 Un peu de th´eoriede la complexit´e . 27 6 Programmation Dynamique 29 6.1 Cas concret : fibonnaci . 29 6.2 Exemple : Multiplication de matrices . 30 6.3 Exemple : Recherche du plus long sous-mot commun `a2 chaines . 33 1 II IN311 - Programmation dynamique 36 7 Applications de la programmation dynamique 38 7.1 Un probl`emed'assortiment . 38 7.2 Compression d'image . 40 7.3 Justification de Parapgraphes . 43 III IN302 - Graphes et algorithmes 45 8 Notions de base 46 8.1 Premi`ered´efinition . 46 8.2 Repr´esentation en m´emoire. 47 8.3 D´efinitionscompl´ementaires . 50 8.4 Chemins, connexit´e. 53 8.5 Repr´esentation matricielle . 59 8.6 Graphe biparti . 60 9 Arbre et arborescences 62 9.1 D´efinitions. 62 9.2 Exemples et applications . 64 9.3 Arbre de poids minimum . 67 9.4 Algorithmes de Kruskal ...................... 69 10 Plus courts chemins 71 10.1 D´efinition . 71 10.2 Probl`ematiquedu plus court chemin . 72 10.3 Algorithme de Floyd ........................ 74 10.4 Algorithme de Bellman ....................... 75 10.5 Algorithme de Dikstra ....................... 78 10.6 Plus courts chemins (Exploration largeur) . 80 10.7 Graphes sans circuits : GSC ..................... 81 11 Cycles eul´erienset hamiltoniens 86 11.1 Cycle eul´erien . 86 11.2 Cycle hamiltonien . 86 12 Flots et r´eseauxde transport 88 12.1 Mod´elisationd'un r´eseaude transport . 88 12.2 Propri´et´esd'un r´eseaude transport . 89 12.3 Algorithme de Ford et Fulkerson . 90 2 13 R´esolutionsde probl`emesen Intelligence artificielle et optimisa- tion 94 13.1 Exemple : le probl`emedes 8 dames . 94 13.2 Strat´egiesde recherche . 95 13.3 Algorithme A et A∗ .......................... 97 IV Annexes 99 A TP1 - Tri par tas 100 A.1 Introduction . 100 A.2 Equations Bool´eennes. 100 A.3 Tas . 102 A.4 Comparaison avec d'autres algorithmes de tri . 106 B TP2 - Arbres de recherche optimaux 110 B.1 Introduction . 110 B.2 Pr´eliminaires . 111 B.3 Recherche dans un arbre . 112 B.4 Arbre binaire de recherche optimal . 114 B.5 Optimisation . 120 C TP3 - Sommes de sous-ensembles 130 C.1 Premier algorithme . 130 C.2 Deuxi`emealgorithme . 131 C.3 Optimisation & parall´elisation. 135 C.4 Code source comment´e . 137 D TP IN311 : le sac `aados 141 D.1 Position du probl`eme. 141 D.2 Equation du r´ecurrence. 141 D.3 Complexit´e . 142 D.4 Reconstruction de la solution . 142 D.5 Exemples d'execution . 142 D.6 Programme complet et comment´e . 144 E Corrections de TD IN302 147 E.1 TD 1 : Recherche de circuits . 147 E.2 Le probl`emede l’affectation . 149 E.3 Le probl`emede la carte routi`ere. 150 E.4 Le probl`emedu voyageur de commerce . 150 F Graphes TP 2 - Analyse de trajectoires dans une chambre `a bulle 151 3 G Graphes TP 3 - Ballades dans le m´etro 158 4 Introduction Ce texte n'a aucune valeur officielle de support pour les cours concern´es.Il contient vraisemblablement beaucoup d'erreurs et d'inexactitudes, le lecteur se reportera `aun ouvrage de r´eferencepour les v´erifications. Le rapport de TP de programmation dynamique a ´et´eco-´ecritavec mon bin^ome Thibaut Varene. Les sujets et les rapports de travaux pratiques de graphes ont ´et´e´ecritpar Gilles Bertrand et Michel Couprie. 5 Premi`erepartie IN202 - Algorithmique 6 Introduction au cours d'algorithmique Ce document fut `al'origine mes notes du cours d'IN202 de l'ESIEE Paris. Cette unit´ea pour but d'initier les ´el`eves ing´enieurs`al'algorithmique gr^ace`ades exemples d´etaill´ees.Le cours ´etaitdivis´een 2 grandes parties, d'abord un apercu de l'algorithmique ainsi que de la notion de complexit´een s'appuyant sur des exemples tels que le quicksort, puis une description de l'approche dite de pro- grammation dynamique. Des notions de programmation en C ainsi des connais- sances basiques en analyses sont requises pour comprendre ce cours. D'autres unit´esdu tronc commun ou de la majeure informatique de l'ESIEE Paris ´elar- gissent et approfondissent le sujet. Mes notes de cours datent de mi-2002. Durant l'´et´ede cette ann´eej'ai enrichi le cours notament de la notion de classe de probl`emeainsi que des d´emonstra- tions, notament la complexit´eminimale d'un algorithme de tri ; finalement j'ai d´ecid´ede les publier sur ma page web. Ces notes de cours ne constituent en rien un support officiel, il faut imp´e- rativement se r´ef`erer`aun ou des ouvrages sp´ecialis´esqui seront cit´esdans une bibliographie (qui n'a pas encore ´et´efaite). Je n'ai en aucun cas la pr´etention de faire un document interessant, exact ou pr´ecis.N´eanmoinsSi vous estimez que je racontre vraiment n'importe quoi, signalez le moi sur mon email : [email protected]. 7 Chapitre 1 Syst`emeformel de preuve de programme de O'Hare On peut formaliser un programme en une forme logique dite de bo^ıtenoire : E P S Logique du 1er ordre Programme Logique du 1er ordre |{z} |{z} |{z} De l`a,plusieurs r`eglesagissent sur ces ´enonc´es: 1.1 R`egleset axiomes R´eglede la post-condition : EP S0 ; S0 S EP S ) 7−! R´eglede la pr´e-condition: E0PS ; E0 E EP S ) 7−! R´egledu ou : EP S ; E0PS (E E0)PS 7−! _ R´egledu et : EP S ; EP S0 EP (S S0) 7−! ^ 8 R`egledu si-sinon : (E B)PS ;(E B)QS E Si B alors P sinon Q S ^ ^ 7−! f g Remarque : L'´evaluation de B ne doit pas modifier E. R`egledu tant-que : (E B)PE ; E tant que B faire P (E B) ^ 7−! f g ^ Axiomes d’affectation : EP A ; AQS E P ; Q S 7−! f g Exemple : D´emontrer : EP S ; E0PS0 (E E0)P (S S0) 7−! ^ _ 1. EP S 2. E0PS0 3. E E0 E ^ ) 4. (E E0)PS (Pr´econditionsur 1 et 3) ^ 5. (E E0)PS0 (Pr´econditionsur 2 et 3) ^ 6. (E E0)P (S S0) (R`egledu ET sur 4 et 5) ^ ^ 7. (S S0) (S S0) ^ ) _ 8. (E E0)P (S S0) (Postcondition sur 7 et 8) ^ _ 1.2 Construction de programmes sur invariant Construire un programme d´ecritpar l'invariant, c'est utiliser une proposition bool´eenne I(x1; x2; ; xn), fonction de plusieurs variables xi, qui d´ecritle pro- bl`eme`aun instant donn´e.Lorsque··· l'on veut faire un algorithme qui r´esoud EP S bas´esur un invariant, on doit donner plusieurs propositions : Invariant : D´ecritla position du probl`emeen fonction de variables xi. Initialisation : Ce sont des conditions sur les variables xi, tels que la proposition E soit v´erifi´eet I(xi) le soit ´egalement. 9 Condition d'arr^et: Ce sont des conditions sur les variables xi, tels que la pro- position S soit v´erifi´ee.Lorsque la condition d'arr^etest v´erifi´ee,le pro- gramme doit avoir fini son traitement, et S est verifi´ee. Implications : Elles sont de la forme I(x1; x2; ; xn) (Pi vraies) I(x0 ; x0 ; ; x0 ). ··· Vi ) 1 2 ··· 3 O`u Pi r´epresentent des propositions On en d´eduitl'expression : I(xi) T ANT QUE (Condition d'arr^et)F AIRE Pi I(xi) (Condition d'arr^et) ^ 1.2.1 Exemple : somme des ´el´ements d'un tableau Soit un programme r´ealisant la somme de tous les ´el´ements de 0 `a n 1 d'un tableau T . On veut un programme P v´erifiant l'´enonc´esuivant : − n 1 − (n > 0) P S = T [i]! X i=0 On se propose de d´ecrirece programme par l'invariant : k 1 Invariant : I(s; k) s = i=0− T [i]. s est la somme des k premiers ´el´ements de T . ≡ P Initialisation : s = 0 et k = 0. En effet la somme des 0 premiers ´el´ements fait 0. Donc I(0; 0) est v´erifi´e. Condition d'arr^et: k = n. On s'arr^etelorsque l'on a r´ealis´eela somme des n premiers ´el´ements, comme demand´e. Implication : I(s; k) (s = s + T [k]) I(s; k + 1). ^ ) En sortie du programme, on aura I(s; k) (k = n). La translation de cet algo- rithme en pseudo langage est tr`essimple :^ s <- 0 k <- 0 // Ici on a I(0,0) tant que (k != n) faire : s <- s + T[k] // Ici on a I(s,k+1) k <- k + 1 // Ici on a I(s,k) fin tant que // Ici on a I(s,n) 10 Chapitre 2 Probl`emesde recherches Le probl`emeque nous nous posons est de rechercher un ´el´ement (nombre, caract`ereou autre) dans un tableau homog`ened'´elements.
Recommended publications
  • Algorithms, Searching and Sorting
    Python for Data Scientists L8: Algorithms, searching and sorting 1 Iterative and recursion algorithms 2 Iterative algorithms • looping constructs (while and for loops) lead to iterative algorithms • can capture computation in a set of state variables that update on each iteration through loop 3 Iterative algorithms • “multiply x * y” is equivalent to “add x to itself y times” • capture state by • result 0 • an iteration number starts at y y y-1 and stop when y = 0 • a current value of computation (result) result result + x 4 Iterative algorithms def multiplication_iter(x, y): result = 0 while y > 0: result += x y -= 1 return result 5 Recursion The process of repeating items in a self-similar way 6 Recursion • recursive step : think how to reduce problem to a simpler/smaller version of same problem • base case : • keep reducing problem until reach a simple case that can be solved directly • when y = 1, x*y = x 7 Recursion : example x * y = x + x + x + … + x y items = x + x + x + … + x y - 1 items = x + x * (y-1) Recursion reduction def multiplication_rec(x, y): if y == 1: return x else: return x + multiplication_rec(x, y-1) 8 Recursion : example 9 Recursion : example 10 Recursion : example 11 Recursion • each recursive call to a function creates its own scope/environment • flow of control passes back to previous scope once function call returns value 12 Recursion vs Iterative algorithms • recursion may be simpler, more intuitive • recursion may be efficient for programmer but not for computers def fib_iter(n): if n == 0: return 0 O(n) elif n == 1: return 1 def fib_recur(n): else: if n == 0: O(2^n) a = 0 return 0 b = 1 elif n == 1: for i in range(n-1): return 1 tmp = a else: a = b return fib_recur(n-1) + fib_recur(n-2) b = tmp + b return b 13 Recursion : Proof by induction How do we know that our recursive code will work ? → Mathematical Induction To prove a statement indexed on integers is true for all values of n: • Prove it is true when n is smallest value (e.g.
    [Show full text]
  • Decomposition, Abstraction, Functions
    SEARCHING AND SORTING ALGORITHMS 6.00.01X LECTURE 1 SEARCH ALGORITHMS . search algorithm – method for finding an item or group of items with specific properties within a collection of items . collection could be implicit ◦ example – find square root as a search problem ◦ exhaustive enumeration ◦ bisection search ◦ Newton-Raphson . collection could be explicit ◦ example – is a student record in a stored collection of data? SEARCHING ALGORITHMS . linear search • brute force search • list does not have to be sorted . bisection search • list MUST be sorted to give correct answer • will see two different implementations of the algorithm 6.00.01X LECTURE 3 LINEAR SEARCH ON UNSORTED LIST def linear_search(L, e): found = False for i in range(len(L)): if e == L[i]: found = True return found . must look through all elements to decide it’s not there . O(len(L)) for the loop * O(1) to test if e == L[i] . overall complexity is O(n) – where n is len(L) 6.00.01X LECTURE 4 CONSTANT TIME LIST ACCESS . if list is all ints … ◦ ith element at ◦ base + 4*I .if list is heterogeneous ◦ indirection ◦ references to other objects … 6.00.01X LECTURE 5 6.00.01X LECTURE 6 LINEAR SEARCH ON SORTED LIST def search(L, e): for i in range(len(L)): if L[i] == e: return True if L[i] > e: return False return False . must only look until reach a number greater than e . O(len(L)) for the loop * O(1) to test if e == L[i] . overall complexity is O(n) – where n is len(L) 6.00.01X LECTURE 7 USE BISECTION SEARCH 1.
    [Show full text]
  • 3 Algorithms and Data Structures
    3 Algorithms and Data Structures Program ®le for this chapter: algs What's wrong with this procedure? to process.sentence :sent output fput (process.word first :sent) (process.sentence bf :sent) end If you said ªIt's a recursive procedure without a stop rule,º you've solved a simple problem in analysis of algorithms. This branch of computer science is concerned with the correctness and the ef®ciency of programs. The ®eld is called analysis of algorithms rather than analysis of programs because the emphasis is on the meaning of a program (how you might express the program in English) and not on the details of its expression in a particular language. For example, the error in the procedure to process.sentence :sent if emptyp :sent [output []] output fput (process.word first :sent) (process.snetence bf :sent) end is just as fatal as the error in the ®rst example, but there isn't much of theoretical interest to say about a misspelled procedure name. On the other hand, another branch of computer science, program veri®cation, is concerned with developing techniques to ensure that a computer program really does correctly express the algorithm it is meant to express. The examples I've given so far are atypical, it's worth noting, in that you were able to see the errors in the procedures without having any real idea of what they do! Without seeing process.word you can tell that this program is doing something or other to each word of a sentence, but you don't know whether the words are being translated 107 to another language, converted from upper case to lower case letters, translated into a string of phoneme codes to be sent to a speech synthesizer, or what.
    [Show full text]
  • 4Th, the Friendly Forth Compiler
    4tH, the friendly Forth compiler J.L. Bezemer July 14, 2021 Contents 1 What’s new 19 I Getting Started 24 2 Overview 25 2.1 Introduction . 25 2.2 History . 25 2.3 Applications . 26 2.4 Architecture . 26 2.4.1 The 4tH language . 28 2.4.2 H-code . 28 2.4.3 H-code compiler . 29 2.4.4 Error handling . 30 2.4.5 Interfacing with C . 30 3 Installation Guide 32 3.1 About this package . 32 3.1.1 Example code . 32 3.1.2 Main program . 32 3.1.3 Unix package . 33 3.1.4 Linux package . 34 3.1.5 Android package . 38 3.1.6 MS-DOS package . 40 3.1.7 MS-Windows package . 41 3.2 Setting up your working directory . 43 3.3 Now what? . 43 3.4 Pedigree . 43 3.5 Contributors . 44 1 CONTENTS 2 3.6 Questions . 44 3.6.1 4tH website . 44 3.6.2 4tH Google group . 45 3.6.3 Newsgroup . 45 4 A guided tour 46 4.1 4tH interactive . 46 4.2 Starting up 4tH . 46 4.3 Running a program . 47 4.4 Starting an editing session . 47 4.5 Writing your first 4tH program . 48 4.6 A more complex program . 52 4.7 Advanced features . 56 4.8 Suspending a program . 63 4.9 Calculator mode . 65 4.10 Epilogue . 65 5 Frequently asked questions 66 II Primer 69 6 Introduction 70 7 4tH fundamentals 71 7.1 Making calculations without parentheses . 71 7.2 Manipulating the stack .
    [Show full text]
  • Advanced Programming Language Design (Raphael A. Finkel)
    See discussions, stats, and author profiles for this publication at: https://www.researchgate.net/publication/220692467 Advanced programming language design. Book · January 1996 Source: DBLP CITATIONS READS 41 3,040 1 author: Raphael Finkel University of Kentucky 122 PUBLICATIONS 6,757 CITATIONS SEE PROFILE All content following this page was uploaded by Raphael Finkel on 16 December 2013. The user has requested enhancement of the downloaded file. ADVANCED PROGRAMMING LANGUAGE DESIGN Raphael A. Finkel ❖ UNIVERSITY OF KENTUCKY ▲ ▼▼ Addison-Wesley Publishing Company Menlo Park, California · Reading, Massachusetts New York · Don Mills, Ontario · Harlow, U.K. · Amsterdam Bonn · Paris · Milan · Madrid · Sydney · Singapore · Tokyo Seoul · Taipei · Mexico City · San Juan, Puerto Rico hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh On-line edition copyright 1996 by Addison-Wesley Publishing Company. Permission is granted to print or photo- copy this document for a fee of $0.02 per page, per copy, payable to Addison-Wesley Publishing Company. All other rights reserved. Acquisitions Editor: J. Carter Shanklin Proofreader: Holly McLean-Aldis Editorial Assistant: Christine Kulke Text Designer: Peter Vacek, Eigentype Senior Production Editor: Teri Holden Film Preparation: Lazer Touch, Inc. Copy Editor: Nick Murray Cover Designer: Yvo Riezebos Manufacturing Coordinator: Janet Weaver Printer: The Maple-Vail Book Manufacturing Group Composition and Film Coordinator: Vivian McDougal Copyright 1996 by Addison-Wesley Publishing Company All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or any other media embodiments now known, or hereafter to become known, without the prior written permission of the publisher.
    [Show full text]
  • Team-Fly® Page I
    Y L F M A E T Team-Fly® Page i C The Complete Reference Fourth Edition Page ii ABOUT THE AUTHOR Herbert Schildt is the world's leading programming author. He is an authority on the C and C++ languages, a master Windows programmer, and an expert on Java. His programming books have sold more that 2.5 million copies worldwide and have been translated into all major foreign languages. He is the author of numerous bestsellers, including C++: The Complete Reference, Teach Yourself C, Teach Yourself C++, C++ from the Ground Up, Windows 2000 Programming from the Ground Up, and Java: The Complete Reference. Schildt holds a master's degree in computer science from the University of Illinois. He can be reached at his consulting office at (217) 586-4683. Page iii C The Complete Reference Fourth Edition Herbert Schildt Page iv Copyright © 2000 by The McGraw-Hill Companies. All rights reserved. Manufactured in the United States of America. Except as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher. 0-07-213295-7 The material in this eBook also appears in the print version of this title: 0-07-212124-6. All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark.
    [Show full text]
  • Lecture 5 Randomized Algorithms and Quicksort Announcements • HW2 Is Due Today (11:59Pm PT Wednesday)
    Lecture 5 Randomized algorithms and QuickSort Announcements • HW2 is due today (11:59pm PT Wednesday). • Add/drop deadline is Friday. • Exam 1 starts soon • Topics: Lectures 1-4 (not today’s lecture though) • Time: Contiguous 2-hour window during Thursday (starting 12:01am PT) and Friday (ending 11:59pm PT) • Instructions: Website -> Exams Honor Code • Collaboration on homework is OK, but each person should individually write their solution from scratch. Sharing written/typed solutions is NOT OK. • NO collaboration on exams! • You may consult inanimate sources, but not directly seek answers to questions on the exam. • Read the BJA guidance on honor code: “ In all cases, it is not permissible for students to enter exam questions into any software, apps, or websites. Accessing resources that directly explain how to answer questions from the actual assignment or exam is a violation of the Honor Code. ” • Website -> Policies -> Honor Code Last time • We saw a divide-and-conquer algorithm to solve the Select problem in time O(n) in the worst-case. • It all came down to picKing the pivot… We choose a pivot cleverly We choose a pivot randomly. Randomized algorithms • We maKe some random choices during the algorithm. • We hope the algorithm worKs. For today we will look at algorithms that always work • We hope the algorithm is fast. and are probably fast. E.g., Select with a random pivot is a randomized algorithm. • Always works (aka, is correct). • Probably fast. Today • How do we analyze randomized algorithms? • A few randomized algorithms for sorting. • BogoSort • QuickSort • BogoSort is a pedagogical tool.
    [Show full text]
  • MIT6 0001F16 Searching and Sorting Algorithms
    SEARCHING AND SORTING ALGORITHMS (download slides and .py files and follow along!) 6.0001 LECTURE 12 6.0001 LECTURE 12 1 SEARCH ALGORITHMS § search algorithm – method for finding an item or group of items with specific properes within a collecon of items § collecon could be implicit ◦ example – find square root as a search problem ◦ exhausve enumeraon ◦ bisecon search ◦ Newton-Raphson § collecon could be explicit ◦ example – is a student record in a stored collecon of data? 6.0001 LECTURE 12 2 SEARCHING ALGORITHMS § linear search • brute force search (aka Brish Museum algorithm) • list does not have to be sorted § bisecon search • list MUST be sorted to give correct answer • saw two different implementaons of the algorithm 6.0001 LECTURE 12 3 LINEAR SEARCH ON UNSORTED LIST: RECAP def linear_search(L, e): found = False for i in range(len(L)): if e == L[i]: found = True return found § must look through all elements to decide it’s not there § O(len(L)) for the loop * O(1) to test if e == L[i] § overall complexity is O(n) – where n is len(L) 6.0001 LECTURE 12 4 LINEAR SEARCH ON SORTED LIST: RECAP def search(L, e): for i in range(len(L)): if L[i] == e: return True if L[i] > e: return False return False § must only look unl reach a number greater than e § O( len(L)) for the loop * O(1) to test if e == L[i] § overall complexity is O(n) – where n is len(L) 6.0001 LECTURE 12 5 USE BISECTION SEARCH: RECAP 1.
    [Show full text]
  • Java Algorithms
    Java Algorithms Scott Robert Ladd McGraw-Hill New York San Francisco Washington, D.С Auckland Bogota Caracas Lisbon London Madrid Mexico City Milan Montreal New Delhi San Juan Singapore Sydney Tokyo Toronto Table of Contents V TABLE OF CONTENTS Introduction Purpose and Objective 1 Chapter 1 Sorting Things Out 3 A Framework for Sorting 3 Elementary Sorts 8 Selection Sort 8 Insertion Sort 10 Shell Sort 12 Quicksort 14 The Basic Algorithm 14 Avoiding SlowSort 20 Removing Recursion 25 The Code 27 Other Considerations 33 Array Conversions 33 Onward 36 Chapter 2 Mathematical Components 37 Java Does Math 37 Conversions 38 Floating Point Bestiary 39 The Math Class 40 Polynomials 41 A Java Implementation 42 Complex Numbers 48 Complex Polynomials 53 The Fast Fourier Transform 58 Onward 68 Chapter 3 Java Strings and Text 69 Brute Force 69 Boyer-Moore String Searching 72 Classes for Searching 74 A Generic Base 75 The BoyerMoore Class 78 Tools for Searching Text Areas 82 Onward 88 Chapter 4 Layout Managers 89 Types of Layout Managers 89 Building a Vertical Flow Layout 92 Building a Clock Layout 103 Onward 112 VI Table of Contents Chapter 5 High-Performance Containers 113 Garbage Collection Issues 114 Exceptions 115 Stacks 115 Queues 119 Deques 122 Heaps & Priority Queues 127 An Example 134 Onward 140 Chapter 6 Tree Structures 141 Binary Trees 141 Searching 142 Insertion 144 Deletion 145 Exceptions 147 Implementing a Binary Tree 148 Iterators 150 Constructors and Properties 154 Utility Methods 155 Adding and Removing Objects 158 Limitations 161 Red
    [Show full text]
  • Algorithms and Complexity Internet Edition, Summer, 1994
    Algorithms and Complexity Herbert S. Wilf University of Pennsylvania Philadelphia, PA 19104-6395 Copyright Notice Copyright 1994 by Herbert S. Wilf. This material may be reproduced for any educational purpose, multiple copies may be made for classes, etc. Charges, if any, for reproduced copies must be just enough to recover reasonable costs of reproduction. Reproduction for commercial purposes is prohibited. This cover page must be included in all distributed copies. Internet Edition, Summer, 1994 This edition of Algorithms and Complexity is available at the web site <http://www/cis.upenn.edu/ wilf>. It may be taken at no charge by all interested persons. Comments and corrections are welcome, and should be sent to [email protected] A Second Edition of this book was published in 2003 and can be purchased now. The Second Edition contains solutions to most of the exercises. CONTENTS Chapter 0: What This Book Is About 0.1Background......................................1 0.2 Hard vs. easy problems . 2 0.3Apreview.......................................4 Chapter 1: Mathematical Preliminaries 1.1 Orders of magnitude . 5 1.2 Positional number systems . 11 1.3 Manipulations with series . 14 1.4 Recurrence relations . 16 1.5Counting...................................... 21 1.6Graphs....................................... 24 Chapter 2: Recursive Algorithms 2.1Introduction..................................... 30 2.2Quicksort...................................... 31 2.3 Recursive graph algorithms . 38 2.4 Fast matrix multiplication . 47 2.5 The discrete Fourier transform . 50 2.6 Applications of the FFT . 56 2.7Areview....................................... 60 Chapter 3: The Network Flow Problem 3.1Introduction..................................... 63 3.2 Algorithms for the network flow problem . 64 3.3 The algorithm of Ford and Fulkerson .
    [Show full text]
  • Slow Sort Algorithm Comprises Three Nested Loops
    Slow Sorting: A Whimsical Inquiry Bryant A . Julstrom Department of Computer Science St . Cloud State Universit y St. Cloud, Minnesota 5630 1 julstrom@eeyore .stcloud.msus.edu Abstract We divide sorting algorithms into two categories dependin g Sorting is one of the most common and important computin g on the situations in which they apply . Internal sorting orders operations . In analyzing and comparing sorting algorithms, w e elements held in the computer's memory, as in an array or a consider their execution times, as indicated by the number o f linked list . External sorting orders elements held on a mass operations they execute as they sort is elements . The simples t storage device such as a disk drive. Here we consider the internal algorithms have times that grow approximately as n 2, while sorting of elements held in an array. For simplicity the element s more complex algorithms offer times that grow approximately a s will be single values, say integers ; that is, each element is it s nlogn. This paper pursues a contrary goal : a sorting algorith m own key. whose time grows more quickly than that of the well-known al- We generally place internal sorting algorithms in two cate- gorithms . The paper exhibits and analyzes such an algorithm . gories based on the times they take to sort n elements. The sim- plest algorithms—Insertion Sort, Selection Sort, Bubble Sort — L Introduction have two nested loops. The outer loop executes n — 1 time s while the inner loop carries out an operation that leaves on e Sorting is one of the most common and important computin g more element in its proper sorted position relative to the ele- operations .
    [Show full text]
  • Lecture 5 Randomized Algorithms and Quicksort Announcements • HW1 Is Graded! Thanks Tas for Super-Fast Turnaround!! • HW2 Is Posted! Due Friday
    Lecture 5 Randomized algorithms and QuickSort Announcements • HW1 is graded! Thanks TAs for super-fast turnaround!! • HW2 is posted! Due Friday. • Please send any OAE letters to Jessica Su ([email protected]) by Friday. • Garrick attempts to make my cultural references more up-to-date: Thanks Garrick! Last time • We saw a divide-and-conquer algorithm to solve the Select problem in time O(n) in the worst-case. • It all came down to picking the pivot… We choose a pivot randomly and then a bad guy gets to decide what the array was. We choose a pivot cleverly and then a bad guy gets to decide what the array was. The bad guy gets to decide what the array was and then we choose a pivot randomly. Randomized algorithms • We make some random choices during the algorithm. • We hope the algorithm works. • We hope the algorithm is fast. e.g., Select with a random pivot is a randomized algorithm. It was actually always correct Looks like it’s probably fast but not always. Today • How do we analyze randomized algorithms? • A few randomized algorithms for sorting. • BogoSort • QuickSort • BogoSort is a pedagogical tool. • QuickSort is important to know. (in contrast with BogoSort…) How do we measure the runtime of a randomized algorithm? Scenario 1 Scenario 2 1. Bad guy picks the input. 1. Bad guy picks the input. 2. You run your randomized 2. Bad guy chooses the algorithm. randomness (fixes the dice) • In Scenario 1, the running time is a random variable. • It makes sense to talk about expected running time.
    [Show full text]