Posts

Showing posts from October, 2026

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...