Posts

cst370 week 7

 This week we covered dynamic programming and then moved on to the greedy technique. Both avoid using brute force but in different ways. Dynamic programming was developed in the 1950s, and the programming means planning, not coding. When a problem’s subproblems overlap we record each answer in a table and read the final answer off the table. Fibonacci's naive recursion solution computes F(1) five times just to get F(5), while DP computes each value once to solve fibonacci in Θ(n) instead of exponential time.  The coin row and coin collecting problems make a table of intermediate solutions and then backtrack to find the full optimal solution. Warshall’s algorithm finds the transitive closure by building matrices R(0) to R(n), where R(k) only allows the first k vertices as intermediates. Floyd’s algorithm uses the same structure with a minimum selection criteria to find all-pairs shortest paths. Both Warshall's and Floyd's algorithms run in O(n³).  Greedy is the opposite mi...

cst370 week 6

This week we reviewed four different structures for fast search and updates. We started from learning that a binary search tree that is built from sorted inputs is skewed and is effectively a linked list, so search becomes O(n) instead of O(n log n). AVL trees were introduced that keep a measure of the balance of its subtrees. When a subtree is imbalanced as the result of an insert, we rotate at the imbalanced node closest to the new leaf. The direction of rotation depends on the skew of the new leaf. A 2-3 tree lets a node hold two sorted values and promotes the middle value of an overfull node that comes as a result of an insert. The tree grows at the root and all leaves stay level. Heaps are my favorite binary tree. We studied max heaps, where each parent's value is greater than its children. Getting the max value is O(1) because it's stored at the front of the array (when using array representation). Inserting and removing the max is O(log n). Heapsort is deleting the max n...

CST370 week 5

 This week we dove deeper on something- and-conquer algorithms: divide-and-conquer, decrease-and-conquer, and transform-and-conquer. In all three we reduce the input set as we progress through the algorithm, resulting in O(n log n) or O(log n) algorithms. Quicksort is the most common search algorithm because its average case is Θ(n log n), but has worst case  Θ ( n 2 ) when the list is already sorted. It works by picking a "pivot" and swapping element in place when there is an out of order element on the left and right of the pivot value, searching with two pointers from the begin and end of an array. Binary tree traversals are Θ(n) because they visit each node once. There are three modes of traversal we use. Preorder traverses root → left → right. Inorder traverses left → root → right. Postorder traverses left → right → root. From postorder traversing a binary tree, we can find the height by keeping a counter each time we recurse into a root node from the deepe...

CST370 week 4

 This week was midterm prep but we also studied merge sort. Merge sort analysis is a first real application of using the Master Theorem on a divide and conquer algorithm. Merge sort works by continually dividing the array to be sorted in half, and sorting that half. Then we merge the two sorted halves by advancing a pointer on each half, coping the lowest value of either pointer to the result array and incrementing the pointer, until one half is exhausted, then we append the remainder of the sorted half to the end of the result array. The dividing is easy, and is a constant step = 1, which is the d  component of the master theorem. the a  is 2, because we divide both halves of the array and recurse, and the b  is 2 because it's dividing by 2. I.e. T(n) = 2T(n/2) + Θ(n), which is case 2 of the Master Theorem which means merge sort is Θ(n log n). 

CST370 week 3

 This week we covered some different searching problems. First was brute force methods for finding a substring in a string, the traveling salesman problem, assignment problem, and the knapsack problem. We looked at the high cost of using brute force on combinatorial problems: TSP and assignment problem are factorial and knapsack is exponential. The brute force strategy here is to do an exhaustive search through all potential solutions, either combinations or permutations, and determine the best solution(s) out of all possible answers. We then learned about depth first search. This was again a review of intro to algorithms. We use a stack and a tracking array, and a list of vertices with adjacency lists to search the whole graph in Θ(|V| + |E|) time. If we used an adjacency matrix it would by Θ(|V|²) time. Breadth first search, by contrast, uses a queue instead of a stack. The time complexity is the same as depth first search. The difference then is in how it performs as a sear...

CST370 week 2

 This week started with a review of the time category of algorithms and the three asymptotic notations we use to specify the bounds of an algorithm's time category. Big Omega is used to denote the best case boundary. Big O is used to denote the worst case boundary. Big Theta is used to denote a tight bound, where best and worst is the same time category. The time categories, in ascending order are 1, log n, n, n*log n, n², n³, 2ⁿ, and n!. Given the same amount of data being processed, the further left the faster the algorithm, and the further right the slower the algorithm. We learned about how to identify the category, by dropping all lower order terms and any constant coefficients to get the final category. For example, 5n*log n + 2n +14 is O(n log n). This is more straightforward to analyze in non-recursive algorithms because we count basic operation in loops or nested loops. Recursion doesn't loop per se, so we solve with backwards substitution by counting the basic operati...

CST 370 week 1

Week 1 of CST 370 introduced what an algorithm is: a finite sequence of unambiguous steps that turns a valid input for a problem into the required output. The GCD problem showed why the definition matters, and contrasted Euclid's algorithm against the middle-school procedure, which is not an algorithm because it has ambiguous steps. The pseudocode lecture set the notation for writing pseudocode which is purposefully different from writing actual code, so the logic of the algorithm takes the focus instead of language or system specific implementation details. We should analyze the problem first, then write the algorithm solution in pseudocode, then code. Up next was groundwork on some key problem types and data structures. Problem types covered were sorting, searching, and graphs. We then reviewed graph definitions and representations, and trees as a special type of graphs. Most of this was review from intro to algorithms. The analysis framework picks a basic operation, usually in t...