Lab 15: Graphs
In this lab, you will write code to convert graph data into the format used by the Graphviz tool.
Instructions
We encourage you to work with a partner.
Start by downloading graphs.zip, which contains the starter files for this assignment.
Task 1: Install Graphviz on your device
Graphviz is a tool for visualizing graphs.
- First, install Graphviz by following the instructions for your platform
- On macOS, install
graphviz-guivia MacPorts - On Windows, use the latest 64-bit EXE installer. Accept the option to add Graphviz to your PATH.
- On macOS, install
- Next, use Graphviz to render
starter-files.dot. You should see something like the image below.- On macOS, open
starter-files.dotusing the “Graphviz” app installed in/Applications/MacPorts/ - On Windows, use the
dotcommand in the IntelliJ terminal to save a PNG image of the graph using:dot -Tpng starter-files.dot -o starter-files.png
- On macOS, open
- Familiarize yourself with Graphviz’s DOT language by comparing the data in
starter-files.dotto the graph rendered by Graphviz.
Note: Spend at most 15 minutes trying to install Graphviz. If you cannot install Graphviz on either partner’s device, you can use an online Graphviz renderer.
Task 2: Complete GraphDOT.java
The GraphDOT class contains the unfinished getDOT() method. When completed, it should convert a Java Graph object into Graphviz DOT data, suitable for rendering in Graphviz.
GraphDOT’s main method is already completed. The main method reads Graph data in the format described in the documentation for the Graph class from either STDIN or from a file argument, and uses getDOT() to print Graphviz data to STDOUT.
For example, clusters.txt contains Graph data, with 10 vertices and edges:
10
10
1 2
3 4
4 5
5 3
6 7
6 8
6 9
7 8
7 9
8 9
When the GraphDOT class is completed, it should convert clusters.txt into Graphviz data. Notice that each edge is printed once, and vertices with degree zero are printed without associated edges:
> java-algs4 GraphDOT clusters.txt
graph {
"0"
"1" -- "2"
"3" -- "5"
"3" -- "4"
"4" -- "5"
"6" -- "9"
"6" -- "8"
"6" -- "7"
"7" -- "9"
"7" -- "8"
"8" -- "9"
}
Redirect the Graphviz data to a new file, clusters.dot:
> java-algs4 GraphDOT clusters.txt > clusters.dot
Open clusters.dot with Graphviz, which should display:
Note: When running GraphDOT from the command-line, you may get the message:
Exception in thread "main" java.lang.NoClassDefFoundError: StdOut
This happens because the java-algs4 command can’t locate the StdOut class. The java-introcs command can locate StdOut, but won’t be able to locate the Graph class! To resolve the issue, simply use System.out.println() instead of StdOut.println().
Task 3: Generate New Graphs
We have included the GenerateGraph class, which you can use to generate graphs using the single-argument methods of the GraphGenerator class. In particular, all of these methods should work:
For example, to randomly generate a wheel with ten vertices, run:
> java-algs4 GenerateGraph wheel 10 | java-algs4 GraphDOT > wheel.dot
Open wheel.dot in Graphviz, and you should see something similar to:
Although the visualization below depicts an equivalent graph, its layout makes the graph’s structure more interpretable:
Graphviz offers a variety of different layout engines. By default, the dot layout is used, which attempts to draw the graph heirarchically. However, this is not appropriate for all types of graphs. To change the layout engine, add a layout statement within your graph block. For example, to use the neato layout:
graph {
layout="neato"
"0" -- "2"
"0" -- "9"
...
Referring to the list of Graphviz layout engines, match each graph type with the most appropriate layout. In your testing, generate graphs with ten vertices.
| Graph Type | Ideal Layout Engine |
|---|---|
| Binary Tree | |
| Complete | |
| Cycle | |
| Path | |
| Star | |
| Tree | |
| Wheel | neato |
Note: In general, you should choose layouts that minimize the number of intersecting edges.
Note: If you’re using the online Graphviz renderer, you can change the layout engine using the “Engine” menu.
Bonus Tasks
If you finish early, try using the optional attributes of the layout engines to further improve your graph visualizations.
Also, try writing code to generate other types of graphs using the GraphGenerator class. In particular, try the methods that accept more than one parameter.
Submit
Upload the following files to Gradescope:
- GraphDOT.java
- A completed readme.txt file, including your names, etc.
- Also, match the graph types to the ideal layout engines!
Finally, be sure to indicate your group member on Gradescope.
Learning Goals
- Practice using graphs in programs
- Practice visualizing graphs