CSE373: Data Structure & Algorithms Lecture 23: More Sorting and Other Classes of Algorithms. Nicki Dell Spring 2014

Similar documents
Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 6 Lecture - 37 Divide and Conquer: Counting Inversions

Past questions from the last 6 years of exams for programming 101 with answers.

Lecture 20: Combinatorial Search (1997) Steven Skiena. skiena

Heuristics, and what to do if you don t know what to do. Carl Hultquist

CS/COE 1501

Topic 10 Recursive Backtracking

Algorithms and Data Structures CS 372. The Sorting Problem. Insertion Sort - Summary. Merge Sort. Input: Output:

CSE 373 DECEMBER 4 TH ALGORITHM DESIGN

Chapter 5 Backtracking. The Backtracking Technique The n-queens Problem The Sum-of-Subsets Problem Graph Coloring The 0-1 Knapsack Problem

Divide & conquer. Which works better for multi-cores: insertion sort or merge sort? Why?

CSE465, Spring 2009 March 16 1

DATA STRUCTURES USING C

More Recursion: NQueens

Introduction to. Algorithms. Lecture 10. Prof. Piotr Indyk

Introduction to. Algorithms. Lecture 10. Prof. Constantinos Daskalakis CLRS

CSc 110, Spring Lecture 40: Sorting Adapted from slides by Marty Stepp and Stuart Reges

Lectures: Feb 27 + Mar 1 + Mar 3, 2017

Second Annual University of Oregon Programming Contest, 1998

MA/CSSE 473 Day 14. Permutations wrap-up. Subset generation. (Horner s method) Permutations wrap up Generating subsets of a set

CSE 573 Problem Set 1. Answers on 10/17/08

Using a Stack. Data Structures and Other Objects Using C++

Outline for today s lecture Informed Search Optimal informed search: A* (AIMA 3.5.2) Creating good heuristic functions Hill Climbing

CSE 332: Data Structures and Parallelism Games, Minimax, and Alpha-Beta Pruning. Playing Games. X s Turn. O s Turn. X s Turn.

Merge Sort. Note that the recursion bottoms out when the subarray has just one element, so that it is trivially sorted.

Lecture 12: Divide and Conquer Algorithms. Divide and Conquer Algorithms

CSE 21 Practice Final Exam Winter 2016

MAS336 Computational Problem Solving. Problem 3: Eight Queens

Previous Lecture. How can computation sort data faster for you? Sorting Algorithms: Speed Comparison. Recursive Algorithms 10/31/11

Search then involves moving from state-to-state in the problem space to find a goal (or to terminate without finding a goal).

CSE 417: Review. Larry Ruzzo

MITOCW watch?v=krzi60lkpek

The remarkably popular puzzle demonstrates man versus machine, backtraking and recursion, and the mathematics of symmetry.

CS188: Section Handout 1, Uninformed Search SOLUTIONS

Introduction to Genetic Algorithms

Fast Sorting and Pattern-Avoiding Permutations

Animation Demos. Shows time complexities on best, worst and average case.

6.006 Introduction to Algorithms. Lecture 20: Dynamic Programming III Prof. Erik Demaine

Chapter 7: Sorting 7.1. Original

Eight Queens Puzzle Solution Using MATLAB EE2013 Project

MITOCW watch?v=uk5yvoxnksk

2048: An Autonomous Solver

MITOCW 7. Counting Sort, Radix Sort, Lower Bounds for Sorting

SOME MORE DECREASE AND CONQUER ALGORITHMS

ENGR170 Assignment Problem Solving with Recursion Dr Michael M. Marefat

Lecture 7: The Principle of Deferred Decisions

CS61B Lecture #22. Today: Backtracking searches, game trees (DSIJ, Section 6.5) Last modified: Mon Oct 17 20:55: CS61B: Lecture #22 1

CSL 356: Analysis and Design of Algorithms. Ragesh Jaiswal CSE, IIT Delhi

Checkpoint Questions Due Monday, October 7 at 2:15 PM Remaining Questions Due Friday, October 11 at 2:15 PM

MITOCW R3. Document Distance, Insertion and Merge Sort


A Real-Time Algorithm for the (n 2 1)-Puzzle

Using a Stack. The N-Queens Problem. The N-Queens Problem. The N-Queens Problem. The N-Queens Problem. The N-Queens Problem

In order for metogivebackyour midterms, please form. a line and sort yourselves in alphabetical order, from A

MITOCW watch?v=6fyk-3vt4fe

CSE548, AMS542: Analysis of Algorithms, Fall 2016 Date: Sep 25. Homework #1. ( Due: Oct 10 ) Figure 1: The laser game.

Binary trees. Application: AVL trees and the golden ratio. The golden ratio. φ 1=1/φ 1. φ =1+

CPS331 Lecture: Heuristic Search last revised 6/18/09

Overview. Algorithms: Simon Weber CSC173 Scheme Week 3-4 N-Queens Problem in Scheme

Monte Carlo based battleship agent

ISudoku. Jonathon Makepeace Matthew Harris Jamie Sparrow Julian Hillebrand

N-Queens Problem. Latin Squares Duncan Prince, Tamara Gomez February

Compressing Pattern Databases

10/5/2015. Constraint Satisfaction Problems. Example: Cryptarithmetic. Example: Map-coloring. Example: Map-coloring. Constraint Satisfaction Problems

More on games (Ch )

MA/CSSE 473 Day 13. Student Questions. Permutation Generation. HW 6 due Monday, HW 7 next Thursday, Tuesday s exam. Permutation generation

Problem A. Jumbled Compass

CSE : Python Programming

Complete and Incomplete Algorithms for the Queen Graph Coloring Problem

isudoku Computing Solutions to Sudoku Puzzles w/ 3 Algorithms by: Gavin Hillebrand Jamie Sparrow Jonathon Makepeace Matthew Harris

CS431 homework 2. 8 June Question 1 (page 54, problem 2.3). Is lg n = O(n)? Is lg n = Ω(n)? Is lg n = Θ(n)?

A Lower Bound for Comparison Sort

ACM ICPC World Finals Warmup 2 At UVa Online Judge. 7 th May 2011 You get 14 Pages 10 Problems & 300 Minutes

Image Forgery. Forgery Detection Using Wavelets

MITOCW 6. AVL Trees, AVL Sort

Tiling Problems. This document supersedes the earlier notes posted about the tiling problem. 1 An Undecidable Problem about Tilings of the Plane

Lecture 6: Latin Squares and the n-queens Problem

Prolog - 3. Prolog Nomenclature

1. Non-Adaptive Weighing

MITOCW R7. Comparison Sort, Counting and Radix Sort

1 Introduction The n-queens problem is a classical combinatorial problem in the AI search area. We are particularly interested in the n-queens problem

Extended Introduction to Computer Science CS1001.py

GENERALIZATION: RANK ORDER FILTERS

CS3334 Data Structures Lecture 4: Bubble Sort & Insertion Sort. Chee Wei Tan

Maze Solving Algorithms for Micro Mouse

Lecture 13 Register Allocation: Coalescing

Comp th February Due: 11:59pm, 25th February 2014

Adversary Search. Ref: Chapter 5

Backtracking. Chapter Introduction

MITOCW MIT6_172_F10_lec13_300k-mp4

Games and Adversarial Search II

Data Structure Analysis

1 Permutations. 1.1 Example 1. Lisa Yan CS 109 Combinatorics. Lecture Notes #2 June 27, 2018

The Mathematics Behind Sudoku Laura Olliverrie Based off research by Bertram Felgenhauer, Ed Russel and Frazer Jarvis. Abstract

Playing Games. Henry Z. Lo. June 23, We consider writing AI to play games with the following properties:

Advanced Automata Theory 4 Games

Solving Problems by Searching

Universiteit Leiden Opleiding Informatica

AI Approaches to Ultimate Tic-Tac-Toe

Computer Graphics (CS/ECE 545) Lecture 7: Morphology (Part 2) & Regions in Binary Images (Part 1)

Collectives Pattern CS 472 Concurrent & Parallel Programming University of Evansville

Transcription:

CSE373: Data Structure & Algorithms Lecture 23: More Sorting and Other Classes of Algorithms Nicki Dell Spring 2014

Admin No class on Monday Extra time for homework 5 J 2

Sorting: The Big Picture Surprising amount of neat stuff to say about sorting: Simple algorithms: O(n 2 ) Fancier algorithms: O(n log n) Comparison lower bound: Ω(n log n) Specialized algorithms: O(n) Handling huge data sets Insertion sort Selection sort Shell sort Heap sort Merge sort Quick sort Bucket sort Radix sort External sorting 3

