放送大学でお勉強してきたことシリーズ。
プログラム編。
Pythonのグラフ理論パッケージnetworkXを、
無料で使える環境Google CoLab
で動かす方法
今回はその1
日本語でグラフを表示
隣接リストから隣接行列取得
なんだけど、時間がないので、今とりあえず図だけ貼っておく
後日、言葉を入れて書き直す予定
(とりあえず図を入れておかないと忘れそうなので・・)




ソース
!apt-get install graphviz libgraphviz-dev pkg-config >/dev/null
!pip install pygraphviz >/dev/null
!pip install japanize-matplotlib >/dev/null
!pip install pygraphviz >/dev/null
!pip install japanize-matplotlib >/dev/null
import networkx as nx
from networkx.drawing.nx_agraph import write_dot
import japanize_matplotlib
import numpy as np
from networkx.drawing.nx_agraph import write_dot
import japanize_matplotlib
import numpy as np
graph = nx.Graph()
graph.add_nodes_from(["ノード1", "ノード2", "ノード3", "ノード4"])
graph.add_edges_from([("ノード1", "ノード2"),
("ノード1", "ノード3"),
("ノード2", "ノード4"),
("ノード3", "ノード4")]
)
nx.draw_networkx(graph,font_family='IPAexGothic', font_weight='bold')
digraph = nx.DiGraph()
digraph.add_nodes_from(["ノード1", "ノード2", "ノード3", "ノード4"])
digraph.add_edges_from([("ノード1", "ノード2"),
("ノード1", "ノード3"),
("ノード2", "ノード4"),
("ノード3", "ノード4")]
)
nx.draw_networkx(digraph,font_family='IPAexGothic', font_weight='bold')
myadm = nx.adjacency_matrix(digraph).todense().astype(int)
print(myadm)
graph.add_nodes_from(["ノード1", "ノード2", "ノード3", "ノード4"])
graph.add_edges_from([("ノード1", "ノード2"),
("ノード1", "ノード3"),
("ノード2", "ノード4"),
("ノード3", "ノード4")]
)
nx.draw_networkx(graph,font_family='IPAexGothic', font_weight='bold')
digraph = nx.DiGraph()
digraph.add_nodes_from(["ノード1", "ノード2", "ノード3", "ノード4"])
digraph.add_edges_from([("ノード1", "ノード2"),
("ノード1", "ノード3"),
("ノード2", "ノード4"),
("ノード3", "ノード4")]
)
nx.draw_networkx(digraph,font_family='IPAexGothic', font_weight='bold')
myadm = nx.adjacency_matrix(digraph).todense().astype(int)
print(myadm)