Skip to content

Commit 0d4ffc3

Browse files
authored
Merge pull request igraph#725 from iosonofabio/tutorial-dag
2 parents 89858b2 + be1e85f commit 0d4ffc3

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
3+
.. _tutorials-dag:
4+
5+
======================
6+
Directed Acyclic Graph
7+
======================
8+
9+
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.
10+
"""
11+
import igraph as ig
12+
import matplotlib.pyplot as plt
13+
import random
14+
15+
16+
# %%
17+
# First, we set a random seed for reproducibility
18+
random.seed(0)
19+
20+
# %%
21+
# First, we generate a random undirected graph without loops
22+
g = ig.Graph.Erdos_Renyi(n=15, p=0.3, directed=False, loops=False)
23+
24+
# %%
25+
# Then we convert it to a DAG *in place*
26+
g.to_directed(mode="acyclic")
27+
28+
# %%
29+
# We can print out a summary of the DAG
30+
ig.summary(g)
31+
32+
33+
# %%
34+
# Finally, we can plot the graph using the Sugiyama layout from :meth:`igraph.Graph.layout_sugiyama`:
35+
fig, ax = plt.subplots()
36+
ig.plot(
37+
g,
38+
target=ax,
39+
layout="sugiyama",
40+
vertex_size=15,
41+
vertex_color="grey",
42+
edge_color="#222",
43+
edge_width=1,
44+
)
45+
plt.show()

0 commit comments

Comments
 (0)