Docsity

Graph - Data Structures - Lecture Notes, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
1. Draw the graph for
sumList
(O(n)) and
someLoops
(O(n
2
)) from the previous lecture.
10,000 20,000 30,000 40,000 50,000 60,000
10 sec
30 sec
50 sec
20 sec
40 sec
60 sec
n
Execution Time
2. Consider the following sumSomeListItems function.
import time
def main():
n = eval(input("Enter size of list: "))
aList = list(range(1, n+1))
start = time.clock()
sum = sumSomeListItems(aList)
end = time.clock()
print("Time to sum the list was %.9f seconds" % (end-start))
def sumSomeListItems(myList):
"""Returns the sum of some items in myList"""
total = 0
index = len(myList) - 1
while index > 0:
total = total + myList[index]
index = index // 2
return total
main()
a) What is the problem size of sumSomeListItems?
b) If we input n of 10,000 and sumSomeListItems takes 10 seconds, how long would you expect
sumSomeListItems to take for n of 20,000?
(Hint: For n of 20,000, how many more times would the loop execute than for n of 10,000?)
c) What is the big-oh notation for sumSomeListItems?
d) Add the execution-time graph for sumSomeListItems to the graph.
Data Structures (CS 1520) Lecture 3 Name:_____________________
Lecture 3 Page 1
Docsity.com
pf3

Partial preview of the text

Download Graph - Data Structures - Lecture Notes and more Study notes Data Structures and Algorithms in PDF only on Docsity!

  1. Draw the graph for sumList ( O (n)) and someLoops ( O (n^2 )) from the previous lecture.

10,000 20,000 30,000 40,000 50,000 60,

10 sec

30 sec

50 sec

20 sec

40 sec

60 sec

n

Execution Time

  1. Consider the following sumSomeListItems function.

import time def main(): n = eval(input("Enter size of list: ")) aList = list(range(1, n+1)) start = time.clock() sum = sumSomeListItems(aList) end = time.clock() print("Time to sum the list was %.9f seconds" % (end-start)) def sumSomeListItems(myList): """Returns the sum of some items in myList""" total = 0 index = len(myList) - 1 while index > 0: total = total + myList[index] index = index // 2 return total main()

a) What is the problem size of sumSomeListItems?

b) If we input n of 10,000 and sumSomeListItems takes 10 seconds, how long would you expect sumSomeListItems to take for n of 20,000?

(Hint: For n of 20,000, how many more times would the loop execute than for n of 10,000?)

c) What is the big-oh notation for sumSomeListItems?

d) Add the execution-time graph for sumSomeListItems to the graph.

Lecture 3 Page 1

i = 1 while i <= n: for j in range(n):

something of O(1)

end for

i = i * 2

end while

a) Analyze the above algorithm to determine its big-oh notation, O ( ).

b) If n of 10,000, takes 10 seconds, how long would you expect the above code to take for n of 20,000?

c) Add the execution-time graph for the above code to the graph.

  1. Most programming languages have a built-in array data structure to store a collection of same-type items. Arrays are implemented in RAM memory as a contiguous block of memory locations. Consider an array X that contains the odd integers:

a) Any array element can be accessed randomly by calculating its address. For example, address of X[5] = 4000 + 5 * 4 = 4020. What is the general formula for calculating the address of the ith element in an array?

b) What is the big-oh notation for accessing the ith element?

c) A Python list uses an array of references (pointers) to list items in their implementation of a list. For example, a list of strings containing the alphabet:

'a' 'b' 'c' 'z'

0 1 2 3 (len( )-1)

Since a Python list can contain heterogeneous data, how does storing references in the list aid implementation?

Lecture 3 Page 2

Execution flow i = 1 (^) i = 2 i = 4 (^) i = n

j = 0 to n-1 j = 0 to n-1^ j = 0 to n- loops n times loops n times loops n times

address

Memory

X[0] X[1] X[2] X[3] X[4] X[5] X[6]