[英]python igraph cannot add edges
我想通過igraph包在Python中構建圖形,我可以添加頂點,但是當添加邊線時,存在一個我不知道如何解決的問題。 我在上面打印頂點和邊緣。
g = ig.Graph()
g.add_vertices(dfset)
g.add_edges(edges)
InternalError:在type_indexededgelist.c處發生錯誤:c:272:無法添加邊,無效的頂點ID
print(dfset)= [1437218112, 1710990122, 1346433521, 1750346227, 1487721718, 1528123965]
print(edges) = [(1346433521, 1437218112),
(1528123965, 1710990122),
(1528123965, 1750346227),
(1710990122, 1750346227),
(1750346227, 1346433521),
(1750346227, 1437218112),
(1750346227, 1487721718)]
InternalError Traceback (most recent call last)
<ipython-input-75-82114955e940> in <module>()
----> 1 g.add_edges(edges)
/software/python/2.7.10/b1/lib/python2.7/site-packages/igraph/__init__.pyc in add_edges(self, es)
253 endpoints. Vertices are enumerated from zero.
254 """
--> 255 return GraphBase.add_edges(self, es)
256
257 def add_vertex(self, name=None, **kwds):
InternalError: Error at type_indexededgelist.c:272: cannot add edges, Invalid vertex id
堆棧跟蹤中的其中一行顯示為:
Vertices are enumerated from zero
因此,請確保您使用任意整數添加邊的方法是可行的(例如在networkx中,它可以使用任何可散列的內容)。
在我看來,您需要自己進行映射(例如,簡單的dict;或通過在文檔中提到的庫中使用屬性支持),以便使用range(0,n_vertices)
值調用add_edges()
函數僅range(0,n_vertices)
!
來自文檔的小摘錄:
igraph在其核心中使用頂點和邊緣ID。 這些ID是從零開始的整數,並且在圖的生存期內,它們在任何給定的時間實例處始終是連續的。 這意味着每當刪除頂點和邊時,都會對大量的邊和可能的頂點ID進行重新編號,以確保連續性。
sascha的答案涵蓋了大部分內容。 一些可能有用的附加信息:
從文檔中:
add_vertices(n)
source code
Adds some vertices to the graph.
Parameters:
n - the number of vertices to be added, or the name of a single vertex to be added, or an iterable of strings, each corresponding to the name of a vertex to be added. Names will be assigned to the name vertex attribute.
Overrides: GraphBase.add_vertices
igraph將頂點和邊存儲為直線陣列。 頂點和邊緣ID是它們在該數組中的偏移量; 這就是完成所有索引編制的方式。
與networkx不同,當您添加ID列表時,它們會轉換為字符串,並放入多個新頂點的name
屬性,這些頂點的屬性應為0到5。邊必須使用實際的頂點ID,而不是用戶分配的屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.