Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.06 KB | None | 0 0
  1. static void Odtwórz(int?[] drogi)
  2.         {
  3.             Stack<int?> s1 = new Stack<int?>();
  4.             for (int i = 2; i < drogi.Length; i++)
  5.             {
  6.                 int? tmp = drogi[i];
  7.                 s1.Push(i);
  8.                 while (tmp != null)
  9.                 {
  10.                     s1.Push(tmp);
  11.                     tmp = drogi[(int)tmp];
  12.                 }
  13.                 Console.Write("(");
  14.                 while (s1.Count > 0)
  15.                 {
  16.  
  17.  
  18.                     if (s1.Count == 1)
  19.                     {
  20.                         Console.Write($"{s1.Pop()}");
  21.                     }
  22.                     else
  23.                     {
  24.                         Console.Write($"{s1.Pop()},");
  25.                     }
  26.  
  27.  
  28.                 }
  29.                 Console.Write(") ");
  30.  
  31.  
  32.             }
  33.             Console.WriteLine();
  34.         }
  35.         static void BFS(int[,] Graf, bool[] odwiedzony, int?[] droga, int wierzcholek)
  36.         {
  37.             Queue<int> q1 = new Queue<int>();
  38.             int tmp = wierzcholek;
  39.             q1.Enqueue(tmp);
  40.  
  41.             while (q1.Count > 0)
  42.             {
  43.                 tmp = q1.Dequeue();
  44.                 Console.Write(tmp + " ");
  45.                 odwiedzony[tmp] = true;
  46.                 for (int i = 0; i < odwiedzony.Length; i++)
  47.                 {
  48.                     if (!odwiedzony[i] && Graf[tmp, i] != 0)
  49.                     {
  50.                         q1.Enqueue(i);
  51.                         odwiedzony[i] = true;
  52.                         droga[i] = tmp;
  53.  
  54.                     }
  55.                 }
  56.  
  57.             }
  58.             Console.WriteLine();
  59.         }
  60.         static void BFS2(int[,] Graf, bool[] odwiedzony, int?[] droga, int wierzcholek)
  61.         {
  62.             Queue<int>q1 = new Queue<int>();
  63.             int tmp = wierzcholek;
  64.             q1.Enqueue(tmp);
  65.  
  66.             while (q1.Count > 0)
  67.             {
  68.                 tmp = q1.Dequeue();
  69.  
  70.                 if (!odwiedzony[tmp])
  71.                 {
  72.                     Console.Write(tmp + " ");
  73.                     odwiedzony[tmp] = true;
  74.                     for (int i = 0; i <odwiedzony.Length; i++)
  75.                     {
  76.                         if (!odwiedzony[i] && Graf[tmp, i] != 0)
  77.                         {
  78.                             q1.Enqueue(i);
  79.                             // odwiedzony[i] = true;
  80.                             droga[i] = tmp;
  81.  
  82.                         }
  83.                     }
  84.                 }
  85.  
  86.  
  87.             }
  88.  
  89. static void DFS(int[,] Graf, bool[] odwiedzony, int?[] droga, int wierzcholek)
  90.         {
  91.             Stack<int> s1 = new Stack<int>();
  92.             int tmp = wierzcholek;
  93.             s1.Push(tmp);
  94.  
  95.             while (s1.Count > 0)
  96.             {
  97.                 tmp = s1.Pop();
  98.                 Console.Write(tmp + " ");
  99.                 odwiedzony[tmp] = true;
  100.                 for (int i = odwiedzony.Length - 1; i >= 0; i--)
  101.                 {
  102.                     if (!odwiedzony[i] && Graf[tmp, i] != 0)
  103.                     {
  104.                         s1.Push(i);
  105.                         odwiedzony[i] = true;
  106.                         droga[i] = tmp;
  107.  
  108.                     }
  109.                 }
  110.  
  111.             }
  112.             Console.WriteLine();
  113.         }
  114.         static void DFS2(int[,] Graf, bool[] odwiedzony, int?[] droga, int wierzcholek)
  115.         {
  116.             Stack<int> s1 = new Stack<int>();
  117.             int tmp = wierzcholek;
  118.             s1.Push(tmp);
  119.  
  120.             while (s1.Count > 0)
  121.             {
  122.                 tmp = s1.Pop();
  123.  
  124.                 if (!odwiedzony[tmp])
  125.                 {
  126.                     Console.Write(tmp + " ");
  127.                     odwiedzony[tmp] = true;
  128.                     for (int i = odwiedzony.Length - 1; i >= 0; i--)
  129.                     {
  130.                         if (!odwiedzony[i] && Graf[tmp, i] != 0)
  131.                         {
  132.                             s1.Push(i);
  133.                             // odwiedzony[i] = true;
  134.                             droga[i] = tmp;
  135.  
  136.                         }
  137.                     }
  138.                 }
  139.  
  140.  
  141.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement