简体   繁体   中英

Loading data from a list of lists (representing edges) into an igraph graph in python

I have some relational data in the format of a list of lists that I would like to import into an iGraph.Graph() . The list of lists contains duplicate edges, and ultimately, I would like to add a edge weight for duplicate edges. However, currently, I cannot figure out what I'm doing wrong when just trying to add the edges to the graph, minus the weight factor.

I thought the problem was that I had to import all the vertices into the graph first, then I could add the edges between the vertices, but that doesn't appear to be the case.

What am I doing wrong in loading these edges into the graph?

*How can I modify my edge load process to first look for the edge in the graph, if isn't not found, add the edge, and if it is found, increment the weight of that edge by 1, *

DATA

In [60]:    edges

Out[60]:    [['a', 'b'],
             ['a', 'b'],
             ['a', 'b'],
             ['b', 'a'],
             ['a', 'c'],
             ['c', 'a'],
             ['c', 'd'],
             ['c', 'd'],
             ['d', 'c'],
             ['d', 'c']]

IGRAPH CODE TO LOAD EDGES INTO A GRAPH AND ERROR

In [61]:    # extract vertices for edges list
            vertices = []
            for line in edges:
                nodes.append(line[0])
                nodes.append(line[1])

            # find unique vertices
            uniq_vertices = set(sorted(nodes))

In [62]:    # create an empty graph
            g = igraph.Graph()

In [63]:    # add vertices to the graph
            g.add_vertices(uniq_vertices)

In [64]:    # for each line in the edges list, check to see if it's already in the graph, and if not, add it to the graph.
           for line in edges:
                 if not line in g.get_edgelist():
                     g.add_edges(edges)     

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-64-04a376c78860> in <module>()
      2 for line in edges:
      3     if not line in g.get_edgelist():
----> 4         g.add_edges(edges)

C:\Users\Curtis\Anaconda\lib\site-packages\igraph\__init__.pyc in add_edges(self, es)
    228           endpoints. Vertices are enumerated from zero.
    229         """
--> 230         return GraphBase.add_edges(self, es)
    231 
    232     def add_vertex(self, name=None, **kwds):

ValueError: no such vertex: 'a'

The problem is probably here (at least this part of the code does not make sense to me):

for line in edges:
    if not line in g.get_edgelist():
        g.add_edges(edges)

Here you check whether line is in g.get_edgelist() , and rest assured that it won't be because your edges list contains lists, while g.get_edgelist() returns tuples instead. Then you add all the edges, not just the one that you have checked.

I'm attaching an alternative version of your code that seems to do the job for me. Note that I do not eliminate multiple edges when creating the graph - I add them and then simply ask igraph to collapse them into a single one and add their weights together:

import igraph

edges = [['a', 'b'],
         ['a', 'b'],
         ['a', 'b'],
         ['b', 'a'],
         ['a', 'c'],
         ['c', 'a'],
         ['c', 'd'],
         ['c', 'd'],
         ['d', 'c'],
         ['d', 'c']]

# collect the set of vertex names and then sort them into a list
vertices = set()
for line in edges:
    vertices.update(line)
vertices = sorted(vertices)

# create an empty graph
g = igraph.Graph()

# add vertices to the graph
g.add_vertices(vertices)

# add edges to the graph
g.add_edges(edges)

# set the weight of every edge to 1
g.es["weight"] = 1

# collapse multiple edges and sum their weights
g.simplify(combine_edges={"weight": "sum"})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM