CS321 Spring 2016 Lecture 3 Jan 19 2016. Admin A1 Due this Friday – 11:00PM Thursday = Recurrence...
-
Upload
annis-hubbard -
Category
Documents
-
view
212 -
download
0
description
Transcript of CS321 Spring 2016 Lecture 3 Jan 19 2016. Admin A1 Due this Friday – 11:00PM Thursday = Recurrence...
![Page 1: CS321 Spring 2016 Lecture 3 Jan 19 2016. Admin A1 Due this Friday – 11:00PM Thursday = Recurrence Equations – Important. Everyone Should be added to class.](https://reader037.fdocuments.in/reader037/viewer/2022083120/5a4d1b5a7f8b9ab0599aaca8/html5/thumbnails/1.jpg)
CS321 Spring 2016
Lecture 3Jan 19 2016
![Page 2: CS321 Spring 2016 Lecture 3 Jan 19 2016. Admin A1 Due this Friday – 11:00PM Thursday = Recurrence Equations – Important. Everyone Should be added to class.](https://reader037.fdocuments.in/reader037/viewer/2022083120/5a4d1b5a7f8b9ab0599aaca8/html5/thumbnails/2.jpg)
Admin
• A1 Due this Friday – 11:00PM• Thursday = Recurrence Equations – Important.• Everyone Should be added to class.
![Page 3: CS321 Spring 2016 Lecture 3 Jan 19 2016. Admin A1 Due this Friday – 11:00PM Thursday = Recurrence Equations – Important. Everyone Should be added to class.](https://reader037.fdocuments.in/reader037/viewer/2022083120/5a4d1b5a7f8b9ab0599aaca8/html5/thumbnails/3.jpg)
Course in 4 Sections
• Section I: Sorting, Recurrence, Basics• Section II: Hash Tables and Basic Data Structs• Section III: Binary Search Trees• Section IV: Graphs
![Page 4: CS321 Spring 2016 Lecture 3 Jan 19 2016. Admin A1 Due this Friday – 11:00PM Thursday = Recurrence Equations – Important. Everyone Should be added to class.](https://reader037.fdocuments.in/reader037/viewer/2022083120/5a4d1b5a7f8b9ab0599aaca8/html5/thumbnails/4.jpg)
Section I
• Sorting methods and Data Structures• Algorithm Analysis: recurrence equations.• Start with Heap Sort
![Page 5: CS321 Spring 2016 Lecture 3 Jan 19 2016. Admin A1 Due this Friday – 11:00PM Thursday = Recurrence Equations – Important. Everyone Should be added to class.](https://reader037.fdocuments.in/reader037/viewer/2022083120/5a4d1b5a7f8b9ab0599aaca8/html5/thumbnails/5.jpg)
Basic Computing Problem
• Sort a list of numbers in the quickest time• A=[1,9,2,8,4,5,0,3,6,7]For i = 0 to 9:
for j = i to 9:if (A[j] < A[i] ) swap(A,i,j);
What is the runtime of this?
![Page 6: CS321 Spring 2016 Lecture 3 Jan 19 2016. Admin A1 Due this Friday – 11:00PM Thursday = Recurrence Equations – Important. Everyone Should be added to class.](https://reader037.fdocuments.in/reader037/viewer/2022083120/5a4d1b5a7f8b9ab0599aaca8/html5/thumbnails/6.jpg)
Answer O(n^2)
• Very simple sorting algorithm takes O(n^2).
• Can we do better?• If the world were magic what is the best we
could do?– O(n)
• So should be able to do better than O(n^2)
![Page 7: CS321 Spring 2016 Lecture 3 Jan 19 2016. Admin A1 Due this Friday – 11:00PM Thursday = Recurrence Equations – Important. Everyone Should be added to class.](https://reader037.fdocuments.in/reader037/viewer/2022083120/5a4d1b5a7f8b9ab0599aaca8/html5/thumbnails/7.jpg)
Heapsort
• A sorting algorithm that uses a specific type of data structure: Max-Heap
• Has a worst case and best case performance of Θ(n*log(n)).
• Point1: Choice of data structure critical for algorithm performance.
• Point2: Additional example of Big-O analysis.
![Page 8: CS321 Spring 2016 Lecture 3 Jan 19 2016. Admin A1 Due this Friday – 11:00PM Thursday = Recurrence Equations – Important. Everyone Should be added to class.](https://reader037.fdocuments.in/reader037/viewer/2022083120/5a4d1b5a7f8b9ab0599aaca8/html5/thumbnails/8.jpg)
Components.
• Input: list of N numbers stored in an array. Do not know the order of the numbers.
• Desired Output: the numbers sorted smallest to largest.
• Data structure: Max-Heap• Algorithm: Heapsort.
![Page 9: CS321 Spring 2016 Lecture 3 Jan 19 2016. Admin A1 Due this Friday – 11:00PM Thursday = Recurrence Equations – Important. Everyone Should be added to class.](https://reader037.fdocuments.in/reader037/viewer/2022083120/5a4d1b5a7f8b9ab0599aaca8/html5/thumbnails/9.jpg)
Max-Heap
![Page 10: CS321 Spring 2016 Lecture 3 Jan 19 2016. Admin A1 Due this Friday – 11:00PM Thursday = Recurrence Equations – Important. Everyone Should be added to class.](https://reader037.fdocuments.in/reader037/viewer/2022083120/5a4d1b5a7f8b9ab0599aaca8/html5/thumbnails/10.jpg)
Heap Properties
![Page 11: CS321 Spring 2016 Lecture 3 Jan 19 2016. Admin A1 Due this Friday – 11:00PM Thursday = Recurrence Equations – Important. Everyone Should be added to class.](https://reader037.fdocuments.in/reader037/viewer/2022083120/5a4d1b5a7f8b9ab0599aaca8/html5/thumbnails/11.jpg)
Max-Heap in an Array
![Page 12: CS321 Spring 2016 Lecture 3 Jan 19 2016. Admin A1 Due this Friday – 11:00PM Thursday = Recurrence Equations – Important. Everyone Should be added to class.](https://reader037.fdocuments.in/reader037/viewer/2022083120/5a4d1b5a7f8b9ab0599aaca8/html5/thumbnails/12.jpg)
Maintain Heap Method
2i2i + 1
![Page 13: CS321 Spring 2016 Lecture 3 Jan 19 2016. Admin A1 Due this Friday – 11:00PM Thursday = Recurrence Equations – Important. Everyone Should be added to class.](https://reader037.fdocuments.in/reader037/viewer/2022083120/5a4d1b5a7f8b9ab0599aaca8/html5/thumbnails/13.jpg)
Maintain Heap Example
![Page 14: CS321 Spring 2016 Lecture 3 Jan 19 2016. Admin A1 Due this Friday – 11:00PM Thursday = Recurrence Equations – Important. Everyone Should be added to class.](https://reader037.fdocuments.in/reader037/viewer/2022083120/5a4d1b5a7f8b9ab0599aaca8/html5/thumbnails/14.jpg)
Run time Max Heapify A[i]
O(9) + O(max-heapify (2n/3))
![Page 15: CS321 Spring 2016 Lecture 3 Jan 19 2016. Admin A1 Due this Friday – 11:00PM Thursday = Recurrence Equations – Important. Everyone Should be added to class.](https://reader037.fdocuments.in/reader037/viewer/2022083120/5a4d1b5a7f8b9ab0599aaca8/html5/thumbnails/15.jpg)
Runtime Max HeapifyO(9) + O(max-heapify (2n/3))
O(9) + O(9) + O(max-heapify (4n/9))
O(9) + O(9) + O(9) + O(max-heapify(8n/27))
So, how many times can we divide N by 2: N = 2h , h = log(N).
So, run time for Max Heapify = log(N)*O(9) = O(logN)
![Page 16: CS321 Spring 2016 Lecture 3 Jan 19 2016. Admin A1 Due this Friday – 11:00PM Thursday = Recurrence Equations – Important. Everyone Should be added to class.](https://reader037.fdocuments.in/reader037/viewer/2022083120/5a4d1b5a7f8b9ab0599aaca8/html5/thumbnails/16.jpg)
Build a Max-Heap
![Page 17: CS321 Spring 2016 Lecture 3 Jan 19 2016. Admin A1 Due this Friday – 11:00PM Thursday = Recurrence Equations – Important. Everyone Should be added to class.](https://reader037.fdocuments.in/reader037/viewer/2022083120/5a4d1b5a7f8b9ab0599aaca8/html5/thumbnails/17.jpg)
Heapsort
![Page 18: CS321 Spring 2016 Lecture 3 Jan 19 2016. Admin A1 Due this Friday – 11:00PM Thursday = Recurrence Equations – Important. Everyone Should be added to class.](https://reader037.fdocuments.in/reader037/viewer/2022083120/5a4d1b5a7f8b9ab0599aaca8/html5/thumbnails/18.jpg)
Next lecture
• Solving Recurrence equations