Skip to content

Instantly share code, notes, and snippets.

@ocindev
Created May 13, 2020 16:03
Show Gist options
  • Save ocindev/b800b0637d174ed52407ea31ff9d4414 to your computer and use it in GitHub Desktop.
Save ocindev/b800b0637d174ed52407ea31ff9d4414 to your computer and use it in GitHub Desktop.
short_4 recursive function to determine the shortest ways through directed graph.
import java.util.*;
public class Algorithm {
private List<Point> V;
private List<Edge> E;
Algorithm(List<Point> V, List<Edge> E){
this.V = V;
this.E = E;
}
List<Point> getV() {
return V;
}
List<Edge> getE() {
return E;
}
static class Point {
Character c;
Point(final Character c){
this.c = c;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
return Objects.equals(c, point.c);
}
@Override
public int hashCode() {
return Objects.hash(c);
}
@Override
public String toString() {
return "Point{" +
"c=" + c +
'}';
}
}
static class Edge {
Point source;
Point target;
double distance;
Edge(final Point s, final Point t, final double d) {
this.source = s;
this.target = t;
this.distance = d;
}
double getDistance(){
return distance;
}
}
void startAlgorithm(int c){
int k = 0;
while(k <= c){
double[][] matrix = new double[V.size()][V.size()];
int i = 0;
for (Point v : V) {
int j = 0;
for(Point v_ : V){
double d = short_4(v, v_, k);
matrix[i][j] = d;
j++;
}
i++;
}
printMatrix(matrix, k);
k++;
}
}
double dist(Point s, Point t){
if(s.equals(t)) return 0;
return E.stream().filter(e -> e.source.equals(s) && e.target.equals(t)).findAny().map(Edge::getDistance).orElse(Double.POSITIVE_INFINITY);
}
public double short_4(Point s, Point t, int k){
if(k == 0){
return dist(s, t);
}
return V.stream().map(v -> short_4(s, v, k-1) + short_4(v, t, k-1)).min(Comparator.comparing(Double::valueOf)).orElse(Double.POSITIVE_INFINITY);
}
public static void main(final String[] args) {
Algorithm short4Algorithm = prepareShortAlgorithm();
short4Algorithm.startAlgorithm(3);
}
static void printMatrix(double[][] matrix, int k ){
StringBuilder sb = new StringBuilder();
sb.append("\nk=").append(k).append("\n");
for (double[] doubles : matrix) {
for (int j = 0; j < matrix[0].length; j++) {
sb.append("[ ").append(doubles[j]).append(" ]");
}
sb.append("\n");
}
System.out.println(sb.toString());
}
static Algorithm prepareShortAlgorithm(){
Point one = new Point('1');
Point two = new Point('2');
Point three = new Point('3');
Point four = new Point('4');
Point five = new Point('5');
Point six = new Point('6');
List<Point> V = Arrays.asList(one, two, three, four, five, six);
Edge one_two = new Edge(one, two, 5.0);
Edge one_four = new Edge(one, four, 2.0);
Edge two_three = new Edge(two, three, 5.0);
Edge two_five = new Edge(two, five, 2.0);
Edge three_six = new Edge(three, six, 2.0);
Edge four_two = new Edge(four, two, 3.0);
Edge four_five = new Edge(four, five, 5.0);
Edge five_three = new Edge(five, three, 3.0);
Edge five_six = new Edge(five, six, 5.0);
List<Edge> E = Arrays.asList(one_two, one_four, two_three, two_five, three_six, four_two, four_five, five_three, five_six);
return new Algorithm(V, E);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment