Analysis of Algorithms continued Recursion.  On Capstone Project? ◦ Automatic extension to Monday morning ◦ If a team member does not wish to join the.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Topic 16 Sorting Using ADTs to Implement Sorting Algorithms.
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, Java Version, Third Edition.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
HST 952 Computing for Biomedical Scientists Lecture 10.
Fundamentals of Python: From First Programs Through Data Structures
Chapter 9: Searching, Sorting, and Algorithm Analysis
HST 952 Computing for Biomedical Scientists Lecture 9.
CS50 SECTION: WEEK 3 Kenny Yu. Announcements  Watch Problem Set 3’s walkthrough online if you are having trouble.  Problem Set 1’s feedback have been.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
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.
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
CSSE221: Software Dev. Honors Day 13 Announcements Announcements Contractions throughout the night… Contractions throughout the night… Late day assignments.
Quicksort.
Efficiency of Algorithms Csci 107 Lecture 8. Last time –Data cleanup algorithms and analysis –  (1),  (n),  (n 2 ) Today –Binary search and analysis.
Quicksort
COMP 110 Introduction to Programming Mr. Joshua Stough.
Elementary Data Structures and Algorithms
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 1 Introduction Definition of Algorithm An algorithm is a finite sequence of precise instructions for performing a computation or for solving.
Slides modified by Erin Chambers Problem Solving and Algorithm Design, part 2.
Abstract Data Types (ADTs) Data Structures The Java Collections API
COMP s1 Computing 2 Complexity
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Efficiency of Algorithms Csci 107 Lecture 7. Last time –Data cleanup algorithms and analysis –  (1),  (n),  (n 2 ) Today –Binary search and analysis.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
CS 46B: Introduction to Data Structures July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
1 Joe Meehean.  Problem arrange comparable items in list into sorted order  Most sorting algorithms involve comparing item values  We assume items.
Data structures Abstract data types Java classes for Data structures and ADTs.
ACM/JETT Workshop - August 4-5, 2005 Using Visualization Tools To Teach Data Structures and Algorithms Java applets by Dr. R. Mukundan, University of Canterbury,
CSC 211 Data Structures Lecture 13
SortingBigOh ASFA AP Computer Science A. Big-O refers to the order of an algorithm runtime growth in relation to the number of items I. O(l) - constant.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
CS 61B Data Structures and Programming Methodology July 21, 2008 David Sun.
Searching CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Sorting.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Data-structure-palooza Checkout DataStructures from SVN.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Searching/Sorting. Searching Searching is the problem of Looking up a specific item within a collection of items. Searching is the problem of Looking.
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Introduction to Search Algorithms
Warmup What is an abstract class?
structures and their relationships." - Linus Torvalds
structures and their relationships." - Linus Torvalds
Presentation transcript:

Analysis of Algorithms continued Recursion

 On Capstone Project? ◦ Automatic extension to Monday morning ◦ If a team member does not wish to join the team in its extension- decision, see me to work it out ◦ Final reflection is open – do it when you are done with project!!!  On Exam 2? ◦ Complete by now unless you have made/make arrangements with me  On grading of Exam 1: ◦ Earn back points! ◦ Fix FIXME’s (but keep FIXME in comment) and recommit. ◦ Complete before the final exam.  Final exam: ◦ Take it either (your choice):  Tuesday 1 p.m. in F-231 (CSSE conference room), or  Friday 1 p.m. in G-313 or G-315 (your choice) ◦ Open everything, HALF paper and pencil, about 90 minutes ◦ Covers last few days  Questions on anything else?

 Algorithm analysis, review  Recursion, review  Recursion, making it efficient  Data structures, how to choose  Implementation of Linked Lists  Work on Capstone

 Formal: ◦ We say that f(n) is O(g(n)) if and only if ◦ there exist constants c and n 0 such that ◦ for every n ≥ n 0 we have ◦ f(n) ≤ c × g(n)  Informal: ◦ f(n) is roughly proportional to g(n), for large n

 Factorial:  Ackermann function: Base Case Recursive step Q4

 Always have a base case that doesn’t recurse  Make sure recursive case always makes progress, by solving a smaller problem  You gotta believe ◦ Trust in the recursive solution ◦ Just consider one step at a time

 Describe basic searching & sorting algorithms: ◦ Search  Linear search of an UNsorted array  Linear seach of a sorted array (silly, but good example)  Binary search of a sorted array ◦ Sort  Selection sort  Insertion sort  Merge sort  Determine the best and worst case inputs for each  Derive the run-time efficiency of each, for best and worst-case

 For an unsorted / unorganized array: ◦ Linear search is as good as anything:  Go through the elements of the array, one by one  Quit when you find the element (best-case = early) or you get to the end of the array (worst-case) ◦ We’ll see mapping techniques for unsorted but organized data ◦ Best-case: O(1) ◦ Worst-case: O(n)

 For a sorted array: ◦ Linear search of a SORTED array:  Go through the elements starting at the beginning  Stop when either:  You find the sought-for number, or  You get past where the sought-for number would be ◦ But binary search (next slide) is MUCH better ◦ Best-case: O(1) ◦ Worst-case: O(n)

search(Comparable[] a, int start, int stop, Comparable sought) { if (start > stop) { return NOT_FOUND; } int middle = (left + right) / 2; int comparison = a[middle].compareTo(sought); if (comparison == 0) { return middle; } else if (comparison > 0) { return search(a, 0, middle – 1, sought); } else { return search(a, middle + 1, stop, sought); } Best-case: O(1) Worst-case: O(log n)

 Basic idea: ◦ Think of the list as having a sorted part (at the beginning) and an unsorted part (the rest) ◦ Find the smallest number in the unsorted part ◦ Exchange it with the element at the beginning of the unsorted part (making the sorted part bigger and the unsorted part smaller) Repeat until unsorted part is empty Best-case: O(n 2 ) Worst-case: O(n 2 )

 Basic idea: ◦ Think of the list as having a sorted part (at the beginning) and an unsorted part (the rest) ◦ Get the first number in the unsorted part ◦ Insert it into the correct location in the sorted part, moving larger values up in the array to make room Repeat until unsorted part is empty Best-case: O(n) Worst-case: O(n 2 )

 Basic recursive idea: ◦ If list is length 0 or 1, then it’s already sorted ◦ Otherwise:  Divide list into two halves  Recursively sort the two halves  Merge the sorted halves back together  Analysis: use tree-based sketch… Best-case: O(n log n) Worst-case: O(n log n)

 Algorithm analysis, review  Recursion, review  Recursion, making it efficient  Data structures, how to choose  Implementation of Linked Lists  Work on Capstone

 Why does recursive Fibonacci take so long?!? ◦ Answer: it recomputes subproblems repeatedly: O(2 n )  Can we fix it? Yes! Just: 1.“Memorize” every solution we find to subproblems, and 2.Before you recursively compute a solution to a subproblem, look it up in the “memory table” to see if you have already computed it This is a classic time-space tradeoff A deep discovery of computer science Tune the solution by varying the amount of storage space used and the amount of computation performed Studied by “Complexity Theorists” Used everyday by software engineers A more careful analysis yields a smaller base but it is still exponential.

 Algorithm analysis, review  Recursion, review  Recursion, making it efficient  Data structures, how to choose  Implementation of Linked Lists  Work on Capstone

Understanding the engineering trade-offs when storing data

 Efficient ways to store data based on how we’ll use it  So far we’ve seen ArrayList s ◦ Fast addition to end of list ◦ Fast access to any existing position ◦ Slow inserts to and deletes from middle of list

 What if we have to add/remove data from a list frequently?  LinkedList s support this: ◦ Fast insertion and removal of elements  Once we know where they go ◦ Slow access to arbitrary elements “random access”

 void addFirst(E element)  void addLast(E element)  E getFirst()  E getLast()  E removeFirst()  E removeLast()  What about accessing the middle of the list? ◦ LinkedList implements Iterable

Enhanced For LoopWhat Compiler Generates for (String s : list) { // do something } Iterator iter = list.iterator(); while (iter.hasNext()) { String s = iter.next(); // do something }

 A simplified version, with just the essentials  Won’t implement the java.util.List interface  Will have the usual linked list behavior ◦ Fast insertion and removal of elements  Once we know where they go ◦ Slow random access

 Boil down data types (e.g., lists) to their essential operations  Choosing a data structure for a project then becomes: ◦ Identify the operations needed ◦ Identify the abstract data type that most efficient supports those operations  Goal: that you understand several basic abstract data types and when to use them

 Array List  Linked List  Stack  Queue  Set  Map Implementations for all of these are provided by the Java Collections Framework in the java.util package.

Operations Provided Array List Efficiency Linked List Efficiency Random accessO(1)O(n) Add/remove itemO(n) (do you see why?) O(1) if you are “at” the item Q1,2

 A last-in, first-out (LIFO) data structure  Real-world stacks ◦ Plate dispensers in the cafeteria ◦ Pancakes!  Some uses: ◦ Tracking paths through a maze ◦ Providing “unlimited undo” in an application Operations Provided Efficiency Push itemO(1) Pop itemO(1) Implemented by Stack, LinkedList, and ArrayDeque in Java Q3

 A first-in, first-out (FIFO) data structure  Real-world queues ◦ Waiting line at the BMV ◦ Character on Star Trek TNG  Some uses: ◦ Scheduling access to shared resource (e.g., printer) Operations Provided Efficiency Enqueue itemO(1) Dequeue itemO(1) Implemented by LinkedList and ArrayDeque in Java Q4

 Unordered collections without duplicates  Real-world sets ◦ Students ◦ Collectibles  Some uses: ◦ Quickly checking if an item is in a collection OperationsHashSetTreeSet Add/remove itemO(1)O(lg n) Contains?O(1)O(lg n) Can hog space Sorts items! Q5

 Associate keys with values  Real-world “maps” ◦ Dictionary ◦ Phone book  Some uses: ◦ Associating student ID with transcript ◦ Associating name with high scores OperationsHashMapTreeMap Insert key-value pairO(1)O(lg n) Look up value for keyO(1)O(lg n) Can hog space Sorts items by key! Q6

 Algorithm analysis, review  Recursion, review  Recursion, making it efficient  Data structures, how to choose  Implementation of Linked Lists – part of your final exam!  Work on Capstone