Skip to content

Commit

Permalink
pushing project
Browse files Browse the repository at this point in the history
finished Held-Karp Implementation
  • Loading branch information
Ben Samsom committed Apr 17, 2018
0 parents commit 225d68a
Show file tree
Hide file tree
Showing 14 changed files with 522 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ExampleGraph10x10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
example graph10x10
Shortest path distance: 6586
Shortest path: 1->10->7->6->5->3->9->8->4->2->1
10
-1 0 648 2625 549 2185 1898 1458 1752 1963 427
-2 648 0 2363 481 2129 2030 1641 1594 1638 557
-3 2625 2362 0 1965 669 1274 1541 920 744 2172
-4 549 481 1965 0 1667 1605 1194 1132 1242 431
-5 2185 2129 669 1667 0 621 906 541 643 1733
-6 1898 2030 1274 1605 621 0 443 662 978 1482
-7 1458 1641 1541 1194 906 443 0 754 1106 1074
-8 1752 1594 920 1132 541 662 754 0 347 1293
-9 1963 1638 744 1242 643 978 1106 347 0 1511
-10 427 557 2172 431 1733 1482 1074 1293 1511 0
19 changes: 19 additions & 0 deletions ExampleGraph15x15.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
example graph15x15
Shortest path distance: 9010
Shortest path: 1->15->2->4->11->12->8->13->9->3->5->6->7->14->10->1
15
-1 0 648 2625 549 2185 1898 1458 1752 1963 427 1743 1817 1899 1060 1148
-2 648 0 2363 481 2129 2030 1641 1594 1638 557 1214 1492 1710 1126 825
-3 2625 2362 0 1965 669 1274 1541 920 744 2172 1623 875 720 1595 3085
-4 549 481 1965 0 1667 1605 1194 1132 1242 431 963 1096 1280 664 1249
-5 2185 2129 669 1667 0 621 906 541 643 1733 1504 733 459 1187 2880
-6 1898 2030 1274 1605 621 0 443 662 978 1482 1669 925 839 1007 2855
-7 1458 1641 1541 1194 906 443 0 754 1106 1074 1447 976 968 638 2477
-8 1752 1594 920 1132 541 662 754 0 347 1293 1015 261 209 724 2319
-9 1963 1638 744 1242 643 978 1106 347 0 1511 961 170 183 970 2361
-10 427 557 2172 431 1733 1482 1074 1293 1511 0 1318 1363 1447 585 1378
-11 1743 1214 1623 963 1504 1669 1447 1015 961 1318 0 813 1078 918 1571
-12 1817 1492 875 1096 733 925 976 261 170 1363 813 0 271 826 2215
-13 1899 1710 720 1280 459 839 968 209 183 1447 1078 271 0 882 2453
-14 1060 1126 1595 664 1187 1007 638 724 970 585 918 826 882 0 1891
-15 1148 825 3085 1249 2880 2855 2477 2319 2361 1378 1571 2215 2453 1891 0
24 changes: 24 additions & 0 deletions ExampleGraph20x20.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
example graph20x20
Shortest path distance: 10162
Shortest path: 9->19>8->13->16->20->3->5->6->7->18->14->10->1->4->2->17->15->11->12->9
20
-1 0 648 2625 549 2185 1898 1458 1752 1963 427 1743 1817 1899 1060 1148 2084 732 1095 1725 2524
-2 648 0 2363 481 2129 2030 1641 1594 1638 557 1214 1492 1710 1126 825 1861 811 1195 1375 2262
-3 2625 2362 0 1965 669 1274 1541 920 744 2172 1623 875 720 1595 3085 543 3113 1734 1111 103
-4 549 481 1965 0 1667 1605 1194 1132 1242 431 963 1096 1280 664 1249 1464 1276 799 979 1866
-5 2185 2129 669 1667 0 621 906 541 643 1733 1504 733 459 1187 2880 479 2791 1169 932 566
-6 1898 2030 1274 1605 621 0 443 662 978 1482 1669 925 839 1007 2855 929 2541 843 1107 1172
-7 1458 1641 1541 1194 906 443 0 754 1106 1074 1447 976 968 638 2477 1148 2132 435 1027 1442
-8 1752 1594 920 1132 541 662 754 0 347 1293 1015 261 209 724 2319 389 2346 841 443 818
-9 1963 1638 744 1242 643 978 1106 347 0 1511 961 170 183 970 2361 287 2389 1124 388 688
-10 427 557 2172 431 1733 1482 1074 1293 1511 0 1318 1363 1447 585 1378 1631 1063 641 1275 2071
-11 1743 1214 1623 963 1504 1669 1447 1015 961 1318 0 813 1078 918 1571 1182 1882 1147 582 1583
-12 1817 1492 875 1096 733 925 976 261 170 1363 813 0 271 826 2215 373 2242 979 241 774
-13 1899 1710 720 1280 459 839 968 209 183 1447 1078 271 0 882 2453 192 2408 1049 504 621
-14 1060 1126 1595 664 1187 1007 638 724 970 585 918 826 882 0 1891 1066 1667 251 798 1495
-15 1148 825 3085 1249 2880 2855 2477 2319 2361 1378 1571 2215 2453 1891 0 2583 559 2042 2108 2984
-16 2084 1861 543 1464 479 929 1148 389 287 1631 1182 373 192 1066 2583 0 2612 1220 611 442
-17 732 811 3113 1276 2791 2541 2132 2346 2389 1063 1882 2242 2408 1667 559 2612 0 1701 2125 3012
-18 1095 1195 1734 799 1169 843 435 841 1124 641 1147 979 1049 251 2042 1220 1701 0 977 1634
-19 1725 1375 1111 979 932 1107 1027 443 388 1275 582 241 504 798 2108 611 2125 977 0 1011
-20 2524 2262 103 1866 566 1172 1442 818 688 2071 1583 774 621 1495 2984 442 3012 1634 1011 0
7 changes: 7 additions & 0 deletions ExampleGraph3x3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
example graph3x3
Shortest path distance: 9
Shortest path: 1->2->3->1
3
-1 0 1 2
-2 6 0 3
-3 5 4 0
8 changes: 8 additions & 0 deletions ExampleGraph4x4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
example graph4x4
Shortest path distance: 21
Shortest path: 1->3->4->2->1
4
-1 0 2 9 10
-2 1 0 6 4
-3 15 7 0 8
-4 6 3 12 0
9 changes: 9 additions & 0 deletions ExampleGraph5x5-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
example graph5x5-2
Shortest path distance: 29
Shortest path: 1->3->5->4->2->1
5
-1 0 8 4 9 9
-2 8 0 6 7 10
-3 4 6 0 5 6
-4 9 7 5 0 4
-5 9 10 6 4 0
9 changes: 9 additions & 0 deletions ExampleGraph5x5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
example graph5x5
Shortest path distance: 9
Shortest path: 1->3->4->5->2->1
5
-1 0 2 0 6 1
-2 1 0 4 4 2
-3 5 3 0 1 5
-4 4 7 2 0 1
-5 2 6 3 6 0
10 changes: 10 additions & 0 deletions ExampleGraph6x6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
example graph6x6
Shortest path distance: 229
Shortest path: 1->6->5->4->3->2->1
6
-1 0 36 32 54 20 40
-2 36 0 22 58 54 67
-3 32 22 0 36 42 71
-4 54 58 36 0 50 92
-5 20 54 42 50 0 45
-6 40 67 71 92 45 0
26 changes: 26 additions & 0 deletions GraphSolutions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
3x3
Shortest path distance: 9
Shortest path: 1->2->3->1
or
Shortest path distance: 9
Shortest path: 1->3->2->1
4x4
Shortest path distance: 21
Shortest path: 1->3->4->2->1
5x5
Shortest path distance: 9
Shortest path: 1->3->4->5->2->1
5x5-2
Shortest path distance: 29
Shortest path: 1->3->5->4->2->1
6x6
Shortest path distance: 229
Shortest path: 1->6->5->4->3->2->1
10x10
Shortest path distance: 6586
Shortest path: 1->10->7->6->5->3->9->8->4->2->1

