You are on page 1of 22

Ma Trận Kề => Danh Sách Cạnh và Danh sách Kề.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nhapvaomtk_inradanhsachke
{
class Program
{
static void Nhap(ref int[,]mt,int n,int m)
{

for (int i = 0; i < m; ++i)


for (int j = 0; j < n; ++j)
{
Console.Write("a[{0},{1}]= ", i, j); mt[i, j] =
int.Parse(Console.ReadLine());
}
}
static void InRaDanhSachKe(ref int[,] mt, int n, int m)
{
Console.WriteLine("Ma tran ke => Danh sach ke la.");
for (int i = 0; i < m; ++i)
{
int dem = 0;
for (int j = 0; j < n; ++j)
{
if (mt[i, j] == 1)
{
dem++;
if(dem<2)
Console.Write("{0} {1}", i + 1, j + 1);
else
Console.Write("{0}", j + 1);
}

}
Console.WriteLine();
}
}
static void InRaDanhSachcanh(ref int[,] mt, int n, int m)
{
Console.WriteLine("Ma tran ke => Danh sach canh la.");
for (int i = 0; i < m; ++i)
{
int dem = 0;
for (int j = 0; j < n; ++j)
{
if (mt[i, j] ==1&& mt[j, i]==1)
Console.WriteLine("{0} {1}", i + 1, j + 1);
mt[j, i] = 0;
}
}

}
static void Hien(ref int[,] mt, int n, int m)
{
Console.WriteLine("Ma tran vua nhap la");
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
Console.Write("{0} ", mt[i, j]);
}
Console.WriteLine();
}
}
static void Main(string[] args)
{
Console.WriteLine("Bai chuyen doi Ma Tran Ke => Danh Sach Ke,DS
Canh(Ap dung Do thi vo huong)");
int n, m;
Console.Write("so hang ma tran= "); m =
int.Parse(Console.ReadLine());
Console.Write("so cot= "); n = int.Parse(Console.ReadLine());
int[,] a = new int[m, n];
Nhap(ref a, n, m);
Hien(ref a, n, m);
InRaDanhSachKe(ref a, n, m);
InRaDanhSachcanh(ref a, n, m);

Console.ReadKey();
}
}
}

DISTRA
using System;

using System.Collections.Generic;
using System.IO;
using System.Text;

