Advertisement
nguyenvanquan7826

duyet do thi

Apr 13th, 2013
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <queue>
  5. #include <stack>
  6. #include <fstream>
  7. #define INP "input.INP"
  8. using namespace std;
  9.  
  10. typedef int item;
  11. typedef struct GRAPH
  12. {
  13.     char *name;
  14.     item ** G;
  15.     int n;
  16. } Graph;
  17.  
  18. int *visit;
  19. void input(Graph &Gr)
  20. {
  21.     ifstream inp(INP);
  22.     if (inp == NULL)
  23.     {
  24.         cout<<"Khong thay file input";
  25.         return;
  26.     }
  27.     inp >> Gr.n ;
  28.    
  29.     Gr.name = new char [Gr.n];
  30.     for (int i=0; i<Gr.n; i++)
  31.         inp >> Gr.name[i];
  32.     Gr.G = new int *[Gr.n];
  33.    
  34.     for (int i=0; i<Gr.n; i++)
  35.     {
  36.         Gr.G[i] = new int [Gr.n];
  37.         for (int j=0; j<Gr.n; j++)
  38.             inp >> Gr.G[i][j];
  39.     }
  40. }
  41.  
  42. void output(Graph Gr)
  43. {
  44.     cout<<endl<<"Ma tran ke cua do thi"<<endl<<endl;
  45.     for (int i=0; i<Gr.n; i++)
  46.         cout<<setw(2)<<Gr.name[i];
  47.     cout<<endl<<endl;
  48.     for (int i=0; i<Gr.n; i++)
  49.     {
  50.         for (int j=0; j<Gr.n; j++)
  51.             cout<<setw(2)<<Gr.G[i][j];
  52.         cout<<endl;
  53.     }
  54. }
  55.  
  56. void Menu(int &select, int &begin)
  57. {
  58.     cout<<endl<<"Moi ban chon cach duyet do thi:"<<endl;
  59.     cout<<"1: Duyet do thi theo chieu sau dung de quy - DFS"<<endl;
  60.     cout<<"2: Duyet do thi theo chieu sau dung stack - DFS_stack"<<endl;
  61.     cout<<"3: Duyet do thi theo chieu rong dung queue - BFS"<<endl;
  62.     cout<<"4: Thoat !"<<endl;
  63.     cin >> select;
  64.     if (select !=4 )
  65.     {
  66.         cout<<"Nhap dinh bat dau duyet (tu dinh 0->12) : ";
  67.         cin>>begin;
  68.     }
  69. }
  70.  
  71. void DFS (Graph Gr, int i)//Thu tuc duyet DFS, duyet theo chieu sau su dung ky thuat de quy.
  72. {
  73.     visit[i] = 1;
  74.     cout<<setw(5)<<Gr.name[i];
  75.     for (int j=0; j<Gr.n; j++)
  76.         if (Gr.G[i][j])
  77.             if (!visit[j])
  78.                 DFS (Gr,j);
  79. }
  80.  
  81. void DFS_stack (Graph Gr, int i)//Thu tuc duyet DFS, duyet theo chieu sau su dung stack khu de quy.
  82. {
  83.     visit[i] = 1;
  84.     stack <int> S;
  85.     S.push(i);
  86.     while (!S.empty())
  87.     {
  88.         i = S.top();
  89.         S.pop();
  90.         cout<<setw(5)<<Gr.name[i];
  91.         for (int j = 0; j<Gr.n; j++)
  92.             if (Gr.G[i][j])
  93.                 if (!visit[j])
  94.                 {
  95.                     visit[j] = 1;
  96.                     S.push(j);
  97.                 }
  98.     }
  99. }
  100.  
  101. void BFS (Graph Gr, int i)//Thu tuc duyet BFS, duyet theo chieu rong su dung queue.
  102. {
  103.     queue <int> Q;
  104.     visit[i] = 1;
  105.     Q.push(i);
  106.     while (!Q.empty())
  107.     {
  108.         i = Q.front();
  109.         Q.pop();
  110.         cout<<setw(5)<<Gr.name[i];
  111.         for (int j=0; j<Gr.n; j++)
  112.             if (Gr.G[i][j])
  113.                 if (!visit[j])
  114.                 {
  115.                     visit[j] = 1;
  116.                     Q.push(j);
  117.                 }
  118.     }
  119. }
  120.  
  121.  
  122. int main()
  123. {
  124.    
  125.     Graph Gr;
  126.     int select = 1, Begin;
  127.     input(Gr);
  128.     visit = new int [Gr.n];
  129.    
  130.     while (select)
  131.     {
  132.         system("cls");
  133.         fill (visit, visit + Gr.n, 0);
  134.         output(Gr);
  135.         Menu(select, Begin);
  136.         switch (select)
  137.         {
  138.             case 1:
  139.             {
  140.                 cout<<"Duyet do thi theo chieu sau dung de quy - DFS"<<endl;
  141.                 DFS (Gr,Begin);
  142.                 cout<<endl;
  143.                 system("pause");
  144.                 break;
  145.             }
  146.             case 2:
  147.             {
  148.                 cout<<"2: Duyet do thi theo chieu sau dung stack - DFS_stack"<<endl;
  149.                 DFS_stack (Gr,Begin);
  150.                 cout<<endl;
  151.                 system("pause");
  152.                 break;
  153.             }
  154.             case 3:
  155.             {
  156.                 cout<<"3: Duyet do thi theo chieu rong dung queue - BFS"<<endl;
  157.                 BFS (Gr,Begin);
  158.                 cout<<endl;
  159.                 system("pause");
  160.                 break;
  161.             }
  162.         }
  163.         if (select == 4) break;
  164.     }
  165.     system("cls");
  166.     cout<<"***** Duyet do thi - [email protected] - CNTT&TT Thai Nguyen *****"<<endl;
  167.     system("pause");
  168.     return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement