Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- //Zadanie 1
- //A)
- //Dla prostego grafu nieskierowanego bez wag krawędzi, napisz metodę
- // konwertująca reprezentację w postaci macierzy sąsiedztw na reprezentację w postaci list sąsiedztwa.
- // Przetestuj jej działanie na przykład dla grafu z wykładu.
- class Program
- {
- // numeracja od 0
- static List<int>[] KonwersjaNaListy(int[,] G)
- {
- List<int>[] graf = new List<int>[G.GetLength(0)];
- for(int i =0; i< graf.Length;i++)
- {
- graf[i] = new List<int>();
- for (int j = 0; j < G.GetLength(0); j++)
- if (G[i, j] != 0) graf[i].Add(j);
- }
- return graf;
- }
- static void Main(string[] args)
- {
- int[,] G = {
- { 0, 1, 0, 0, 1, 0 },
- { 1, 0, 1, 0, 1, 0 },
- { 0, 1, 0, 1, 0, 0 },
- { 0, 0, 1, 0, 1, 1 },
- { 1, 1, 0, 1, 0, 0 },
- { 0, 0, 0, 1, 0, 0 }
- };
- List<int>[] graf = KonwersjaNaListy(G);
- for (int i = 0; i < graf.Length; i++)
- {
- Console.Write("L("+i+") = {");
- for (int j = 0; j < graf[i].Count-1; j++)
- Console.Write(graf[i][j]+", ");
- Console.Write(graf[i][graf[i].Count - 1] + "}");
- Console.WriteLine();
- }
- Console.ReadKey();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement