What is a Graph? · Graphs

Lesson 15

Nikolai Chukhin · Alexander S. Kulikov

In Python, it is particularly convenient to work with graphs and we will see many code snippets below. One can create a graph simply by passing the list of its edges. One can also draw a graph using various layout options.

from networkx import nx_agraph, DiGraph

graph = DiGraph([(1, 2), (2, 3), (3, 4), (4, 5), (1, 5)])

for layout in ('circo', 'dot'):
    nx_agraph.to_agraph(graph).draw(f'graph_{layout}.png', prog=layout)