Differences
This shows you the differences between two versions of the page.
courses:cs211:winter2014:journals:colin:chapter2 [2014/01/22 10:19] – created mohnacsc | courses:cs211:winter2014:journals:colin:chapter2 [2014/01/23 06:47] (current) – mohnacsc | ||
---|---|---|---|
Line 4: | Line 4: | ||
+ | === Arrays and Lists === | ||
+ | An array is able to maintain a list of elements either sorted either numerically or alphabetically to allow for O(log n) access via binary search. | ||
- | === xxx === | + | Linked lists sequence elements by having each point to the next in the list. Traversing the list is of time proportional to the length, or O(i). Insertion and deletion in a linked lists requires changing pointers of values. |
+ | |||
+ | |||
+ | === Implementing the Stable Matching Algorithm | ||
+ | |||
+ | In order for the G-S algorithm to run in n^2 time, each iteration | ||
+ | |||
+ | * Identify a free man, m | ||
+ | * Identify m’s most preferred woman, w | ||
+ | * Decide if w is engaged and identify current partner | ||
+ | * Decide if w prefers m to her current partner | ||
+ | |||
+ | This is accomplished by maintaining two arrays for each list of preferences, | ||
+ | |||
+ | Using these arrays allows us to complete each step in constant time, O(1), and the G-S algorithm in O(n^2) time. | ||
+ | |||
+ | |||
+ | === Discussion === | ||
+ | |||
+ | The information in the section is straight-forward and calls for little debate. | ||
+ | |||
+ | |||
+ | |||
+ | ==== 2.4 - A Survey of Common Running Times ==== | ||
+ | |||
+ | |||
+ | **Linear O(n) time**- running time is at most a constant factor times the size of input | ||
+ | |||
+ | Linear time is found in algorithms computing the maximum element of a list or merging two sorted lists - constant work per element. | ||
+ | |||
+ | |||
+ | **O(n log n) time** - running time of any algorithm that splits its input into two equal pieces, solves each piece recursively, | ||
+ | |||
+ | This can be found in the Mergesort algorithm and also when the most expensive step of an algorithm is to sort the input. | ||
+ | |||
+ | |||
+ | **Quadratic O(n^2) time** - running time where a loop with O(n) iterations takes O(n) time for each iteration. | ||
+ | |||
+ | |||
+ | **Cubic time O(n^3)** - running time of more elaborate sets of nested loops with O(n) time | ||
+ | |||
+ | |||
+ | **O(n^k) time** - running time when searching over all subsets of size k | ||
+ | |||
+ | |||
+ | Run times of 2^n and n! grow faster than polynomial functions. | ||
+ | |||
+ | |||
+ | Sublinear time has cases where the running time is asymptotically smaller than linear. | ||
+ | |||
+ | |||
+ | === Discussion === | ||
+ | |||
+ | Upon further reading, I found it easier to compute times by identifying smaller processes within an algorithm and referring back to other algorithms where I can relate the current problem. | ||
+ | |||
+ | |||
+ | |||
+ | ==== 2.5 - A More Complex Data Structure: Priority Queues ==== | ||
+ | |||
+ | |||
+ | === The Problem === | ||
+ | |||
+ | A priority queue is a data structure that maintains a set of elements S, where each element (v in S) has an associated value key v that denotes the priority of element v. Supports addition and deletion. | ||
+ | |||
+ | A sequence of O(n) priority queue operations can be used to sort a set of n numbers in O(n log n) time. | ||
+ | |||
+ | |||
+ | === A Data Structure for Implementing a Priority Queue === | ||
+ | |||
+ | Priority queue is implemented in a heap. A heap is a balanced binary tree with a root and nodes containing up to two children. | ||
+ | |||
+ | |||
+ | === Implementing the Heap Operations === | ||
+ | |||
+ | The procedure Heapify-up fixes the heap property in O(log i) time, assuming that the array H is almost a heap with the key of H[i] too small. | ||
+ | |||
+ | The procedure Heapify-down fixes the heap property in O(log n) time, assuming that H is almost a heap with the key value of H[i] too big. Using Heapify-up or Heapify-down we can delete a new element in a heap of n elements in O(log n) time. | ||
+ | |||
+ | |||
+ | === Discussion === | ||
+ | |||
+ | The heap is a simple tree structure that focuses on the Heapify-up/ | ||
+ | |||
+ |