Quantcast
Channel: Strg+Shift+R
Viewing all articles
Browse latest Browse all 50

How to create algorithms which are independent from the used datastructure

$
0
0
Normally if you want to use an existing algorithm found in the internet you have to adapt it so that it works on your object structure. But if the algorithm is written properly you don't have to! So next time you write an algorithm, maybe you want to do it like the following example:

I just implemented the MiniMax algol. A very simple algorithm, so its perfect for this demonstration how to create algorithms which can be used on any object structure. Here is the algorithm class:


import java.util.List;

public class MiniMaxAlgo {

public interface MinMaxTree {
/**
* will only be called on nodes that have no children by this algorithm
*
* @return
*/
int getUtility();

/**
* @return null if the node is a leaf
*/
List getChildren();
}

public interface ResultListener {

void resultFound(MinMaxTree bestSubtree);

}

public void getDecision(MinMaxTree l, ResultListener r) {
getDecision(l, r, true);
}

public void getDecision(MinMaxTree l, ResultListener r, boolean maxStarts) {
if (maxStarts)
maxValue(l, r);
else
minValue(l, r);
}

private int maxValue(MinMaxTree l, ResultListener r) {
List list = l.getChildren();
if (list == null)
return l.getUtility();

int result = Integer.MIN_VALUE;
MinMaxTree bestSubtree = null;
for (MinMaxTree a : list) {
int possibleMax = minValue(a, null);
if (possibleMax > result) {
result = possibleMax;
bestSubtree = a;
}
}
if (r != null) {
r.resultFound(bestSubtree);
}
return result;
}

private int minValue(MinMaxTree l, ResultListener r) {
List list = l.getChildren();
if (list == null)
return l.getUtility();

int result = Integer.MAX_VALUE;
MinMaxTree bestSubtree = null;
for (MinMaxTree a : list) {
int possibleMin = maxValue(a, null);
if (possibleMin < result) {
result = possibleMin;
bestSubtree = a;
}
}
if (r != null) {
r.resultFound(bestSubtree);
}
return result;
}
}
Search the web if you don't now how the algo works. Ok now if we want to use an algorithm normally we have to use the data-structure it operates on. But if you stick close enough to the pseudo-code it is possible to create algorithm which can run on any data-structure. As an example i created my simple custom tree structure:
public class MyTreeStructure implements MiniMaxAlgo.MinMaxTree {

private int utility;
private List myChildren;

public MyTreeStructure(int leafUtility) {
utility = leafUtility;
}

public MyTreeStructure(List list) {
myChildren = list;
}

@Override
public int getUtility() {
return utility;
}

@Override
public List getChildren() {
return myChildren;
}

}
Now i can work with my own object structure and only have to implement the required interface to be allowed to pass it to the algorithm. Here is the sample test code:
MiniMaxAlgo m = new MiniMaxAlgo();

MyTreeStructure leafA = new MyTreeStructure(4);
MyTreeStructure leafB = new MyTreeStructure(5);
MyTreeStructure leafC = new MyTreeStructure(6);
MyTreeStructure leafD = new MyTreeStructure(8);
List list = new ArrayList();
list.add(leafA);
list.add(leafB);
list.add(leafC);
list.add(leafD);
MyTreeStructure myTree = new MyTreeStructure(list);

m.getDecision(myTree, new MiniMaxAlgo.ResultListener() {

@Override
public void resultFound(MiniMaxAlgo.MinMaxTree bestSubtree) {
// this will return the best subtree for the max player
// so it will be leafD in this case
}
});

Viewing all articles
Browse latest Browse all 50

Trending Articles