Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.12 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.  
  7. namespace ZZ_Zadania_Z_Kolokiwum_
  8. {       //Zadanie 5
  9.         //W algorytmie BFS kolejność przechodzenia
  10.         //wierzchołków wyznacza pewne drzewo
  11.         //wierzchołek a zwraca listę krawędzi:
  12.         //pray( wierzchołek początkowy,wierzchołek końcowy)
  13.         //drzew ..
  14.         //Dla grafu jak na rysunku i wierzchołka 1 zwraca:
  15.         //{(1,2) , (2,5), (2,3) , (3,4), (4,5);
  16.  
  17.     class Graf
  18.     {
  19.         public int początkowy;
  20.         public int końcowy;
  21.  
  22.         public Graf(int wierzchołek, int sąsiad)
  23.         {
  24.             this.początkowy = wierzchołek;
  25.             this.końcowy = sąsiad;
  26.         }
  27.         public override string ToString()
  28.         {
  29.             return "(" + (początkowy + 1) + ";" + (końcowy +1) + ")";
  30.         }
  31.  
  32.     }
  33.     class Program
  34.     {
  35.         public static List<List<int>> MacierzNaListe(int[,] tab)
  36.         {
  37.             List<List<int>> wynik = new List<List<int>>();
  38.             for (int i = 0; i < tab.GetLength(0); i++)
  39.             {
  40.                 wynik.Add(new List<int>());
  41.                 for (int j = 0; j < tab.GetLength(1); j++)
  42.                 {
  43.                     if(tab[i,j] != 0)
  44.                     {
  45.                         wynik[i].Add(j);
  46.                     }
  47.                 }
  48.             }
  49.  
  50.             return wynik;
  51.         }
  52.  
  53.  
  54.         public static List<Graf> BFS(List<List<int>> graf)
  55.         {
  56.             int start = 0;
  57.             Stack<int> kolejka = new Stack<int>();
  58.             List<Graf> wyniki = new List<Graf>();
  59.             kolejka.Push(start);
  60.             bool[] odwiedzone = new bool[graf.Count];
  61.  
  62.             while (kolejka.Count != 0)
  63.             {
  64.                 int pobrany = kolejka.Pop();
  65.  
  66.                 if (odwiedzone[pobrany] == false)
  67.                 {
  68.                     odwiedzone[pobrany] = true;
  69.  
  70.                     for (int i = 0; i < graf[pobrany].Count; i++)
  71.                     {
  72.  
  73.                         int nastepny = graf[pobrany][graf[pobrany].Count - i - 1];
  74.                         if (!odwiedzone[nastepny])
  75.                         {
  76.                             kolejka.Push(nastepny);
  77.                             wyniki.Add(new Graf(pobrany, nastepny));
  78.                         }
  79.                     }
  80.  
  81.  
  82.                 }
  83.                
  84.  
  85.             }
  86.             return wyniki;
  87.         }
  88.  
  89.  
  90.  
  91.         static void Main(string[] args)
  92.         {                  //1  2  3  4  5 6
  93.             int[,] tab ={   {0, 1, 0, 0, 1, 0},
  94.                             {1, 0, 1, 0, 1, 0},
  95.                             {0, 1, 0, 1, 0, 0},
  96.                             {0, 0, 1, 0, 1, 1},
  97.                             {1, 1, 0, 1, 0, 0},
  98.                             {0, 0, 0, 1, 0 ,0} };
  99.  
  100.  
  101.             List<List<int>> graf = MacierzNaListe(tab);
  102.             List<Graf> lista = BFS(graf);
  103.             foreach (var item in lista)
  104.             {
  105.                 Console.Write(item + "->");
  106.  
  107.  
  108.             }
  109.  
  110.             Console.ReadKey();
  111.  
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement