Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Graph
- {
- private class Node
- {
- private int[,] array;
- private bool[] nov;
- public int this[int i, int j]
- {
- get { return array[i, j]; }
- set { array[i, j] = value; }
- }
- public int Size
- {
- get { return array.GetLength(0); }
- }
- public void NovSet()
- {
- for (int i = 0; i < Size; i++)
- nov[i] = true;
- }
- public Node(int[,] tmp)
- {
- array = tmp;
- nov = new bool[tmp.GetLength(0)];
- }
- public void Dfs(int vertex)
- {
- Console.Write("{0} ", vertex);
- nov[vertex] = false;
- for (int i = 0; i < Size; i++)
- if (array[vertex, i] != 0 && nov[i])
- Dfs(i);
- }
- public void Bfs(int vertex)
- {
- Queue<int> quque = new Queue <int>();
- quque.Enqueue(vertex);
- nov[vertex] = false;
- while (quque.Count != 0)
- {
- vertex = quque.Dequeue();
- Console.Write("{0} ", vertex);
- for(int i = 0; i < Size; i++)
- if (array[vertex, i] != 0 && nov[i])
- {
- quque.Enqueue(i);
- nov[i] = false;
- }
- }
- }
- public void RemoveEdge(int firstVertex, int secondVertex)
- {
- array[firstVertex, secondVertex] = 0;
- array[secondVertex, firstVertex] = 0;
- for (int i = 0; i < Size; i++)
- {
- Console.WriteLine();
- for(int j = 0; j < Size; j++)
- Console.Write("{0} ", array[i, j]);
- }
- }
- public void SearchNearestVertex(int vertex)
- {
- int min = int.MaxValue;
- for (int j = 0; j < Size; j++)
- if (array[vertex, j] < min && array[vertex, j] != 0)
- min = array[vertex, j];
- if (min == int.MaxValue) Console.WriteLine("This vertex haven't nearest vertex");
- else
- {
- Console.Write("The nearest vertex of vertex {0} is: ", vertex + 1);
- for (int j = 0; j < Size; j++)
- {
- if (array[vertex, j] == min)
- Console.Write("{0} ", j + 1);
- }
- }
- }
- }
- private Node graph;
- public Graph(string nameOfFile)
- {
- StreamReader fin = new StreamReader(nameOfFile);
- int n = int.Parse(fin.ReadLine());
- int[,] tmp = new int[n, n];
- for (int i = 0; i < n; i++)
- {
- string[] arr = fin.ReadLine().Split(' ');
- for (int j = 0; j < n; j++)
- tmp[i, j] = int.Parse(arr[j]);
- }
- graph = new Node(tmp);
- }
- public void Dfs(int vertex)
- {
- graph.NovSet();
- graph.Dfs(vertex);
- Console.WriteLine();
- }
- public void Bfs(int vertex)
- {
- graph.NovSet();
- graph.Bfs(vertex);
- Console.WriteLine();
- }
- public void RemoveEdge(int firstVertex, int secondVertex)
- {
- graph.RemoveEdge(firstVertex - 1, secondVertex - 1);
- Console.WriteLine();
- }
- public void SearchNearestVertex(int vertex)
- {
- graph.SearchNearestVertex(vertex - 1);
- Console.WriteLine();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Graph graph1 = new Graph("input.txt");
- //graph1.RemoveEdge(2, 4);
- graph1.SearchNearestVertex(2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement