Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 6: Generative.

Slides:



Advertisements
Similar presentations
Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 14: Stepwise.
Advertisements

Higher-Order Functions and Loops c. Kathi Fisler,
Introduction to Algorithms Quicksort
Recursion Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein.
Types of Algorithms.
Anany Levitin ACM SIGCSE 1999SIG. Outline Introduction Four General Design Techniques A Test of Generality Further Refinements Conclusion.
Recursion Michael Ernst UW CSE 140 To seal: moisten flap, fold over, and seal.
New Mexico Computer Science For All
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Divide and Conquer.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CMPS1371 Introduction to Computing for Engineers SORTING.
Chapter 19: Searching and Sorting Algorithms
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 280 Data Structures Professor John Peterson. Project Not a work day but I’ll answer questions as long as they keep coming! I’ll try to leave the last.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
CS 280 Data Structures Professor John Peterson. Project Questions?
CS 280 Data Structures Professor John Peterson. Project Questions? /CIS280/f07/project5http://wiki.western.edu/mcis/index.php.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
Induction and recursion
Towers of Hanoi. Introduction This problem is discussed in many maths texts, And in computer science an AI as an illustration of recursion and problem.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Recursion, Complexity, and Sorting By Andrew Zeng.
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
1 Lecture 16: Lists and vectors Binary search, Sorting.
Fundamentals of Algorithms MCS - 2 Lecture # 7
1 CS 177 Week 16 Recitation Recursion. 2 Objective To understand and be able to program recursively by breaking down a problem into sub problems and joining.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Data Structures Using C++ 2E Chapter 10 Sorting Algorithms.
CSC 211 Data Structures Lecture 13
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 8: Accumulating.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
UNIT-I INTRODUCTION ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 1:
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
Types of Algorithms. 2 Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We’ll talk about a classification.
Introducing General Recursion CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
A Recipe for Algorithm Design Or, What to do when the data type doesn't match the function…yet.
Chapter 9 Recursion © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Recursion Topic 5.
CS 1321.
Data Structures Using C++ 2E
Types of Algorithms.
Types of Algorithms.
Types of Algorithms.
Recursive Algorithms 1 Building a Ruler: drawRuler()
Presentation transcript:

Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 6: Generative Recursion Prof. Dr. Max Mühlhäuser Dr. Guido Rößling

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Outline Introduction to generative recursion Sorting with generative recursion Guidelines for the design of generative recursive procedures Structural versus generative recursion Backtracking: Traversing graphs 2

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Generative Recursion So far, we have used structural recursion to process structurally recursive data –We have decomposed the data to their immediate structural components –We have processed these components and combined the results However: 1.Not all problems can be solved using structurally recursive functions 2.Even if there is a structurally recursive solution for a problem, it might not be optimal Now we will consider a new programming style: Generative recursion 3

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Generative Recursion Divide and Conquer is an important principle underlying generative recursion –If the problem is trivial to solve, the solution for the trivial case is returned –Otherwise: Divide the problem in new smaller problems (generate new sub-problems) Conquer the sub-problems by applying the same technique recursively Combine the solutions for the sub-problems into a solution for the original problem The design of generative recursive programs is more an ad-hoc activity as compared to the design of the structural recursive programs that needs an insight – a “Eureka!" 4

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Modeling a rolling ball on a table Task Description: –The ball rolls at a constant speed until it drops off the edge of the table –We can model the table as a canvas/surface with a pre-defined length and width –The ball can be represented as a disc that moves across the canvas –The disc movement can be represented by repeating the following steps: Draw the disc at the current position on the canvas Wait for a certain pre-defined time Erase the disc at the current position Move it to a new position 5

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Ball Structure and Operations 6 ;;TeachPack: draw.ss ;; structure: (make-ball number number number number) (define-struct ball (x y delta-x delta-y)) ;; draw-and-clear : a-ball -> true (define (draw-and-clear a-ball) (and (draw-solid-disk (make-posn (ball-x a-ball) (ball-y a-ball)) 5 'red) (sleep-for-a-while DELAY) (clear-solid-disk (make-posn (ball-x a-ball) (ball-y a-ball)) 5 'red)))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Ball Structure and Operations 7 ;; move-ball : ball -> ball (define (move-ball a-ball) (make-ball (+ (ball-x a-ball) (ball-delta-x a-ball)) (+ (ball-y a-ball) (ball-delta-y a-ball)) (ball-delta-x a-ball) (ball-delta-y a-ball))) ;; Dimension of surface (define WIDTH 100) (define HEIGHT 100) ;; Delay constant (define DELAY.1)

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Ball Structure and Operations 8 To move the ball multiple times we can write: This gets tedious after a while. We need a function that moves the ball until it is out of bounds. (define the-ball (make-ball )) (and (draw-and-clear the-ball) (and (draw-and-clear (move-ball the-ball))...))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Rolling the Ball Determine whether a-ball is outside of the bounds: Template for moving the ball until it is out of bounds: ;; out-of-bounds? : a-ball -> boolean (define (out-of-bounds? a-ball) (not (and (<= 0 (ball-x a-ball) WIDTH) (<= 0 (ball-y a-ball) HEIGHT)))) ;; move-until-out : a-ball -> true (define (move-until-out a-ball) (cond [(out-of-bounds? a-ball)... ] [else...])) The trivial case: return true ? true

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Rolling the Ball 10 After drawing and moving the ball, we apply move-until-out again, which means the function is recursive: We can now test the function as follows: The code creates a canvas of proper size and a ball that moves to the bottom left of the canvas. (start WIDTH HEIGHT) (move-until-out (make-ball )) (stop) (define (move-until-out a-ball) (cond [(out-of-bounds? a-ball) true] [else (and (draw-and-clear a-ball) (move-until-out (move-ball a-ball)))]))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 A New Type of Recursion 11 The procedure move-until-out uses a new type of recursion: –Conditions have nothing to do with the input data –Recursive application in the body does not use part of the input move-until-out generates an entirely new and different ball structure and uses it for the recursion We do not have a design recipe for this (define (move-until-out a-ball) (cond [(out-of-bounds? a-ball) true] [else (and (draw-and-clear a-ball) (move-until-out (move-ball a-ball)))]))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Outline Introduction to generative recursion Sorting with generative recursion Guidelines for the design of generative recursive procedures Structural versus generative recursion Backtracking: Traversing graphs 12

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Sorting: Quicksort and Mergesort We are once more concerned with sorting the elements of a list … –We have seen insertion sort : A structurally recursive procedure –Now we will see two other algorithms for sorting: Quicksort and Mergesort Classic examples of generative recursion Based on the idea of “divide and conquer” 13

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 [Reminder: insertion sort ] 14 ;; insertion-sort: list-of-numbers -> list-of-numbers ;; creates a sorted list of numb. from numbers in alon (define (insertion-sort alon) (cond [(empty? alon) empty] [else (insert (first alon) (insertion-sort (rest alon)))])) an sortedunsorted

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Quicksort : The Idea An arbitrary intermediate step: sorting an arbitrary sub- list: L 0 =(list el p …el r ) –Divide: Partition L 0 in two (possibly empty) lists, L 1 = (list el p …el q-1 ) and L 2 = (list el q+1 …el r ), such that each element of L 1 is smaller than el q, and the latter is in turn smaller than each element in L 2 –Conquer: Apply the same procedure recursively to sort L 1 and L 2 –Combine: simply concatenate the sorted lists L 1 and L 2 15 <= el q > el q el q Turning point

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Quicksort : The Idea Two open questions so far: –How do we select the pivot element? We always use the first element –When do we stop? That is: what is the trivial case of Quicksort ? The empty list is always sorted! 16

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Quicksort : Approach 1.Select the first element from the list as pivot item 2.Determine the first sub-list with elements <= pivot 3.Sort this sub-list recursively using Quicksort 4.Determine the second sub-list with elements > pivot 5.Sort this sub-list recursively using Quicksort 6.Concatenate the sorted sub-lists to a new list 17

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Work 18 Sort (list ) : 1.Select the first element from '( ) as pivot item:11 2.Determine the first sub-list <= 11: '(8 7) 3.Sort this sub-list 1.Select the first element from '(8 7) as pivot item: 8 2.Determine the first sub-list with elements <= 8: '(7) 3.Sort this sub-list 1.Select the first element from '(7) as pivot item: 7 2.Determine the first sub-list with elements <= 7: empty 3.Sort this sub-list  Result empty 4.Determine the second sub-list with elements > 7: empty 5.Sort this sub-list  Result empty 6.Concatenate (empty 7 empty) to a new list -> (list 7) 4.Determine the second sub-list with elements > 8:  empty 5.Sort this sub-list  Result empty 6.Concatenate((list 7) 8 empty) to a new list  (list 7 8) 4.Determine …

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Quicksort at Work 19

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Quicksort schematically 20 List to be sorted Sorting process for the partition with elements smaller than the pivot element sorting process for the partition with elements larger than the pivot element Sorted list pivot item

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Quicksort Algorithm 21 quick-sort distinguishes two cases: –If the input is empty, it returns empty. –Otherwise, it performs recursion. Each sub-list is sorted separately using quick-sort The sorted versions of the two lists are then combined using append ;; quicksort2: (listof number) -> (listof number) (define (quicksort2 alon) (cond [(empty? alon) empty] [else (append (quicksort2 (less-or-equal (rest alon) (first alon))) (list (first alon)) (quicksort2 (greater-than (rest alon) (first alon)))) ]))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Auxiliary Functions of Quicksort greater-than filters out the items that are larger than threshold : less-than filters out the items that are smaller than threshold : 22 (define (greater-than alon threshold) (filter1 > alon threshold))) (define (less-than alon threshold) (filter1 < alon threshold)))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Quicksort Evaluation Example 23 (quick-sort (list )) = (append (quick-sort (list 8 7)) (list 11) (quick-sort (list 14))) = (append (append (quick-sort (list 7)) (list 8) (quick-sort empty)) (list 11) (quick-sort (list 14))) = (append (append (append (quick-sort empty) (list 7) (quick-sort empty)) (list 8) (quick-sort empty)) (list 11) (quick-sort (list 14))) =...

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Quicksort Evaluation Example 24 = (append (append (append empty (list 7) empty) (list 8) empty) (list 11) (quick-sort (list 14))) = (append (append (list 7) (list 8) empty) (list 11) (quick-sort (list 14))) = (append (list 7 8) (list 11) (quick-sort (list 14))) =...

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 mergesort: The Idea 25 Idea: 1.split the list in the middle 2.apply the function to the sub-lists recursively 3.merge the two sorted lists into a new sorted list

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Merging two ordered lists Given two ordered lists ls1 and ls2 How do we merge them into a new ordered list? ls-1ls sorted-list Compare and copy the smaller element, then continue

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Merging two ordered lists 27 (define (merge ls1 ls2) (cond [(empty? ls1) ls2] [(empty? ls2) ls1] [(<= (first ls1) (first ls2)) (cons (first ls1) (merge (rest ls1) ls2)) ] [else (cons (first ls2) (merge ls1 (rest ls2)))] ) )

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 mergesort : algorithm in Scheme 28 (define (merge-sort alon) (local ((define (merge-step left right) (cond [(>= left right) alon] [else (local ( (define mid (floor (/ (+ left right) 2))) (define left-list (merge-sort (extract alon left mid))) (define right-list (merge-sort (extract alon (+ mid 1) right)))) (merge left-list right-list) ) ] ))) (merge-step 1 (length alon))))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 mergesort : algorithm in Scheme 29 (define (extract alon left right) (cond [(empty? alon) empty] [(> left right) empty] [(> left 1) (extract (rest alon) (- left 1) (- right 1))] [else (cons (first alon) (extract alon (+ left 1) right))]))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Outline Introduction to generative recursion Sorting with generative recursion Guidelines for the design of generative recursive procedures Structural versus generative recursion Backtracking: Traversing graphs 30

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Guidelines for Designing Generative Recursive Procedures Understand the nature of the data of the procedure Describe the process in terms of data, creating a new structure or partitioning a list of numbers Distinguish between those input data –which can be processed trivially, –and those which cannot The generation of problems is the key to algorithm design The solutions of the generated problems must be combined 31

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 A Return to the Six Stages of Structural Design 1.Data analysis and design: –Analyze and define data collections representing the problem 2.Contract, purpose, header -Specify what the function does -Explain in general terms how it works 3.Function examples: –Illustrate how the algorithm proceeds for some given input 4.Template: –Follow a general template 5.Definition: –Answer the questions posed by the template 6.Test -Test the completed function -Eliminate the bugs 32

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 General Template for Generative Recursive Functions 33 (define (generative-recursive-fun problem) (cond [(trivially-solvable? problem) (determine-solution problem)] [else (combine-solutions... problem... (generative-recursive-fun (generate-problem-1 problem))... (generative-recursive-fun (generate-problem-n problem)))]))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Procedure Definition 1.What is a trivially solvable problem, and the pertaining solution? 2.How do we generate new problems that are easier to solve than the initial problem? Is there one new problem to generate, or are there more? 3.Is the solution for the given problem the same as for (one of) the new problems? Or do we have to combine solutions to create a solution for the initial problem? If so, do we require parts of the data constituting the initial problem? 34

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Termination of Structurally Recursive Procedures So far, each function has produced an output for a valid input  Evaluation of structurally recursive procedures has always terminated. Important characteristic of the recipe for structurally recursive procedures: –Each step of natural recursion uses an immediate component of the input, not the input itself Because data is constructed in a hierarchical manner, the input shrinks at every stage –The function sooner or later consumes an atomic piece of data and terminates. 35

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Termination of Generative Recursive Procedures This characteristic is not true for generative recursive functions. –The internal recursions do not consume an immediate component of the input, but some new piece of data, which is generated from the input. This step may produce the initial input over and over again and thus prevent the evaluation from ever producing a result –We say that the program is trapped in an infinite loop. 36

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Non-Terminating Procedures What happens if we place the following three expressions at the bottom of the DrScheme Definitions window and click execute ? Does the second expression ever produce a value so that the third expression is evaluated and the canvas disappears? 37 (start WIDTH HEIGHT) (move-until-out (make-ball )) (stop)

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Non-Terminating Procedures 38 ;; less-or-equal: (list-of-numbers) number -> (list-of-numbers) (define (less-or-equal alon threshold) (cond [(empty? alon) empty] [else (if (<= (first alon) threshold) (cons (first alon) (less-or-equal alon threshold)) (less-or-equal (rest alon) threshold))])) Instead of (rest alon) (quick-sort (list 5)) = (append (quicksort (less-or-equal 5 (list 5))) (list 5) (quicksort (greater-than 5 (list 5)))) = (append (quicksort (list 5)) (list 5) (quicksort (greater-than 5 (list 5)))) The slightest mistake in process definition may cause an infinite loop: Quicksort does not terminate with the new function

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Termination Argument The termination argument is an additional step in the design recipe of generative recursive procedures Termination argument explains –why the process produces an output for every input –how the function implements this idea –when the process possibly does not terminate 39

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Termination Argument for Quicksort 40 At each step, quick-sort partitions the list into two sublists using less-or-equal and greater-than. Each function produces a list that is smaller than the input (the second argument), even if the pivot element (the first argument) is an item on the list. Hence, each recursive application of quick-sort consumes a strictly shorter list than the given one. Eventually, quick-sort receives an empty list and returns empty.

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 New Termination Cases Termination argument may reveal additional termination cases. This knowledge can be added to the algorithm: 41 So liefert (less-or-equal N (list N)) und (greater-than N (list N)) immer empty ;; quick-sort : (listof number) -> (listof number) (define (quick-sort alon) (cond [(empty? alon) empty] [(empty? (rest alon)) alon] [else (append (quick-sort (less-or-equal (rest alon) (first alon))) (list (first alon)) (quick-sort (greater-than alon (first alon)))) ]))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Guidelines for Algorithm Design 42 PhaseGoalActivity ExamplesCharacterize the input- output relationship and the computational process via examples Create and show examples of trivially solvable problems Create and show examples that require recursive processing Illustrate how to work through the examples Body Define an algorithm Formulate tests for trivially solvable problems Formulate answers for the trivial cases Determine how to generate new problems from the given problem Determine how to combine the solutions of these problems into a solution for the given problem … TerminationArgue that the algorithm terminates for all possible inputs Show that the inputs to the recursive applications are smaller than the given input

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Outline Introduction to generative recursion Sorting with generative recursion Guidelines for the design of generative recursive procedures Structural versus generative recursion Backtracking: Traversing graphs 43

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Structural Recursion as a Special Case of Generative Recursion 44 (define (generative-recursive-fun problem) (cond [(trivially-solvable? problem) (determine-solution problem)] [else (combine-solutions problem (generative-recursive-fun (generate-problem problem)))])) (define (generative-recursive-fun problem) (cond [(empty? problem) (determine-solution problem)] [else (combine-solutions problem (generative-recursive-fun (rest problem)))])) Template for generative recursion Template for list processing trivially-solvable?empty? generate-problemrest

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Structural vs. Generative Recursion Is there a difference between structural and generative recursion? –Structurally recursive functions seem to be just special cases of generative recursion –But this "it's all the same" attitude does not improve the understanding of the design process Structurally and generative recursive functions are designed using different approaches and have different consequences. 45 Structural RecursionGenerative Recursion Relies on a systematic data analysis Requires a deep insight into the problem-solving process Leads to naturally terminating functions Requires a termination argument

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Greatest Common Denominator (GCD) Examples –6 and 25 are both numbers with several denominators: 6 is evenly divisible by 1, 2, 3, and 6; 25 is evenly divisible by 1, 5, and 25. The greatest common denominator of 25 and 6 is 1. –18 and 24 have many common denominators : 18 is evenly divisible by 1, 2, 3, 6, 9, and 18; 24 is evenly divisible by 1, 2, 3, 4, 6, 8, 12, and 24. The greatest common denominator is 6. 46

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 GCD Based on Structural Recursion 47 Test for every number i = [min(n,m).. 1] whether it divides both n and m evenly and return the first such number. ;; gcd-structural : N[>= 1] N[>= 1] -> N ;; structural recursion using data definition of N[>= 1] (define (gcd-structural n m) (local ((define (first-divisor i) (cond [(= i 1) 1] [(and (= (remainder n i) 0) (= (remainder m i) 0)) i] [else (first-divisor (- i 1))] ) ) ) (first-divisor (min m n)))) Inefficient for large numbers!

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Analysis of Structural Recursion gcd-structural simply tests every number whether it divides both n and m evenly and returns the first such number –For small natural numbers, this process works just fine However, for (gcd-structural ) = 177 the procedure will compare – 177 = numbers! –Even reasonably fast computers spend several minutes on this task. Enter the definition of gcd-structural into the Definitions window and evaluate the following expression… in the Interactions window … and go get a coffee (time (gcd-structural )) 48

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 GCD Euclidean Algorithm The Euclidean algorithm to determine the greatest common denominator (GCD) of two integers is one of the oldest algorithms known –Appeared in Euclid’s Elements around 300 BC –However, the algorithm probably was not discovered by Euclid and it may have been known up to 200 years earlier. 49 Insight: For two natural numbers n and m, n > m, GCD(n, m) = GCD(m, remainder(n,m)) (gcd larger smaller) = (gcd smaller (remainder larger smaller)) Example: GCD(18, 24) = GCD(18,remainder(24/18)) = GCD(18,6) = GCD(6,0) = 6

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 GCD Generative Algorithm clever-gcd is based on generative recursion: –The trivially solvable case is when smaller is 0 –The generative step calls clever-gcd with smaller and (remainder larger smaller) 50 ;; gcd-generative : N[>= 1] N[>=1] -> N (define (gcd-generative n m) (local ((define (clever-gcd larger smaller) (cond [(= smaller 0) larger] [else (clever-gcd smaller (remainder larger smaller))])) ) (clever-gcd (max m n) (min m n)))) (gcd-generative )  needs only 9 iterations!

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 GCD Euclidean Algorithm 51 Let n = mq + r, then any number u which divides both n and m ( n = su and m = tu ), also divides r r = n – qm = su – qtu = (s – qt)u Any number v which divides both m and r ( m = s’v and r = t’v ), also divides n n = qm + r = qs´v + t´v = (s´q + t´)v Therefore, every common denominator of n and m is also a common denominator of m and r. gcd(n,m) = gcd(m,r ) It is enough if we continue the process with m and r Since r is smaller in absolute value than m, we will reach r = 0 after a finite number of steps.

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Which one to use? Question: can we conclude that generative recursion is better than structural recursion? Answer: no, not automatically. –Even a well-designed generative procedure is not always faster than structural recursion. For example, quick-sort wins over insertion sort only for large lists –Structural recursion is easier to design Designing generative recursive procedures often requires deep mathematical insight –Structural recursion is easier to understand It may be difficult to grasp the idea of the generative step. 52

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Which one to use? 53 Start by using structural recursion. If it is too slow, try to design generative recursion. Document the problem generation with good examples, give a good termination argument.

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Outline Introduction to generative recursion Sorting with generative recursion Guidelines for the design of generative recursive procedures Structural versus generative recursion Backtracking: Traversing graphs 54

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Traversing Graphs A graph is a collection of nodes and edges. The edges represent one-way connections between the nodes. Can be used to describe –a plan of one-way streets in a city, –relationships between persons, –connections on the Internet, etc. 55 A B E C F D G Scheme - list representation (define Graph '((A (B E)) (B (E F)) (C (D)) (D ()) (E (C)) (F (D G)) (G ())))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Traversing Graphs 56 ;; find-route : node node graph -> (listof node) ;; to create a path from origin to destination in G ;; false, if there is no path, (define (find-route origin destination G)...) (find-route 'C 'D Graph) = (list 'C 'D) (find-route 'E 'D Graph) = (list 'E 'C 'D) (find-route 'C 'G Graph) = false A B E C F D G

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Backtracking Algorithms A backtracking algorithm follows a specific template: 1.Pursue a (possible) path to a solution until the solution is found (success!  terminate), or the path cannot be continued. 2.If the path cannot be continued: Walk back along the path unto the last branch where alternatives exist that have not yet been chosen, Choose such an alternative and proceed with step 1. 3.If the starting point is reached again and there are no more alternatives: failure!  terminate Usage examples: –"N Queens Problem", finding paths in graphs 57

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Traversing Graphs - Example Find the path from node A to G ! 58 A B E C F D G BACKTRACK !

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Traversing Graphs If the origin is equal to the destination, the problem is trivial; the answer is (list destination). Otherwise, try to find route from all neighbors of the origin. 59 (define (find-route origination destination aGraph) (cond [(symbol=? origination destination) (list destination)] [else... (find-route/list (neighbors origination aGraph) destination aGraph)...]))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Neighbor nodes neighbors is similar to the function contains-doll? 60 ;; neighbors : node graph -> (listof node) ;; to lookup the neighbors of node in graph (define (neighbors node graph) (cond [(empty? graph) (error 'neighbors "empty graph")] [(symbol=? (first (first graph)) node) (second (first graph))] [else (neighbors node (rest graph))]))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Traversing Graphs find-route/list –Processes a list of nodes –Determine, for each of them, whether a path to the destination node in this graph exists 61 ;; find-route/list : ;; (listof node) node graph -> (listof node) or false (define (find-route/list lo-origins destination G)...) The result of find-route depends on that of find- route/list, which can be one of these: –a path from a neighbor node to the destination –false, if no path from one of the neighbors could be found

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Traversing Graphs 62 (define (find-route origination destination aGraph) (cond [(symbol=? origination destination) (list destination)] [else (local ((define possible-route (find-route/list (neighbors origination aGraph) destination aGraph))) (cond [(boolean? possible-route)...] [else (cons? possible-route)...]))]))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Traversing Graphs 63 (define (find-route origin destination aGraph) (cond [(symbol=? origin destination) (list destination)] [else (local ((define possible-route (find-route/list (neighbors origin aGraph) destination aGraph))) (cond [(boolean? possible-route) false] [else (cons origin possible-route)]))]))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Traversing Graphs - Example 64 A B E C F D G Neighbors of A Find the path from node A to G !

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Traversing Graphs 65 (define (find-route/list lo-Os dest aG) (cond [(empty? lo-Os) false] [else (local ((define possible-route (find-route (first lo-Os) dest aG))) (cond [(boolean? possible-route) (find-route/list (rest lo-Os) dest aG)] [else possible-route]) ) ]) ) A B E C F D G A B E C F D G

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Traversing Graphs 66 The function fails to terminate in a graph with a cycle: A B E C F D G (find-route 'B 'D Cyclic-graph) =... (find-route/list (list 'E 'F) 'D Cyclic-graph)... =... (find-route 'E 'D Cyclic-graph)... =... (find-route/list (list 'C 'F) 'D Cyclic-graph)... =... (find-route 'C 'D Cyclic-graph)... =... (find-route/list (list 'B 'D) 'D Cyclic-graph)... =... (find-route 'B 'D Cyclic-graph)... =... B, E, C is a cycle

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T6 Summary There are problems that cannot be solved, or can only be solved sub-optimally, using structural recursion Generative recursion is based on the principle: “divide-and-conquer” The design recipe for generative recursive functions has to be adapted: –In particular, we need an argument for the termination Structurally recursive functions are a subset of generative recursive functions When both strategies are possible the choice depends on a case-by-case basis –one cannot say that one class is better than the other 67