Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why cycle behaves differently in just one iteration?

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 file graph.txt contains this:

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 number are telling me, that GRAPH has 5 nodes and 10 edges. The Following number pairs demonstrate the edges between nodes. For example "1 4" means an edge between node 1 and 4.

Problem is, the output should be this:

[[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 of that, I get this:

[[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 can't understand why is this happening. The edge "3 1" is not present. Can someone explain, where is the problem?

like image 539
Adam Bajger Avatar asked Dec 08 '16 11:12

Adam Bajger


People also ask

What are the two types of iterations?

We will study three forms of iteration: tail-recursion, while loops, and for loops. We will use the task of reversing a list as an example to illustrate how different forms of iteration are related to each other and to recursion. A recursive implementation of reverse is given below.

Which process is the only process that does not involve iteration?

Waterfall is the most common non-iterative process. In the waterfall model, you and your team will define project phases before the project starts. Each phase begins once a previous phase is completed in its entirety.

What is an iteration cycle?

Iteration is a cycle-once you measure the results of the change and decide whether or not to keep it, you go back to beginning to observe what's happening, and the cycle repeats. For best results, clearly define what you're trying to accomplish with each iteration.

Why is the iterative process important?

The iterative process gives you the ability to refine and revise a product quickly, especially if you have an initial version of a product but still need to identify detailed features and functions.


1 Answers

Change for i in range(0, gp_edgeCount-1): to

for i in range(0, gp_edgeCount):

The range() function already does the "-1" operation. range(0,3) "==" [0,1,2]

And it is not the "3 1" edge that is missing, it is the "2 0" edge that is missing, and that is the last edge. The matrices start counting at 0.

like image 101
Matthias Avatar answered Oct 01 '22 19:10

Matthias