コード例 #1
0
        private void button4_Click(object sender, EventArgs e)
        {
            if (vertices.Count != 0)
            {
                bool[] arr = new bool[vertices.Count];
                arr = DepthFirst(arr, vertices[0].pos);
                int i = 0;
                while (i < arr.Length && arr[i])
                {
                    i++;
                }
                if (i == arr.Length && IsEvenVertices())
                {
                    List <Vertice> edges = new List <Vertice>();
                    Vertice        v1, v2;
                    int            V      = vertices[pos].pos;
                    int            n      = AmountEdges(vertices);
                    int            number = 0;
                    do
                    {
                        v1 = vertices[RetPos(V)];
                        edges.Add(new Vertice(v1, v1.Weight));

                        v1 = v1.next;

                        int p = AdjVertices(V, edges);
                        if (number != n - 1 && p > 1)
                        {
                            while (v1 != null && (ContainEdge(edges, V, v1.pos) || IsBridgeEdge(V, V, v1.pos, edges, new bool[vertices.Count()])))
                            {
                                v1 = v1.next;
                            }
                        }
                        else
                        {
                            while (v1 != null && ContainEdge(edges, V, v1.pos))
                            {
                                v1 = v1.next;
                            }
                        }


                        if (v1 != null)
                        {
                            V  = v1.pos;
                            v2 = edges[edges.Count - 1];
                            while (v2.next != null)
                            {
                                v2 = v2.next;
                            }
                            v2.next = new Vertice(v1, v1.Weight);
                            number++;
                        }
                    } while (number != n);

                    OstTree ECycle = new OstTree(edges, (SolidBrush)brush, (SolidBrush)StringColor, sz, OstTree.Algorithm.EllerCycle);
                    ECycle.Show();
                    return;
                }
            }
            MessageBox.Show("Неможливо побудувати ейлерів цикл для заданого графу", default, MessageBoxButtons.OK, MessageBoxIcon.Error);
コード例 #2
0
        private void GraphTree_Click(object sender, EventArgs e)
        {
            if (vertices.Count != 0)
            {
                bool[] arr = new bool[vertices.Count];
                arr = DepthFirst(arr, vertices[0].pos);
                int i = 0;
                while (i < arr.Length && arr[i] == true)
                {
                    i++;
                }
                if (i == arr.Length)
                {
                    Array.Clear(arr, 0, arr.Length);
                    int            m1 = 1, m2 = RetPos(vertices[0].pos);
                    decimal        min;
                    int            j;
                    Vertice        v     = null;
                    List <int>     vt    = new List <int>();
                    List <Vertice> GTree = new List <Vertice>();
                    GTree.Add(new Vertice(vertices[0], vertices[0].Weight));
                    // m1 m2 - ребро яке потрібно буде додати до остовного дерева
                    while (vt.Count < vertices.Count)
                    {
                        arr[m2] = true;
                        vt.Add(m2);
                        min = decimal.MaxValue;
                        for (i = 0; i < vt.Count; i++)
                        {
                            v = vertices[vt[i]].next;

                            while (v != null)
                            {
                                if (!arr[RetPos(v.pos)] && min > v.Weight)
                                {
                                    m1  = vt[i];
                                    m2  = RetPos(v.pos);
                                    min = v.Weight;
                                }
                                v = v.next;
                            }
                        }
                        if (min != decimal.MaxValue)
                        {
                            j = 0;
                            while (j < GTree.Count && RetPos(GTree[j].pos) != m1)
                            {
                                j++;
                            }
                            v = GTree[j];
                            while (v.next != null)
                            {
                                v = v.next;
                            }
                            v.next = new Vertice(vertices[m2], min);
                            GTree.Add(new Vertice(vertices[m2], 0));
                        }
                    }
                    tree = new OstTree(GTree, (SolidBrush)brush, (SolidBrush)StringColor, sz, OstTree.Algorithm.OstTree);
                    tree.Show();
                    return;
                }
            }
            MessageBox.Show("Неможливо побудувати мінімальне остовне дерево для цього графу!", "Остовне дерево", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }