Advertisement
Guest User

zad5.5

a guest
May 22nd, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.97 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.Threading;
  7.  
  8. namespace PW1
  9. {
  10.     class Program
  11.     {
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.             GraphM g1 = new GraphM(3, 0.5);
  16.             GraphM g2 = new GraphM(3, 0.5);
  17.  
  18.             g1.Print();
  19.             g2.Print();
  20.  
  21.             Isomorphism iso = new Isomorphism(g1, g2);
  22.             iso.IsIsomorphicThreads();
  23.  
  24.  
  25.             Console.ReadKey();
  26.         }
  27.  
  28.     }
  29.  
  30.     class Isomorphism
  31.     {
  32.         GraphM g1;
  33.         GraphM g2;
  34.         int n;
  35.  
  36.         Thread[] watki;
  37.         static readonly object syncObject = new object();
  38.  
  39.         public Isomorphism(GraphM gr1, GraphM gr2)
  40.         {
  41.             this.g1 = gr1;
  42.             this.g2 = gr2;
  43.             n = g1.n;
  44.         }
  45.  
  46.         public void IsIsomorphic()
  47.         {
  48.             int[] t = new int[n];
  49.  
  50.             for (int i = 0; i < n; i++)
  51.                 t[i] = i;
  52.  
  53.             try
  54.             {
  55.                 Permutacje.PermTab(t, n, 0, Out, -1);
  56.             }
  57.             catch (Exception e)
  58.             {
  59.                 Console.WriteLine("Grafy sa izomorficzne");
  60.                 return;
  61.             }
  62.             Console.WriteLine("Grafy nie sa izomorficzne");
  63.         }
  64.  
  65.         public void Out(int[] t, int w)
  66.         {
  67.             GraphM gTmp = new GraphM(Permutacje.GraphMatrixPerm(g2.t2, t, n), n);
  68.             // gTmp = permutacja mcierzy g2
  69.  
  70.             for (int i = 0; i < n; i++) // sprawdzanie czy macierze sa rowne
  71.             {
  72.                 for (int j = 0; j < n; j++)
  73.                 {
  74.                     if (g1.t2[i, j] != gTmp.t2[i, j])
  75.                         return;
  76.                 }
  77.             }
  78.  
  79.             throw new Exception(); // jesli doszlo do tego momentu, to znacyz ze wszystkie pola w macierzach sa rowne wiec grafy sa izomorficzne
  80.         }
  81.  
  82.         public void IsIsomorphicThreads()
  83.         {
  84.             watki = new Thread[n];
  85.  
  86.             for (int i = 0; i < n; i++)
  87.             {
  88.                 int x = i;
  89.                 watki[i] = new Thread(() => F(x));
  90.             }
  91.  
  92.             for (int i = 0; i < n; i++)
  93.                 watki[i].Start();
  94.  
  95.  
  96.             for (int i = 0; i < n; i++)
  97.                 watki[i].Join();
  98.  
  99.         }
  100.  
  101.         public void OutT(int[] tOkrojone, int w)
  102.         {
  103.             int[] t = new int[n];
  104.  
  105.             t[0] = w;
  106.             for (int i = 0; i < n - 1; i++)
  107.             {
  108.                 t[i + 1] = tOkrojone[i];
  109.             }
  110.  
  111.             GraphM gTmp = new GraphM(Permutacje.GraphMatrixPerm(g2.t2, t, n), n);
  112.             // gTmp = permutacja mcierzy g2
  113.  
  114.             for (int i = 0; i < n; i++) // sprawdzanie czy macierze sa rowne
  115.             {
  116.                 for (int j = 0; j < n; j++)
  117.                 {
  118.                     if (g1.t2[i, j] != gTmp.t2[i, j])
  119.                         return;
  120.                 }
  121.             }
  122.  
  123.             throw new Exception(); // jesli doszlo do tego momentu, to znacyz ze wszystkie pola w macierzach sa rowne wiec grafy sa izomorficzne
  124.         }
  125.  
  126.         public void F(int w)
  127.         {
  128.             int[] t = new int[n];
  129.  
  130.             for (int i = 0; i < n; i++)
  131.                 t[i] = i;
  132.  
  133.             Permutacje.InitPermForThread(t, n, w);
  134.  
  135.             int[] tOkrojone = new int[n - 1];
  136.             for (int i = 0; i < n - 1; i++)
  137.                 tOkrojone[i] = t[i + 1];
  138.  
  139.             try
  140.             {
  141.                 Permutacje.PermTab(tOkrojone, n - 1, 0, OutT, w);
  142.             }
  143.             catch (Exception e)
  144.             {
  145.                 Console.WriteLine("W{0}: Grafy sa izomorficzne", w);
  146.  
  147.                 return;
  148.             }
  149.  
  150.             Console.WriteLine("W{0}: nie stwierdzono izomorfizmu", w);
  151.  
  152.         }
  153.  
  154.     }
  155.    
  156.     class GraphM
  157.     {
  158.         // GraphM - graf w reprezentacji Macierzy zerojedynkowej
  159.  
  160.         static Random rnd = new Random();
  161.  
  162.         public int n; // liczba wierzcholkow
  163.         public int[,] t2; // macierz
  164.  
  165.         public GraphM(int[,] matrix, int n)
  166.         {
  167.             this.n = n;
  168.             this.t2 = matrix;
  169.         }
  170.  
  171.         public GraphM(int n, double p)
  172.         {
  173.             this.n = n;
  174.             t2 = Gnp(n, p);
  175.         }
  176.  
  177.         public int[,] Gnp(int n, double p)
  178.         {
  179.             int[,] t2 = new int[n, n];
  180.  
  181.             for (int i = 0; i < n - 1; i++)
  182.                 for (int j = i + 1; j < n; j++)
  183.                 {
  184.                     if (rnd.Next(0, 10) <= (int)(p * 10))
  185.                         t2[i, j] = t2[j, i] = 1;
  186.                     else
  187.                         t2[i, j] = t2[j, i] = 0;
  188.                 }
  189.  
  190.             return t2;
  191.         }
  192.  
  193.         public void Print()
  194.         {
  195.             for (int i = 0; i < n; i++)
  196.             {
  197.                 for (int j = 0; j < n; j++)
  198.                     Console.Write(t2[i, j] + " ");
  199.  
  200.                 Console.WriteLine();
  201.             }
  202.             Console.WriteLine();
  203.         }
  204.  
  205.  
  206.     }
  207.    
  208.     class Permutacje
  209.     {
  210.         static void Swap(ref int a, ref int b)
  211.         {
  212.             int c = a;
  213.             a = b;
  214.             b = c;
  215.         }
  216.  
  217.         public static void PermTab(int[] t, int n, int ix, Action<int[], int> Out, int w) // ix = 0..N-1, wywolujemy od 0, w wykorzystujemy tylko jak uzywamy watkow
  218.         {
  219.             if (ix < n - 1)
  220.             {
  221.                 for (int i = ix; i < n; i++)
  222.                 {
  223.                     Swap(ref t[ix], ref t[i]); // zamienia dwa elementy miejscami
  224.                     PermTab(t, n, ix + 1, Out, w);
  225.                     Swap(ref t[ix], ref t[i]); // przywracamy
  226.                 }
  227.             }
  228.  
  229.             else Out(t, w);
  230.             // powracamy do metody, w klasie ktora wywolala Permutacje
  231.             // konieczne jest zaimplementowanie tej metody w klasie wywolujacej
  232.  
  233.         }
  234.  
  235.         public static int[,] GraphMatrixPerm(int[,] A, int[] perm, int n)
  236.         {
  237.             int[,] B = new int[n, n];
  238.             int x, y;
  239.  
  240.             for (int i = 0; i < n - 1; i++)
  241.             {
  242.                 for (int j = i + 1; j < n; j++)
  243.                 {
  244.                     if (A[i, j] == 1)
  245.                     {
  246.                         x = perm[i];
  247.                         y = perm[j];
  248.                         B[x, y] = B[y, x] = 1;
  249.                     }
  250.                 }
  251.             }
  252.  
  253.             return B;
  254.         }
  255.  
  256.         public static void InitPermForThread(int[] perm, int n, int w)
  257.         {
  258.             int i, poz = 1;
  259.             perm[0] = w;
  260.             for (i = 0; i < w; i++) { perm[poz] = i; poz++; }
  261.             for (i = w + 1; i < n; i++) { perm[poz] = i; poz++; }
  262.         }
  263.  
  264.     }
  265.    
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement