Exemplo n.º 1
0
 public Preview(Graph graph)
 {
     InitializeComponent();
     this.graph = graph;
     labelError.Visible = !LoadDrawer();
     if (labelError.Visible)
         this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
 }
Exemplo n.º 2
0
        /*public static Charge[] FindLocation(Graph graph, double G, double k, double l)
        {
            Charge[] charges = startLocations(graph.VertexCount);
            bool stop = false;
            double[][] oldcoords = new double[graph.VertexCount][];
            for (int i = 0; i < graph.VertexCount; i++)
                oldcoords[i] = new double[2];
            int counter = 0;
            double dSumm = 0;
            double olddSumm = 0;
            double totalCount = 0;
            while (!stop && totalCount++ <= 100000)
            {
                counter++;
                Parallel.For(0, graph.VertexCount, (i) =>
                //for (int i = 0; i < graph.VertexCount; i++)
                {
                    double Fx = 0;
                    double Fy = 0;
                    for (int j = 0; j < graph.VertexCount; j++)
                        if (i == j)
                            continue;
                        else
                        {
                            double distance_ij = Distance(charges[i], charges[j]);
                            if (distance_ij < 10000)
                            {
                                double ex = (charges[i].x - charges[j].x) / distance_ij;
                                double ey = (charges[i].y - charges[j].y) / distance_ij;
                                Fx += G * ex / Math.Pow(distance_ij, 2);
                                Fy += G * ey / Math.Pow(distance_ij, 2);
                                if (graph[i, j] == 1 || graph[j, i] == 1)
                                {
                                    Fx -= k * (distance_ij - l) * ex;
                                    Fy -= k * (distance_ij - l) * ey;
                                }
                                charges[i].dx = (int)(Fx * speed_coeff);
                                charges[i].dy = (int)(Fy * speed_coeff);

                                if (charges[i].dx > 700)
                                    charges[i].dx = 700;
                                if (charges[i].dx < -700)
                                    charges[i].dx = -700;
                                if (charges[i].dy > 700)
                                    charges[i].dy = 700;
                                if (charges[i].dy < -700)
                                    charges[i].dy = -700;
                            }
                            if (counter == 1)
                            {
                                oldcoords[i][0] = charges[i].x;
                                oldcoords[i][1] = charges[i].y;
                            }
                        }
                });
                for (int i = 0; i < graph.VertexCount; i++)
                {
                    charges[i].Move();
                    if (counter == 10)
                    {
                        dSumm += (Math.Abs(oldcoords[i][0] - charges[i].x) +
                                  Math.Abs(oldcoords[i][1] - charges[i].y));
                    }
                }
                if (counter == 100)
                {
                    if (Math.Abs(olddSumm - dSumm) < 0.1)
                        stop = true;
                    else
                    {
                        olddSumm = dSumm;
                        dSumm = 0;
                        counter = 0;
                    }
                }
            }
            double minD = double.MaxValue;
            double minX = charges[0].x;
            double maxX = charges[0].x;
            double minY = charges[0].y;
            double maxY = charges[0].y;
            for (int i = 0; i < graph.VertexCount; i++)
            {
                if (charges[i].x < minX)
                    minX = charges[i].x;
                if (charges[i].x > maxX)
                    maxX = charges[i].x;
                if (charges[i].y < minY)
                    minY = charges[i].y;
                if (charges[i].y > maxY)
                    maxY = charges[i].y;
                for (int j = i+1; j < graph.VertexCount; j++)
                {
                    double d = Distance(charges[i], charges[j]);
                    if (minD > d)
                        minD = d;
                }
            }
            System.Windows.Forms.MessageBox.Show(string.Format("Число шагов = {0}\nМинимальное расстояние = {1}\nX = [{2}; {3}]\n Y = [{4}; {5}]", totalCount, minD, minX, maxX, minY, maxY));
            return charges;
        }*/
        public static Charge[] FindLocation(Graph graph)
        {
            Charge[] charges = startLocations(graph.VertexCount);
            bool stop = false;
            double[][] oldcoords = new double[graph.VertexCount][];
            for (int i = 0; i < graph.VertexCount; i++)
                oldcoords[i] = new double[2];
            int counter = 0;
            double dSumm = 0;
            double olddSumm = 0;
            double totalCount = 0;
            while (!stop && totalCount <= 10000)
            {
                counter++;
                totalCount++;
                Parallel.For(0, graph.VertexCount, (i) =>
                //for (int i = 0; i < graph.VertexCount; i++)
                {
                    double Fx = 0;
                    double Fy = 0;
                    for (int j = 0; j < graph.VertexCount; j++)
                        if (i == j)
                            continue;
                        else
                        {
                            double distance_ij = Distance(charges[i], charges[j]);
                            if (distance_ij < 1000)
                            {
                                double ex = (charges[i].x - charges[j].x) / distance_ij;
                                double ey = (charges[i].y - charges[j].y) / distance_ij;
                                Fx += G * ex / Math.Pow(distance_ij, 2);
                                Fy += G * ey / Math.Pow(distance_ij, 2);
                                //if (distance_ij < 50)
                                //{
                                //    Fx *= 100;
                                //    Fy *= 100;
                                //}
                                if (graph[i, j] == 1 || graph[j, i] == 1)
                                {
                                    Fx -= Charge.k * (distance_ij - l) * ex;
                                    Fy -= Charge.k * (distance_ij - l) * ey;
                                }
                                charges[i].dx = (int)(Fx * speed_coeff);
                                charges[i].dy = (int)(Fy * speed_coeff);

                                if (charges[i].dx > 700)
                                    charges[i].dx = 700;
                                if (charges[i].dx < -700)
                                    charges[i].dx = -700;
                                if (charges[i].dy > 700)
                                    charges[i].dy = 700;
                                if (charges[i].dy < -700)
                                    charges[i].dy = -700;
                            }
                            if (counter == 1)
                            {
                                oldcoords[i][0] = charges[i].x;
                                oldcoords[i][1] = charges[i].y;
                            }
                        }
                });
                for(int i = 0; i < graph.VertexCount; i++)
                {
                    charges[i].Move();
                    if (counter == 10)
                    {
                        dSumm += (Math.Abs(oldcoords[i][0] - charges[i].x) +
                                  Math.Abs(oldcoords[i][1] - charges[i].y));
                    }
                }
                if (counter == 100)
                {
                    if (Math.Abs(olddSumm - dSumm) < 0.1)
                        stop = true;
                    else
                    {
                        olddSumm = dSumm;
                        dSumm = 0;
                        counter = 0;
                    }
                }
            }
            return charges;
        }
