Advertisement
Hazem3529

Graph DataStrucre

Jan 10th, 2016
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Graph
  6. {
  7.     public class stack
  8.     {
  9.         private const int size = 20;
  10.         private int count;
  11.         public int Count
  12.         {
  13.             get
  14.             {
  15.                 return count;
  16.             } // end get
  17.             set
  18.             {
  19.                 count = value;
  20.             } // end set
  21.         } // end property Count
  22.         private int top;
  23.  
  24.         private int[] s;
  25.  
  26.         public stack()
  27.         {
  28.             s = new int[size];
  29.             top = -1;
  30.             Count = 0;
  31.         }
  32.  
  33.         public int push(int p_item)
  34.         {
  35.             if ((top + 1) == s.Length) return -1;// overflow
  36.             top = top + 1; Count++;
  37.             s[top] = p_item;
  38.             return 1;
  39.         }
  40.         public int pop()
  41.         {
  42.             if (top == -1) return -1;// underflow
  43.             int p_item = s[top];
  44.             top = top - 1; Count--;
  45.             return p_item;
  46.         }
  47.         public int peek()
  48.         {
  49.             if (top == -1) return -1;// underflow
  50.             int p_item = s[top];
  51.             return p_item;
  52.         }
  53.     }
  54.  
  55.  
  56.     public class queue
  57.     {
  58.         private const int size = 20;
  59.         private int count;
  60.  
  61.         public int Count
  62.         {
  63.             get
  64.             {
  65.                 return count;
  66.             } // end get
  67.             set
  68.             {
  69.                 count = value;
  70.             } // end set
  71.         } // end property Count
  72.         private int front, rear;
  73.         private int[] q;
  74.         public queue()
  75.         {
  76.             q = new int[size];
  77.             front = -1;
  78.             rear = -1;
  79.         }
  80.         public int enqueue(int i_item)
  81.         {
  82.             if ((rear + 1) == q.Length) return -1;// overflow
  83.             if (front == -1) front = 0;
  84.             rear = rear + 1;
  85.             q[rear] = i_item;
  86.             return 1;
  87.         }
  88.         public int dequeue()
  89.         {
  90.             if ((front == -1) || (front > rear)) return -1;// underflow
  91.             int d_item = q[front];
  92.             front = front + 1;
  93.             return d_item;
  94.         }
  95.  
  96.     }
  97.  
  98.  
  99.     public class vertex
  100.     {
  101.         public bool wasvisited;
  102.         public string label;
  103.         public vertex(string label)
  104.         {
  105.             this.label = label;
  106.             wasvisited = false;
  107.         }
  108.     }
  109.  
  110.  
  111.     public class graph
  112.     {
  113.         private const int num_vertices = 20;
  114.         private vertex[] vertices;
  115.         private int[,] adjmatrix;
  116.         int numverts;
  117.         ////////////////////////
  118.         public graph()
  119.         {
  120.             vertices = new vertex[num_vertices];
  121.             adjmatrix = new int[num_vertices, num_vertices];
  122.             numverts = 0;
  123.             for (int j = 0; j <= num_vertices - 1; j++)
  124.                 for (int k = 0; k <= num_vertices - 1; k++)
  125.                     adjmatrix[j, k] = 0;
  126.         }
  127.         //////////////////////////////////
  128.         public void addvertex(string label)
  129.         {
  130.             vertices[numverts] = new vertex(label);
  131.             numverts++;
  132.             for (int j = 0; j < numverts; j++)
  133.             {
  134.                 adjmatrix[j, numverts - 1] = 0;
  135.                 adjmatrix[numverts - 1, j] = 0;
  136.             }
  137.         }
  138.         //////////////////////////////////////////////
  139.         public void addedge(int start, int end)
  140.         {
  141.             adjmatrix[start, end] = 1;
  142.             adjmatrix[end, start] = 1;
  143.         }
  144.         //////////////////////////////////////////////
  145.         public void deledge(int start, int end)
  146.         {
  147.             adjmatrix[start, end] = 0;
  148.             adjmatrix[end, start] = 0;
  149.         }
  150.         //////////////////////////////////////////////
  151.         public void showvertex(int v)
  152.         {
  153.             Console.Write(vertices[v].label + " ");
  154.         }
  155.         ///////////////////////////////////////////
  156.         private int getadj_unvisited_vertex(int v)
  157.         {
  158.             for (int j = 0; j <= numverts - 1; j++)
  159.                 if ((adjmatrix[v, j] == 1) && (vertices[j].wasvisited == false))
  160.                     return j;
  161.             return -1;
  162.         }
  163.         /////////////////////////////////////////////
  164.         public void dfs()
  165.         {
  166.             stack gstack = new stack(); // 1
  167.             vertices[0].wasvisited = true; // 2
  168.             showvertex(0); // 3
  169.             gstack.push(0); // 4
  170.            
  171.             while (gstack.Count > 0) // 5
  172.             {
  173.                 int v = getadj_unvisited_vertex(gstack.peek()) ; // 6
  174.                 if (v == -1) // 7
  175.                     gstack.pop(); // 8
  176.                 else
  177.                 {
  178.                     vertices[v].wasvisited = true; // 9
  179.                     showvertex(v); // 10
  180.                     gstack.push(v); // 11
  181.                 }
  182.             } // 12
  183.  
  184.             for (int j = 0; j <= numverts - 1; j++) // Hena b2a b3d ma ykon 3adda 3la kol el graph .. byraga3o taany kolo unVisited
  185.                 vertices[j].wasvisited = false;
  186.  
  187.         }// End DFS
  188.         ////////////////////////////////////////////////////////
  189.         public void bfs()
  190.         {
  191.             queue gqueue = new queue(); // 1
  192.  
  193.             vertices[0].wasvisited = true; //2
  194.  
  195.             showvertex(0); // 3
  196.  
  197.             gqueue.enqueue(0); // 4
  198.  
  199.             while (gqueue.Count > 0) // 5
  200.             {
  201.                 int vert1 = gqueue.dequeue(); // 6
  202.                 int vert2 = getadj_unvisited_vertex(vert1); // 7
  203.  
  204.                 while (vert2 != -1) // 8
  205.                 {
  206.                     vertices[vert2].wasvisited = true;
  207.                     showvertex(vert2);
  208.                     gqueue.enqueue(vert2);
  209.                     vert2 = getadj_unvisited_vertex(vert1);
  210.                 }
  211.             }
  212.  
  213.             for (int j = 0; j <= numverts - 1; j++)// Hanraga3 kol el Vertices Unvisited
  214.                 vertices[j].wasvisited = false;
  215.  
  216.  
  217.         }// end BFS
  218.         /////////////////////////////////////////////////////////
  219.         public void delvertex(int vert)
  220.         {
  221.             if (vert != numverts - 1)
  222.             {
  223.                 for (int j = vert; j < numverts - 1; j++)
  224.                     vertices[j] = vertices[j + 1];
  225.                 for (int row = vert; row < numverts - 1; row++)
  226.                     moverow(row, numverts);
  227.                 for (int col = vert; col < numverts - 1; col++)
  228.                     movecol(col, numverts);
  229.             }
  230.             numverts--;
  231.         }// end deletevertex
  232.         ///////////////////////////////////////////////////
  233.         public void moverow(int row, int length)
  234.         {
  235.  
  236.             for (int col = 0; col <= length - 1; col++)
  237.                 adjmatrix[row, col] = adjmatrix[row + 1, col];
  238.         }// end moverow
  239.         ///////////////////////////////////////////////////
  240.         public void movecol(int col, int length)
  241.         {
  242.  
  243.             for (int row = 0; row <= length - 1; row++)
  244.                 adjmatrix[row, col] = adjmatrix[row, col + 1];
  245.         }// end movecol
  246.         ///////////////////////////////////////////////////
  247.         public void display()
  248.         {
  249.             Console.Write(" ");
  250.             for (int j = 0; j < numverts; j++)
  251.                 Console.Write("  " + vertices[j].label);
  252.             Console.WriteLine();
  253.             for (int j = 0; j <= numverts - 1; j++)
  254.             {
  255.                 Console.Write(vertices[j].label);
  256.                 for (int k = 0; k <= numverts - 1; k++)
  257.                     Console.Write("  " + adjmatrix[j, k]);
  258.                 Console.WriteLine();
  259.             }
  260.         }// end display
  261.         ////////////////////////////////////////////////////////
  262.     }
  263.  
  264.  
  265.     class GraphTest
  266.     {
  267.  
  268.         static void Main(string[] args)
  269.         {
  270.             graph theGraph = new graph();
  271.  
  272.             int choice, n, i;
  273.             string label;
  274.             int v1, v2;
  275.             while (true)
  276.             {
  277.                 Console.WriteLine();
  278.                 Console.WriteLine("Main Menu");
  279.                 Console.WriteLine("1.Add Vertex");
  280.                 Console.WriteLine("2.Add Edge");
  281.                 Console.WriteLine("3.Delete Vertex");
  282.                 Console.WriteLine("4.Delete Edge");
  283.                 Console.WriteLine("5.Show Graph");
  284.                 Console.WriteLine("6.Depth First Search");
  285.                 Console.WriteLine("7.Breadth First Search");
  286.                 Console.WriteLine("8.Quit");
  287.                 Console.Write("Enter your choice: ");
  288.                 choice = Convert.ToInt32(Console.ReadLine());
  289.  
  290.                 switch (choice)
  291.                 {
  292.                     case 1:
  293.                         Console.Write("How many vertices you want: ");
  294.                         n = Convert.ToInt32(Console.ReadLine());
  295.                         for (i = 1; i <= n; i++)
  296.                         {
  297.                             Console.Write("Enter vertex label : ");
  298.                             label = Console.ReadLine();
  299.                             theGraph.addvertex(label);
  300.                         }
  301.                         break;
  302.                     case 2:
  303.                         Console.Write("How many edges you want: ");
  304.                         n = Convert.ToInt32(Console.ReadLine());
  305.                         for (i = 1; i <= n; i++)
  306.                         {
  307.                             Console.Write("Enter start vertex : ");
  308.                             v1 = Convert.ToInt32(Console.ReadLine());
  309.                             Console.Write("Enter end vertex : ");
  310.                             v2 = Convert.ToInt32(Console.ReadLine());
  311.                             theGraph.addedge(v1, v2);
  312.                         }
  313.                         break;
  314.                     case 3:
  315.                         Console.Write("Enter the vertex to be deleted : ");
  316.                         v1 = Convert.ToInt32(Console.ReadLine());
  317.                         theGraph.delvertex(v1);
  318.                         break;
  319.  
  320.                     case 4:
  321.                         Console.Write("Enter start vertex : ");
  322.                         v1 = Convert.ToInt32(Console.ReadLine());
  323.                         Console.Write("Enter end vertex : ");
  324.                         v2 = Convert.ToInt32(Console.ReadLine());
  325.                         theGraph.deledge(v1, v2);
  326.                         break;
  327.                     case 5:
  328.                         theGraph.display();
  329.                         break;
  330.                     case 6:
  331.                         theGraph.dfs();
  332.                         break;
  333.                     case 7:
  334.                         theGraph.bfs();
  335.                         break;
  336.                     case 8:
  337.                         return;
  338.                     default:
  339.                         Console.WriteLine("Wrong choice");
  340.                         break;
  341.                 }/*End of switch*/
  342.             }/*End of while*/
  343.  
  344.         }// end Main
  345.  
  346.     }// End class GraphTest
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement