grr...merge count

This commit is contained in:
Armin Wolf 2017-06-18 15:22:43 +02:00
parent da8e5d800c
commit dba545b114
4 changed files with 76 additions and 51 deletions

View File

@ -0,0 +1,51 @@
package Presenter.Algorithms;
import java.util.List;
/**
* Created by armin on 18.06.17.
*/
public class InversionCounter {
/**
* Angepasster Merge-Sort Algorithmus.
* Die Funktion bekommt neben den standard Parametern zusätzlich eine Liste mit Elementen
* die als Groundtruth dienen.
* @param a Eingabefeld mit den Elementen die überprüft werden sollen.
* @param start Startpunkt des Eingabefeldes.
* @param end Endpunkt des Eingabefeldes.
* @param aux Groundtruth Ordnung um die Anzahl der Inversionen zu bestimmen.
* @return Anzahl der inversionen zwischen a und aux.
*/
public static int run(List<Double> a, int start, int end, List<Double> aux) {
if (start >= end) {
return 0;
}
int invCount = 0;
int mid = start + (end - start) / 2;
int invCountLeft = run(a, start, mid, aux); // divide and conquer
int invCountRight = run(a, mid + 1, end, aux); // divide and conquer
invCount += (invCountLeft + invCountRight);
for (int i = start; i <= end; i++) {
aux.set(i, a.get(i));
}
int left = start;
int right = mid + 1;
int index = start;
while (left <= mid && right <= end) {
if (aux.get(left) < aux.get(right)) {
a.set(index++, aux.get(left++));
} else {
a.set(index++, aux.get(right++));
invCount += mid - left + 1; // number of inversions for aux[right]
}
}
while (left <= mid) {
a.set(index++, aux.get(left++));
}
// no need to copy over remaining aux[right++] because they are already inside a
return invCount;
}
}

View File

@ -4,9 +4,6 @@ import Model.Line;
import Model.Point;
import Model.Slab;
import Presenter.Presenter;
import View.MainFrame;
import javafx.beans.*;
import sun.applet.Main;
import java.util.*;
import java.util.Observable;
@ -158,50 +155,12 @@ public class LeastMedianOfSquaresEstimator extends Observable implements Algorit
umax.add((slab.getUpper() * p.getM()) + p.getB());
}
numberOfInversions = mergeSort(umin, 0, umin.size() - 1, umax);
numberOfInversions = InversionCounter.run(umin, 0, umin.size() - 1, umax);
return numberOfInversions;
}
/**
* Angepasster Merge-Sort Algorithmus.
* Die Funktion bekommt neben den standard Parametern zusätzlich eine Liste mit Elementen
* die als Groundtruth dienen.
* @param a Eingabefeld mit den Elementen die überprüft werden sollen.
* @param start Startpunkt des Eingabefeldes.
* @param end Endpunkt des Eingabefeldes.
* @param aux Groundtruth Ordnung um die Anzahl der Inversionen zu bestimmen.
* @return Anzahl der inversionen zwischen a und aux.
*/
public int mergeSort(List<Double> a, int start, int end, List<Double> aux) {
if (start >= end) {
return 0;
}
int invCount = 0;
int mid = start + (end - start) / 2;
int invCountLeft = mergeSort(a, start, mid, aux); // divide and conquer
int invCountRight = mergeSort(a, mid + 1, end, aux); // divide and conquer
invCount += (invCountLeft + invCountRight);
for (int i = start; i <= end; i++) {
aux.set(i, a.get(i));
}
int left = start;
int right = mid + 1;
int index = start;
while (left <= mid && right <= end) {
if (aux.get(left) < aux.get(right)) {
a.set(index++, aux.get(left++));
} else {
a.set(index++, aux.get(right++));
invCount += mid - left + 1; // number of inversions for aux[right]
}
}
while (left <= mid) {
a.set(index++, aux.get(left++));
}
// no need to copy over remaining aux[right++] because they are already inside a
return invCount;
}
/**
* @param slab

View File

@ -129,8 +129,22 @@ public class RepeatedMedianEstimator implements Algorithm {
return i+1;
}
public void countNumberOfIntersectionsAbscissas(){}
public void estimateMedianIntersectionAbscissas(ArrayList<Line> sampledLines){}
public void countNumberOfIntersectionsAbscissas(){
}
public void estimateMedianIntersectionAbscissas(ArrayList<Line> sampledLines){
ArrayList<Double> low = new ArrayList<>();
ArrayList<Double> high = new ArrayList<>();
for (Line line : sampledLines){
low.add(interval.getLower() * line.getM() + line.getB());
high.add(interval.getUpper() * line.getM() + line.getB());
}
int inversions = InversionCounter.run(low, 0, low.size()-1, high);
}
}

View File

@ -7,9 +7,7 @@ import Model.Slab;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import static org.junit.Assert.*;
@ -51,12 +49,14 @@ public class LeastMedianOfSquaresEstimatorTest {
@Test
public void mergeSort() throws Exception {
double[] umin = {1,5,6,4,20};
double[] umax = {1,4,6,5,20};
//double[] umin = {1,5,6,4,20};
//double[] umax = {1,4,6,5,20};
double[] umin = {1,2,3,4};
double[] umax = {1,4,2,3};
ArrayList<Double> a = new ArrayList<>();
ArrayList<Double> b = new ArrayList<>();
for (double d :umin) {
a.add(d);
}
@ -64,9 +64,10 @@ public class LeastMedianOfSquaresEstimatorTest {
for (double d :umax) {
b.add(d);
}
/
int ret = InversionCounter.run(a,0,a.size()-1,b);
int ret = lms.mergeSort(a,0,a.size()-1,b);
assertEquals(2, ret);
}