namespace TOANROIRAC
{
public struct datadinh
{
public int nhan, dtruoc;
}
//-----------------------------
class Dijkstra
{
static int[,] a;
static int[] b;
static datadinh[,] dinh;
static int sodinh, dxp,dkt,min;
static void doctep(string duongdan)
{
int i = 0, j = 0; int[] tmp = new int[3];
StreamReader doc = new StreamReader(duongdan);
string line;
line = doc.ReadLine();
foreach (string s in line.Split(' '))
{
if (s.Length > 0)
{
tmp[i] = int.Parse(s);
i++;
}
}
sodinh = tmp[0]; dxp = tmp[1]-1; dkt = tmp[2]-1;
doc.ReadLine();
a = new int[sodinh, sodinh];
dinh = new datadinh[sodinh,sodinh];
b = new int[sodinh];
//--------------------------
while ((line = doc.ReadLine()) != null)
{
i = 0;
foreach (string s in line.Split(' '))
{
if (s.Length > 0)
{
if (i > 0) a[j, i - 1] = int.Parse(s);
i++;
}
}
j++;
}

}
//-------------------------------------------
static void saochep(ref datadinh dich, datadinh nguon)
{
dich.nhan = nguon.nhan;
dich.dtruoc = nguon.dtruoc;
}
//------------------------------------------------------
static void hien()
{
Console.Write(" ");
for (int i = 0; i < sodinh; i++)
Console.Write(" " + (i + 1));
for (int i = 0; i < sodinh; i++)
{
Console.WriteLine();
Console.Write((i + 1) + " ");
for (int j = 0; j < sodinh; j++)
Console.Write(a[i, j] + " ");
}
}
//-----------------------------------------------------------
static void timnhannn(int hang,ref int min)
{
datadinh tmp = new datadinh();
min=0;
tmp.nhan = 1000;
for (int i = 0; i < sodinh; i++)
if (b[i] == 0 && dinh[hang, i].nhan < tmp.nhan)
{
saochep(ref tmp, dinh[hang, i]);
min = i;
}
b[min] = 1;
}
//---------------------------------------------------------
static void khoitao()
{
for (int i = 0; i < sodinh; i++)
{
dinh[0, i].dtruoc = dxp;
dinh[0, i].nhan = 1000;
}
dinh[0, dxp].dtruoc = 1;
dinh[0, dxp].nhan = 0;
b[dxp] = 1;
}
//----------------------------------------------------------
static void distra()
{
khoitao();
int i, j;
for (i = 1; i < sodinh; i++)
{
timnhannn(i-1,ref min);
for(j=0;j<sodinh;j++)
if (a[min, j] != 0 && b[j] == 0)
{
if (dinh[i-1, j].nhan > dinh[i - 1, min].nhan + a[min,
j])
{
dinh[i, j].nhan = dinh[i - 1, min].nhan + a[min,
j];
dinh[i, j].dtruoc = min;
}
else
{
saochep(ref dinh[i, j], dinh[i - 1, j]);
}
}
else
{
saochep(ref dinh[i, j], dinh[i - 1, j]);
}
}
}
//-------------------------------------------------------------
static string hienduongdi(int dkt)
{
string duongdi=(dkt+1).ToString()+"==>";
if (dinh[sodinh - 1, dkt].dtruoc == dxp)
return duongdi + (dxp+1).ToString();
else return duongdi + hienduongdi(dinh[sodinh - 1, dkt].dtruoc);
}
//--------------------------------------------------
public static void thuchien()
{
string duongdan;
do
{
Console.Write("nhap duong dan: ");
duongdan = Console.ReadLine();
} while (!(File.Exists(duongdan)));

doctep(duongdan);
hien();
Console.Write("duong di tim dc:\n");
distra();
Console.WriteLine(hienduongdi(dkt));
Console.WriteLine("chi phi:{0}", dinh[sodinh - 1, dkt].nhan);
for (int i = 1; i < sodinh; i++)
{
Console.WriteLine("duong di ngan nhat tu dinh {0},den {1} \n
la:{2} voi chi phi la:{3}", dxp + 1, i + 1, hienduongdi(i), dinh[sodinh - 1,
i].nhan);
}
}
//static void Main(string[] args)
//{
// string duongdan;
// do
// {
// Console.Write("nhap duong dan: ");
// duongdan = Console.ReadLine();
// } while (!(File.Exists(duongdan)));

// doctep(duongdan);
// hien();
// Console.Write("duong di tim dc:\n");
// distra();
// Console.WriteLine( hienduongdi(dkt));
// Console.WriteLine("chi phi:{0}", dinh[sodinh - 1, dkt].nhan);
// for (int i = 1; i < sodinh ; i++)
// {
// Console.WriteLine("duong di ngan nhat tu dinh {0},den {1} \n
la:{2} voi chi phi la:{3}", dxp+1, i+1, hienduongdi(i),dinh[sodinh-1,i].nhan);
// }
// Console.ReadKey();
//}
}
}
PRIM

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace TOANROIRAC
{
struct data
{
public int dau, cuoi, trongso;
}
class Prim
{
static int socanh, sodinh, dinhbd;//tao 3 bien luu so canh,sodinh,dinh
bat dau duyet cua do thi
static data[] caykhung;//khai bao mang chua cac canh cua cay khung
static data[] canh;//khai bao mang chua ds canh cua do thi
static int[] ttdinh;//khai bao mang danh dau xet cua cac dinh trong do
thi
//mac dinh chua xet la 0,xet roi la dc danh dau la 1
static void doctep(string duongdan)//doc tep
{
string[] a = new string[100];//tao mang tam
string line; int j = 0, i = 0;
StreamReader docdt = new StreamReader(duongdan);
line = docdt.ReadLine();//doc dong thu nhat
Console.WriteLine(line);//in dong thu nhat
foreach (string s in line.Split(' '))//duyet dong thu nhat
{
if (s.Length > 0)//neu xau con co do dai >0
{
a[i] = s;//ga vao mang tam a
i++;//tang chi so
}
}
sodinh = int.Parse(a[0].ToString());//lay gia tri cho bien chua so
dinh
socanh = int.Parse(a[1].ToString());//lay gia tri cho bien chua so
cahnh
dinhbd = int.Parse(a[2].ToString());//lay gia tri cho bien chua
dinh dc chon lam dinh xuat phat
//quy uoc la dong dau tien cua tep chua so dinh ,so acanh,va dinh
xuat phat
canh = new data[socanh];//cap phat cho mang chua ds cac canh cua
dt
caykhung = new data[sodinh];//cap phat mang chua ds cac canh cua
cay khung
ttdinh = new int[sodinh + 1];//cap phat mang danh dau trang thai
duoc xet cua cac dinh
i = 0;
while ((line = docdt.ReadLine()) != null)//chung nao dong dc doc
khac rong
{
j = 0;//dinh j dung de lay chi so cho phan tu cua mang a
foreach (string s in line.Split(' '))
{
if (s.Length > 0)
{
a[j] = s;
j++;
}
}
canh[i].trongso = int.Parse(a[2].ToString());
canh[i].cuoi = int.Parse(a[1].ToString());
canh[i].dau = int.Parse(a[0].ToString());
i++;//bien i dung de danh dau la dang lay gia tri cho canh nao
cua do thi
}

}
static void saochep(ref data dich, data nguon)//sao chep mot canh tu
mot canh khac
{
dich.cuoi = nguon.cuoi;
dich.dau = nguon.dau;
dich.trongso = nguon.trongso;
}
static void hien(int socanh, data[] canh)//hien do thi
{
for (int i = 0; i < socanh; i++)
{
Console.WriteLine("{0} {1} {2}", canh[i].dau, canh[i].cuoi,
canh[i].trongso);
}
}
static void hien2()//hien cay khung
{
for (int i = 0; i < sodinh - 1; i++)
{
Console.WriteLine("{0} {1} {2}", caykhung[i].dau,
caykhung[i].cuoi, caykhung[i].trongso);
}
}
static void caykhungnn(ref data[] caykhung)
{
int i = 0;
data min = new data();//khai bao mot canh tam chua
ttdinh[dinhbd] = 1;//danh dau dc xet doi voi dinh ban dau
while (i < sodinh - 1)//chung nao so canh dc dua vao cay khung nho
hon so dinh -1 thi lam
{
min.trongso = 100; //gan gia tri du lon cho canh tam
foreach (data mc in canh)//duyet rong toan bo ds canh cua dt
{

if ((ttdinh[mc.cuoi] == 0 && ttdinh[mc.dau] == 1) ||


(ttdinh[mc.dau] == 0 && ttdinh[mc.cuoi] == 1))
//neu mot dinh dc danh dau set va mot dinh chua dc danh
dau xet
{
if (mc.trongso < min.trongso)//neu tron g so cua canh
duoc duyet nho hon canh tam
{
saochep(ref min, mc);//sao chep gia tri cua canh
dc duyet vao canh tam
}
}
}//sau khi thoat khoi vong lap ta tim dc canh thoa man dk la
canh co trong so nho nhat va ke voi cac dinh da dc duyet roi
if (min.trongso != 100)//neu canh tam thay doi so voi ban dau
{
saochep(ref caykhung[i], min);//lay canh thu i cho cay
khung
i++;//tang chi so
ttdinh[min.cuoi] = 1;//danh dau xet cho dinh dau
ttdinh[min.dau] = 1; //danh dau xet xho dinh cuoi
}
}
}
//--------------------------------------------------------------------
--
public static void thuchien()
{
string duongdan;
do
{
Console.WriteLine("nhap duong dan toi tep can kt:");
duongdan = Console.ReadLine();
} while (!(File.Exists(duongdan)));
doctep(duongdan);
hien(socanh, canh);
Console.WriteLine("caykhung tim dc");
caykhungnn(ref caykhung);
hien2();
}
//static void Main(string[] args)
//{
// string duongdan;
// Console.WriteLine("nhap duong dan toi tep can kt:");
// duongdan = Console.ReadLine();
// //doctep("d:\\danhsach.txt");
// if (File.Exists(duongdan))
// {
// doctep(duongdan);
// hien(socanh, canh);
// Console.WriteLine("caykhung tim dc");
// caykhungnn(ref caykhung);
// hien2();
// }
// else Console.WriteLine("khong ton tai");
// Console.ReadKey();
//}
}
}

KRUSKAL
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace TOANROIRAC
{
//--tao mot struct bao gom tat ca cac thong tin ve 1 canh:dinh dau,dinh
cuoi,trong so
// struct data
//{
// public int dau, cuoi, trongso;
//}
class Kruskal
{
static data[] canh;//khai bao mot danh sach chua cac canh cua do thi
static data[] cknn;//khai bao danh sach chua cay khung nho nhat
static int []b;//khai bao mang de chua cac phan tu lien thuoc
static int socanh,sodinh;//khai bao hai bien de bieu dien so canh,dinh
cua do thi
static void doctep(string duongdan)
{
int i=0,j=0;
string line;//chuoi trung gian de chua 1 dong bat ky cua tep dc
nhap vao
int[]a=new int[3];//khai bao mot mang tam
StreamReader doc = new StreamReader(duongdan);//khai bao pt doc dl
line = doc.ReadLine();//doc dong thu nhat
foreach (string s in line.Split(' '))//duyet tung chuoi nho trong
dong thu nhat dc ngan canh nhau boi dau canh(' ')
{
if (s.Length > 0)//neu chuoi do co chieu dai lon hon 0
{
a[i] = int.Parse(s); i++;//gan phan tu thu i cua mang a co
gia tri la no
}
}
sodinh=a[0];//lay gia tri cho so dinh(o day quy uoc la tren hang
dau tien
//cua tep nhap vao chua so dinh,so canh)
socanh = a[1];//lay gia tri cho so canh
canh=new data[socanh];//cap phat mang chua ds canh
cknn=new data[sodinh-1];//cap phat mang chua cay khung nho nhat
b = new int[sodinh+1];//cap phat mang chua pt lien thuoc
while ((line = doc.ReadLine()) != null)//lam cho toi chung nao ma
dong doc duoc la dong khac rong
{
i=0;//bien i de danh dau la dang lay phan tu nao cho mang tam
a
foreach (string s in line.Split(' '))
{
if (s.Length > 0) { a[i] = int.Parse(s); i++; }
}
canh[j].dau = a[0];
canh[j].cuoi = a[1];
canh[j].trongso = a[2];
j++;//bien j danh dau la dnag lay canh nao cua do thi
}
}
static void hien(data[] a)//hien toan bo mot danh sach bat ky
{
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine("{0}\t{1}\t{2}", a[i].dau, a[i].cuoi,
a[i].trongso);
}
}
//--------------------------------------------------------------------
---------
static void saochep(ref data dich ,data nguon)//pt cong cu de lay gia
tri cua mot canh tu 1 canh khac
{
dich.trongso = nguon.trongso;
dich.dau = nguon.dau;
dich.cuoi = nguon.cuoi;
}
static void sapxep(ref data[] canh)//phuong thuc sap xep theo kieu lua
chon
{
int i, j,nn;
for (i = 0; i < canh.Length; i++)//i chay tu dau den cuoi
{
nn = i;
for (j = i; j < canh.Length; j++)//j chay tu phan tu thu
(i)den cuoi
if (canh[j].trongso < canh[nn].trongso)
nn = j;
if (nn != i)
{
data tmp = new data();
saochep(ref tmp, canh[i]);
saochep(ref canh[i], canh[nn]);
saochep(ref canh[nn], tmp);
}
}
}
/*kt xem canh xet co tao chu trinh khong
mang b chua cac pt lien thuoc
neu sau moi lan xet neu canh do thoa man la hai dinh cua no khong
cung lam trong mot
cay con thi dua canh do vao ds cknn,va danh dau dinh cuoi va dinh dau
lien thuoc voi nhau
bang canh la gan toan bo cac pt trong mang b co gia tri bang gia tri
cua pt
b[canhduoc duavao.dinh cuoi]bang b[canh duoc dua vao.dinh dau]*/
static bool ktchutrinh(data a)//kiem tra chu trinh
{
return (b[a.cuoi] == b[a.dau]);//neu haidinh lien thuoc voi nhau
}
static void timcknn(ref data[] cknn)
{
for (int t = 0; t < b.Length; t++)//ban dau cac phan tu chua lien
thuoc voi nhau
b[t] = t; //nen gan gt cua b[i]bang gt cua
dinh do

saochep(ref cknn[0], canh[0]);//lay canh dau tien cho cknn


b[canh[0].cuoi] = b[canh[0].dau];//danh dau hai dinh cua canh thu
nhat thuoc vao cung mot thanh phan lien thuoc
int i = 1;//danh dau canh lay tiep theo cho cay khung la canh thu
hai cknn[1];
foreach (data s in canh)//duyet tat cac canh cua do thi
{
if (!(ktchutrinh(s)))//neu khong tao chu trinh
{
saochep(ref cknn[i], s);//lay canh tiep theo cho cknn
for (int k = 1; k < b.Length;k++)//cap nhat lai thong tin
ve cac thanh phan lien thuoc
{
if (b[k] == b[s.cuoi])
b[k] = b[s.dau];
}
i++;
}
}
}
public static void thuchien()
{
string duongdan;//nhap duong dan
do
{
Console.Write("nhap duong dan :");
duongdan = Console.ReadLine();
if (!(File.Exists(duongdan))) Console.WriteLine("nhap sai
duong dan nhap lai");
} while (!(File.Exists(duongdan)));
doctep(duongdan);//doc tep
hien(canh);//hien do thi
Console.WriteLine("ds canh cua dt sau khi sx");//sap sep canh
sapxep(ref canh);
hien(canh);
Console.WriteLine("cay khung nho nhat:");//tim cay khung nn
timcknn(ref cknn);
hien(cknn);
}
//static void Main(string[] args)
//{
// string duongdan;//nhap duong dan
// do
// {
// Console.Write("nhap duong dan :");
// duongdan = Console.ReadLine();
// if (!(File.Exists(duongdan))) Console.WriteLine("nhap sai
duong dan nhap lai");
// } while (!(File.Exists(duongdan)));
// doctep(duongdan);//doc tep
// hien(canh);//hien do thi
// Console.WriteLine("ds sua khi sx");//sap sep canh
// sapxep(ref canh);
// hien(canh);
// Console.WriteLine("cay khung nho nhat:");//tim cay khung nn
// timcknn(ref cknn);
// hien(cknn);
// Console.ReadKey();
//}
}
}

DUYỆT THEO CHIỀU RỘNG


using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace TOANROIRAC
{
class DUYET_CHIEURONG
{
static int[,] a;
static int[] b;
static queue q = new queue();
static int sodinh, dxp;
static void doctep(string duongdan)
{
int i = 0, j = 0; int[] tmp = new int[2];
StreamReader doc = new StreamReader(duongdan);
string line;
line = doc.ReadLine();
foreach (string s in line.Split(' '))
{
if (s.Length > 0)
{
tmp[i] = int.Parse(s);
i++;
}
}
sodinh = tmp[0]; dxp = tmp[1];
doc.ReadLine();
a = new int[sodinh, sodinh];
b = new int[sodinh + 1];
//--------------------------
while ((line = doc.ReadLine()) != null)
{
i = 0;
foreach (string s in line.Split(' '))
{
if (s.Length > 0)
{
if (i > 0) a[j, i - 1] = int.Parse(s);
i++;
}
}
j++;
}

}
//------------------------------------------------------
static void hien()
{
Console.Write(" ");
for (int i = 0; i < sodinh; i++)
Console.Write(" " + (i + 1));
for (int i = 0; i < sodinh; i++)
{
Console.WriteLine();
Console.Write((i + 1) + " ");
for (int j = 0; j < sodinh; j++)
Console.Write(a[i, j] + " ");
}
}
//------------------------------------------------------------------
static void duyet(ref queue q)
{
q.dayvao(dxp - 1);
b[dxp - 1] = 1;
while (!(q.emtry()))
{
int tmp = q.layra();
for (int i = 0; i < sodinh; i++)
if (a[tmp, i] != 0 && b[i] == 0)
{
b[i] = 1;
q.dayvao(i);
}
}
}
//--------------------------------------------------------------------
---
static void hiendinhlt()
{
string s1 = "", s2 = "";
for (int i = 1; i < sodinh + 1; i++)
if (b[i - 1] == 1) s1 = s1 + i.ToString() + " ";
else s2 = s2 + i.ToString() + " ";
Console.WriteLine();
Console.WriteLine("nhung dinh lien thong:" + s1);
Console.WriteLine("nhung dinh khong lien thong:" + s2);
}
//-------------------------------------------------------------------
static void kttinhlt()
{
bool ok = true;
for (int i = 1; i < sodinh + 1; i++)
if (b[i - 1] == 0)
ok = false;
if (ok == false) Console.WriteLine("vay do thi khong lien thong");
else Console.WriteLine("vay do thi lien thong");
}
//---------------------------------------------------------------
public static void thuchien()
{
string duongdan;
do
{
Console.Write("nhap duong dan: ");
duongdan = Console.ReadLine();
} while (!(File.Exists(duongdan)));

doctep(duongdan);
hien();
//Console.WriteLine(dxp + " " + sodinh);
duyet(ref q);
hiendinhlt();
kttinhlt();
}
//static void Main(string[] args)
//{
// string duongdan;
// do
// {
// Console.Write("nhap duong dan: ");
// duongdan = Console.ReadLine();
// } while (!(File.Exists(duongdan)));

// doctep(duongdan);
// hien();
// //Console.WriteLine(dxp + " " + sodinh);
// duyet(ref q);
// hiendinhlt();
// kttinhlt();
// Console.ReadKey();
//}
}
}

