TUTORIAL 8 Generative Recursion. Reminders  Deadline of midterm remark  Assignment 7 due Wed, Mar 18 th.

Slides:



Advertisements
Similar presentations
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Advertisements

CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Alon Efrat Computer Science Department University of Arizona SkipList.
Finite Automata CPSC 388 Ellen Walker Hiram College.
Lecture 3: Parallel Algorithm Design
CS 116 Tutorial 2 Functional Abstraction. Reminders Assignment 2 is due this Wednesday at Noon.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Skip List & Hashing CSE, POSTECH.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
Chapter 7: Sorting Algorithms
Finding the Median Algorithm : Design & Analysis [11]
CMPS1371 Introduction to Computing for Engineers SORTING.
1 Issues with Matrix and Vector Issues with Matrix and Vector Quicksort Quicksort Determining Algorithm Efficiency Determining Algorithm Efficiency Substitution.
Tirgul 10 Rehearsal about Universal Hashing Solving two problems from theoretical exercises: –T2 q. 1 –T3 q. 2.
Review Binary –Each digit place is a power of 2 –Any two state phenomenon can encode a binary number –The number of bits (digits) required directly relates.
Sorting. Introduction Assumptions –Sorting an array of integers –Entire sort can be done in main memory Straightforward algorithms are O(N 2 ) More complex.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Data Structures Review Session 1
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Special Sum The First N Integers. 9/9/2013 Sum of 1st N Integers 2 Sum of the First n Natural Numbers Consider Summation Notation ∑ k=1 n k =
CSE 373 Data Structures Lecture 15
Recursively Defined Sequences Lecture 35 Section 8.1 Wed, Mar 23, 2005.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
EE2174: Digital Logic and Lab Professor Shiyan Hu Department of Electrical and Computer Engineering Michigan Technological University CHAPTER 2 Number.
1 Lecture 16: Lists and vectors Binary search, Sorting.
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
1 Data Structures CSCI 132, Spring 2014 Lecture 32 Tables I.
Algorithms and their Applications CS2004 ( ) Professor Jasna Kuljis (adapted from Dr Steve Swift) 6.1 Classic Algorithms - Sorting.
When data from a table (or tables) needs to be manipulated, easier to deal with info in form of a matrix. Matrices FreshSophJunSen A0342 B0447 C2106 D1322.
Sets of Digital Data CSCI 2720 Fall 2005 Kraemer.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Programming Abstractions Cynthia Lee CS106X. Today’s Topics Sorting! 1.The warm-ups  Selection sort  Insertion sort 2.Let’s use a data structure! 
Overview Excel is a spreadsheet, a grid made from columns and rows. It is a software program that can make number manipulation easy and somewhat painless.
1 Ch.19 Divide and Conquer. 2 BIRD’S-EYE VIEW Divide and conquer algorithms Decompose a problem instance into several smaller independent instances May.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
Recitation 8 Programming for Engineers in Python.
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
23 February Recursion and Logarithms CSE 2011 Winter 2011.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
(Proof By) Induction Recursion
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Warmup What is an abstract class?
CS1010 Discussion Group 11 Week 11 – Recursion recursion recursion….
Data Structures in Java with JUnit ©Rick Mercer
Sorting by Tammy Bailey
Algorithm Design Methods
Hash tables Hash table: a list of some fixed size, that positions elements according to an algorithm called a hash function … hash function h(element)
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
5.1 The Stack Abstract Data Type
Algorithm design and Analysis
Recursion UW CSE 160 Winter 2017
Recursion UW CSE 160 Spring 2018
Unit-2 Divide and Conquer
“Human Sorting” It’s a “Problem Solving” game:
Data Structures Review Session
Recitation Outline C++ STL associative containers Examples
Word Problems Numerical Solutions
Recursion (Continued)
Intro to Computer Science CS1510 Dr. Sarah Diesburg
“Human Sorting” It’s a “Problem Solving” game:
Recursion Review Spring 2019 CS 1110
Presentation transcript:

TUTORIAL 8 Generative Recursion

Reminders  Deadline of midterm remark  Assignment 7 due Wed, Mar 18 th

Tips for python  Don’t leave lines in the middle of your code  Up arrow key in interactions window  Make sure you are using Python 3  Do not add any special characters in the code

Generative recursion  CQ: which one is a generative recursion?

Generative recursion  CQ: which one is a generative recursion?

Generative recursion  CQ: what does recursion3([1,2,2,1]) and recursion3([1,2,1]) produce?  A) True  B) False  C) None

Generative recursion  CQ: what does recursion3([1,2,2,1]) and recursion3([1,2,1]) produce?  A) True  B) False  C) None

Generative Recursion  Sorting algorithms  Log(n) and nlog(n) runtimes  Sort vs sorted functions  Insertion, selection, merge 

Question 1 - smaller Write a function smaller that consumes a string containing only numeric characters and produces the smallest digit in the string. The produced digit should be in the form of a string. For example, smaller(“4325”) => “2” smaller(“1”) => “1” smaller(“2325”) => “2”

Solution method: - If there is only one digit, produce that digit - Compare the first digit with the last digit - Remove the larger of the two digits from the string and repeat the process - If there is a tie, remove the last digit from the string and repeat the process YEAH!!

Question 2 - Quicksort Consider a different way of sorting a list L of distinct integers: - Let n be the first element of the list - Let lst1 be all the elements in the list smaller than n - Let lst2 be all the elements in the list larger than n - Sort lst1 and lst2 - Add lst1 and lst2 together with n in the middle

Example quicksort([2,3,1,4,0])  quicksort([1,0]) + [2] + quicksort([3,4])  (quicksort([0]) + [1]) + [2] + ([3] +quicksort([4]))  ([0] + [1])+ [2] + ([3] + [4])  [0,1] + [2] +[3,4]  [0,1,2,3,4]

Questions 6 – sum_columns  Consider a new type: Table. A Table is a (listof (listof Int)), which is nonempty, and in which each list corresponds to a row of a Table. It is assumed that each row is nonempty and each row has the same number of entries as every other row.  Write a function sum_columns that consumes a Table t, and produces a list containing the columns sums for t.

Example  t0 = [[1]] t1 = [[1,2,3]] t2 = [[1],[2],[3]] t3 = [[1,2],[3,4],[5,6],[7,8]]  sum_columns(t0) => [1] sum_columns(t1) => [1,2,3] sum_columns(t3) => [16, 20]

Question 4 – binary_gcd Consider a different algorithm for computing the greatest common divisors of two positive integers m and n: - If either m or n are 0, produce the other number - If both numbers are even, produce 2*binary_gcd(m/2,n/2) - If m is even and n is odd, produce binary_gcd(m/2,n) - If n is even and m is odd, produce binary_gcd(m,n/2) - If both m and n are odd and m>=n, produce binary_gcd((m- n)/2,n) - If both m and n are odd and m<n, produce binary_gcd((n- m)/2,m)

Question 3 – is_balanced We define a string to be “balanced” based on the following criteria: - Any string not containing any brackets (any of: (,),{,},[,] ) is considered balanced - If s is a balanced string then (s), {s}, and [s] are also balanced strings - If s is a balanced string and t is a string not containing any brackets, then s+t is a balanced string and so is t+s - For example, “(Wo)rd” is balanced as well as “rd{Wo}”

Examples  Balanced strings:  “”  “(hi)”  “wrong{}thing”  “[cool(brackets)]”  “{[([{()}])]}”  Unbalanced strings:  “NO)PE”  “{no)”

Question 5 – skip_value Given a list L of positive integers, what is the skip- value of the list? - If L is empty, the skip value is 0 - If L is nonempty: - Add one to the current skip value - Move ahead in the list by n (where n is the first element in the list) and repeat the process

Example 1 skip_value([1,1,1])  1+skip_value([1,1])  1+(1+skip_value([1]))  1+(1+(1+skip_value([])))  1+(1+(1+0))  3

Example 2 skip_value([2,100,3,1,1,1])  1+skip_value([3,1,1,1])  1+(1+skip_value([1]))  1+(1+(1+skip_value([])))  1+(1+(1+0))  3