Esempio n. 1
0
        public void WriteFile(string FileName)
        {
            try
            {
                using (StreamWriter writetext = new StreamWriter(FileName))
                {
                    // ghi danh sach dinh
                    writetext.WriteLine(dsDinh.Count);
                    for (int index = 0; index < dsDinh.Count; index++)
                    {
                        Dinh dinh = dsDinh[index];
                        writetext.WriteLine(dinh.ID + " " + dinh.x + " " + dinh.y + " " + dinh.Ten);
                    }

                    // ghi danh sach dinh
                    writetext.WriteLine(dsCanh.Count);
                    for (int index = 0; index < dsCanh.Count; index++)
                    {
                        Canh canh = dsCanh[index];
                        writetext.WriteLine(canh.ID + " " + canh.IDXP + " " + canh.IDKT + " " + canh.x1 + " " + canh.y1 + " " + canh.x2 + " " + canh.y2 + " " + canh.TrongSo + " " + canh.LoaiCanh);
                    }

                    // thong bao
                    MessageBox.Show("Lưu đồ thị thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch
            {
                MessageBox.Show("Lưu đồ thị thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 2
0
        private static void SaveGraph(int mod)
        {
            DoThi z = dothi.Clone();

            for (int i = 1; i <= z.dsCanh.Count; i++)
            {
                Canh canh = z.dsCanh[i - 1];
                if (kt[i] == true)
                {
                    canh.mau = Color.Green;
                    continue;
                }

                if (i == mod)
                {
                    canh.mau = Color.OrangeRed;
                }
                else
                {
                    canh.mau = Color.LightGray;
                }
            }

            ans.Add(z);
        }
Esempio n. 3
0
        private static void KhoiTaoDanhSachCanh()
        {
            for (int index = 0; index < maxn; index++)
            {
                listCanh[index] = new List <p>();
            }

            for (int index = 0; index < dothi.dsCanh.Count; index++)
            {
                Canh canh = dothi.dsCanh[index];
                int  u    = IndexOf(canh.IDXP);
                int  v    = IndexOf(canh.IDKT);

                p xt = new p();
                xt.v = v;
                xt.w = canh.TrongSo;

                listCanh[u].Add(xt);
                if (canh.LoaiCanh == 0)
                {
                    // nếu là cạnh 2 chiều
                    p qp = new p();
                    qp.v = u;
                    qp.w = canh.TrongSo;
                    listCanh[v].Add(qp);
                }
            }
        }
Esempio n. 4
0
        private static void FordBellmanMain()
        {
            int m = dothi.dsCanh.Count;

            for (int index = 1; index <= dothi.dsDinh.Count; index++)
            {
                int cnt = dothi.dsCanh.Count;

                for (int i = 0; i < dothi.dsCanh.Count; i++)
                {
                    kt[i] = false;
                }
                SaveGraph(-1);

                for (int i = 0; i < dothi.dsCanh.Count; i++)
                {
                    Canh canh = dothi.dsCanh[i];

                    bool ok = false;

                    int u = IndexOf(canh.IDXP);
                    int v = IndexOf(canh.IDKT);


                    SaveGraph(i);


                    if (d[v] > d[u] + canh.TrongSo)
                    {
                        d[v]     = d[u] + canh.TrongSo;
                        trace[v] = u;
                        ok       = true;
                        kt[i]    = true;
                    }

                    if (canh.LoaiCanh == 0 && d[u] > d[v] + canh.TrongSo)
                    {
                        d[u]     = d[v] + canh.TrongSo;
                        trace[u] = v;
                        ok       = true;
                        kt[i]    = true;
                    }

                    if (ok)
                    {
                        cnt--;
                    }
                }

                if (cnt == m)
                {
                    return;
                }
            }
        }
Esempio n. 5
0
        private static void SaveGraph(int IDcanh)
        {
            DoThi z = dothi.Clone();

            if (IDcanh >= 0)
            {
                flag[IDcanh] = true;
            }

            for (int index = 0; index < dothi.dsCanh.Count; index++)
            {
                Canh canh = z.dsCanh[index];
                if (flag[index])
                {
                    canh.mau = Color.Green;
                }
                else
                {
                    canh.mau = Color.LightGray;
                }

                if (index == IDcanh)
                {
                    canh.mau = Color.OrangeRed;
                }
            }

            for (int i = 0; i < dothi.dsDinh.Count; i++)
            {
                Dinh dinh = z.dsDinh[i];
                if (d[i] == oo)
                {
                    dinh.ThongTinThem = "D[" + dinh.Ten + "] = +oo";
                }
                else
                {
                    dinh.ThongTinThem = "D[" + dinh.Ten + "] = " + d[i];
                }

                if (kt[i] == false)
                {
                    dinh.mau = Color.Green;
                }
                else
                {
                    dinh.mau = Color.LightGray;
                }
            }

            ans.Add(z);
        }
        public int CompareTo(object bz)
        {
            if (bz == null)
            {
                return(1);
            }
            Canh b = bz as Canh;

            if (TrongSo < b.TrongSo)
            {
                return(-1);
            }
            if (TrongSo == b.TrongSo)
            {
                return(0);
            }
            return(1);
        }
        private static void Find(int k)
        {
            int n = dothi.dsDinh.Count;

            if (k == n + 2 && LuaChon[n + 1] == S)
            {
                SaveGraph(LuaChon[k - 1]);
                GhiKetQua();
                return;
            }

            if (isFound)
            {
                return;
            }
            SaveGraph(LuaChon[k - 1]);
            int u = LuaChon[k - 1];

            foreach (p canh in listCanh[u])
            {
                if (isFound)
                {
                    return;
                }

                int v = canh.v;
                if (kt[v] == true)
                {
                    if (k != n + 1 && v == S)
                    {
                        continue;
                    }
                    kt[v] = false;
                    Canh cz = dothi.dsCanh.Where(p => p.ID == canh.ID).FirstOrDefault();
                    cz.mau     = Color.Red;
                    LuaChon[k] = v;
                    Find(k + 1);
                    cz.mau = Color.LightGray;
                    kt[v]  = true;
                }
            }
        }
Esempio n. 8
0
        private static void KruskalMain()
        {
            dothi.dsCanh.Sort();
            Init_Kruskal();
            SaveGraph(-1);

            for (int i = 1; i <= dothi.dsCanh.Count; i++)
            {
                Canh canh = dothi.dsCanh[i - 1];
                SaveGraph(i);
                int u = IndexOf(canh.IDXP);
                int v = IndexOf(canh.IDKT);
                int r = Findset(u);
                int s = Findset(v);
                if (r != s)
                {
                    Uni(r, s);
                    kt[i] = true;
                }
            }
        }
Esempio n. 9
0
        private static void SaveGraph(int mod)
        {
            DoThi z = dothi.Clone();

            for (int index = 0; index < z.dsDinh.Count; index++)
            {
                Dinh dinh = z.dsDinh[index];

                if (kt[index])
                {
                    dinh.mau = Color.LightGray;
                }
                else
                {
                    dinh.mau = Color.Green;
                }

                if (mod == index)
                {
                    dinh.mau = Color.OrangeRed;
                }
            }

            if (mod == -2 && kt[T] == false)
            {
                // in đường đi cuối cùng
                int u = T;
                while (u != S)
                {
                    int x = trace[u];

                    // tô màu đỏ cạnh nối từ x đến u
                    int IDu = dothi.dsDinh[u].ID;
                    int IDx = dothi.dsDinh[x].ID;

                    // tìm cạnh 2 chiều
                    Canh canh = z.dsCanh
                                .Where(p => p.LoaiCanh == 0)
                                .Where(p => (p.IDXP == IDx && p.IDKT == IDu) || (p.IDXP == IDu && p.IDKT == IDx))
                                .FirstOrDefault();
                    if (canh != null)
                    {
                        canh.mau = Color.Red;
                    }
                    else
                    {
                        // tìm canhk 1 chiều
                        canh = z.dsCanh
                               .Where(p => p.LoaiCanh == 1)
                               .Where(p => (p.IDXP == IDx && p.IDKT == IDu))
                               .FirstOrDefault();
                        if (canh != null)
                        {
                            canh.mau = Color.Red;
                        }
                    }

                    u = trace[u];
                }
            }

            ans.Add(z);
        }
Esempio n. 10
0
        public void ReadFile(string FileName)
        {
            try
            {
                DoThi temp = new DoThi();
                using (StreamReader readtext = new StreamReader(FileName))
                {
                    // Load list
                    int cntDinh;
                    cntDinh = Int32.Parse(readtext.ReadLine());
                    for (int index = 0; index < cntDinh; index++)
                    {
                        Dinh dinh = new Dinh();

                        string   line = readtext.ReadLine();
                        string[] k    = line.Split(' ');

                        dinh.ID  = Int32.Parse(k[0]);
                        dinh.x   = Int32.Parse(k[1]);
                        dinh.y   = Int32.Parse(k[2]);
                        dinh.Ten = k[3];
                        dinh.mau = Color.Green;

                        temp.dsDinh.Add(dinh);
                    }

                    // Load list canh
                    int cntCanh;
                    cntCanh = Int32.Parse(readtext.ReadLine());
                    for (int index = 0; index < cntCanh; index++)
                    {
                        Canh canh = new Canh();

                        string   line = readtext.ReadLine();
                        string[] k    = line.Split(' ');

                        canh.ID       = Int32.Parse(k[0]);
                        canh.IDXP     = Int32.Parse(k[1]);
                        canh.IDKT     = Int32.Parse(k[2]);
                        canh.x1       = Int32.Parse(k[3]);
                        canh.y1       = Int32.Parse(k[4]);
                        canh.x2       = Int32.Parse(k[5]);
                        canh.y2       = Int32.Parse(k[6]);
                        canh.TrongSo  = Int32.Parse(k[7]);
                        canh.LoaiCanh = Int32.Parse(k[8]);
                        canh.mau      = Color.Black;

                        temp.dsCanh.Add(canh);
                    }
                }

                MessageBox.Show("Load đồ thị thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                // lưu đồ thị theo đồ thị vừa đọc
                this.dsDinh = temp.dsDinh;
                this.dsCanh = temp.dsCanh;
            }
            catch
            {
                MessageBox.Show("Đọc đồ thị thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 11
0
 public void AddCanh(Canh tg)
 {
     dsCanh.Add(tg);
 }
Esempio n. 12
0
        private static void SaveGraph(int mod)
        {
            DoThi z = dothi.Clone();

            for (int index = 0; index < z.dsDinh.Count; index++)
            {
                Dinh dinh = z.dsDinh[index];

                if (d[index] != oo)
                {
                    dinh.ThongTinThem = "D[" + dinh.Ten + "] = " + d[index];
                }
                else
                {
                    dinh.ThongTinThem = "D[" + dinh.Ten + "] = +oo";
                }
            }

            for (int index = 0; index < z.dsCanh.Count; index++)
            {
                Canh canh = z.dsCanh[index];

                canh.mau = Color.LightGray;
                if (kt[index])
                {
                    canh.mau = Color.Green;
                }
                if (index == mod)
                {
                    canh.mau = Color.OrangeRed;
                }
            }

            if (mod == -2 && d[T] != oo)
            {
                // in đường đi cuối cùng
                int u = T;
                while (u != S)
                {
                    int x = trace[u];

                    // tô màu đỏ cạnh nối từ x đến u
                    int IDu = dothi.dsDinh[u].ID;
                    int IDx = dothi.dsDinh[x].ID;

                    // tìm cạnh 2 chiều
                    Canh canh = z.dsCanh
                                .Where(p => p.LoaiCanh == 0 && p.TrongSo == d[u] - d[x])
                                .Where(p => (p.IDXP == IDx && p.IDKT == IDu) || (p.IDXP == IDu && p.IDKT == IDx))
                                .FirstOrDefault();
                    if (canh != null)
                    {
                        canh.mau = Color.Red;
                    }
                    else
                    {
                        // tìm canhk 1 chiều
                        canh = z.dsCanh
                               .Where(p => p.LoaiCanh == 1 && p.TrongSo == d[u] - d[x])
                               .Where(p => (p.IDXP == IDx && p.IDKT == IDu))
                               .FirstOrDefault();
                        if (canh != null)
                        {
                            canh.mau = Color.Red;
                        }
                    }

                    u = trace[u];
                }
            }

            ans.Add(z);
        }