Search⌘ K

Render a Graph with Similar Node Labels

Explore how to render graphs with similar node labels in Elixir. Understand challenges in DOT serialization for graphs with duplicate labels and apply custom solutions to accurately visualize complex graph structures like the water molecule.

Water molecule graph

Let’s try another example graph, this time one with two edges. For this, we’ll pick the chemical description for the water molecule—H2O. This is an example of an undirected, labeled graph.

XML
iex> g = Graph.new(type: :undirected) |>
...> Graph.add_vertex(:h1, "H") |>
...> Graph.add_vertex(:h2, "H") |>
...> Graph.add_vertex(:o, "O") |>
...> Graph.add_edge(:o, :h1) |>
...> Graph.add_edge(:o, :h2)
iex> with {:ok, dot} = Graph.to_dot(g) do
...> write_graph(dot, "dot/h2o.dot")
...> end
iex> IO.puts read_graph("dot/h2o.dot").data
iex> list_graphs_dir("dot")
iex> write_image read_graph("dot/h2o.dot")

Explanation

Lines 1–6: We create the graph and add vertices and edges.
Lines 8–10: We store the graph to the graph store.
Lines 12–13: We confirm that the graph has ...