Use directional graphs with networkx

NetworkX is a Python package for working with graphs.

I recently learned how to get other nodes related to a particular node in the NetworkX directed graph DiGraph, so I'll write it for later.

The assumed effective graph looks like this. chartfornetwrkx (3).png

Focusing on node C, B is the parent of C and D is the child of C. Also, A and B are ancestors of C, and D and E are descendants of C.

First, let's import NetworkX and create an empty directional graph.

>>> import networkx as nx
>>> g = nx.DiGraph()

Next, create the graph in the above figure with ʻadd_path`. When you create a path, it seems that you can create a node without explicitly adding it.

>>> g.add_path(["A","B","C","D","E"])
>>> g.nodes()
['E', 'C', 'D', 'A', 'B']

Now, to get the parent and child of C, use the methods predecessors and successors of graph g. The result is returned as list, as there can be multiple parents and children each.

>>> g.predecessors("C")
['B']
>>> g.successors("C")
['D']

On the other hand, to get the ancestors and offspring of C, use the functions descendants and ʻansstors. The result is returned with set`.

>>> nx.descendants(g, "C")
{'D', 'E'}
>>> nx.ancestors(g, "C")
{'A', 'B'}

Note that for parents and children, the method returns the result as list, for ancestors and offspring, the function is used, and the result is returned as set.

Recommended Posts

Use directional graphs with networkx
Use mecab-ipadic-neologd with igo-python
Use RTX 3090 with PyTorch
Use ansible with cygwin
Use pipdeptree with virtualenv
[Python] Use JSON with Python
Use Mock with pytest
Use indicator with pd.merge
Use Gentelella with django
Use mecab with Python3
Use tensorboard with Chainer
Use DynamoDB with Python
Use pip with MSYS2
Labeled graphs on NetworkX
Use Python 3.8 with Anaconda
Use pyright with Spacemacs
Use python with docker
Use TypeScript with django-compressor
Use LESS with Django
Use MySQL with Django
Overlay graphs with sympy
Use Enums with SQLAlchemy
Use tensorboard with NNabla
Use GPS with Edison
Use nim with Jupyter
Site notes to help you use NetworkX with Python
Use Trello API with python
Use "$ in" operator with mongo-go-driver
Use custom tags with PyYAML
Use TensorFlow with Intellij IDEA
Draw a graph with NetworkX
Use Twitter API with Python
Use pip with Jupyter Notebook
Use DATE_FORMAT with SQLAlchemy filter
Use TUN / TAP with Python
Use sqlite3 with NAO (Pepper)
Use sqlite load_extensions with Pyramid
Use Windows 10 fonts with WSL
Use chainer with Jetson TK1
Use SSL with Celery + Redis
Use Cython with Jupyter Notebook
Use Maxout + CNN with Pylearn2
Use WDC-433SU2M2 with Manjaro Linux
Use OpenBLAS with numpy, scipy
Use subsonic API with python3
Use Sonicwall NetExtener with Systemd
Draw a graph with networkx
Animate multiple graphs with matplotlib
Use prefetch_related conveniently with Django
Use AWS interpreter with Pycharm
Use Bokeh with IPython Notebook
Use Python-like range with Rust
Use networkx, a library that handles graphs in python (Part 2: Tutorial)
Visualize graphs with Japanese labeled edges in NetworkX and PyGraphviz / Gephi
Use pyright with CentOS7, emacs lsp-mode
Python: How to use async with
Use Azure SQL Database with SQLAlchemy
Use PointGrey camera with Python (PyCapture2)
Use vl53l0x with Raspberry Pi (python)
Publish nice graphs online with plotly
Use PX-S1UD / PX-Q1UD with Jetson nano