Algorithms. Margaret M. Fleck. 18 October 2010
|
|
|
- Alban Warner
- 9 years ago
- Views:
Transcription
1 Algorithms Margaret M. Fleck 18 October 2010 These notes cover how to analyze the running time of algorithms (sections 3.1, 3.3, 4.4, and 7.1 of Rosen). 1 Introduction The main reason for studying big-o notation and solving recurrences is so that we can predict how fast different computer programs will run. This allows us to choose good designs for different practical applications. We will normally do these analyses only up to big-o, i.e. ignoring multiplicative constants and considering only the terms that dominate the running time. We ll look at basic searching and sorting algorithms, plus one or more other algorithms with similar structure (e.g. the Towers of Hanoi solver). When analyzing these algorithms, we ll see how to focus in on the parts of the code that determine the big-o running time and where we can cut corners to keep our analysis simple. Such an approach may seem like sloppiness right now, but it becomes critical as you analyze more and more complex algorithms in later CS classes. In lecture, we typically do demos of how these algorithms work. Similar demos are easy to find on the web, if you aren t sure you understand how the pseudocode works. 2 Linear Search Suppose that we have an array of real numbers a 1, a 2,...a n and an input number b. The goal of a search algorithm is to find out whether b is in the list and, if so, at which array index it resides. Let s assume, for simplicity, 1
2 that the list contains no duplicates or (if it does) that our goal is to return the first array position containing the value b. The values in our input array might be in any random order. For example, if our input array contains (in order) 7, 3, 17, 2 and b = 17, we would return 3. If b = 27, we would return some special value to indicate that b isn t in the array. I ll pick zero for our special value. The pseudo-code for linear search looks like: 1: linearsearch(a 1,..., a n : reals, b real) 2: i = 1 3: while (a i b and i < n) 4: i = i + 1 5: if (a i = b) return i 6: else return 0 Notice that the input array starts with position 1. This is common in pseudocode, though most programming languages have array indices that start with zero. Also notice that the length of the array is given somewhat implicitly as the subscript on a n. The main loop in lines 3-4 walks through the array from start to end, looking for a value that matches b. The loop stops when it finds such a value (a i = b) or when we are about to go past the end of the input array (i n). To analyze this code in big-o terms, first notice that the start-up code in lines 1-2 and the ending code in lines 5-6 takes the same amount of time regardless of the input size n. So we ll say that it takes constant time or O(1) time. The big-o running time of linear search depends entirely on the behavior of the loop in lines 3-4, because the number of loop iterations does depend on n. In each iteration of the loop, we do a constant amount of work. So we just have to figure out how many times the loop runs, as a function of n. 3 Worst case and average case analysis So the big-o analysis would be easy except for the fact that the number of time the loop runs depends on how quickly we find a matching value in the 2
3 array. If b is near the start of the array, the code stops quickly. If b is near the end or not in the array at all, the code runs for n or close to n iterations. This sort of variable running time is quite common in computer algorithms. There are two ways to handle it: worst case analysis and average case analysis. In a worst-case analysis, we pick some random value for the input size n and ask what is the worst possible input of that size. That is, which input of size n will make the program run slowest. For linear search, it s an input where b is at the end of the array or not in the array. So, in the worst case, linear search requires n loop iterations. So its worst-case running time is O(n). In an average-case analysis, we again fix n but then work out the average behavior of the code on all inputs of size n. To do this, we need to model how often we expect to see different types of inputs. First, let s suppose that n is in the array. For linear search, it s normally reasonable to assume that different orderings of the array occur equally often. So b is as likely to be at position 37 as at position 8. In this case, the average number of times our loop runs is n n = 1 n n k = k=1 n(n + 1) 2n = n = O(n) If b might not be in the array, we d need to add in a term for this possibility, weighted by how frequently we expect b to be missing. This is very application-dependent, but the running time will still be O(n) because the loop runs n times whenever b is not found. If we expected some particular ordering to our input array, then the average running time might be very different. For example, in some applications, it s common to build the array by adding new items onto the front and then soon afterwards search the array for new copies of the same item. In this case, the average search time might be substantially less, perhaps even not O(n). But such applications are unusual. 4 Binary search In many applications, it s sensible to assume that the input array is sorted. That is a 1 a 2... a n. We ll see that it would take O(n log n) time to 3
4 sort an unsorted array. But sometimes an array of data comes pre-sorted. And sometimes an application will do many searches on an array of data that s constant or changes only very slowly, e.g. the registrar s big database of student records. In such cases, the cost of sorting the array may be insignificant compared to the cost of doing the searches. When our input array is sorted, we can use a much more efficient algorithm: binary search. Let s assume that our array is sorted with the smallest element at the start. (The same idea works, with small code changes, if the array is sorted in reverse order.) 1: binarysearch(a 1,..., a n : sorted array of reals, b real) 2: i = 1 3: j = n 4: while (i < j) 5: m = (i + j)/2 6: if (b > a m ) 7: then i = m+1 8: else j = m 9: if (a i = b) return i 10: else return 0 The two variables i and j define a range of array positions. At the start of the function, this range is all of the input array. In each iteration of the loop, we shrink the range in half, either by making i larger or by making j smaller. The loop terminates when the range contains only a single array position i. To do the shrinking, we examine the value at the middle position in the current range. If it s smaller than b, we know that b must live in the upper half of the range (if it s in the array at all). Otherwise, b must live in the lower half of the range (again, if it s in the array). 4
5 5 Analysis of binary search To do a big-o analysis of binary search s running time, notice that the code in lines 1-3 and 9-10 takes constant time, i.e. the same amount of time regardless of the input array size. So it s O(1) time and doesn t matter to our big-o analysis. The code inside the loop (lines 5-8) also takes constant time. So the running time of the code (as a function of n) is determined by how many times the loop runs. To figure out how many times the loop runs, first suppose that n is a power of two, e.g. n = 2 k. Since the range starts at length n and is cut in half in each iteration, it will take k iterations before the range has length 1 and the loop stops. If you have to do such an analysis really fast, e.g. on an exam, you might be uncertain about whether the loop really runs k times or perhaps k + 1 or k 1 times. This difference doesn t matter: it s still O(k) times. Since k = log 2 n, this means that the loop runs O(log n) times. If n isn t a power of two, we can get an upper bound on the running time. The code run on an array of size n will clearly take no more time than if it were run on an array of size 2 k where 2 k is the next power of two larger than n. So, the loop still runs O(logn) times. So, the overall running time of binary search is O(logn), where n is the length of the input array. In general, if an algorithm works by reducing the size of its problem by a factor of two (or three or four...), it probably takes O(log n) time. 6 Bubble sort A sorting algorithm takes an input array of real numbers and puts them into sorted order, e.g. smallest number first. Sorting algorithms are not actually restricted to numbers. With some minor changes, they can operate on any sort of data where you can precisely define an ordering. For example, they can be used to sort strings (e.g. names) into dictionary order. We ll stick to real numbers for simplicity. The bubble sort algorithm is an especially stupid sorting method. So stupid, in fact, that you re unlikely to see it outside of theory classes. But it is a useful starting point for looking at the running times of sorting algorithms. 1: bubblesort(a 1,..., a n : array of reals) 2: for j = 1 to n 1 5
6 3: for k = 1 to n j 4: if (a k > a k+1 ) 5: swap a k and a k+1 This function doesn t return anything explicitly. Rather, it rearranges the values within its input array, so the input array will be nicely sorted after the function returns. (Rather like organizing someone s closet for them.) The swap command in line 5 interchanges two values in the array. To implement this in most programming languages, you d need to put one of the values into a temporary variable e.g. t = a k, then move the other value into the first one s e.g. a k = a k+1, then put in the new second value e.g. a k+1 = t. Normally we hide this gory detail when writing pseudocode. Bubblesort walks repeatedly through the input array, fixing local problems in the ordering. Some versions of bubblesort just keep sweeping through the entire array repeatedly until they do a complete sweep without finding any more local problems. This version of bubblesort is a bit smarter. After the first time through the outer loop (line 2), we know that the largest value must have worked its way all the way down to the last array position. So this last position has its final value and so we can ignore it in subsequent iterations. After the second time through the outer loop. the last two positions are ok and can be left alone. This is why the top index in the inner loop (line 3) keeps decreasing. The running time of this algorithm is proportional to how many times the code inside both loops (lines 4-5) runs. This is n 1 n 1 (n i) = (n 1) + (n 2) = i = i=1 i=1 n(n 1) 2 = O(n 2 ) When reversing a summation like this, it would be easy to get something off by one, e.g. the top or bottom bound. Writing out the terms of the summation is helpful for checking this. 7 Insertion sort Like bubble sort, insertion sort works by side-effect: it doesn t return a value but, instead, rearranges the numbers within an array. The idea behind 6
7 insertion sort is to divide the input array (conceptually) into two pieces: a portion that has been sorted and a portion that isn t sorted yet. At the start, the sorted portion contains only a single number: the original first number in the array. We add the other numbers one by one, inserting each one into its proper place in the growing sorted list. When we add some new number a j to the sorted part of the array, we first remove it from the array. Then we first need to find its proper position in the sorted portion. Then we usually need to shove some of the sorted values to the right to make a space to insert a j. Then we actually insert a j. Our pseudo-code looks like 1: insertionsort(a 1,..., a n : array of reals) 2: for j = 2 to n { 3: t = a j 4: i = 1 5: while (a i < t) 6: i = i + 1 7: for k = j down to i + 1 9: a i = t } 8: a k = a k 1 The inside of the big loop moves the value a j from the unsorted part of the array into the sorted part. Lines 4-6 locate the place where a j belongs in the sorted portion, which will be right before the value a i. Lines 7-8 then shove the sorted values starting with a i over to the right to make a blank space, into which line 9 puts a j. Notice that the loop index in line 7 moves downwards rather than upwards. In some programming languages, you can just do this directly. In others, you (annoyingly) need to use an upward-moving loop variable and do some algebra on the index values to derive the downward-moving indices. 7
8 8 Running time of insertion sort Suppose that T(n) is the worst-case running time for insertion sort. That is, for each input array length n, T(n) is the maximum time that insertion sort might take on any array of that length. Notice that we now have two closely-related functions floating around: insertionsort and its running time T. Insertion sort has two nested loops. This should immediately suggest to you: O(n 2 ) time. So let s check out the details of the loops to see if that s right. The inner while loop (line 5-6) runs no more than j times. If it hasn t already halted earlier, it definitely has to halt when i = j. To be really precise, the test for this loop (line 5) gets executed no more than j times and the material inside the loop (line 6) no more than j 1 times. Similarly, the work inside the for loop (lines 7-8) takes at most j time. So, the code inside the loops runs no more than n j=2 j = n 1 j=1 (j +1) = (n 1) + n(n 1) = O(n 2 ) 2 Other parts of the code run less often. E.g. the loop index computation and testing in line 2 takes O(n) time. And there s probably some constanttime work at the start and end of the function. But this work is dominated by the O(n 2 ) work in the inner loop. So, T(n) = O(n 2 ). 9 Linked Lists So far, all of our sorting and searching algorithms have stored their data in arrays. The next algorithm, merge sort, can be implemented using arrays but is more often described using linked lists. Like an array, a linked list stores an ordered sequence of objects (e.g. numbers). However, its length is flexible and it s easy to add and remove objects. When you take the data structures course, you ll learn how to implement several varieties of linked lists. In these notes, we ll ignore the implementation details. To keep things simple, we ll assume a generic and fairly flexible type of linked list. A linked list starts with its head and ends with its tail. For example, if our list is (1, 7, 3, 4, 7, 19), then the value at the head end is 1 and the value at the tail end is 19. Adding or removing a single element at either end of the 8
9 list takes constant (aka O(1)) time. 1 Elements in the middle of the list are accessed by starting at the head and walking down the list. So retrieving or changing the value in the nth list position takes O(n) time, as does deleting the nth item from the list. 10 Merge sort Mergesort takes an input linked list of numbers and returns a new linked list containing the sorted numbers. This is somewhat different from bubble and insertion sort, which rearrange the values within a single array (and don t return anything). Merge sort is a so-called divide-and-conquer algorithm. That is, it takes a big problem (sorting a list of length n) and divides it into two smaller problems (sorting two lists of length n/2). We divide up the list repeatedly until we have a large number of very short lists, of length 1 or 2 (depending on the preferences of the code writer). A length-1 list is necessarily sorted. A length 2 list can be sorted in constant time. Then, we take all these small sorted lists and merge them together in pairs, gradually building up longer and longer sorted lists until we have one sorted list containing all of our original input numbers. The big trick here is merging two sorted lists, a 1,..., a n and b 1,..., b m. To do this, we make a new third list to contain our merged output. Then we examine the first elements of the two input lists and move the smaller value onto our output list. We keep looking at the first elements of both lists until one list is empty. We then copy over the rest of the non-empty list. This merger process is very efficient, because each merger step only examines the two values at the heads of the lists. And, in fact, the running time of merge sort is O(n log n), which is the best possible big-o running time for a sorting algorithm. 11 Pseudo-code for mergesort The pseudocode for mergesort might look as follows: 1 If you do know something about data structures, this is only strictly true for doublylinked lists. However, many algorithms, including merge sort, can also be implemented with singly-linked lists, either by keeping a pointer to the tail of the list or by strategic reversal of the list order. 9
10 1. mergesort(l = a 1, a 2,...,a n : list of real numbers) 2. if (n = 1) then return L 3. else 4. m = n/2 5. L 1 = (a 1, a 2,...,a m ) 6. L 2 = (a m+1, a m+2,...,a n ) 7. return merge(mergesort(l 1 ),mergesort(l 2 )) And the pseudocode for the merge function might look like the following, assuming that head(l) returns the value at the head of a list L. 1. merge(l 1,L 2 : sorted lists of real numbers) 2. O = emptylist 3. while (L 1 is not empty or L 2 is not empty) 4. if (L 1 is empty) 5. move head(l 2 ) to the tail of O 6. else if (L 2 is empty) 7. move head(l 1 ) to the tail of O 8. else if (head(l 1 ) <= head(l 2 )) 9. move head(l 1 ) to the tail of O 10. else move head(l 2 ) to the tail of O 11. return O 12 Analysis of mergesort Now, let s do a big-o analysis of mergesort s running time. We need to start by looking at the merge function. For merge, a good way to measure the size of the input n is that n is the sum of the lengths of the two input arrays. Or, equivalently, n is the length of the output array. The merge function has one big loop. Since the operations within the loop all take constant time, we just need to figure out how many times the loop runs. Each time the loop runs, we move one number from L 1 or L 2 onto 10
11 the output list O. n numbers have to be moved, in total. So the loop must run n times. So merge takes O(n) (aka linear) time. In this analysis of merge, we have a resource: the numbers in the list. This resource has a known size and is being consumed at some known rate. For this algorithm, analyzing the consumption of the resource is the simplest way to figure out how many times the critical loop will execute. Now, let s analyze the running time of mergesort on an input of length n. Mergesort makes two recursive calls to itself. It also does O(n) work dividing the list in half, because it must walk, element by element, from the head of the list down to the middle position. And it does O(n) work merging the two results. So if the running time of mergesort is T(n), we can write the following recurrence for T(n), where c and d are constants. T(1) = c T(n) = 2T(n/2) + dn This recurrence has the following recursion tree: dn dn/2 dn/2 dn/4 dn/4 dn/4 dn/ The tree has O(log n) non-leaf levels and the work at each level sums up to dn. So the work from the non-leaf nodes sums up to O(n log n). In addition, there are n leaf nodes (aka base cases for the recursive function), each of which involves c work. So the total running time is O(n logn) + cn which is just O(n log n). 11
12 13 The practical truth for sorting algorithms The worst-case big-o running time of merge sort is as good as you can do. However, mergesort s constants are only so-so. A better practical choice in most circumstances is a different O(n log n) algorithm called heap sort. On small to moderately-sized sets of data, the algorithm of choice is actually quick sort, which we won t discuss in this class because it requires a more complicated, statistical analysis. Quicksort has a O(n 2 ) worst-case running time, but a O(n logn) average running time and good constants. Most built-in sort functions use quicksort. Aside from making an nice example in theory classes, mergesort is used for certain specialized sorting tasks. These include applications where data is already stored in a linked list and applications requiring a stable sort in which elements with identical value maintain their relative positions. Because the key merge step accesses its three lists in storage order, variations of this technique are used when access to the data is very slow. In the Bad Old Days of ancient computers, slow storage usually involved medium-sized datasets stored on magnetic tapes. These days, slow access typically involves data that is distributed across several processors or across the internet, e.g. extremely large datasets. 14 Tower of Hanoi The Tower of Hanoi puzzle was invented by the French mathematician Edouard Lucas in It consists of three pegs and a set of disks of graduated size that fit on them. The disks start out in order on one peg. The goal is to rebuild the ordered tower on another peg without ever placing a disk on top of a smaller disk. Show the game and its solution using the animation at: The best way to understand the solution is recursively. Suppose that we know how to move k disks from one peg to another peg, using a third temporary-storage peg. To move k + 1 disks from peg A to peg B using a third peg C, we first move the top k disks from A to C using B as temporary storage. Then we move the biggest disk from A to B. Then we move the other k disks from C to B, using A as temporary storage. 12
13 So our recursive solver looks like 1. hanoi(a,b,c: pegs, d 1, d 2... d n : disks) 2. if (n = 1) move d 1 = d n from A to B. 3. else 4. hanoi(a,c,b,d 1, d 2,...d n 1 ) 5. move d n from A to B. 6. hanoi(c,b,a,d 1, d 2,...d n 1 ) The function hanoi breaks up a problem of size n into two problems of size n 1. Alert! Warning bells! This can t be good: the sub-problems aren t much smaller than the original problem! Anyway, hanoi breaks up a problem of size n into two problems of size n 1. Other than the two recursive calls, it does only a constant amount of work. So the running time T(n) for the function hanoi would be given by the recurrence (where c and d are constants): T(1) = c T(n) = 2T(n 1) + d If we unroll this recurrence, we get T(n) = 2T(n 1) + d = 2 2(T(n 2) + d) + d = 2 2(2(T(n 3) + d) + d) + d = 2 3 T(n 3) d + 2d + d k 1 = 2 k T(n k) + d i=0 2 i We ll hit the base case when k = n 1. So 13
14 k 1 T(n) = 2 k T(n k) + d n 2 = 2 n 1 c + d i=0 2 i i=0 = 2 n 1 c + d(2 n 1 1) = 2 n 1 c + 2 n 1 d d) = O(2 n ) 15 Multiplying big integers Suppose we want to multiply two integers. Back in the Bad Old Days of slow computers, we would need to multiply moderate-sized integers digit-by-digit. These days, we typically have a CPU that can multiply two moderate-sized numbers (e.g. 16-bit, 32-bit) as a single operation. But some applications (e.g. in cryptography) involve multiplying very, very large integers. Each very long integer must then be broken up into a sequence of 16-bit or 32-bit integers. Let s suppose that we are breaking up each integer into individual digits, and that we re working in base 2. The recursive multiplication algorithm divides each n-digit input number into two n/2-digit halves. Specifically, suppose that our inputs numbers are x and y and they each have 2m digits. We can then divide them up as 2 i x = x 1 2 m + x 0 y = y 1 2 m + y 0 If we multiply x by y in the obvious way, we get xy = A2 2m + B2 m + C where A = x 1 y 1, B = x 0 y 1 + x 1 y 0, and C = x 0 y 0. Set up this way, computing xy requires multiplying four numbers with half the number of digits. So, if you just count the number of multiplications, the running time of this naive method has a recurrence 14
15 T(n) = 4T(n/2) + O(n) If you solve this recurrence with T(1) being a constant, you find that T(n) = O(n 2 ). (Unrolling works well for finding this solution.) In this analysis, we worry primarily about the multiplications and treat other arithmetic as being fast, aka O(n). To understand why this is reasonable, first notice that adding two numbers requires time proportional to the number of digits in them. Multiplying by 2 m is also fast, because it just requires left-shifting the bits in the numbers. If you haven t done much with binary numbers yet (and perhaps don t know what it means to shift them), then it s just like multiplying by 10 m when working with (normal) base-10 numbers. To multiply by 10 m, you just have to add m zeros to the end of the number. However, we can rewrite our algebra for computing B as follows B = (x 1 + x 0 )(y 1 + y 0 ) A C So we can compute B with only one multiplication. So, if we use this formula for B, the running time of multiplication has the recurrence: P(n) = 3P(n/2) + O(n) It s not obvious that we ve gained anything substantial, but we have. If we build a recursion tree for P, we discover that the kth level of the tree contains 3 k problems, each involving n 1 work. The tree height is log 2 k 2 (n). So each level requires n( 3 2 )k work. If you add up this up for all levels, you get a summation that is dominated by the term for the bottom level of the tree: n( 3 2 )log 2 n work. If you mess with this expression a bit, using facts about logarithms, you find that it s O(n log 2 3 ) which is approximately O(n ). So this trick, due to Anatolii Karatsuba, has improved our algorithm s speed from O(n 2 ) to O(n ) with essentially no change in the constants. If n = = 1024, then the naive algorithm requires (2 1 0) 2 = 1, 048, 576 multiplications, whereas Katatsuba s method requires 3 10 = 59, 049 multiplications. So this is a noticable improvement and the difference will widen as n increases. There are actually other integer multiplication algorithms with even faster running times, e.g. Schoöhage-Strassen s method takes O(n log n log log n) time. But these methods are more involved. 15
Dynamic Programming. Lecture 11. 11.1 Overview. 11.2 Introduction
Lecture 11 Dynamic Programming 11.1 Overview Dynamic Programming is a powerful technique that allows one to solve many different types of problems in time O(n 2 ) or O(n 3 ) for which a naive approach
In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write
Big O notation (with a capital letter O, not a zero), also called Landau's symbol, is a symbolism used in complexity theory, computer science, and mathematics to describe the asymptotic behavior of functions.
The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!
The Tower of Hanoi Recursion Solution recursion recursion recursion Recursive Thinking: ignore everything but the bottom disk. 1 2 Recursive Function Time Complexity Hanoi (n, src, dest, temp): If (n >
CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team
CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team Lecture Summary In this lecture, we learned about the ADT Priority Queue. A
CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting
CSC148 Lecture 8 Algorithm Analysis Binary Search Sorting Algorithm Analysis Recall definition of Big Oh: We say a function f(n) is O(g(n)) if there exists positive constants c,b such that f(n)
recursion, O(n), linked lists 6/14
recursion, O(n), linked lists 6/14 recursion reducing the amount of data to process and processing a smaller amount of data example: process one item in a list, recursively process the rest of the list
Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction
Class Overview CSE 326: Data Structures Introduction Introduction to many of the basic data structures used in computer software Understand the data structures Analyze the algorithms that use them Know
Binary Heap Algorithms
CS Data Structures and Algorithms Lecture Slides Wednesday, April 5, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks [email protected] 2005 2009 Glenn G. Chappell
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Linda Shapiro Today Registration should be done. Homework 1 due 11:59 pm next Wednesday, January 14 Review math essential
Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *
Binary Heaps A binary heap is another data structure. It implements a priority queue. Priority Queue has the following operations: isempty add (with priority) remove (highest priority) peek (at highest
6. Standard Algorithms
6. Standard Algorithms The algorithms we will examine perform Searching and Sorting. 6.1 Searching Algorithms Two algorithms will be studied. These are: 6.1.1. inear Search The inear Search The Binary
CSC 180 H1F Algorithm Runtime Analysis Lecture Notes Fall 2015
1 Introduction These notes introduce basic runtime analysis of algorithms. We would like to be able to tell if a given algorithm is time-efficient, and to be able to compare different algorithms. 2 Linear
Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.
Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik Demaine and Shafi Goldwasser Quiz 1 Quiz 1 Do not open this quiz booklet until you are directed
1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.
1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D. base address 2. The memory address of fifth element of an array can be calculated
Symbol Tables. Introduction
Symbol Tables Introduction A compiler needs to collect and use information about the names appearing in the source program. This information is entered into a data structure called a symbol table. The
Analysis of Binary Search algorithm and Selection Sort algorithm
Analysis of Binary Search algorithm and Selection Sort algorithm In this section we shall take up two representative problems in computer science, work out the algorithms based on the best strategy to
Data Structures. Algorithm Performance and Big O Analysis
Data Structures Algorithm Performance and Big O Analysis What s an Algorithm? a clearly specified set of instructions to be followed to solve a problem. In essence: A computer program. In detail: Defined
Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb
Binary Search Trees Lecturer: Georgy Gimel farb COMPSCI 220 Algorithms and Data Structures 1 / 27 1 Properties of Binary Search Trees 2 Basic BST operations The worst-case time complexity of BST operations
Converting a Number from Decimal to Binary
Converting a Number from Decimal to Binary Convert nonnegative integer in decimal format (base 10) into equivalent binary number (base 2) Rightmost bit of x Remainder of x after division by two Recursive
Pseudo code Tutorial and Exercises Teacher s Version
Pseudo code Tutorial and Exercises Teacher s Version Pseudo-code is an informal way to express the design of a computer program or an algorithm in 1.45. The aim is to get the idea quickly and also easy
Algorithm Design and Recursion
Chapter 13 Algorithm Design and Recursion Objectives To understand basic techniques for analyzing the efficiency of algorithms. To know what searching is and understand the algorithms for linear and binary
14:440:127 Introduction to Computers for Engineers. Notes for Lecture 06
14:440:127 Introduction to Computers for Engineers Notes for Lecture 06 Rutgers University, Spring 2010 Instructor- Blase E. Ur 1 Loop Examples 1.1 Example- Sum Primes Let s say we wanted to sum all 1,
Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally
Recurrence Relations Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally modeled by recurrence relations. A recurrence relation is an equation which
The Taxman Game. Robert K. Moniot September 5, 2003
The Taxman Game Robert K. Moniot September 5, 2003 1 Introduction Want to know how to beat the taxman? Legally, that is? Read on, and we will explore this cute little mathematical game. The taxman game
3. Mathematical Induction
3. MATHEMATICAL INDUCTION 83 3. Mathematical Induction 3.1. First Principle of Mathematical Induction. Let P (n) be a predicate with domain of discourse (over) the natural numbers N = {0, 1,,...}. If (1)
Heaps & Priority Queues in the C++ STL 2-3 Trees
Heaps & Priority Queues in the C++ STL 2-3 Trees CS 3 Data Structures and Algorithms Lecture Slides Friday, April 7, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks
Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)
Sorting revisited How did we use a binary search tree to sort an array of elements? Tree Sort Algorithm Given: An array of elements to sort 1. Build a binary search tree out of the elements 2. Traverse
Kapitel 1 Multiplication of Long Integers (Faster than Long Multiplication)
Kapitel 1 Multiplication of Long Integers (Faster than Long Multiplication) Arno Eigenwillig und Kurt Mehlhorn An algorithm for multiplication of integers is taught already in primary school: To multiply
Binary search algorithm
Binary search algorithm Definition Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than
Chapter 13: Query Processing. Basic Steps in Query Processing
Chapter 13: Query Processing! Overview! Measures of Query Cost! Selection Operation! Sorting! Join Operation! Other Operations! Evaluation of Expressions 13.1 Basic Steps in Query Processing 1. Parsing
CS473 - Algorithms I
CS473 - Algorithms I Lecture 9 Sorting in Linear Time View in slide-show mode 1 How Fast Can We Sort? The algorithms we have seen so far: Based on comparison of elements We only care about the relative
SQL Query Evaluation. Winter 2006-2007 Lecture 23
SQL Query Evaluation Winter 2006-2007 Lecture 23 SQL Query Processing Databases go through three steps: Parse SQL into an execution plan Optimize the execution plan Evaluate the optimized plan Execution
Introduction to Data Structures
Introduction to Data Structures Albert Gural October 28, 2011 1 Introduction When trying to convert from an algorithm to the actual code, one important aspect to consider is how to store and manipulate
1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++
Answer the following 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++ 2) Which data structure is needed to convert infix notations to postfix notations? Stack 3) The
Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child
Binary Search Trees Data in each node Larger than the data in its left child Smaller than the data in its right child FIGURE 11-6 Arbitrary binary tree FIGURE 11-7 Binary search tree Data Structures Using
Lecture 3: Finding integer solutions to systems of linear equations
Lecture 3: Finding integer solutions to systems of linear equations Algorithmic Number Theory (Fall 2014) Rutgers University Swastik Kopparty Scribe: Abhishek Bhrushundi 1 Overview The goal of this lecture
Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1
Recursion Slides by Christopher M Bourke Instructor: Berthe Y Choueiry Fall 007 Computer Science & Engineering 35 Introduction to Discrete Mathematics Sections 71-7 of Rosen cse35@cseunledu Recursive Algorithms
Lecture Notes on Binary Search Trees
Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 17 October 23, 2014 1 Introduction In this lecture, we will continue considering associative
Sample Questions Csci 1112 A. Bellaachia
Sample Questions Csci 1112 A. Bellaachia Important Series : o S( N) 1 2 N N i N(1 N) / 2 i 1 o Sum of squares: N 2 N( N 1)(2N 1) N i for large N i 1 6 o Sum of exponents: N k 1 k N i for large N and k
This Unit: Floating Point Arithmetic. CIS 371 Computer Organization and Design. Readings. Floating Point (FP) Numbers
This Unit: Floating Point Arithmetic CIS 371 Computer Organization and Design Unit 7: Floating Point App App App System software Mem CPU I/O Formats Precision and range IEEE 754 standard Operations Addition
The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,
The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge, cheapest first, we had to determine whether its two endpoints
B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees
B-Trees Algorithms and data structures for external memory as opposed to the main memory B-Trees Previous Lectures Height balanced binary search trees: AVL trees, red-black trees. Multiway search trees:
A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:
Binary Search Trees 1 The general binary tree shown in the previous chapter is not terribly useful in practice. The chief use of binary trees is for providing rapid access to data (indexing, if you will)
APP INVENTOR. Test Review
APP INVENTOR Test Review Main Concepts App Inventor Lists Creating Random Numbers Variables Searching and Sorting Data Linear Search Binary Search Selection Sort Quick Sort Abstraction Modulus Division
Algorithm Analysis [2]: if-else statements, recursive algorithms. COSC 2011, Winter 2004, Section N Instructor: N. Vlajic
1 Algorithm Analysis []: if-else statements, recursive algorithms COSC 011, Winter 004, Section N Instructor: N. Vlajic Algorithm Analysis for-loop Running Time The running time of a simple loop for (int
Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010
Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010 Problem 1. In each of the following question, please specify if the statement
Record Storage and Primary File Organization
Record Storage and Primary File Organization 1 C H A P T E R 4 Contents Introduction Secondary Storage Devices Buffering of Blocks Placing File Records on Disk Operations on Files Files of Unordered Records
- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time
Skip Lists CMSC 420 Linked Lists Benefits & Drawbacks Benefits: - Easy to insert & delete in O(1) time - Don t need to estimate total memory needed Drawbacks: - Hard to search in less than O(n) time (binary
CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) 3 4 4 7 5 9 6 16 7 8 8 4 9 8 10 4 Total 92.
Name: Email ID: CSE 326, Data Structures Section: Sample Final Exam Instructions: The exam is closed book, closed notes. Unless otherwise stated, N denotes the number of elements in the data structure
THIS CHAPTER studies several important methods for sorting lists, both contiguous
Sorting 8 THIS CHAPTER studies several important methods for sorting lists, both contiguous lists and linked lists. At the same time, we shall develop further tools that help with the analysis of algorithms
Lecture 2 February 12, 2003
6.897: Advanced Data Structures Spring 003 Prof. Erik Demaine Lecture February, 003 Scribe: Jeff Lindy Overview In the last lecture we considered the successor problem for a bounded universe of size u.
Why? A central concept in Computer Science. Algorithms are ubiquitous.
Analysis of Algorithms: A Brief Introduction Why? A central concept in Computer Science. Algorithms are ubiquitous. Using the Internet (sending email, transferring files, use of search engines, online
Algorithm Design and Analysis Homework #1 Due: 5pm, Friday, October 4, 2013 TA email: [email protected]. === Homework submission instructions ===
Algorithm Design and Analysis Homework #1 Due: 5pm, Friday, October 4, 2013 TA email: [email protected] === Homework submission instructions === For Problem 1, commit your source code and a brief documentation
Sequential Data Structures
Sequential Data Structures In this lecture we introduce the basic data structures for storing sequences of objects. These data structures are based on arrays and linked lists, which you met in first year
Kenken For Teachers. Tom Davis [email protected] http://www.geometer.org/mathcircles June 27, 2010. Abstract
Kenken For Teachers Tom Davis [email protected] http://www.geometer.org/mathcircles June 7, 00 Abstract Kenken is a puzzle whose solution requires a combination of logic and simple arithmetic skills.
Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park
Sorting Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park Overview Comparison sort Bubble sort Selection sort Tree sort Heap sort Quick sort Merge
Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles
B-Trees Algorithms and data structures for external memory as opposed to the main memory B-Trees Previous Lectures Height balanced binary search trees: AVL trees, red-black trees. Multiway search trees:
Persistent Binary Search Trees
Persistent Binary Search Trees Datastructures, UvA. May 30, 2008 0440949, Andreas van Cranenburgh Abstract A persistent binary tree allows access to all previous versions of the tree. This paper presents
6 March 2007 1. Array Implementation of Binary Trees
Heaps CSE 0 Winter 00 March 00 1 Array Implementation of Binary Trees Each node v is stored at index i defined as follows: If v is the root, i = 1 The left child of v is in position i The right child of
ECON 459 Game Theory. Lecture Notes Auctions. Luca Anderlini Spring 2015
ECON 459 Game Theory Lecture Notes Auctions Luca Anderlini Spring 2015 These notes have been used before. If you can still spot any errors or have any suggestions for improvement, please let me know. 1
Chapter 7. Functions and onto. 7.1 Functions
Chapter 7 Functions and onto This chapter covers functions, including function composition and what it means for a function to be onto. In the process, we ll see what happens when two dissimilar quantifiers
Section 1.3 P 1 = 1 2. = 1 4 2 8. P n = 1 P 3 = Continuing in this fashion, it should seem reasonable that, for any n = 1, 2, 3,..., = 1 2 4.
Difference Equations to Differential Equations Section. The Sum of a Sequence This section considers the problem of adding together the terms of a sequence. Of course, this is a problem only if more than
11 Multivariate Polynomials
CS 487: Intro. to Symbolic Computation Winter 2009: M. Giesbrecht Script 11 Page 1 (These lecture notes were prepared and presented by Dan Roche.) 11 Multivariate Polynomials References: MC: Section 16.6
Node-Based Structures Linked Lists: Implementation
Linked Lists: Implementation CS 311 Data Structures and Algorithms Lecture Slides Monday, March 30, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks [email protected]
Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R
Binary Search Trees A Generic Tree Nodes in a binary search tree ( B-S-T) are of the form P parent Key A Satellite data L R B C D E F G H I J The B-S-T has a root node which is the only node whose parent
Closest Pair Problem
Closest Pair Problem Given n points in d-dimensions, find two whose mutual distance is smallest. Fundamental problem in many applications as well as a key step in many algorithms. p q A naive algorithm
Operation Count; Numerical Linear Algebra
10 Operation Count; Numerical Linear Algebra 10.1 Introduction Many computations are limited simply by the sheer number of required additions, multiplications, or function evaluations. If floating-point
1. The RSA algorithm In this chapter, we ll learn how the RSA algorithm works.
MATH 13150: Freshman Seminar Unit 18 1. The RSA algorithm In this chapter, we ll learn how the RSA algorithm works. 1.1. Bob and Alice. Suppose that Alice wants to send a message to Bob over the internet
Lecture 1: Course overview, circuits, and formulas
Lecture 1: Course overview, circuits, and formulas Topics in Complexity Theory and Pseudorandomness (Spring 2013) Rutgers University Swastik Kopparty Scribes: John Kim, Ben Lund 1 Course Information Swastik
Algorithms and Data Structures
Algorithms and Data Structures CMPSC 465 LECTURES 20-21 Priority Queues and Binary Heaps Adam Smith S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Trees Rooted Tree: collection
From Last Time: Remove (Delete) Operation
CSE 32 Lecture : More on Search Trees Today s Topics: Lazy Operations Run Time Analysis of Binary Search Tree Operations Balanced Search Trees AVL Trees and Rotations Covered in Chapter of the text From
language 1 (source) compiler language 2 (target) Figure 1: Compiling a program
CS 2112 Lecture 27 Interpreters, compilers, and the Java Virtual Machine 1 May 2012 Lecturer: Andrew Myers 1 Interpreters vs. compilers There are two strategies for obtaining runnable code from a program
Lecture Notes on Linear Search
Lecture Notes on Linear Search 15-122: Principles of Imperative Computation Frank Pfenning Lecture 5 January 29, 2013 1 Introduction One of the fundamental and recurring problems in computer science is
root node level: internal node edge leaf node CS@VT Data Structures & Algorithms 2000-2009 McQuain
inary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from each
1 Review of Newton Polynomials
cs: introduction to numerical analysis 0/0/0 Lecture 8: Polynomial Interpolation: Using Newton Polynomials and Error Analysis Instructor: Professor Amos Ron Scribes: Giordano Fusco, Mark Cowlishaw, Nathanael
Lecture Notes on Binary Search Trees
Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning Lecture 17 March 17, 2010 1 Introduction In the previous two lectures we have seen how to exploit the structure
Questions 1 through 25 are worth 2 points each. Choose one best answer for each.
Questions 1 through 25 are worth 2 points each. Choose one best answer for each. 1. For the singly linked list implementation of the queue, where are the enqueues and dequeues performed? c a. Enqueue in
Module 2 Stacks and Queues: Abstract Data Types
Module 2 Stacks and Queues: Abstract Data Types A stack is one of the most important and useful non-primitive linear data structure in computer science. It is an ordered collection of items into which
Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s
Quiz 4 Solutions Q1: What value does function mystery return when called with a value of 4? int mystery ( int number ) { if ( number
A Note on Maximum Independent Sets in Rectangle Intersection Graphs
A Note on Maximum Independent Sets in Rectangle Intersection Graphs Timothy M. Chan School of Computer Science University of Waterloo Waterloo, Ontario N2L 3G1, Canada [email protected] September 12,
Binary Trees and Huffman Encoding Binary Search Trees
Binary Trees and Huffman Encoding Binary Search Trees Computer Science E119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Motivation: Maintaining a Sorted Collection of Data A data dictionary
To My Parents -Laxmi and Modaiah. To My Family Members. To My Friends. To IIT Bombay. To All Hard Workers
To My Parents -Laxmi and Modaiah To My Family Members To My Friends To IIT Bombay To All Hard Workers Copyright 2010 by CareerMonk.com All rights reserved. Designed by Narasimha Karumanchi Printed in
Chapter 11 Number Theory
Chapter 11 Number Theory Number theory is one of the oldest branches of mathematics. For many years people who studied number theory delighted in its pure nature because there were few practical applications
DATABASE DESIGN - 1DL400
DATABASE DESIGN - 1DL400 Spring 2015 A course on modern database systems!! http://www.it.uu.se/research/group/udbl/kurser/dbii_vt15/ Kjell Orsborn! Uppsala Database Laboratory! Department of Information
Introduction to data structures
Notes 2: Introduction to data structures 2.1 Recursion 2.1.1 Recursive functions Recursion is a central concept in computation in which the solution of a problem depends on the solution of smaller copies
Computer Science 210: Data Structures. Searching
Computer Science 210: Data Structures Searching Searching Given a sequence of elements, and a target element, find whether the target occurs in the sequence Variations: find first occurence; find all occurences
The Running Time of Programs
CHAPTER 3 The Running Time of Programs In Chapter 2, we saw two radically different algorithms for sorting: selection sort and merge sort. There are, in fact, scores of algorithms for sorting. This situation
10CS35: Data Structures Using C
CS35: Data Structures Using C QUESTION BANK REVIEW OF STRUCTURES AND POINTERS, INTRODUCTION TO SPECIAL FEATURES OF C OBJECTIVE: Learn : Usage of structures, unions - a conventional tool for handling a
csci 210: Data Structures Recursion
csci 210: Data Structures Recursion Summary Topics recursion overview simple examples Sierpinski gasket Hanoi towers Blob check READING: GT textbook chapter 3.5 Recursion In general, a method of defining
Mathematical Induction
Mathematical Induction (Handout March 8, 01) The Principle of Mathematical Induction provides a means to prove infinitely many statements all at once The principle is logical rather than strictly mathematical,
Data Structures and Data Manipulation
Data Structures and Data Manipulation What the Specification Says: Explain how static data structures may be used to implement dynamic data structures; Describe algorithms for the insertion, retrieval
Section IV.1: Recursive Algorithms and Recursion Trees
Section IV.1: Recursive Algorithms and Recursion Trees Definition IV.1.1: A recursive algorithm is an algorithm that solves a problem by (1) reducing it to an instance of the same problem with smaller
Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015
Data Structures Jaehyun Park CS 97SI Stanford University June 29, 2015 Typical Quarter at Stanford void quarter() { while(true) { // no break :( task x = GetNextTask(tasks); process(x); // new tasks may
Data Structures and Algorithms Written Examination
Data Structures and Algorithms Written Examination 22 February 2013 FIRST NAME STUDENT NUMBER LAST NAME SIGNATURE Instructions for students: Write First Name, Last Name, Student Number and Signature where
Algorithms and Data Structures
Algorithms and Data Structures Part 2: Data Structures PD Dr. rer. nat. habil. Ralf-Peter Mundani Computation in Engineering (CiE) Summer Term 2016 Overview general linked lists stacks queues trees 2 2
5544 = 2 2772 = 2 2 1386 = 2 2 2 693. Now we have to find a divisor of 693. We can try 3, and 693 = 3 231,and we keep dividing by 3 to get: 1
MATH 13150: Freshman Seminar Unit 8 1. Prime numbers 1.1. Primes. A number bigger than 1 is called prime if its only divisors are 1 and itself. For example, 3 is prime because the only numbers dividing
CSE 421 Algorithms: Divide and Conquer
CSE 421 Algorithms: Divide and Conquer Summer 2011 Larry Ruzzo Thanks to Paul Beame, James Lee, Kevin Wayne for some slides hw2 empirical run times e n Plot Time vs n Fit curve to it (e.g., with Excel)
