Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7915429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:26:59+00:00 2026-06-03T14:26:59+00:00

The following function returns a randomly generated adjacency matrix of size nxn , representing

  • 0

The following function returns a randomly generated adjacency matrix of size nxn, representing a graph.

import random

def random_adjacency_matrix(n):   
    matrix = [[random.randint(0, 1) for i in range(n)] for j in range(n)]

    # No vertex connects to itself
    for i in range(n):
        matrix[i][i] = 0

    # If i is connected to j, j is connected to i
    for i in range(n):
        for j in range(n):
            matrix[j][i] = matrix[i][j]

    return matrix

It is a very naive solution, and I’d like it to satisfy two additional requirements.

  1. The graph the matrix represents must be fully connected, in other words there must not be any nodes that can’t be reached from any other node in some number of steps.

  2. Each node has a number of edges. Right now it’s totally random, and thus fairly uniform how many edges a node has, and when n is large the average number of edges tends to be large too for all graphs. I would like the average number of edges per node to vary more, independent of the size of the graph, so that some graphs have very few connections and others have a lot.

Edit:
Using the networkx package, this seems to be what I want, only with point 1 above also satisfied:

import networkx, random
G = networkx.binomial_graph(50, random.random()) # 50 nodes, random probability of an edge
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:27:01+00:00Added an answer on June 3, 2026 at 2:27 pm

    First, let met recommend the networkx graph library. It’s very easy to use and has a nice toolbox of algorithms already included. For instance, you can easily test a graph for connectivity or ask for the set of connected components in the graph.

    Second, there are a variety of named statstical distributions that are canon. If you could find the one you’re thinking of, that would be helpful to both you and anyone that’s offering to help. From the rough description that you’ve given, it sounds like you might be looking for a power-law distribution (aka “long tail” distribution, see also “scale free network“). You see these kinds of distributions a lot in (e.g.) social networks, in which case there are relatively few VERY highly connected nodes (i.e. the popular ones), and then many, many nodes with very low connectivity. Between the few highly connected, and the many sparsely connected, there is a sort of exponential decay in the middle. This seems to be THE paper that describes randomly generated social networks: Random Graph Models of Social Networks.

    Given networkx and the appropriate statistical distribution, you should be able to build the graph of your dreams. Good luck with your project, and happy coding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume i have the following function: def select_queryset(value_to_decide_upon): this function returns either any of
I'm having a weird problem with the following function, which returns a string with
In python I have the following function: def is_a_nice_element(element, parameter): #do something return True
The following function returns a type mismatch. I don't understand, as I paid attention
I have the following function CREATE FUNCTION GetIdentity() RETURNS INT AS BEGIN RETURN (IDENT_CURRENT('tblTempPo'))
findParent() function returns the following array. Array ( [0] => Array ( [order_item_id] =>
Level: Beginner In the following code my 'samePoint' function returns False where i am
I have the following function in my script at the minute: def _convert_time(p): Converts
I have the following function written to randomly pick a file from a directory.
I have written the following function in Java. This function returns current timestamp by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.