Esempio n. 1
0
 private void DepthFirstSearch(Graph graph, int vertex)
 {
     marked[vertex] = true;
     foreach (var w in graph.Adjacency(vertex))
     {
         if (!marked[w])
         {
             edgeTo[w] = vertex;
             DepthFirstSearch(graph, w);
         }
     }
 }
Esempio n. 2
0
        private void BreadthFirstSearch(Graph graph, int startVertex)
        {
            Queue <int> queue = new Queue <int>();

            marked[startVertex] = true;
            queue.Enqueue(startVertex);

            while (queue.Count != 0)
            {
                int v = queue.Dequeue();

                foreach (var w in graph.Adjacency(v))
                {
                    if (!marked[w])
                    {
                        edgeTo[w] = v;
                        marked[w] = true;
                        queue.Enqueue(w);
                    }
                }
            }
        }
        private void BreadthFirstSearch(Graph graph, int vertex)
        {
            var queue = new Queue <int>();

            marked[vertex] = true;
            queue.Enqueue(vertex);

            while (queue.Count != 0)
            {
                int tailVertex = queue.Dequeue();

                foreach (var headVertex in graph.Adjacency(tailVertex))
                {
                    if (marked[headVertex])
                    {
                        continue;
                    }

                    edgeTo[headVertex] = tailVertex;
                    marked[headVertex] = true;
                    queue.Enqueue(headVertex);
                }
            }
        }