Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to convert the graph to adjacency matrix. #134

Merged
merged 7 commits into from
Oct 3, 2024

Conversation

weilycoder
Copy link
Contributor

@weilycoder weilycoder commented Oct 1, 2024

增加将图转化为邻接矩阵的函数

EDITED:

重边默认覆盖,允许用户自定义

EDITED2:

新增类 GraphMatrix 用于直接输出。

import random

random.seed(0)


from numpy import array
from cyaron.graph import *

g = Graph.graph(3, 10, weight_limit=10)
print(g.to_str())
print("=====")
print(g.to_matrix())
print("=====")
print(g.to_matrix(merge=lambda val, edge: max(edge.weight, val)))
print("=====")
print(g.to_matrix(merge=lambda val, edge: min(edge.weight, val) if val >= 0 else edge.weight))
print("=====")
mat = g.to_matrix()
print(array(list(mat)))
print(mat(2, 3))

可能的输出:

1 3 5
1 1 10
1 2 2
1 3 6
2 2 1
2 3 8
2 2 8
2 3 4
2 3 10
2 3 2
=====
10 2 6
-1 8 2
-1 -1 -1
=====
10 2 6
-1 8 10
-1 -1 -1
=====
10 2 5
-1 1 2
-1 -1 -1
=====
[[-1 -1 -1 -1]
 [-1 10  2  6]
 [-1 -1  8  2]
 [-1 -1 -1 -1]]
2

@Mr-Python-in-China
Copy link
Collaborator

重边你咋处理的

@weilycoder
Copy link
Contributor Author

weilycoder commented Oct 1, 2024

等我想想

要不传一个 merge 函数

@weilycoder
Copy link
Contributor Author

weilycoder commented Oct 3, 2024

矩阵类没必要放 graph.py 里,要不我先 close,等有时间实现一个通用的矩阵类再说 @Mr-Python-in-China

EDITED:哦哦,我以为 Vector 只实现了一维的来着

Copy link
Collaborator

@Mr-Python-in-China Mr-Python-in-China left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

matrix除了graph有别的地方用吗

@weilycoder
Copy link
Contributor Author

T 先给你放进去,项目都没有 __all__

@weilycoder
Copy link
Contributor Author

还有个问题是如果你用那个类输出的话,0行0列都是空的。

@Mr-Python-in-China Mr-Python-in-China merged commit 476534c into luogu-dev:master Oct 3, 2024
@Mr-Python-in-China Mr-Python-in-China linked an issue Oct 3, 2024 that may be closed by this pull request
@weilycoder weilycoder deleted the dev4 branch October 4, 2024 01:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

增强图的输出形式
2 participants