15x15
Shortest path distance: 9010
Shortest path: 1->15->2->4->11->12->8->13->9->3->5->6->7->14->10->1

79 changes: 79 additions & 0 deletions HashTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import java.util.Set;
public class HashTable{
private class Node {
public Set<Integer> data;
public int vertex;
public int minCost;
public Node next;
public Node(Set<Integer> data, int vertex, int minCost) {
this.data = data;
this.vertex = vertex;
this.minCost = minCost;
this.next = null;
}
}
public HashTable(int size){
list = new Node[size];
for(int i = 0; i < list.length;i++){
list[i] = null;
}
totalSize = 0;
}
private Node[] list;
public int totalSize;
private int hash(int vertex, Set<Integer> data){
if(data != null){
return ((23 * vertex + (data.hashCode())) % list.length);
}
else{
return vertex % list.length;
}
}
public void insert(Set<Integer> input, int vertex, int minCost) {
Node temp = new Node(input,vertex,minCost);
int key = hash(vertex,input);
if(list[key] == null){
list[key] = temp;
}
else{
Node curr = list[key];
while(curr.next != null){
curr = curr.next;
}
curr.next = temp;
}
totalSize++;
}
public int get(Set<Integer> set, int vertex){
int key = hash(vertex,set);
Node curr = list[key];
if(!set.containsAll(curr.data) || curr.vertex != vertex){
while(curr.next != null){
if(vertex == curr.vertex){
if(set.containsAll(curr.data)){
return curr.minCost;
}
}
curr = curr.next;
}
}
return curr.minCost;
}
public void print(){
for(int i = 0; i < list.length;i++){
Node curr = list[i];
System.out.println("List[" + i + "]");
while(curr != null){
System.out.print("\tVertex: " + curr.vertex + " Data:");
if(curr.data.isEmpty()){
System.out.print("[0]");
}
else{
System.out.print(curr.data);
}
System.out.println("\t\tMin cost: " + curr.minCost);
curr = curr.next;
}
}
}
}
125 changes: 125 additions & 0 deletions Held_Karp_Algorithm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import java.util.HashSet;
import java.util.Set;

public class Held_Karp_Algorithm {
private int[][] D;
private int numVertices = -1;
private HashTable minCostDP;
private HashTable parent;
private LinkedList sets;
public long run(int[][] data, int verticies) {
D = data;
numVertices = verticies;
sets = new LinkedList();
createSets();
int size = numVertices*numVertices;
minCostDP = new HashTable(size);
parent = new HashTable(size);

long startTime = System.currentTimeMillis();
int result = TSP();
long time = System.currentTimeMillis()-startTime;

printTour();
System.out.println(", Distance: " + result);
return time;
}
private int TSP(){
Node curr = sets.top;
while(curr != null){
for(int i = 1; i < numVertices; i++) {
if(curr.set.contains(i)) {
continue;
}
int[] temp = loop(curr.set, Integer.MAX_VALUE, 0, i);
if(curr.set.isEmpty()) {
temp[0] = D[0][i];
}
minCostDP.insert(curr.set, i, temp[0]);
parent.insert(curr.set, i, temp[1]);
}
curr = curr.next;
}
Set<Integer> set = new HashSet<>();
for(int i = 1; i < numVertices; i++) {
set.add(i);
}
int[] temp = loop(set, Integer.MAX_VALUE, -1, 0);
parent.insert(set, 0, temp[1]);
return temp[0];
}
private int[] loop(Set<Integer> set, int min, int minPos, int val){
int[] results = new int[2];
results[0] = min;
results[1] = minPos;
Set<Integer> setCopy = deepCopy(set); //you get an error if you use the normal set
for(int k : set) {
int cost = D[k][val] + Cost(setCopy, k);
if(cost < results[0]) {
results[0] = cost;
results[1] = k;
}
}
return results;
}
private int Cost(Set<Integer> set, int j) {
set.remove(j);
int cost = minCostDP.get(set, j);
set.add(j);
return cost;
}
private Set<Integer> deepCopy(Set<Integer> set){
Set<Integer> result = new HashSet<>();
for(int i: set){
result.add(i);
}
return result;
}
private void printTour() {
String order = "";
Set<Integer> set = new HashSet<>();
for(int i = 0; i < numVertices; i++) {
set.add(i);
}
int start = 0;
while(!set.isEmpty()) {
order = (start + 1) + " -> " + order;// fix for 0 indexing
set.remove(start);
start = parent.get(set,start);
}
order = (0 + 1) + " -> " + order;// we start from vertex 0
System.out.print("Shortest path: " + order.substring(0,order.length()-4));
// -4 to remove the last " -> "
}
private void printSets(){
Node top = sets.top;
while(top != null){
if(top.set.isEmpty()){
System.out.println("[0]");
}
else{
System.out.println(top.set);
}
top = top.next;
}
}
private void createSets() {
int result[] = new int[numVertices-1];
generateCombination(0, 0, result);
}
private void generateCombination(int start, int end, int result[]) {
if(end != result.length) {
Set<Integer> set = new HashSet<>();
if(end > 0){
for(int i = 0; i < end; i++) {
set.add(result[i]);
}
}
sets.addOrdered(set);
for(int i = start; i < result.length; i++) {
result[end] = i+1;
generateCombination(i+1, end+1, result);
}
}
}
}
Loading

0 comments on commit 225d68a

Please sign in to comment.