%matplotlib inline
import networkx as nx
G = nx.Graph()
G.add_edges_from([("Gaddafi", "Beth"), ("Gaddafi", "Reagan"),("Gaddafi", "Clinton"),("Gaddafi", "Mitterand"), ("Gorbachev","Beth"), ("Clinton", "Beth")])
nx.draw(G, with_labels = True)
G_bar = nx.complement(G)
print(nx.graph_clique_number(G_bar))
Twitterは,自分の投稿と予め「フォロー」をしたユーザーの投稿が見えるものである.
以下のテキストデータは,前者が後者をフォローしていることを表している.(上から順に,AさんがBさんをフォローしている,AさんがCさんをフォローしている…)
まずは,以下のテキストデータを読み込み,相互フォローをしている人たちのネットワークを構成せよ.
$Hint:$前者,後者両方から枝を伸びているものを抽出する
G=nx.Graph ( )
G.add_edges_from([(1, 2), (1, 5), (2, 3), (2, 6), (3, 4), (3 ,7), (4, 8),
(5, 6), (6, 7), (7, 8), (2, 9), (6, 9), (3, 10), (7, 10)])
print(nx.is_eulerian(G))
for e in nx.eulerian_circuit(G):
print(e, end=" ")
G=nx.grid_2d_graph(2,2)
for e in nx.eulerian_circuit(G):
print(e, end=" ")
G=nx.grid_2d_graph(3,4)
print(nx.maximal_matching(G))
print(nx.max_weight_matching(G))
G=nx.Graph()
G.add_weighted_edges_from([("Arigator","WhiteBear",2),("Arigator","Bull",1),
("Bull","WhiteBear",1),("Bull","Shark",3),
("WhiteBear","Condor",3),("WhiteBear","Shark",5),
("Shark","Condor",4)])
print(nx.minimum_spanning_tree(G).edges())
G=nx.DiGraph()
G.add_edges_from([("s",1),("s",2),(1,2),(1,"t"),(2,1),(2,3),(3,"t")])
print(nx.has_path(G,"s","t"))
path=nx.shortest_path(G)
path["s"]["t"]
print(nx.single_source_shortest_path(G,"s"))
print(nx.predecessor(G,"s"))
print(nx.bellman_ford(G,"s"))
G=nx.DiGraph()
G.add_weighted_edges_from([("s",1,10),("s",2,5),(1,2,2),(1,"t",1),(2,1,3),(2,3,2),(3,"t",6)])
print(nx.dijkstra_path(G,"s","t"))
print(nx.single_source_dijkstra_path(G,"s"))
G=nx.DiGraph()
G.add_weighted_edges_from([("s",1,5),("s",2,8),(1,"t",8),(2,1,2),(2,3,5),(3,"t",6)])
print(nx.maximum_flow(G,"s","t",capacity="weight"))
print(nx.minimum_cut(G,"s","t",capacity="weight"))
G=nx.DiGraph()
G.add_node("s",demand=-10)
G.add_node("t",demand=10)
G.add_edge("s",1,weight=10, capacity=5)
G.add_edge("s",2,weight=5, capacity=8)
G.add_edge(2,1,weight=3,capacity=2)
G.add_edge(1,"t",weight=1,capacity=8)
G.add_edge(2,3,weight=2,capacity=5)
G.add_edge(3,"t",weight=6,capacity=6)
print(nx.network_simplex(G))
例題と同様な形で,10単位の氷を以下のグラフで地点1から地点6に運ぶときにどのように氷を運べば最も安い費用で氷を運ぶことが可能であるか求めよ