how to create an adjacency matrix from a G(n,p) graph using python?

823 views Asked by At

I am asking to create an adjacency matrix from a random graph. I really have no idea how to do it, the only way I knew was to build adjacency matrix through graph and vertex.

def adjacency_matrix(n,p) The n is number of vertex and the p is the probability of there is an edge between 2 vertex.

1

There are 1 answers

0
Gambit1614 On

You can take a look at NetworkX package. You can use the function gnp_random_graph for your purpose. It has the following parameters:

n (int) – The number of nodes.
p (float) – Probability for edge creation.
seed (integer, random_state, or None (default)) – Indicator of random number generation state. See Randomness.
directed (bool, optional (default=False)) – If True, this function returns a directed graph.

import networkx as nx

n = 20
p = 0.3

G = nx.generators.random_graphs.gnp_random_graph(n, p)
nx.draw(G)

enter image description here