Венгерский алгоритм - C#

Узнай цену своей работы

Формулировка задачи:

Ребята прошу Вашей помощи, нужен исходник Венгерского алгоритма на C#

Решение задачи: «Венгерский алгоритм»

textual
Листинг программы
  1. using System;
  2.  
  3. public class OptimalAssignmentSolver {
  4.     /// <summary>
  5.     /// Solves optimal assignment problem using the Kuhn-Munkres
  6.     /// (aka Hungarian) algorithm. Time complexity: O(n^3).
  7.     ///
  8.     /// References:
  9.     /// [url]http://www.math.uwo.ca/~mdawes/courses/344/kuhn-munkres.pdf[/url]
  10.     /// [url]http://www.ecp6.jussieu.fr/pageperso/bondy/books/gtwa/pdf/chapter5.pdf[/url]
  11.     /// </summary>
  12.     /// <param name="a">A square matrix.</param>
  13.     /// <returns>
  14.     /// A permutation p of integers 0, 1, ..., n-1, (where
  15.     /// n is the size of matrix a) such that the expression
  16.     ///   a[0, p[0]] + a[1, p[1]] + ... + a[n-1, p[n-1]]
  17.     /// is maximum possible among all such permutations.
  18.     /// </returns>
  19.     public static int[] KuhnMunkres(int[,] a) {
  20.         int N = a.GetLength(0);
  21.         if (N == 0)
  22.             return new int[0];
  23.  
  24.         int[] lx = new int[N], ly = new int[N];   // labelling function for vertices in first and second partitions
  25.         int[] mx = new int[N], my = new int[N];   // mx[u]=v, my[v]=u <==> u and v are currently matched;  -1 values means 'not matched'
  26.         int[] px = new int[N], py = new int[N];   // predecessor arrays.  used in DFS to reconstruct paths.
  27.         int[] stack = new int[N];
  28.  
  29.         // invariant: lx[u] + ly[v] >= a[u, v]
  30.         // (implies that any perfect matching in subgraph containing only
  31.         // edges u, v for which lx[u]+ly[v]=a[u,v] is the optimal matching.)
  32.  
  33.         // compute initial labelling function:  lx[i] = max_j(a[i, j]), ly[j] = 0;
  34.         for (int i = 0; i < N; i++) {
  35.             lx[i] = a[i, 0];
  36.             for (int j = 0; j < N; j++)
  37.                 if (a[i, j] > lx[i]) lx[i] = a[i, j];
  38.             ly[i] = 0;
  39.  
  40.             mx[i] = my[i] = -1;
  41.         }
  42.  
  43.         for (int size = 0; size < N;) {
  44.             int s;
  45.             for (s = 0; mx[s] != -1; s++);
  46.  
  47.             // s is an unmatched vertex in the first partition.
  48.             // At the current iteration we will either find an augmenting path
  49.             // starting at s, or we'll extend the equality subgraph so that
  50.             // such a path will exist at the next iteration.
  51.  
  52.             for (int i = 0; i < N; i++)
  53.                 px[i] = py[i] = -1;
  54.             px[s] = s;
  55.  
  56.             // DFS
  57.             int t = -1;
  58.             stack[0] = s;
  59.             for (int top = 1; top > 0;) {
  60.                 int u = stack[--top];
  61.                 for (int v = 0; v < N; v++) {
  62.                     if (lx[u] + ly[v] == a[u, v] && py[v] == -1) {
  63.                         if (my[v] == -1) {
  64.                             // we've found an augmenting path
  65.                             t = v;
  66.                             py[t] = u;
  67.                             top = 0;
  68.                             break;
  69.                         }
  70.  
  71.                         py[v] = u;
  72.                         px[my[v]] = v;
  73.                         stack[top++] = my[v];
  74.                     }
  75.                 }
  76.             }
  77.  
  78.             if (t != -1) {
  79.                 // augment along the found path
  80.                 while (true) {
  81.                     int u = py[t];
  82.                     mx[u] = t;
  83.                     my[t] = u;
  84.                     if (u == s) break;
  85.                     t = px[u];
  86.                 }
  87.                 ++size;
  88.             } else {
  89.                 // No augmenting path exists from s in the current equality graph,
  90.                 // Modify labelling function a bit...
  91.  
  92.                 int delta = int.MaxValue;
  93.                 for (int u = 0; u < N; u++) {
  94.                     if (px[u] == -1) continue;
  95.                     for (int v = 0; v < N; v++) {
  96.                         if (py[v] != -1) continue;
  97.                         int z = lx[u] + ly[v] - a[u, v];
  98.                         if (z < delta)
  99.                             delta = z;
  100.                     }
  101.                 }
  102.  
  103.                 for (int i = 0; i < N; i++) {
  104.                     if (px[i] != -1) lx[i] -= delta;
  105.                     if (py[i] != -1) ly[i] += delta;
  106.                 }
  107.             }
  108.         }
  109.  
  110.         // Verify optimality
  111.         bool correct = true;
  112.         for (int u = 0; u < N; u++) {
  113.             for (int v = 0; v < N; v++) {
  114.                 correct &= (lx[u] + ly[v] >= a[u, v]);
  115.                 if (mx[u] == v)
  116.                     correct &= (lx[u] + ly[v] == a[u, v]);
  117.  
  118.                 if (!correct) {
  119.                     throw new Exception(
  120.                         "*** Internal error: optimality conditions are not satisfied ***\n" +
  121.                         "Most probably an overflow occurred");
  122.                 }
  123.             }
  124.         }
  125.  
  126.         return mx;
  127.     }
  128. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

12   голосов , оценка 4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы