Dothi1: #Include #Include Int Int Void Int

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 11

DoThi1

#include <iostream.h>
#include <fstream.h>

int stack[30];
int last;
void Push(int v)//day 1 gia tri vao trong ham stack(dut dia vao ngan
xep)
{
last++;
stack[last]=v;
}
int Pop()// lay 1 dinh cua "ngan xep" tra ve gia tri cho kq ham
{
int x;
x=stack[last];
last--;
return x;
}
int Get()//lay 1 phan tu tren top ngan xep
{
int x;
x=stack[last];
return x;
}

class DoThi
{
private:
int N;
int A[20][20];
int B[20]; //danh dau dinh da qua(voi chu do thi HMT)
int C[20]; //luu duong di
bool daqua[30];
int baccuadinh[30];
int luuvet[30];
int dinhdau[30];
public:
DoThi()
{
}

void khoitao(int d)
{
N=d;
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
A[i][j]=0;
}
daqua[i]=false;
B[i]=0;
}
C[0] = 0;B[1]=1;//xuat fat tu dinh 1
}
void set_canh(int x,int y)
{
A[x][y]=1;
A[y][x]=1;

bool lacanh(int x,int y)


{

return (A[x][y]==1);
}

void display()
{
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
cout<<A[i][j]<<" ";
}
cout<<endl;
}
}

void docfile(char infile[30])


{
int x,y,n;
ifstream fin(infile);
fin>>n;

khoitao(n);
while(!fin.eof())
{
fin>>x;
fin>>y;
set_canh(x,y);
}

int tinhbac(int d) //tinhbaccua1dinh


{
int bac=0;
for(int i=1;i<=N;i++)
{

bac=bac+A[d][i];

}
return bac;

}
void baccuacacdinh()
{
for(int i=1;i<=N;i++)
{
baccuadinh[i]=tinhbac(i);

}
}

void DFS(int u)
{
//cout<<u<<" ";
daqua[u]=true;
Push(u);
for(int i=1;i<=N;i++)
if(lacanh(u,i)&&daqua[i]==false)
{
daqua[i]=true;
luuvet[i]=u;
DFS(i);
}
}

void duongdi(int x,int y)


{
DFS(x);
if(daqua[y]==false)
cout<<"Khong co duong di giua 2 dinh nay"<<endl;
else
{

cout<<"Duong di tu "<<x<<" den "<<y<<" la:"<<endl;


int i=y;
cout<<y<<"<-";
while(luuvet[i]!=x)
{
cout<<luuvet[i]<<"<-";
i=luuvet[i];

}
cout<<x<<endl;
}

int thanhphanlienthong()
{
int tplt=0;
for(int i=1;i<=N;i++)
{
if(daqua[i]==false)
{
DFS(i);
tplt++;
}
}
return tplt;
}

bool lalienthong()
{
return thanhphanlienthong()==1;
}

bool isChuTrinhEuler()
{
baccuacacdinh();
bool co=true;
for(int i=1;i<=N;i++)
if(baccuadinh[i]%2!=0)
{
co=false;
break;
}
return co&&lalienthong();
}

bool isDuongdiEuler()
{

int dem=0;
baccuacacdinh();
for(int i=1;i<=N;i++)
if(baccuadinh[i]%2!=0)
{
dem++;
}

return (dem<=2)&&(lalienthong());
}

bool isTree()
{
int tong=0;
for(int i=1;i<=N;i++)
{
tong+=tinhbac(i);
}

return lalienthong()&&(tong/2==N-1);
}

void duongdiHMT()
{
for(int i=0;i<N;i++)
{
cout<<C[i]+1<<" ";
}
cout<<C[0]+1<<endl;

}
void Hamilton(int j)
{
for (int k = 1; k <= N; k++)
if (B[k]==0 && A[C[j-1]][k])
{
B[k] = 1; C[j] = k;
if (j==N-1)
{
if(A[C[j]][C[0]])
duongdiHMT();
}
else Hamilton(j+1);
B[k] = 0;
}

void Euler()
{
int u,v;
stack[1]=1;//khoi tao ngan xep chi co dinh 1
last=1;
while (last!=0)//voi dk la ngan xep chua ro~ng ta con lam
tiep
{
u=Get();
for (v=1; v<=N;v++)
if (A[u][v]==1)//neu dinh v ke voi u thi lam
{

Push(v); // day dinh tiep theo vao ngan


xep (tai day la dinh v se vao ngan xep )
A[u][v]=0;
A[v][u]=0;// xoa canh (u,v),(v,u)
break;
}
if (u==Get())// neu u bang dinh cua ngan xep va ko con
dinh ke;
{
cout<<Pop()<<" ";// in ra dinh ngan xep
tren euler
}
}
}

void timcaykhung()
{
DFS(1);
int u;
int dem=0;
cout<<"Cac cap canh cua cay khung"<<endl;
while(last!=0)
{
cout<<"("<<Pop()<<",";
u=Get();
cout<<u<<")"<<endl;
dem++;
if(dem==N-1)
break;

}
}

};

DoThi2
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

#define vocung 100


int gan[30];
bool chuaxet[30];
bool danhdau[30];
bool lienthong;

int kc[30];
int truoc[30];
int d[30];

class DoThiTrongSo
{
private:
int N;
int A[30][30];
public:
DoThiTrongSo()
{
}

DoThiTrongSo(int d)
{
N=d;
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
{
A[i][j]=0;
}
}

void set_canh(int x,int y,int ts)


{

A[x][y]ts;
A[y][x]=ts;
}

bool lacanh(int x,int y)


{

return (A[x][y]!=100);
}

void display()
{
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
cout<<A[i][j]<<" \t";
}
cout<<endl;
}
}

void khoitao()
{

for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
A[i][j]=vocung;
}
chuaxet[i]=true;
}

