Пример #1
0
        /// <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);
                }
            }
        }
Пример #2
0
 /// <summary>
 /// Performs the Breadth First Search start from specified vertex.
 /// </summary>
 /// <param name="s">The s.</param>
 public void PerformBFS(Vertex s)
 {
 }
Пример #3
0
        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;
        }
Пример #4
0
        /// <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;
        }
Пример #5
0
 /// <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);
     }
 }
Пример #6
0
 /// <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);
     }
 }