-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89858b2
commit be1e85f
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |