Connected Component in Undirected Graph

该博客探讨了如何在无向图中找到连接组件。每个节点包含标签和邻居列表。连通组件是指图中任意两个节点间存在路径,并且不与其他超图节点相连的子图。解决方案需要返回按标签升序排列的组件标签集合。
部署运行你感兴趣的模型镜像

Connected Component in Undirected Graph

Description

Find connected component in undirected graph.

Each node in the graph contains a label and a list of its neighbors.

(A connected component of an undirected graph is a subgraph in which any two vertices are connected to each other by paths, and which is connected to no additional vertices in the supergraph.)

You need return a list of label set.

Nodes in a connected component should sort by label in ascending order. Different connected components can be in any order.

Similar problem

leetcode 323 number-of-connected-components-in-an-undirected-graph

Code

def connectedSet(self, nodes: List[UndirectedGraphNode]) -> List[List[int]]:
	    if not nodes:
		    return []
		
	    queue = collections.deque()
	    visited = set()
	    res = []
	    for node in nodes:
		    if node in visited:
			    continue
		    queue.append(node)
		    visited.add(node)
		    subgraph = []
		    while queue:
			    current_node = queue.popleft()
			    subgraph.append(current_node.label)
			    for neighbor in current_node.neighbors:
				    if neighbor in visited:
					    continue
				    queue.append(neighbor)
				    visited.add(neighbor)
		    subgraph.sort()
		    res.append(subgraph)
	    return res

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

A-3 Linear Component 分数 25 作者 陈越 单位 浙江大学 A component of an undirected graph G is a maximum connected sub-graph of G. A linear component is a component that has a nonlinear structure property-- that is, each vertex in that component has exactly one or two previous vertex (except the first one), and one next vertex (except the last one). The size of a linear component is the number of edges plus vertices in the component. Now given an undirected graph, you are supposed to use assembly language find the largest size of its linear components. Input Specification: Each input file contains one test case, which first gives 2 positive integers: n (≤10 4 ), the number of vertices, and m (≤5n), the number of edges. Then m lines follow, each describes an edge in the form u v, where u and v are the indices of the two ends of the edge. The vertices are indexed from 1 to n. It is guaranteed that no duplicated edges are given. Output Specification: For each case, print in a line the number of vertices and the largest size of the linear components. Use variable dhjskycvi0 to store data. The numbers must be separated by a space. If there is no linear component, the maximum size is defined to be −1. Sample Input 1: 14 11 1 2 2 3 3 1 4 5 6 5 6 7 8 7 9 10 11 12 11 13 11 14 Sample Output 1: 2 4 Sample Input 2: 4 4 1 2 2 3 3 4 4 2 Sample Output 2: 0 -1 代码长度限制 16 KB Java (javac) 时间限制 1100 ms 内存限制 512 MB Python (python3) 时间限制 500 ms 内存限制 256 MB 其他编译器 时间限制 400 ms 内存限制 64 MB 栈限制 8192 KB c++题解
最新发布
07-23
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值