Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithm Fundamentals Shane Carr CSE 232, Fall 2015.

Similar presentations


Presentation on theme: "Algorithm Fundamentals Shane Carr CSE 232, Fall 2015."— Presentation transcript:

1 Algorithm Fundamentals Shane Carr CSE 232, Fall 2015

2 Five Strategies Based on those in CtCI Brute Force and Fix Base Case and Build Simplify and Generalize Reduce and Relate Do It Yourself

3 The Most Important Data Structures NameCommentsPythonJava Normal Array Keeps an ordering between items. Beware of slow operations. [ a, b, c ]ArrayList (Hash) SetMost operations are O(1). No ordering and no duplicates. Supports operations like “union” and “intersect” in O(n). { a, b, c }HashSet Priority Queue O(log n) to add. O(log n) to remove the minimum element. PriorityQueue FIFOO(1) to push or pop an element.Queue DictionaryLike (hash) set, but uses key-value pairs instead of just the values. { a: 1, b: 2 } HashMap

4 Lower Level Optimization Techniques Based on CP3 chapter 3 1.Avoid allocating big data structures in loops or recursive functions. 2.Access 2-D arrays in row-major fashion to reduce cache misses. 3.If you come up with a correct algorithm, but the algorithm is approaching ~100M or more computations, consider writing it in a fast language (rather than Python). 4.In Java, use ArrayList rather than Vector. 5.In Java, use StringBuilder if you need to modify strings. Python users are safe using normal strings.

5 Example 1: n-Queens On a n-by-n chess board, in how many different ways can you arrange n queens such that no queen attacks any other queen? Live Code! This is a classic problem, and you frequently see variations of it in programming competitions.

6 Moving On to More Advanced Algorithms

7 The Most Important Classes of Algorithms NameComments Binary SearchUseful if the solution to a problem is a single number which you can “guess and check.” GreedyUsually yields fast solutions, but you need to be clever and careful. Prone to failing on edge cases. Dynamic Programming The most common class of algorithms I use in real-life programming competitions.

8 Note about D&C  D&C (and Contraction) are great tools in theory classes, and are the basis for a lot of great algorithms:  Merge Sort  Search Trees  Closest Pair  Maximal Independent Set  However, in real life, you don’t frequently use D&C straight-up.  Binary Search is a subclass of D&C.

9 Should you use top-down or bottom-up DP? Top-Down DP 1.Easy to implement on top of brute force recursion 2.Computes sub-problems only when necessary 3.Deep recursion stack can cause problems 4.The full DP table is always stored in memory Bottom-Up DP 1.Can be less intuitive to come up with, but more elegant once you do 2.Always visits all states 3.Less overhead from recursive function calls 4.Stronger dependency structure means that you can discard old DP entries

10 Example 2: Coin Change At a convenience store, you buy a bag of chips and need C cents in change. The clerk has infinitely many coins in k different denominations, denoted S = {s 1, s 2, …, s k }. What is the fewest number of coins the clerk can give you such that you receive exactly C cents? Live Code!


Download ppt "Algorithm Fundamentals Shane Carr CSE 232, Fall 2015."

Similar presentations


Ads by Google