Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 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. //Zadanie 1
  8. //A)
  9. //Dla prostego grafu nieskierowanego bez wag krawędzi, napisz metodę
  10. // konwertująca reprezentację w postaci macierzy sąsiedztw na reprezentację w postaci list sąsiedztwa.
  11. // Przetestuj jej działanie na przykład dla grafu z wykładu.
  12.  
  13.  
  14. class Program
  15. {
  16. // numeracja od 0
  17. static List<int>[] KonwersjaNaListy(int[,] G)
  18. {
  19. List<int>[] graf = new List<int>[G.GetLength(0)];
  20. for(int i =0; i< graf.Length;i++)
  21. {
  22. graf[i] = new List<int>();
  23. for (int j = 0; j < G.GetLength(0); j++)
  24. if (G[i, j] != 0) graf[i].Add(j);
  25. }
  26. return graf;
  27. }
  28. static void Main(string[] args)
  29. {
  30. int[,] G = {
  31. { 0, 1, 0, 0, 1, 0 },
  32. { 1, 0, 1, 0, 1, 0 },
  33. { 0, 1, 0, 1, 0, 0 },
  34. { 0, 0, 1, 0, 1, 1 },
  35. { 1, 1, 0, 1, 0, 0 },
  36. { 0, 0, 0, 1, 0, 0 }
  37. };
  38.  
  39. List<int>[] graf = KonwersjaNaListy(G);
  40. for (int i = 0; i < graf.Length; i++)
  41. {
  42. Console.Write("L("+i+") = {");
  43. for (int j = 0; j < graf[i].Count-1; j++)
  44. Console.Write(graf[i][j]+", ");
  45. Console.Write(graf[i][graf[i].Count - 1] + "}");
  46. Console.WriteLine();
  47. }
  48.  
  49. Console.ReadKey();
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement