algorithms and data structures for data science lists 3

29
Department of Computer Science Algorithms and Data Structures for Data Science CS 277 Brad Solomon September 20, 2021 Lists 3

Upload: others

Post on 05-Dec-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

Department of Computer Science

Algorithms and Data Structures for Data Science

CS 277 Brad Solomon

September 20, 2021

Lists 3

reflections on mp_racing

Submission for mp_racing is end of semester

Key concepts: File IO / Conditionals and Loops / Logic

Avg: 51 / 60 (Bonus raised average significantly)

reflections on lab_debug

No student feedback given

Seems to be successful — both in time and score

Avg: 14.67 / 15

reflections on lab_proficiency

Limiting factor is time

Converting 1D to 2D most missed question

Count peaks most answered question

We will continue to move forward on course content

Learning Objectives

Review asymptotic notation

Discuss implementation tradeoffs for lists

Introduce alternative ordered 1D data structures

Big-O notation

n,sizeofinput

operations

is iff suchthat f(n) O(g(n)) ∃c, k f(n) ≤ cg(n) ∀n > k

1) is an upper bound on cg(n) f(n)

2) This is true for all input values larger than some arbitrary k

Big-O Complexity Classes

O(log n)

O(n)

O(n2)

O(2n)

O(1)

count_words_in_linedef count_words_in_line(inFile): countList = []

with open(inFile) as myFile:

for line in myFile: count = 0 line = line.strip() spline = line.split(" ")

for word in spline: word = word.strip() if word: count+=1

countList.append(count)

return countList

1 2 3 4 5 6 7 8 9

10 11 12 13 14 15 16 17 18 19 20 21 22 23

Identifying the Big O of an algorithm

1) Label the key factors that drive algorithm performance

2) Write out the worst-case performance for each step

3) Identify (or reduce to) the largest terms for each factor

count_peaksdef countPeaks(inList): count = 0

for i in range(len(inList)): pv = nv = 0

if i == 0: pv = -1 if i == len(inList)-1: nv = -1 if pv != -1: pv = inList[i-1] if nv != -1: nv = inList[i+1]

cv = inList[i] if cv > pv and cv > nv: count+=1

return count

1 2 3 4 5 6 7 8 9

10 11 12 13 14 15 16 17 18 19 20 21 22 23

convert_1D_to_2Ddef convert_1D_to_2D(inList, rowSize): listLen = len(inList) numRows = math.ceil(listLen/rowSize)

outList = [] count = 0

for i in range(numRows): tempList = []

for j in range(rowSize):

if count >= listLen: tempList.append(-1) else: tempList.append(inList[count])

count+=1

outList.append(tempList)

return outList

1 2 3 4 5 6 7 8 9

10 11 12 13 14 15 16 17 18 19 20 21 22 23

List Functions

Add: Given value, insert value at front of list

Index Look up: Given index, return value at index

Remove: Given value, remove first instance in list

Pop: Given index, remove and return value at index position

Insert: Given value and index, insert value at index position in list

List Implementations

1. Linked List

2. Arrays

Linked List

C S 2 7 7None

look_up()Head

class linkedList:

def look_up(self, pos): curr = self.head

i = 0 while(curr and i < pos): curr = curr.next i+=1 if i == pos: return curr

1 2 3 4 5 6 7 8 9

10 11 12 13

Linked List

C S 2 7 7None

insert()Head

class linkedList:

def insert(self, data, pos=0): if (pos == 0): self.add(data) else: prev = self.index(pos-1)

temp = prev.next

prev.next = Node(data,temp)

1 2 3 4 5 6 7 8 9

10 11 12 13

Linked List

C S 2 7 7None

remove()Head

def remove(self, data): prev = None curr = self.head while(curr): if curr.data == data: if prev == None: self.head = self.head.next break else: prev.next = curr.next break else: prev = curr curr = curr.next

1 2 3 4 5 6 7 8 9

10 11 12 13 14 15 16

Array

C S 2 7 7

look_up()

def look_up(self, pos): return self.array[pos]

1 2 3

Array

def insert(self, data, pos=0): self.array.insert(pos, data)

1 2 3

insert()

Resize Strategy: +2 elements every resize

Resize Strategy: +2 elements every resize

Resize Strategy: x2 elements every resize

Resize Strategy: x2 elements every resize

Observing built-in resizeimport sys

l = [] n = 4

size = sys.getsizeof(l) print("empty list:", size) for i in range(n): l.append(i) nsize = sys.getsizeof(l) print("n={} list size: {}".format(n, nsize))

c = (nsize-size)//8 print("capacity:", c) print("length:", len(l))

1 2 3 4 5 6 7 8 9

10 11 12 13 14 15 16 17 18 19 20 21 22 23

Array

def insert(self, data, pos=0): self.array.insert(pos, data)

1 2 3

C S 2 7 7

insert()

Array

def remove(self, data): self.array.remove(data)

1 2 3

remove()

SinglyLinkedList Array

Lookupgivenaninputposition

Searchgivenaninputvalue

Insert/Removeatfront

Insert/Removeatarbitrarylocation

Insert/Removeatgivenlocation

Array Implementation

Organizing a to-do list

Queue ADTOrder:

Functions:

Runtime:

Stack ADTOrder:

Functions:

Runtime: