/// <summary> /// Generate a random graph and then use it to color it. /// /// This method investigate: How much color is needed on average for a graph with 1000 /// vertex, where p varies from 0.002 to 0.02 /// </summary> public static string RandomGraphColoringReport( int n = 1000, double p_start = 0.002, double p_end = 0.02, int N = 10, // Increment for edge density int samples = 100 ) { StringBuilder sb = new StringBuilder(); sb.AppendLine("["); for (double p = p_start, delta = (p_end - p_start) / N; p <= p_end; p += delta) { sb.AppendLine("{"); sb.AppendLine($"\"n\":{n},"); sb.AppendLine($"\"p\":{p},"); double avgColorUsed = Double.NaN; for (int I = 0; I < samples; I++) { var aRandomGraph = new ColoringGraph(KissMe.RandGraph(n, p)); avgColorUsed = Double.IsNaN(avgColorUsed) ? 0 : avgColorUsed; avgColorUsed += aRandomGraph.ColorUsed(); } avgColorUsed /= samples; sb.AppendLine($"\"AvgColor\": {avgColorUsed}"); sb.AppendLine($"}} {(p == p_end ? ' ' : ',')}"); } sb.AppendLine("]"); return(sb.ToString()); }
/// <summary> /// Function will print result for this experiment to the console. /// </summary> private static void PrintHW3ConnectedComponents() { int n = (int)1e4; double range = 1.5e-3; int N = 30; double delta = range / N; double[] p = new double[N]; for (int I = 0; I < N; I++) { p[I] = Math.Log(n) / n - range + (I + N / 2) * delta; } WriteLine("The graph has 10^4 vertex and it's randomly generated with different " + "edge density, here is the statistics regarding it's connected components: "); WriteLine("Vertex, Edge_Density, TotalComponents, Max_Component_Size" + ", Min_Component_size, Size_SD"); for (int I = 0; I < p.Length; I++) { //0, 2, 3, 4 var G = KissMe.RandGraph(n, p[I]); var g = new SimpleGraph(G); var res = g.ProduceStats(); WriteLine($"{n}, {p[I]}, {res[0]}, {res[2]}, {res[3]}, {res[4]}"); } }
/// <summary> /// The result will get printed out to the console and you should copy then and get the /// image elsewhere. /// /// The printed result will be formatted as json file for convenience. /// </summary> public static void HW4P4PrintVertexDegreeDistribution( int n = 1000, double p_start = 0.002, double p_end = 0.02, int N = 10) { double[] EdgeDensity = new double[N]; StringBuilder sb = new StringBuilder(); sb.AppendLine("["); for (double P = p_start, delta = (p_end - p_start) / N; P <= p_end; P += delta) { var G = new ColoringGraph(KissMe.RandGraph(n, P)); //output.WriteLine($"Ded Distribution with p = {P}; n = {n};"); sb.AppendLine("{"); sb.AppendLine($"\t\"p\":{P},"); sb.AppendLine($"\t\"n\":{n},"); foreach (KeyValuePair <int, int> kvp in G.GetDegStats()) { sb.AppendLine($"\t\"{kvp.Key}\": {kvp.Value},"); } sb.Remove(sb.Length - 3, 2); sb.AppendLine("},"); } sb.Remove(sb.Length - 3, 2); sb.AppendLine("]"); WriteLine(sb.ToString()); }