Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 91 additions & 20 deletions projects/project1/project1algorithms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# PROJECT GROUP 21 - PROJECT 1
# ---------------------------------------
# Members: Albert Le
# <leal@onid.oregonstate.edu>
#
# Charles Jenkins
# <jenkinch@onid.oregonstate.edu>
#
# Colin Bradford
# <bradfoco@onid.oregonstate.edu>
#
# Class: CS325 Analysis of Algorithms
#
# Description: Program uses four different
# algorithms to find the maximum subarray
# of a given array.
# ---------------------------------------

import sys
import math
import re
Expand All @@ -10,6 +28,16 @@

results = [[],[],[],[]]

# ---------------------------------------
# Name: max_subarray_algorithm1
#
# Description: Finds max subarray using
# enumeration.
#
# Receives: array
#
# Returns: max subarray
# ---------------------------------------
def max_subarray_algorithm1(array):
max_array = current_sum = start = end = 0
for i in range(0, len(array)):
Expand All @@ -24,7 +52,17 @@ def max_subarray_algorithm1(array):
start = i
end = k+1
return array[start:end]


# ---------------------------------------
# Name: max_subarray_algorithm2
#
# Description: Finds max subarray using
# better enumeration.
#
# Receives: array
#
# Returns: max subarray
# ---------------------------------------
def max_subarray_algorithm2(array):
max_array = current_sum = start = end = 0
for i in range(0, len(array)):
Expand All @@ -37,7 +75,17 @@ def max_subarray_algorithm2(array):
start = i
end = j+1
return array[start:end]


# ---------------------------------------
# Name: max_subarray_algorithm3
#
# Description: Finds max subarray using
# divide and conquer.
#
# Receives: array
#
# Returns: max subarray sum
# ---------------------------------------
def max_subarray_algorithm3(array):
current_sum = start = end = 0
if len(array) == 0:
Expand All @@ -60,10 +108,18 @@ def max_subarray_algorithm3(array):
if current_sum > right_max:
right_max = current_sum
end = i
print start
print end
return max(max(left_array,right_array),(left_max+right_max))


# ---------------------------------------
# Name: max_subarray_algorithm4
#
# Description: Finds max subarray using
# dynamic programming.
#
# Receives: array
#
# Returns: max subarray
# ---------------------------------------
def max_subarray_algorithm4(array):
max_array = current_sum = start = end = 0
for i in range(0,len(array)):
Expand All @@ -78,47 +134,62 @@ def max_subarray_algorithm4(array):
start = 0
return array[start:end]

#main function
# Main Function
if __name__ == '__main__':
args = sys.argv
if len(args) == 2:
# Process test file and check for correctness for given algorithm arg
with open("./MSS_Problems.txt","r") as f:
file = f.read() #bad if taking in big file
file = f.read() # Bad if taking in big file
reg = "(\[.*\])(?:\n|\r\n)(\[.*\])(?:\n|\r\n)(\d+)"
#reg = "(\[.*\])(?:\n|\r\n)"

for m in re.findall(reg,file):
if m == None: continue
array = eval(m[0]) #interpret the square bracketed stuff as python list
solution = int(m[2]) #cast the string number to an int
array = eval(m[0]) # Interpret the square bracketed stuff as python list
solution = int(m[2]) # Cast the string number to an int
print array
result = 0

if int(args[1]) == 1:
result_array = max_subarray_algorithm1(array)
print result_array
result = sum(result_array)

if int(args[1]) == 2:
result_array = max_subarray_algorithm2(array)
print result_array
result = sum(result_array)

if int(args[1]) == 3:
result = max_subarray_algorithm3(array)
#print result_array
#result = sum(result_array)
for i in range(0,len(array)-1):
check = 0
j = i
while check != result and j <= len(array)-1:
check += array[j]
j += 1
if check == result:
break
print array[i:j]

if int(args[1]) == 4:
result_array = max_subarray_algorithm4(array)
print result_array
result = sum(result_array)

if result == solution:
print "Correct: %s==%s"%(result, solution)
else:
print "Wrong: %s!=%s"%(result, solution)
else:
# Time and plot results of all algorithms for random arrays of size n
reps = 10
test = 1
for i in range(0,len(sizes1)):
n = sizes1[i]
setup1 = "import random;from __main__ import max_subarray_algorithm1;array = [random.randint(-1000, 1000) for i in xrange(%s)]"%(n)
test1 = "max_subarray_algorithm1(array)" #timeit interprets strings passed to it as python code and runs them
test1 = "max_subarray_algorithm1(array)" # timeit interprets strings passed to it as python code and runs them
result1 = timeit.timeit(test1, setup=setup1, number=reps)
results[0].append(result1)
r = [float(math.log(o)) for o in results[0]]
Expand All @@ -137,8 +208,8 @@ def max_subarray_algorithm4(array):

for i in range(0,len(sizes2)):
n = sizes2[i]
setup3 = "import random;from __main__ import max_subarray_algorithm2;array = [random.randint(-1000, 1000) for i in xrange(%s)]"%(n)
test3 = "max_subarray_algorithm2(array)"
setup3 = "import random;from __main__ import max_subarray_algorithm3;array = [random.randint(-1000, 1000) for i in xrange(%s)]"%(n)
test3 = "max_subarray_algorithm3(array)"
result3 = timeit.timeit(test3, setup=setup3, number=reps)
results[2].append(result3)
r = [float(math.log(o)) for o in results[2]]
Expand All @@ -147,18 +218,18 @@ def max_subarray_algorithm4(array):

for i in range(0,len(sizes2)):
n = sizes2[i]
setup4 = "import random;from __main__ import max_subarray_algorithm2;array = [random.randint(-1000, 1000) for i in xrange(%s)]"%(n)
test4 = "max_subarray_algorithm2(array)"
setup4 = "import random;from __main__ import max_subarray_algorithm4;array = [random.randint(-1000, 1000) for i in xrange(%s)]"%(n)
test4 = "max_subarray_algorithm4(array)"
result4 = timeit.timeit(test4, setup=setup4, number=reps)
results[3].append(result4)
r = [float(math.log(o)) for o in results[3]]
coefficient = math.exp(numpy.polyfit(sizes2, r, 1)[0])
print 'algorithm 4: ' + str(coefficient)

algorithm1,= mat.pyplot.loglog(sizes1,results[0], label='algorithm 1')
algorithm2,= mat.pyplot.loglog(sizes2,results[1], label='algorithm 2')
algorithm3,= mat.pyplot.loglog(sizes2,results[2], label='algorithm 3')
algorithm4,= mat.pyplot.loglog(sizes2,results[3], label='algorithm 4')
algorithm1 = mat.pyplot.loglog(sizes1,results[0], label='algorithm 1')
algorithm2 = mat.pyplot.loglog(sizes2,results[1], label='algorithm 2')
algorithm3 = mat.pyplot.loglog(sizes2,results[2], label='algorithm 3')
algorithm4 = mat.pyplot.loglog(sizes2,results[3], label='algorithm 4')
plt.title('loglog plot of runtime vs. array size',fontsize=10)
plt.ylabel('log(runtime)',fontsize=12)
plt.xlabel('log(array size)',fontsize=12)
Expand Down