Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Graph
- {
- public class stack
- {
- private const int size = 20;
- private int count;
- public int Count
- {
- get
- {
- return count;
- } // end get
- set
- {
- count = value;
- } // end set
- } // end property Count
- private int top;
- private int[] s;
- public stack()
- {
- s = new int[size];
- top = -1;
- Count = 0;
- }
- public int push(int p_item)
- {
- if ((top + 1) == s.Length) return -1;// overflow
- top = top + 1; Count++;
- s[top] = p_item;
- return 1;
- }
- public int pop()
- {
- if (top == -1) return -1;// underflow
- int p_item = s[top];
- top = top - 1; Count--;
- return p_item;
- }
- public int peek()
- {
- if (top == -1) return -1;// underflow
- int p_item = s[top];
- return p_item;
- }
- }
- public class queue
- {
- private const int size = 20;
- private int count;
- public int Count
- {
- get
- {
- return count;
- } // end get
- set
- {
- count = value;
- } // end set
- } // end property Count
- private int front, rear;
- private int[] q;
- public queue()
- {
- q = new int[size];
- front = -1;
- rear = -1;
- }
- public int enqueue(int i_item)
- {
- if ((rear + 1) == q.Length) return -1;// overflow
- if (front == -1) front = 0;
- rear = rear + 1;
- q[rear] = i_item;
- return 1;
- }
- public int dequeue()
- {
- if ((front == -1) || (front > rear)) return -1;// underflow
- int d_item = q[front];
- front = front + 1;
- return d_item;
- }
- }
- public class vertex
- {
- public bool wasvisited;
- public string label;
- public vertex(string label)
- {
- this.label = label;
- wasvisited = false;
- }
- }
- public class graph
- {
- private const int num_vertices = 20;
- private vertex[] vertices;
- private int[,] adjmatrix;
- int numverts;
- ////////////////////////
- public graph()
- {
- vertices = new vertex[num_vertices];
- adjmatrix = new int[num_vertices, num_vertices];
- numverts = 0;
- for (int j = 0; j <= num_vertices - 1; j++)
- for (int k = 0; k <= num_vertices - 1; k++)
- adjmatrix[j, k] = 0;
- }
- //////////////////////////////////
- public void addvertex(string label)
- {
- vertices[numverts] = new vertex(label);
- numverts++;
- for (int j = 0; j < numverts; j++)
- {
- adjmatrix[j, numverts - 1] = 0;
- adjmatrix[numverts - 1, j] = 0;
- }
- }
- //////////////////////////////////////////////
- public void addedge(int start, int end)
- {
- adjmatrix[start, end] = 1;
- adjmatrix[end, start] = 1;
- }
- //////////////////////////////////////////////
- public void deledge(int start, int end)
- {
- adjmatrix[start, end] = 0;
- adjmatrix[end, start] = 0;
- }
- //////////////////////////////////////////////
- public void showvertex(int v)
- {
- Console.Write(vertices[v].label + " ");
- }
- ///////////////////////////////////////////
- private int getadj_unvisited_vertex(int v)
- {
- for (int j = 0; j <= numverts - 1; j++)
- if ((adjmatrix[v, j] == 1) && (vertices[j].wasvisited == false))
- return j;
- return -1;
- }
- /////////////////////////////////////////////
- public void dfs()
- {
- stack gstack = new stack(); // 1
- vertices[0].wasvisited = true; // 2
- showvertex(0); // 3
- gstack.push(0); // 4
- while (gstack.Count > 0) // 5
- {
- int v = getadj_unvisited_vertex(gstack.peek()) ; // 6
- if (v == -1) // 7
- gstack.pop(); // 8
- else
- {
- vertices[v].wasvisited = true; // 9
- showvertex(v); // 10
- gstack.push(v); // 11
- }
- } // 12
- for (int j = 0; j <= numverts - 1; j++) // Hena b2a b3d ma ykon 3adda 3la kol el graph .. byraga3o taany kolo unVisited
- vertices[j].wasvisited = false;
- }// End DFS
- ////////////////////////////////////////////////////////
- public void bfs()
- {
- queue gqueue = new queue(); // 1
- vertices[0].wasvisited = true; //2
- showvertex(0); // 3
- gqueue.enqueue(0); // 4
- while (gqueue.Count > 0) // 5
- {
- int vert1 = gqueue.dequeue(); // 6
- int vert2 = getadj_unvisited_vertex(vert1); // 7
- while (vert2 != -1) // 8
- {
- vertices[vert2].wasvisited = true;
- showvertex(vert2);
- gqueue.enqueue(vert2);
- vert2 = getadj_unvisited_vertex(vert1);
- }
- }
- for (int j = 0; j <= numverts - 1; j++)// Hanraga3 kol el Vertices Unvisited
- vertices[j].wasvisited = false;
- }// end BFS
- /////////////////////////////////////////////////////////
- public void delvertex(int vert)
- {
- if (vert != numverts - 1)
- {
- for (int j = vert; j < numverts - 1; j++)
- vertices[j] = vertices[j + 1];
- for (int row = vert; row < numverts - 1; row++)
- moverow(row, numverts);
- for (int col = vert; col < numverts - 1; col++)
- movecol(col, numverts);
- }
- numverts--;
- }// end deletevertex
- ///////////////////////////////////////////////////
- public void moverow(int row, int length)
- {
- for (int col = 0; col <= length - 1; col++)
- adjmatrix[row, col] = adjmatrix[row + 1, col];
- }// end moverow
- ///////////////////////////////////////////////////
- public void movecol(int col, int length)
- {
- for (int row = 0; row <= length - 1; row++)
- adjmatrix[row, col] = adjmatrix[row, col + 1];
- }// end movecol
- ///////////////////////////////////////////////////
- public void display()
- {
- Console.Write(" ");
- for (int j = 0; j < numverts; j++)
- Console.Write(" " + vertices[j].label);
- Console.WriteLine();
- for (int j = 0; j <= numverts - 1; j++)
- {
- Console.Write(vertices[j].label);
- for (int k = 0; k <= numverts - 1; k++)
- Console.Write(" " + adjmatrix[j, k]);
- Console.WriteLine();
- }
- }// end display
- ////////////////////////////////////////////////////////
- }
- class GraphTest
- {
- static void Main(string[] args)
- {
- graph theGraph = new graph();
- int choice, n, i;
- string label;
- int v1, v2;
- while (true)
- {
- Console.WriteLine();
- Console.WriteLine("Main Menu");
- Console.WriteLine("1.Add Vertex");
- Console.WriteLine("2.Add Edge");
- Console.WriteLine("3.Delete Vertex");
- Console.WriteLine("4.Delete Edge");
- Console.WriteLine("5.Show Graph");
- Console.WriteLine("6.Depth First Search");
- Console.WriteLine("7.Breadth First Search");
- Console.WriteLine("8.Quit");
- Console.Write("Enter your choice: ");
- choice = Convert.ToInt32(Console.ReadLine());
- switch (choice)
- {
- case 1:
- Console.Write("How many vertices you want: ");
- n = Convert.ToInt32(Console.ReadLine());
- for (i = 1; i <= n; i++)
- {
- Console.Write("Enter vertex label : ");
- label = Console.ReadLine();
- theGraph.addvertex(label);
- }
- break;
- case 2:
- Console.Write("How many edges you want: ");
- n = Convert.ToInt32(Console.ReadLine());
- for (i = 1; i <= n; i++)
- {
- Console.Write("Enter start vertex : ");
- v1 = Convert.ToInt32(Console.ReadLine());
- Console.Write("Enter end vertex : ");
- v2 = Convert.ToInt32(Console.ReadLine());
- theGraph.addedge(v1, v2);
- }
- break;
- case 3:
- Console.Write("Enter the vertex to be deleted : ");
- v1 = Convert.ToInt32(Console.ReadLine());
- theGraph.delvertex(v1);
- break;
- case 4:
- Console.Write("Enter start vertex : ");
- v1 = Convert.ToInt32(Console.ReadLine());
- Console.Write("Enter end vertex : ");
- v2 = Convert.ToInt32(Console.ReadLine());
- theGraph.deledge(v1, v2);
- break;
- case 5:
- theGraph.display();
- break;
- case 6:
- theGraph.dfs();
- break;
- case 7:
- theGraph.bfs();
- break;
- case 8:
- return;
- default:
- Console.WriteLine("Wrong choice");
- break;
- }/*End of switch*/
- }/*End of while*/
- }// end Main
- }// End class GraphTest
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement