Download - Announcements: Project 5
![Page 1: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/1.jpg)
Announcements: Project 5Everything we have been learning thus far will
enable us to solve interesting problems Project 5 will focus on applying the skills we
have learned on a problem from biology, specifically computational biology
Email your group / group requests by FridayProject 5 will be released tomorrow late
afternoon
![Page 2: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/2.jpg)
AnnouncementsMidterm 2: Same curve as Midterm 1Pre Lab 15 will be released next FridayStale version of week 12 slides was accidently
uploaded to the wiki (correct slides were presented in class) – this has been fixed, please re download
![Page 3: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/3.jpg)
O(1) Exampledef isOdd(list): return (len(list)%2 == 1)
>>> isOdd([0])True>>> isOdd([0,1])False
![Page 4: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/4.jpg)
Clicker Questiondef getFirst(list): if len(list) == 0: return -1 return (list[0])
A: O(n) B: O(n2)C: O(1)
>>> getFirst([])-1>>> getFirst([0,1,2,3])0>>> getFirst(["a", "b", "c"])'a’
![Page 5: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/5.jpg)
Building the IntuitionLogic 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?
![Page 6: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/6.jpg)
Solution 1: Weigh one marble vs another
What is the complexity of this solution?
![Page 7: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/7.jpg)
Finding the complexityStep 1: What is our input?
The marblesStep 2: How much work do we do per marble?
We weight each marble once (except one)Step 3: What is the total work we did?
8 measurementsWhat if we had 100 marbles or 1000?
![Page 8: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/8.jpg)
Clicker Question: What is the complexity of this
algorithm?
A: O(n) B: O(n2)C: O(1) D: O(log n)
![Page 9: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/9.jpg)
We can do better!Lets pull some intuition from our search
algorithm that was O(log n)We want a way to eliminated ½ (or more) of the
marbles with each measurementHow might we do this?
What about weighing multiple marbles at once?
![Page 10: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/10.jpg)
The Optimal SolutionSplit the marbles into three groups
We can then weigh two of the groups
![Page 11: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/11.jpg)
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 measurementsWhat if we had 100 marbles or 1000?
![Page 12: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/12.jpg)
What happens at each step?
We eliminated 2/3rds of the marbles
![Page 13: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/13.jpg)
Clicker Question: What is the complexity of this
algorithm?
A: O(n) B: O(n2)C: O(1)D: O(log n)
![Page 14: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/14.jpg)
SortingMotivation
We can answer questions like min/max very efficiently
We can search very efficientlyWhat if we need to search many many times
Many algorithms require their input to be sorted
![Page 15: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/15.jpg)
Intuition behind bubble sort
Background reading (homework):http://en.wikipedia.org/wiki/Bubble_sort
Bubble sort takes a list and returns a sorted listCompare each pair of adjacent items and swap
them if the one to the right is smaller Assumption: we want our list sorted from smallest to
greatest
![Page 16: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/16.jpg)
Intuition behind bubble sort
[5, 7, 9, 0, 3, 5, 6][5, 7, 9, 0, 3, 5, 6][5, 7, 9, 0, 3, 5, 6]
[5, 7, 9, 0, 3, 5, 6]
[5, 7, 0, 9, 3, 5, 6]
[5, 7, 0, 3, 9, 5, 6]
[5, 7, 0, 3, 5, 9, 6]
[5, 7, 0, 3, 5, 6, 9]
![Page 17: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/17.jpg)
Intuition behind bubble sort
[5, 7, 0, 3, 5, 6, 9]
[5, 7, 0, 3, 5, 6, 9]
[5, 7, 0, 3, 5, 6, 9]
[5, 0, 7, 3, 5, 6, 9][5, 0, 3, 7, 5, 6, 9]
[5, 0, 3, 5, 7, 6, 9]
[5, 0, 3, 5, 6, 7, 9][5, 0, 3, 5, 6, 7, 9]
![Page 18: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/18.jpg)
Intuition behind bubble sort
How many passes do we have to do before we are guaranteed the list is sorted?n passes, where n is the length of the list
In each pass we do how much work?n-1 comparisons
What is the total work? Complexity?
![Page 19: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/19.jpg)
Changing our Intuition into Code
def BubbleSort(myList): swapped = True while swapped: swapped = False for i in range(len(myList)-1): if myList[i] > myList[i+1]: temp = myList[i] myList[i] = myList[i+1] myList[i+1] = temp swapped = True return myList
The main loop
Keep executing theloop IF we do a swap
![Page 20: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/20.jpg)
Changing our Intuition into Code
def BubbleSort(myList): swapped = True while swapped: swapped = False for i in range(len(myList)-1): if myList[i] > myList[i+1]: temp = myList[i] myList[i] = myList[i+1] myList[i+1] = temp swapped = True return myList
The loop that executes the swaps
![Page 21: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/21.jpg)
Changing our Intuition into Code
def BubbleSort(myList): swapped = True while swapped: swapped = False for i in range(len(myList)-1): if myList[i] > myList[i+1]: temp = myList[i] myList[i] = myList[i+1] myList[i+1] = temp swapped = True return myList
Check if the twonumbersshould be swapped
![Page 22: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/22.jpg)
Changing our Intuition into Code
def BubbleSort(myList): swapped = True while swapped: swapped = False for i in range(len(myList)-1): if myList[i] > myList[i+1]: temp = myList[i] myList[i] = myList[i+1] myList[i+1] = temp swapped = True return myList
Swap!
![Page 23: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/23.jpg)
Fast Swapping of Two Variables
Python provides us the ability to perform the swap in a much more efficient manner
>>> a = 5>>> b = 7>>> a, b = b, a>>> print a7>>> print b5
variable1, variable 2 = variable2, variable1
![Page 24: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/24.jpg)
Changing our Intuition into Code
def BubbleSort(myList): swapped = True while swapped: swapped = False for i in range(len(myList)-1): if myList[i] > myList[i+1]: myList[i], myList[i+1] =
myList[i+1], myList[i] swapped = True return myList
Swap!
![Page 25: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/25.jpg)
HomeworkReach Chapter 11 from the text book
![Page 26: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/26.jpg)
Can we sort faster?Bubble sort certainly will sort our data for us
Unfortunately it simply is not fast enoughWe can sort faster!
There are algorithms which sort in O(n log n) or log linear time
Lets reason why this is the case
![Page 27: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/27.jpg)
Observation 1: We can merge two sorted lists in
linear timeWhat is in the input?
Both the lists, n = total amount of elementsWhy is the complexity linear?
We must examine each element in each of the lists Its linear in the total amount of elements
O(len(list1) + len(list2)) = O(n)
![Page 28: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/28.jpg)
Observation 1: We can merge two sorted lists in
linear time[5,9,10, 100, 555]
[3,4,12, 88, 535]
[3]
![Page 29: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/29.jpg)
Observation 1: We can merge two sorted lists in
linear time[5,9,10, 100, 555]
[3,4,12, 88, 535]
[3, 4]
![Page 30: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/30.jpg)
Observation 1: We can merge two sorted lists in
linear time[5,9,10, 100, 555]
[3,4,12, 88, 535]
[3,4,5]
![Page 31: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/31.jpg)
Observation 1: We can merge two sorted lists in
linear time[5,9,10, 100, 555]
[3,4,12, 88, 535]
[3,4,5,9]
![Page 32: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/32.jpg)
Observation 1: We can merge two sorted lists in
linear time[5,9,10, 100, 555]
[3,4,12, 88, 535]
[3,4,5,9,10]
![Page 33: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/33.jpg)
Observation 1: We can merge two sorted lists in
linear time[5,9,10, 100, 555]
[3,4,12, 88, 535]
[3,4,5,9,10,12]
![Page 34: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/34.jpg)
Observation 1: We can merge two sorted lists in
linear time[5,9,10, 100, 555]
[3,4,12, 88, 535]
[3,4,5,9,10,12, 88]
![Page 35: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/35.jpg)
Observation 2Notice that merging two lists of length one ends
up producing a sorted list of length two
[5]
[3][3]
[5]
[3][3,5]
[5]
[3][3,5]
![Page 36: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/36.jpg)
Lets build the intuition for Merge-Sort
We know we can merge sorted lists in linear time
We know that merging two lists of length one results in a sorted list of length two
Lets split our unsorted list into a bunch of lists of length one and merge them into progressively bigger lists!We split a list into two smaller lists of equal partsKeep splitting until we have lists of length one
![Page 37: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/37.jpg)
Visual Representation
log(n)n elementsmerged
![Page 38: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/38.jpg)
Putting it all togetherWe know that there are log(n) splits
At each “level” we split each list in twoWe know that we need to merge a total of n
elements at each “level”n * log(n) thus O(n log n)
![Page 39: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/39.jpg)
SynopsisTook a look at the code for bubble sortWe learned an efficient way to swap the
contents of two variables (or list locations)We built the intuition as to why we can sort
faster than quadratic time Introduced the concept of merge sort
![Page 40: Announcements: Project 5](https://reader035.vdocuments.mx/reader035/viewer/2022062521/56816650550346895dd9cf64/html5/thumbnails/40.jpg)
HomeworkStart working on Project 5Play around with the concepts presented in
recitation as well as the pre labThey will help both with the lab AND project 5
Review the project 4 solutionMany of the same concepts will be used in project
5