I have this code:
gs = open("graph.txt", "r") gp = gs.readline() gp_splitIndex = gp.find(" ") gp_nodeCount = int(gp[0:gp_splitIndex]) gp_edgeCount = int(gp[gp_splitIndex+1:-1]) matrix = [] # predecare the array for i in range(0, gp_nodeCount): matrix.append([]) for y in range(0, gp_nodeCount): matrix[i].append(0) for i in range(0, gp_edgeCount-1): gp = gs.readline() gp_splitIndex = gp.find(" ") # get the index of space, dividing the 2 numbers on a row gp_from = int(gp[0:gp_splitIndex]) gp_to = int(gp[gp_splitIndex+1:-1]) matrix[gp_from][gp_to] = 1 print matrix
The graph.txt file contains the following:
5 10 0 1 1 2 2 3 3 4 4 0 0 3 3 1 1 4 4 2 2 0
The first two numbers tell me that GRAPH has 5 nodes and 10 edges. The following pairs of pairs show edges between nodes. For example, β1 4β means the line between node 1 and 4.
The problem is that the output should be as follows:
[[0, 1, 0, 1, 0], [0, 0, 1, 0, 1], [1, 0, 0, 1, 0], [0, 1, 0, 0, 1], [1, 0, 1, 0, 0]]
But instead, I get the following:
[[0, 1, 0, 1, 0], [0, 0, 1, 0, 1], [0, 0, 0, 1, 0], [0, 1, 0, 0, 1], [1, 0, 1, 0, 0]]
Only one number is different, and I do not understand why this is happening. Edge "3 1" is missing. Can someone explain where the problem is?