1 CS Review, iClicker -Questions Week 15
ANY QUESTIONS? 2
Decision Tree 3
Trees can be more complex Tree = [‘Root’, ‘Leaf1’, ‘Leaf2’, [‘Node1’, ‘Leaf3’, ‘Leaf4’, ‘Leaf5’]] Root Leaf1Leaf2 Leaf3 Leaf4Leaf5 Node1
Trees can be more complex Tree = [‘Root’, ‘Leaf1’, ‘Leaf2’, [‘Node1’, ‘Leaf3’, ‘Leaf4’, ‘Leaf5’]] Root Leaf1Leaf2 Leaf3 Leaf4Leaf5 Node1
Trees can be more complex Tree = [‘Root’, ‘Leaf1’, ‘Leaf2’, [‘Node1’, ‘Leaf3’, ‘Leaf4’, ‘Leaf5’]] Root Leaf1Leaf2 Leaf3 Leaf4Leaf5 Node1
Indices allow us to “traverse” the tree Root Leaf1 Leaf2 Leaf3Leaf4 Leaf6 Tree = [‘Root’, [‘Node1’, ‘Leaf0’, ‘Leaf1’], ‘Leaf2’, [‘Node2’, ‘Leaf3’, ‘Leaf4’, [‘Node3’, ‘Leaf5’, ‘Leaf6’]]] Leaf0 Leaf5 [0] [3][1] [3][3][1] [1][1][1][2] [1] [3][2] [3][3][2] [2] [3][3] [3] [1][0] [3][0] [3][3][0] Node1 Node2 Node3
Recursion 8
11 Recursion : String Reversal def reverse(s): if s == "": return s else: return reverse(s[1:]) + s[0] >>> reverse("Hello") 'olleH'
Big-O Notation 12
Big-O Notation
Why Size Matters?
CQ: Arithmetic Series
CQ: Series If it is an arithmetic series : ½ n (n+1)….. T(n) is O(n 2 )
Clicker Question def getFirst(list): if len(list) == 0: return -1 return (list[0]) A: O(n) B: O(n 2 ) C: O(1) >>> getFirst([]) >>> getFirst([0,1,2,3]) 0 >>> getFirst(["a", "b", "c"]) 'a’
Clicker Question def getFirst(list): if len(list) == 0: return -1 return (list[0]) A: O(n) B: O(n 2 ) C: O(1) >>> getFirst([]) >>> getFirst([0,1,2,3]) 0 >>> getFirst(["a", "b", "c"]) 'a’
Sorting Arrays 19
How to find an alien Logic Puzzle: You have 9 marbles. 8 marbles weigh 1 ounce each, & one marble weighs 1.5 ounces. You are unable to determine which is the heavier marble by looking at them. How do you find the marble which weighs more?
Solution 1: Weigh one marble vs another What is the complexity of this solution?
Clicker Question: What is the complexity of this algorithm? A: O(n) B: O(n 2 ) C: O(1) D: O(log n)
Finding the complexity of the optimal solution Step 1: What is our input? The marbles Step 2: How much work do we do per marble? LOGARITHMIC Step 3: What is the total work we did? 2 measurements What if we had 100 marbles or 1000?
Clicker Question: What is the complexity of this algorithm? A: O(n) B: O(n 2 ) C: O(1) D: O(log n)
Big-O – Sorting Arrays Type of SortBestWorstAverageMemory Space Bubble SortO(N)O(N 2 ) 1 Selection Sort O(N 2 ) 1 Heap Sort O(N log N) 1 Merge SortO(N log N) Worst =N
Two Phases using a Priority Queue 1. Put all items in the input list into a priority queue 2. Extract items by decreasing magnitude and put them into the output list We get a sorted list that way [3,7,2,9,… [9,8,7,6,…
Example Tree, Encoding & Access Tree encoding: a = [9, 6, 4, 5, 1, 2] :1: 0: 3:4:5: 6: Find Parent of: 4 a[2]=4 i=(2-1)//2 = 0 Parent is at a[0] =9
Heap Representation
Step- 1 Step-3 Step- 2 Heap- Insert
Tree-Complexity
CQ: Is this a Priority Queue? A. Yes B. No :1: 0: 3:4:5: 6: x yz ≥≥
CQ: Is this a Priority Queue? A. Yes B. No :1: 0: 3:4:5: 6: x yz ≥≥
CQ: Is this a priority queue? [9,8,4,7,3,2,5,6,1] A. Yes B. No
CQ: Is this a priority queue? [9,8,4,7,3,2,5,6,1] A. Yes B. No Tree encoding: a = [9,8,4,7,3,2,5,6,1] Find Parent of: 5 a[6]=5 i=(6-1)//2 =2 Parent is at a[2] =4
CQ: how many children for L[5]? [9,7,4,6,5,4,2,2,1,1,1,1] A. 0 B. 1 C. 2
CQ: how many children for L[5]? [9,7,4,6,5,4,2,2,1,1,1,1] A. 0 B. 1 C
Merge Sort log(n) n elements merged
Putting it all together We know that there are log(n) splits At each “level” we split each list in two We know that we need to merge a total of n elements at each “level” n * log(n) thus O(n log n)