Advertisement
DimasDark

~DFS Ponto-a-Ponto~

Aug 17th, 2013
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. //Consiste num Grafo, representado por suas aretas pelo qual o algoritmo faz a busca em profundidade de um nó //inicial até um final, imprimindo a ordem da busca
  2.  
  3. struct no
  4. {
  5.     int info;
  6.     struct no *proximo;
  7. };
  8.  
  9. class pilha
  10. {
  11.     struct no *topo;
  12. public:
  13.     pilha();
  14.     void push(int);
  15.     int pop();
  16.     bool isVazia();
  17.     void mostrar();
  18. };
  19.  
  20. pilha::pilha()
  21. {
  22.     this->topo = NULL;
  23. }
  24.  
  25. void pilha::push(int v)
  26. {
  27.     no *p = new no;
  28.     p->info = v;
  29.     p->proximo = NULL;
  30.     if(topo!=NULL)
  31.     {
  32.         p->proximo = topo;
  33.     }
  34.     topo = p;
  35. }
  36.  
  37. int pilha::pop()
  38. {
  39.     struct no *temp;
  40.     int valor;
  41.     if(topo!=NULL)
  42.     {
  43.         temp = topo;
  44.         topo = topo->proximo;
  45.         valor = temp->info;
  46.         delete temp;
  47.     }
  48.     return valor;
  49. }
  50.  
  51. bool pilha::isVazia()
  52. {
  53.     return (topo == NULL);
  54. }
  55.  
  56. void pilha::mostrar()
  57. {
  58.     struct no *p = topo;
  59.     if(topo!=NULL)
  60.     {
  61.         cout<<"\npilha:\n";
  62.         while(p!=NULL)
  63.         {
  64.             cout<<p->info<<endl;
  65.             p = p->proximo;
  66.         }
  67.     }
  68. }
  69.  
  70. class Grafo
  71. {
  72. private:
  73.     int n;
  74.     int **A;
  75. public:
  76.     Grafo(int size = 2);
  77.     ~Grafo();
  78.     bool isConectado(int, int);
  79.     void insereAresta(int x, int y);
  80.     int DFS(int , int);
  81. };
  82.  
  83. Grafo::Grafo(int size)
  84. {
  85.     int i, j;
  86.     if (size < 2) n = 2;
  87.     else n = size;
  88.     A = new int*[n];
  89.     for (i = 0; i < n; ++i)
  90.         A[i] = new int[n];
  91.     for (i = 0; i < n; ++i)
  92.         for (j = 0; j < n; ++j)
  93.             A[i][j] = 0;
  94. }
  95.  
  96. Grafo::~Grafo()
  97. {
  98.     for (int i = 0; i < n; ++i)
  99.         delete [] A[i];
  100.     delete [] A;
  101. }
  102.  
  103. bool Grafo::isConectado(int x, int y)
  104. {
  105.     return (A[x][y] == 1);
  106. }
  107.  
  108. void Grafo::insereAresta(int x, int y)
  109. {
  110.     A[x][y] = A[y][x] = 1;
  111. }
  112.  
  113. void Grafo::DFS(int x, int chegada)
  114. {
  115.     pilha s;
  116.     bool *visitado = new bool[n+1];
  117.     int i;
  118.     for(i = 0; i <= n; i++)
  119.         visitado[i] = false;
  120.     s.push(x);
  121.     visitado[x] = true;
  122.     if(x == chegada) return;
  123.     cout << "DFS comecando de ";
  124.     cout << x << " para "<< chegada << " : " << endl;
  125.     while(!s.isVazia())
  126.     {
  127.         int k = s.pop();
  128.         if(k == chegada)
  129.         {
  130.             break;
  131.         }
  132.         cout<<k<<" ";
  133.         for (i = n; i >= 0 ; --i)
  134.             if (isConectado(k, i) && !visitado[i])
  135.             {
  136.                 s.push(i);
  137.                 visitado[i] = true;
  138.             }
  139.     }
  140.     cout<<endl;
  141.     delete [] visitado;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement