Advertisement
Lusien_Lashans

KraskalAlgo

Jan 1st, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace Kraskal
  9. {
  10.     class Program
  11.     {
  12.         public static Dictionary<char, int> dictionary;
  13.         private static int[] name;
  14.         private static int[] next;
  15.         private static int[] size;
  16.         static void Main(string[] args)
  17.         {
  18.             dictionary = new Dictionary<char, int>();
  19.             var characters = "ABCDEFG";
  20.             var countOfNodes = 0;
  21.             foreach (var ch in characters)
  22.             {
  23.                 dictionary.Add(ch, countOfNodes);
  24.                 countOfNodes++;
  25.             }
  26.  
  27.             var graph = Reader.GetGraph("in.txt");
  28.             //
  29.             name = new int[countOfNodes];
  30.             next = new int[countOfNodes];
  31.             size = new int[countOfNodes];
  32.  
  33.             graph = graph.OrderBy(x => x.Weight).ToList();
  34.             foreach (var v in characters)
  35.             {
  36.                 var key = dictionary[v];
  37.                 name[key] = key;
  38.                 next[key] = key;
  39.                 size[key] = 1;
  40.             }
  41.             var tree = new List<Edge>();
  42.             while (tree.Count != countOfNodes - 2)
  43.             {
  44.                 var kkk = graph.Take(1);
  45.                 var edge = new Edge();
  46.                 foreach (var kek in kkk)
  47.                     edge = kek;
  48.                 graph = graph.Skip(1).ToList();
  49.                 var w = edge.From;
  50.                 var v = edge.To;
  51.                 var p = name[dictionary[v]];
  52.                 var q = name[dictionary[w]];
  53.                 if (p != q)
  54.                 {
  55.                     if (size[p] > size[q])
  56.                         Merge(dictionary[w], dictionary[v], p, q);
  57.                     else
  58.                         Merge(dictionary[v], dictionary[w], q, p);
  59.  
  60.                     tree.Add(edge);
  61.                 }
  62.             }
  63.             //
  64.             Writer.ShowTree(tree);
  65.         }
  66.  
  67.         private static void Merge(int v, int w, int p, int q)
  68.         {
  69.             name[w] = p;
  70.             var u = next[w];
  71.             while (name[u] != p)
  72.             {
  73.                 name[u] = p;
  74.                 u = next[u];
  75.             }
  76.  
  77.             size[p] += size[q];
  78.             var temp1 = next[v];
  79.             var temp2 = next[v];
  80.             next[v] = temp2;
  81.             next[w] = temp1;
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement