lol
This commit is contained in:
commit
613bef2496
108 changed files with 8397 additions and 0 deletions
39
src/zutil/algo/path/DijkstraPathFinder.java
Normal file
39
src/zutil/algo/path/DijkstraPathFinder.java
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package zutil.algo.path;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class DijkstraPathFinder {
|
||||
|
||||
public static LinkedList<PathNode> find(PathNode start, PathNode stop){
|
||||
// TODO
|
||||
/*
|
||||
1
|
||||
|
||||
5 dist[source] := 0 // Distance from source to source
|
||||
6 Q := copy(Graph) // All nodes in the graph are unoptimized - thus are in Q
|
||||
7 while Q is not empty: // The main loop
|
||||
8 u := extract_min(Q) // Remove and return best vertex from nodes in two given nodes
|
||||
// we would use a path finding algorithm on the new graph, such as depth-first search.
|
||||
9 for each neighbor v of u: // where v has not yet been considered
|
||||
10 alt = dist[u] + length(u, v)
|
||||
11 if alt < dist[v] // Relax (u,v)
|
||||
12 dist[v] := alt
|
||||
13 previous[v] := u
|
||||
14 return previous[]
|
||||
*/
|
||||
|
||||
|
||||
|
||||
LinkedList<PathNode> path = new LinkedList<PathNode>();
|
||||
PathNode current = stop;
|
||||
while(true){
|
||||
path.addFirst(current);
|
||||
current = current.getSourceNeighbor();
|
||||
if(current.equals(start)){
|
||||
path.addFirst(start);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return path;
|
||||
}
|
||||
}
|
||||
117
src/zutil/algo/path/DynamicProgramming.java
Normal file
117
src/zutil/algo/path/DynamicProgramming.java
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
package zutil.algo.path;
|
||||
|
||||
public class DynamicProgramming {
|
||||
public static char[][] words = new char[][]{
|
||||
"bibba".toCharArray(),
|
||||
"bitas".toCharArray(),
|
||||
"brott".toCharArray(),
|
||||
"blöja".toCharArray(),
|
||||
"boson".toCharArray()
|
||||
};
|
||||
|
||||
public static void main(String[] args){
|
||||
new DynamicProgramming().search();
|
||||
}
|
||||
/*
|
||||
|
||||
int search(words[][][])
|
||||
matrix[][][] = 0
|
||||
shortest = -1
|
||||
|
||||
for w=0->length(words)
|
||||
for y=0->length(words)
|
||||
for x=0->length(words)
|
||||
// första raden i matrisen
|
||||
if y == 0
|
||||
// finns första bokstaven i rätt position i första ordet?
|
||||
if words[0][x] != words[w][0]
|
||||
matrix[w][y][x] = -1
|
||||
else
|
||||
matrix[w][y][x] = 0
|
||||
else
|
||||
// om föregående är negativ sätt nuvarande till negativ
|
||||
if matrix[w][y-1][x] < 0
|
||||
matrix[w][y-1][x] = -1
|
||||
// här så händer det riktiga i algoritmen
|
||||
else
|
||||
tmp = minstaForskjutning(words[y], words[w][y], x)
|
||||
if tmp >= 0
|
||||
matrix[w][y][x] = matrix[w][y-1][x] + tmp
|
||||
else
|
||||
matrix[w][y][x] = -1
|
||||
// kolla om det är sista raden i matrisen
|
||||
if y == length(matrix)
|
||||
if (tmp < shortest || shortest < 0) && tmp >= 0
|
||||
shortest = tmp;
|
||||
|
||||
return shortest
|
||||
|
||||
int minstaForskjutning(word[], find, index){
|
||||
minsta = -1
|
||||
for i=0->length(word)
|
||||
if word[i] == cfind && (abs(index-i) < minsta || minsta < 0)
|
||||
minsta = abs(index-i)
|
||||
|
||||
return minsta
|
||||
|
||||
*/
|
||||
|
||||
public int search(){
|
||||
int[][][] matrix = new int[words.length][words.length][words.length];
|
||||
int shortest = -1;
|
||||
|
||||
for(int w=0; w<words.length ;w++){ //lodräta ordet
|
||||
System.out.print("\n\n"+new String(words[w])+"\n ");
|
||||
for(int y=0; y<words.length ;y++){ // vågräta ordet
|
||||
System.out.print("\n"+ new String(words[y])+": ");
|
||||
for(int x=0; x<words.length ;x++){ // psition i y
|
||||
// första vågräta ordet
|
||||
if(y == 0){
|
||||
if(words[0][x] != words[w][0]){
|
||||
matrix[w][y][x] = -1;
|
||||
}
|
||||
else{
|
||||
matrix[w][y][x] = 0;
|
||||
}
|
||||
}
|
||||
//resten av de vågräta orden
|
||||
else{
|
||||
if(matrix[w][y-1][x] < 0){
|
||||
matrix[w][y][x] = -1;
|
||||
}
|
||||
else{
|
||||
int tmp = minstaForskjutning(words[y], words[w][y], x);
|
||||
if(tmp >= 0){
|
||||
matrix[w][y][x] = matrix[w][y-1][x] + tmp;
|
||||
}
|
||||
else{
|
||||
matrix[w][y][x] = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(y == words.length-1){
|
||||
int tmp = matrix[w][y][x];
|
||||
if((tmp<shortest || shortest<0)
|
||||
&& tmp>= 0){
|
||||
shortest = tmp;
|
||||
}
|
||||
}
|
||||
System.out.print(" "+matrix[w][y][x]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("\n\nKortaste förflyttningen: "+shortest);
|
||||
return shortest;
|
||||
}
|
||||
|
||||
private int minstaForskjutning(char[] word, char cfind, int index){
|
||||
int minsta = -1;
|
||||
for(int i=0; i<word.length ;i++){
|
||||
if(word[i] == cfind && (Math.abs(index-i)<minsta || minsta<0)){
|
||||
minsta = Math.abs(index-i);
|
||||
}
|
||||
}
|
||||
return minsta;
|
||||
}
|
||||
}
|
||||
17
src/zutil/algo/path/PathNode.java
Normal file
17
src/zutil/algo/path/PathNode.java
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package zutil.algo.path;
|
||||
|
||||
|
||||
public interface PathNode {
|
||||
|
||||
public void setVisited(boolean b);
|
||||
|
||||
public boolean visited();
|
||||
|
||||
public Iterable<PathNode> getNeighbors();
|
||||
|
||||
public int getNeighborCost(PathNode neighbor);
|
||||
|
||||
public void setSourceNeighbor(PathNode neighbor);
|
||||
|
||||
public PathNode getSourceNeighbor();
|
||||
}
|
||||
38
src/zutil/algo/path/PathNodeDefault.java
Normal file
38
src/zutil/algo/path/PathNodeDefault.java
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
package zutil.algo.path;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class PathNodeDefault implements PathNode{
|
||||
private HashMap<PathNode,Integer> neighbors;
|
||||
private PathNode neighbor;
|
||||
private boolean visited;
|
||||
|
||||
public PathNodeDefault(){
|
||||
neighbors = new HashMap<PathNode,Integer>();
|
||||
visited = false;
|
||||
}
|
||||
|
||||
public void setVisited(boolean b){
|
||||
visited = b;
|
||||
}
|
||||
|
||||
public int getNeighborCost(PathNode neighbor) {
|
||||
return neighbors.get(neighbor);
|
||||
}
|
||||
|
||||
public Iterable<PathNode> getNeighbors() {
|
||||
return neighbors.keySet();
|
||||
}
|
||||
|
||||
public boolean visited() {
|
||||
return visited;
|
||||
}
|
||||
|
||||
public void setSourceNeighbor(PathNode n) {
|
||||
neighbor = n;
|
||||
}
|
||||
|
||||
public PathNode getSourceNeighbor() {
|
||||
return neighbor;
|
||||
}
|
||||
}
|
||||
95
src/zutil/algo/path/SimplePathFinder.java
Normal file
95
src/zutil/algo/path/SimplePathFinder.java
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
package zutil.algo.path;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
/**
|
||||
* A class for simple path finding algorithms
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class SimplePathFinder {
|
||||
|
||||
/**
|
||||
* Returns the first path to the destination
|
||||
*
|
||||
* @param start Start Node
|
||||
* @param stop Stop Node
|
||||
* @return A list with the path
|
||||
*/
|
||||
public LinkedList<PathNode> BreadthFirstSearch(PathNode start, PathNode stop){
|
||||
Queue<PathNode> queue = new LinkedList<PathNode>();
|
||||
|
||||
queue.add(start);
|
||||
start.setVisited(true);
|
||||
|
||||
PathNode tmp;
|
||||
while(!queue.isEmpty()){
|
||||
tmp = queue.poll();
|
||||
|
||||
for(PathNode next : tmp.getNeighbors()){
|
||||
if(!next.visited() && tmp.getNeighborCost(next) > 0){
|
||||
queue.add(next);
|
||||
next.setVisited(true);
|
||||
next.setSourceNeighbor(tmp);
|
||||
|
||||
if(next.equals(stop)){
|
||||
LinkedList<PathNode> path = new LinkedList<PathNode>();
|
||||
for(PathNode current=stop; !current.equals(start) ;current = current.getSourceNeighbor()){
|
||||
path.addFirst(current);
|
||||
}
|
||||
path.addFirst(start);
|
||||
return path;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new LinkedList<PathNode>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first path to the destination
|
||||
*
|
||||
* @param start Start Node
|
||||
* @param stop Stop Node
|
||||
* @return A list with the path
|
||||
*/
|
||||
public LinkedList<PathNode> DepthFirstSearch(PathNode start, PathNode stop){
|
||||
LinkedList<PathNode> path = new LinkedList<PathNode>();
|
||||
PathNode current = DepthFirstSearchInternal(start, stop);
|
||||
while(current != null){
|
||||
path.addFirst(current);
|
||||
current = current.getSourceNeighbor();
|
||||
if(current.equals(start)){
|
||||
path.addFirst(start);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* The DepthFirstSearch algorithm
|
||||
* @param node The node to search from
|
||||
* @return The stop PathNode if a path was found else null
|
||||
*/
|
||||
private PathNode DepthFirstSearchInternal(PathNode node, PathNode stop){
|
||||
node.setVisited(true);
|
||||
if(node.equals(stop)){
|
||||
return node;
|
||||
}
|
||||
|
||||
for(PathNode next : node.getNeighbors()){
|
||||
if(!next.visited() && node.getNeighborCost(next) > 0){
|
||||
next.setSourceNeighbor(node);
|
||||
PathNode tmp = DepthFirstSearchInternal(next, stop);
|
||||
if(tmp != null){
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue