Libraries

DGL (Deep Graph Library)

References

Networkx

# import
import networkx as nx

# load adjacency matrix
G = nx.from_numpy_matrix(A)

# get adjacency matrix
A = nx.adjacency_matrix(G)
A = nx.adjacency_matrix(G).todense()

# drawing
fig = plt.figure(figsize=(10,8))
ax = plt.subplot(111)
pos = nx.spring_layout(G)
#pos = nx.kamada_kawai_layout(G)
nx.draw(G,
        ax=ax,
        pos=pos,
        node_size=10,
        node_color=colors,
        alpha=0.5,
        with_labels=True)

# or
nx.draw_networkx_nodes(G, ax=ax, pos=pos, node_size=100, node_color=colors, alpha=0.5)
nx.draw_networkx_edges(G, ax=ax, pos=pos, alpha=0.1)

# Laplacian Matrix
lap = nx.linalg.laplacianmatrix.laplacian_matrix(G)
lap = nx.linalg.laplacianmatrix.normalized_laplacian_matrix(G) # N=D^(-1/2)LD^(-1/2)

# connected components
nx.algorithms.components.number_connected_components(G)

# relabel
G = nx.relabel_nodes(G, lambda x: int(x[1:]))

# get weights
for n, nbrs in G.adj.items():
    for nbr, eattr in nbrs.items():
        wt = eattr['weight']
        print('(%d, %d, %.3f)' % (n, nbr, wt))

for (u, v, wt) in G.edges.data('weight'):
    print('(%d, %d, %.3f)' % (u, v, wt))

# shorest path
nx.shorest_paht(G, source, target)
nx.shorest_paht_length(G, source, target)

Projects