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

  • Home
  • SEARCH
  • 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 8003849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:45:39+00:00 2026-06-04T16:45:39+00:00

i am using the edges_iter function in networkx python…i wish to randomly extract edges

  • 0

i am using the edges_iter function in networkx python…i wish to randomly extract edges from a graph and then using these edges, get the nodes at each end… i heard that edge_iter could help me with getting the random edges (i need random edges) but i cant figure out how to get the nodes attached. i’ve saved the graph in gpickle format and load it…then i use
a=G.edges_iter()
print a
and i get this
indicating the occurance of an edge…what i want next is to find out the nodes that are attached at the ends of this edge…help me out with this…thanks a lot guys.

  • 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-04T16:45:40+00:00Added an answer on June 4, 2026 at 4:45 pm

    edges_iter returns a generator object, so I guess you want to iterate through the edges that you get.

    Anyway, the edges already contain information about the nodes they are associated with, since each edge can be seen as a tuple of two nodes. Consider this example:

    import networkx as nx
    
    g = Graph()
    g.add_edges_from([(1, 2), (2, 3), (4,5), (1, 5), (2, 4)])
    # Nodes are automatically added
    
    for edge in g.edges_iter():
        print edge
        print 'Node 1:', edge[0]
        print 'Node 2:', edge[1]
    

    edge[0] will be one node and edge[1] will be the second.

    However, I don’t see how you would get random edges with this approach. Another solution to do that could be to use random.choice and just select edges with that:

    from random import choice
    
    ... # Initialize graph
    
    random_edge = choice(g.edges())
    

    If you instead want to select a number of random edges, you could use random.sample:

    from random import sample
    
    ... # Initialize graph
    
    # Get 2 random edges
    random_edges = sample(g.edges(), 2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Python's Networkx library, I created an undirected graph to represent a relationship network
I am using networkx (a python graph-drawing package) http://networkx.lanl.gov/index.html for one of my project.
I'm trying to display edges going from right to left (i.e. backwards) using dot:
I need to generate a graph using integer arrays. Edges of the graphs are
I am using vertex helper to get the edges of my sprites. I am
I have a large graph (30k vertices, 250m edges) and using boost graph library
I am using Microsoft Glee to draw a graph with many nodes and edges.
I am using igraph (via python) for graph clustering. I have a tree (a
I'm using neato to output a graph, which works nicely. But neato merges edges
Using the Boost Graph Library, is it possible to get the port identifiers for

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.