public static void Floyd() { Graf main = new Graf(4, 4); main.PrintGrafAStar(); var dijkstra = new Floyd(main); var path = dijkstra.GetPath(main.graf[0, 0], main.graf[3, 3]); for (int i = 0; i < path.Count; i++) { Console.WriteLine("PozX: " + path[i].pozX.ToString() + "PozY: " + path[i].pozX.ToString() + "\n"); } }
/// <summary> /// zmodyfikowany algorytm przeszukiwania w glab ktory zwroci pierwszy cykl grafu ktory znajdzie ktory spelnia wlasnosc /// ze jego dlugosc jest wieksza o 1 od jego stopnia minimalnego. /// </summary> /// <param name="graf">graf wejsciowy</param> public void zwrocCykl(Graf graf) { this.graf = graf; if (graf.StopienMinimalny() < 2) { Console.WriteLine("Stopien grafu za niski."); return; } for (int i = 0; i < graf.LiczbaWierzcholkow; i++) { wierzcholekStartowy = i; przeszukaj(graf); } }
static void Main(string[] args) { Console.WriteLine("Graf nieskierowany"); //macierz sąsiedztwa int[,] matrix = { { 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 }, }; // macierz w postaci list sąsiedztwa - numeracja od 0 List <List <int> > listOfMatrix = new List <List <int> >(); List <int> l1 = new List <int>(); l1.Add(1); l1.Add(4); listOfMatrix.Add(l1); List <int> l2 = new List <int>(); l2.Add(0); l2.Add(2); l2.Add(4); listOfMatrix.Add(l2); List <int> l3 = new List <int>(); l3.Add(1); l3.Add(3); listOfMatrix.Add(l3); List <int> l4 = new List <int>(); l4.Add(2); l4.Add(4); l4.Add(5); listOfMatrix.Add(l4); List <int> l5 = new List <int>(); l5.Add(0); l5.Add(1); l5.Add(3); listOfMatrix.Add(l5); List <int> l6 = new List <int>(); l6.Add(3); listOfMatrix.Add(l6); Console.WriteLine("Lista sąsiedztwa bezpośrednio"); ShowListOfMatrix(listOfMatrix); List <List <int> > GG = Graf.MacierzWListe(matrix); Console.WriteLine("Lista sąsiedztwa z macierzy (konwertowanie Macierz => Lista sasiedztwa)"); ShowListOfMatrix(GG); int[,] tab2 = Graf.ListaWMacierz(listOfMatrix); Console.WriteLine("Macierz z listy sąsiedztwa (konwertowanie Lista => Macierz)"); ShowMatrix(tab2); Console.WriteLine("------------------------------------"); int[,] tabA = { { 0, 1, 1, 0, 0 }, { 1, 0, 1, 1, 0 }, { 1, 1, 0, 0, 1 }, { 0, 1, 0, 0, 1 }, { 0, 0, 1, 1, 0 }, }; //macierz sąsiedztwa int[,] tabB = { { 1, 0, 0, 3, 0, 0, 7 }, { 0, 0, 2, 0, 4, 0, 1 }, { 0, 2, 0, 4, 1, 2, 3 }, { 3, 0, 4, 0, 5, 3, 3 }, { 0, 4, 1, 5, 0, 1, 2 }, { 0, 0, 2, 3, 1, 0, 2 }, { 7, 1, 3, 3, 2, 2, 0 }, }; List <int> wyniki = Graf.DFS(tabA); foreach (var item in wyniki) { Console.Write(item + " "); } Console.WriteLine("\n"); List <int> wyniki2 = Graf.BFS(tabA); foreach (var item in wyniki2) { Console.Write(item + " "); } Console.WriteLine("\nDFS na liście sąsiedztwa \n"); List <List <int> > lista3 = Graf.MacierzWListe(tabA); List <int> wyniki3 = Graf.DFS(lista3); foreach (var item in wyniki3) { Console.Write(item + " "); } Console.ReadKey(); }
/// <summary> /// Wlasciwy algorytm. /// </summary> /// <param name="graf">Graf do przeszukania.</param> /// <param name="wierzcholekZaczynajacy">wierzcholek od ktorego zaczynamy.</param> private void przeszukaj(Graf graf) { wierzcholki = new bool[graf.LiczbaWierzcholkow]; for (int i = 0; i < wierzcholki.Length; i++) { wierzcholki[i] = false; } dlugoscMinimalna = graf.StopienMinimalny() + 1; sciezka.Clear(); odwiedz(wierzcholekStartowy); }
static void Main(string[] args) { /* List<Tuple<int,int>> krawedzie = new List<Tuple<int,int>>(); krawedzie.Add(new Tuple<int,int>(0,1)); krawedzie.Add(new Tuple<int,int>(1,2)); krawedzie.Add(new Tuple<int,int>(2,0)); Graf graf = new Graf(3, krawedzie); Console.WriteLine(graf); graf.Dodaj(2); graf.Dodaj(new Tuple<int, int>(3, 2)); graf.Dodaj(new Tuple<int, int>(3, 1)); graf.Dodaj(new Tuple<int, int>(4, 3)); graf.Dodaj(new Tuple<int,int>(4, 0)); Console.WriteLine(graf); Console.WriteLine(graf); Console.WriteLine(graf.IleStopniNieparzystych()); Console.WriteLine(graf.IleStopniParzystych()); Console.WriteLine(String.Join(",", graf.CiagStopni())); Console.ReadKey(); List<Tuple<int,int>> krawedzie2 = new List<Tuple<int,int>>(); krawedzie2.Add(new Tuple<int,int>(1, 0)); krawedzie2.Add(new Tuple<int,int>(2, 0)); krawedzie2.Add(new Tuple<int, int>(3, 0)); krawedzie2.Add(new Tuple<int, int>(3, 2)); krawedzie2.Add(new Tuple<int, int>(3, 1)); Graf graf2 = new Graf(4, krawedzie2); Console.WriteLine(graf2); Console.WriteLine(graf2.CzyJestCyklC3()); List<Tuple<int,int>> krawedzie3 = new List<Tuple<int,int>>(); krawedzie3.Add(new Tuple<int,int>(1, 0)); krawedzie3.Add(new Tuple<int,int>(2, 0)); krawedzie3.Add(new Tuple<int, int>(3, 0)); Graf graf3 = new Graf(4, krawedzie3); Console.WriteLine(graf3); Console.WriteLine(graf3.CzyJestCyklC3()); Console.ReadKey(); */ Graf graf = new Graf(); while(true){ Console.WriteLine("Stworz graf podajac mu krawedzie - 1\nDodaj krawedz do grafu - 2\nDodaj wierzcholek do grafu - 3\n" + "Usun krawedz z grafu - 4\nUsun wierzcholek z grafu - 5\nWyznacz stopien wierzcholka - 6\nPodaj stopien minimalny - 7\n" + "Podaj stopien maksymalny - 8\nPodaj ile wierzcholkow jest stopnia parzystego - 9\nPodaj ile wierzcholkow jest stopnia nieparzystego - 10\n" + "Wypisz ciag stopni - 11\nNarysuj graf na konsoli - 12\nCzy jest cykl C3? - 13\nZadanie 2.1 - 14"); string read = Console.ReadLine(); int conv = Convert.ToInt32(read); switch (conv) { case 1: Console.WriteLine("Podaj ile wierzcholkow ma miec graf: "); string read1 = Console.ReadLine(); int conv1 = Convert.ToInt32(read1); Console.WriteLine("Podaj krawedzie oddzielone znakiem ',' w formie '[nr wierzcholka] [nr wierzcholka]'"); read1 = Console.ReadLine(); string[] split1 = read1.Split(','); List<Tuple<int, int>> krawedzie1 = new List<Tuple<int, int>>(); foreach (string str in split1) { string[] splitTemp = str.Split(' '); krawedzie1.Add(new Tuple<int, int>(Convert.ToInt32(splitTemp[0]), Convert.ToInt32(splitTemp[1]))); } graf = new Graf(conv1, krawedzie1); Console.WriteLine(graf); break; case 2: Console.WriteLine("Podaj krawedz do przeczytania w formie '[nr wierzcholka] [nr wierzcholka]'"); string read2 = Console.ReadLine(); string[] split2 = read2.Split(' '); graf.Dodaj(new Tuple<int,int>(Convert.ToInt32(split2[0]), Convert.ToInt32(split2[1]))); Console.WriteLine(graf); break; case 3: Console.WriteLine("Podaj ile wierzcholkow chcesz dodac: "); string read3 = Console.ReadLine(); int conv3 = Convert.ToInt32(read3); graf.Dodaj(conv3); Console.WriteLine(graf); break; case 4: Console.WriteLine("Podaj ktora krawedz chcesz usunac: "); string read4 = Console.ReadLine(); string[] split4 = read4.Split(' '); graf.Usun(new Tuple<int, int>(Convert.ToInt32(split4[0]), Convert.ToInt32(split4[1]))); Console.WriteLine(graf); break; case 5: Console.WriteLine("Podaj ktory wierzcholek chcesz usunac: "); string read5 = Console.ReadLine(); int conv5 = Convert.ToInt32(read5); graf.Usun(conv5); Console.WriteLine(graf); break; case 6: Console.WriteLine("Podaj stopien ktorego wierzcholka wyswietlic: "); string read6 = Console.ReadLine(); int conv6 = Convert.ToInt32(read6); Console.WriteLine(graf.Stopien(conv6)); break; case 7: Console.WriteLine(graf.StopienMinimalny()); break; case 8: Console.WriteLine(graf.StopienMaksymalny()); break; case 9: Console.WriteLine(graf.IleStopniParzystych()); break; case 10: Console.WriteLine(graf.IleStopniNieparzystych()); break; case 11: int[] ciag = graf.CiagStopni(); StringBuilder sb = new StringBuilder(); foreach(int i in ciag){ sb.Append(i + " "); } Console.WriteLine(sb.ToString()); break; case 12: Console.WriteLine(graf); break; case 13: Console.WriteLine(graf.CzyJestCyklC3()); break; case 14: zadanie21 zadanie = new zadanie21(); zadanie.zwrocCykl(graf); break; } } }
public Floyd(Graf graf) { this.graf = graf; }