You could use this solution or its variations to improve memory usage.
Give a try.
The idea is that you associate to each node an integer ordinal tag
and you use that tag to access to the list containing the neighors lists.
import csv
class Graph(object):
def __init__(self,infile):
tags = {}
self.neighbors = []
with open(infile, "r") as source_file:
for row in csv.reader(source_file, delimiter=' '):
node1 = int(row[0])
node2 = int(row[1])
if node1 not in tags:
tags[node1] = len(tags)
self.neighbors.append([])
if node2 not in tags:
tags[node2] = len(tags)
self.neighbors.append([])
self.neighbors[tags[node1]].append(tags[node2])
self.neighbors[tags[node2]].append(tags[node1])
def getNodes(self):
return xrange(len(self.neighbors))
def getNeighs(self,i):
return self.neighbors[i]
g = Graph('infile.txt')
print "MyNodes:\n ",
print [i for i in g.getNodes()]
print 'myNeighs\n ',
for i in g.getNodes():
print g.getNeighs(i),
let me show a possible variant. Notice that in both cases the
dictionary 'tags' can be garbage collected (i.e. deleted) after
__init__ has run because nothing refers to it. You are left
with lists only.
import csv
class Graph2(object):
def __init__(self,infile):
tags = {}
self.inverseTags = []
self.neighbors = []
with open(infile, "r") as source_file:
for row in csv.reader(source_file, delimiter=' '):
node1 = int(row[0])
node2 = int(row[1])
if node1 not in tags:
tags[node1] = len(tags)
self.neighbors.append([])
self.inverseTags.append(node1)
if node2 not in tags:
tags[node2] = len(tags)
self.neighbors.append([])
self.inverseTags.append(node2)
self.neighbors[tags[node1]].append(tags[node2])
self.neighbors[tags[node2]].append(tags[node1])
def getNodes(self):
return xrange(len(self.neighbors))
def getNodesOriginalNames(self):
return (self.inverseTags[i] for i in xrange(len(self.neighbors)))
def getNeighs(self,i):
return self.neighbors[i]
def getNeighsOriginalName(self,i):
return [self.inverseTags[j] for j in self.neighbors[i]]
g = Graph2('infile.txt')
print "MyNodes:\n ",
print [i for i in g.getNodesOriginalNames()]
print 'myNeighs\n ',
for i in g.getNodes():
print g.getNeighsOriginalName(i),
Of course if in your csv file the node's names are already zero-based
ordinals you can save all the translation work that __init__ is doing.