How to generate a PNG image from a dot file format with Graphviz
graphviz, java, queue
Solution
When I ran the `.dot` file above through `dot` at the command line, I got:
$ dot -Tpng queue.dot -oqueue.png
Warning: queue.dot:2: syntax error in line 2 near '('
Thus, the parenthesised numbers in the node names are not valid in `dot` syntax. If you remove them, I expect the `.png` file would be created successfully. If you need the parenthesised numbers in your output, I suggest looking up node labels in the GraphViz documentation.
I'd also note that `toString()` does not seem like a particularly clear name for a function that creates a `.png` file so changing the name of the function might be advisable.
Problem
I have a java class that implements a priority queue. Then I have a class test that generates a graph like this: ``` digraph G { Milan (0.0) -> Turin (1.2) Milan (0.0) -> Montreal (7.0) Turin (1.2) -> Paris (5.8) Turin (1.2) -> Tokyo (2.2) } ``` This graph is saved in a file called "queue". Now I wish that this graph was displayed in a PNG image using Graphviz. So the last call of my test files (after you have created and filled the queue with priority) is: ``` queue.toString("queue"); ``` All right. The toString method is the following: ``` public void toString(String fileDot){ try { FileOutputStream file = new FileOutputStream(fileDot); PrintStream Output = new PrintStream(file); Output.print(this.printQueue()); Output.close(); File f = new File(fileDot); String arg1 = f.getAbsolutePath(); String arg2 = arg1 + ".png"; String[] c = {"dot", "-Tpng", arg1, "-o", arg2}; Process p = Runtime.getRuntime().exec(c); int err = p.waitFor(); } catch(IOException e1) { System.out.println(e1); } catch(InterruptedException e2) { System.out.println(e2); } } private String printQueue() throws IOException { String g = new String(""); char c = '"'; g = g.concat("digraph G {\n"); if(isEmpty()) g = g.concat(" " + "Empty priority queue."); else { for(int i = 0; i < lastIndex; i++) { if(heap[2 * i] != null) { g = g.concat("" + heap[i].elem + " (" + heap[i].prior + ") " + " " + " -> " + " " + "" + heap[i * 2].elem + " (" + heap[i * 2].prior + ") \n" ); if(heap[2 * i + 1] != null) g = g.concat("" + heap[i].elem + " (" + heap[i].prior + ") " + " " + " -> " + " " + "" + heap[i * 2 + 1].elem + " (" + heap[i * 2 + 1].prior + ") \n" ); } } //end for } //end else g = g.concat("}"); return g; } ``` Why is not generated image .png? Where am I wrong? Of course I installed Graphviz. Thanks