DUYỆT THEO CHIỀU SÂU

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace TOANROIRAC
{
class DUYET_CHIEUSAU
{
static int[,] a;
static int[] b;
static int sodinh, dxp;
static void doctep(string duongdan)
{
int[] tmp = new int[3];
StreamReader doc = new StreamReader(duongdan);
string line;
line = doc.ReadLine();
int i = 0, j = 0;
foreach (string s in line.Split(' '))
{
if (s.Length > 0)
{
tmp[i] = int.Parse(s);
i++;
}
}
sodinh = tmp[0];
dxp = tmp[1];
a = new int[sodinh + 1, sodinh + 1];
b = new int[sodinh + 1];

while ((line = doc.ReadLine()) != null)


{
Console.WriteLine(line);
i = 0;
foreach (string s in line.Split(' '))
{
if (s.Length > 0)
{
a[j, i] = int.Parse(s);
i++;
}
}
j++;
}
}
//--------------------------------------------------------------------
-
static void duyet(int dxp)
{
b[dxp] = 1;
for (int i = 1; i <= sodinh; i++)
if (b[i] == 0 && a[dxp, i] != 0)
{
duyet(i);
}
}
//----------------------------------------------------------
static void ktlt()
{
string lt = "", klt = "";
bool ok = true;
for (int i = 1; i < sodinh + 1; i++)
{
if (b[i] == 0)
{
klt = klt + (i).ToString() + ",";
ok = false;
}
else
{
lt = lt + (i).ToString() + ",";
}
}
if (ok)
{
Console.WriteLine("do thi lien thong!");
Console.WriteLine("cac thanh phan lt:{0}", lt);
}
else
{
Console.WriteLine("do thi khong lien thong!");
Console.WriteLine("cac thanh phan lt:{0}", lt);
Console.WriteLine("cac thanh phan khong lt:{0}", klt);
}
}
//-----------------------------------------------------------
public static void thuchien()
{
string duongdan;
do
{
Console.WriteLine("nhap duong dan:");
duongdan = Console.ReadLine();
} while (!(File.Exists(duongdan)));
doctep(duongdan);
duyet(dxp);
ktlt();
}
//-------------------------------------------------------------
//static void Main(string[] args)
//{
// string duongdan;
// Console.WriteLine("nhap duong dan:");
// duongdan = Console.ReadLine();
// doctep(duongdan);
// duyet(dxp);
// ktlt();
// Console.ReadKey();
//}
}
}
QUEUE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TOANROIRAC
{
class queue
{
const int max = 100;
int[] q = new int[max];
int f, r;
public queue()
{
f = r = -1;
}
public bool emtry()
{
return (f == -1 && r == -1);
}
public bool full()
{
return ((f == 0 && r == max - 1) || f == r + 1);
}
public void dayvao(int x)
{
if (full())
{
Console.WriteLine("tran khong the bo sung");
}
else
{
if (emtry()) f = 0;
if (r == max - 1)
r = 0;
else r = r + 1;
q[r] = x;
}

}
public int layra()
{
if (emtry())
{
Console.WriteLine("trong khong the lay ra"); return 0;
}
else
{
int tmp = q[f];
if (f == r) f = r = -1;
else
{
if (f == max - 1) f = 0;
else f = f + 1;
}
return tmp;
}

}
}
}

