diff --git a/guava/Makefile b/guava/Makefile index bb8060d..7924adb 100644 --- a/guava/Makefile +++ b/guava/Makefile @@ -1,6 +1,7 @@ # Set path to guava -HOME=/Volumes/noospace/Users/charles -GUAVA=$(HOME)/codes/guava/jars/guava-21.0.jar +MAYA_HOME=/Users/charles +CRONUS_HOME=/Volumes/noospace/Users/charles +GUAVA=$(MAYA_HOME)/codes/guava/jars/guava-21.0.jar # Set compile target TARGET=TSP.java @@ -23,6 +24,9 @@ run: # If no size, use default java $(CP) $(BIN) +dot: + dot graphviz.dot -Tpng -o graphviz.png + time: # Java times itself, we just have to pass it the size java $(CP) $(BIN) $(SIZE) diff --git a/guava/TSP_Version1.java b/guava/TSP_Version1.java index 85a149f..ff241d8 100644 --- a/guava/TSP_Version1.java +++ b/guava/TSP_Version1.java @@ -306,6 +306,29 @@ class RandomGraph { return frozen_roads; } + + public static void toDot( ImmutableNetwork<Node,Edge> graph) { + String dot = "digraph G{"; + dot += "\tnode [shape = circle];\n"; + int i = 0; + for(Node n : graph.nodes()) { + dot += "\t" + n.id + " [style=filled fillcolor=gray];\n"; + } + int j = 0; + for(Edge e : graph.edges()) { + EndpointPair<Node> pair = graph.incidentNodes(e); + Node left = pair.nodeU(); + Node right = pair.nodeV(); + dot += "\t" + left.id + " -> " + right.id + " [label = \"" + e.value + "\"];\n"; + } + dot += "}\n"; + try{ + PrintStream p = new PrintStream(new File("graphviz.dot")); + p.println(dot); + } catch(Exception e) { + // meh. + } + } } /** Static class to shuffle an array. diff --git a/guava/TSP_Version2.java b/guava/TSP_Version2.java index 6132d11..98043f6 100644 --- a/guava/TSP_Version2.java +++ b/guava/TSP_Version2.java @@ -5,10 +5,13 @@ import java.util.TreeMap; import java.util.List; import java.util.LinkedList; import java.util.Arrays; +import java.io.File; +import java.io.PrintStream; import com.google.common.graph.Network; import com.google.common.graph.NetworkBuilder; import com.google.common.graph.ImmutableNetwork; import com.google.common.graph.MutableNetwork; +import com.google.common.graph.EndpointPair; import java.lang.IllegalArgumentException; //// Or be lazy: @@ -32,12 +35,14 @@ public class TSP { double conn = 1.00; TSP t = new TSP(N,conn); + /* long start = System.nanoTime(); t.solve(); long end = System.nanoTime(); long duration = end - start; System.out.printf("Found solution...?\n"); System.out.printf("Elapsed time %03f s\n ", (duration/1E9) ); + */ } @@ -70,6 +75,7 @@ public class TSP { // Build the graph this.graph = RandomGraph.buildGraph(N, connectivity); + RandomGraph.toDot(this.graph); this.graphSize = N; // Initialize route variable, shared across recursive method instances @@ -313,6 +319,29 @@ class RandomGraph { return frozen_roads; } + + public static void toDot( ImmutableNetwork<Node,Edge> graph) { + String dot = "digraph G{\n"; + dot += "\tratio = fill; node [shape = circle]; edge [dir=none];\n"; + int i = 0; + for(Node n : graph.nodes()) { + dot += "\t" + n.id + " [style=filled fillcolor=gray];\n"; + } + int j = 0; + for(Edge e : graph.edges()) { + EndpointPair<Node> pair = graph.incidentNodes(e); + Node left = pair.nodeU(); + Node right = pair.nodeV(); + dot += "\t" + left.id + " -- " + right.id + " [label = \"" + e.value + "\"];\n"; + } + dot += "}\n"; + try{ + PrintStream p = new PrintStream(new File("graphviz.dot")); + p.println(dot); + } catch(Exception e) { + // meh. + } + } } /** Static class to shuffle an array.