1 / 38

CS 17700 Review, iClicker -Questions Week 15

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.

neona
Download Presentation

CS 17700 Review, iClicker -Questions Week 15

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 17700 Review, iClicker-Questions Week 15

  2. ANY QUESTIONS?

  3. Decision Tree

  4. Trees can be more complex Root Node1 Leaf1 Leaf2 Leaf3 Leaf4 Leaf5 Tree = [‘Root’, ‘Leaf1’, ‘Leaf2’, [‘Node1’, ‘Leaf3’, ‘Leaf4’, ‘Leaf5’]]

  5. Trees can be more complex Root Node1 Leaf1 Leaf2 Leaf3 Leaf4 Leaf5 Tree = [‘Root’, ‘Leaf1’, ‘Leaf2’, [‘Node1’, ‘Leaf3’, ‘Leaf4’, ‘Leaf5’]]

  6. Trees can be more complex Root Node1 Leaf1 Leaf2 Leaf3 Leaf4 Leaf5 Tree = [‘Root’, ‘Leaf1’, ‘Leaf2’, [‘Node1’, ‘Leaf3’, ‘Leaf4’, ‘Leaf5’]]

  7. 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’]]]

  8. Recursion

  9. Recursion

  10. Recursion

  11. Recursion : String Reversal • def reverse(s):if s == "":returnselse: returnreverse(s[1:]) + s[0] • >>> reverse("Hello")'olleH'

  12. Big-ONotation

  13. Big-O Notation

  14. Why Size Matters?

  15. CQ: Arithmetic Series Let . Then • T(n) is O(n) • T(n) is O(n2) • T(n) is O(n3)

  16. 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)

  17. 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)

  18. 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)

  19. Sorting Arrays

  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(n2) 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(n2) C: O(1) D: O(log n)

  25. Big-O – Sorting Arrays

  26. 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,…

  27. 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:

  28. Heap Representation

  29. Heap- Insert Step- 1 Step- 2 Step-3

  30. Tree-Complexity

  31. CQ: Is this a Priority Queue? • Yes • No 0: 9 1: 2: 6 4 6: 3: 4: 5: 4 5 2 x ≥ ≥ y z

  32. CQ: Is this a Priority Queue? • Yes • No 0: 9 1: 2: 6 4 6: 3: 4: 5: 4 5 2 x ≥ ≥ y z

  33. CQ: Is this a priority queue? [9,8,4,7,3,2,5,6,1] • Yes • No

  34. 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

  35. CQ: how many children for L[5]? [9,7,4,6,5,4,2,2,1,1,1,1] • 0 • 1 • 2

  36. 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:

  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)

More Related