Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing graph (vertex, edge) in Python

What is the easiest way to print a graph in Python? i.e. I am wanting to visualize the maximum clique of a graph.

My current data structures are:

adjacency_matrix = [[False, True, False, ...], 
                    [True, False, True, ...],
                    ..]

adjacency_set = [[45, 2], [1, 32], ...]

max_clique = [23, 143, 1, 2, 42, 12, 3, ...] # the vertices in the max clique

Would I use matplotlib to do this?

like image 506
Wizard Avatar asked Mar 08 '26 14:03

Wizard


1 Answers

Consider using networkx package for creating and manipulating graphs. It also has visualization functions such as drawing with matplotlib.

Note that you will need to convert your data structure to one that can initialize networkx graph. here is the docs.

Also you might want to read this answer on how to visualize maximal cliques.

example:

import numpy as np
import networkx as nx
import matplotlib.pyplot as plt

A = np.matrix([[1,1],[2,1]])
G = nx.from_numpy_matrix(A)
nx.draw(G)
plt.show()
like image 100
Nadav Avatar answered Mar 10 '26 02:03

Nadav