Exemplo n.º 3
0
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                this.SuspendLayout();
                //oldSelectedCol = oldSelectedRow = -1;
                //dgridMatrix.Rows.Clear();
                //dgridMatrix.Columns.Clear();
                tboxPath.Text = openFileDialog.FileName;
                StreamReader reader = File.OpenText(openFileDialog.FileName);
                try
                {
                    bool Directed;
                    int i, index;

                    String tmp = reader.ReadLine();
                    int VertexCount = Int32.Parse(tmp, CultureInfo.InvariantCulture.NumberFormat);
                    if (VertexCount <= 0)
                    {
                        reader.Close();
                        throw new FormatException();
                    }

                    tmp = reader.ReadLine();
                    if (Int32.Parse(tmp, CultureInfo.InvariantCulture.NumberFormat) == 1)
                        Directed = true;
                    else
                        if (Int32.Parse(tmp, CultureInfo.InvariantCulture.NumberFormat) == 0)
                            Directed = false;
                        else
                        {
                            reader.Close();
                            throw new FormatException();
                        }

                    tmp = reader.ReadLine();
                    String[] Names = new String[VertexCount];
                    i = 0;
                    do
                    {
                        index = tmp.IndexOf('	');
                        if (index == 0)
                        {
                            tmp = tmp.Substring(1);
                            continue;
                        }
                        if (index < 0)
                        {
                            if (i != VertexCount - 1)
                            {
                                reader.Close();
                                throw new FormatException();
                            }
                            else
                            {
                                Names[i] = tmp;
                                break;
                            }
                        }
                        Names[i] = tmp.Substring(0, index);
                        for (int j = 0; j < i; j++)
                            if (Names[j] == Names[i])
                            {
                                reader.Close();
                                throw new FormatException();
                            }
                        i++;
                        tmp = index + 1 < tmp.Length ? tmp.Substring(index + 1) : "";
                    } while (!String.IsNullOrEmpty(tmp));

                    //dgridMatrix.DefaultCellStyle.NullValue = null;
                    //dgridMatrix.DefaultCellStyle.Font =
                    //    new Font(dgridMatrix.DefaultCellStyle.Font, FontStyle.Regular);
                    //for (i = 0; i < VertexCount + 1; i++)
                    //{
                    //    dgridMatrix.Columns.Add("C" + i.ToString(CultureInfo.InvariantCulture.NumberFormat), "");
                    //    dgridMatrix.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
                    //    dgridMatrix.Columns[i].Width = 24;
                    //    dgridMatrix.Columns[i].ReadOnly = true;
                    //    dgridMatrix.Rows.Add();
                    //    dgridMatrix.Rows[0].Cells[i].Value =
                    //        dgridMatrix.Rows[i].Cells[0].Value = i != 0 ? Names[i - 1] : null;
                    //    dgridMatrix.Rows[i].Cells[0].Style.BackColor =
                    //        dgridMatrix.Rows[0].Cells[i].Style.BackColor = HeaderColor;
                    //    dgridMatrix.Rows[i].Height = 24;
                    //}
                    //dgridMatrix.Columns[0].Frozen = true;
                    //dgridMatrix.Rows[0].Frozen = true;
                    graph = new Graph(VertexCount, Directed);
                    for (i = 0; i < VertexCount; i++)
                    {
                        graph[i] = Names[i];
                        tmp = reader.ReadLine();
                        for (int j = 0; j < VertexCount; j++)
                        {
                            index = tmp.IndexOf('	');
                            if (index < 0)
                            {
                                if (j != VertexCount)
                                {
                                    reader.Close();
                                    throw new FormatException();
                                }
                                else
                                {
                                    if (tmp != "0" && tmp != "1")
                                    {
                                        reader.Close();
                                        throw new FormatException();
                                    }
                                    //dgridMatrix.Rows[i].Cells[j].Value = tmp;
                                    graph[i, j] = Int32.Parse(tmp);
                                    tmp = "";
                                    break;
                                }
                            }
                            if (index == 0)
                            {
                                tmp = tmp.Substring(1);
                                j--;
                                continue;
                            }
                            if (tmp.Substring(0, index) != "0" && tmp.Substring(0, index) != "1")
                            {
                                reader.Close();
                                throw new FormatException();
                            }
                            //dgridMatrix.Rows[i].Cells[j].Value = tmp.Substring(0, index);
                            graph[i, j] = Int32.Parse(tmp.Substring(0, index));
                            tmp = index + 1 < tmp.Length ? tmp.Substring(index + 1) : "";
                        }
                        if (!String.IsNullOrEmpty(tmp))
                        {
                            reader.Close();
                            throw new FormatException();
                        }
                    }
                    //if (!Directed)
                    //    for (i = 1; i < VertexCount + 1; i++)
                    //        for (int j = i; j < VertexCount + 1; j++)
                    //            if (dgridMatrix.Rows[i].Cells[j].Value.ToString() !=
                    //                dgridMatrix.Rows[j].Cells[i].Value.ToString())
                    //            {
                    //                reader.Close();
                    //                throw new FormatException();
                    //            }
                    reader.Close();
                }
                catch (IOException)
                {
                    MessageBox.Show(this, "При чтении файла возникла ошибка. Возможно, файл имеет неверный формат." +
                        "\nПожалуйста, выберите другой файл.",
                        "",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Warning);
                    reader.Close();
                }
                //btnOK.Enabled = true;
                this.ResumeLayout();
            }
        }
