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

graph_test.py 中 test_repeated_edges 的逻辑错误 #142

Closed
weilycoder opened this issue Oct 10, 2024 · 1 comment · Fixed by #143
Closed

graph_test.py 中 test_repeated_edges 的逻辑错误 #142

weilycoder opened this issue Oct 10, 2024 · 1 comment · Fixed by #143

Comments

@weilycoder
Copy link
Contributor

weilycoder commented Oct 10, 2024

注意一下 graph_test.py 的第 95 行,代码的原意是检查边集是否有重。

但是,由于 Edge 类没有定义 __hash____eq__,两个不同的对象永远不会被 set 判定为相等。

我在着手修复这项问题#143

def test_repeated_edges(self):
graph_size = 20
for _ in range(20):
graph = Graph.graph(graph_size, int(graph_size*2), repeated_edges=True)
edges = [(e.start, e.end) for e in graph.iterate_edges()]
has_repeated_edges = len(edges) > len(set(edges))
if has_repeated_edges:
break
self.assertTrue(has_repeated_edges)
for _ in range(10):
graph = Graph.graph(graph_size, int(graph_size*2), repeated_edges=False)
edges = list(graph.iterate_edges())
self.assertEqual(len(edges), len(set(edges)))

cyaron/cyaron/graph.py

Lines 9 to 35 in c92f60d

class Edge:
"""Class Edge: A class of the edge in the graph"""
def __init__(self, u, v, w):
"""__init__(self, u, v, w) -> None
Initialize a edge.
int u -> the start vertex
int v -> the end vertex
int w -> the weight.
"""
self.start = u
self.end = v
self.weight = w
def __str__(self):
"""__str__(self) -> str
Return a string to output the edge. The string contains the start vertex, end vertex and weight(u,v,w) and splits with space.
"""
return "%d %d %d" % (self.start, self.end, self.weight)
@staticmethod
def unweighted_edge(edge):
"""unweighted_edge(edge) -> str
Return a string to output the edge without weight. The string contains the start vertex, end vertex(u,v) and splits with space.
"""
return '%d %d' % (edge.start, edge.end)

相关行为参阅:

@weilycoder
Copy link
Contributor Author

再注意一下第 86 行,合理猜测写这段代码的人发现 test 老是 fail,然后把 fail 的地方改了

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 a pull request may close this issue.

2 participants