Radix sort Origins go back to the 1890 U.S. census Radix = the base of a number system Examples will use 10 because we are used to that Idea: In implementations use larger numbers For example, for ASCII strings, might use 128 Bucket sort on one digit at a time Number of buckets = radix Starting with least significant digit Keeping sort stable Do one pass per digit Invariant: After k passes (digits), the last k digits are sorted 4

Example Radix = 10 0 1 2 3 4 5 6 7 8 9 721 3 143 537 67 478 38 9 Input: 478 537 9 721 3 38 143 67 First pass: bucket sort by ones digit Order now: 721 3 143 537 67 478 38 9 5

Example 0 1 721 2 3 3 143 4 5 6 7 537 67 8 478 38 9 9 Radix = 10 0 1 2 3 4 5 6 7 8 9 3 9 721 537 38 143 67 478 Order was: 721 3 143 537 67 478 38 9 Second pass: stable bucket sort by tens digit Order now: 3 9 721 537 6 38 143 67 478

0 1 2 3 4 5 6 7 8 9 Example 3 9 721 537 38 143 67 478 Radix = 10 Order was: 3 9 721 537 38 143 67 478 0 3 9 38 67 1 143 Third pass: 2 3 4 stable bucket sort by 100s digit Order now: 3 7 478 5 537 6 7 721 8 9 9 38 67 143 478 537 721

Analysis Input size: n Number of buckets = Radix: B Number of passes = Digits : P Work per pass is 1 bucket sort: O(B+n) Total work is O(P(B+n)) Compared to comparison sorts, sometimes a win, but often not Example: Strings of English letters up to length 15 Run-time proportional to: 15*(52 + n) This is less than n log n only if n > 33,000 Of course, cross-over point depends on constant factors of the implementations And radix sort can have poor locality properties 8

Sorting: The Big Picture Surprising amount of neat stuff to say about sorting: Simple algorithms: O(n 2 ) Fancier algorithms: O(n log n) Comparison lower bound: Ω(n log n) Specialized algorithms: O(n) Handling huge data sets Insertion sort Selection sort Shell sort Heap sort Merge sort Quick sort Bucket sort Radix sort External sorting 9

Last Slide on Sorting Simple O(n 2 ) sorts can be fastest for small n Selection sort, Insertion sort (latter linear for mostly-sorted) Good for below a cut-off to help divide-and-conquer sorts O(n log n) sorts Heap sort, in-place but not stable nor parallelizable Merge sort, not in place but stable and works as external sort Quick sort, in place but not stable and O(n 2 ) in worst-case Often fastest, but depends on costs of comparisons/copies Ω (n log n) is worst-case and average lower-bound for sorting by comparisons Non-comparison sorts Bucket sort good for small number of possible key values Radix sort uses fewer buckets and more phases Best way to sort? It depends! 10

Done with sorting! (phew..) Moving on. There are many many algorithm techniques in the world We ve learned a few What are a few other classic algorithm techniques you should at least have heard of? And what are the main ideas behind how they work? 11

Algorithm Design Techniques Greedy Shortest path, minimum spanning tree, Divide and Conquer Divide the problem into smaller subproblems, solve them, and combine into the overall solution Often done recursively Quick sort, merge sort are great examples Dynamic Programming Brute force through all possible solutions, storing solutions to subproblems to avoid repeat computation Backtracking A clever form of exhaustive search 12

Dynamic Programming: Idea Divide a bigger problem into many smaller subproblems If the number of subproblems grows exponentially, a recursive solution may have an exponential running time L Dynamic programming to the rescue! J Often an individual subproblem may occur many times! Store the results of subproblems in a table and re-use them instead of recomputing them Technique called memoization 13

Fibonacci Sequence: Recursive The fibonacci sequence is a very famous number sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... The next number is found by adding up the two numbers before it. Recursive solution: fib(int n) { if (n == 1 n == 2) { return 1 } return fib(n 2) + fib(n 1) } Exponential running time! A lot of repeated computation 14

Repeated computation f(7) f(5) f(6) f(3) f(4) f(5) f(1) f(2) f(2) f(3) f(4) f(1) f(2) f(4) f(2) f(1) f(3) f(2) f(1) f(3) f(2) f(2) f(1) f(3) f(2) 15

