Definition - Data Structures - Lab, Exercises of Data Structures and Algorithms

Some concept of Data Structures are Data Structures, Dynamic Programming, First-In-First-Out, Implementation, Python Code. Main points of this lecture are: Definition, Python Classes, Big-Oh Notation, Analyzing, Algorithms, Computational, Necessarily Linear, Problem Size, Large Size Problem, Quick Lunch

Typology: Exercises

2012/2013

Uploaded on 04/30/2013

naji
naji 🇮🇳

4.3

(6)

87 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Objective: To get a feel for big-oh notation by analyzing algorithms as well as timing them (Part A), and gain
some experience writing Python classes.
Informal Big-oh (and Big-Theta) Definition: As the size of a computational problem grows (i.e., more data),
we expect our program to run longer, but this run-time growth is not necessarily linear. Big-oh notation gives us
an idea how our program’s run-time will grow with respect to its problem size on larger data.
This might seem like a lot of mathematical mumbo-jumbo, but knowing an algorithms big-oh notation can help us
predict its run-time on large problem sizes. While running a large size problem, we might want to know if we
have time for a quick lunch, a long lunch, a long nap, go home for the day, take a week of vacation, pack-up the
desk because the boss will fire you for a slow algorithm, etc.
For example, consider the following algorithm:
result = 0
for r in range(n): loops n times
for c in range(n): executes a total of n*n times
for d in range(n//2): executes a total of n*n*n/2 times
result = result + d executes a total of n*n*n/2 times
# end for
# end for
# end for
Clearly, the body of the inner-most loop (the “result = result + d” statement) will execute n3 /2 times, so this
algorithm is “big-oh” of n-cubed, O( n3 ). Thus, the execution-time formula with-respect-to n is:
T(n) = c n3 + (slower growing terms).
For large values of n, T(n) c n3, where c is the constant of proportionality on the fastest growing term (the
l
machine dependent time related to how long it takes to execute the inner-most loop once) and T(n) represents
execution time as a function of n. If we know that T(10,000) = 1 second = c x 10,0003 , then we can predict what
T(1,000,000). First approximate c as c T(n) / n3 = 1 second / 10,0003 = 1 second / 1012 = 10-12 seconds. Since
l
we are running the algorithm on the same machine, c is unchanged for the larger problem. Thus, T(1,000,000) c
l
1,000,0003 = c 1018 = 10-12 seconds * 1018 = 106 seconds or about 11.6 days. (A couple weeks of vacation is
appropriate!)
To start the lab: Download and unzip the file lab2.zip from
Part A: In the folder lab2, open the timeStuff.py program in IDLE. (Right-click on timeStuff.py | Edit with
IDLE) Start it running in IDLE by selecting Run | Run Module from the menu. While it is running, answer
the following questions about each of the algorithms in timeStuff.py.
a) What is the big-oh of Algorithm 0?
Algorithm 0:
result = 0
for i in range(10000000):
result = result + i
b) What is the big-oh of Algorithm 1?
Algorithm 1:
result = 0
for i in range(n):
result = result + i
# end for
Data Structures (CS 1520) Lab 2 Name:___________________________
Lab 2 - 1
Docsity.com
pf3
pf4

Partial preview of the text

Download Definition - Data Structures - Lab and more Exercises Data Structures and Algorithms in PDF only on Docsity!

Objective: To get a feel for big-oh notation by analyzing algorithms as well as timing them (Part A), and gain some experience writing Python classes.

Informal Big-oh (and Big-Theta) Definition: As the size of a computational problem grows (i.e., more data), we expect our program to run longer, but this run-time growth is not necessarily linear. Big-oh notation gives us

an idea how our program’s run-time will grow with respect to its problem size on larger data.

This might seem like a lot of mathematical mumbo-jumbo, but knowing an algorithms big-oh notation can help us predict its run-time on large problem sizes. While running a large size problem, we might want to know if we have time for a quick lunch, a long lunch, a long nap, go home for the day, take a week of vacation, pack-up the desk because the boss will fire you for a slow algorithm, etc.

