Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace Kraskal
- {
- class Program
- {
- public static Dictionary<char, int> dictionary;
- private static int[] name;
- private static int[] next;
- private static int[] size;
- static void Main(string[] args)
- {
- dictionary = new Dictionary<char, int>();
- var characters = "ABCDEFG";
- var countOfNodes = 0;
- foreach (var ch in characters)
- {
- dictionary.Add(ch, countOfNodes);
- countOfNodes++;
- }
- var graph = Reader.GetGraph("in.txt");
- //
- name = new int[countOfNodes];
- next = new int[countOfNodes];
- size = new int[countOfNodes];
- graph = graph.OrderBy(x => x.Weight).ToList();
- foreach (var v in characters)
- {
- var key = dictionary[v];
- name[key] = key;
- next[key] = key;
- size[key] = 1;
- }
- var tree = new List<Edge>();
- while (tree.Count != countOfNodes - 2)
- {
- var kkk = graph.Take(1);
- var edge = new Edge();
- foreach (var kek in kkk)
- edge = kek;
- graph = graph.Skip(1).ToList();
- var w = edge.From;
- var v = edge.To;
- var p = name[dictionary[v]];
- var q = name[dictionary[w]];
- if (p != q)
- {
- if (size[p] > size[q])
- Merge(dictionary[w], dictionary[v], p, q);
- else
- Merge(dictionary[v], dictionary[w], q, p);
- tree.Add(edge);
- }
- }
- //
- Writer.ShowTree(tree);
- }
- private static void Merge(int v, int w, int p, int q)
- {
- name[w] = p;
- var u = next[w];
- while (name[u] != p)
- {
- name[u] = p;
- u = next[u];
- }
- size[p] += size[q];
- var temp1 = next[v];
- var temp2 = next[v];
- next[v] = temp2;
- next[w] = temp1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement