Browse Source

Lecture 2

master
Oliver Hofmann 7 months ago
parent
commit
f92be5e617

+ 4
- 0
README.md View File



### SoSe24/lec01_alg ### SoSe24/lec01_alg
- Beispiele aus der ersten Vorlesung - Beispiele aus der ersten Vorlesung

### SoSe24/lec02_alg
- Beispiele aus der zweiten Vorlesung
- erstes Praktikum

+ 26
- 0
SoSe24/lec02_maxfolge/cubic.py View File

from SoSe24.algodat.foundation import AlgoDatArray, AlgoDatValue, read_int_sequence, MinusInf
from time import perf_counter as pfc

def maxfolge1(z: AlgoDatArray) -> (int, int, int):
maximum = AlgoDatValue(MinusInf())
current_sum = AlgoDatValue(0)
left = AlgoDatValue(0)
right = AlgoDatValue(0)
for i in range(z.size):
for j in range(i, z.size):
current_sum.value = 0
for k in range(i, j + 1):
current_sum += z[k]
if current_sum > maximum:
maximum.value = current_sum.value
left.value = i
right.value = j
return maximum.value, left, right


if __name__ == "__main__":
z = read_int_sequence("../../seq0.txt")
start = pfc()
print(maxfolge1(z))
print(f"Dauer: {pfc() - start:.4f}s")
AlgoDatValue.summary()

+ 44
- 0
SoSe24/lec02_maxfolge/divide_et_impera.py View File

from SoSe24.algodat.foundation import AlgoDatArray, AlgoDatValue, read_int_sequence, MinusInf
from time import perf_counter as pfc
def in_between(z: AlgoDatArray, l: int, m: int, r: int) -> (AlgoDatValue, int, int):
left_max = AlgoDatValue(MinusInf())
left = AlgoDatValue(0)
current_sum = AlgoDatValue(0)
for i in range(m, l - 1, -1):
current_sum += z[i]
if current_sum > left_max:
left_max.value = current_sum.value
left.value = i
right_max = AlgoDatValue(MinusInf())
right = AlgoDatValue(0)
current_sum.value = 0
for i in range(m + 1, r + 1):
current_sum += z[i]
if current_sum > right_max:
right_max.value = current_sum.value
right.value = i
return left_max + right_max, left, right

def maxfolge_part(z: AlgoDatArray, l:int, r:int) -> (int, int, int):
if l == r:
return z[l].value, l, r
m = (l + r) // 2
left_sum, left_left, left_right = maxfolge_part(z, l, m)
right_sum, right_left, right_right = maxfolge_part(z, m + 1, r)
cross_sum, cross_left, cross_right = in_between(z, l, m, r)
if left_sum >= right_sum and left_sum >= cross_sum:
return left_sum, left_left, left_right
elif right_sum >= left_sum and right_sum >= cross_sum:
return right_sum, right_left, right_right
else:
return cross_sum, cross_left, cross_right

def maxfolge3(z: AlgoDatArray) -> (int, int, int):
return maxfolge_part(z, 0, z.size - 1)

if __name__ == "__main__":
z = read_int_sequence("../../seq3.txt")
start = pfc()
print(maxfolge3(z))
print(f"Dauer: {pfc() - start:.4f}s")
AlgoDatValue.summary()

+ 27
- 0
SoSe24/lec02_maxfolge/linear.py View File

from SoSe24.algodat.foundation import AlgoDatArray, AlgoDatValue, read_int_sequence, MinusInf
from time import perf_counter as pfc

def maxfolge4(z: AlgoDatArray) -> (int, int, int):
maximum = AlgoDatValue(MinusInf())
current_sum = AlgoDatValue(0)
current_left = AlgoDatValue(0)
left = AlgoDatValue(0)
right = AlgoDatValue(0)
for i in range(z.size):
current_sum += z[i]
if current_sum > maximum:
maximum.value = current_sum.value
left.value = current_left.value
right.value = i
if current_sum < 0:
current_sum.value = 0
current_left.value = i + 1
return maximum.value, left, right


if __name__ == "__main__":
z = read_int_sequence("../../seq3.txt")
start = pfc()
print(maxfolge4(z))
print(f"Dauer: {pfc() - start:.4f}s")
AlgoDatValue.summary()

+ 25
- 0
SoSe24/lec02_maxfolge/quad.py View File

from SoSe24.algodat.foundation import AlgoDatArray, AlgoDatValue, read_int_sequence, MinusInf
from time import perf_counter as pfc

def maxfolge2(z: AlgoDatArray) -> (int, int, int):
maximum = AlgoDatValue(MinusInf())
current_sum = AlgoDatValue(0)
left = AlgoDatValue(0)
right = AlgoDatValue(0)
for i in range(z.size):
current_sum.value = 0
for j in range(i, z.size):
current_sum += z[j]
if current_sum > maximum:
maximum.value = current_sum.value
left.value = i
right.value = j
return maximum.value, left, right


if __name__ == "__main__":
z = read_int_sequence("../../seq0.txt")
start = pfc()
print(maxfolge2(z))
print(f"Dauer: {pfc() - start:.4f}s")
AlgoDatValue.summary()

Loading…
Cancel
Save