private static void BipartiteMatchings() { Graph matching; int count; Graph g; for (int i = 0; i <= 5; ++i) { g = generator.BipariteGraph(typeof(AdjacencyMatrixGraph), random.Next(5) + 5, random.Next(5) + 5, 0.6); try { count = g.GetMaxMatching(out matching); if (IsMatchingFeasible(matching, g)) { Console.WriteLine("g{0} : {1}", i + 1, count); } else { Console.WriteLine("g{0} : {1}", i + 1, "skojarzenie nie jest dopuszczalne"); } } catch (ArgumentException e) { Console.WriteLine("g{0}: {1}", i + 1, e.Message); } } }
public static Graph bipartite() { int n = rnd.Next(3, 500); int m = rnd.Next(3, 500); double density = rnd.Next(3, 1000); density /= 1000; Graph bipartite = rgg.BipariteGraph(typeof(AdjacencyListsGraph <SimpleAdjacencyList>), n, m, density); return(changeVerticesNumeration(bipartite)); }
public static void Main() { Random rng = new Random(); RandomGraphGenerator rgg = new RandomGraphGenerator(); for (int i = 1; i <= testRnds; i++) { foreach (Type type in types) { TestGraph(rgg.UndirectedGraph(type, size, rng.NextDouble()), "Random undirected graph"); TestGraph(rgg.DirectedGraph(type, size, rng.NextDouble()), "Random directed graph"); TestGraph(rgg.UndirectedGraph(type, size, rng.NextDouble() * 2 / size, 0, 100), "Sparse undirected graph"); int k = rng.Next() % (size - 1) + 1; TestGraph(rgg.BipariteGraph(type, k, size - k, rng.NextDouble()), "Bipartite graph"); } } Console.WriteLine("Test summary:\n Reverse graphs:\t{0}/{3}, \n Bipartite graphs:\t{1}/{3},\n Kruskal's algorithm:\t{2}/{3}", reverseOk, bipartiteOk, kruskalOk, totalTestCount); }
static void Main(string[] args) { try { var rgg = new RandomGraphGenerator(123); IGraph h1, h2; IGraph g1 = new AdjacencyMatrixGraph(false, 5); IGraph g2 = new AdjacencyListsGraph(true, 4); Console.WriteLine("# licznik: {0}", Graph.Counter); g1.AddEdge(0, 1); g1.AddEdge(1, 2); g1.AddEdge(0, 2); g1.AddEdge(0, 4); g1.AddEdge(2, 4); g1.AddEdge(2, 3); Console.WriteLine("# licznik: {0}", Graph.Counter); Console.WriteLine("Graf g1 jest typu: {0} i jest skierowany: {1}", g1.GetType(), g1.Directed); h1 = g1.AddVertex(); Console.WriteLine("\nDodawanie\nGraf h1 ma {0} (powinno być 6) wierzchołków, a ostatni wierzchołek ma stopień {1} (powinno być 0)", h1.VerticesCount, h1.InDegree(h1.VerticesCount - 1)); Console.WriteLine("Graf h1 ma {0} (powinno być 6) krawędzi", h1.EdgesCount); Console.WriteLine("# licznik: {0}", Graph.Counter); h1 = g1.DeleteVertex(2); Console.WriteLine("\nUsuwanie\nGraf h1 ma {0} (powinno być 4) wierzchołków, a wierzchołki 1,2,3 mają odpowiednio stopienie {1} (powinno być 1),{2} (powinno być 0),{3} (powinno być 1)", h1.VerticesCount, h1.InDegree(1), h1.InDegree(2), h1.InDegree(3)); Console.WriteLine("Graf h1 ma {0} (powinno być 2) krawędzi", h1.EdgesCount); Console.WriteLine("# licznik: {0}", Graph.Counter); h1 = g1.Complement(); Console.WriteLine("\nDopełnienie g1 ma {0} (powinno być 4) krawędzi", h1.EdgesCount); Console.WriteLine("# licznik: {0}", Graph.Counter); h1 = g1.Closure(); Console.WriteLine("\nDomknięcie g1 ma {0} (powinno być 10) krawędzi", h1.EdgesCount); Console.WriteLine("# licznik: {0}", Graph.Counter); h1 = (g1.AddVertex()).Closure(); Console.WriteLine("Domknięcie g1 + K1 ma {0} (powinno być 10) krawędzi", h1.EdgesCount); Console.WriteLine("# licznik: {0}", Graph.Counter); Console.WriteLine("\nCzy h1 jest dwudzielny ?: {0} (powinno być False)", h1.IsBipartite()); Console.WriteLine("# licznik: {0}", Graph.Counter); h1 = rgg.BipariteGraph(typeof(AdjacencyMatrixGraph), 30, 50, 0.5); Console.WriteLine("\nCzy nowy h1 jest dwudzielny ?: {0} (powinno być True)", h1.IsBipartite()); Console.WriteLine("# licznik: {0}", Graph.Counter); Console.WriteLine("\n\n*************************\n\n"); g2.AddEdge(0, 1); g2.AddEdge(2, 1); g2.AddEdge(3, 2); Console.WriteLine("# licznik: {0}", Graph.Counter); Console.WriteLine("Graf g2 jest typu: {0} i jest skierowany: {1}", g2.GetType(), g2.Directed); h2 = g2.AddVertex(); Console.WriteLine("\nDodawanie\nGraf h2 ma {0} (powinno być 5) wierzchołków, a ostatni wierzchołek ma stopień wy: {1} (powinno być 0) i we: {2} (powinno być 0) ", h2.VerticesCount, h2.InDegree(h2.VerticesCount - 1), h2.OutDegree(h2.VerticesCount - 1)); Console.WriteLine("Graf h2 ma {0} (powinno być 3) krawędzi", h2.EdgesCount); Console.WriteLine("# licznik: {0}", Graph.Counter); h2 = g2.DeleteVertex(1); Console.WriteLine("\nUsuwanie\nGraf h2 ma {0} (powinno być 3) wierzchołków, a wierzchołek 1 ma stopień wy: {1} (powinno być 0) ", h2.VerticesCount, h2.OutDegree(1)); Console.WriteLine("Graf h2 ma {0} (powinno być 1) krawędzi", h2.EdgesCount); Console.WriteLine("# licznik: {0}", Graph.Counter); h2 = g2.Complement(); Console.WriteLine("\nDopełnienie g2 ma {0} (powinno być 9) krawędzi", h2.EdgesCount); Console.WriteLine("# licznik: {0}", Graph.Counter); h2 = g2.Closure(); Console.WriteLine("\nDomknięcie g2 + K1 ma {0} (powinno być 4) krawędzi", h2.EdgesCount); Console.WriteLine("# licznik: {0}", Graph.Counter); h2 = (g2.AddVertex()).Closure(); Console.WriteLine("Domknięcie g2 + K1 ma {0} (powinno być 4) krawędzi", h2.EdgesCount); Console.WriteLine("# licznik: {0}", Graph.Counter); Console.WriteLine("\nCzy h2 jest dwudzielny ?: {0} (powinno być False)", g2.IsBipartite()); Console.WriteLine("# licznik: {0}", Graph.Counter); } catch (Exception e) { Console.WriteLine(e.Message); } }
public static void Main() { int[] backtrackingColors; int[] greedyColors; int n, i, j, mb, mg; long counter0, counter1, counter2; string[] message1 = { "Zwykly maly graf:", "Maly dwudzielny:", "Mala klika:" }; int[] bestColorsNumbers1 = { 4, 2, 9 }; string[] message2 = { "Zwykly graf:", "Graf dwudzielny:", "Cykl parzysty:", "Klika:" }; int[] bestColorsNumbers2 = { 6, 2, 2, 200 }; string[] message3 = { "Zwykly duzy graf:", "Duzy dwudzielny:", "Duza klika:" }; int[] bestColorsNumbers3 = { 59, 2, 4000 }; Graph[] g1 = new Graph[message1.Length]; Graph[] g2 = new Graph[message2.Length]; Graph[] g3 = new Graph[message3.Length]; var rgg = new RandomGraphGenerator(); Console.WriteLine(); Console.WriteLine("Generowanie grafow"); Console.WriteLine(); rgg.SetSeed(101); g1[0] = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 8, 0.5); rgg.SetSeed(102); g1[1] = rgg.BipariteGraph(typeof(AdjacencyMatrixGraph), 5, 3, 0.75); n = 9; g1[2] = new AdjacencyMatrixGraph(false, n); for (i = 0; i < n; ++i) { for (j = i + 1; j < n; ++j) { g1[2].AddEdge(i, j); } } rgg.SetSeed(103); g2[0] = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 20, 0.5); rgg.SetSeed(104); g2[1] = rgg.BipariteGraph(typeof(AdjacencyMatrixGraph), 30, 20, 0.25); n = 50; g2[2] = new AdjacencyListsGraph <SimpleAdjacencyList>(false, n); for (i = 1; i < n; ++i) { g2[2].AddEdge(i - 1, i); } g2[2].AddEdge(n - 1, 0); rgg.SetSeed(105); g2[2] = rgg.Permute(g2[2]); n = 200; g2[3] = new AdjacencyMatrixGraph(false, n); for (i = 0; i < n; ++i) { for (j = i + 1; j < n; ++j) { g2[3].AddEdge(i, j); } } rgg.SetSeed(106); g3[0] = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 75, 0.99); rgg.SetSeed(107); g3[1] = rgg.BipariteGraph(typeof(AdjacencyMatrixGraph), 2000, 2000, 0.55); n = 5000; g3[2] = new AdjacencyMatrixGraph(false, n); for (i = 0; i < n; ++i) { for (j = i + 1; j < n; ++j) { g3[2].AddEdge(i, j); } } Console.WriteLine("Grafy za 1 pkt"); Console.WriteLine(); for (i = 0; i < g1.Length; ++i) { counter0 = (long)Graph.Counter; mb = g1[i].BacktrackingColor(out backtrackingColors); counter1 = (long)Graph.Counter; mg = g1[i].GreedyColor(out greedyColors); counter2 = (long)Graph.Counter; Console.WriteLine("{0,-17} liczba wierzcholkow {1,4}, optymalna liczba kolorow {2,4}", message1[i], g1[i].VerticesCount, bestColorsNumbers1[i]); Console.WriteLine(" Backtracking: liczba kolorow {0,4}, zlozonosc {1,8}", mb, counter1 - counter0); Console.WriteLine(" Greedy: liczba kolorow {0,4}, zlozonosc {1,8}", mg, counter2 - counter1); Console.WriteLine(); } Console.WriteLine("Grafy za 2 pkt"); Console.WriteLine(); for (i = 0; i < g2.Length; ++i) { counter0 = (long)Graph.Counter; mb = g2[i].BacktrackingColor(out backtrackingColors); counter1 = (long)Graph.Counter; mg = g2[i].GreedyColor(out greedyColors); counter2 = (long)Graph.Counter; Console.WriteLine("{0,-17} liczba wierzcholkow {1,4}, optymalna liczba kolorow {2,4}", message2[i], g2[i].VerticesCount, bestColorsNumbers2[i]); Console.WriteLine(" Backtracking: liczba kolorow {0,4}, zlozonosc {1,8}", mb, counter1 - counter0); Console.WriteLine(" Greedy: liczba kolorow {0,4}, zlozonosc {1,8}", mg, counter2 - counter1); Console.WriteLine(); } Console.WriteLine("Grafy za 3 pkt"); Console.WriteLine(); for (i = 0; i < g3.Length; ++i) { counter0 = (long)Graph.Counter; mb = g3[i].BacktrackingColor(out backtrackingColors); counter1 = (long)Graph.Counter; mg = g3[i].GreedyColor(out greedyColors); counter2 = (long)Graph.Counter; Console.WriteLine("{0,-17} liczba wierzcholkow {1,4}, optymalna liczba kolorow {2,4}", message3[i], g3[i].VerticesCount, bestColorsNumbers3[i]); Console.WriteLine(" Backtracking: liczba kolorow {0,4}, zlozonosc {1,8}", mb, counter1 - counter0); Console.WriteLine(" Greedy: liczba kolorow {0,4}, zlozonosc {1,8}", mg, counter2 - counter1); Console.WriteLine(); } Console.WriteLine("Koniec"); Console.WriteLine(); }
public static void Main(string[] args) { AdjacencyListsGraph <SimplyAdjacencyList> star = new AdjacencyListsGraph <SimplyAdjacencyList>(false, 10); for (int i = 0; i < 9; i++) { star.AddEdge(i, 9); } var evenCSmall = new AdjacencyListsGraph <SimplyAdjacencyList>(false, 10); for (int i = 0; i < evenCSmall.VerticesCount; i++) { evenCSmall.AddEdge(i, (i + 1) % evenCSmall.VerticesCount); } var oddCSmall = new AdjacencyListsGraph <SimplyAdjacencyList>(false, 11); for (int i = 0; i < oddCSmall.VerticesCount; i++) { oddCSmall.AddEdge(i, (i + 1) % oddCSmall.VerticesCount); } RandomGraphGenerator rgg = new RandomGraphGenerator(12345); var biparSmall = rgg.BipariteGraph(typeof(AdjacencyMatrixGraph), 9, 9, 1); var hypercube = new AdjacencyListsGraph <SimplyAdjacencyList>(false, 32); for (int i = 0; i < hypercube.VerticesCount; i++) { for (int j = 0; j < i; j++) { if (NumberOfBits(i ^ j) == 1) { hypercube.AddEdge(i, j); } } } var oddC = new AdjacencyListsGraph <SimplyAdjacencyList>(false, 31); for (int i = 0; i < oddC.VerticesCount; i++) { oddC.AddEdge(i, (i + 2) % oddC.VerticesCount); } AdjacencyMatrixGraph full = new AdjacencyMatrixGraph(false, 50); for (int i = 0; i < full.VerticesCount; i++) { for (int j = 0; j < i; j++) { full.AddEdge(i, j); } } Test[] tests = { new Test(star, 1, 1), new Test(evenCSmall, 5, 2), new Test(oddCSmall, 6, 11), new Test(biparSmall, 9, 2) }; Test[] testLarge = { new Test(hypercube, 16, 2), new Test(oddC, 16, 31), new Test(full, 49, 50) }; Console.Out.WriteLine("Ma³e testy"); for (int i = 0; i < tests.Length; i++) { PerformTest(i, tests[i]); } Console.Out.WriteLine("Du¿e testy"); for (int i = 0; i < testLarge.Length; i++) { PerformTest(i, testLarge[i]); } }
private static void TestBipartite() { var rgg = new RandomGraphGenerator(12345); Graph[] g = new Graph[BipartiteTestSize]; bool?[] res = { true, false, null, false, true }; Graph gg; bool r; int[] part; ulong cr; ulong[] cgg = { 77, 457, 1, 2500007, 1 }; g[0] = rgg.BipariteGraph(typeof(AdjacencyMatrixGraph), 4, 3, 0.4, -99, 99); g[1] = rgg.UndirectedGraph(typeof(AdjacencyListsGraph <HashTableAdjacencyList>), 100, 0.1, -999, 999); g[2] = rgg.DirectedGraph(typeof(AdjacencyMatrixGraph), 10, 0.5); g[3] = rgg.UndirectedCycle(typeof(AdjacencyListsGraph <SimplyAdjacencyList>), 50001, -99, 99); g[4] = new AdjacencyListsGraph <AVLAdjacencyList>(false, 50000); for (int i = 0; i < BipartiteTestSize; ++i) { Console.Write($" Test {i} - "); gg = g[i].Clone(); try { cr = Graph.Counter; r = g[i].Lab03IsBipartite(out part); cr = Graph.Counter - cr; if (res[i] == null) { Console.WriteLine("Failed : exception Lab03Exception expected"); continue; } if (!g[i].IsEqual(gg)) { Console.WriteLine("Failed : graph was destroyed"); continue; } if (r != res[i]) { Console.WriteLine("Failed : bad result"); continue; } if (r && !IsProperPartition(g[i], part)) { Console.WriteLine("Failed : invalid partition"); continue; } if (!r && part != null) { Console.WriteLine("Failed : part==null expected"); continue; } if (cr > 1.5 * cgg[i]) { Console.WriteLine($"Failed : poor efficiency {cr} (should be {cgg[i]})"); continue; } Console.WriteLine("Passed"); } catch (Lab03Exception e) { if (res[i] == null) { Console.WriteLine("Passed"); } else { Console.WriteLine($"Failed : {e.GetType()} : {e.Message}"); } } catch (System.Exception e) when(maskExceptions) { Console.WriteLine($"Failed : {e.GetType()} : {e.Message}"); } } }
public static void Main() { int[] backtrackingColors; int[] greedyColors; int n,i,j,mb,mg; long counter0, counter1, counter2; string[] message1 = { "Zwykly maly graf:", "Maly dwudzielny:", "Mala klika:" }; int[] bestColorsNumbers1 = { 4, 2, 9 }; string[] message2 = { "Zwykly graf:", "Graf dwudzielny:", "Cykl parzysty:", "Klika:" }; int[] bestColorsNumbers2 = { 6, 2, 2, 200 }; string[] message3 = { "Zwykly duzy graf:", "Duzy dwudzielny:", "Duza klika:" }; int[] bestColorsNumbers3 = { 59, 2, 4000 }; IGraph[] g1 = new IGraph[message1.Length]; IGraph[] g2 = new IGraph[message2.Length]; IGraph[] g3 = new IGraph[message3.Length]; var rgg = new RandomGraphGenerator(); //GraphExport ge = new GraphExport(true, "C:\\Users\\polgrabiat\\Desktop\\Graphviz2.26.3\\Graphviz2.26.3\\bin\\dot.exe"); Console.WriteLine(); Console.WriteLine("Generowanie grafow"); Console.WriteLine(); rgg.SetSeed(101); g1[0] = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph),8,0.5); rgg.SetSeed(102); g1[1] = rgg.BipariteGraph(typeof(AdjacencyMatrixGraph),5,3,0.75); n=9; g1[2] = new AdjacencyMatrixGraph(false,n); for ( i=0 ; i<n ; ++i ) for ( j=i+1 ; j<n ; ++ j ) g1[2].AddEdge(i,j); rgg.SetSeed(103); g2[0] = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 20, 0.5); rgg.SetSeed(104); g2[1] = rgg.BipariteGraph(typeof(AdjacencyMatrixGraph), 30, 20, 0.25); n = 50; g2[2] = new AdjacencyListsGraph(false, n); for (i = 1; i < n; ++i) g2[2].AddEdge(i - 1, i); g2[2].AddEdge(n - 1, 0); rgg.SetSeed(105); g2[2] = rgg.Permute(g2[2]); n = 200; g2[3] = new AdjacencyMatrixGraph(false, n); for (i = 0; i < n; ++i) { for (j = i + 1; j < n; ++j) g2[3].AddEdge(i, j); } rgg.SetSeed(106); g3[0] = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 75, 0.99); rgg.SetSeed(107); g3[1] = rgg.BipariteGraph(typeof(AdjacencyMatrixGraph), 2000, 2000, 0.55); n = 5000; g3[2] = new AdjacencyMatrixGraph(false, n); for (i = 0; i < n; ++i) { for (j = i + 1; j < n; ++j) g3[2].AddEdge(i, j); } //Console.WriteLine("{0}", g3[2].EdgesCount); Console.WriteLine("Grafy za 1 pkt"); Console.WriteLine(); for ( i=0 ; i<g1.Length ; ++i ) { // ge.Export(g1[i], "ala"); counter0=Graph.Counter; mb=g1[i].BacktrackingColor(out backtrackingColors); counter1=Graph.Counter; mg=g1[i].GreedyColor(out greedyColors); counter2=Graph.Counter; Console.WriteLine("{0,-17} liczba wierzcholkow {1,4}, optymalna liczba kolorow {2,4}", message1[i], g1[i].VerticesCount, bestColorsNumbers1[i]); Console.WriteLine(" Backtracking: liczba kolorow {0,4}, zlozonosc {1,8}", mb, counter1-counter0); Console.WriteLine(" Greedy: liczba kolorow {0,4}, zlozonosc {1,8}", mg, counter2-counter1); Console.WriteLine(); } Console.WriteLine("Grafy za 2 pkt"); Console.WriteLine(); for (i = 0; i < g2.Length; ++i) { counter0 = Graph.Counter; mb = g2[i].BacktrackingColor(out backtrackingColors); counter1 = Graph.Counter; mg = g2[i].GreedyColor(out greedyColors); counter2 = Graph.Counter; Console.WriteLine("{0,-17} liczba wierzcholkow {1,4}, optymalna liczba kolorow {2,4}", message2[i], g2[i].VerticesCount, bestColorsNumbers2[i]); Console.WriteLine(" Backtracking: liczba kolorow {0,4}, zlozonosc {1,8}", mb, counter1 - counter0); Console.WriteLine(" Greedy: liczba kolorow {0,4}, zlozonosc {1,8}", mg, counter2 - counter1); Console.WriteLine(); } Console.WriteLine("Grafy za 3 pkt"); Console.WriteLine(); for (i = 0; i < g3.Length; ++i) { counter0 = Graph.Counter; mb = g3[i].BacktrackingColor(out backtrackingColors); counter1 = Graph.Counter; mg = g3[i].GreedyColor(out greedyColors); counter2 = Graph.Counter; Console.WriteLine("{0,-17} liczba wierzcholkow {1,4}, optymalna liczba kolorow {2,4}", message3[i], g3[i].VerticesCount, bestColorsNumbers3[i]); Console.WriteLine(" Backtracking: liczba kolorow {0,4}, zlozonosc {1,8}", mb, counter1 - counter0); Console.WriteLine(" Greedy: liczba kolorow {0,4}, zlozonosc {1,8}", mg, counter2 - counter1); Console.WriteLine(); } Console.WriteLine("Koniec"); Console.WriteLine(); }