Exemplo n.º 4
0
        private static List<int[]> FindSCCs(Graph MyGraph)
        {
            List<int[]> SCCsList = new List<int[]>();
            AccessibilityMatrix = new int[MyGraph.VertexCount, MyGraph.VertexCount];

            for (int i = 0; i < MyGraph.VertexCount; ++i)
                buildAccessibility(i, i, MyGraph);
            S = new int[MyGraph.VertexCount, MyGraph.VertexCount];
            for (int i = 0; i < MyGraph.VertexCount; ++i)
                for (int j = 0; j < MyGraph.VertexCount; ++j)
                    S[i, j] = AccessibilityMatrix[i, j] * AccessibilityMatrix[j, i];

            bool[] added = new bool[MyGraph.VertexCount];
            for (int i = 0; i < added.Length; ++i)
                added[i] = false;

            for (int i = 0; i < MyGraph.VertexCount; ++i)
            {
                if (added[i])
                    continue;
                List<int> scc = new List<int>();
                added[i] = true;
                scc.Add(i);
                for (int j = 0; j < MyGraph.VertexCount; ++j)
                    if (!added[j] && S[i, j] == 1)
                    {
                        added[j] = true;
                        scc.Add(j);
                    }
                SCCsList.Add(scc.ToArray());
            }
            return SCCsList;
        }
Exemplo n.º 5
0
        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;
        }
Exemplo n.º 6
0
 private static void buildAccessibility(int startIndex, int currentIndex, Graph MyGraph)
 {
     for (int i = 0; i < MyGraph.VertexCount; ++i)
         if (i == startIndex)
             continue;
         else if (MyGraph[currentIndex, i] == 1 && AccessibilityMatrix[startIndex, i] == 0)
         {
             AccessibilityMatrix[startIndex, i] = 1;
             buildAccessibility(startIndex, i, MyGraph);
         }
 }
Exemplo n.º 7
0
 /// <summary>
 /// Создаёт копию данного графа.
 /// </summary>
 public object Clone()
 {
     Graph Copy = new Graph(this.VertexCount, this.Directed);
     Copy.M = this.Matrix.Clone() as AdjMatrix;
     Copy.Vs = new Vertex[this.VertexCount];
     for (int i = 0; i < this.VertexCount; i++)
         Copy.Vs[i] = new Vertex(this.Vs[i].Name);
     return Copy;
 }
Exemplo n.º 8
0
        /// <summary>
        /// Удаляет из графа вершину с индексом index;
        /// </summary>
        public IGraph RemoveVertex(int index)
        {
            if (index < 0 || index >= this.VertexCount)
                throw new ArgumentOutOfRangeException("index", "index должен быть в диапазоне [0, " +
                    (M.Dimension - 1).ToString(CultureInfo.InvariantCulture.NumberFormat) + "].");

            Graph newGraph = new Graph(this.VertexCount - 1, this.Directed);
            for (int i = 0; i < this.VertexCount; i++)
                if (i == index)
                    continue;
                else
                {
                    int j = this.Directed ? 0 : i;
                    newGraph.Vs[i < index ? i : i - 1] = (Vertex)this.Vs[i].Clone();
                    for (; j < this.VertexCount; j++)
                        if (j == index)
                            continue;
                        else if (j < index)
                            newGraph.M[i < index ? i : i - 1, j] = this.M[i, j];
                        else
                            newGraph.M[i < index ? i : i - 1, j - 1] = this.M[i, j];
                }
            return newGraph;
        }
Exemplo n.º 9
0
        /// <summary>
        /// Добавляет в граф вершину v.
        /// </summary>
        public IGraph AddVertex(Vertex v)
        {
            if (v == null)
                throw new ArgumentNullException("v");
            foreach (Vertex V in Vs)
                if (v.Name == V.Name)
                    throw new ArgumentException("Вершина с таким именем уже присутствует в графе.");

            Graph newGraph = new Graph(this.VertexCount + 1, this.Directed);
            for (int i = 0; i < this.VertexCount; i++)
            {
                int j = this.Directed ? 0 : i;
                for (; j < this.VertexCount; j++)
                    newGraph.M[i, j] = this.M[i, j];
                newGraph.Vs[i] = (Vertex)this.Vs[i].Clone();
            }
            newGraph.Vs[this.VertexCount] = v;
            return newGraph;
        }
