用ai生成mermaid代码时,经常出现一些不合理的结构,因此写了一个后处理的函数,剔除这些结构:
import re
#!问题函数:
def is_sequential_chain_fault(text):
# 提取所有边:X --> Y
edges = re.findall(r'(\w+)(?:\[.*?\])?\s*-->\s*(\w+)(?:\[.*?\])?', text)
if len(edges) > 30:
return True
# 构建图
graph = {}
out_degree = {}
in_degree = {}
for a, b in edges:
graph.setdefault(a, []).append(b)
out_degree[a] = out_degree.get(a, 0) + 1
in_degree[b] = in_degree.get(b, 0) + 1
# 找到起点:入度为0,出度为1
start = None
for node in graph:
if in_degree.get(node, 0) == 0 and out_degree.get(node, 0) == 1:
start = node
break
if not start:
return False # 不是链
# 遍历链
current = start
chain = [current]
while current in graph and len(graph[current]) == 1:
if len(chain) >= 26:
print("链过长(>=26),筛除")
return True # 筛除
next_node = graph[current][0]
chain.append(next_node)
current = next_node
# 检查是否按字母顺序递增
expected = [chr(ord(chain[0]) + i) for i in range(len(chain))]
if chain == expected:
return True # 是顺序链,应筛除
return False
但是代码引发了内存泄漏,具体在最后chain.append(next_node),当图中出现环路时,
chain.append(next_node)会导致无限循环和内存泄漏。
问题分析
-
环检测缺失:如果图中存在环,
while current in graph and len(graph[current]) == 1会无限循环 -
内存泄漏:
chain.append(next_node)会不断增长直到内存耗尽
解决方案
筛除环:
while current in graph and len(graph[current]) == 1:
next_node = graph[current][0]
# 情况3:检测到环
if next_node in visited:
print(f"检测到环:{next_node}已访问过,筛除", flush=True)
return True # 筛除(有环)
473

被折叠的 条评论
为什么被折叠?



