[PYTHON] In-graph path search using Networkx

This is a personal note:

Creating a graph

It is troublesome to write each time, so create it in the form of an adjacency list

import networkx as nx
g = nx.read_adjlist('/path/to/adjacencylist', create_using=nx.DiGraph())
print('nodes: ' + ', '.join(g.nodes()))

printDisplay all nodes in the graph by adding a statement.

Search for the shortest path

If you add weights etc., you need them separately, but for the sake of simplicity, we will not consider weights here.

nx.shortest_path(g, source="hoge", target="fuga")

The return value will be a list containing nodes

Enumeration of routes visited only once on a node

for path in nx.all_simple_paths(g, source='hoge", target="fuga"):
    print(path)

It also displays the detour route.

Recommended Posts

In-graph path search using Networkx
Search Twitter using Python
Search algorithm using word2vec [python]
oslo.config config file search path
Find the critical path of PERT using breadth-first search and depth-first search
Simple grid search template using Scikit-learn
First OSMnx ~ With shortest path search ~
Search for profitable brands using COTOHA
Automatic roster creation using tabu search
Search / list synonyms using Japanese WordNet
Try using Python's networkx with AtCoder
Depth-first search using stack in Python
Get the file path using Pathlib