Can't add vertices with vertex data in SGraph

User 423 | 9/22/2014, 8:55:09 PM

I can't add vertices to SGraph. When I "print g.summary()" I get "{'numedges': 0, 'numvertices': 0}".

This is the code:

<pre><code> variables = set([1, 2, 3]) satinstance = [[1, 3, 2], [-1, 2, -3]] graph = gl.SGraph() litnid_map = defaultdict(lambda: [])

nid = 0
for clause_no, clause in enumerate(sat_instance):
    clause_node_vector = []
    vertices = []
    for lit in clause:
        vertex = gl.Vertex(nid, attr={'lit': lit, 'clause': clause_no})
        graph.add_vertices(vertex)
        #vertices.append(vertex)
        clause_node_vector.append(nid)
        lit_nid_map[lit].append(nid)

        nid += 1
    #graph.add_vertices(vertices)
    #Add nodes in a gadget for this single clause
    edges = []
    for index, clause_node1 in enumerate(clause_node_vector):
        for clause_node2 in clause_node_vector[(index+1):]:
            graph.add_edges(gl.Edge(clause_node1, clause_node2))

for var in variables:
    positive_vector = lit_nid_map[var]
    negative_vector = lit_nid_map[-var]
    edges = []
    for positive_lit in positive_vector:
        for negative_lit in negative_vector:
            graph.add_edges(gl.Edge(positive_lit, negative_lit))

print(graph.summary())
pagerank = gl.pagerank.create(graph)
pr_out = pagerank['pagerank'] 
vertices = graph.vertices
ranks = vertices.join(pr_out, on={'__id':'__id'}, how='outer')
print(ranks.topk('pagerank', k=100))

</code></pre>

Comments

User 4 | 9/23/2014, 5:27:22 PM

Hi @riyadparvez‌,

The SGraph structure is immutable. The addedges and addvertices methods actually return a new SGraph.

The typical pattern is to do something like this (if you want to add vertices, but not preserve the original SGraph). If your SGraph is named sg:

<pre><code> sg = sg.add_vertices(vertices) </code></pre>