예제 #1
0
파일: Settings.cs 프로젝트: svtz/GraphLabs
 public Settings(ModuleSettings settings)
 {
     InitializeComponent();
     this.Resize += new EventHandler(Settings_Resize);
     if (settings != null)
     {
         nudVertexCount.Value = settings.VertexCount;
         chboxDirected.Checked = settings.Directed;
         switch (settings.GenerationMethod)
         {
             case GraphGenerationMethod.DensityMethod:
                 cbGenerationMethod.SelectedIndex = 2;
                 nudDensity.Value = (int)settings.parameters[0];
                 break;
             case GraphGenerationMethod.EmptyMethod:
                 cbGenerationMethod.SelectedIndex = 1;
                 break;
             case GraphGenerationMethod.FullMethod:
                 cbGenerationMethod.SelectedIndex = 0;
                 break;
             case GraphGenerationMethod.SccMethod:
                 cbGenerationMethod.SelectedIndex = 3;
                 nudSccCount.Value = (int)settings.parameters[0];
                 nudComponentDensity.Value = (int)settings.parameters[1];
                 break;
             default:
                 throw new NotImplementedException();
         }
     }
 }
예제 #2
0
파일: Settings.cs 프로젝트: svtz/GraphLabs
 private void btnOK_Click(object sender, EventArgs e)
 {
     if (rbtnNew.Checked)
     {
         ModuleSettings settings = new ModuleSettings();
         switch (cbGenerationMethod.SelectedIndex)
         {
             case 0: settings.GenerationMethod = GraphGenerationMethod.FullMethod;
                 break;
             case 1: settings.GenerationMethod = GraphGenerationMethod.EmptyMethod;
                 break;
             case 2: settings.GenerationMethod = GraphGenerationMethod.DensityMethod;
                 settings.parameters = new object[] { Convert.ToInt32(nudDensity.Value) };
                 break;
             case 3: settings.GenerationMethod = GraphGenerationMethod.SccMethod;
                 if (nudSccCount.Value > nudVertexCount.Value)
                 {
                     MessageBox.Show(this, "Число компонент сильной связности не может превосходить число вершин.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                     return;
                 }
                 settings.parameters = new object[] { Convert.ToInt32(nudSccCount.Value), Convert.ToInt32(nudComponentDensity.Value) };
                 break;
             default:
                 throw new NotImplementedException();
         }
         settings.VertexCount = Convert.ToInt32(nudVertexCount.Value);
         settings.Directed = chboxDirected.Checked;
         OnSettingsSet(new SettingsEventArgs(settings));
     }
     else
     {
         if (graph != null)
             OnSettingsSet(new SettingsEventArgs(graph));
         else
             MessageBox.Show(this, "Необходимо указать файл для загрузки", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
예제 #3
0
파일: Generator.cs 프로젝트: svtz/GraphLabs
        public static Graph Generate(ModuleSettings settings)
        {
            Graph newGraph;
            switch (settings.GenerationMethod)
            {
                case GraphGenerationMethod.DensityMethod:
                    int density = (int)settings.parameters[0];
                    Random rnd = new Random((int)DateTime.Now.Ticks);
                    int oneCount = (int)Math.Round((double)(settings.VertexCount *
                            settings.VertexCount * density / 100));
                    newGraph = new Graph(settings.VertexCount, settings.Directed);
                    if (!settings.Directed)
                        oneCount /= 2;

                    for (int i = 0; i < oneCount; i++)
                    {
                        int x, y;
                        do
                        {
                            x = rnd.Next(settings.VertexCount);
                            y = rnd.Next(settings.VertexCount);
                        } while (newGraph[x, y] != 0);
                        newGraph[x, y] = 1;
                    }
                    break;
                case GraphGenerationMethod.EmptyMethod:
                    newGraph = new Graph(settings.VertexCount, settings.Directed);
                    break;
                case GraphGenerationMethod.FullMethod:
                    newGraph = new Graph(settings.VertexCount, settings.Directed);
                    for (int i = 0; i < settings.VertexCount; ++i)
                        for (int j = 0; j < settings.VertexCount; ++j)
                            newGraph[i, j] = 1;
                    break;
                case GraphGenerationMethod.SccMethod:
                    newGraph = new Graph(settings.VertexCount, settings.Directed);
                    if (settings.Directed)
                    {
                        Random rand = new Random();
                        // SCCs[i] - массив индексов ксс, стрекли в которые существует из i-й
                        int SccCount = (int)settings.parameters[0];
                        int[][] SCCs = new int[SccCount][];
                        int LeftCount = SccCount - 1;
                        BuildTree(ref SCCs, 0, ref LeftCount, rand);
                        if (LeftCount > 0)
                        {
                            Array.Resize<int>(ref SCCs[0], SCCs[0].Length + 1);
                            SCCs[0][SCCs[0].Length - 1] = SccCount - LeftCount;
                            int current = SccCount - LeftCount;
                            --LeftCount;
                            while(LeftCount > 0)
                            {
                                SCCs[current] = new int[1];
                                SCCs[current][0] = SccCount - LeftCount;
                                current = SccCount - LeftCount;
                                --LeftCount;
                            }
                        }
                        for (int i = 0; i < SccCount; ++i)
                            if (SCCs[i] == null)
                                SCCs[i] = new int[0];
                        for (int i = 0; i < SccCount; ++i)
                        {
                            int addEdges = rand.Next(SccCount - i - SCCs[i].Length);
                            for (int j = 0; j < addEdges; j++)
                                //if (rand.NextDouble() > 0.5)
                                {
                                    Array.Resize<int>(ref SCCs[i], SCCs[i].Length + 1);
                                    SCCs[i][SCCs[i].Length - 1] = rand.Next(i + SCCs[i].Length, SccCount - 1);
                                }
                        }
                        //-----------------
                        List<int>[] SCCsVertices = new List<int>[SccCount];
                        bool[] used = new bool[settings.VertexCount];
                        int unusedCount = settings.VertexCount;
                        for (int i = 0; i < settings.VertexCount; ++i)
                            used[i] = false;

                        for (int i = 0; i < SccCount; ++i)
                        {
                            int count;
                            SCCsVertices[i] = new List<int>(0);
                            int max = unusedCount > SccCount - i ? Math.Min(unusedCount - SccCount + i + 1, (int)Math.Round((double)(settings.VertexCount / SccCount * 2))) : 1;
                            if (unusedCount > SccCount - i)
                                count = rand.Next(1, max + 1);
                            else
                                count = 1;
                            for (int j = 0; j < count; ++j)
                            {
                                int toAdd;
                                do
                                {
                                    toAdd = rand.Next(0, settings.VertexCount);
                                } while (used[toAdd]);
                                SCCsVertices[i].Add(toAdd);
                                used[toAdd] = true;
                                --unusedCount;
                            }
                        }

                        for (int i = 0; i < settings.VertexCount; ++i)
                            if (!used[i])
                                SCCsVertices[rand.Next(SCCsVertices.Length)].Add(i);

                        for (int i = 0; i < SccCount; ++i)
                        {
                            switch (SCCsVertices[i].Count)
                            {
                                case 1:
                                    break;
                                case 2:
                                    newGraph[SCCsVertices[i][0], SCCsVertices[i][1]] =
                                        newGraph[SCCsVertices[i][1], SCCsVertices[i][0]] = 1;
                                    break;
                                case 3:
                                    if (rand.NextDouble() > 0.5)
                                        newGraph[SCCsVertices[i][0], SCCsVertices[i][1]] =
                                            newGraph[SCCsVertices[i][1], SCCsVertices[i][2]] =
                                            newGraph[SCCsVertices[i][2], SCCsVertices[i][0]] = 1;
                                    else
                                        newGraph[SCCsVertices[i][0], SCCsVertices[i][2]] =
                                            newGraph[SCCsVertices[i][2], SCCsVertices[i][1]] =
                                            newGraph[SCCsVertices[i][1], SCCsVertices[i][0]] = 1;
                                    break;
                                default:
                                    ModuleSettings set = new ModuleSettings();
                                    set.VertexCount = SCCsVertices[i].Count;
                                    set.Directed = true;
                                    set.GenerationMethod = GraphGenerationMethod.DensityMethod;
                                    set.parameters = new object[] { settings.parameters[1] };
                                    Graph SCC = Generate(set);
                                    List<int[]> sccList = FindSCCs(SCC);
                                    if (rand.NextDouble() < 0.2)
                                    { // star
                                        int center = rand.Next(sccList.Count);
                                        for (int j = 0; j < sccList.Count; ++j)
                                            if (j == center)
                                                continue;
                                            else
                                            {
                                                int jOut = rand.Next(sccList[j].Length);
                                                int jIn = rand.Next(sccList[j].Length);
                                                int cOut = rand.Next(sccList[center].Length);
                                                int cIn = rand.Next(sccList[center].Length);
                                                SCC[sccList[j][jOut], sccList[center][cIn]] =
                                                    SCC[sccList[center][cOut], sccList[j][jIn]] = 1;
                                            }
                                    }
                                    else
                                    { //ring
                                        for (int j = 0; j < sccList.Count; ++j)
                                        {
                                            int next = j + 1 < sccList.Count ? j + 1 : 0;
                                            int jOut = rand.Next(sccList[j].Length);
                                            int nIn = rand.Next(sccList[next].Length);
                                            SCC[sccList[j][jOut], sccList[next][nIn]] = 1;
                                        }
                                    }

                                    for (int x = 0; x < SCC.VertexCount; ++x)
                                        for (int y = 0; y < SCC.VertexCount; ++y)
                                            if (SCC[x, y] == 1)
                                                newGraph[SCCsVertices[i][x], SCCsVertices[i][y]] = 1;
                                    break;
                            }
                        }
                        for (int i = 0; i < SCCs.Length; ++i)
                        {
                            for (int j = 0; j < SCCs[i].Length; ++j)
                            {
                                int iOut = rand.Next(SCCsVertices[i].Count);
                                int jIn = rand.Next(SCCsVertices[SCCs[i][j]].Count);
                                newGraph[SCCsVertices[i][iOut], SCCsVertices[SCCs[i][j]][jIn]] = 1;
                            }
                        }
                    }
                    break;
                default:
                    throw new NotImplementedException();
            }
            return newGraph;
        }
예제 #4
0
 public void InitModule(object receivedValue, object Settings)
 {
     InitModule(receivedValue);
     if (Settings.GetType() == typeof(Graph))
     {
         NewGraph = (Graph)Settings;
         btnRegenerate.Visible = false;
         LoadDrawer(NewGraph);
         //drawGraphModule.Refresh();
     }
     else if (Settings.GetType() == typeof(ModuleSettings))
     {
         this.Settings = (ModuleSettings)Settings;
         NewGraph = Generator.Generate(this.Settings);
         LoadDrawer(NewGraph);
     }
     else throw new ArgumentException("Неверный аргумент", "Settings");
     this.CreateGraph_Resize(null, null);
     if ((Settings as ModuleSettings).GenerationMethod != GraphGenerationMethod.EmptyMethod &&
         (Settings as ModuleSettings).GenerationMethod != GraphGenerationMethod.FullMethod)
         btnRegenerate.Visible = true;
 }