Class GraphVisualizer

java.lang.Object
com.shyarnis.topsort.utils.GraphVisualizer

public class GraphVisualizer extends Object
Utility class for visualizing directed graphs in GraphViz DOT format and rendering them to PNG.

This class provides static methods to:

  • Convert a graph to DOT format representing the original edges
  • Convert a topologically sorted list of nodes to a chained DOT graph
  • Render DOT files to PNG using the external GraphViz tool
  • Method Details

    • visualizeOriginalGraph

      public static <T> void visualizeOriginalGraph(Graph<T> graph, String dotPath) throws IOException
      Generates a GraphViz DOT representation of the original directed graph.
      Type Parameters:
      T - the type of node in the graph
      Parameters:
      graph - the directed graph to visualize
      dotPath - the output path for the generated DOT file
      Throws:
      IOException - if the DOT file cannot be written
    • visualizeSortedGraph

      public static <T> void visualizeSortedGraph(List<T> sortedOrder, String dotPath) throws IOException
      Generates a GraphViz DOT representation of a topologically sorted list of nodes, displaying them as a simple directed chain.
      Type Parameters:
      T - the type of node
      Parameters:
      sortedOrder - the list of nodes in topological order
      dotPath - the output path for the generated DOT file
      Throws:
      IOException - if the DOT file cannot be written
    • renderDotToPng

      public static void renderDotToPng(String dotPath, String pngPath) throws IOException, InterruptedException
      Renders a GraphViz DOT file to a PNG image using the GraphViz CLI tool.

      Requires the dot command to be installed and available in the system path.

      Parameters:
      dotPath - the path to the input DOT file
      pngPath - the path to the output PNG file
      Throws:
      IOException - if the external process cannot be started
      InterruptedException - if the rendering process is interrupted
      RuntimeException - if GraphViz fails to render the image