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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:57:13+00:00 2026-06-01T17:57:13+00:00

I was trying to write the code in python using igraph and when i

  • 0

I was trying to write the code in python using igraph and when i tried to add edges using a while loop this error came up

while(i<k)
  g.add_vertices(theInts[i])
  i=i+1
  g.add_edges([(theInts[i-1],theInts[i])])

I thought that indexing might be a problem so i also included a if statement but that doesnt seems to be the problem.

Please Help!!!

  • 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-01T17:57:14+00:00Added an answer on June 1, 2026 at 5:57 pm

    I think this all depends on what g has for vertices. If you start off with an empty g, you only have the vertex 0, so if you’re trying to call add_edges with two different vertices, it’s just not going to work. You have to add some more vertices. Of course this all depends on what your graph looks like before the loop, and what i is.

    You can display some brief information about your graph with print. For example,

    >>> import igraph
    >>> graph = igraph.Graph()
    >>> print graph
    Undirected graph (|V| = 1, |E| = 0)
    

    If i starts at 0, then you will not add any vertices with your loop the first time around. So when you try to add edges, you’re trying to add to vertices that do not exist.

    >>> graph.add_vertices(0)
    <igraph.Graph object at 0xcea850>
    >>> print graph
    Undirected graph (|V| = 1, |E| = 0)
    >>> graph.add_edges([(0, 1)])
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    igraph.core.InternalError: Error at type_indexededgelist.c:245: cannot add edges, Invalid vertex id
    

    If that is not the issue, try printing the edges and see if they match up with what you want.

    >>> graph.add_vertices(5)
    <igraph.Graph object at 0xcea850>
    >>> print graph
    Undirected graph (|V| = 6, |E| = 3)
    >>> graph.add_edges([(1, 1), (2, 3), (3, 5)])
    <igraph.Graph object at 0xcea850>
    >>> graph.get_edgelist()
    [(1, 1), (2, 3), (3, 5)]
    

    Also, it might be a bit more helpful to have the complete TraceBack.

    EDIT: Based on your comment

    So you’re saying you have a structure like this:

    >>> graph = igraph.Graph()
    >>> print graph
    Undirected graph (|V| = 1, |E| = 0)
    

    And you want to add just vertex 2? I am not sure that you can do that with igraph. It seems to have to have each vertex in order. You can check to see if you have the vertex and then add them if necessary, remembering that these graphs are 0-based. Something like this.

    >>> vertices = 1, 2, 13, 4, 21, 5
    >>> map_graph = igraph.Graph()
    >>> print map_graph
    Undirected graph (|V| = 1, |E| = 0)
    >>> map_graph.add_vertices(max(vertices))
    <igraph.Graph object at 0xceaa50>
    >>> print map_graph
    Undirected graph (|V| = 22, |E| = 0)
    >>> map(map_graph.add_edges, zip(vertices, vertices[1:]))
    [<igraph.Graph object at 0xceaa50>, <igraph.Graph object at 0xceaa50>, <igraph.Graph object at 0xceaa50>, <igraph.Graph object at 0xceaa50>, <igraph.Graph object at 0xceaa50>]
    >>> print map_graph
    Undirected graph (|V| = 22, |E| = 5)
    >>> map_graph.get_edgelist()
    [(1, 2), (2, 13), (4, 13), (4, 21), (5, 21)]
    

    Or if you don’t like maps, you can loop it up.

    >>> vertices = 1, 2, 13, 4, 21, 5
    >>> loop_graph = igraph.Graph()
    >>> print loop_graph
    Undirected graph (|V| = 1, |E| = 0)
    >>> loop_graph.add_vertices(max(vertices))
    <igraph.Graph object at 0xcea950>
    >>> print loop_graph
    Undirected graph (|V| = 22, |E| = 0)
    >>> for pair in zip(vertices, vertices[1:]):
    ...     loop_graph.add_edges(pair)
    ... 
    <igraph.Graph object at 0xcea950>
    <igraph.Graph object at 0xcea950>
    <igraph.Graph object at 0xcea950>
    <igraph.Graph object at 0xcea950>
    <igraph.Graph object at 0xcea950>
    >>> print loop_graph
    Undirected graph (|V| = 22, |E| = 5)
    >>> loop_graph.get_edgelist()
    [(1, 2), (2, 13), (4, 13), (4, 21), (5, 21)]
    

    There is probably a better way to do this though. If this isn’t what you’re looking for, please edit your original question with more detail, and some actual code.

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

Sidebar

Related Questions

I am trying to write to a .tsv file using python's CSV module, this
I'm using asynchat and trying to use python3. getting this error: error: uncaptured python
I'm trying to write metadata to a pdf file using the following python code:
I'm trying to access some C code via Python using ctypes, so I wrote
I'm trying to write some Python code that will establish an invisible relay between
I'm trying to write some code using pure SQL using ASP.NET MVC. I assume
I'm trying to write a secure transfer file program using Python and AES and
I am using Python 3.0 to write a program. In this program I deal
I am trying to write an app in python to control a motor using
I'm trying to copy files inside a Python script using the following code: inf,outf

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.