0

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.

Shayan Shafiq
  • 1,447
  • 5
  • 18
  • 25

1 Answers1

1

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

Gambit1614
  • 8,547
  • 1
  • 25
  • 51