master > master: src go - algorithmen + display methoden hinzugefügt
This commit is contained in:
53
code/golang/internal/algorithms/search/binary/binary.go
Normal file
53
code/golang/internal/algorithms/search/binary/binary.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package binary
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"ads/internal/core/logging"
|
||||
"ads/internal/core/metrics"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* GLOBAL VARIABLES/CONSTANTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
//
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* ALGORITHM binary search
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
Inputs: L = Liste von Zahlen, x = Zahl.
|
||||
|
||||
Annahme: L sei aufsteigend sortiert.
|
||||
|
||||
Outputs: Position von x in L, sonst −1 wenn x nicht in L.
|
||||
*/
|
||||
func BinarySearch(L []int, x int) int {
|
||||
if len(L) == 0 {
|
||||
logging.LogDebug(fmt.Sprintf("x nicht in L"))
|
||||
return -1
|
||||
}
|
||||
metrics.AddTimeCost()
|
||||
m := int(len(L) / 2)
|
||||
if L[m] == x {
|
||||
logging.LogDebug(fmt.Sprintf("x in Position m gefunden"))
|
||||
return m
|
||||
} else if x < L[m] {
|
||||
logging.LogDebug(fmt.Sprintf("Suche in linker Hälfte fortsetzen."))
|
||||
index := BinarySearch(L[:m], x)
|
||||
return index
|
||||
} else { // } else if x > L[m] {
|
||||
logging.LogDebug(fmt.Sprintf("Suche in rechter Hälfte fortsetzen."))
|
||||
index := BinarySearch(L[m+1:], x)
|
||||
if index >= 0 {
|
||||
index += (m + 1) // NOTE: muss Indexwert kompensieren
|
||||
}
|
||||
return index
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package binary
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"ads/internal/core/metrics"
|
||||
"ads/internal/core/utils"
|
||||
"ads/internal/setup"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* GLOBAL VARIABLES/CONSTANTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
//
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* CHECKS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func preChecks(L []int, _ ...interface{}) error {
|
||||
if !utils.IsSortedListInt(L) {
|
||||
return fmt.Errorf("Ungültiger Input: L muss aufsteigend sortiert sein!")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func postChecks(L []int, x int, index int, _ ...interface{}) error {
|
||||
if utils.ArrayContains(L, x) {
|
||||
if !(index >= 0) {
|
||||
return fmt.Errorf("Der Algorithmus sollte nicht -1 zurückgeben.")
|
||||
}
|
||||
if L[index] != x {
|
||||
return fmt.Errorf("Der Algorithmus hat den falschen Index bestimmt.")
|
||||
}
|
||||
} else {
|
||||
if index != -1 {
|
||||
return fmt.Errorf("Der Algorithmus sollte -1 zurückgeben.")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* ALGORITHM binary search + Display
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func FancyBinarySearch(input_L []int, input_x int) (int, error) {
|
||||
var name = "Binärsuchalgorithmus"
|
||||
var inputs = map[string]interface{}{
|
||||
"L": input_L,
|
||||
"x": input_x,
|
||||
}
|
||||
var outputs map[string]interface{}
|
||||
var (
|
||||
output_index int
|
||||
)
|
||||
var err error
|
||||
|
||||
do_once := true
|
||||
for do_once {
|
||||
do_once = false
|
||||
setup.DisplayStartOfAlgorithm(name, inputs)
|
||||
metrics.RestartMetrics()
|
||||
|
||||
// Prechecks:
|
||||
err = preChecks(input_L, input_x)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
// Ausführung des Algorithmus:
|
||||
output_index = BinarySearch(input_L, input_x)
|
||||
outputs = map[string]interface{}{
|
||||
"index": output_index,
|
||||
}
|
||||
|
||||
// Letzte Messages:
|
||||
setup.DisplayMetrics()
|
||||
setup.DisplayEndOfAlgorithm(outputs)
|
||||
|
||||
// Postchecks:
|
||||
err = postChecks(input_L, input_x, output_index)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return output_index, err
|
||||
}
|
||||
69
code/golang/internal/algorithms/search/interpol/interpol.go
Normal file
69
code/golang/internal/algorithms/search/interpol/interpol.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package interpol
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"ads/internal/core/logging"
|
||||
"ads/internal/core/metrics"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* GLOBAL VARIABLES/CONSTANTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
//
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* ALGORITHM interpolation
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
Inputs: L = Liste von Zahlen, x = Zahl.
|
||||
|
||||
Annahme: L sei aufsteigend sortiert.
|
||||
|
||||
Outputs: Position von x in L, sonst −1 wenn x nicht in L.
|
||||
*/
|
||||
func InterpolationSearch(L []int, x int, u int, v int) int {
|
||||
if len(L) == 0 {
|
||||
logging.LogDebug(fmt.Sprintf("Liste L leer, also x nicht in L"))
|
||||
return -1
|
||||
} else if !(L[u] <= x && x <= L[v]) {
|
||||
logging.LogDebug(fmt.Sprintf("x liegt außerhalb der Grenzen von L"))
|
||||
return -1
|
||||
}
|
||||
metrics.AddTimeCost()
|
||||
p := getSuchposition(L, x, u, v)
|
||||
logging.LogDebug(fmt.Sprintf("Interpolante von x in (u, v)=(%[1]v, %[2]v) ist p = %[3]v.", u, v, p))
|
||||
if L[p] == x {
|
||||
logging.LogDebug(fmt.Sprintf("x in Position p gefunden"))
|
||||
return p
|
||||
} else if x < L[p] {
|
||||
logging.LogDebug(fmt.Sprintf("Suche in linker Hälfte fortsetzen."))
|
||||
return InterpolationSearch(L, x, u, p-1)
|
||||
} else { // } else if x > L[p] {
|
||||
logging.LogDebug(fmt.Sprintf("Suche in rechter Hälfte fortsetzen."))
|
||||
return InterpolationSearch(L, x, p+1, v)
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* ALGORITHM interpolation
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
Inputs: L = Liste von Zahlen, x = Zahl, [u, v] = Suchinterval.
|
||||
|
||||
Outputs: Interpolierte Position, um Suchinterval ausgeglichen aufzuteilen.
|
||||
*/
|
||||
func getSuchposition(L []int, x int, u int, v int) int {
|
||||
metrics.AddTimeCost()
|
||||
r := float64(x-L[u]) / float64(L[v]-L[u])
|
||||
p := int(math.Floor(float64(u) + r*float64(v-u)))
|
||||
return p
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package interpol
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"ads/internal/core/metrics"
|
||||
"ads/internal/core/utils"
|
||||
"ads/internal/setup"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* GLOBAL VARIABLES/CONSTANTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
//
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* CHECKS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func preChecks(L []int, _ ...interface{}) error {
|
||||
if !utils.IsSortedListInt(L) {
|
||||
return fmt.Errorf("Ungültiger Input: L muss aufsteigend sortiert sein!")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func postChecks(L []int, x int, index int, _ ...interface{}) error {
|
||||
if utils.ArrayContains(L, x) {
|
||||
if !(index >= 0) {
|
||||
return fmt.Errorf("Der Algorithmus sollte nicht -1 zurückgeben.")
|
||||
}
|
||||
if L[index] != x {
|
||||
return fmt.Errorf("Der Algorithmus hat den falschen Index bestimmt.")
|
||||
}
|
||||
} else {
|
||||
if index != -1 {
|
||||
return fmt.Errorf("Der Algorithmus sollte -1 zurückgeben.")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* ALGORITHM interpolation + Display
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func FancyInterpolationSearch(input_L []int, input_x int, input_u int, input_v int) (int, error) {
|
||||
var name = "Interpolationssuchalgorithmus"
|
||||
var inputs = map[string]interface{}{
|
||||
"L": input_L,
|
||||
"x": input_x,
|
||||
"u": input_u,
|
||||
"v": input_v,
|
||||
}
|
||||
var outputs map[string]interface{}
|
||||
var (
|
||||
output_index int
|
||||
)
|
||||
var err error
|
||||
|
||||
do_once := true
|
||||
for do_once {
|
||||
do_once = false
|
||||
setup.DisplayStartOfAlgorithm(name, inputs)
|
||||
metrics.RestartMetrics()
|
||||
|
||||
// Prechecks:
|
||||
err = preChecks(input_L, input_x, input_u, input_v)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
// Ausführung des Algorithmus:
|
||||
output_index = InterpolationSearch(input_L, input_x, input_u, input_v)
|
||||
outputs = map[string]interface{}{
|
||||
"index": output_index,
|
||||
}
|
||||
|
||||
// Letzte Messages:
|
||||
setup.DisplayMetrics()
|
||||
setup.DisplayEndOfAlgorithm(outputs)
|
||||
|
||||
// Postchecks:
|
||||
err = postChecks(input_L, input_x, output_index)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return output_index, err
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package ith_element
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"ads/internal/core/logging"
|
||||
"ads/internal/core/metrics"
|
||||
"ads/internal/core/utils"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* GLOBAL VARIABLES/CONSTANTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
//
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* ALGORITHM find ith smallest
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
Inputs: L = Liste von Zahlen, i = Ordinalzahl
|
||||
|
||||
Annahmen:
|
||||
|
||||
- L enthält keine Duplikate.
|
||||
- L enthält mindestens i Elemente.
|
||||
|
||||
Outputs: Wert des i. kleinste Element in L.
|
||||
Beachte 1.kleinstes <==> Minimum.
|
||||
*/
|
||||
func FindIthSmallest(L []int, i int) int {
|
||||
n := len(L)
|
||||
// extrahiere Wert + Index des Minimums - bedarf n Schritte
|
||||
metrics.AddTimeCost(n)
|
||||
index := utils.ArgMinInt(L)
|
||||
minValue := L[index]
|
||||
// Falls i = 1, dann wird das Minimum gesucht, sonst Minimum entfernen und nach i-1. Element suchen
|
||||
if i == 1 {
|
||||
logging.LogDebug("Das i. kleinste Element wurde gefunden.")
|
||||
return minValue
|
||||
} else {
|
||||
logging.LogDebug(fmt.Sprintf("Entferne Minimum: %[1]v.", minValue))
|
||||
i = i - 1
|
||||
L_ := utils.PopIndexListInt(L, index) // entferne Element mit Index = index
|
||||
return FindIthSmallest(L_, i)
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* ALGORITHM find ith smallest (D & C)
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
Inputs: L = Liste von Zahlen, i = Ordinalzahl
|
||||
|
||||
Annahmen:
|
||||
|
||||
- L enthält keine Duplikate.
|
||||
|
||||
- L enthält mindestens i Elemente.
|
||||
|
||||
Outputs: Wert des i. kleinste Element in L.
|
||||
Beachte 1.kleinstes <==> Minimum.
|
||||
*/
|
||||
func FindIthSmallestDC(L []int, i int) int {
|
||||
metrics.AddTimeCost()
|
||||
p := L[len(L)-1] // NOTE: Pivotelement kann beliebig gewählt werden
|
||||
// Werte in L in linke und rechte Teillisten um das Pivotelement aufteilen:
|
||||
Ll := []int{} // wird alle Elemente in L < p enthalten
|
||||
Lr := []int{} // wird alle Elemente in L > p enthalten
|
||||
for i := 0; i < len(L); i++ {
|
||||
x := L[i]
|
||||
if x < p {
|
||||
Ll = append(Ll, x)
|
||||
} else if x > p {
|
||||
Lr = append(Lr, x)
|
||||
}
|
||||
}
|
||||
// Fallunterscheidung:
|
||||
if len(Ll) == i-1 {
|
||||
logging.LogDebug(fmt.Sprintf("Es gibt i-1 Elemente vor p=%[1]v. ==> i. kleinste Element = p", p))
|
||||
return p
|
||||
} else if len(Ll) >= i {
|
||||
logging.LogDebug(fmt.Sprintf("Es gibt >= i Elemente vor p=%[1]v. ==> Suche in linker Hälfte!", p))
|
||||
return FindIthSmallestDC(Ll, i)
|
||||
} else {
|
||||
logging.LogDebug(fmt.Sprintf("Es gibt < i-1 Elemente vor p=%[1]v. ==> Suche in rechter Hälfte!", p))
|
||||
i = i - (len(Ll) + 1)
|
||||
return FindIthSmallestDC(Lr, i)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
package ith_element
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"ads/internal/core/metrics"
|
||||
"ads/internal/core/utils"
|
||||
"ads/internal/setup"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* GLOBAL VARIABLES/CONSTANTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
//
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* CHECKS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func preChecks(L []int, i int, _ ...interface{}) error {
|
||||
if !(1 <= i && i <= len(L)) {
|
||||
return fmt.Errorf("Der Wert von i muss zw. %[1]v und %[2]v liegen.", 1, len(L))
|
||||
} else if !utils.ContainsNoDuplicatesListInt(L) {
|
||||
return fmt.Errorf("Ungültiger Input: L darf keine Duplikate enthalten!")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func postChecks(L []int, i int, value int, _ ...interface{}) error {
|
||||
L_ := make([]int, len(L))
|
||||
copy(L_, L)
|
||||
sort.Ints(L_)
|
||||
if !(L_[i-1] == value) {
|
||||
return fmt.Errorf("Das i=%[1]v. kleinste Element ist %[2]v und nicht %[3]v.", i, L_[i-1], value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* ALGORITHM find ith smallest + Display
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func FancyFindIthSmallest(input_L []int, input_i int) (int, error) {
|
||||
var name = "Auswahlproblem (i. kleinstes Element)"
|
||||
var inputs = map[string]interface{}{
|
||||
"L": input_L,
|
||||
"i": input_i,
|
||||
}
|
||||
var outputs map[string]interface{}
|
||||
var (
|
||||
output_value int
|
||||
)
|
||||
var err error
|
||||
|
||||
do_once := true
|
||||
for do_once {
|
||||
do_once = false
|
||||
setup.DisplayStartOfAlgorithm(name, inputs)
|
||||
metrics.RestartMetrics()
|
||||
|
||||
// Prechecks:
|
||||
err = preChecks(input_L, input_i)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
// Ausführung des Algorithmus:
|
||||
output_value = FindIthSmallest(input_L, input_i)
|
||||
outputs = map[string]interface{}{
|
||||
"value": output_value,
|
||||
}
|
||||
|
||||
// Letzte Messages:
|
||||
setup.DisplayMetrics()
|
||||
setup.DisplayEndOfAlgorithm(outputs)
|
||||
|
||||
// Postchecks:
|
||||
err = postChecks(input_L, input_i, output_value)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return output_value, err
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* ALGORITHM find ith smallest (D & C) + Display
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func FancyFindIthSmallestDC(input_L []int, input_i int) (int, error) {
|
||||
var name = "Auswahlproblem (i. kleinstes Element, D & C)"
|
||||
var inputs = map[string]interface{}{
|
||||
"L": input_L,
|
||||
"i": input_i,
|
||||
}
|
||||
var outputs map[string]interface{}
|
||||
var (
|
||||
output_value int
|
||||
)
|
||||
var err error
|
||||
|
||||
do_once := true
|
||||
for do_once {
|
||||
do_once = false
|
||||
setup.DisplayStartOfAlgorithm(name, inputs)
|
||||
metrics.RestartMetrics()
|
||||
|
||||
// Prechecks:
|
||||
err = preChecks(input_L, input_i)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
// Ausführung des Algorithmus:
|
||||
output_value = FindIthSmallestDC(input_L, input_i)
|
||||
outputs = map[string]interface{}{
|
||||
"value": output_value,
|
||||
}
|
||||
|
||||
// Letzte Messages:
|
||||
setup.DisplayMetrics()
|
||||
setup.DisplayEndOfAlgorithm(outputs)
|
||||
|
||||
// Postchecks:
|
||||
err = postChecks(input_L, input_i, output_value)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return output_value, err
|
||||
}
|
||||
91
code/golang/internal/algorithms/search/jump/jump.go
Normal file
91
code/golang/internal/algorithms/search/jump/jump.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package jump
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"ads/internal/algorithms/search/sequential"
|
||||
"ads/internal/core/logging"
|
||||
"ads/internal/core/metrics"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* GLOBAL VARIABLES/CONSTANTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
//
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* ALGORITHM jump search
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
Inputs: L = Liste von Zahlen, x = Zahl, m = lineare Sprunggröße.
|
||||
|
||||
Annahmen:
|
||||
- L sei aufsteigend sortiert.
|
||||
- L enthält keine Duplikate.
|
||||
|
||||
Outputs: Position von x in L, sonst −1 wenn x nicht in L.
|
||||
*/
|
||||
func JumpSearchLinear(L []int, x int, m int) int {
|
||||
i0 := 0
|
||||
i1 := m
|
||||
// ACHTUNG: dies ist eine while-Schleife ist golang:
|
||||
for i0 < len(L) {
|
||||
metrics.AddTimeCost()
|
||||
block := L[i0:i1]
|
||||
elementAfterBlock := block[len(block)-1] + 1
|
||||
if x < elementAfterBlock {
|
||||
logging.LogDebug(fmt.Sprintf("Element muss sich im Block [%[1]v, %[2]v) befinden.", i0, i1))
|
||||
index := sequential.SequentialSearch(block, x)
|
||||
if index >= 0 {
|
||||
index += i0 // NOTE: muss wegen Offset kompensieren
|
||||
}
|
||||
return index
|
||||
}
|
||||
logging.LogDebug(fmt.Sprintf("Element befindet sich nicht im Block [%[1]v, %[2]v).", i0, i1))
|
||||
i0 = i1
|
||||
i1 += m
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* ALGORITHM jump search - exponentiell
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
Inputs: L = Liste von Zahlen, x = Zahl.
|
||||
|
||||
Annahmen:
|
||||
- L sei aufsteigend sortiert.
|
||||
- L enthält keine Duplikate.
|
||||
|
||||
Outputs: Position von x in L, sonst −1 wenn x nicht in L.
|
||||
*/
|
||||
func JumpSearchExponentiell(L []int, x int) int {
|
||||
i0 := 0
|
||||
i1 := 1
|
||||
// ACHTUNG: dies ist eine while-Schleife ist golang:
|
||||
for i0 < len(L) {
|
||||
metrics.AddTimeCost()
|
||||
block := L[i0:i1]
|
||||
elementAfterBlock := block[len(block)-1] + 1
|
||||
if x < elementAfterBlock {
|
||||
logging.LogDebug(fmt.Sprintf("Element muss sich im Block [%[1]v, %[2]v) befinden.", i0, i1))
|
||||
index := sequential.SequentialSearch(block, x)
|
||||
if index >= 0 {
|
||||
index += i0 // NOTE: muss wegen Offset kompensieren
|
||||
}
|
||||
return index
|
||||
}
|
||||
logging.LogDebug(fmt.Sprintf("Element befindet sich nicht im Block [%[1]v, %[2]v).", i0, i1))
|
||||
i0 = i1
|
||||
i1 *= 2
|
||||
}
|
||||
return -1
|
||||
}
|
||||
143
code/golang/internal/algorithms/search/jump/jump_fancy.go
Normal file
143
code/golang/internal/algorithms/search/jump/jump_fancy.go
Normal file
@@ -0,0 +1,143 @@
|
||||
package jump
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"ads/internal/core/metrics"
|
||||
"ads/internal/core/utils"
|
||||
"ads/internal/setup"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* GLOBAL VARIABLES/CONSTANTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
//
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* CHECKS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func preChecks(L []int, _ ...interface{}) error {
|
||||
if !utils.IsSortedListInt(L) {
|
||||
return fmt.Errorf("Ungültiger Input: L muss aufsteigend sortiert sein!")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func postChecks(L []int, x int, index int, _ ...interface{}) error {
|
||||
if utils.ArrayContains(L, x) {
|
||||
if !(index >= 0) {
|
||||
return fmt.Errorf("Der Algorithmus sollte nicht -1 zurückgeben.")
|
||||
}
|
||||
if L[index] != x {
|
||||
return fmt.Errorf("Der Algorithmus hat den falschen Index bestimmt.")
|
||||
}
|
||||
} else {
|
||||
if index != -1 {
|
||||
return fmt.Errorf("Der Algorithmus sollte -1 zurückgeben.")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* ALGORITHM jump search + Display
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func FancyJumpSearchLinear(input_L []int, input_x int, input_m int) (int, error) {
|
||||
var name = "Sprungsuche"
|
||||
var inputs = map[string]interface{}{
|
||||
"L": input_L,
|
||||
"x": input_x,
|
||||
"m": input_m,
|
||||
}
|
||||
var outputs map[string]interface{}
|
||||
var (
|
||||
output_index int
|
||||
)
|
||||
var err error
|
||||
|
||||
do_once := true
|
||||
for do_once {
|
||||
do_once = false
|
||||
setup.DisplayStartOfAlgorithm(name, inputs)
|
||||
metrics.RestartMetrics()
|
||||
|
||||
// Prechecks:
|
||||
err = preChecks(input_L, input_x, input_m)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
// Ausführung des Algorithmus:
|
||||
output_index = JumpSearchLinear(input_L, input_x, input_m)
|
||||
outputs = map[string]interface{}{
|
||||
"index": output_index,
|
||||
}
|
||||
|
||||
// Letzte Messages:
|
||||
setup.DisplayMetrics()
|
||||
setup.DisplayEndOfAlgorithm(outputs)
|
||||
|
||||
// Postchecks:
|
||||
err = postChecks(input_L, input_x, output_index)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return output_index, err
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* ALGORITHM jump search - exponentiell + Display
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func FancyJumpSearchExponentiell(input_L []int, input_x int) (int, error) {
|
||||
var name = "Sprungsuche (exponentiell)"
|
||||
var inputs = map[string]interface{}{
|
||||
"L": input_L,
|
||||
"x": input_x,
|
||||
}
|
||||
var outputs map[string]interface{}
|
||||
var (
|
||||
output_index int
|
||||
)
|
||||
var err error
|
||||
|
||||
do_once := true
|
||||
for do_once {
|
||||
do_once = false
|
||||
setup.DisplayStartOfAlgorithm(name, inputs)
|
||||
metrics.RestartMetrics()
|
||||
|
||||
// Prechecks:
|
||||
err = preChecks(input_L, input_x)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
// Ausführung des Algorithmus:
|
||||
output_index = JumpSearchExponentiell(input_L, input_x)
|
||||
outputs = map[string]interface{}{
|
||||
"index": output_index,
|
||||
}
|
||||
|
||||
// Letzte Messages:
|
||||
setup.DisplayMetrics()
|
||||
setup.DisplayEndOfAlgorithm(outputs)
|
||||
|
||||
// Postchecks:
|
||||
err = postChecks(input_L, input_x, output_index)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return output_index, err
|
||||
}
|
||||
173
code/golang/internal/algorithms/search/poison/poison.go
Normal file
173
code/golang/internal/algorithms/search/poison/poison.go
Normal file
@@ -0,0 +1,173 @@
|
||||
package poison
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"ads/internal/core/logging"
|
||||
"ads/internal/core/metrics"
|
||||
"ads/internal/core/utils"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* GLOBAL VARIABLES/CONSTANTS
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
||||
|
||||
//
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* ALGORITHM find poison
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
||||
|
||||
/*
|
||||
Inputs: L = Liste von Getränken: durch boolesche Werte wird dargestellt, ob ein Getränk vergiftet ist.
|
||||
|
||||
Annahme: Genau ein Getränk sei vergiftet.
|
||||
|
||||
Outputs: Der Index des vergifteten Getränks, falls es eines gibt, ansonsten -1.
|
||||
|
||||
NOTE: Zeitkosten hier messen nur die Anzahl der Vorkoster.
|
||||
*/
|
||||
func FindPoison(L []int) int {
|
||||
logging.LogDebug("Bereite Vorkoster vor")
|
||||
n := len(L)
|
||||
testers := [][]int{}
|
||||
for i := 0; i < n; i++ {
|
||||
metrics.AddSpaceCost()
|
||||
logging.LogDebug(fmt.Sprintf("Füge Vorkoster hinzu, der nur Getränk %[1]v testet.", i))
|
||||
testers = append(testers, []int{i})
|
||||
}
|
||||
logging.LogDebug("Warte auf Effekte")
|
||||
effects := waitForEffects(L, testers)
|
||||
logging.LogDebug("Effekte auswerten, um vergiftete Getränke zu lokalisieren.")
|
||||
poisened := evaluateEffects(testers, effects)
|
||||
if len(poisened) > 0 {
|
||||
return poisened[0]
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* ALGORITHM find poison fast
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
||||
|
||||
/*
|
||||
Inputs: L = Liste von Getränken: durch boolesche Werte wird dargestellt, ob ein Getränk vergiftet ist.
|
||||
|
||||
Annahme: Genau ein Getränk sei vergiftet.
|
||||
|
||||
Outputs: Der Index des vergifteten Getränks, falls es eines gibt, ansonsten -1.
|
||||
|
||||
NOTE: Zeitkosten hier messen nur die Anzahl der Vorkoster.
|
||||
*/
|
||||
func FindPoisonFast(L []int) int {
|
||||
logging.LogDebug("Bereite Vorkoster vor")
|
||||
n := len(L)
|
||||
p := utils.LengthOfBinary(n)
|
||||
testers := [][]int{}
|
||||
// Für jedes Bit i=0 bis p-1 ...
|
||||
for i := 0; i < p; i++ {
|
||||
tester0 := []int{}
|
||||
tester1 := []int{}
|
||||
for k := 0; k < n; k++ {
|
||||
if utils.NthBit(k, i) == 0 {
|
||||
tester0 = append(tester0, k)
|
||||
} else {
|
||||
tester1 = append(tester1, k)
|
||||
}
|
||||
}
|
||||
/*
|
||||
* NOTE: tester1 ist virtuell: aus den Effekten auf tester0 und den Annahmen
|
||||
* lassen sich die Effekte auf tester0 erschließen.
|
||||
* Darum zählen wir nicht 2 sondern 1 Vorkoster.
|
||||
*/
|
||||
metrics.AddSpaceCost(1)
|
||||
logging.LogDebug(fmt.Sprintf("Füge Vorkoster hinzu, der alle Getränke k testet mit %[1]v. Bit von k = 0.", i))
|
||||
testers = append(testers, tester0)
|
||||
testers = append(testers, tester1)
|
||||
}
|
||||
logging.LogDebug("Warte auf Effekte")
|
||||
effects := waitForEffects(L, testers)
|
||||
logging.LogDebug("Effekte auswerten, um vergiftete Getränke zu lokalisieren.")
|
||||
poisened := evaluateEffects(testers, effects)
|
||||
if len(poisened) > 0 {
|
||||
return poisened[0]
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* AUXILIARY METHOD wait for effects, evaluate effects
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
||||
|
||||
/*
|
||||
Inputs:
|
||||
|
||||
- L = Liste von Getränken: durch boolesche Werte wird dargestellt, ob ein Getränk vergiftet ist.
|
||||
|
||||
- testers = Liste von Vorkostern. Jeder Vorkoster kostet eine 'Teilliste' der Getränke.
|
||||
|
||||
Outputs: effects = eine Liste, die jedem Vorkoster zuordnet, wie viele vergiftete Getränke er konsumiert hat.
|
||||
*/
|
||||
func waitForEffects(L []int, testers [][]int) []int {
|
||||
m := len(testers)
|
||||
effects := make([]int, m)
|
||||
for i, tester := range testers {
|
||||
effect := 0
|
||||
for _, k := range tester {
|
||||
effect += L[k]
|
||||
}
|
||||
effects[i] = effect
|
||||
}
|
||||
return effects
|
||||
}
|
||||
|
||||
/*
|
||||
Inputs:
|
||||
|
||||
- testers = Liste von Vorkostern. Jeder Vorkoster kostet eine 'Teilliste' der Getränke.
|
||||
|
||||
- effects = eine Liste, die jedem Vorkoster zuordnet, wie viele vergiftete Getränke er konsumiert hat.
|
||||
|
||||
Annahmen: Vorkoster wurden so angewiesen, dass es garantiert ist, vergiftete Getränke zu finden, wenn es die gibt.
|
||||
|
||||
Outputs: Liste der Indexes aller vergifteten Getränke.
|
||||
*/
|
||||
func evaluateEffects(testers [][]int, effects []int) []int {
|
||||
var states = map[int]bool{}
|
||||
var poisened = []int{}
|
||||
|
||||
// Werte Effekte aus, um Gift zu lokalisieren:
|
||||
// Zuerst die Indexes der Getränke bei allen vergifteten Tester zusammenführen:
|
||||
for i, tester := range testers {
|
||||
// wenn Tester positiv testet, dann ist eines der von ihm probierten Getränks vergiftet
|
||||
if effects[i] > 0 {
|
||||
// markiere alle vom Tester probierten Getränke
|
||||
for _, k := range tester {
|
||||
states[k] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
// jetzt eliminieren wir alle Getränke, die von nicht vergifteten Testern konsumiert wurden:
|
||||
for i, tester := range testers {
|
||||
// wenn Tester negativ testet, dann ist KEINES der von ihm probierten Getränks vergiftet
|
||||
if effects[i] == 0 {
|
||||
// schließe alle vom Tester probierten Getränke aus
|
||||
for _, k := range tester {
|
||||
states[k] = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// was übrig bleibt sind die vergifteten Getränke, vorausgesetzt genug Vorkoster wurden ausgewählt
|
||||
for k, state := range states {
|
||||
if state {
|
||||
poisened = append(poisened, k)
|
||||
}
|
||||
}
|
||||
|
||||
return poisened
|
||||
}
|
||||
136
code/golang/internal/algorithms/search/poison/poison_fancy.go
Normal file
136
code/golang/internal/algorithms/search/poison/poison_fancy.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package poison
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"ads/internal/core/metrics"
|
||||
"ads/internal/core/utils"
|
||||
"ads/internal/setup"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* GLOBAL VARIABLES/CONSTANTS
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
||||
|
||||
//
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* CHECKS
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
||||
|
||||
func preChecks(L []int, _ ...interface{}) error {
|
||||
s := utils.SumListInt(L)
|
||||
if !(s > 0) {
|
||||
return fmt.Errorf("Mindestens ein Getränk muss vergiftet sein!")
|
||||
} else if !(s <= 1) {
|
||||
return fmt.Errorf("Höchstens ein Getränk darf vergiftet sein!")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func postChecks(L []int, index int, _ ...interface{}) error {
|
||||
if !(index >= 0) {
|
||||
return fmt.Errorf("Der Algorithmus hat kein vergiftetes Getränk gefunden, obwohl per Annahme eines existiert.")
|
||||
} else if !(L[index] > 0) {
|
||||
return fmt.Errorf("Der Algorithmus hat das vergiftete Getränk nicht erfolgreich bestimmt.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* ALGORITHM find poison + Display
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
||||
|
||||
func FancyFindPoison(input_L []int) (int, error) {
|
||||
var name = "Giftsuche (O(n) Vorkoster)"
|
||||
var inputs = map[string]interface{}{
|
||||
"L": input_L,
|
||||
}
|
||||
var outputs map[string]interface{}
|
||||
var (
|
||||
output_index int
|
||||
)
|
||||
var err error
|
||||
|
||||
do_once := true
|
||||
for do_once {
|
||||
do_once = false
|
||||
setup.DisplayStartOfAlgorithm(name, inputs)
|
||||
metrics.RestartMetrics()
|
||||
|
||||
// Prechecks:
|
||||
err = preChecks(input_L)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
// Ausführung des Algorithmus:
|
||||
output_index = FindPoison(input_L)
|
||||
outputs = map[string]interface{}{
|
||||
"index": output_index,
|
||||
}
|
||||
|
||||
// Letzte Messages:
|
||||
setup.DisplayMetrics()
|
||||
setup.DisplayEndOfAlgorithm(outputs)
|
||||
|
||||
// Postchecks:
|
||||
err = postChecks(input_L, output_index)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return output_index, err
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* ALGORITHM find poison fast + Display
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
||||
|
||||
func FancyFindPoisonFast(input_L []int) (int, error) {
|
||||
var name = "Giftsuche (O(log(n)) Vorkoster)"
|
||||
var inputs = map[string]interface{}{
|
||||
"L": input_L,
|
||||
}
|
||||
var outputs map[string]interface{}
|
||||
var (
|
||||
output_index int
|
||||
)
|
||||
var err error
|
||||
|
||||
do_once := true
|
||||
for do_once {
|
||||
do_once = false
|
||||
setup.DisplayStartOfAlgorithm(name, inputs)
|
||||
metrics.RestartMetrics()
|
||||
|
||||
// Prechecks:
|
||||
err = preChecks(input_L)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
// Ausführung des Algorithmus:
|
||||
output_index = FindPoisonFast(input_L)
|
||||
outputs = map[string]interface{}{
|
||||
"index": output_index,
|
||||
}
|
||||
|
||||
// Letzte Messages:
|
||||
setup.DisplayMetrics()
|
||||
setup.DisplayEndOfAlgorithm(outputs)
|
||||
|
||||
// Postchecks:
|
||||
err = postChecks(input_L, output_index)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return output_index, err
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package sequential
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"ads/internal/core/logging"
|
||||
"ads/internal/core/metrics"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* GLOBAL VARIABLES/CONSTANTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
//
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* ALGORITHM sequential search
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
Inputs: L = Liste von Zahlen, x = Zahl.
|
||||
Outputs: Position von x in L, sonst −1 wenn x nicht in L.
|
||||
*/
|
||||
func SequentialSearch(L []int, x int) int {
|
||||
n := len(L)
|
||||
for i := 0; i < n; i++ {
|
||||
metrics.AddTimeCost()
|
||||
if L[i] == x {
|
||||
logging.LogDebug(fmt.Sprintf("Element in Position %[1]v gefunden.", i))
|
||||
return i
|
||||
}
|
||||
logging.LogDebug(fmt.Sprintf("Element nicht in Position %[1]v.", i))
|
||||
}
|
||||
return -1
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package sequential
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"ads/internal/core/metrics"
|
||||
"ads/internal/core/utils"
|
||||
"ads/internal/setup"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* GLOBAL VARIABLES/CONSTANTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
//
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* CHECKS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func preChecks(L []int, _ ...interface{}) error {
|
||||
// Keine Checks!
|
||||
return nil
|
||||
}
|
||||
|
||||
func postChecks(L []int, x int, index int, _ ...interface{}) error {
|
||||
if utils.ArrayContains(L, x) {
|
||||
if !(index >= 0) {
|
||||
return fmt.Errorf("Der Algorithmus sollte nicht -1 zurückgeben.")
|
||||
}
|
||||
if L[index] != x {
|
||||
return fmt.Errorf("Der Algorithmus hat den falschen Index bestimmt.")
|
||||
}
|
||||
} else {
|
||||
if index != -1 {
|
||||
return fmt.Errorf("Der Algorithmus sollte -1 zurückgeben.")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHOD sequential search + Display
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func FancySequentialSearch(input_L []int, input_x int) (int, error) {
|
||||
var name = "Sequenziellsuchalgorithmus"
|
||||
var inputs = map[string]interface{}{
|
||||
"L": input_L,
|
||||
"x": input_x,
|
||||
}
|
||||
var outputs map[string]interface{}
|
||||
var (
|
||||
output_index int
|
||||
)
|
||||
var err error
|
||||
|
||||
do_once := true
|
||||
for do_once {
|
||||
do_once = false
|
||||
setup.DisplayStartOfAlgorithm(name, inputs)
|
||||
metrics.RestartMetrics()
|
||||
|
||||
// Prechecks:
|
||||
err = preChecks(input_L, input_x)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
// Ausführung des Algorithmus:
|
||||
output_index = SequentialSearch(input_L, input_x)
|
||||
outputs = map[string]interface{}{
|
||||
"index": output_index,
|
||||
}
|
||||
|
||||
// Letzte Messages:
|
||||
setup.DisplayMetrics()
|
||||
setup.DisplayEndOfAlgorithm(outputs)
|
||||
|
||||
// Postchecks:
|
||||
err = postChecks(input_L, input_x, output_index)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return output_index, err
|
||||
}
|
||||
169
code/golang/internal/algorithms/sum/maxsubsum/maxsubsum.go
Normal file
169
code/golang/internal/algorithms/sum/maxsubsum/maxsubsum.go
Normal file
@@ -0,0 +1,169 @@
|
||||
package maxsubsum
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"ads/internal/core/logging"
|
||||
"ads/internal/core/metrics"
|
||||
"ads/internal/core/utils"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* GLOBAL VARIABLES/CONSTANTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
//
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* ALGORITHM max sub sum
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
Inputs: L = Liste von Zahlen
|
||||
|
||||
Outputs:
|
||||
|
||||
- maxSum = Wert der maximalen Summe einer Teilliste aufeinanderfolgender Elemente
|
||||
|
||||
- u, v = Indexes so dass die Teilliste [L[u], L[u+1], ..., L[v]] die maximale Summe aufweist
|
||||
*/
|
||||
func MaxSubSum(L []int) (int, int, int) {
|
||||
maxSum := 0
|
||||
u := 0
|
||||
v := -1
|
||||
for i := 0; i < len(L); i++ {
|
||||
// Bestimme maximale Teilsumme der linken Rände der Liste ab Index i {
|
||||
maxSum_, _, k := lRandSum(L[i:])
|
||||
if maxSum_ > maxSum {
|
||||
k += i // NOTE: muss wegen Offset kompensieren
|
||||
maxSum, u, v = maxSum_, i, k
|
||||
logging.LogDebug(fmt.Sprintf("max Teilsumme aktualisiert: Summe L[i] von i=%[1]v .. %[2]v = %[3]v", u, v, maxSum))
|
||||
}
|
||||
}
|
||||
return maxSum, u, v
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* ALGORITHM max sub sum (D & C)
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
Inputs: L = Liste von Zahlen
|
||||
|
||||
Outputs:
|
||||
|
||||
- maxSum = Wert der maximalen Summe einer Teilliste aufeinanderfolgender Elemente
|
||||
|
||||
- u, v = Indexes so dass die Teilliste [L[u], L[u+1], ..., L[v]] die maximale Summe aufweist
|
||||
*/
|
||||
func MaxSubSumDC(L []int) (int, int, int) {
|
||||
maxSum := 0
|
||||
u := 0
|
||||
v := -1
|
||||
if len(L) == 1 {
|
||||
// wenn Liste aus 1 Element besteht, nicht teilen:
|
||||
if L[0] > maxSum {
|
||||
v = 0
|
||||
maxSum = L[0]
|
||||
}
|
||||
} else {
|
||||
u = utils.Ceil(float64(len(L)) / 2)
|
||||
Ll := L[:u]
|
||||
Lr := L[u:]
|
||||
// berechnet maximale Teilsumme der linken Hälfte:
|
||||
maxSum1, u1, v1 := MaxSubSumDC(Ll)
|
||||
// berechnet maximale Teilsumme der rechten Hälfte:
|
||||
maxSum2, u2, v2 := MaxSubSumDC(Lr)
|
||||
u2, v2 = u2+len(Ll), v2+len(Ll) // offsets kompensieren
|
||||
// berechnet maximale Teilsumme mit Überschneidung zw. den Hälften:
|
||||
maxSum3, u3, v3 := lrRandSum(Ll, Lr)
|
||||
// bestimme Maximum der 3 Möglichkeiten:
|
||||
maxSum = utils.MaxInt(maxSum1, maxSum2, maxSum3)
|
||||
if maxSum == maxSum1 {
|
||||
maxSum, u, v = maxSum1, u1, v1
|
||||
logging.LogDebug(fmt.Sprintf("max Teilsumme kommt in linker Partition vor: Summe L[i] von i=%[1]v .. %[2]v = %[3]v", u, v, maxSum))
|
||||
} else if maxSum == maxSum3 {
|
||||
maxSum, u, v = maxSum3, u3, v3
|
||||
logging.LogDebug(fmt.Sprintf("max Teilsumme kommt in Überschneidung vor: Summe L[i] von i=%[1]v .. %[2]v = %[3]v", u, v, maxSum))
|
||||
} else { // } else if maxSum == maxSum2 {
|
||||
maxSum, u, v = maxSum2, u2, v2
|
||||
logging.LogDebug(fmt.Sprintf("max Teilsumme kommt in rechter Partition vor: Summe L[i] von i=%[1]v .. %[2]v = %[3]v", u, v, maxSum))
|
||||
}
|
||||
}
|
||||
return maxSum, u, v
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* AUXILIARY METHODS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
Bestimmt maximale Teilsumme einer Teiliste einer Liste,
|
||||
wobei die Liste in zwei Intervalle partitioniert ist
|
||||
und die Teilliste beide überschneidet.
|
||||
|
||||
Inputs: Ll, Lr = eine Partition einer Liste von Zahlen in zwei Intervalle
|
||||
|
||||
Outputs: maxSum, u=0, v
|
||||
*/
|
||||
func lrRandSum(Ll []int, Lr []int) (int, int, int) {
|
||||
maxSumL, u, _ := rRandSum(Ll)
|
||||
maxSumR, _, v := lRandSum(Lr)
|
||||
maxSum := maxSumL + maxSumR
|
||||
v += len(Ll) // offsets kompensieren
|
||||
return maxSum, u, v
|
||||
}
|
||||
|
||||
/*
|
||||
Bestimmt maximale Teilsumme aller nicht leeren linken Segmente einer Liste.
|
||||
|
||||
Inputs: L = Liste von Zahlen
|
||||
|
||||
Outputs: maxSum, u(=0), v
|
||||
*/
|
||||
func lRandSum(L []int) (int, int, int) {
|
||||
n := len(L)
|
||||
// berechne kumulative Summen (vorwärts)
|
||||
metrics.AddTimeCost(n)
|
||||
total := L[0]
|
||||
maxSum := total
|
||||
u := 0
|
||||
v := 0
|
||||
for i := 0; i < n; i++ {
|
||||
total += L[i]
|
||||
if total > maxSum {
|
||||
v = i
|
||||
maxSum = total
|
||||
}
|
||||
}
|
||||
return maxSum, u, v
|
||||
}
|
||||
|
||||
/*
|
||||
Bestimmt maximale Teilsumme aller nicht leeren rechten Segmente einer Liste.
|
||||
|
||||
Inputs: L = Liste von Zahlen
|
||||
|
||||
Outputs: maxSum, u, v(=len(L)-1)
|
||||
*/
|
||||
func rRandSum(L []int) (int, int, int) {
|
||||
n := len(L)
|
||||
// berechne kumulative Summen (rückwärts)
|
||||
metrics.AddTimeCost(n)
|
||||
total := L[n-1]
|
||||
maxSum := total
|
||||
u := n - 1
|
||||
v := n - 1
|
||||
for i := n - 2; i >= 0; i-- {
|
||||
total += L[i]
|
||||
if total > maxSum {
|
||||
u = i
|
||||
maxSum = total
|
||||
}
|
||||
}
|
||||
return maxSum, u, v
|
||||
}
|
||||
136
code/golang/internal/algorithms/sum/maxsubsum/maxsubsum_fancy.go
Normal file
136
code/golang/internal/algorithms/sum/maxsubsum/maxsubsum_fancy.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package maxsubsum
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"ads/internal/core/metrics"
|
||||
"ads/internal/setup"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* GLOBAL VARIABLES/CONSTANTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
//
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* CHECKS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func preChecks(L []int, _ ...interface{}) error {
|
||||
if !(len(L) > 0) {
|
||||
return fmt.Errorf("Liste darf nicht leer sein.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func postChecks(L []int, _ ...interface{}) error {
|
||||
// TODO
|
||||
return nil
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHOD Algorithm + Display
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func FancyMaxSubSum(input_L []int) (int, int, int, error) {
|
||||
var name = "MaxSubSum (Maximale Teilsumme)"
|
||||
var inputs = map[string]interface{}{
|
||||
"L": input_L,
|
||||
}
|
||||
var outputs map[string]interface{}
|
||||
var (
|
||||
output_maxSum int
|
||||
output_indexFrom int
|
||||
output_indexTo int
|
||||
)
|
||||
var err error
|
||||
|
||||
do_once := true
|
||||
for do_once {
|
||||
do_once = false
|
||||
setup.DisplayStartOfAlgorithm(name, inputs)
|
||||
metrics.RestartMetrics()
|
||||
|
||||
// Prechecks:
|
||||
err = preChecks(input_L)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
// Ausführung des Algorithmus:
|
||||
output_maxSum, output_indexFrom, output_indexTo = MaxSubSum(input_L)
|
||||
outputs = map[string]interface{}{
|
||||
"maxSum": output_maxSum,
|
||||
"index_from": output_indexFrom,
|
||||
"index_to": output_indexTo,
|
||||
}
|
||||
|
||||
// Letzte Messages:
|
||||
setup.DisplayMetrics()
|
||||
setup.DisplayEndOfAlgorithm(outputs)
|
||||
|
||||
// Postchecks:
|
||||
err = postChecks(input_L, output_maxSum, output_indexFrom, output_indexTo)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return output_maxSum, output_indexFrom, output_indexTo, err
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* ALGORITHM max sub sum (D & C)
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func FancyMaxSubSumDC(input_L []int) (int, int, int, error) {
|
||||
var name = "MaxSubSum (Maximale Teilsumme mit D & C)"
|
||||
var inputs = map[string]interface{}{
|
||||
"L": input_L,
|
||||
}
|
||||
var outputs map[string]interface{}
|
||||
var (
|
||||
output_maxSum int
|
||||
output_indexFrom int
|
||||
output_indexTo int
|
||||
)
|
||||
var err error
|
||||
|
||||
do_once := true
|
||||
for do_once {
|
||||
do_once = false
|
||||
setup.DisplayStartOfAlgorithm(name, inputs)
|
||||
metrics.RestartMetrics()
|
||||
|
||||
// Prechecks:
|
||||
err = preChecks(input_L)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
// Ausführung des Algorithmus:
|
||||
output_maxSum, output_indexFrom, output_indexTo = MaxSubSumDC(input_L)
|
||||
outputs = map[string]interface{}{
|
||||
"maxSum": output_maxSum,
|
||||
"index_from": output_indexFrom,
|
||||
"index_to": output_indexTo,
|
||||
}
|
||||
|
||||
// Letzte Messages:
|
||||
setup.DisplayMetrics()
|
||||
setup.DisplayEndOfAlgorithm(outputs)
|
||||
|
||||
// Postchecks:
|
||||
err = postChecks(input_L, output_maxSum, output_indexFrom, output_indexTo)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return output_maxSum, output_indexFrom, output_indexTo, err
|
||||
}
|
||||
Reference in New Issue
Block a user