Skip to content

Commit

Permalink
Tutorial on DAG (#724)
Browse files Browse the repository at this point in the history
  • Loading branch information
iosonofabio committed Oct 27, 2023
1 parent 89858b2 commit be1e85f
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions doc/examples_sphinx-gallery/generate_dag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
.. _tutorials-dag:
======================
Directed Acyclic Graph
======================
This example demonstrates how to create a random directed acyclic graph (DAG), which is useful in a number of contexts including for Git commit history.
"""
import igraph as ig
import matplotlib.pyplot as plt
import random


# %%
# First, we set a random seed for reproducibility
random.seed(0)

# %%
# First, we generate a random undirected graph without loops
g = ig.Graph.Erdos_Renyi(n=15, p=0.3, directed=False, loops=False)

# %%
# Then we convert it to a DAG *in place*
g.to_directed(mode="acyclic")

# %%
# We can print out a summary of the DAG
ig.summary(g)


# %%
# Finally, we can plot the graph using the Sugiyama layout from :meth:`igraph.Graph.layout_sugiyama`:
fig, ax = plt.subplots()
ig.plot(
g,
target=ax,
layout="sugiyama",
vertex_size=15,
vertex_color="grey",
edge_color="#222",
edge_width=1,
)
plt.show()

0 comments on commit be1e85f

Please sign in to comment.