Algorithmic Problem Solving

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

CSC 421: Algorithm Design & Analysis
Types of Algorithms.
What is divide and conquer? Divide and conquer is a problem solving technique. It does not imply any specific computing problems. The idea is to divide.
CSCE 3110 Data Structures & Algorithm Analysis
Iteration and Recursion Tonga Institute of Higher Education.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
CMPS1371 Introduction to Computing for Engineers SORTING.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Recursion, Complexity, and Sorting By Andrew Zeng.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
1 Summary: Design Methods for Algorithms Andreas Klappenecker.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
Types of Algorithms. 2 Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We’ll talk about a classification.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Sorting and selection – Part 2 Prof. Noah Snavely CS1114
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Sorting Quick, Merge & Radix Divide-and-conquer Technique subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to.
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
CMPT 438 Algorithms.
Greedy algorithms: CSC317
Some more Decrease-and-conquer Algorithms
CMSC201 Computer Science I for Majors Lecture 20 – Recursion (Continued) Prof. Katherine Gibson Based on slides from UPenn’s CIS 110, and from previous.
Week 9 - Monday CS 113.
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
CS 3343: Analysis of Algorithms
CSC 421: Algorithm Design & Analysis
Quick-Sort 9/12/2018 3:26 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Section 10.3b Quick Sort.
Teach A level Computing: Algorithms and Data Structures
Types of Algorithms.
Divide And Conquer Distinguish between small and large instances.
Quicksort and Mergesort
CS 3343: Analysis of Algorithms
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Last Class We Covered Data representation Binary numbers ASCII values
Types of Algorithms.
Ch 7: Quicksort Ming-Te Chi
“Human Sorting” It’s a “Problem Solving” game:
Data Structures Review Session
B- Trees D. Frey with apologies to Tom Anastasio
Last Class We Covered Dictionaries Hashing Dictionaries vs Lists
Topic: Divide and Conquer
Discrete Mathematics CMP-101 Lecture 12 Sorting, Bubble Sort, Insertion Sort, Greedy Algorithms Abdul Hameed
Many slides borrowed lovingly from Skiena
Sub-Quadratic Sorting Algorithms
CSE373: Data Structure & Algorithms Lecture 21: Comparison Sorting
Quickselect Prof. Noah Snavely CS1114
Quicksort.
CSC 421: Algorithm Design & Analysis
CS 1114: Sorting and selection (part two)
Ch. 2: Getting Started.
Types of Algorithms.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Divide & Conquer Sorting
Divide And Conquer Distinguish between small and large instances.
The Selection Problem.
CSC 421: Algorithm Design & Analysis
“Human Sorting” It’s a “Problem Solving” game:
Quicksort.
Week 6 - Monday CS221.
Sorting and selection Prof. Noah Snavely CS1114
Presentation transcript:

Algorithmic Problem Solving SPIS 2017 Benjamin Cosman

”Find the biggest number in a list of positive numbers.” What is APS? There are two steps to getting from here… …to here: def findMax(lst):     biggest = 0     for num in lst:         if num > biggest:             biggest = num     return biggest ”Find the biggest number in a list of positive numbers.” ???

Algorithmic Problem Solving What is APS? Programming is turning algorithms into code APS is finding the algorithm in the first place This is an important skill, and it doesn’t require a computer! ”For each number in the list, if it is greater than the old biggest number, forget that old number and store the new one instead.” def findMax(lst):     biggest = 0     for num in lst:         if num > biggest:             biggest = num     return biggest Algorithmic Problem Solving ”Find the biggest number in a list of positive numbers.” Programming

What is APS? “As there is little foolish wand-waving here, many of you will hardly believe this is magic.” –Snape, on Potions class There will be no computers in this course, but it is Computer Science!

What is APS? Goals in this course: Develop a toolbox of algorithmic strategies Practice applying those strategies to solve problems Write solutions precisely and coherently Analyze the speed of algorithms

Today: Divide-and-Conquer

Learning Goals Divide-and-Conquer will be the first tool in your toolbox After this week you should be able to Define the Divide-and-Conquer algorithmic strategy Apply the strategy to solve some of those problems Analyze how many steps a simple algorithm takes to run

Example 1: Find the Fake Coin You have a pile of 12 coins, 1 of which is counterfeit. Real coins each weigh 10 grams; the fake one weighs 9. You have a scale which can tell you how much a pile of coins weighs. How can you identify the fake coin in as few weighings as possible? Is the problem clear? Work with neighbors

Fake Coin solution Divide and Conquer! Split the coins into two piles as evenly as possible Weigh one of the piles. If the weight ends in 9, it is the pile with the fake. Otherwise, the other pile has the fake. Use this same strategy on whichever pile has the fake to keep narrowing it down by half. If there is one coin left, we are done: it’s the fake! Division Recursion Base Case

