S
S
SergWaikat2019-05-26 13:26:40
Python
SergWaikat, 2019-05-26 13:26:40

How to display a list of edges read from a file?

Using OOP.
The theme is graphs.
The in.txt file stores the adjacency matrix.

from dataclasses import dataclass
 
# считывание матрицы из файла
with open('in.txt') as f:
    matrix =[list(map(int, row.split())) for row in f.readlines()]
 

@dataclass
class edgeStruct:
    u: int                  #начальная вершина
    v: int                  #конечная вершина
    weight: int         #вес

The contents of the in.txt file
0	6	20	10	13	9	6	10	2	3
6	0	0	11	7	0	9	16	17	2
20	0	0	20	17	10	2	14	2	4
10	11	20	0	2	18	14	16	15	0
13	7	17	2	0	21	6	0	13	18
9	0	10	18	21	0	4	7	18	9
6	9	2	14	6	4	0	3	15	10
10	16	14	16	0	7	3	0	8	3
2	17	2	15	13	18	15	8	0	0
3	2	4	0	18	9	10	3	0	0

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dmshar, 2019-05-26
@dmshar

Either I'm misunderstanding something, or the question is almost childish.
In your setting, any non-zero matrix element corresponds to an edge, the indexes of this element are the nodes connected by the given edge, and the value is the weight. All!
What is the question? How to iterate over all elements of a matrix?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question