Advertisement
vencinachev

MatrixGraph

Sep 10th, 2019
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.60 KB | None | 0 0
  1. class GraphMatrix
  2.     {
  3.         private int[,] vertices;
  4.  
  5.         public GraphMatrix(int[,] vertices)
  6.         {
  7.             this.vertices = vertices;
  8.         }
  9.  
  10.         public void AddEdge(int i, int j, int weight)
  11.         {
  12.             vertices[i, j] = weight;
  13.         }
  14.  
  15.         public void AddEdge(int i, int j)
  16.         {
  17.             vertices[i, j] = 1;
  18.         }
  19.  
  20.         public void RemoveEdge(int i, int j)
  21.         {
  22.             vertices[i, j] = 0;
  23.         }
  24.  
  25.         public bool HasEdge(int i, int j)
  26.         {
  27.             return vertices[i, j] != 0;
  28.         }
  29.  
  30.         public IList<int> GetSuccessors(int i)
  31.         {
  32.             IList<int> successors = new List<int>();
  33.  
  34.             for (int j = 0; j < vertices.GetLength(1); j++)
  35.             {
  36.                 if (vertices[i, j] != 0)
  37.                 {
  38.                     successors.Add(j);
  39.                 }
  40.             }
  41.             return successors;
  42.         }
  43.  
  44.  
  45.         public void PrintBFS(int startV)
  46.         {
  47.             if (startV >= this.vertices.GetLength(0))
  48.             {
  49.                 throw new ArgumentException("No such vertice!");
  50.             }
  51.             IList<int> visited = new List<int>();
  52.             Queue<int> queue = new Queue<int>();
  53.             queue.Enqueue(startV);
  54.             visited.Add(startV);
  55.             while (queue.Count != 0)
  56.             {
  57.                 int current = queue.Dequeue();
  58.                 Console.Write(current + " ");
  59.  
  60.                 foreach (int child in this.GetSuccessors(current))
  61.                 {
  62.                     if (!visited.Contains(child))
  63.                     {
  64.                         visited.Add(child);
  65.                         queue.Enqueue(child);
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.  
  71.         public void PrintDFS(int startV)
  72.         {
  73.             if (startV >= this.vertices.GetLength(0))
  74.             {
  75.                 throw new ArgumentException("No such vertice!");
  76.             }
  77.             IList<int> visited = new List<int>();
  78.             Stack<int> stack = new Stack<int>();
  79.             stack.Push(startV);
  80.             visited.Add(startV);
  81.             while (stack.Count != 0)
  82.             {
  83.                 int current = stack.Pop();
  84.                 Console.Write(current + " ");
  85.  
  86.                 foreach (int child in this.GetSuccessors(current))
  87.                 {
  88.                     if (!visited.Contains(child))
  89.                     {
  90.                         visited.Add(child);
  91.                         stack.Push(child);
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement