380 likes | 481 Views
CS 17700 Review, iClicker -Questions Week 15. ANY QUESTIONS?. Decision Tree. Trees can be more complex. Root. Node1. Leaf1. Leaf2. Leaf3. Leaf4. Leaf5. Tree = [‘Root’, ‘Leaf1 ’, ‘Leaf2’, [‘Node1’, ‘Leaf3’, ‘Leaf4’, ‘Leaf5’]]. Trees can be more complex. Root. Node1. Leaf1.
E N D
Trees can be more complex Root Node1 Leaf1 Leaf2 Leaf3 Leaf4 Leaf5 Tree = [‘Root’, ‘Leaf1’, ‘Leaf2’, [‘Node1’, ‘Leaf3’, ‘Leaf4’, ‘Leaf5’]]
Trees can be more complex Root Node1 Leaf1 Leaf2 Leaf3 Leaf4 Leaf5 Tree = [‘Root’, ‘Leaf1’, ‘Leaf2’, [‘Node1’, ‘Leaf3’, ‘Leaf4’, ‘Leaf5’]]
Trees can be more complex Root Node1 Leaf1 Leaf2 Leaf3 Leaf4 Leaf5 Tree = [‘Root’, ‘Leaf1’, ‘Leaf2’, [‘Node1’, ‘Leaf3’, ‘Leaf4’, ‘Leaf5’]]
Indices allow us to “traverse” the tree Root [0] [1] [3] Node2 [3][0] [2] Node1 [1][0] Leaf2 [3][3] Node3 [1][1] [1][2] [3][1] [3][2] [3][3][0] Leaf0 Leaf1 Leaf3 Leaf4 [3][3][1] [3][3][2] Leaf5 Leaf6 Tree = [‘Root’, [‘Node1’, ‘Leaf0’, ‘Leaf1’], ‘Leaf2’, [‘Node2’, ‘Leaf3’, ‘Leaf4’, [‘Node3’, ‘Leaf5’, ‘Leaf6’]]]
Recursion : String Reversal • def reverse(s):if s == "":returnselse: returnreverse(s[1:]) + s[0] • >>> reverse("Hello")'olleH'
CQ: Arithmetic Series Let . Then • T(n) is O(n) • T(n) is O(n2) • T(n) is O(n3)
CQ: Series Let . Then • T(n) is O(n) • T(n) is O(n2) • T(n) is O(n3) • If it is an arithmetic series: • ½ n (n+1)….. • T(n) is O(n2)
Clicker Question defgetFirst(list): iflen(list) == 0: return -1 return (list[0]) >>> getFirst([]) -1 >>> getFirst([0,1,2,3]) 0 >>> getFirst(["a", "b", "c"]) 'a’ A: O(n) B: O(n2) C: O(1)
Clicker Question defgetFirst(list): iflen(list) == 0: return -1 return (list[0]) >>> getFirst([]) -1 >>> getFirst([0,1,2,3]) 0 >>> getFirst(["a", "b", "c"]) 'a’ A: O(n) B: O(n2) C: O(1)
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(n2) 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(n2) C: O(1) D: O(log n)
Two Phases using a Priority Queue • Put all items in the input list into a priority queue • 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 0: 9 1: 2: 6 4 Tree encoding: a = [9, 6, 4, 5, 1, 2] 6: 3: 4: 5: 1 5 2 • Find Parent of: 4 • a[2]=4 • i=(2-1)//2 = 0 • Parent is at a[0] =9 Access Mappings: Parent to left child: Parent to right child: Child to parent:
Heap- Insert Step- 1 Step- 2 Step-3
CQ: Is this a Priority Queue? • Yes • No 0: 9 1: 2: 6 4 6: 3: 4: 5: 4 5 2 x ≥ ≥ y z
CQ: Is this a Priority Queue? • Yes • No 0: 9 1: 2: 6 4 6: 3: 4: 5: 4 5 2 x ≥ ≥ y z
CQ: Is this a priority queue? [9,8,4,7,3,2,5,6,1] • Yes • No
CQ: Is this a priority queue? [9,8,4,7,3,2,5,6,1] • Yes • 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] • 0 • 1 • 2
CQ: how many children for L[5]? [9,7,4,6,5,4,2,2,1,1,1,1] • 0 • 1 • 2 9 7 4 5 4 2 6 2 1 1 1 1 Access Mappings: Parent to left child: Parent to right child: Child to parent:
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)