Advertisement
myname0

Практикум16_IV

May 26th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.31 KB | None | 0 0
  1. public class Graph
  2.     {
  3.         private class Node
  4.         {
  5.             List<List<int>> array = new List<List<int>>();
  6.             int countVertex;
  7.             bool[] nov;
  8.  
  9.             public Node(List<List<int>> tmp, int n)
  10.             {
  11.                 array = tmp;
  12.                 countVertex = n;
  13.                 nov = new bool[countVertex];
  14.             }
  15.  
  16.             public int Size
  17.             {
  18.                 get { return countVertex; }
  19.             }
  20.  
  21.             public void NovSet()
  22.             {
  23.                 for (int i = 0; i < countVertex; i++)
  24.                     nov[i] = false;
  25.             }
  26.  
  27.             public long[,] Floyd(out int[,] p)
  28.             {
  29.                 int i, j, k;
  30.                 long[,] a = new long[countVertex, countVertex];
  31.                 p = new int[countVertex, countVertex];
  32.  
  33.                 for (i = 0; i < countVertex; i++)
  34.                     for (j = 0; j < countVertex; j++)
  35.                     {
  36.                         if (i == j) a[i, j] = 0;
  37.                         else a[i, j] = int.MaxValue;
  38.                         p[i, j] = -1;
  39.                     }
  40.  
  41.                 foreach (List<int> item in array)
  42.                     a[item[0], item[1]] = item[2];
  43.  
  44.                 for (k = 0; k < countVertex; k++)
  45.                     for (i = 0; i < countVertex; i++)
  46.                         for (j = 0; j < countVertex; j++)
  47.                         {
  48.                             long distance = a[i, k] + a[k, j];
  49.                             if (a[i, j] > distance)
  50.                             {
  51.                                 a[i, j] = distance;
  52.                                 p[i, j] = k;
  53.                             }
  54.                         }
  55.                 return a;
  56.             }
  57.  
  58.             public void WayFloyd(int a, int b, int[,] p, ref Queue<int> items)
  59.             {
  60.                 int k = p[a, b];
  61.                 if (k != -1)
  62.                 {
  63.  
  64.                     WayFloyd(a, k, p, ref items);
  65.                     items.Enqueue(k);
  66.                     WayFloyd(k, b, p, ref items);
  67.                 }
  68.             }
  69.  
  70.             public int [] Dijkstr(int v, out int []p)
  71.             {
  72.                 int[,] c = new int[countVertex, countVertex];
  73.                 for (int i = 0; i < countVertex; i++)
  74.                     for (int j = 0; j < countVertex; j++)
  75.                         c[i, j] = int.MaxValue;
  76.  
  77.                 foreach (List<int> item in array)
  78.                     c[item[0], item[1]] = item[2];
  79.  
  80.                 p = new int [countVertex];
  81.                 for (int j = 0; j < countVertex; j++)                    
  82.                         p[j] = v;
  83.                 p[v] = 0;
  84.  
  85.                 int[] distance = new int[countVertex];
  86.                 int count, index = 0, u, m = v;
  87.  
  88.                 for (int i = 0; i < countVertex; i++)
  89.                     distance[i] = int.MaxValue;
  90.                 distance[v] = 0;
  91.  
  92.                 for (count = 0; count < countVertex - 1; count++)
  93.                 {
  94.                     int min = int.MaxValue;
  95.                     for (int i = 0; i < countVertex; i++)
  96.                         if (!nov[i] && distance[i] <= min)
  97.                         {
  98.                             min = distance[i];
  99.                             index = i;
  100.                         }
  101.  
  102.                     u = index;
  103.                     nov[u] = true;
  104.                     for (int i = 0; i < countVertex; i++)
  105.                         if (!nov[i] && c[u, i] != int.MaxValue && distance[u] != int.MaxValue && distance[u] + c[u, i] < distance[i])//delete
  106.                         {
  107.                             distance[i] = distance[u] + c[u, i];
  108.                             p[i] = u;
  109.                         }
  110.                 }
  111.                 return distance;
  112.             }
  113.  
  114.             public void WayDijkstr(int a, int b, int[] p, ref Stack<int> items)
  115.             {
  116.                 items.Push(b);
  117.                 if (a == p[b]) items.Push(a);
  118.                 else WayDijkstr(a, p[b], p, ref items);
  119.             }
  120.         }
  121.  
  122.         private Node graph;
  123.  
  124.         public Graph(string nameOfFile)
  125.         {
  126.             StreamReader fin = new StreamReader(nameOfFile);
  127.             int n = int.Parse(fin.ReadLine());
  128.             int m = int.Parse(fin.ReadLine());
  129.             List<List<int>> tmp = new List<List<int>>();
  130.             for (int i = 0; i < n; i++)
  131.             {
  132.                 List<int> tmp2 = new List<int>();
  133.                 string[] arr = fin.ReadLine().Split(' ');
  134.                 for (int j = 0; j < arr.Length; j++)
  135.                     tmp2.Add(int.Parse(arr[j]));
  136.                 tmp.Add(tmp2);
  137.             }
  138.             fin.Close();
  139.             graph = new Node(tmp, m);
  140.         }
  141.  
  142.         public void Floyd()
  143.         {
  144.             int[,] p;
  145.             Console.WriteLine("Введите вершину, из кторой необходимо найти кратчайший путь: ");
  146.             int a = int.Parse(Console.ReadLine()) ;
  147.             Console.WriteLine("Введите вершину, в которую необходимо найти кратчайший путь: ");
  148.             int b = int.Parse(Console.ReadLine()) ;
  149.  
  150.             long[,] tmp = graph.Floyd(out p);
  151.  
  152.  
  153.             if (tmp[a, b] == int.MaxValue)
  154.                 Console.WriteLine("Пути из вершины {0} в вершину {1} не существует", a , b );
  155.             else
  156.             {
  157.                 Console.WriteLine("Кратчайший путь от вершины {0} до вершины {1} равен {2}, ", a , b , tmp[a, b]);
  158.  
  159.                 Console.Write("Путь: ");
  160.                 Queue<int> items = new Queue<int>();
  161.                 items.Enqueue(a);
  162.                 graph.WayFloyd(a, b, p, ref items);
  163.                 items.Enqueue(b);
  164.                 while (items.Count != 0)
  165.                     Console.Write("{0} ", items.Dequeue());
  166.                 Console.WriteLine();
  167.             }
  168.         }
  169.  
  170.         public void Dijkstr(int v)
  171.         {
  172.             graph.NovSet();
  173.             int[] p;
  174.             int[] d = graph.Dijkstr(v, out p);
  175.             Console.WriteLine("Длина кратчайшие пути от вершины {0} до вершины", v);
  176.             for (int i = 0; i < graph.Size; i++)
  177.             {
  178.                 if ((i != v) && (d[i] != int.MaxValue))
  179.                 {
  180.                     Console.Write("{0} равна {1}, ", i, d[i]);
  181.                     Console.Write("путь ");
  182.  
  183.                     Stack<int> items = new Stack<int>();
  184.                     graph.WayDijkstr(v, i, p, ref items);
  185.                     while (items.Count != 0)
  186.                     {
  187.                         Console.Write("{0} ", items.Pop());
  188.                     }
  189.                 }              
  190.                 Console.WriteLine();
  191.             }            
  192.         }
  193.        
  194.     }
  195.     class Program
  196.     {
  197.         static void Main(string[] args)
  198.         {
  199.             Graph graph1 = new Graph("input.txt");
  200.             Console.WriteLine("Алгоритм Флойда: ");
  201.             Console.WriteLine();
  202.             graph1.Floyd();
  203.             Console.WriteLine();
  204.             Console.WriteLine("Алгортим Дейкстры: ");
  205.             Console.WriteLine();
  206.             graph1.Dijkstr(0);
  207.         }
  208.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement