Skip to content

Instantly share code, notes, and snippets.

@aahnik
Last active July 31, 2023 16:38
Show Gist options
  • Save aahnik/5ac479b51fe6ab51da0791fcf3a6fde2 to your computer and use it in GitHub Desktop.
Save aahnik/5ac479b51fe6ab51da0791fcf3a6fde2 to your computer and use it in GitHub Desktop.
Visualizing graphs and trees while solving dsa problems

Simple guide to visualize graphs

Install the graphviz tool

code_graphviz_install

Run dot -V to check the version and if it was installed properly or not.

Understanding the dot syntax

This is the dot syntax for creating a simple binary tree like a heap. You can see the output at the end of this article.

code_graphviz_digraph_text

See official docs for more customization https://graphviz.org/doc/info/lang.html

Your custom function to print your tree/graph in a text file

Based on your data structure, add one prints that prints all nodes in the specific format of dot language.

code_graphviz_c_func

code_print_to_file

From you main function you can call this utility function. code_from_main

Automate with makefile

Every time I run make vis, I will get the visualization in my desired format.

code_graph_makefile

They support various output formats including PDF and png which are most commonly used. Read this for more info https://graphviz.org/docs/outputs/.

Final Output

Screenshot from 2023-07-31 21-44-00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment