Pass numbers from input to array in Python

1

I'm doing an exercise on split graphs, in which input is given as follows:

6
2 4 5
1 3
2 3 5
1 0
2 2 1
1 1

Being 6 (the number of the first line) the number of lines to be read, and the other lines are about the vertices that have connection, in the form:

[Número de linhas/vértices]
[Vértice 1] [Vértice X ligado a V1] [Vértice Y ligado a V1]
[Vértice 2] [Vértice X ligado a V2] [Vértice Y ligado a V2]
[Vértice 3] [Vértice X ligado a V3] [Vértice Y ligado a V3]
[Vértice 4] [Vértice X ligado a V4] [Vértice Y ligado a V4]
.
.
.

I'm going to use adjacency array to represent this and use an algorithm, so I need to turn that input into an array. How can I do this?

The above entry generates the adjacency matrix below (the numbers after the # are just to illustrate):

#           0 1 2 3 4 5
g.graph = [[0,0,0,0,1,1], #0
           [0,0,0,1,0,0], #1
           [0,0,0,1,0,1], #2
           [1,0,0,0,0,0], #3
           [0,1,1,0,0,0], #4
           [0,1,0,0,0,0]] #5

Another example:

10
3 1 3 5
2 6 8
2 8 4
1 2
4 0 2 6 8
2 3 7
1 1
2 6 2
1 1
3 1 2 3

What should be represented in the array:

#            0 1 2 3 4 5 6 7 8 9
g.graph =  [[0,1,0,1,0,1,0,0,0,0], #0
            [0,0,0,0,0,0,1,0,1,0], #1
            [0,0,0,0,1,0,0,0,1,0], #2
            [0,0,1,0,0,0,0,0,0,0], #3
            [1,0,1,0,0,0,1,0,1,0], #4
            [0,0,0,1,0,0,0,1,0,0], #5
            [0,1,0,0,0,0,0,0,0,0], #6
            [0,0,1,0,0,0,1,0,0,0], #7
            [0,1,0,0,0,0,0,0,0,0], #8
            [0,1,1,1,0,0,0,0,0,0]] #9
    
asked by anonymous 28.11.2018 / 22:04

0 answers