rong2sao
02-02-2010, 22:12
Mình vừa code duyệt đồ thị theo chiều sâu dùng stack. Cô bắt dựa theo thuật toán này :
void Dtraverse (int k)
{
int h;
Stack<int> S(50);
S.Push(k);
chuaxet[k]=0;
while(!S.Empty())
{
h=S.Pop();
cout<<h<< " ";
for(i=n;i>=1;i--)
if(chuaxet[i] && A[h][i])
{
S.Push(i);
chuaxet[i]=0;
}
}
return;
}
Đây là code mình làm, nó chạy được trên CFree nhưng chả ra kết quả duyệt đồ thị gì cả. MÌnh chạy trên Turbo C thì nó báo 17 lỗi, trong đó có 1 kỗi là không đọc được file stack.h
Các bạn xem rồi giúp mình với, chả biết sai chỗ nào nữa. (:-)??
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <assert.h>
template <class Item>
class Stack
{
public :
Stack(int m=1);
// Khoi tao ngan xep rong~ voi' dung luong m
Stack(const Stack &S);
// Ham` kien' tao copy
~Stack() // Ham` huy? stack
{ delete []element;}
Stack &operator=(const Stack &S);
// Toan' tu? gan'
bool Empty() const;
void Push(const Item &x);
Item &Pop();
private:
Item *element;
int size;
int top;
};
//---------------------------------------------------------------------------------
template <class Item>
Stack<Item> ::Stack(int m) // Ham kien' tao Stack
{
element = new Item[m]; // Toan tu new dung` de khai bao 1 doi' tuong Item
size=m;
top=-1;
}
template <class Item>
Stack<Item> :: Stack(const Stack<Item> &S) // Ham kien' tao copy
{
element = new Item[S.size];
size = S.size;
top= S.top;
for(int i=0;i<=top;i++)
element[i]=S.element[i];
}
template <class Item> // Toan' tu gan'
Stack<Item> &Stack<Item> :: operator = (const Stack<Item> &S)
{
if(size != S.size)
{
delete []element;
element= new Item[S.size];
size= S.size;
}
top=S.top;
for(int i=0;i<=top;i++)
element[i]=S.element[i];
return *this;
}
// Cai` dat cac ham` thanh` phan` cua lop' Stack
template <class Item>
bool Stack<Item> :: Empty() const //Kiem tra Stack co rong~ khong
{
return top<0;
}
template <class Item>
void Stack<Item> ::Push(const Item &x) //Day? 1 phan tu vao dinh Stack
{
if(top==size-1) // Mang day`
{
Item *A = new Item[2*size];
assert(A!=NULL);
for(int i=0;i<=top;i++)
A[i]=element[i];
size=2*size;
delete []element;
element=A;
};
element[++top]=x;
}
template <class Item>
Item &Stack<Item> :: Pop() // Loai phan tu o dinh? cua ngan xep
{
assert(top>=0);
return element[top--];
}
//-----------------------------------------------------------------------------
int a[50][50],n,i,j,chuaxet[50];
void Dtraverse (int k)
{
int h;
Stack<int> S(50);
S.Push(k);
chuaxet[k]=false;
while(!S.Empty())
{
h=S.Pop();
cout<<h<< " ";
for(i=n;i>=1;i--)
if(chuaxet[i] && a[h][i])
{
S.Push(i);
chuaxet[i]=0;
}
}
return;
}
void nhap (int a[50][50])
{
int i,j;
cout<<"Nhap so dinh cua do thi n = "; cin>>n;
for (i=1;i<=n;i++)
{
for (j=i+1;j<=n;j++)
{
cout<<"a["<<i<<","<<j<<"]="; cin>>a[i][j];
a[j][i]=a[i][j];
}
a[i][i]=0;
}
}
void indothi (int a[50][50])
{
int i,j;
cout<<"Do thi da nhap : "<<endl;
for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
cout<<" " <<a[i][j];
cout<<endl;
}
}
void timkiem (int a[50][50])
{
int i,u;
nhap (a);
indothi (a);
while (1)
{
for (i=1;i<=n;i++)
chuaxet[i]=0;
cout<<"Ban muon duyet tu dinh nao (bam -1 thoat) : "; cin>>u;
if (u==-1) break;
if (u>n || u<0)
{
cout<<"Dinh nay khong thuoc do thi !";
break;
}
Dtraverse(u);
}
}
//-----------------------------------------
void main ()
{
int a[50][50];
timkiem (a);
}
void Dtraverse (int k)
{
int h;
Stack<int> S(50);
S.Push(k);
chuaxet[k]=0;
while(!S.Empty())
{
h=S.Pop();
cout<<h<< " ";
for(i=n;i>=1;i--)
if(chuaxet[i] && A[h][i])
{
S.Push(i);
chuaxet[i]=0;
}
}
return;
}
Đây là code mình làm, nó chạy được trên CFree nhưng chả ra kết quả duyệt đồ thị gì cả. MÌnh chạy trên Turbo C thì nó báo 17 lỗi, trong đó có 1 kỗi là không đọc được file stack.h
Các bạn xem rồi giúp mình với, chả biết sai chỗ nào nữa. (:-)??
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <assert.h>
template <class Item>
class Stack
{
public :
Stack(int m=1);
// Khoi tao ngan xep rong~ voi' dung luong m
Stack(const Stack &S);
// Ham` kien' tao copy
~Stack() // Ham` huy? stack
{ delete []element;}
Stack &operator=(const Stack &S);
// Toan' tu? gan'
bool Empty() const;
void Push(const Item &x);
Item &Pop();
private:
Item *element;
int size;
int top;
};
//---------------------------------------------------------------------------------
template <class Item>
Stack<Item> ::Stack(int m) // Ham kien' tao Stack
{
element = new Item[m]; // Toan tu new dung` de khai bao 1 doi' tuong Item
size=m;
top=-1;
}
template <class Item>
Stack<Item> :: Stack(const Stack<Item> &S) // Ham kien' tao copy
{
element = new Item[S.size];
size = S.size;
top= S.top;
for(int i=0;i<=top;i++)
element[i]=S.element[i];
}
template <class Item> // Toan' tu gan'
Stack<Item> &Stack<Item> :: operator = (const Stack<Item> &S)
{
if(size != S.size)
{
delete []element;
element= new Item[S.size];
size= S.size;
}
top=S.top;
for(int i=0;i<=top;i++)
element[i]=S.element[i];
return *this;
}
// Cai` dat cac ham` thanh` phan` cua lop' Stack
template <class Item>
bool Stack<Item> :: Empty() const //Kiem tra Stack co rong~ khong
{
return top<0;
}
template <class Item>
void Stack<Item> ::Push(const Item &x) //Day? 1 phan tu vao dinh Stack
{
if(top==size-1) // Mang day`
{
Item *A = new Item[2*size];
assert(A!=NULL);
for(int i=0;i<=top;i++)
A[i]=element[i];
size=2*size;
delete []element;
element=A;
};
element[++top]=x;
}
template <class Item>
Item &Stack<Item> :: Pop() // Loai phan tu o dinh? cua ngan xep
{
assert(top>=0);
return element[top--];
}
//-----------------------------------------------------------------------------
int a[50][50],n,i,j,chuaxet[50];
void Dtraverse (int k)
{
int h;
Stack<int> S(50);
S.Push(k);
chuaxet[k]=false;
while(!S.Empty())
{
h=S.Pop();
cout<<h<< " ";
for(i=n;i>=1;i--)
if(chuaxet[i] && a[h][i])
{
S.Push(i);
chuaxet[i]=0;
}
}
return;
}
void nhap (int a[50][50])
{
int i,j;
cout<<"Nhap so dinh cua do thi n = "; cin>>n;
for (i=1;i<=n;i++)
{
for (j=i+1;j<=n;j++)
{
cout<<"a["<<i<<","<<j<<"]="; cin>>a[i][j];
a[j][i]=a[i][j];
}
a[i][i]=0;
}
}
void indothi (int a[50][50])
{
int i,j;
cout<<"Do thi da nhap : "<<endl;
for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
cout<<" " <<a[i][j];
cout<<endl;
}
}
void timkiem (int a[50][50])
{
int i,u;
nhap (a);
indothi (a);
while (1)
{
for (i=1;i<=n;i++)
chuaxet[i]=0;
cout<<"Ban muon duyet tu dinh nao (bam -1 thoat) : "; cin>>u;
if (u==-1) break;
if (u>n || u<0)
{
cout<<"Dinh nay khong thuoc do thi !";
break;
}
Dtraverse(u);
}
}
//-----------------------------------------
void main ()
{
int a[50][50];
timkiem (a);
}