Quantcast
Viewing all articles
Browse latest Browse all 14126

How to create random graph where each node has at least 1 edge using Networkx

I've managed to create a random undirected weighted graph for testing with Dijkstra's algorithm, but how can I make it so each node has at least one edge that connects them to the graph?

I'm using Networkx and my graph generator is as follows:

import networkx as nximport randomrandom.seed()nodes = random.randint(5,10)seed = random.randint(1,10)probability = random.random()G = nx.gnp_random_graph(nodes,probability,seed, False)for (u, v) in G.edges():    G.edges[u,v]['weight'] = random.randint(0,10)

This creates the graph well, and I managed to plot it, so I can actually see it, my problem is with the probability for edge creation. I don't want it so high that all nodes have the max amount of edges, but putting a low value can result in a node with 0 edges. Is there a way to make sure that each node has at least one edge?


Viewing all articles
Browse latest Browse all 14126

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>