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 8983713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:54:31+00:00 2026-06-15T20:54:31+00:00

So it is clear with NetworkX that they use an algorithm in n^2 time

  • 0

So it is clear with NetworkX that they use an algorithm in n^2 time to generate a random geometric graph. They say there is a faster algorithm possible with the use of K-D Trees. My question is how would one go about attempting to implement the K-D Tree version of this algorithm? I am not familiar with this data structure, nor would I call myself a python expert. Just trying to figure this out. All help is appreciated, thanks!

    def random_geometric_graph(n, radius, dim=2, pos=None):
        G=nx.Graph()
        G.name="Random Geometric Graph"
        G.add_nodes_from(range(n)) 
        if pos is None:
            # random positions
            for n in G:
                G.node[n]['pos']=[random.random() for i in range(0,dim)]
        else:
            nx.set_node_attributes(G,'pos',pos)
        # connect nodes within "radius" of each other
        # n^2 algorithm, could use a k-d tree implementation
        nodes = G.nodes(data=True)
        while nodes:
            u,du = nodes.pop()
            pu = du['pos']
            for v,dv in nodes:
                pv = dv['pos']
                d = sum(((a-b)**2 for a,b in zip(pu,pv)))
                if d <= radius**2:
                    G.add_edge(u,v)
    return G
  • 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-15T20:54:32+00:00Added an answer on June 15, 2026 at 8:54 pm

    Here is a way that uses the scipy KD-tree implementation mentioned by @tcaswell above.

    import numpy as np
    from scipy import spatial
    import networkx as nx
    import matplotlib.pyplot as plt
    nnodes = 100
    r = 0.15
    positions =  np.random.rand(nnodes,2)
    kdtree = spatial.KDTree(positions)
    pairs = kdtree.query_pairs(r)
    G = nx.Graph()
    G.add_nodes_from(range(nnodes))
    G.add_edges_from(list(pairs))
    pos = dict(zip(range(nnodes),positions))
    nx.draw(G,pos)
    plt.show()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We use a web-based service that runs on Oracle. They are strict about only
Possible Duplicate: list.clear() vs list = new ArrayList<int>(); I have a list: List<Integer> l1
Is there any clear definition of RPC and Web Service? A quick wikipedia search
I'm experiencing a difficult ajax error in IE9. I will say up-front that this
I want to make my understanding about hadoop distributed cache clear. I know that
I have a use case where a servlet (that has reasonably high concurrent use)
I have a networkx graph. I am adding nodes by adding edges G.add_edge(route[i-1],route[i]); Now
The differences between distributed and parallel computing are not that clear to me. I've
Hopefully this will be one of those simple answers that is so simple they
I've got a simple question that I just KNOW won't be possible, but here

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.