Download presentation
Presentation is loading. Please wait.
Published byCory Webster Modified over 8 years ago
1
1 CS 17700 Review, iClicker -Questions Week 15
2
ANY QUESTIONS? 2
3
Decision Tree 3
4
Trees can be more complex Tree = [‘Root’, ‘Leaf1’, ‘Leaf2’, [‘Node1’, ‘Leaf3’, ‘Leaf4’, ‘Leaf5’]] Root Leaf1Leaf2 Leaf3 Leaf4Leaf5 Node1
5
Trees can be more complex Tree = [‘Root’, ‘Leaf1’, ‘Leaf2’, [‘Node1’, ‘Leaf3’, ‘Leaf4’, ‘Leaf5’]] Root Leaf1Leaf2 Leaf3 Leaf4Leaf5 Node1
6
Trees can be more complex Tree = [‘Root’, ‘Leaf1’, ‘Leaf2’, [‘Node1’, ‘Leaf3’, ‘Leaf4’, ‘Leaf5’]] Root Leaf1Leaf2 Leaf3 Leaf4Leaf5 Node1
7
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
8
Recursion 8
11
11 Recursion : String Reversal def reverse(s): if s == "": return s else: return reverse(s[1:]) + s[0] >>> reverse("Hello") 'olleH'
12
Big-O Notation 12
13
Big-O Notation
14
Why Size Matters?
15
CQ: Arithmetic Series
16
CQ: Series If it is an arithmetic series : ½ n (n+1)….. T(n) is O(n 2 )
17
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’
18
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’
19
Sorting Arrays 19
20
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?
21
Solution 1: Weigh one marble vs another What is the complexity of this solution?
22
Clicker Question: What is the complexity of this algorithm? A: O(n) B: O(n 2 ) C: O(1) D: O(log n)
23
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?
24
Clicker Question: What is the complexity of this algorithm? A: O(n) B: O(n 2 ) C: O(1) D: O(log n)
25
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
26
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,…
27
Example Tree, Encoding & Access Tree encoding: a = [9, 6, 4, 5, 1, 2] 9 64 52 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
28
Heap Representation
29
Step- 1 Step-3 Step- 2 Heap- Insert
30
Tree-Complexity
31
CQ: Is this a Priority Queue? A. Yes B. No 9 64 52 4 2:1: 0: 3:4:5: 6: x yz ≥≥
32
CQ: Is this a Priority Queue? A. Yes B. No 9 64 52 4 2:1: 0: 3:4:5: 6: x yz ≥≥
33
CQ: Is this a priority queue? [9,8,4,7,3,2,5,6,1] A. Yes B. No
34
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
35
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
36
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 9 74 5 6 42 2 1 1 1 1
37
Merge Sort log(n) n elements merged
38
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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.