Searching Algorithms/Time Analysis
|
|
- Barnaby Wilson
- 6 years ago
- Views:
Transcription
1 Searching Algorithms/Time Analysis CSE21 Fall 2017, Day 8 Oct 16,
2 (MinSort) loop invariant induction Loop invariant: After the t th iteration of the loop, the first t elements are: 1. The smallest t elements of the original list. 2. In sorted order. Inductive Hypothesis: Suppose that for some t 0, the loop invariant is true after t iterations. Show that it is true after t + 1 iterations:
3 (MinSort) loop invariant induction Loop invariant: After the t th iteration of the loop, the first t elements are: 1. The smallest t elements of the original list. 2. In sorted order. Inductive Hypothesis: Suppose that for some t 0, the loop invariant is true after t iterations. Show that it is true after t + 1 iterations: During the (t + 1)st iteration, the algorithm sets a m to be the minimum value of a t+1,, a n. Swap a m and a t+1. So, after the (t + 1)st iteration, a t+1 is the minimum value of a t+1,, a n.
4 (MinSort) loop invariant induction Show that it is true after t + 1 iterations: During the (t + 1)st iteration, the algorithm sets a m to be the minimum value of a t+1,, a n. Swap a m and a t+1.so, after the (t + 1)st iteration, a t+1 is the minimum value of a t+1,, a n. By the inductive hypothesis, after t iterations, a 1,, a t are: 1. The smallest t elements of the original list. 2. In sorted order. So, we have that a 1,, a t are all less than a t+1 and a t+1 is less than all of a t+1,, a n : 1. a 1,, a t+1 are the smallest t + 1 elements of the original list. Since a 1,, a t are in sorted order and a 1,, a t are all less than a t+1 : 2. a 1,, a t+1 are in sorted order. Therefore: for any 0 t n, the loop invariant is true after t iterations.
5 (MinSort) loop invariant conclusion Therefore: for any 0 t n, the loop invariant is true after t iterations. In particular: the loop invariant is true after n iterations and so after the algorithm terminates, the first n elements are: 1. The smallest n elements 2. In sorted order. This is exactly the algorithm specification!!!!
6 2 Proving Loop Invariants Induction variable (k): the number of times through the loop. Base case: Need to show the statement holds for k=0, before the loop Inductive step: Let k be a positive integer. Need help? See website for extra practice problems on Induction hypothesis: Suppose the statement invariants and holds induction. after k-1 times through the loop. Need to show that the statement holds after k times through the loop.
7 Why sort? A TA facing a stack of exams needs to input all 400 scores into a spreadsheet where the students are listed in alphabetical order. OR You want to find all the duplicate values in a long list.
8 Sorting helps with searching Two searching algorithms: One that works for any data, sorted or not One that is much faster, but relies on the data being sorted
9 The search problem Given an array A[1], A[2], A[3],..., A[n] and a target value x, find an index j where x = A[j] or determine that no such index exists because x is not in the array; Return j=0 if no index exists
10 Example Suppose our array is A[1]=2, A[2]=5, A[3]=0, A[4]=1. If x = 1, what j would the search problem find? A) j=2 B) j=4 C) j=0 D) no such j exists
11 Searching an unsorted array If you needed to find a DVD on this shelf, where the DVDs are in no particular order, how would you proceed?
12 Linear Search Search through the array one index at a time, asking at each position. ''Is this my target value?'' If you answer yes, return the position where you found it.
13 Searching: Specification WHAT Rosen page 194 Given a list a 1, a 2,..., a n and a target value x find an index j where x=a j or determine that there is no such index. Values can be any type. For simplicity, use integers.
14 HOW reverse engineering Pseudocode: procedure mystery (x: integer, a 1, a 2,..., a n : distinct integers ) i := 1 while (i <= n and x a i ) i := i+1 if i <=n then location := i else location := 0 return location { location is the subscript of the term that equals x, or is 0 if x is not found } In groups, describe in English (high-level), what the algorithm is doing.
15 Correctness of Linear Search: WHY What's the loop invariant? Try to write it down yourself, first.
16 Proof of correctness Why is this algorithm correct? Easy case: if the algorithm returns some i in the range 1 through n, it must be because x= A[i] Remaining case:?
17 Invariant for main loop Invariant: If after the loop, i=k, then????
18 Invariant for main loop Invariant: If after the loop, i=k, then x is not among A[1],,...A[k-1] Base case: k=1; x is not among the empty set Induction step. Assume i=k+1. Then in the previous iteration, i was k. Thus, x is not among the first k-1 elements of the array. In the current iteration, we compared x to A[k]. Since we incremented i, x was also not A[k].
19 If Linear Search returns 0 Linear search only returns 0 if i = n+1 Then the invariant says x is not in the first n positions of the array But this means x is not in the array, so 0 is the correct answer
20 How fast is linear search? Suppose you have an unsorted array and you are searching for a target value x that you know is in the array somewhere. What position for x will cause linear search to take the longest amount of time? A) x is the first element of the array B) x is the last element of the array C) x is somewhere in the middle of the array
21 How fast is linear search? Suppose you have an unsorted array and you are searching for a target value x that you know is in the array somewhere. What position for x will cause linear search to take the longest amount of time? A) x is the first element of the array B) x is the last element of the array C) x is somewhere in the middle of the array
22 How fast is linear search? Say our array A has n elements and we are searching for x. The time it takes to find x (or determine it is not present) depends on the number of probes, that is the number of entries we have to retrieve and compare to x.
23 How many probes? Worst-case scenario: Best-case scenario: On average:
24 How many probes? Worst-case scenario: The target is the last element in the array or not present at all. n probes Best-case scenario: The target is the first element in the array. 1 probe On average: We'd expect to have to search about half of the array until we find the target. about n/2 probes
25 Searching a sorted array How would you search through a pile of alphabetized papers to find the one with your name on it?
26 Using order in search Suppose our list is sorted in increasing order. If we probe the list at position m, what do we learn in each of these three cases? x = a m x < a m A.if x is in the list, it occurs BEFORE position m B.if x is in the list, it occurs AFTER position m C. x occurs at position m D. x is not in the list x > a m
27 How does having a sorted array help us? Suppose we probe A at position i. What do we learn? If x=a[m], we are done. If x<a[m], then if x is in the array, it is in position p for p<m. m-1 positions remain to be checked If x>a[m], then if x is in the array, it is in position p for p>m. n-m positions remain to be checked
28 How do we choose where to probe? If our array has 100 elements and we probe at position 5, it is most likely that the target is in the second half, so we'll have to search 95 entries in the worst case. How do we make the worst case as favorable as possible?
29 How do we choose where to probe? If our array has 100 elements and we probe at position 5, it is most likely that the target is in the second half, so we'll have to search 95 entries in the worst case. How do we make the worst case as favorable as possible? Probe in the middle. If we are searching the subarray A[i],, A[j], probe at index i + j 2
30 Binary Search: The Approach Probe in the middle of the array. Based on what you find there, determine which half to search in next. Continue until the target is found or you can be sure the target is not in the array. (When can you be sure the target is not in the array?) Return the location of the target, or 0 if it's not in the array
31 Binary Search: HOW Starting at the middle of the list and based on what you find, determine which half to search next. Continue until target is found or we are sure that it isn t there. procedure binary search (x: integer, a 1, a 2,..., a n : increasing integers ) { location is the subscript of the term that equals x, or is 0 if x is not found }
32 Binary Search: HOW Starting at the middle of the list and based on what you find, determine which half to search next. Continue until target is found or we are sure that it isn t there. procedure binary search (x: integer, a 1, a 2,..., a n : increasing integers ) i := 1 j := n Location:=0 while i<j A. i<n B. j<n C. i<j D. j<i E.None of the above m := floor( (i+j)/2 ) if x = a m then location:= m if x > a m then i:=m+1 if x < a m then j:=m-1 return location { location is the subscript of the term that equals x, or is 0 if x is not found }
33 Binary Search: WHY procedure binary search (x: integer, a 1, a 2,..., a n : increasing integers ) i := 1 j := n Location:=0 while i<j m := floor( (i+j)/2 ) if x = a m then location:= m if x > a m then i:=m+1 if x < a m then j:=m-1 return location
34 Binary Search is Telling the Truth? (correctness) We must show two things: 1) If binary search returns some nonzero position p, then a p = x. 2) If binary search returns 0, then there is no position p for which a p = x.
35 1) If it returns nonzero p, then a p = x. procedure binary search (x: integer, a 1, a 2,..., a n : increasing integers ) i := 1 j := n Location:=0 while i<j m := floor( (i+j)/2 ) if x = a m then location:= m if x > a m then i:=m+1 if x < a m then j:=m-1 return location
36 What is the contrapositive of (2)? 2) If binary search returns 0, then there is no position p for which a p =x.
37 What is the contrapositive? 2) If binary search returns 0, then there is no position p for which a p =x. Contrapositive: If there is a position p for which a p =x, then binary search doesn't return 0.
38 Loop Invariant Want to show: If there is a position p for which a p =x, then binary search doesn't return 0. Loop invariant: Suppose there is a position p for which a p =x, then: i p j
39 Invariant: procedure binary search (x: integer, a 1, a 2,..., a n : increasing integers ) i := 1 j := n Location:=0 while i<j m := floor( (i+j)/2 ) if x = a m then location:= m if x > a m then i:=m+1 if x < a m then j:=m-1 return location
40 Invariant: Proof
41 Binary Search Terminates With each probe, you reduce the size of the subarray you are searching to at most half of its previous size. Number of probes Max size of subarray 1 n/2 2 n/4 3 n/8 4 n/
42 Binary Search Terminates With each probe, you reduce the size of the subarray you are searching to at most half of its previous size. Number of probes Max size of subarray 1 n/2 2 n/4 3 n/8 4 n/ w
43 Binary Search Terminates How many probes w do we need to make sure our search has terminated? That is, what value of w is sure to make the subarray size less than 1? n < 1 2 w n < 2 w log n < w The smallest integer w that is sure to be greater than log(n) is w = log n + 1
44 Comparison with Linear Search Therefore, we have shown that it takes at most log n + 1 probes for binary search to terminate. This is the worst case. By comparison, linear search takes as many as n probes.even in the average case, linear search takes about n/2 probes.
45 The cost of binary search While binary search is faster than linear search, it depends upon your array being sorted. What if you have an unsorted array you need to search? Should you sort it first so you can use the better search?
46 If you need to search the same array many times, it becomes more worthwhile to invest the initial time in sorting your array. The cost of binary search There's a tradeoff between the cost of sorting an array and the benefit of being able to search faster.
47 General questions to ask about algorithms 1) What problem are we solving? SPECIFICATION 2) How do we solve the problem? ALGORITHM DESCRIPTION 3) Why do these steps solve the problem? CORRECTNESS 4) When do we get an answer? RUNNING TIME PERFORMANCE
48 Counting comparisons: WHEN Measure Time Number of operations Comparisons of list elements! For selection sort (MinSort), how many times do we have to compare the values of some pair of list elements?
49 Selection Sort (MinSort) Pseudocode Rosen page 203, exercises procedure selection sort(a 1, a 2,..., a n : real numbers with n >=2 ) for i := 1 to n-1 m := i for j:= i+1 to n if ( a j < a m ) then m := j interchange a i and a m { a 1,..., a n is in increasing order} How many times do we compare pairs of elements? A. n B. log n C. n! n 2 D. E. n 2
50 Selection Sort (MinSort) Pseudocode Rosen page 203, exercises procedure selection sort(a 1, a 2,..., a n : real numbers with n >=2 ) for i := 1 to n-1 m := i for j:= i+1 to n if ( a j < a m ) then m := j interchange a i and a m { a 1,..., a n is in increasing order} In the body of the outer loop, when i=1, how many times do we compare pairs of elements? A. 1 B. n C. n-1 D. n+1 E. None of the above.
51 Selection Sort (MinSort) Pseudocode Rosen page 203, exercises procedure selection sort(a 1, a 2,..., a n : real numbers with n >=2 ) for i := 1 to n-1 m := i for j:= i+1 to n if ( a j < a m ) then m := j interchange a i and a m { a 1,..., a n is in increasing order} i = 1: n-1 comparisons i = 2: n-2 comparisons... i = n-1: 1 comparison Total number of comparisons: (n-1)
52 Sum of consecutive integers X = (n-3) + (n-2) + (n-1) X = (n-1) + (n-2) + (n-3) X = n + n + n + + n + n + n 2X = n(n-1) X = n(n-1)/2
53 Counting operations When do we get an answer? RUNNING TIME PERFORMANCE Counting number of times list elements are compared
54 Runtime performance Algorithm: problem solving strategy as a sequence of steps Examples of steps - Comparing list elements (which is larger?) - Accessing a position in a list (probe for value) - Arithmetic operation (+, -, *, ) - etc. "Single step" depends on context
55 Runtime performance How long does a single step take? Some factors - Hardware - Software Let s discuss and list factors that could impact how long a single step takes
56 Runtime performance How long does a single step take? Some factors - Hardware (CPU, climate (!), cache ) - Software (programming language, compiler)
57 Runtime performance The time our program takes will depend on Input size Number of steps the algorithm requires Time for each of these steps on our system
58 Runtime performance Ignore what we can't control Goal: Estimate time as a function of the size of the input, n Focus on how time scales for large inputs
59 Ignore what we can't control Rate of growth Focus on how time scales for large inputs
60 Selection Sort (MinSort) Performance (Again) Rosen page 210, example 5 Number of comparisons of list elements (n-1) + (n-2) + + (1) = n(n-1)/2 Sum of positive integers up to (n-1) Rewrite this expression using big-o notation: A. O(n) B. O(n(n-1)) C. O(n 2 ) D. O(1/2) E. None of the above
61 Exercise: Why is n = n 2?????
62 Linear Search: HOW Starting at the beginning of the list, compare items one by one with x until find it or reach the end procedure linear search (x: integer, a 1, a 2,..., a n : distinct integers ) i := 1 while (i <= n and x a i ) i := i+1 if i <=n then location := i else location := 0 return location { location is the subscript of the term that equals x, or is 0 if x is not found }
63 How fast is Linear Search: WHEN Rosen page 220, part of example 2 The time it takes to find x (or determine it is not present) depends on the number of probes, that is the number of list entries we have to retrieve and compare to x How many probes do we make when doing Linear Search on a list of size n if x happens to equal the first element in the list? if x happens to equal the last element in the list? if x happens to equal an element somewhere in the middle of the list? if x doesn't equal any element in the list?
64 How fast is Linear Search: WHEN Best case: 1 probe target appears first Worst case: n probes target appears last or not at all Average case: n/2 probes target appears in the middle, Running time (expect to have to search about depends on more than size array... of more the input! on half of the expected value later in the Rosen p. 220
65 Binary Search: WHEN procedure binary search (x: integer, a 1, a 2,..., a n : increasing integers ) i := 1 j := n while i<j m := floor( (i+j)/2 ) if x > a m then i := m+1 else j := m if x=a i else location := 0 return location then location := i Each time divide size of "list in play" by 2 { location is the subscript of the term that equals x, or is 0 if x is not found }
66 Rosen page 220, example 3 How fast is Binary Search: WHEN Number of comparisons (probes) depends on number of iterations of loop, hence size of set. After iterations of loop (Max) size of list "in play" 0 n 1 n/2 2 n/4 3 n/8?? 1
67 Rosen page 220, example 3 How fast is Binary Search: WHEN Number of comparisons (probes) depends on number of iterations of loop, hence size of set. After iterations of loop (Max) size of list "in play" 0 n 1 n/2 2 n/4 3 n/8 ceil(log 2 n) 1 Rewrite this formula in order notation: A. B. C. D. E. None of the above
68 Comparing linear search and binary search Rosen pages Linear Search Binary Search Assumptions None Sorted list # probes in * best case or * worst case * average case?? Best case analysis depends on whether we check if midpoint agrees with target right away or wait until list size gets to 1 (as in Rosen)
69 Comparing linear search and binary search Rosen pages Linear Search Binary Search Assumptions None Sorted list # probes in * best case or * worst case * average case??
70 Comparing linear search and binary search Rosen pages Linear Search Binary Search Assumptions None Sorted list # probes in * best case or * worst case * average case?? Is it worth it to sort our list first?
Time Analysis of Sorting and Searching Algorithms
Time Analysis of Sorting and Searching Algorithms CSE21 Winter 2017, Day 5 (B00), Day 3 (A00) January 20, 2017 http://vlsicad.ucsd.edu/courses/cse21-w17 Binary Search: WHEN procedure binary search (x:
More informationNotes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 3 Notes. Class URL:
Notes slides from before lecture CSE 21, Winter 2017, Section A00 Lecture 3 Notes Class URL: http://vlsicad.ucsd.edu/courses/cse21-w17/ Notes slides from before lecture Notes January 18 (1) HW2 has been
More informationSorting Algorithms. CSE21 Winter 2017, Day 2 (B00), Day 1-2 (A00) January 11, 2017
Sorting Algorithms CSE21 Winter 2017, Day 2 (B00), Day 1-2 (A00) January 11, 2017 Sorting (or Ordering) Section 3.1 in Rosen vs. * Assume elements of the set to be sorted have some underlying order Why
More informationLecture 2: Getting Started
Lecture 2: Getting Started Insertion Sort Our first algorithm is Insertion Sort Solves the sorting problem Input: A sequence of n numbers a 1, a 2,..., a n. Output: A permutation (reordering) a 1, a 2,...,
More informationSorting & Growth of Functions
Sorting & Growth of Functions CSci 588: Data Structures, Algorithms and Software Design Introduction to Algorithms, Cormen et al., Chapter 3 All material not from online sources or text copyright Travis
More informationOutline. Computer Science 331. Three Classical Algorithms. The Sorting Problem. Classical Sorting Algorithms. Mike Jacobson. Description Analysis
Outline Computer Science 331 Classical Sorting Algorithms Mike Jacobson Department of Computer Science University of Calgary Lecture #22 1 Introduction 2 3 4 5 Comparisons Mike Jacobson (University of
More informationThe divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.
2.3 Designing algorithms There are many ways to design algorithms. Insertion sort uses an incremental approach: having sorted the subarray A[1 j - 1], we insert the single element A[j] into its proper
More information0.1 Welcome. 0.2 Insertion sort. Jessica Su (some portions copied from CLRS)
0.1 Welcome http://cs161.stanford.edu My contact info: Jessica Su, jtysu at stanford dot edu, office hours Monday 3-5 pm in Huang basement TA office hours: Monday, Tuesday, Wednesday 7-9 pm in Huang basement
More informationAdvanced Algorithms and Data Structures
Advanced Algorithms and Data Structures Prof. Tapio Elomaa Course Basics A new 7 credit unit course Replaces OHJ-2156 Analysis of Algorithms We take things a bit further than OHJ-2156 We will assume familiarity
More informationLecture Notes for Chapter 2: Getting Started
Instant download and all chapters Instructor's Manual Introduction To Algorithms 2nd Edition Thomas H. Cormen, Clara Lee, Erica Lin https://testbankdata.com/download/instructors-manual-introduction-algorithms-2ndedition-thomas-h-cormen-clara-lee-erica-lin/
More information(Refer Slide Time: 00:50)
Programming, Data Structures and Algorithms Prof. N.S. Narayanaswamy Department of Computer Science and Engineering Indian Institute of Technology Madras Module - 03 Lecture 30 Searching Unordered linear
More informationEECS 2011M: Fundamentals of Data Structures
M: Fundamentals of Data Structures Instructor: Suprakash Datta Office : LAS 3043 Course page: http://www.eecs.yorku.ca/course/2011m Also on Moodle Note: Some slides in this lecture are adopted from James
More informationBinomial Coefficients
Binomial Coefficients Russell Impagliazzo and Miles Jones Thanks to Janine Tiefenbruck http://cseweb.ucsd.edu/classes/sp16/cse21-bd/ May 6, 2016 Fixed-density Binary Strings How many length n binary strings
More informationAssignment 1. Stefano Guerra. October 11, The following observation follows directly from the definition of in order and pre order traversal:
Assignment 1 Stefano Guerra October 11, 2016 1 Problem 1 Describe a recursive algorithm to reconstruct an arbitrary binary tree, given its preorder and inorder node sequences as input. First, recall that
More informationComparison of x with an entry in the array
1. Basic operations in algorithm An algorithm to solve a particular task employs some set of basic operations. When we estimate the amount of work done by an algorithm we usually do not consider all the
More informationAnalyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:
CSE 101 Homework 1 Background (Order and Recurrence Relations), correctness proofs, time analysis, and speeding up algorithms with restructuring, preprocessing and data structures. Due Thursday, April
More informationBinary Search to find item in sorted array
Binary Search to find item in sorted array January 15, 2008 QUESTION: Suppose we are given a sorted list A[1..n] (as an array), of n real numbers: A[1] A[2] A[n]. Given a real number x, decide whether
More informationDefinition: A data structure is a way of organizing data in a computer so that it can be used efficiently.
The Science of Computing I Lesson 4: Introduction to Data Structures Living with Cyber Pillar: Data Structures The need for data structures The algorithms we design to solve problems rarely do so without
More informationCS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014
CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014 Study: Chapter 4 Analysis of Algorithms, Recursive Algorithms, and Recurrence Equations 1. Prove the
More informationPlotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1
CS241 - week 1 review Special classes of algorithms: logarithmic: O(log n) linear: O(n) quadratic: O(n 2 ) polynomial: O(n k ), k 1 exponential: O(a n ), a > 1 Classifying algorithms is generally done
More informationRandomized Algorithms, Hash Functions
Randomized Algorithms, Hash Functions Lecture A Tiefenbruck MWF 9-9:50am Center 212 Lecture B Jones MWF 2-2:50pm Center 214 Lecture C Tiefenbruck MWF 11-11:50am Center 212 http://cseweb.ucsd.edu/classes/wi16/cse21-abc/
More informationData Structure and Algorithm Homework #1 Due: 1:20pm, Tuesday, March 21, 2017 TA === Homework submission instructions ===
Data Structure and Algorithm Homework #1 Due: 1:20pm, Tuesday, March 21, 2017 TA email: dsa1@csie.ntu.edu.tw === Homework submission instructions === For Problem 1-3, please put all your solutions in a
More informationCSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )
CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16 In-Class Midterm ( 11:35 AM 12:50 PM : 75 Minutes ) This exam will account for either 15% or 30% of your overall grade depending on your
More informationBinomial Coefficient Identities and Encoding/Decoding
Binomial Coefficient Identities and Encoding/Decoding CSE21 Winter 2017, Day 18 (B00), Day 12 (A00) February 24, 2017 http://vlsicad.ucsd.edu/courses/cse21-w17 MT2 Review Sessions Today and Tomorrow! TODAY
More informationAlgorithms - Ch2 Sorting
Algorithms - Ch2 Sorting (courtesy of Prof.Pecelli with some changes from Prof. Daniels) 1/28/2015 91.404 - Algorithms 1 Algorithm Description Algorithm Description: -Pseudocode see conventions on p. 20-22
More informationAdvanced Algorithms and Data Structures
Advanced Algorithms and Data Structures Prof. Tapio Elomaa tapio.elomaa@tut.fi Course Prerequisites A seven credit unit course Replaced OHJ-2156 Analysis of Algorithms We take things a bit further than
More informationCS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics
CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics 1 Sorting 1.1 Problem Statement You are given a sequence of n numbers < a 1, a 2,..., a n >. You need to
More information1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall
CSE 20 https://goo.gl/forms/1o 1KOd17RMoURxjn2 DISCRETE MATH Fall 2017 http://cseweb.ucsd.edu/classes/fa17/cse20-ab/ Today's learning goals Explain the steps in a proof by mathematical and/or structural
More informationIntroduction to Algorithms
Introduction to Algorithms An algorithm is any well-defined computational procedure that takes some value or set of values as input, and produces some value or set of values as output. 1 Why study algorithms?
More informationData Structures and Algorithms CSE 465
Data Structures and Algorithms CSE 465 LECTURE 2 Analysis of Algorithms Insertion Sort Loop invariants Asymptotic analysis Sofya Raskhodnikova and Adam Smith The problem of sorting Input: sequence a 1,
More informationEncoding/Decoding. Russell Impagliazzo and Miles Jones Thanks to Janine Tiefenbruck. May 9, 2016
Encoding/Decoding Russell Impagliazzo and Miles Jones Thanks to Janine Tiefenbruck http://cseweb.ucsd.edu/classes/sp16/cse21-bd/ May 9, 2016 Review: Terminology A permutation of r elements from a set of
More informationDesign and Analysis of Algorithms
Design and Analysis of Algorithms CSE 5311 Lecture 8 Sorting in Linear Time Junzhou Huang, Ph.D. Department of Computer Science and Engineering CSE5311 Design and Analysis of Algorithms 1 Sorting So Far
More informationSolutions to the Second Midterm Exam
CS/Math 240: Intro to Discrete Math 3/27/2011 Instructor: Dieter van Melkebeek Solutions to the Second Midterm Exam Problem 1 This question deals with the following implementation of binary search. Function
More informationCSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)
Name: Email address: Quiz Section: CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators) Instructions: Read the directions for each question carefully before answering. We will
More informationData Structures and Algorithms Chapter 2
1 Data Structures and Algorithms Chapter 2 Werner Nutt 2 Acknowledgments The course follows the book Introduction to Algorithms, by Cormen, Leiserson, Rivest and Stein, MIT Press [CLRST]. Many examples
More informationData Structures and Algorithms. Part 2
1 Data Structures and Algorithms Part 2 Werner Nutt 2 Acknowledgments The course follows the book Introduction to Algorithms, by Cormen, Leiserson, Rivest and Stein, MIT Press [CLRST]. Many examples displayed
More informationMergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri
CS161, Lecture 2 MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri 1 Introduction Today, we will introduce a fundamental algorithm design paradigm,
More informationAnalysis of Algorithms. Unit 4 - Analysis of well known Algorithms
Analysis of Algorithms Unit 4 - Analysis of well known Algorithms 1 Analysis of well known Algorithms Brute Force Algorithms Greedy Algorithms Divide and Conquer Algorithms Decrease and Conquer Algorithms
More informationAnalyzing Algorithms
Analyzing Algorithms Insertion Sort 5 4 4 7 10 7 5 10 (a) (d) 1 3 4 5 6 5 4 6 1 3 1 3 4 5 6 4 5 6 1 3 (b) (e) 1 3 4 5 6 5 4 6 1 3 1 3 4 5 6 1 4 5 6 3 (c) (f) 1 3 4 5 6 4 5 6 1 3 1 3 4 5 6 1 3 4 5 6 Insertion
More information1. Suppose you are given a magic black box that somehow answers the following decision problem in polynomial time:
1. Suppose you are given a magic black box that somehow answers the following decision problem in polynomial time: Input: A CNF formula ϕ with n variables x 1, x 2,..., x n. Output: True if there is an
More information17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer
Module 2: Divide and Conquer Divide and Conquer Control Abstraction for Divide &Conquer 1 Recurrence equation for Divide and Conquer: If the size of problem p is n and the sizes of the k sub problems are
More informationLECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.
LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes. Input Algorithm Output An algorithm is a step-by-step procedure for
More informationLecture Notes on Quicksort
Lecture Notes on Quicksort 15-122: Principles of Imperative Computation Frank Pfenning Lecture 8 February 5, 2015 1 Introduction In this lecture we consider two related algorithms for sorting that achieve
More informationCS 137 Part 7. Big-Oh Notation, Linear Searching and Basic Sorting Algorithms. November 10th, 2017
CS 137 Part 7 Big-Oh Notation, Linear Searching and Basic Sorting Algorithms November 10th, 2017 Big-Oh Notation Up to this point, we ve been writing code without any consideration for optimization. There
More informationCSE 373 APRIL 3 RD ALGORITHM ANALYSIS
CSE 373 APRIL 3 RD ALGORITHM ANALYSIS ASSORTED MINUTIAE HW1P1 due tonight at midnight HW1P2 due Friday at midnight HW2 out tonight Second Java review session: Friday 10:30 ARC 147 TODAY S SCHEDULE Algorithm
More informationProblem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.
Problem 5. Sorting Simple Sorting, Quicksort, Mergesort Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all 1 i j n. 98 99 Selection Sort
More informationExample Algorithms. CSE 2320 Algorithms and Data Structures Alexandra Stefan. University of Texas at Arlington. Last updated 9/7/2016
Example Algorithms CSE 2320 Algorithms and Data Structures Alexandra Stefan University of Texas at Arlington Last updated 9/7/2016 1 Summary Binary Search See the notation conventions (e.g. log 2 N = lg
More informationLecture 15 : Review DRAFT
CS/Math 240: Introduction to Discrete Mathematics 3/10/2011 Lecture 15 : Review Instructor: Dieter van Melkebeek Scribe: Dalibor Zelený DRAFT Today slectureservesasareviewofthematerialthatwillappearonyoursecondmidtermexam.
More informationS1) It's another form of peak finder problem that we discussed in class, We exploit the idea used in binary search.
Q1) Given an array A which stores 0 and 1, such that each entry containing 0 appears before all those entries containing 1. In other words, it is like {0, 0, 0,..., 0, 0, 1, 1,..., 111}. Design an algorithm
More informationCOMP Analysis of Algorithms & Data Structures
COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Lecture 7 - Jan. 17, 2018 CLRS 7.1, 7-4, 9.1, 9.3 University of Manitoba COMP 3170 - Analysis of Algorithms & Data Structures 1 / 11 QuickSelect
More information1 Dynamic Programming
Recitation 13 Dynamic Programming Parallel and Sequential Data Structures and Algorithms, 15-210 (Fall 2013) November 20, 2013 1 Dynamic Programming Dynamic programming is a technique to avoid needless
More informationRandomized Algorithms: Element Distinctness
Randomized Algorithms: Element Distinctness CSE21 Winter 2017, Day 24 (B00), Day 16-17 (A00) March 13, 2017 http://vlsicad.ucsd.edu/courses/cse21-w17 Element Distinctness: WHAT Given list of positive integers
More informationTJ IOI 2017 Written Round Solutions. Thomas Jefferson High School for Science and Technology
TJ IOI 017 Written Round Solutions Thomas Jefferson High School for Science and Technology Saturday, January 13, 018 Contents 1 Introduction 1 Bead sort 1 3 Elementary sorts 3.1 Selection sort.............................................
More informationTotal Points: 60. Duration: 1hr
CS800 : Algorithms Fall 201 Nov 22, 201 Quiz 2 Practice Total Points: 0. Duration: 1hr 1. (,10) points Binary Heap. (a) The following is a sequence of elements presented to you (in order from left to right):
More informationAssignment 1 (concept): Solutions
CS10b Data Structures and Algorithms Due: Thursday, January 0th Assignment 1 (concept): Solutions Note, throughout Exercises 1 to 4, n denotes the input size of a problem. 1. (10%) Rank the following functions
More informationPractice Sheet 2 Solutions
Practice Sheet 2 Solutions 28th August, 2016 Dynamic Programming 1. Question: Given an array of n positive integers a 1,, a n, give an algorithm to find the length of the longest subsequence a i1,, a ik
More informationAssertions & Verification & Example Loop Invariants Example Exam Questions
2014 November 27 1. Assertions & Verification & Example Loop Invariants Example Exam Questions 2. A B C Give a general template for refining an operation into a sequence and state what questions a designer
More informationCS 561, Lecture 1. Jared Saia University of New Mexico
CS 561, Lecture 1 Jared Saia University of New Mexico Quicksort Based on divide and conquer strategy Worst case is Θ(n 2 ) Expected running time is Θ(n log n) An In-place sorting algorithm Almost always
More informationUNIT 5B Binary Search
205/09/30 UNIT 5B Binary Search Course Announcements Written exam next week (Wed. Oct 7 ) Practice exam available on the Resources page Exam reviews: Sunday afternoon; watch Piazza for times and places
More informationFundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm
cmpt-225 Sorting Sorting Fundamental problem in computing science putting a collection of items in order Often used as part of another algorithm e.g. sort a list, then do many binary searches e.g. looking
More informationHow many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.
Chapter 8. Sorting in Linear Time Types of Sort Algorithms The only operation that may be used to gain order information about a sequence is comparison of pairs of elements. Quick Sort -- comparison-based
More informationCS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total
CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Please check your tutorial (TUT) section from the list below: TUT 101: F 11:30, MC 4042 TUT 102: M 10:30, MC 4042 TUT 103: M 11:30, MC 4058 TUT 104: F 10:30,
More informationFor searching and sorting algorithms, this is particularly dependent on the number of data elements.
Looking up a phone number, accessing a website and checking the definition of a word in a dictionary all involve searching large amounts of data. Searching algorithms all accomplish the same goal finding
More informationCS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017
CS 137 Part 8 Merge Sort, Quick Sort, Binary Search November 20th, 2017 This Week We re going to see two more complicated sorting algorithms that will be our first introduction to O(n log n) sorting algorithms.
More informationDATA STRUCTURES/UNIT 3
UNIT III SORTING AND SEARCHING 9 General Background Exchange sorts Selection and Tree Sorting Insertion Sorts Merge and Radix Sorts Basic Search Techniques Tree Searching General Search Trees- Hashing.
More informationThe divide and conquer strategy has three basic parts. For a given problem of size n,
1 Divide & Conquer One strategy for designing efficient algorithms is the divide and conquer approach, which is also called, more simply, a recursive approach. The analysis of recursive algorithms often
More informationData Structures and Algorithms Key to Homework 1
Data Structures and Algorithms Key to Homework 1 January 31, 2005 15 Define an ADT for a set of integers (remember that a set may not contain duplicates) Your ADT should consist of the functions that can
More informationChapter 2: Complexity Analysis
Chapter 2: Complexity Analysis Objectives Looking ahead in this chapter, we ll consider: Computational and Asymptotic Complexity Big-O Notation Properties of the Big-O Notation Ω and Θ Notations Possible
More informationExam Datastrukturer. DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE
Exam Datastrukturer DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE Day: 2018-10-12, Time: 8:30-12.30, Place: SB Course responsible Alex Gerdes, tel. 031-772 6154. Will visit at around 9:30 and 11:00.
More informationCSE 21 Spring 2016 Homework 5. Instructions
CSE 21 Spring 2016 Homework 5 Instructions Homework should be done in groups of one to three people. You are free to change group members at any time throughout the quarter. Problems should be solved together,
More informationCSE 20 DISCRETE MATH. Fall
CSE 20 DISCRETE MATH Fall 2017 http://cseweb.ucsd.edu/classes/fa17/cse20-ab/ Final exam The final exam is Saturday December 16 11:30am-2:30pm. Lecture A will take the exam in Lecture B will take the exam
More informationCHENNAI MATHEMATICAL INSTITUTE M.Sc. / Ph.D. Programme in Computer Science
CHENNAI MATHEMATICAL INSTITUTE M.Sc. / Ph.D. Programme in Computer Science Entrance Examination, 5 May 23 This question paper has 4 printed sides. Part A has questions of 3 marks each. Part B has 7 questions
More informationSorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?
Sorting Binary search works great, but how do we create a sorted array in the first place? Sorting in Arrays Sorting algorithms: Selection sort: O(n 2 ) time Merge sort: O(nlog 2 (n)) time Quicksort: O(n
More informationAlgorithms. Notations/pseudo-codes vs programs Algorithms for simple problems Analysis of algorithms
Algorithms Notations/pseudo-codes vs programs Algorithms for simple problems Analysis of algorithms Is it correct? Loop invariants Is it good? Efficiency Is there a better algorithm? Lower bounds * DISC
More information1 Dynamic Programming
Recitation 13 Dynamic Programming Parallel and Sequential Data Structures and Algorithms, 15-210 (Spring 2013) April 17, 2013 1 Dynamic Programming Dynamic programming is a technique to avoid needless
More informationCS 173 [A]: Discrete Structures, Fall 2012 Homework 8 Solutions
CS 173 [A]: Discrete Structures, Fall 01 Homework 8 Solutions This homework contains 4 problems worth a total of 35 points. It is due on Wednesday, November 14th, at 5pm. 1 Induction Proofs With Inequalities
More informationCSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)
_ UWNetID: Lecture Section: A CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators) Instructions: Read the directions for each question carefully before answering. We will give
More informationOrder Analysis of Algorithms. Sorting problem
Order Analysis of Algorithms Debdeep Mukhopadhyay IIT Kharagpur Sorting problem Input: A sequence of n numbers, a1,a2,,an Output: A permutation (reordering) (a1,a2,,an ) of the input sequence such that
More information08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58
08 A: Sorting III CS1102S: Data Structures and Algorithms Martin Henz March 10, 2010 Generated on Tuesday 9 th March, 2010, 09:58 CS1102S: Data Structures and Algorithms 08 A: Sorting III 1 1 Recap: Sorting
More informationClassic Data Structures Introduction UNIT I
ALGORITHM SPECIFICATION An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. All algorithms must satisfy the following criteria: Input. An algorithm has zero
More informationCSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)
Name: Email address: Quiz Section: CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators) Instructions: Read the directions for each question carefully before answering. We will
More informationLecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning
Lecture 7 Quicksort 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning In this lecture we consider two related algorithms for sorting that achieve a much better running time than
More informationCIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018
CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018 Q1: Prove or disprove: You are given a connected undirected graph G = (V, E) with a weight function w defined over its
More informationNotes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 10 Notes. Class URL:
Notes slides from before lecture CSE 21, Winter 2017, Section A00 Lecture 10 Notes Class URL: http://vlsicad.ucsd.edu/courses/cse21-w17/ Notes slides from before lecture Notes February 13 (1) HW5 is due
More informationOperations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.
Priority Queue, Heap and Heap Sort In this time, we will study Priority queue, heap and heap sort. Heap is a data structure, which permits one to insert elements into a set and also to find the largest
More informationSearching Elements in an Array: Linear and Binary Search. Spring Semester 2007 Programming and Data Structure 1
Searching Elements in an Array: Linear and Binary Search Spring Semester 2007 Programming and Data Structure 1 Searching Check if a given element (called key) occurs in the array. Example: array of student
More informationFundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.
Fundamental mathematical techniques reviewed: Mathematical induction Recursion Typically taught in courses such as Calculus and Discrete Mathematics. Techniques introduced: Divide-and-Conquer Algorithms
More informationExam Data structures DAT036/DAT037/DIT960
Exam Data structures DAT036/DAT037/DIT960 Time Thursday 20 th August 2015, 14:00 18:00 Place Maskinhuset Course responsible Nick Smallbone, tel. 0707 183062 The exam consists of six questions. For a 3
More informationCSE 21 Spring 2016 Homework 4 Answer Key
1 CSE 21 Spring 2016 Homework 4 Answer Key 1. In each situation, write a recurrence relation, including base case(s), that describes the recursive structure of the problem. You do not need to solve the
More informationSection 1: True / False (2 points each, 30 pts total)
Section 1: True / False (2 points each, 30 pts total) Circle the word TRUE or the word FALSE. If neither is circled, both are circled, or it impossible to tell which is circled, your answer will be considered
More informationAssertions & Verification Example Exam Questions
2009 November 23 Assertions & Verification Example Exam Questions 1. 2. A B C Give a general template for refining an operation into a sequence and state what questions a designer must answer to verify
More informationCPSC 311: Analysis of Algorithms (Honors) Exam 1 October 11, 2002
CPSC 311: Analysis of Algorithms (Honors) Exam 1 October 11, 2002 Name: Instructions: 1. This is a closed book exam. Do not use any notes or books, other than your 8.5-by-11 inch review sheet. Do not confer
More information(D) There is a constant value n 0 1 such that B is faster than A for every input of size. n n 0.
Part : Multiple Choice Enter your answers on the Scantron sheet. We will not mark answers that have been entered on this sheet. Each multiple choice question is worth. marks. Note. when you are asked to
More informationCS2223: Algorithms D- Term, Homework I. Teams: To be done individually. Due date: 03/27/2015 (1:50 PM) Submission: Electronic submission only
CS2223: Algorithms D- Term, 2015 Homework I Teams: To be done individually Due date: 03/27/2015 (1:50 PM) Submission: Electronic submission only 1 General Instructions Python Code vs. Pseudocode: Each
More informationSorting. There exist sorting algorithms which have shown to be more efficient in practice.
Sorting Next to storing and retrieving data, sorting of data is one of the more common algorithmic tasks, with many different ways to perform it. Whenever we perform a web search and/or view statistics
More informationSearching, Sorting. Arizona State University 1
Searching, Sorting CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 9 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State
More informationMath 501 Solutions to Homework Assignment 10
Department of Mathematics Discrete Mathematics Math 501 Solutions to Homework Assignment 10 Section 7.3 Additional Exercises 1. int CountValuesLessThanT(int a[], int n, int T) { int count = 0; for (int
More informationUniversity of Waterloo CS240 Winter 2018 Assignment 2. Due Date: Wednesday, Jan. 31st (Part 1) resp. Feb. 7th (Part 2), at 5pm
University of Waterloo CS240 Winter 2018 Assignment 2 version: 2018-02-04 15:38 Due Date: Wednesday, Jan. 31st (Part 1) resp. Feb. 7th (Part 2), at 5pm Please read the guidelines on submissions: http://www.student.cs.uwaterloo.ca/~cs240/
More informationPROGRAM EFFICIENCY & COMPLEXITY ANALYSIS
Lecture 03-04 PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS By: Dr. Zahoor Jan 1 ALGORITHM DEFINITION A finite set of statements that guarantees an optimal solution in finite interval of time 2 GOOD ALGORITHMS?
More informationAlgorithm. Lecture3: Algorithm Analysis. Empirical Analysis. Algorithm Performance
Algorithm (03F) Lecture3: Algorithm Analysis A step by step procedure to solve a problem Start from an initial state and input Proceed through a finite number of successive states Stop when reaching a
More information