Pearls of Functional Algorithm Design Chapter 2 1 Roger L. Costello July 2011.

Slides:



Advertisements
Similar presentations
CS 410 Applied Algorithms Applied Algorithms Lecture #7 Counting.
Advertisements

Sorting, Sets, and Selecting - Ed. 2. and 3.: Chapter 10 - Ed. 4.: Chapter 11.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Searching, Sorting, and Algorithm Analysis
Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
CMPS1371 Introduction to Computing for Engineers SORTING.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Searching and Sorting Topics  Sequential Search on an Unordered File  Sequential Search on an Ordered File  Binary Search  Bubble Sort  Insertion.
Algorithm Design Strategy Divide and Conquer. More examples of Divide and Conquer  Review of Divide & Conquer Concept  More examples  Finding closest.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
CS 206 Introduction to Computer Science II 12 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 03 / 2008 Instructor: Michael Eckmann.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
1 Algorithms and Analysis CS 2308 Foundations of CS II.
Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Chapter 16: Searching, Sorting, and the vector Type.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
24/3/00SEM107 - © Kamin & ReddyClass 16 - Searching - 1 Class 16 - Searching r Linear search r Binary search r Binary search trees.
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.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Sorting Fun1 Chapter 4: Sorting     29  9.
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All.
CSC 211 Data Structures Lecture 13
Main Index Contents 11 Main Index Contents Building a Ruler: drawRuler() Building a Ruler: drawRuler() Merge Algorithm Example (4 slides) Merge Algorithm.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Recursion on Lists Lecture 5, Programmeringsteknik del A.
Sorting. Sorting Sorting is important! Things that would be much more difficult without sorting: –finding a telephone number –looking up a word in the.
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
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.
Today’s Material Sorting: Definitions Basic Sorting Algorithms
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Chapter 16: Searching, Sorting, and the vector Type.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Advanced Sorting 7 2  9 4   2   4   7
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Chapter 16: Searching, Sorting, and the vector Type
Searching and Sorting Algorithms
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Divide and Conquer divide and conquer algorithms typically recursively divide a problem into several smaller sub-problems until the sub-problems are.
Quick-Sort 11/14/2018 2:17 PM Chapter 4: Sorting    7 9
Quick-Sort 11/19/ :46 AM Chapter 4: Sorting    7 9
Bubble, Selection & Insertion sort
Data Structures and Algorithms
Unit-2 Divide and Conquer
Sorting … and Insertion Sort.
24 Searching and Sorting.
Divide and Conquer Algorithms Part I
Quick-Sort 2/23/2019 1:48 AM Chapter 4: Sorting    7 9
CSE 373 Data Structures and Algorithms
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
CS203 Lecture 15.
Presentation transcript:

Pearls of Functional Algorithm Design Chapter 2 1 Roger L. Costello July 2011

The Problem We Will Solve 2

Recurring Problem Stock Market: each day I record the closing value of the DOW. Occasionally, I pick a date and ask, “How many days after this date has the stock market closed at a higher value?” A more challenging question is, “Which day has the most number of following days where the stock market closed at a higher value?” 3

4 6/1/11 6/2/11 6/3/11 6/6/11 6/7/11 6/8/11 6/9/116/10/11 6/13/11 DOW: 12,324 12,214 12, ,400 12,367 12,380 12,310 12,330 12,340 Date: Number of days that surpassed this day: 6 Number of days that surpassed this day: 7 Number of days that surpassed this day: 1 Number of days that surpassed this day: 0 Number of days that surpassed this day: 1 Number of days that surpassed this day: 2 Number of days that surpassed this day: 1

5 Recurring Problem (cont.) People’s Height: line up a bunch of people. Pick one person and ask, “How many of the following people are taller than this person?” A more challenging question is, “Which person has the most number of following people that are taller?”

6 Tom John George Jim Pete Sam BillMike Shaun Height (inches): Person: Number of persons that surpass this person’s height: 1 Number of persons that surpass this person’s height: 4 Number of persons that surpass this person’s height: 2 Number of persons that surpass this person’s height: 0 Number of persons that surpass this person’s height: 3 Number of persons that surpass this person’s height: 2 Number of persons that surpass this person’s height: 1

7 Recurring Problem (cont.) Word Analysis: take a letter in a word and ask, “How many of the following letters are bigger (occurs later in the alphabet) than this letter?” A more challenging question is, “Which letter has the most number of following letters that are bigger?”

8 Word: G E N E R A T I N G Number of letters that surpass this letter: 5 Number of letters that surpass this letter: 6 Number of letters that surpass this letter: 2 Number of letters that surpass this letter: 5 Number of letters that surpass this letter: 1 Number of letters that surpass this letter: 4 Number of letters that surpass this letter: 0 Number of letters that surpass this letter: 1 Number of letters that surpass this letter: 0

