Ejemplo n.º 1
0
        public void BFS_Search(GraphClass graph, int root, string val)//this is similar to BFS_Traverse method
        {
            //explore root

            if (graph.vertices[root].Value == val)//if the value is equal to the root vertex, return
            {
                MessageBox.Show("Found vertex: " + val);
                return;
            }


            for (int i = 0; i < graph.Size; i++)
            {
                if (graph.matrix[root, i] == 1 && graph.vertices[i].isVisited == false) //if the node is not visited and the value is 1
                {
                    Enque(i);                                                           //put the vertex in the queue. put it in the last
                    graph.vertices[i].isVisited = true;                                 //set that vertex to true
                }
            }

            Qhead = Qhead + 1;  //increment the pointer to be traversed

            if (Qtail <= Qhead) //this is the termination code. When head pointer is already pointing to tail.
                                //meaning all nodes are visited and cannot find the value
            {
                MessageBox.Show("Cannot find vertex: " + val);
                return;
            }

            root = Deque();               //get the first value of the Queue ans set to new root

            BFS_Search(graph, root, val); //traverse again the root
        }
Ejemplo n.º 2
0
        public void BFS_Search_Display(GraphClass graph, string val)
        {
            Queue_ = new int[graph.Size];       //set the size of the Q to the size of the graph
            Enque(0);                           //start from 0 index
            graph.vertices[0].isVisited = true; //set that vertex to visited

            BFS_Search(graph, 0, val);          //traverse the vertex 0
        }
Ejemplo n.º 3
0
 public void BFS_Display(GraphClass graph)
 {
     Queue_ = new int[graph.Size];       //set the size of the Q to the size of the graph
     Enque(0);                           //start from 0 index
     graph.vertices[0].isVisited = true; //set that vertex to visited
     BFS_Traverse(graph, 0);             //traverse the vertex 0
     PrintQueue(graph);                  // print all the values after traversal
 }
Ejemplo n.º 4
0
        public void PrintQueue(GraphClass graph)//traverse the values in the queue
        {
            string str = "";

            foreach (int element in Queue_)
            {
                str = str + graph.vertices[element].Value + ",";
            }
            MessageBox.Show(str, "DFS Traversal");
        }
Ejemplo n.º 5
0
        public void printVertices(GraphClass graph)//method to print vertices of a graph
        {
            string elements = "";

            foreach (Vertex element in graph.vertices)
            {
                elements = elements + element.Value + "  ";
            }
            MessageBox.Show(elements);
        }
