lilf4p
lilf4p

Reputation: 21

How to prevent Networkit from changing the nodes id when reading an edgelist

I have a csv file that represent a direct graph, it's an edgelist with non continuous id nodes and i read it in a neworkit graph with the method

reader = nk.graphio.EdgeListReader(',',1,'#',directed=True,continuous=False)

The problem is that networkit change the nodes id, it should be caused by the fact that the nodes of my graph start from an arbitrary number (not 0 or 1) and are not continuous. I wonder if there is a way to prevent this from happening without having to change the numbering of my nodes.

Here a view of my edgelist

#from_address,to_address
39243,1040
39244,39245
39246,30
39247,39248
39249,1040
39250,2611

And here the code to see nodes id are changed

reader = nk.graphio.EdgeListReader(',',1,'#',directed=True,continuous=False)
try:
    g = reader.read(fname)
except: 
    print("File not exist")
    exit()
i = 0
for u, v in g.iterEdges():
    if i > 5:
        print('...')
        break
    print(u, v)
    i += 1

>
0 1
2 3
4 5
6 7
8 1
9 10
...

Upvotes: 2

Views: 371

Answers (1)

angriman
angriman

Reputation: 430

NetworKit node ids are always in the [0, n-1] interval. However, you can still use your original ids by retrieving the map from your ids to NetworKit node ids with reader.getNodeMap(). Here is an example:

import networkit as nk

reader = nk.graphio.EdgeListReader(',',1,'#',directed=True,continuous=False)
g = reader.read('edgelist.txt')
map = reader.getNodeMap()

# Using your example, this is the resulting map:
# {'1040': 1, '2611': 10, '30': 5, '39243': 0, '39244': 2, '39245': 3, '39246': 4, '39247': 6, '39248': 7, '39249': 8, '39250': 9}

Upvotes: 3

Related Questions