Пример #1
0
        public void BFS(Vertex v)
        {
            Queue <Vertex> vertexQueue = new Queue <Vertex>();

            vertexQueue.Enqueue(v);
            v.SetLabel("VISITED");

            while (vertexQueue.Count != 0)
            {
                Vertex w = vertexQueue.Dequeue();

                if (w.GetConnections().Count != 0)
                {
                    foreach (Edge e in IncidentEdges(w))
                    {
                        if (e.GetLabel() == "UNEXPLORED")
                        {
                            Vertex u = Opposite(w, e);

                            if (u != null && u.GetLabel() == "UNEXPLORED")
                            {
                                e.SetLabel("DISCOVERY");
                                u.SetLabel("VISITED");
                                vertexQueue.Enqueue(u);
                            }
                            else
                            {
                                e.SetLabel("CROSS");
                            }
                        }
                    }
                }
            }
        }
Пример #2
0
        public void DFS(Vertex v)
        {
            v.SetLabel("VISITED");

            Console.WriteLine("");
            Console.WriteLine(v.GetId());
            Console.WriteLine(v.GetLabel());

            foreach (Edge e in IncidentEdges(v))
            {
                if (e.GetLabel() == "UNEXPLORED")
                {
                    Vertex w = Opposite(v, e);
                    if (w != null && w.GetLabel() == "UNEXPLORED")
                    {
                        e.SetLabel("DISCOVERY");
                        DFS(w);
                    }
                    else
                    {
                        e.SetLabel("BACK");
                    }
                }
            }
        }