Ejemplo n.º 6
0
//-------------------------------------------------------------------------------------
//-------------------------DEPTH FIRST SEARCH----------------------------------------
        public void DFS_Traverse(GraphClass graph, int root, int neighbor) //DEPTH FIRST SEARCH TRAVERSAL
        {
            if (root < graph.Size)                                         //ensure that the vertex is checking is not greater than the size of a grpah
            {
                if (graph.vertices[root].isVisited == true)                //if the vertex is visited, go to next vertex
                {
                    return;
                }

                graph.vertices[root].isVisited = true;            //else, visit the vertex and set the IsVisited to True
                print = print + graph.vertices[root].Value + ","; //put the vertex value in the variable for printing later
                while (graph.matrix[root, neighbor] == 0)         //loop though tha columns (neighbor/adjacent) of root while its 0. If its 0, exit from while loop
                {
                    neighbor++;                                   //increment the column

                    //when all columns are 0, meaning no adjacent vertex, exit the loop
                    if (root >= graph.Size || neighbor >= graph.Size)//if that columns is exceeding the graph size
                    {
                        break;
                    }
                }

                //when the matrix [root, neighbor] value is 1, then set that column to new root

                DFS_Traverse(graph, neighbor, 0);//traverse that new root again


                //If it is the end of the traversal, then backtrack

                for (int x = neighbor + 1; x < graph.Size; x++) //loop the next column
                {
                    if (graph.matrix[root, x] == 1)             //if the next column is 1, then traverse again
                    {
                        DFS_Traverse(graph, x, 0);
                        break;
                    }
                    else//if 0, then loop until 1 is encountered
                    {
                        continue;
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public void BFS_Traverse(GraphClass graph, int root)
        {
            //explore root
            for (int i = 0; i < graph.Size; i++)
            {
                if (graph.matrix[root, i] == 1 && graph.vertices[i].isVisited == false) //if the node is not visited and the value is 1
                {
                    Enque(i);                                                           //put the vertex in the queue. put it in the last
                    graph.vertices[i].isVisited = true;                                 //set that vertex to true
                }
            }

            Qhead = Qhead + 1;  //increment the pointer to be traversed

            if (Qtail <= Qhead) //this is the termination code. When head pointer is already pointing to tail. meaning all nodes are visited
            {
                return;
            }

            root = Deque();            //get the first value of the Queue ans set to new root

            BFS_Traverse(graph, root); //traverse that root
        }
Ejemplo n.º 8
0
        public void DisplayMatrix(GraphClass graph)//display  the matrix
        {
            if (graph == graph1)
            {
                CreateMatrix1();
            }
            if (graph == graph2)
            {
                CreateMatrix2();
            }

            string val = "";

            for (int i = 0; i < graph.Size; i++)     // loop though the matrix row
            {
                for (int j = 0; j < graph.Size; j++) // loop though the matrix column
                {
                    val = val + graph.matrix[i, j].ToString() + "   ";
                }
                val = val + "\n";
            }

            MessageBox.Show(val, "Matrix");
        }
Ejemplo n.º 9
0
 //method on adding edge
 public void addEdge(GraphClass graph, string From, string To)
 {
     //no need for me to loop through the matrix since I have hash table already
     graph.matrix[hashFIndex(graph.vertices, From), hashFIndex(graph.vertices, To)] = 1;
 }
Ejemplo n.º 10
0
 public void DFS_Search_Display(GraphClass graph, string val)
 {
     Stack     = new int[graph.Size]; //create the stack
     Stacktail = 0;                   //set the tail to 0 where can insert the vertex
     DFS_Search(graph, 0, 0, val);    //start from 0 index
 }
Ejemplo n.º 11
0
        public int Stacktail; //pointer for stack

        public void DFS_Search(GraphClass graph, int root, int neighbor, string val)
        {
            if ((root >= graph.Size))// if the root is greater than the size, return
            {
                MessageBox.Show("Cannot find vertex: " + val);
                return;
            }

            if (graph.vertices[root].Value == val)//if the value is equal to the root vertex, return
            {
                MessageBox.Show("Found vertex: " + val);
                return;
            }

            else
            {
                //push on stack
                if (graph.vertices[root].isVisited == false)
                {
                    Push(root);                            //push the root vertex to the stack
                    graph.vertices[root].isVisited = true; //make that vertex visited

                    //get the adjacent value
                    while (graph.matrix[root, neighbor] == 0)
                    {
                        neighbor++;

                        if (root >= graph.Size || neighbor >= graph.Size)//if cannot find
                        {
                            break;
                        }
                    }

                    //when the matrix [root, neighbor] value is 1, then set that column to new root
                    root = neighbor;

                    DFS_Search(graph, root, 0, val);             //traverse that new root again
                }
                else if (graph.vertices[root].isVisited == true) //if visted already, back track
                {
                    neighbor = root + 1;                         //mov to the next adjacent vertex
                    root     = Pop();                            // pop the top value in the stack and make it as a root

                    //back track. Traverse that root
                    while (graph.matrix[root, neighbor] == 0)
                    {
                        neighbor++;

                        if (root >= graph.Size || neighbor >= graph.Size)//if cannot find
                        {
                            break;
                        }
                    }


                    if (neighbor >= graph.Size)                 //no more adjacent vertex
                    {
                        neighbor = root;                        //save the previous adjacent
                        DFS_Search(graph, root, neighbor, val); // traverse again
                    }

                    else//make that adjacent vertex as the new root to traverse
                    {
                        root = neighbor;
                        DFS_Search(graph, root, 0, val);//traverse again
                    }
                }
            }
        }
Ejemplo n.º 12
0
 public void DFS_Display(GraphClass graph) //Display the DFS Traversal of a graph
 {
     DFS_Traverse(graph, 0, 0);            // starts from vertex 0
     MessageBox.Show(print, "DFS Traversal");
 }