Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.4 kB
7
Indexable
Never
public void BreadthFirstTraverse(T startID, ref List<T> visited)

        {
            LinkedList<T> adj;
            Queue<T> toVisit = new Queue<T>();
            GraphNode<T> current = new GraphNode<T>(startID);

            toVisit.Enqueue(startID);       //puts the first node into ToVisit

            while (toVisit.Count != 0)      //when toVist is not 0 perform the while loop - stoppping condition
            {
                startID = toVisit.Dequeue();
                current = GetNodeByID(startID);
                visited.Add(current.ID);        //once we are at a node it puts it into visited so we dont output it again

                foreach (GraphNode<T> n in nodes)
                {
                    if (!visited.Contains(n.ID) && !toVisit.Contains(n.ID))     
                    {
                        toVisit.Enqueue(n.ID);
                    }

                }

            } 


private void button4_Click(object sender, EventArgs e)
        {
            List<string> visitedBFS = new List<string>();
            mygraph.BreadthFirstTraverse(textBox4.Text, ref visitedBFS); //start the bfs off with the input in text box4

            foreach(string n in visitedBFS)     //diplay the  list of nodes visited
            {
                textBox6.Text = n;
            }
            //foreach to display visited to text box
        }