Fibonacci Sequence: memoized fib(int n) { Map results = new Map() results.put(1, 1) results.put(2, 1) return fibhelper(n, results) } fibhelper(int n, Map results) { if (!results.contains(n)) { results.put(n, fibhelper(n-2)+fibhelper(n-1)) } return results.get(n) } Now each call of fib(x) only gets computed once for each x! 16

Comments Dynamic programming relies on working from the bottom up and saving the results of solving simpler problems These solutions to simpler problems are then used to compute the solution to more complex problems Dynamic programming solutions can often be quite complex and tricky Dynamic programming is used for optimization problems, especially ones that would otherwise take exponential time Only problems that satisfy the principle of optimality are suitable for dynamic programming solutions i.e. the subsolutions of an optimal solution of the problem are themselves optimal solutions for their subproblems Since exponential time is unacceptable for all but the smallest problems, dynamic programming is sometimes essential 17 Spring 2014 CSE373: Data Structures & Algorithms

Algorithm Design Techniques Greedy Shortest path, minimum spanning tree, Divide and Conquer Divide the problem into smaller subproblems, solve them, and combine into the overall solution Often done recursively Quick sort, merge sort are great examples Dynamic Programming Brute force through all possible solutions, storing solutions to subproblems to avoid repeat computation Backtracking A clever form of exhaustive search 18

Backtracking: Idea Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating possibilities. A standard example of backtracking would be going through a maze. At some point, you might have two options of which direction to go: Portion A Junction Portion B 19

Backtracking One strategy would be to try going through Portion A of the maze. If you get stuck before you find your way out, then you "backtrack" to the junction. At this point in time you know that Portion A will NOT lead you out of the maze, so you then start searching in Portion B Portion A Portion B 20

Backtracking Clearly, at a single junction you could have even more than 2 choices. The backtracking strategy says to try each choice, one after the other, if you ever get stuck, "backtrack" to the junction and try the next choice. If you try all choices and never found a way out, then there IS no solution to the maze. C A B 21

Backtracking (animation)? dead end dead end dead end start??? dead end dead end? success! 22 Spring 2014 CSE373: Data Structures & Algorithms

Backtracking Dealing with the maze: From your start point, you will iterate through each possible starting move. From there, you recursively move forward. If you ever get stuck, the recursion takes you back to where you were, and you try the next possible move. Make sure you don't try too many possibilities, Mark which locations in the maze have been visited already so that no location in the maze gets visited twice. (If a place has already been visited, there is no point in trying to reach the end of the maze from there again. 23

Backtracking The neat thing about coding up backtracking is that it can be done recursively, without having to do all the bookkeeping at once. Instead, the stack of recursive calls does most of the bookkeeping (i.e., keeps track of which locations we ve tried so far.) 24

Backtracking: The 8 queens problem Find an arrangement of 8 queens on a single chess board such that no two queens are attacking one another. In chess, queens can move all the way down any row, column or diagonal (so long as no pieces are in the way). Due to the first two restrictions, it's clear that each row and column of the board will have exactly one queen. 25

Backtracking The backtracking strategy is as follows: 1) Place a queen on the first available square in row 1. 2) Move onto the next row, placing a queen on the first available square there (that doesn't conflict with the previously placed queens). 3) Continue in this fashion until either: a) You have solved the problem, or b) You get stuck. When you get stuck, remove the queens that got you there, until you get to a row where there is another valid square to try. Q Q Q Q Q Continue Animated Example: http://www.hbmeyer.de/ backtrack/achtdamen/ eight.htm#up Q 26

Backtracking 8 queens Analysis Another possible brute-force algorithm is generate all possible permutations of the numbers 1 through 8 (there are 8! = 40,320), Use the elements of each permutation as possible positions in which to place a queen on each row. Reject those boards with diagonal attacking positions. The backtracking algorithm does a bit better constructs the search tree by considering one row of the board at a time, eliminating most non-solution board positions at a very early stage in their construction. because it rejects row and diagonal attacks even on incomplete boards, it examines only 15,720 possible queen placements. 15,720 is still a lot of possibilities to consider Sometimes we have no other choice but to do the best we can J 27

Algorithm Design Techniques Greedy Shortest path, minimum spanning tree, Divide and Conquer Divide the problem into smaller subproblems, solve them, and combine into the overall solution Often done recursively Quick sort, merge sort are great examples Dynamic Programming Brute force through all possible solutions, storing solutions to subproblems to avoid repeat computation Backtracking A clever form of exhaustive search 28