EULER
THEO DANH SÁCH KỀ

using System;
using System.Text;
using System.Collections.Generic;
using System.IO;

//Tran Thi Tinh

class Node {
public int info;
public Node Next;
}

class Euler {
Node[] dinh;
int socanh = 0;

public void Nhap() {


FileStream fs = new FileStream("D:/MTKE.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
int sodinh = int.Parse(sr.ReadLine());
dinh = new Node[sodinh];
for (int i = 0; i < sodinh; i++) {
dinh[i] = new Node();
dinh[i].info = i+1;
dinh[i].Next = null;
Node Ke = dinh[i];
String[] dinhke = sr.ReadLine().Split(' ');
socanh += dinhke.Length;
for (int j = 0; j < dinhke.Length; j++) {
Node P = new Node();
P.info = int.Parse(dinhke[j]);
P.Next = null;
Ke.Next = P;
Ke = Ke.Next;
}
}
socanh /= 2;
sr.Close();
fs.Close();
}

void RemoveKe(Node L, int remove) {


Node P = L.Next;
while (P.info!=remove) {
L = P;
P = P.Next;
}
L.Next = P.Next;
}

public void DoThiEuler() {


//Phuong phap Flor
Stack<int> STACK = new Stack<int>();
int dinhbatdau = 1;//dinh dau tien cua danh sach
STACK.Push(dinhbatdau);
int x, y;
while (STACK.Count > 0) {
x = STACK.Peek();
if (dinh[x-1].Next != null) {
//Y= Ke(x);
Node L = dinh[x-1];
L = L.Next;
y = L.info;
STACK.Push(y);
//Remove ke cua X la Y va remove ke cua Y la X
RemoveKe(dinh[x-1], y);
RemoveKe(dinh[y-1], x);
} else {
int dau, cuoi, CE;
dau = cuoi = STACK.Pop();
CE = 0;
while (STACK.Count > 0) {
cuoi = STACK.Pop();
CE++;
}
if (socanh == CE) {
if (dau == cuoi) {
Console.WriteLine("La do thi Euler");
} else {
Console.WriteLine("la do thi nua Euler");
}
} else {
Console.WriteLine("Day khong phai la do thi Euler");
}
}
}
}
}

static class Test {


static void Main() {
Euler dteuler = new Euler();
dteuler.Nhap();
dteuler.DoThiEuler();
Console.ReadKey();
}
}

THEO MẢNG KỀ

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
public static List<int>[] a;
public static int n;
public static List<int> CE;
public static Stack<int> stack;
static void Main(string[] args)
{

Console.WriteLine("Nhap so dinh cua do thi n=");


n = int.Parse(Console.ReadLine());
a = new List<int>[n];
CE = new List<int>();
//Nhap
for (int i = 0; i < n; i++)
{
Console.WriteLine("Danh sach dinh ke thu {0}\t",i+1);
a[i] = new List<int>();
Console.Write("So dinh ke voi dinh tren:");
int sodinh = int.Parse(Console.ReadLine());
for (int j = 0; j < sodinh; j++)
{
Console.WriteLine("Ke voi dinh thu {0}\t",j+1);
int tam = int.Parse(Console.ReadLine());
a[i].Add(tam);
}
}
//So canh
int sobac=0;
for (int i = 0; i < n; i++)
{
sobac += a[i].Count;
}
int socanh = sobac / 2;
//Stack

stack = new Stack<int>();


stack.Push(1);
while (stack.Count != 0)
{
int y = stack.First();
if (a[y - 1].Count != 0)
{
stack.Push(a[y - 1][0]);
//cap nhat lai ke cua 2 dinh
a[(a[y - 1][0] - 1)].Remove(y);
a[y - 1].Remove(a[y - 1][0]);
}
else
{
int tam = stack.Pop();
CE.Add(tam);
}
}
Console.WriteLine("In ra do thi");
if (CE.Count == socanh + 1)
{
Console.WriteLine("Chu trinh euler");
for (int i = 0; i < CE.Count; i++)
Console.WriteLine("{0}\t",CE[i]);
//Console.ReadKey();
}
Console.ReadKey();
}

}
}

