cs321 spring 2016 lecture 3 jan 19 2016. admin a1 due this friday – 11:00pm thursday = recurrence...

18
CS321 Spring 2016 Lecture 3 Jan 19 2016

Upload: annis-hubbard

Post on 18-Jan-2018

212 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Next lecture

• Solving Recurrence equations