Problem Statement Create a list of values. – Example: create a list of stock market values, or a list of people’s heights, or a list of letters. Simple Problem: select one value in the list, and count the number of following values that surpass it. Harder Problem: for every value in the list solve the simple problem; this produces a list of numbers; return the maximum number. – This is called the “surpasser problem” 9

Solve the Simple Problem Let’s create a function that counts the number of surpassers of a value. The function takes two arguments: 1.The value, x 2.A list, xs, that consists of all the values that follow x 10

Select the list items that are greater than 'G' 11 E N E R A T I N G filter (> 'G' ) ____ [N, R, T, I, N]

Count the selected list items 12 [N, R, T, I, N] length ____ 5 Five items surpass “G”. That’s the answer!

scount 13 “scount” (surpasser count) is a user-defined function; it is the collection of functions shown on the previous two slides. scount :: Ord a => a -> [a] -> Int scount x xs = length (filter (>x) xs) scount

Solve the Harder Problem We need to apply “scount” to each item in the list, producing a list of numbers; then take the maximum of the numbers. 14

Invoke “scount” multiple times 15 "GENERATING" scount 'G' "ENERATING"  5 scount 'E' "NERATING"  6 scount 'N' "ERATING"  2 scount 'E' "RATING"  5 scount 'R' "ATING"  1 scount 'A' "TING"  4 scount 'T' "ING"  0 scount 'I' "NG"  1 scount 'N' "G"  0 scount 'G' ""  0 maximum: 6

tails “tails” is a standard function. It takes one argument, a list. It returns a list of lists, i.e., a list of all items, then a list of all items but the first, then a list of all items but the first and second, etc. 16 tails "GENERATING" ["GENERATING","ENERATING","NERATING",…,"G",""]

List Comprehension Recall that “scount” takes as arguments a value, x, and a list consisting of its following items. A list comprehension will be used to provide the arguments to “scount”: 17 [scount z zs | z : zs <- tails xs] “For each list produced by the tails function, take its first item and the remaining items, and use them as arguments to the scount function.”

Set of surpasser counts 18 [scount z zs | z: zs <- tails ____] "GENERATING" [5,6,2,5,1,4,0,1,0,0]

maximum surpasser count (msc) 19 [5,6,2,5,1,4,0,1,0,0] maximum ____ 6 That’s the answer!

20 msc “msc” (maximum surpasser count) is a user- defined function; it is the collection of functions shown on the previous two slides. msc :: Ord a => [a] -> Int msc xs = maximum [scount z zs | z : zs <- tails xs] msc

21 Here’s the Solution import List -- msc = maximum surpasser count msc :: Ord a => [a] -> Int msc xs = maximum [scount z zs | z : zs <- tails xs] scount :: Ord a => a -> [a] -> Int scount x xs = length (filter (>x) xs)

Time Requirements With a list of length “n” the msc function shown on the previous slide takes on the order of n 2 steps. Here’s why: recall that n surpasser counts are generated (see slide 18). To generate the first surpasser count, we take the first list item and compare it against the remaining n-1 items. To generate the second surpasser count, we take the second list item and compare it against the remaining n-2 items. And so forth. So, the total number of comparisons is: (n-1) + (n-2) + … + 1 = n(n+1)/2, i.e., T(n) = O(n 2 ) 22

23 Divide and Conquer Solution

The Key Concepts 1.Determine the maximum surpasser count (msc) of list ws. 2.Divide ws into two lists: ws  xs + ys 3.Determine the scount of each value in xs and the scount of each value in ys. 4.Assume that xs and ys are sorted in increasing order and ys is of length n. 5.x is the first value in xs and it has an scount (within xs) of c. y is the first value in ys and it has an scount (within ys) of d. There are the two cases to consider: a)x < y: then the scount of x equals c + n (remember, ys is sorted, so if x < y then it is less than all n values in ys). b)x ≥ y: then the scount of y equals d (remember, xs and ys are sorted, so if x ≥ y then y is less than all values in xs and all values in ys). 24

The Simplest Example 25 GE G E Split into xs and ys

26 GE ('G',0) ('E',0) The scount of 'G' in xs is zero and the scount of 'E' in ys is zero.

27 GE ('G',0) ('E',0) xs is sorted in increasing order and so is ys. Obviously.

28 GE ('G',0) ('E',0) Compare 'G' with 'E'. 'G' ≥ 'E' so 'E' must be the smallest value. Output 'E' then 'G'.

29 GE ('G',0) ('E',0) ('E',0) : ('G',0)

30 GE ('G',0) ('E',0) ('E',0) : ('G',0) These are the correct surpasser counts for GE. Furthermore, the resulting list is sorted!

Another Simple Example 31 NE N E Split into xs and ys

32 NE ('N',0) ('E',0) The scount of 'N' in xs is zero and the scount of 'E' in ys is zero.

