public static int ConnectedComponents <TVertex, TEdge>( IUndirectedGraph <TVertex, TEdge> g, TVertex startVertex, IDictionary <TVertex, int> components) where TEdge : IEdge <TVertex> { GraphContracts.AssumeNotNull(g, "g"); GraphContracts.Assume(g.ContainsVertex(startVertex), "g.ContainsVertex(startVertex)"); GraphContracts.AssumeNotNull(components, "components"); var conn = new ConnectedComponentsAlgorithm <TVertex, TEdge>(g, components); conn.Compute(startVertex); return(conn.ComponentCount); }
public static bool PathExistsInGraph(IUndirectedGraph<int, IUndirectedEdge<int>> graph, IEnumerable<IContent> path) { var pathList = path.ToList(); if (!graph.ContainsVertex(pathList[0].Id)) return false; var nextIndex = 1; var node = pathList[0].Id; while (node != pathList.Last().Id) { var nextNode = pathList[nextIndex].Id; if (!graph.AdjacentEdges(node).Any(edge => edge.Target == nextNode || edge.Source == nextNode)) return false; node = nextNode; nextIndex++; } return true; }