Pass input numbers to Python array

Asked

Viewed 92 times

1

I’m doing an exercise on bipartite graphs, in which the input is given in the following form:

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 on the vertices that have binding, 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 matrix to represent that and use an algorithm, so I need to turn that input into an array. How can I do that?

The above entry generates the adjacency matrix below (the numbers after # 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

To be represented in the matrix:

#            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
  • Hello, are you sure your examples are right? Your adjacency matrices are not even symmetrical, which creates even more confusion. Take a look at this link to see if your matrix examples match with https://pt.wikipedia.org/wiki/Matriz_de_adjac%C3%Aancia

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.