Many algorithms represent graphs or trees. Use Graphviz to check that these structures are what you think they are. Examples in Ruby.
First try Graph a Structure with Graphviz
Things to Try
Create an array variable to hold dot statements generated as the program runs. Add defaults for nodes and edges.
@dot = [] @dot << "node [style=filled fillcolor=lightBlue];"
Add nodes and edges as your program runs. Write a subroutine to traverse your data structures if this helps.
@dot << "#{quote a} -> #{quote b};"
Define a function to add quotes around any string you use for node names.
def quote string "\"#{string}\"" end
Write the dot file at the end of the run.
File.open("try2.dot", "w") do |f| f.puts "digraph { #{@dot.join "\n"} }" end
See Quick Brown Letters for a ruby program that draws a graph of a sentence with letters for nodes and adjacent letters connected by edges.
How it Works
Attributes
A node can be followed with attributes enclosed in square brackets. Set attributes for "node" to change defaults for subsequent nodes.
Character adjacency graph from try2.rb. "the quick brown fox ..."
An edge can be followed with attributes enclosed in square brackets. Set attributes for "edge" to change defaults for subsequent edges.
Find a list of attributes in the Graphviz documentation. There are many. Keep a list of attributes you find useful. docs , wayback
Labels
Enclose node names in quotes to use spaces and punctuation in node names. Use \n to separate a name into multiple lines. You may have to type \\n to produce a file with \n.
Try replacing spaces with \n to make nodes with long names more square. These will layout better in the graph.
Use the label=" ... " attribute to add data to nodes or edges. Use multiple attribute definitions to add more attributes to nodes.
Colors
Use color="/pastel19/#{n}" to choose one of nine nicely related colors. pastel19 is a color space, n is the number of a color within it. docs