Ai
1 Star 1 Fork 0

AndyZhang/C-Sharp-Algorithms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ConnectedComponents.cs 2.34 KB
一键复制 编辑 原始数据 按行查看 历史
Ivandro Ismael 提交于 2018-05-21 07:50 +08:00 . remove redundant "else" / "else if"
/***
* Connected Components.
*
* Computes the connected components for undirected graphs.
*
* Provides:
* * Compute: Returns a list of lists of nodes. Each inner list represents a connected component of nodes.
*/
using System;
using System.Collections.Generic;
using DataStructures.Graphs;
namespace Algorithms.Graphs
{
public static class ConnectedComponents
{
/// <summary>
/// Private helper. Discovers a connected component and return from a source vertex in a graph.
/// </summary>
private static List<TVertex> _bfsConnectedComponent<TVertex>(IGraph<TVertex> graph, TVertex source, ref HashSet<TVertex> visited) where TVertex : IComparable<TVertex>
{
var component = new List<TVertex>();
var queue = new Queue<TVertex>();
queue.Enqueue(source);
while (queue.Count > 0)
{
var current = queue.Dequeue();
if (!visited.Contains(current))
{
component.Add(current);
visited.Add(current);
foreach (var adjacent in graph.Neighbours(current))
if (!visited.Contains(adjacent))
queue.Enqueue(adjacent);
}
}
return component;
}
/// <summary>
/// Return the the connected components in graph as list of lists of nodes. Each list represents a connected component.
/// </summary>
public static List<List<TVertex>> Compute<TVertex>(IGraph<TVertex> Graph) where TVertex : IComparable<TVertex>
{
var components = new List<List<TVertex>>();
var visited = new HashSet<TVertex>();
// Validate the graph parameter
if (Graph == null)
throw new ArgumentNullException();
if (Graph.IsDirected == true)
throw new NotSupportedException("Directed Graphs are not supported.");
if(Graph.VerticesCount == 0)
return components;
// Get connected components using BFS
foreach(var vertex in Graph.Vertices)
if(!visited.Contains(vertex))
components.Add(_bfsConnectedComponent<TVertex>(Graph, vertex, ref visited));
return components;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/strongandyzhang/C-Sharp-Algorithms.git
git@gitee.com:strongandyzhang/C-Sharp-Algorithms.git
strongandyzhang
C-Sharp-Algorithms
C-Sharp-Algorithms
master

搜索帮助