Skip to content

Instantly share code, notes, and snippets.

@calizarr
Created September 29, 2013 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calizarr/6748782 to your computer and use it in GitHub Desktop.
Save calizarr/6748782 to your computer and use it in GitHub Desktop.
def makeMaps(filename):
f_in = open(filename,'r')
g, grev = {}, {}
print "Loading from: "+str(filename)
for line in f_in:
nodes = [int(x) for x in line.split()]
## print "Total nodes: "+str(nodes)
vertex = nodes[0]
neighbors = nodes[1:]
## print "g[vertex]: "+str(vertex)+" has these neighbors: "+(str(neighbors))
try:
g[vertex]+=neighbors
except KeyError:
g[vertex]=neighbors
for n in neighbors:
try:
grev[n].append(vertex)
except KeyError:
grev[n]=[vertex]
print "Vertices in dictionary: "+str(len(g))
print "Vertices in reverse graph: "+str(len(grev))
f_in.close()
return g,grev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment