private static void GenerateRandomWithCycle(int i, int j, out Graph g, out Graph h) { // random graph and a cycle g = GraphFactory.GenerateRandom(i, 0.4, 0); h = GraphFactory.GenerateCycle(j).Permute(1); Export("Random_And_Cycle", g, h); }
private static void GenerateCopyWithRedundant(int n, int extra, out Graph g, out Graph h) { g = GraphFactory.GenerateRandom(n, 0.5, 0); h = g.Permute(1); var random = new Random(0); for (int j = 0; j < extra; j += 1) { h.AddVertex(n + j, new HashSet <int>(Enumerable.Range(0, n + j).Where(k => random.NextDouble() < 0.5))); } Export("Copy_With_redundant", g, h); }
public void GraphIsomorphismDisconnected(int n, int repetitions, double density, int generatingSeed, int permutingSeed) { for (int i = 6; i < n; i += 1) { var max = repetitions; if (i == 2) { max = 1; } for (int j = 132; j < max; j += 1) { var random = new Random(j); // randomize a graph of given n and density var g = GraphFactory.GenerateRandom(i, density, generatingSeed + j * j); var h = g.Permute(permutingSeed - j); for (int removed = 0; removed < i; removed += 1) { // run the algorithm SubgraphIsomorphismExactAlgorithm.ParallelSubgraphIsomorphismExtractor.ExtractOptimalSubgraph( g, h, (vertices, edges) => vertices, out var score, out var subgraphEdges, out var gToH, out var hToG, true, false ); Assert.NotEmpty(gToH); Assert.NotEmpty(hToG); // verify the solution Assert.Equal(g.Vertices.Count, gToH.Count); Assert.Equal(g.Vertices.Count, hToG.Count); Assert.Equal(g.EdgeCount, subgraphEdges); AreTransitionsCorrect(gToH, hToG); HasSubgraphCorrectIsomorphism(g, h, gToH, hToG); g.RemoveVertex(g.Vertices.Skip(random.Next(g.Vertices.Count)).First()); } } } }
public void ApproximatingAlgorithmIsNotBetterThanActual(int n, int generatingSeed) { for (int i = 1; i < n; i += 1) { for (int j = 1; j < i; j += 1) { for (double density = 0.1; density < 1d; density += 0.1) { // randomize a graph of given n and density var g = GraphFactory.GenerateRandom(j, density, generatingSeed + j * j + i); var h = GraphFactory.GenerateRandom(i, density, generatingSeed * generatingSeed - j); foreach (var valuation in new Func <int, int, double>[] { (v, e) => v }) { // run the algorithm SubgraphIsomorphismExactAlgorithm.ParallelSubgraphIsomorphismExtractor.ExtractOptimalSubgraph( g, h, valuation, out var score, out var subgraphEdges, out var gToH, out var hToG, false, false ); SubgraphIsomorphismExactAlgorithm.SubgraphIsomorphismGrouppedApproximability.ApproximateOptimalSubgraph( g, h, valuation, out var approximateScore, out var _, out var __, out var ___, false, false, 1000, milisecondTimeLimit: 100d ); Assert.True(approximateScore <= score); } } } } }
public void GraphIsomorphismConnnected(int n, int repetitions, double density, int generatingSeed, int permutingSeed) { for (int i = 1; i < n; i += 1) { var max = repetitions; if (i == 2) { max = 1; } for (int j = 0; j < max; j += 1) { // randomize a graph of given n and density var g = GraphFactory.GenerateRandom(i, density, generatingSeed + j * j); var h = GraphFactory.GeneratePermuted(g, permutingSeed - j); // run the algorithm SubgraphIsomorphismExactAlgorithm.ParallelSubgraphIsomorphismExtractor.ExtractOptimalSubgraph( g, h, (vertices, edges) => vertices, out var score, out var subgraphEdges, out var gToH, out var hToG, false, false ); Assert.NotEmpty(gToH); Assert.NotEmpty(hToG); // verify the solution var maximumConnectedComponentSize = g.ExtractAllConnectedComponents().Max(cc => cc.Count); Assert.Equal(maximumConnectedComponentSize, gToH.Count); Assert.Equal(maximumConnectedComponentSize, hToG.Count); AreTransitionsCorrect(gToH, hToG); HasSubgraphCorrectIsomorphism(g, h, gToH, hToG); } } }
public void Approximating2GraphOfSizeAtMostDouble(int n, int repetitions, double density, int generatingSeed) { for (int i = 1; i < n; i += 1) { var max = repetitions; if (i == 2) { max = 1; } for (int j = 0; j < max; j += 1) { // randomize a graph of given n and density var g = GraphFactory.GenerateRandom(4 * i, density, generatingSeed + j * j); var h = GraphFactory.GenerateRandom(i, density, generatingSeed * generatingSeed - j); // run the algorithm SubgraphIsomorphismExactAlgorithm.ParallelSubgraphIsomorphismExtractor.ExtractOptimalSubgraph( g, h, (vertices, edges) => vertices, out var score, out var subgraphEdges, out var gToH, out var hToG, false, false, heuristicStepsAvailable: (g.EdgeCount + h.EdgeCount + g.Vertices.Count + h.Vertices.Count) * 20 ); Assert.NotEmpty(gToH); Assert.NotEmpty(hToG); AreTransitionsCorrect(gToH, hToG); HasSubgraphCorrectIsomorphism(g, h, gToH, hToG); } } }
public void Approximating1GraphOfSizeAtMostDouble(int n, int repetitions, double density, int generatingSeed) { for (int i = 1; i < n; i += 1) { var max = repetitions; if (i == 2) { max = 1; } for (int j = 0; j < max; j += 1) { // randomize a graph of given n and density var g = GraphFactory.GenerateRandom(4 * i, density, generatingSeed + j * j); var h = GraphFactory.GenerateRandom(i, density, generatingSeed * generatingSeed - j); // run the algorithm SubgraphIsomorphismExactAlgorithm.SubgraphIsomorphismGrouppedApproximability.ApproximateOptimalSubgraph( g, h, (vertices, edges) => vertices, out var score, out var subgraphEdges, out var gToH, out var hToG, false, false, 1000 ); Assert.NotEmpty(gToH); Assert.NotEmpty(hToG); AreTransitionsCorrect(gToH, hToG); HasSubgraphCorrectIsomorphism(g, h, gToH, hToG); } } }
public void GraphOfQuadrupleSize(int n, int repetitions, double density, int generatingSeed) { for (int i = 1; i < n; i += 1) { var max = repetitions; if (i == 2) { max = 1; } for (int j = 0; j < max; j += 1) { // randomize a graph of given n and density var g = GraphFactory.GenerateRandom(4 * i, density, generatingSeed + j * j); var h = GraphFactory.GenerateRandom(i, density, generatingSeed * generatingSeed - j); // run the algorithm SubgraphIsomorphismExactAlgorithm.ParallelSubgraphIsomorphismExtractor.ExtractOptimalSubgraph( g, h, (vertices, edges) => edges, out var score, out var subgraphEdges, out var gToH, out var hToG, false, false ); Assert.NotEmpty(gToH); Assert.NotEmpty(hToG); Assert.Equal(score, subgraphEdges); AreTransitionsCorrect(gToH, hToG); HasSubgraphCorrectIsomorphism(g, h, gToH, hToG); } } }
private static TimeSpan BenchmarkIsomorphism(int algorithm, int n, double density, int seed, out int subgraphVertices, out int subgraphEdges, out double score, bool printGraphs = false, double timeout = 0d) { var sw = new Stopwatch(); var initialSeed = new Random(seed).Next() ^ new Random(n).Next() ^ new Random((int)(density * int.MaxValue)).Next(); var g = GraphFactory.GenerateRandom(n, density, new Random(0).Next() ^ initialSeed).Permute(2); var h = GraphFactory.GenerateRandom(n, density, new Random(1).Next() ^ initialSeed).Permute(3); var gToH = new Dictionary <int, int>(); var hToG = new Dictionary <int, int>(); Func <int, int, double> valuation = (v, e) => e + v; var disconnected = false; // run the algorithm if (algorithm == 0) { sw.Start(); SubgraphIsomorphismExactAlgorithm.ParallelSubgraphIsomorphismExtractor.ExtractOptimalSubgraph( g, h, valuation, out score, out subgraphEdges, out gToH, out hToG, disconnected, false ); sw.Stop(); subgraphVertices = gToH.Keys.Count; } else if (algorithm == 1) { sw.Start(); SubgraphIsomorphismExactAlgorithm.SubgraphIsomorphismGrouppedApproximability.ApproximateOptimalSubgraph( g, h, valuation, out score, out subgraphEdges, out gToH, out hToG, disconnected, false, 100000 , timeout ); sw.Stop(); subgraphVertices = gToH.Keys.Count; } else if (algorithm == 2) { sw.Start(); SubgraphIsomorphismExactAlgorithm.ParallelSubgraphIsomorphismExtractor.ExtractOptimalSubgraph( g, h, valuation, out score, out subgraphEdges, out gToH, out hToG, disconnected, false, Math.Min(g.Vertices.Count, h.Vertices.Count) * 100, 0 ); sw.Stop(); subgraphVertices = gToH.Keys.Count; } else if (algorithm == 3) { sw.Start(); SubgraphIsomorphismExactAlgorithm.ParallelSubgraphIsomorphismExtractor.ExtractOptimalSubgraph( g, h, valuation, out score, out subgraphEdges, out gToH, out hToG, disconnected, false, approximationRatio: 0.9d ); sw.Stop(); subgraphVertices = gToH.Keys.Count; } else { throw new Exception("Wrong algorithm"); } if (printGraphs) { var light = algorithm == 0 ? ConsoleColor.Green : ConsoleColor.Cyan; var dark = algorithm == 0 ? ConsoleColor.DarkGreen : ConsoleColor.DarkCyan; Console.WriteLine("Graph G:"); g.PrintSubgraph(gToH.Keys.ToArray(), gToH, dark, light); Console.WriteLine(); Console.WriteLine("Graph H:"); h.PrintSubgraph(gToH.Keys.Select(key => gToH[key]).ToArray(), hToG, dark, light); Console.WriteLine(); } return(sw.Elapsed); }
private static void GenerateRandom0908(int i, int j, out Graph g, out Graph h) { g = GraphFactory.GenerateRandom(i, 0.9, 0); h = GraphFactory.GenerateRandom(j, 0.8, 1); Export("Random_08_09", g, h); }
private static void GenerateRandom09Petersen(int i, out Graph g, out Graph h) { g = GraphFactory.GeneratePetersenGraph(); h = GraphFactory.GenerateRandom(i, 0.9, 1); Export("Random_Petersen_06", g, h); }