Exemplo n.º 10
0
        public void InitModule(object receivedValue)
        {
            if ((MyGraph = receivedValue as Graph) == null)
                throw new ArgumentException();
            for (int i = 0; i < MyGraph.VertexCount; ++i)
                MyGraph[i, i] = 0;
            drawer.InitModule(receivedValue);
            drawer.Dock = DockStyle.Fill;
            this.splitContainer.Panel1.Controls.Add(drawer);
            label2.SendToBack();
            toolStrip.SendToBack();
            dgv_Matrix.Columns.Add("col_headers", "");
            for (int i = 0; i < MyGraph.VertexCount; ++i)
            {
                dgv_Matrix.Columns.Add("col" + MyGraph[i], MyGraph[i]);
            }
            dgv_Matrix.Rows.Add(MyGraph.VertexCount + 1);
            dgv_Matrix.Columns[0].Width = dgv_Matrix.Rows[0].Height;
            dgv_Matrix.Rows[0].Cells[0].Value = " ";
            for (int i = 1; i < dgv_Matrix.Columns.Count; ++i)
            {
                dgv_Matrix.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                dgv_Matrix.Columns[i].Width = dgv_Matrix.Rows[0].Height;
                dgv_Matrix.Rows[0].Cells[i].Value = dgv_Matrix.Rows[i].Cells[0].Value =
                    MyGraph[i - 1];
                dgv_Matrix.Rows[i].Cells[i].Value = "0";
                dgv_Matrix.Rows[i].Cells[i].ReadOnly = true;
            }
            dgv_Matrix.Rows[0].Frozen = true;
            dgv_Matrix.Columns[0].Frozen = true;
            dgv_Matrix.Columns[0].DefaultCellStyle.BackColor =
                dgv_Matrix.Rows[0].DefaultCellStyle.BackColor = Color.LightGray;

            deactivateStartWaveHandler = new EventHandler(drawer_InstrumentDisactivated);

            dgv_Matrix.Columns.Add(new DataGridViewButtonColumn());
            (dgv_Matrix.Columns[dgv_Matrix.Columns.Count - 1] as DataGridViewButtonColumn).DefaultCellStyle.NullValue = "OK";
            dgv_Matrix.Columns[dgv_Matrix.Columns.Count - 1].Width = dgv_Matrix.Rows[0].Height * 2;
            (dgv_Matrix.Rows[0].Cells[dgv_Matrix.Columns.Count - 1] as DataGridViewButtonCell).FlatStyle = FlatStyle.Flat;
            //(dgv_Matrix.Rows[0].Cells[dgv_Matrix.Columns.Count - 1] as DataGridViewButtonCell). = true;
            (dgv_Matrix.Rows[0].Cells[dgv_Matrix.Columns.Count - 1] as DataGridViewButtonCell).Value = "";

            dgv_Matrix.CellBeginEdit += new DataGridViewCellCancelEventHandler(dgv_Matrix_CellBeginEdit);
            dgv_Matrix.CellEndEdit += new DataGridViewCellEventHandler(dgv_Matrix_CellEndEdit);

            //---------------
            distMatrix = new int[MyGraph.VertexCount][];
            List<int> currentIndex = new List<int>();
            List<int> prevIndex = new List<int>();

            for (int i = 0; i < MyGraph.VertexCount; ++i)
            {
                distMatrix[i] = new int[MyGraph.VertexCount];
                for (int j = 0; j < MyGraph.VertexCount; ++j)
                    if (i != j)
                        distMatrix[i][j] = -1;

                prevIndex.Add(i);
                int index = 1;
                do
                {
                    for (int j = 0; j < prevIndex.Count; ++j)
                    {
                        for (int k = 0; k < MyGraph.VertexCount; ++k)
                            if (MyGraph[prevIndex[j], k] == 1 && distMatrix[i][k] == -1)
                            {
                                currentIndex.Add(k);
                                distMatrix[i][k] = index;
                            }
                    }
                    prevIndex = new List<int>(currentIndex);
                    currentIndex.Clear();
                    index++;
                } while (prevIndex.Count > 0);
            }
        }
Exemplo n.º 11
0
        private bool LoadDrawer(Graph MyGraph)
        {
            string execDir = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf('\\'));
            string modulePath = execDir + @"\modules\DrawGraph.dll";
            if (File.Exists(modulePath))
            {
                Assembly module;
                try
                {
                    module = module = Assembly.LoadFile(modulePath);
                }
                catch (FileLoadException)
                {
                    return false;
                }
                Type[] types = module.GetExportedTypes();
                if (types.Length != 1)
                    return false;
                if (/*!typeof(IGraphsLabModule).IsAssignableFrom(types[0])*/
                    (!typeof(IGraphLabsModule).IsAssignableFrom(types[0])) ||
                    (!typeof(UserControl).IsAssignableFrom(types[0])))
                    return false;

                drawer = (UserControl)Activator.CreateInstance(types[0]);
                SuspendLayout();
                (drawer as IGraphLabsModule).InitModule(MyGraph);
                //drawer.Dock = DockStyle.Fill;
                drawer.Location = panel1.Location;
                drawer.Size = panel1.Size;
                Controls.Add(drawer);
                ResumeLayout(false);
                return true;
            }
            else return false;
        }
Exemplo n.º 12
0
 private void btnRegenerate_Click(object sender, EventArgs e)
 {
     this.Controls.Remove(drawer);
     drawer.Dispose();
     NewGraph = Generator.Generate(this.Settings);
     LoadDrawer(NewGraph);
     CreateGraph_Resize(sender, e);
 }
Exemplo n.º 13
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;
 }