解决mermaid图中有环导致的内存泄漏问题

用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)会导致无限循环和内存泄漏。

问题分析

  1. 环检测缺失:如果图中存在环,while current in graph and len(graph[current]) == 1 会无限循环

  2. 内存泄漏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  # 筛除(有环)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值