Beispiel #1
0
        public override void Run(int verIDStart, int verIDEnd = 0)
        {
            int        V, listTop = 0, listEnd = 0;
            List <int> listV = new List <int>();

            listV.Add(verIDStart);
            while (listTop <= listEnd)
            {
                V = listV[listTop];
                listTop++;

                int e = GetEdgeFromVertex(Parent[V], V);
                if (!IsVertexVisited(V))
                {
                    if (Parent[V] != -1 && e != -1)
                    {
                        // ve canh noi 2 dinh
                        Form1.getInstance().MainListEdge[e].Color = Color.Blue;
                        Form1.getInstance().DrawMoveV(Parent[V], V);
                    }
                    Form1.getInstance().MainListVertex[V].Color = Color.Blue;
                    listV   = GhepList(V, listV, PhatSinhDinh(V));
                    listEnd = listV.Count - 1;
                    Form1.getInstance().fInvalidate();
                }
            }
        }
Beispiel #2
0
 int[,] P; //ma tran phu luu vet
 public Floyd()
     : base()
 {
     G = Form1.getInstance().ConverMatrix();
     A = new int[Form1.getInstance().MainListVertex.Count, Form1.getInstance().MainListVertex.Count];
     P = new int[Form1.getInstance().MainListVertex.Count, Form1.getInstance().MainListVertex.Count];
 }
Beispiel #3
0
 public void InitParent()
 {
     for (int i = 0; i < Form1.getInstance().MainListVertex.Count; i++)
     {
         Parent[i] = -1;
     }
 }
Beispiel #4
0
        private List <Edge> CloneEdges()
        {
            var newList = new List <Edge>(Form1.getInstance().MainListEdge.Capacity);

            newList.AddRange(Form1.getInstance().MainListEdge);
            return(newList);
        }
Beispiel #5
0
 private void btnDone_MouseClick(object sender, MouseEventArgs e)
 {
     if (listVertex.Count != 0 || ListEdge.Count != 0)
     {
         fm1.MainListEdge   = ListEdge.ToList <Edge>();
         fm1.MainListVertex = listVertex.ToList <Vertex>();
         this.Close();
         Form1.getInstance().ChangValue(listVertex.Count - 1, listVertex.Count - 1);
         fm1.fInvalidate();
     }
 }
Beispiel #6
0
        public int Max()
        {
            int sum = 0;

            for (int i = 0; i < Form1.getInstance().MainListVertex.Count; i++)
            {
                for (int j = 0; j < Form1.getInstance().MainListVertex.Count; j++)
                {
                    if (G[i, j] > 0)
                    {
                        sum += G[i, j];
                    }
                }
            }
            return(sum);
        }
Beispiel #7
0
        public override void Run(int verIDStart, int verIDStartEnd = 0)
        {
            List <int> listV = PhatSinhDinh(verIDStart);

            Form1.getInstance().MainListVertex[verIDStart].Color = Color.Blue;

            Form1.getInstance().fInvalidate(); //reset lai cua so ve
            Thread.Sleep(Form1.timeSleep);     //

            Visited.Add(verIDStart);
            if (listV.Count != 0)
            {
                foreach (int i in listV)
                {
                    int e = GetEdgeFromVertex(verIDStart, i);

                    if (!IsVertexVisited(i))
                    {
                        Form1.getInstance().MainListEdge[e].Color = Color.Blue;
                        Form1.getInstance().DrawMoveV(verIDStart, i);
                        Thread.Sleep(Form1.timeSleep);

                        Run(i);
                        Form1.getInstance().DrawMoveV(i, verIDStart);
                        Thread.Sleep(Form1.timeSleep);
                    }
                    else
                    {
                        Form1.getInstance().MainListEdge[e].Color = Color.Red;
                        Form1.getInstance().fInvalidate();
                        Thread.Sleep(Form1.timeSleep * 20);
                    }
                    if (Form1.getInstance().MainListEdge[e].Color == Color.Red)
                    {
                        Form1.getInstance().MainListEdge[e].Color = Color.LightSlateGray;
                    }
                }
            }
        }
Beispiel #8
0
 public override void Run(int verIDStart, int verIDEnd = 0)
 {
     MST = new int[ListVertex.Count, ListVertex.Count];
     InitParent();
     Sort();
     for (int i = 0; i < ListEdge.Count; i++)
     {
         int v1 = FindSet(ListEdge[i].V1.Name);
         int v2 = FindSet(ListEdge[i].V2.Name);
         if (v1 != v2)
         {
             int x = ListEdge[i].V1.Name;
             int y = ListEdge[i].V2.Name;
             ListVertex[x].Color = Color.Blue;
             ListVertex[y].Color = Color.Blue;
             ListEdge[i].Color   = Color.Blue;
             Form1.getInstance().fInvalidate();
             Thread.Sleep(Form1.timeSleep * 20);
             MST[x, y] = ListEdge[i].G;
             Union(v1, v2);
         }
     }
 }
Beispiel #9
0
        public override void Run(int verIDStart, int verIDEnd = 0)
        {
            for (int i = 0; i < Form1.getInstance().MainListVertex.Count; i++)
            {
                if (Matrix[verIDStart, i] > 0)
                {
                    khoangcach[i] = Matrix[verIDStart, i];
                }
                else
                {
                    khoangcach[i] = int.MaxValue;
                }
                if (Matrix[verIDStart, i] > 0)
                {
                    Truoc[i] = verIDStart;
                }
                else
                {
                    Truoc[i] = -1;
                }
            }
            for (int i = 0; i < Form1.getInstance().MainListVertex.Count - 2; i++)
            {
                for (int u = 0; u < Form1.getInstance().MainListVertex.Count; u++)
                {
                    for (int v = 0; v < Form1.getInstance().MainListVertex.Count; v++)
                    {
                        if (verIDStart != u && u != v && verIDStart != v && Matrix[u, v] > 0)
                        {
                            if (khoangcach[v] > khoangcach[u] + Matrix[u, v])
                            {
                                khoangcach[v] = khoangcach[u] + Matrix[u, v];;
                                Truoc[v]      = u;
                            }
                        }
                    }
                }
            }

            //chuyen duong di ngan nhat vao stack
            Stack <int> st = new Stack <int>();

            st.Push(verIDEnd);
            while (true)
            {
                int v = st.Pop();
                if (v == verIDStart)
                {
                    st.Push(v);
                    break;
                }
                if (v == -1)
                {
                    break;
                }
                int vTrc = Truoc[v];

                st.Push(v);
                st.Push(vTrc);
            }
            int s = -1;
            int e = -1;

            //duyet do hoa cho duong di ngan nhat
            while (st.Count != 0)
            {
                e = st.Pop();
                if (Form1.getInstance().MainListVertex[e].Color != Color.Red)
                {
                    Form1.getInstance().MainListVertex[e].Color = Color.Blue;//to mau dinh
                }
                Form1.getInstance().fInvalidate();
                Thread.Sleep(Form1.timeSleep * 20);
                if (s != -1)
                {
                    int v = GetEdgeFromVertex(s, e);
                    Form1.getInstance().MainListEdge[v].Color = Color.Blue;
                    //ve canh
                    Form1.getInstance().DrawMoveV(s, e);
                    Thread.Sleep(Form1.timeSleep * 20);
                }
                s = e;
            }

            Form1.getInstance().fInvalidate();
            if (khoangcach[verIDEnd] != int.MaxValue)
            {
                MessageBox.Show("Khoang cach tu tu " + verIDStart + "->" + verIDEnd + " = " + khoangcach[verIDEnd].ToString());
            }
            else
            {
                MessageBox.Show("khong co duong di ngan nhat tu " + verIDStart + "->" + verIDEnd);
            }
        }
Beispiel #10
0
 public FB()
 {
     Matrix     = Form1.getInstance().ConverMatrix();
     Truoc      = new int[Form1.getInstance().MainListVertex.Count];
     khoangcach = new int[Form1.getInstance().MainListVertex.Count];
 }
Beispiel #11
0
 public Algorithms()
 {
     Visited = new List <int>();
     Matrix  = Form1.getInstance().ConverMatrix();
     Parent  = new int[ListVertex.Count];
 }
Beispiel #12
0
        public override void Run(int verIDStart, int verIDEnd = 0)
        {
            Init(verIDStart);
            int  i  = 0;
            bool kt = true;

            while (!IsVertexVisited(verIDEnd))
            {
                for (i = 0; i < countVertex; i++)
                {
                    if (!IsVertexVisited(i) && Len[i] < Int16.MaxValue)
                    {
                        break;
                    }
                }
                if (i == countVertex)
                {
                    kt = false;
                    break;
                }
                for (int j = 0; j < countVertex; j++)
                {
                    if (!IsVertexVisited(j) && Len[i] > Len[j])
                    {
                        i = j;
                    }
                }
                int temp;
                Visited.Add(i);
                for (int j = 0; j < countVertex; j++)
                {
                    if (!IsVertexVisited(j) && Len[i] + (temp = Matrix[i, j] != -1 ? Matrix[i, j] : Int16.MaxValue) < Len[j])
                    {
                        Len[j]    = Len[i] + Matrix[i, j];
                        Parent[j] = i;
                    }
                }
            }
            if (kt)
            {
                Stack <int> st = new Stack <int>();
                int         k  = verIDEnd;
                while (k != verIDStart)
                {
                    st.Push(k);
                    k = Parent[k];
                }
                st.Push(verIDStart);
                int s = -1;
                int e = -1;
                while (st.Count != 0)
                {
                    e = st.Pop();
                    if (ListVertex[e].Color != Color.Red)
                    {
                        ListVertex[e].Color = Color.Blue;//to mau dinh
                    }
                    Form1.getInstance().fInvalidate();
                    Thread.Sleep(Form1.timeSleep * 20);
                    if (s != -1)
                    {
                        //to canh
                        int v = GetEdgeFromVertex(s, e);
                        ListEdge[v].Color = Color.Blue;
                        //ve canh
                        Form1.getInstance().DrawMoveV(s, e);
                        Thread.Sleep(Form1.timeSleep * 20);
                    }
                    s = e;
                }
                Form1.getInstance().fInvalidate();
                MessageBox.Show("Khoang cach tu tu " + verIDStart + "->" + verIDEnd + " = " + Len[verIDEnd].ToString());
            }
            else
            {
                MessageBox.Show("Khong co duong di tu " + verIDStart + "->" + verIDEnd);
            }
        }
Beispiel #13
0
        public override void Run(int vStart, int vEnd)
        {
            for (int i = 0; i < Form1.getInstance().MainListVertex.Count; i++)
            {
                for (int j = 0; j < Form1.getInstance().MainListVertex.Count; j++)
                {
                    if (G[i, j] > 0)
                    {
                        A[i, j] = G[i, j];
                    }
                    else
                    {
                        A[i, j] = Max();
                    }
                    P[i, j] = -1;
                }
            }

            for (int k = 0; k < Form1.getInstance().MainListVertex.Count; k++)
            {
                for (int i = 0; i < Form1.getInstance().MainListVertex.Count; i++)
                {
                    for (int j = 0; j < Form1.getInstance().MainListVertex.Count; j++)
                    {
                        if (i != j && j != k && k != i && A[i, k] > 0 && A[k, j] > 0 && A[i, j] > A[i, k] + A[k, j])
                        {
                            A[i, j] = A[i, k] + A[k, j];
                            P[i, j] = k;
                        }
                    }
                }
            }
            String temp = "";


            Stack <int> st = new Stack <int>();

            st.Push(vEnd);
            st.Push(vStart);
            while (true)
            {
                int i = st.Pop();
                int j = st.Pop();
                int k = P[i, j];

                st.Push(j);
                if (k == -1)
                {
                    st.Push(i);
                    break;
                }
                st.Push(k);
                st.Push(i);
            }

            int s = -1;
            int e = -1;

            while (st.Count != 0)
            {
                e = st.Pop();
                if (Form1.getInstance().MainListVertex[e].Color != Color.Red)
                {
                    Form1.getInstance().MainListVertex[e].Color = Color.Blue;//to mau dinh
                }
                Form1.getInstance().fInvalidate();
                Thread.Sleep(Form1.timeSleep * 20);
                if (s != -1)
                {
                    //to canh
                    int v = GetEdgeFromVertex(s, e);
                    if (v != -1)
                    {
                        Form1.getInstance().MainListEdge[v].Color = Color.Blue;
                        //ve canh
                        Form1.getInstance().DrawMoveV(s, e);
                        Thread.Sleep(Form1.timeSleep * 20);
                    }
                }
                s = e;
            }
            Form1.getInstance().fInvalidate();
            if (A[vStart, vEnd] != Max())
            {
                MessageBox.Show("Khoang cach tu tu " + vStart + "->" + vEnd + " = " + A[vStart, vEnd].ToString());
            }
            else
            {
                MessageBox.Show("Khong co duong di tu " + vStart + "->" + vEnd);
            }
        }