python无向图可视化

本文通过实例展示了如何使用并查集算法在LeetCode上的图论问题中进行可视化调试,包括一个简单的节点边连接图和一个更复杂的无向图案例。通过NetworkX和Matplotlib库,读者可以直观理解并查集在图中的操作过程。
摘要由CSDN通过智能技术生成

背景

刷到leetcode上一些使用并查集的图的题,难度不小,将其中间过程可视化是一个不错的debug方法

完整代码

示例一

import networkx as nx
import matplotlib.pyplot as plt
 
nodes=[
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    '10',
    '11',
    '12',
    '13',
]

edges=[
    ('1','3'),
    ('2','5'),
    ('3','11'),
    ('4','2'),
    ('5','13'),
    ('6','7'),
    ('7','8'),
    ('8','4'),
    ('9','12'),
    ('10','9'),
    ('11','6'),
    ('13','10')
]

G=nx.Graph()

for node in nodes:
    G.add_node(node)

G.add_edges_from(edges)

# # 计算最短路径
# shortest_way=nx.shortest_path(G,"1","13")
# print(shortest_way)
 
nx.draw(G, with_labels=True,node_color='y')
plt.show()

效果一

效果如下
在这里插入图片描述

示例二

给个更复杂的无向图,只需替换点和edges就可以:

nodes=[
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    '10',
    '11',
    '12',
    '13',
]
edges=[
    ('1','2'),
    ('1','13'),
    ('1','10'),
    ('1','11'),
    ('1','8'),
    ('1','5'),
    ('1','12'),
    ('1','9'),
    ('1','3'),
    ('1','7'),
    ('2','1'),
    ('2','4'),
    ('2','5'),
    ('2','6'),
    ('2','13'),
    ('2','8'),
    ('2','11'),
    ('2','12'),
    ('2','9'),
    ('3','13'),
    ('3','7'),
    ('3','12'),
    ('3','8'),
    ('3','11'),
    ('3','4'),
    ('3','10'),
    ('3','1'),
    ('4','2'),
    ('4','10'),
    ('4','12'),
    ('4','13'),
    ('4','11'),
    ('4','3'),
    ('4','6'),
    ('4','9'),
    ('5','2'),
    ('5','12'),
    ('5','13'),
    ('5','11'),
    ('5','1'),
    ('5','10'),
    ('6','2'),
    ('6','7'),
    ('6','9'),
    ('6','8'),
    ('6','13'),
    ('6','12'),
    ('6','10'),
    ('6','4'),
    ('6','11'),
    ('7','6'),
    ('7','8'),
    ('7','3'),
    ('7','11'),
    ('7','10'),
    ('7','12'),
    ('7','13'),
    ('7','1'),
    ('8','7'),
    ('8','6'),
    ('8','9'),
    ('8','3'),
    ('8','1'),
    ('8','13'),
    ('8','2'),
    ('9','6'),
    ('9','12'),
    ('9','8'),
    ('9','13'),
    ('9','1'),
    ('9','10'),
    ('9','4'),
    ('9','2'),
    ('10','4'),
    ('10','1'),
    ('10','13'),
    ('10','7'),
    ('10','11'),
    ('10','5'),
    ('10','9'),
    ('10','3'),
    ('10','6'),
    ('10','12'),
    ('11','7'),
    ('11','1'),
    ('11','5'),
    ('11','3'),
    ('11','10'),
    ('11','4'),
    ('11','13'),
    ('11','6'),
    ('11','2'),
    ('12','5'),
    ('12','9'),
    ('12','4'),
    ('12','3'),
    ('12','7'),
    ('12','1'),
    ('12','6'),
    ('12','10'),
    ('12','2'),
    ('13','3'),
    ('13','5'),
    ('13','6'),
    ('13','1'),
    ('13','9'),
    ('13','10'),
    ('13','4'),
    ('13','8'),
    ('13','2'),
    ('13','7'),
    ('13','11')
]

效果二

在这里插入图片描述

  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值