Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
courses:cs211:winter2018:journals:beckg:ch2 [2018/01/30 00:35] beckgcourses:cs211:winter2018:journals:beckg:ch2 [2018/01/30 04:48] (current) beckg
Line 73: Line 73:
 Over the course of analyzing algorithms, a number of running times come up quite frequently, such as //O(n), O(n<sup>2</sup>),// and //O(n log n)//. This is not a coincidence, as these tend to originate from very common algorithm designs and structures. We will go over these in the coming section. Additionally, note that a good starting point in much of algorithm analysis is the //natural search space//: the set of all possible solutions. Thus, the point of algorithm design is to find algorithms which perform better than brute force searching our way through all of them. Over the course of analyzing algorithms, a number of running times come up quite frequently, such as //O(n), O(n<sup>2</sup>),// and //O(n log n)//. This is not a coincidence, as these tend to originate from very common algorithm designs and structures. We will go over these in the coming section. Additionally, note that a good starting point in much of algorithm analysis is the //natural search space//: the set of all possible solutions. Thus, the point of algorithm design is to find algorithms which perform better than brute force searching our way through all of them.
  
-=== //O(n)// Linear Time ===+=== Linear Time ===
 Algorithms that run in //O(n)// time are at most always a constant factor times the input size. Many of these end up being "one-pass" algorithms, in which we access everything and compute what we need by passing through it once. For example, simply computing the maximum of a random list of numbers. Algorithms that run in //O(n)// time are at most always a constant factor times the input size. Many of these end up being "one-pass" algorithms, in which we access everything and compute what we need by passing through it once. For example, simply computing the maximum of a random list of numbers.
  
 However, as we learned in class, these could be more nuanced, as is the case of merging two sorted lists. Here, we always compare the smallest two items of each list, and remove the smaller, adding it to the end of our third merged list. The important part of proving this linear is noting that on each comparison, one of the total //2n// items (supposing two lists of //n// length) is added to the new list. Therefore, there are only //2n// iterations total.  However, as we learned in class, these could be more nuanced, as is the case of merging two sorted lists. Here, we always compare the smallest two items of each list, and remove the smaller, adding it to the end of our third merged list. The important part of proving this linear is noting that on each comparison, one of the total //2n// items (supposing two lists of //n// length) is added to the new list. Therefore, there are only //2n// iterations total. 
  
-=== //O(n log n)// Time === +=== "Linearithmic" Time === 
-"Linearithmic" time is also very common. Specifically, this is seen in //Mergesort// and other good sorting algorithms. The key property that gives rise to this time is the splitting of the input data in two, and solving the problem on each half recursively. We also saw in class how this is the time of finding the maximum time interval given random time stamps in an input. This costs //O(n log n)// to sort the times, then processes them through once to find maximum interval, which is linear. So, it ends up being //O((n log n) + n) = O(n log n)//. +Running time of //O(n log n)// is also very common. Specifically, this is seen in //Mergesort// and other good sorting algorithms. The key property that gives rise to this time is the splitting of the input data in two, and solving the problem on each half recursively. We also saw in class how this is the time of finding the maximum time interval given random time stamps in an input. This costs //O(n log n)// to sort the times, then processes them through once to find maximum interval, which is linear. So, it ends up being //O((n log n) + n) = O(n log n)//. 
  
-=== //O(n<sup>2</sup>)// Quadratic Time ===+=== Quadratic Time ===
 Quadratic time arises naturally from the //Closest-Pair Problem//--finding the closest pair of points given an input of many in a given plane. This is because it simply is the full natural search space: search over all pairs of input items (//n<sup>2</sup>// total number of pairs), and spend constant time per pair. We will later in Ch. 5 go over a more efficient algorithm for this problem, though. Quadratic time arises naturally from the //Closest-Pair Problem//--finding the closest pair of points given an input of many in a given plane. This is because it simply is the full natural search space: search over all pairs of input items (//n<sup>2</sup>// total number of pairs), and spend constant time per pair. We will later in Ch. 5 go over a more efficient algorithm for this problem, though.
  
 Importantly, quadratic time arises from //nested loops// which must each iterate through //O(n)// times. Importantly, quadratic time arises from //nested loops// which must each iterate through //O(n)// times.
  
-=== //O(n<sup>k</sup>)// Cubic and Higher Order Polynomial Time ===+=== Cubic and Higher Order Polynomial Time ===
 We get higher orders of polynomial times from similar situations. For example, we arrive at //O(n<sup>3</sup>)// cubic time by looking through //n// subsets of a length //n// set to see if any are disjoint. This is simply a triple of nested loops (for each pair of sets, search through both looking for a pair), and the same cubic time would be attained by searching a set of planar points for the triplet with closest distance. We get higher orders of polynomial times from similar situations. For example, we arrive at //O(n<sup>3</sup>)// cubic time by looking through //n// subsets of a length //n// set to see if any are disjoint. This is simply a triple of nested loops (for each pair of sets, search through both looking for a pair), and the same cubic time would be attained by searching a set of planar points for the triplet with closest distance.
  
Line 99: Line 99:
  
 === Sublinear Time ===  === Sublinear Time === 
-The most common sublinear time is the logarithmic //O(log n)// time, of which the most famous example is the binary search. Crucially, these typically require a certain pre-existing knowledge of the data set, just as the binary search requires that the data set be sorted ahead of time. So, algorithms like this can often require preprocessing. +Back into comfortable run times, the most common sublinear time is the logarithmic //O(log n)// time, of which the most famous example is the binary search. Crucially, these typically require a certain pre-existing knowledge of the data set, just as the binary search requires that the data set be sorted ahead of time. So, algorithms like this can often require preprocessing. 
  
  
 This was a good section, 9/10. Definitely interesting and explains everything quite well. This was a good section, 9/10. Definitely interesting and explains everything quite well.
  
 +===== 2.5: More Complex Data Structure: Priority Queues =====
 +As we know, a priority queue (PQ) is one that maintains a set of elements, each of which has an associated numeric value, or key. The smaller this key, the higher the priority. These support insertion and deletion and selection of element with smallest key. As is shown in the book, a sequence of //O(n)// priority queue operations can be used to sort a list of //n// numbers. So, if we can get each PQ operation to work in //O(log n)// time, then we will have an implementation of sorting in //O(n log n)// time.
  
 +This PQ implementation takes the form of a //heap//. A heap is a binary tree whose keys are in //heap order//, meaning that the values of each node's children are greater than or equal to the parent node's value. An easy implementation of this is an array, where we call the first index 1 (which is the root of the heap), and then define for each parent node //i//, its left child is at //2i// and its right child at //2i + 1//.
  
 +To implement the heap operations, we define ''Heapify-up'' and ''Heapify-down''. The former operates for a given node by checking if its parent's value is greater than its own, and if so, swapping the two. This is then repeated recursively until the condition is met or the value makes its way to the heap root. Heapify-down work's similarly, just by checking if the given node's value is greater than either of its children's values, and if so, swapping with the "smaller" of the children. Then this is done recursively down. Both of these guarantee the heap order and balance is kept upon insertion and deletion in //O(log n)// time due to a particular bit of cleverness: we only add or delete from the very last spot in the array. Then, to keep it balanced, say, for deletion of an arbitrary spot on the left side of the heap, we simply swap that deleted value with the value in the last spot, delete the one that was to be deleted, and then let the one from the end that we swapped into its location Heapify-up/down to its rightful place.
 +
 +=== Implementing PQs with Heaps ===
 +Initializing the PQ array runs at most //O(N)//, where //N// is a constraint on the input size by being the size of the array. Due to the above Heapify operations, we can then insert and delete any element in //O(log n)// time. Finding the minimum value then takes //O(1)// time because it is simply the root of the heap--always the first element in our array. Thus, extracting the minimum value simply means removing that minimum value, therefore takes //O(log n)// time. 
 +
 +So, as mentioned in our goal, we have found an implementation of the PQ for which each operation is //O(log n)//. So, we can then use this to implement a sort in //O(n log n)// time. 
 +
 +This was a good section which complemented class very well. They seemed to get a little more caught in notation which led to some confusion at times, but still a 8/10 in my book.
courses/cs211/winter2018/journals/beckg/ch2.1517272546.txt.gz · Last modified: by beckg
CC Attribution-Noncommercial-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0