HAMINTAL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Hamilton
{
public static List<int>[] a;
public static int n;
public static int[] X;
public static bool[] chuaxet;
public void HienHamilton(int[]x)
{
x = new int[n];
for (int i = 0; i <x.Length; i++)
{
Console.WriteLine(x[i]+"\t");
}
}
public void CTHamilton(int k)
{
foreach(int y in a[X[k]-1])
{
if ((y == 1) && (k + 1 == n))
{
Console.WriteLine("Do thi Hamilton la:");
HienHamilton(X);
Console.WriteLine(y);
}
else
{
if (chuaxet[y - 1])
{
X[k + 1] = y;
chuaxet[y - 1] = false;
CTHamilton(k + 1);
chuaxet[y - 1] = true;
}
}
}
}
public void Nhap()
{
Console.WriteLine("Nhap so dinh cuar do thi");
n = int.Parse(Console.ReadLine());
a = new List<int>[n];
for (int i = 0; i < n; i++)
{
Console.WriteLine("Danh sach ke cho dinh {0}",i+1);
a[i] = new List<int>();
Console.WriteLine("So dinh ke:");
int sodinh = int.Parse(Console.ReadLine());
for (int j = 0; j < sodinh; j++)
{
Console.WriteLine("Dinh ke thu {0}",j+1);
int tam = int.Parse(Console.ReadLine());
a[i].Add(tam);
}
}
}

static void Main(string[] args)


{
Hamilton t = new Hamilton();
t.Nhap(); t.HienHamilton(X);
chuaxet = new bool[n];
X = new int[n];
X[0] = 1;
for (int i = 0; i < n; i++)
{
chuaxet[i] = true;
}
chuaxet[0] = false;
t.CTHamilton(0);
Console.ReadKey();

}
}
}

You might also like