public void Compute() { Clusters = new List <HashSet <TVertex> >(); var currentCluster = new HashSet <TVertex>(GetTopologicallyFirstVertices(_unprocessedGraph.Vertices)); while (currentCluster.Count > 0) { var nextCluster = new HashSet <TVertex>(); foreach (var vertex in currentCluster) { var neighbours = GetTopologicallyNextVertices(vertex); _unprocessedGraph.RemoveVertex(vertex); nextCluster.Add(GetTopologicallyFirstVertices(neighbours)); } Clusters.Add(currentCluster); currentCluster = nextCluster; } if (!_unprocessedGraph.IsVerticesEmpty) { throw new NonAcyclicGraphException(); } }
protected static IEnumerable <T> GetPath(IMutableBidirectionalGraph <T, IEdge <T> > graph) { var result = new LinkedList <T>(); var candidates = new HashSet <T>(graph.Vertices.Where(graph.IsOutEdgesEmpty), new IdentityComparer <T>()); while (candidates.Count > 0) { var component = candidates.First(); candidates.Remove(component); result.AddFirst(component); var inEdges = graph.InEdges(component).ToList(); graph.RemoveVertex(component); foreach (var inEdge in inEdges) { if (graph.IsOutEdgesEmpty(inEdge.Source)) { candidates.Add(inEdge.Source); } } } return(result); }
/// <summary> /// Hides the vertex <code>v</code>. /// </summary> /// <param name="v">The vertex to hide.</param> public bool HideVertex(TVertex v) { if (_graph.ContainsVertex(v) && !hiddenVertices.Contains(v)) { HideEdges(EdgesFor(v)); //hide the vertex _graph.RemoveVertex(v); hiddenVertices.Add(v); OnVertexHidden(v); return(true); } return(false); }
public static void DeleteVerticesAndMergeEdges <TVertex, TEdge> (this IMutableBidirectionalGraph <TVertex, TEdge> graph, TVertex vertexToDelete, Func <TEdge, TEdge, TEdge> mergeFunc) where TEdge : IEdge <TVertex> { if (graph.IsInEdgesEmpty(vertexToDelete) || graph.IsOutEdgesEmpty(vertexToDelete)) { //simple case graph.RemoveVertex(vertexToDelete); } else { //need to keep connections var newEdges = from e1 in graph.InEdges(vertexToDelete) from e2 in graph.OutEdges(vertexToDelete) select mergeFunc(e1, e2); //create edges var newEdgesList = newEdges.ToList(); graph.AddEdgeRange(newEdgesList); graph.RemoveVertex(vertexToDelete); } }
/// <inheritdoc /> public bool HideVertex(TVertex vertex) { if (!_hiddenVertices.Contains(vertex)) { if (_graph.ContainsVertex(vertex)) { HideEdges(EdgesFor(vertex)); // Hide the vertex _graph.RemoveVertex(vertex); _hiddenVertices.Add(vertex); OnVertexHidden(vertex); return(true); } throw new VertexNotFoundException(); } return(false); }
public static void MergeVertices <TVertex, TEdge>(this IMutableBidirectionalGraph <TVertex, TEdge> graph, IEnumerable <TVertex> verticesToMerge, TVertex replacement, Func <TEdge, TVertex, TEdge> mergeInFunc, Func <TVertex, TEdge, TEdge> mergeOutFunc, IEqualityComparer <TVertex> comparer = null, bool checkExistence = false) where TEdge : IEdge <TVertex> { var verticies = (checkExistence ? graph.Vertices.Intersect(verticesToMerge, comparer) : verticesToMerge).ToList(); var allInEdges = verticies.SelectMany(graph.InEdges); var allOutEdges = verticies.SelectMany(graph.OutEdges); var newInEdges = allInEdges.Select(x => mergeInFunc(x, replacement)); var newOutEdges = allOutEdges.Select(x => mergeOutFunc(replacement, x)); verticies.ForEach(x => graph.RemoveVertex(x)); graph.AddVertex(replacement); graph.AddEdgeRange(newInEdges); graph.AddEdgeRange(newOutEdges); }