Advertisement
myname0

graph_I_16

May 4th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1. public class Graph
  2.     {
  3.         private class Node
  4.         {
  5.             private int[,] array;
  6.             private bool[] nov;
  7.  
  8.             public int this[int i, int j]
  9.             {
  10.                 get { return array[i, j]; }
  11.                 set { array[i, j] = value; }
  12.             }
  13.  
  14.             public int Size
  15.             {
  16.                 get { return array.GetLength(0); }
  17.             }
  18.  
  19.             public void NovSet()
  20.             {
  21.                 for (int i = 0; i < Size; i++)
  22.                     nov[i] = true;
  23.             }
  24.  
  25.             public Node(int[,] tmp)
  26.             {
  27.                 array = tmp;
  28.                 nov = new bool[tmp.GetLength(0)];
  29.             }
  30.  
  31.             public void Dfs(int vertex)
  32.             {
  33.                 Console.Write("{0} ", vertex);
  34.                 nov[vertex] = false;
  35.  
  36.                 for (int i = 0; i < Size; i++)
  37.                     if (array[vertex, i] != 0 && nov[i])
  38.                         Dfs(i);
  39.             }
  40.  
  41.             public void Bfs(int vertex)
  42.             {
  43.                 Queue<int> quque = new Queue <int>();
  44.                 quque.Enqueue(vertex);
  45.                 nov[vertex] = false;
  46.                 while (quque.Count != 0)
  47.                 {
  48.                     vertex = quque.Dequeue();
  49.                     Console.Write("{0} ", vertex);
  50.                     for(int i = 0; i < Size; i++)
  51.                         if (array[vertex, i] != 0 && nov[i])
  52.                         {
  53.                             quque.Enqueue(i);
  54.                             nov[i] = false;
  55.                         }
  56.                 }
  57.             }
  58.  
  59.             public void RemoveEdge(int firstVertex, int secondVertex)
  60.             {
  61.                 array[firstVertex, secondVertex] = 0;
  62.                 array[secondVertex, firstVertex] = 0;
  63.                 for (int i = 0; i < Size; i++)
  64.                 {
  65.                     Console.WriteLine();
  66.                     for(int j = 0; j < Size; j++)
  67.                         Console.Write("{0} ", array[i, j]);
  68.                 }
  69.             }
  70.         }
  71.         private Node graph;
  72.  
  73.         public Graph(string nameOfFile)
  74.         {
  75.             StreamReader fin = new StreamReader(nameOfFile);
  76.             int n = int.Parse(fin.ReadLine());
  77.             int[,] tmp = new int[n, n];
  78.             for (int i = 0; i < n; i++)
  79.             {
  80.                 string[] arr = fin.ReadLine().Split(' ');
  81.                 for (int j = 0; j < n; j++)
  82.                     tmp[i, j] = int.Parse(arr[j]);
  83.             }
  84.             graph = new Node(tmp);
  85.         }
  86.  
  87.         public void Dfs(int vertex)
  88.         {
  89.             graph.NovSet();
  90.             graph.Dfs(vertex);
  91.             Console.WriteLine();
  92.         }
  93.  
  94.         public void Bfs(int vertex)
  95.         {
  96.             graph.NovSet();
  97.             graph.Bfs(vertex);
  98.             Console.WriteLine();
  99.         }
  100.  
  101.         public void RemoveEdge(int firstVertex, int secondVertex)
  102.         {
  103.             graph.RemoveEdge(firstVertex - 1, secondVertex - 1);
  104.         }
  105.  
  106.     }
  107.     class Program
  108.     {
  109.         static void Main(string[] args)
  110.         {
  111.             Graph graph1 = new Graph("input.txt");
  112.             graph1.RemoveEdge(2, 4);
  113.         }
  114.     }
  115.  
  116. //input.txt
  117. 5
  118. 0 10 0 0 20
  119. 10 0 60 40 10
  120. 0 60 0 50 0
  121. 0 40 50 0 30
  122. 20 10 0 30 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement