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 Θ(n2) 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 deepest children. once we hit a root with no parent, we are at the full tree height. Note that height counts edges from deepest child to the root.

Decrease and conquer has three varaitions. 

  1. Decrease by constant where we shring by one steap each time, like in topological sorting. 
  2. Decrease by constant factor, e.g. binary search, where we "throw away" half the data in each step.
  3. Decrease by variable size, like insertion to a binary search tree because the amount varies based on the value being inserted and shape of the tree.
We also reviewed Kahn's algorithm for topological sorting and applied this in the homework assignment.

Transform-and-conquer example studied was presorting. For example, taking some raw input and sorting it first to make the problem easier to solve. Example problems are checking element uniqueness and preparing data into a binary search tree, where we can apply earlier algorithms. 

Comments

Popular posts from this blog

week 15-16/100

week 8/100

CST 334 Week 3 (27/100)