/// <summary> /// Performs the Depth First Search start from specified vertex. /// </summary> /// <param name="s">The depth first search start vertex.</param> public void PerformDFS(Vertex s) { // White marks vertices that have yet to be discovered. foreach (var u in _graph.Vertices) { u.Mark = VertexMarks.White; } // if there is a starting vertex, start from it: if (s != null) { VisitDFS(s); } // process each vertex foreach (var u in _graph.Vertices) { if (u.Mark == VertexMarks.White) { VisitDFS(u); } } }
/// <summary> /// Performs the Breadth First Search start from specified vertex. /// </summary> /// <param name="s">The s.</param> public void PerformBFS(Vertex s) { }
private void VisitDFS(Vertex u) { if (u == null) throw new ArgumentNullException("u", "Vertex cannot be null!"); // Gray marks a vertex that is discovered // but still has vertices adjacent to it that are undiscovered. u.Mark = VertexMarks.Gray; Vertex v = null; for (var edge = u.FirstOut; edge != null; edge = edge.NextOut) { v = edge.In; if (v.Mark == VertexMarks.White) { VisitDFS(v); edge.Type = EdgeType.TreeEdge; } else if (v.Mark == VertexMarks.Gray) { edge.Type = EdgeType.BackEdge; } else { edge.Type = EdgeType.ForwardEdge; } } // Black marks vertex is discovered vertex that is not adjacent to any white vertices. u.Mark = VertexMarks.Black; }
/// <summary> /// Adds a new <see cref="Edge" /> with the out and in vertices to the graph and returns it. /// </summary> /// <param name="out">The out vertex to create edge.</param> /// <param name="in">The in vertex to create edge.</param> /// <returns> /// The new edge added to the graph. /// </returns> public Edge AddEdge(Vertex @out, Vertex @in) { bool exist = false; foreach (var edge in _edgeList) { if (edge.Out == @out && edge.In == @in) { exist = true; break; } } if (!exist) { // create edge var edge = _factory.NewEdge(@out, @in); _edgeList.Add(edge); return edge; } return null; }
/// <summary> /// Removes the <see cref="Vertex" /> from the graph. /// </summary> /// <param name="v">The vertex.</param> public void RemoveVertex(Vertex v) { if (!_vertexList.Contains(v)) { _vertexList.Remove(v); } }
/// <summary> /// Adds a new <see cref="Vertex" /> to the graph and returns it. /// </summary> /// <param name="v">The vertex.</param> public void AddVertex(Vertex v) { if (!_vertexList.Contains(v)) { _vertexList.Add(v); } }