kc[1]=0;
gan[1]=1;
for (int v=2;v<=N;v++) //gan khoang canh tu cac dinh den
dinh 1 la vo cung
kc[v]=vocung;

void khoitaoDRT(int u)//cho bai DRT


{
int w;
for (w=1;w<=N;w++)
{
chuaxet[w]=true;
d[w]=A[u][w];
truoc[w]=u;
}
chuaxet[u]=false;
d[w]=0;
}

void Dijkstra(int v)//cho bai DRT


{
int s;
int w;
do{
//int dem=0;
/*for(w=1;w<=N;w++)
{
cout<<"("<<d[w]<<","<<truoc[w]<<")\t";
dem++;
if(dem%N==0)
{
cout<<endl;
}
}*/
w=1;
while (!chuaxet[w])
{
w++;
}//tim den dinh dau tien chua duoc xet;
int min=vocung;
s=w;
for(int z=1;z<=N;z++)
{
if(chuaxet[z] && d[z]<min)
{
min=d[z];
s=z;
}
}
chuaxet[s]=false;
//cout<<s<<endl;
if(s!=v)
{
for(w=1;w<=N;w++)
{
if(d[w]>d[s]+A[s][w])
{
d[w]=d[s]+A[s][w];
truoc[w]=s;
}
}
}
}while(s!=v);
}

void Duongdingannhat(int x,int y)//cho bai DRT


{
int temp[30];
int dem=0;

khoitaoDRT(x);
Dijkstra(y);
if(d[y]==vocung)
{
cout<<"Khong co duong di!"<<endl;
}
else
{
cout<<"Khoang cach nho nhat tu "<<x<<" den "<<y<<" la:
"<<d[y]<<endl;
cout<<"Duong di nguoc :";
while (x!=y)
{
temp[dem+1]=y;
cout<<y<<"<-";
dem++;
y=truoc[y];
}
cout<<x;
cout<<endl;
cout<<"Duong di xuoi :";
cout<<x<<"->";
for(int j=dem;j>1;j--)
{
cout<<temp[j]<<"->";
}
cout<<temp[j]<<endl;
}
}

void Prim()
{
int u=0,min;
lienthong=true;
for (int k=1;k<=N;k++)
{
min=vocung;
for (int i=1;i<=N;i++)
{
if(chuaxet[i]&&kc[i]<min)
{
min=kc[i];
u=i;
}
}
if(u==0)
{
lienthong=false;
break;
}//neu co u ko thoa man(kc[u]<vocung) thi tu break luc
do do thi ko lien thong

chuaxet[u]=false;//u thoa man danh dau u ;


for (int v=1;v<=N;v++)
if (chuaxet[v]&&kc[v]>A[u][v])
{
kc[v]=A[u][v];
gan[v]=u;
}
}
}

void InCayKhung()
{
Prim();
if(lienthong==false)
cout<<"Ko lien thong"<<endl;
else
{
cout<<"Cay khung nho nhat la: "<<endl;
int w=0;
for (int v=2;v<=N;v++)
{
cout<<"("<<gan[v]<<","<<v<<") ="<<A[gan[v]]
[v]<<endl;
w=w+A[gan[v]][v];
}
cout<<"Voi tong khoang cach la: "<<w<<endl;
}
}

void docfile(char infile[30])


{
int x,y,ts;
ifstream fin(infile);
fin>>N;
khoitao();

while(!fin.eof())
{
fin>>x;
fin>>y;
fin>>ts;
set_canh(x,y,ts);
}

};
#include "DoThiSo1.h" #include "DoThiSo2.h"
void main()
{ void main()
DoThi dt; {
dt.docfile("DTHMT.txt"); DoThiTrongSo dt;
dt.display(); dt.docfile("DT.txt");
/*cout<<endl<<"-------------" dt.display();
<<endl; //dt.InCayKhung();
dt.Hamilton(1);*/ dt.Duongdingannhat(1,4);

/*cout<<endl<<"-------------"
<<endl;
if(dt.isChuTrinhEuler()) }
{
cout<<"Chu trinh Euler
la: "<<endl;
dt.Euler();
}
else
cout<<"Do thi khong co
chu trinh Euler"<<endl;
if(dt.isDuongdiEuler())
{
cout<<"Duong di Euler
la: "<<endl;
dt.Euler();
}
else
cout<<"Do thi khong co
duong di Euler"<<endl;*/

//dt.duongdi(1,3);
//dt.timcaykhung();

You might also like