33 GE ('N',0) ('E',0) xs is sorted in increasing order and so is ys. Obviously.

34 GE ('N',0) ('E',0) Compare 'N' with 'E'. 'N' ≥ 'E' so 'E' must be the smallest value. Output 'E' then 'N'.

35 GE ('N',0) ('E',0) ('E',0) : ('N',0)

36 GE ('N',0) ('E',0) ('E',0) : ('N',0) These are the correct surpasser counts for NE. Furthermore, the resulting list is sorted!

A larger example 37 GENE GE NE Split into xs and ys

38 GENE GE NE The previous slides showed how to process the two sub- lists. ('E',0) : ('G',0) ('E',0) : ('N',0)

39 GENE GE NE ('E',0) : ('G',0) ('E',0) : ('N',0) Compare 'E' with 'E'. 'E' ≥ 'E' so the right 'E' must be the smallest value. Output 'E' and process the remaining sub-lists.

40 GENE GE NE ('E',0) : ('G',0) ('N',0) Output: ('E', 0)

41 GENE GE NE ('E',0) : ('G',0) ('N',0) Compare 'E' with 'N'. 'E' < 'N' so all the values in ys must be surpassers of 'E'. Output 'E', but first increment its surpasser count by length ys.

42 GENE GE NE ('G',0) ('N',0) Output: ('E', 0) : ('E', 1)

43 GENE GE NE ('G',0) ('N',0) Compare 'G' with 'N'. 'G' < 'N' so all the values in ys must be surpassers of 'N'. Output 'G', but first increment its surpasser count by length ys.

44 GENE GE NE ('N',0) Output: ('E', 0) : ('E', 1) : ('G', 1) ""

45 GENE GE NE ('N',0) Output 'N'. ""

46 GENE GE NE Output: ('E', 0) : ('E', 1) : ('G', 1) : ('N', 0) ""

Surpasser Counts 47 GENE Output: ('E', 0) : ('E', 1) : ('G', 1) : ('N', 0) let zs = the list of second values in each pair msc = the maximum of zs

Terminology: table 48 GENE ('E', 0) : ('E', 1) : ('G', 1) : ('N', 0) The result of processing is a list of pairs. The second value is the scount of the first value. This list of pairs is called a "table". The "table function" takes as its argument a list and returns a table.

Terminology: join 49 GE ('N',0) ('E',0) ('E',0) : ('N',0) Processing two sub-lists to create one list is called "join". The "join function" takes as its arguments two lists, xs and ys, and returns a table.

Here's how to implement the table function 50 table :: Ord a => [a] -> [(a,Int)] table (w:[]) = [(w, 0)] table ws = join (table xs) (table ys) where m = length (ws) n = m `div` 2 (xs,ys) = splitAt n (ws) "Process a list. If there is just one value in the list then its surpasser count is zero and return a list containing one pair, where the second value is zero. If there's more than one value in the list then divide the list in half, into xs and ys; get the table of xs and the table of ys (i.e., recurse) and then join those two tables."

51 Here's how to implement the join function join :: Ord a => [(a,Int)] -> [(a,Int)] -> [(a,Int)] join [] tys = tys join txs [] = txs join | x < y = (x, c + length ys) : join txxs ys | x >= y = (y, d) : join xs tyys "Join two tables, txs and tys. If txs is empty then return tys. It tys is empty then return txs. Compare the first value of txs with the first value of tys. Specifically, compare the first value of each pair, (x,c) and (y,d). If x = y then y 's surpasser count is d. Join the remaining tables."

Efficiency improvment Each time the join function is invoked it computes the length of tys. To gain a slight efficiency improvement, invoke join with an additional argument: a value, n, corresponding to the length of tys. 52

Here's how to implement msc 53 msc :: Ord a => [a] -> Int msc ws = maximum (map snd (table ws)) "Invoke the table function with the list, ws. It returns a list of pairs, (value, surpasser count). Create a list containing all the surpasser counts. Use map snd to accomplish this. Now get the largest surpasser count."

Here's the complete implementation 54 import List -- msc = maximum surpasser count msc :: Ord a => [a] -> Int msc ws = maximum (map snd (table ws)) table :: Ord a => [a] -> [(a,Int)] table (w:[]) = [(w, 0)] table ws = join (table xs) (table ys) where m = length (ws) n = m `div` 2 (xs,ys) = splitAt n (ws) join :: Ord a => [(a,Int)] -> [(a,Int)] -> [(a,Int)] join [] tys = tys join txs [] = txs join | x < y = (x, c + length ys) : join txxs ys | x >= y = (y, d) : join xs tyys

Time Requirements With a list of length “n” the msc function shown on the previous slide takes on the order of n log n steps. That's a lot faster than the first solution, especially for a large list. 55