NetworkX: graphes basiques
objectif
Expliquer et montrer comment créer un graphe, mesures de base et plus courts chemins.
code minimal
import networkx as nx
G = nx.Graph()
G.add_edges_from([("A","B"),("B","C"),("C","D"),("A","D"),("B","D")])
deg = dict(G.degree())
path = nx.shortest_path(G, "A", "C")
deg, path
utilisation
# centralités
bc = nx.betweenness_centrality(G)
cc = nx.closeness_centrality(G)
(list(bc.items())[:2], list(cc.items())[:2])
variante(s) utile(s)
# graphe orienté + PageRank
DG = nx.DiGraph()
DG.add_edges_from([("A","B"),("B","C"),("C","A")])
pr = nx.pagerank(DG)
pr
notes
- NetworkX est pratique pour petits graphes; pour grands, voir graph-tool/igraph.
- Choisissez Graph vs DiGraph selon le besoin.