For example, consider the following algorithm: result = 0 for r in range(n): ← loops n times for c in range(n): ← executes a total of nn times for d in range(n//2): ← executes a total of nnn/2 times result = result + d ← executes a total of nn*n/2 times

end for

end for

end for

Clearly, the body of the inner-most loop (the “result = result + d” statement) will execute n^3 /2 times, so this algorithm is “big-oh” of n-cubed, O ( n^3 ). Thus, the execution-time formula with-respect-to n is: T(n) = c n^3 + (slower growing terms). For large values of n, T(n) lc n^3 , where c is the constant of proportionality on the fastest growing term (the

machine dependent time related to how long it takes to execute the inner-most loop once) and T(n) represents execution time as a function of n. If we know that T(10,000) = 1 second = c x 10,000^3 , then we can predict what T(1,000,000). First approximate c as c lT(n) / n^3 = 1 second / 10,000^3 = 1 second / 10^12 = 10-12^ seconds. Since we are running the algorithm on the same machine, c is unchanged for the larger problem. Thus, T(1,000,000) (^) lc

1,000,000^3 = c 10^18 = 10-12^ seconds * 10^18 = 10^6 seconds or about 11.6 days. (A couple weeks of vacation is appropriate!)

To start the lab: Download and unzip the file lab2.zip from

Part A: In the folder lab2, open the timeStuff.py program in IDLE. (Right-click on timeStuff.py | Edit with

IDLE) Start it running in IDLE by selecting Run | Run Module from the menu. While it is running , answer the following questions about each of the algorithms in timeStuff.py.

a) What is the big-oh of Algorithm 0?

Algorithm 0: result = 0 for i in range(10000000): result = result + i

b) What is the big-oh of Algorithm 1?

Algorithm 1: result = 0 for i in range(n): result = result + i

end for

Lab 2 - 1

c) What is the big-oh of Algorithm 2?

Algorithm 2: result = 0 for r in range(n): c = n while c > 1: result = result + c c = c // 2

end while

end for

d) What is the big-oh of Algorithm 3?

Algorithm 3: result = 0 for r in range(n): for c in range(n): result = result + c

end for

end for

e) What is the big-oh of Algorithm 4?

Algorithm 4 : result = 0 for r in range(n): for c in range(n): for d in range(nnn): result = result + d

end for

end for

end for

f) What is the big-oh of Algorithm 5?

Algorithm 5 : result = 0 i = 0 while i < 2**n: result = result + i i += 1

end while

Lab 2 - 2

b) For testing certain dice games, suppose we want to extend the AdvancedDie class to include a new method

setRoll which takes as a parameter a roll value that is used to set a die’s roll to a specified value. We might invoke this method as:

myDie.setRoll(3) # sets myDie to a current roll of 3

Implement a new subclass MoreAdvancedDie (in a new file more_advanced_die.py) which inherits from the AdvancedDie class, and includes the new setRoll method. When implementing the MoreAdvancedDie

class: ( see the AdvancedDie class and its methods, especially the init method as an example )

 Include documentation with the MoreAdvancedDie class (right below it’s class line)  Include documentation with the setRoll method including preconditions and postconditions

 Enforce preconditions by raising appropriate exceptions.

c) View the programmer-authored documentation for the MoreAdvancedDie class by typing help(MoreAdvanceDie) at the IDLE shell prompt (“>>>”) after selecting Run | Run Module in the file

more_advanced_die.py which contains the MoreAdvancedDie class.

After you have implemented AND fully tested your MoreAdvancedDie class, raise you hand and demonstrate it.

If you complete all parts of the lab, nothing needs to be turned in for this lab. If you do not get done today, then show me the completed lab in next week’s lab period. When done, remember to log off and take your USB drive.

If you have extra time, this would be a good chance to work on Homework #1!

Lab 2 - 4