def buildGraph(fileref): # Read and parse the first line of input. # This contains the the number of vertices and the number of edges line = fileref.readline() graphSize = line.split() n = int(graphSize[0]) # n is the number of vertices m = int(graphSize[1]) # m is the number of edges # The graph will be stored as a dictionary whose keys are the vertices # This loop processes the lines corresponding to vertex names graph = {} for i in range(0, n): line = fileref.readline() vertex = line.split() # The neighbors of each vertex are also stored in a dictionary and this is # being initialized below graph[vertex[0]] = {} # The lines in the input file corresponding to edges are processsed for i in range(0, m): edge = fileref.readline() endpoints = edge.split() graph[endpoints[0]][endpoints[1]] = int(endpoints[2]) graph[endpoints[1]][endpoints[0]] = int(endpoints[2]) return graph def getNeighbors(graph, v): return graph[v] def isEdge(graph, u, v): return (u in graph[v].keys()) def getEdgeWeight(graph, u, v): return graph[u][v] def main(): fileref = open(pickAFile(), 'r') graph = buildGraph(fileref) for v in graph.keys(): printNow(v) printNow(graph[v])