Divide-and-conquer Division – Split the problem into 2 or more subproblems Recursion – Solve subproblems the same way as the original Base Case – When the problem gets small enough, declare victory There will always be some additional work somewhere in there In example 1: before recursion, to choose which subproblem to look at Usually best to put base case first

Fake Coin solution Why is is important to put that base case first? a) If we don’t, it can be impossible to do what the algorithm says b) If we don’t, the algorithm can go on forever c) There is a different reason it’s important d) I don’t know Fake Coin solution Split the coins into two piles as evenly as possible. Weigh one of the piles. If the weight ends in 9, it is the pile with the fake. Otherwise, the other pile has the fake. Use this same strategy on whichever pile has the fake to keep narrowing it down by half. If there is one coin left, we are done: it’s the fake!

Fake Coin solution How long does this algorithm take on 12 coins? 4 weighings 5 weighings 4 or 5 3 or 4 I don’t know Fake Coin solution If there is one coin left, we are done: it’s the fake! Split the coins into two piles as evenly as possible. Weigh one of the piles. If the weight ends in 9, it is the pile with the fake. Otherwise, the other pile has the fake. Use this same strategy on whichever pile has the fake to keep narrowing it down by half.

How long does this algorithm take on n coins, in the worst case? n weighings n/3 or n/4 weighings 𝑙𝑜𝑔 2 (𝑛) weighings What do and 𝑙𝑜𝑔 2 mean? I don’t know Fake Coin solution If there is one coin left, we are done: it’s the fake! Split the coins into two piles as evenly as possible. Weigh one of the piles. If the weight ends in 9, it is the pile with the fake. Otherwise, the other pile has the fake. Use this same strategy on whichever pile has the fake to keep narrowing it down by half.

A quick refresher on Logarithms 𝑙𝑜𝑔 𝑎 (𝑏) is the number for which this is true: 𝑎 𝑙𝑜𝑔 𝑎 (𝑏) =𝑏 In other words, it’s the number to which you have to raise a to get b, …which you can think of as the number of times you have to divide b by a to get 1 E.g. 𝑙𝑜𝑔 2 8 =3 because 2 3 =8 …or because you have to divide 8 by 2 three times to get to 1 Very useful for analyzing divide-and-conquer! just means round up, e.g. 2.3 =3= 3

Example 2: Tile a Grid with L’s You have a grid of squares with 2 𝑛 squares per side. (n > 0) 2x2, 4x4, 8x8, 16x16, etc. One square has been removed from the grid. Can you cover the remaining squares using non-overlapping L-shaped trominos? How can we split this into subproblems (like the original, but smaller?)

Tiling solution If the grid size is 2x2 (with a removed square), place one tromino on the remaining 3 squares and we’re done. Otherwise, split the grid into quarters. The center 4 squares of the grid belong one each to the four different quarters. Place a tromino on the 3 of them belonging to the quarters that do not yet have a removed square. Treat those covered squares as “removed”: every quarter now has a removed square. Solve each quarter recursively using this same algorithm.

Divide-and-conquer Division – Split the problem into 2 or more subproblems Recursion – Solve subproblems the same way as the original Base Case – When the problem gets small enough, declare victory There will always be some additional work somewhere in there In example 1: before recursion, to choose which subproblem to look at In example 2: before recursion, to make the subproblems actually subproblems

Example 3: Sorting a list You have a list of numbers, e.g. [2, 4, 1, 6, 8, 5, 3, 7] How can you sort the list? How can you sort the list quickly?

Sorting a list, take 1 “Insertion sort”: https://youtu.be/i-SKeOcBwko?t=3m57s In the worst case, how far do we have to slide each number? The 1st number doesn’t have to move The 2nd might have to move 1 space The 3rd might have to move 2 spaces … The nth might have to move n-1 spaces Total slides: 0 + 1 + 2 + … + n-1 ≈ n2/2

Sorting a list, take 2 “Merge sort”: https://youtu.be/TzeBrDU-JaY?t=2m25s It takes (approx.) n comparisons to merge the lists at one level of the tree up to the next level There are (approx.) log2n levels of the tree So merge sort takes ≈ n * log2n comparisons For a large list (large n), this is many fewer than n2/2

Divide-and-conquer Division – Split the problem into 2 or more subproblems Recursion – Solve subproblems the same way as the original Base Case – When the problem gets small enough, declare victory There will always be some additional work somewhere in there In example 1: before recursion, to choose which subproblem to look at In example 2: before recursion, to make the subproblems actually subproblems In example 3: after recursion, to merge the subproblem solutions into an overall solution

Course Logistics Meet here Wed and Fri 8:45-10am (Approximate) topic list: Divide-and-Conquer ✓ Greedy Method Iterative Improvement Backtracking Dynamic Programming Homework due Sunday night at 10pm

Homework You can brainstorm ideas together, but write solutions by yourself Do not use any resource other than those linked from the SPIS site Have a resource you think you should be able to use, or otherwise unsure about these expectations? Ask me (or a mentor) This week, I will be at open lab hours every day (1:15-2:45)