|
1 | 1 | from .utils import * |
2 | 2 | import random |
3 | | -from typing import TypeVar, Callable |
| 3 | +from typing import TypeVar, Callable, List |
4 | 4 |
|
5 | 5 |
|
6 | 6 | class Edge: |
@@ -54,16 +54,15 @@ def edge_count(self): |
54 | 54 | return cnt |
55 | 55 |
|
56 | 56 | def to_matrix(self, **kwargs): |
57 | | - """to_matrix(self, **kwargs) -> list[list[Any]] |
| 57 | + """to_matrix(self, **kwargs) -> GraphMatrix |
58 | 58 | Convert the graph to adjacency matrix. |
59 | 59 | **kwargs(Keyword args): |
60 | 60 | int default = -1 -> the default value when the edge does not exist. |
61 | 61 | Any merge(Any, Edge) |
62 | 62 | = lambda val, edge: edge.weight |
63 | 63 | -> the mapping from the old values in matrix and the edges to the new values in matrix. |
64 | | - Note that the index start from 0 and the values in the Column 0 or the Row 0 are always the default. |
65 | 64 | """ |
66 | | - return GraphMatrix(self) |
| 65 | + return GraphMatrix(self, **kwargs) |
67 | 66 |
|
68 | 67 | def to_str(self, **kwargs): |
69 | 68 | """to_str(self, **kwargs) -> str |
@@ -551,4 +550,10 @@ def __init__(self, |
551 | 550 | self.matrix[edge.start][edge.end], edge) |
552 | 551 |
|
553 | 552 | def __str__(self): |
554 | | - return '\n'.join([' '.join(map(str, row)) for row in self.matrix]) |
| 553 | + return '\n'.join([' '.join(map(str, row[1:])) for row in self.matrix[1:]]) |
| 554 | + |
| 555 | + def __call__(self, u: int, v: int): |
| 556 | + return self.matrix[u][v] |
| 557 | + |
| 558 | + def __iter__(self): |
| 559 | + return self.matrix.__iter__() |
0 commit comments