Advertisement
alvaro_93

DFS

Jul 3rd, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.80 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define MAX 100
  5.  
  6. /*
  7.  *
  8.  */
  9. typedef int TElemento;
  10. //ESTRUCTURAS PARA LISTAS
  11.  
  12. typedef struct node
  13. {
  14.     TElemento value;
  15.     struct node* next;
  16. }TNodoL;
  17.  
  18. typedef struct list
  19. {
  20.     TNodoL* head;  
  21.     int nElements;
  22. }Tlist;
  23.  
  24. void list_init(Tlist* l)
  25. {
  26.     l -> head = NULL;
  27.     l -> nElements = 0;
  28. }
  29.  
  30. void insert_tail(Tlist* l, TElemento val)
  31. {
  32.     TNodoL* auxNode = malloc(sizeof(TNodoL));
  33.     auxNode -> value = val;
  34.     auxNode -> next = NULL;
  35.     TNodoL* currNode;
  36.     currNode = l -> head;
  37.     if (currNode == NULL)
  38.         l -> head = auxNode;
  39.     else
  40.     {
  41.         while (currNode -> next != NULL)
  42.             currNode = currNode -> next;
  43.         currNode -> next = auxNode;
  44.     }
  45.     l -> nElements ++;
  46. }
  47.  
  48. int search_element(Tlist* l, TElemento val)
  49. {
  50.     int found = 0;
  51.     TNodoL* currNode;
  52.     currNode = l -> head;
  53.     while (currNode != NULL && found == 0)
  54.     {
  55.         if (currNode -> value == val)
  56.             found = 1; 
  57.         currNode = currNode -> next;
  58.     }
  59.     if (found == 1)
  60.             return 1;
  61.     else
  62.             return 0;
  63. }
  64.  
  65.  
  66. //ESTRUCTURAS PARA PILAS
  67.  
  68. typedef struct nodo {
  69.         TElemento info;
  70.         struct nodo *sig;
  71. } TNodoP;
  72.  
  73. typedef struct {
  74.         TNodoP *top;
  75. } TPila;
  76.  
  77.  
  78. void Pila_init(TPila *P){
  79.     P->top = NULL;
  80. }
  81. void Pila_push(TPila *P, TElemento nuevoElem){
  82.     TNodoP *ptr_nuevoElem;
  83.    
  84.     ptr_nuevoElem = malloc(sizeof(TNodoP));
  85.    
  86.     if (ptr_nuevoElem){
  87.         ptr_nuevoElem->info = nuevoElem;
  88.         ptr_nuevoElem->sig = P->top;
  89.         P->top = ptr_nuevoElem;
  90.     }
  91. }
  92.  
  93. int Pila_isEmpty(TPila *P){
  94.     return (P->top == NULL);
  95. }
  96.  
  97. TElemento Pila_pop(TPila *P){
  98.     TElemento top_info; TNodoP *ptr_top;
  99.     if (!Pila_isEmpty(P)){
  100.         ptr_top = P->top;
  101.         top_info = ptr_top->info;
  102.         P->top = P->top->sig;
  103.         free(ptr_top);
  104.         return top_info;
  105.     }
  106.     return -1;
  107. }
  108.  
  109.  
  110. //ESTRUCTURAS PARA GRAFOS
  111.  
  112. typedef struct arista{//esta estructura sirve para ser conectada a la estructura vertice, son como nodos
  113.     TElemento info;   //los cuales formaran parte de una lista de nodos aristas  
  114.     float peso;//informa el coste que cuesta llegar desde el vertice origen a este nodo arista
  115.     struct arista *sig;//es un puntero al siguiente nodo arista correpondiente a determinado vertice origen
  116. }TArista;
  117.  
  118. typedef struct vertice{//estas estructuras forman parte de una lista de vertices que son indicadas por la estructura grafo
  119.     TElemento info;//contiene la informacion del contenido de cada vertice
  120.     TArista *aristasInicio;//es un puntero al primer nodo arista al cual esta vinculado
  121.     TArista *aristasFin;//es un puntero al ultimo nodo arista al cual esta vinculado
  122.     struct vertice *sig;// es un puntero al siguiente vertice del grafo
  123. }TVertice;
  124.  
  125. typedef struct grafo{// esta estructura tiene informacion del numero de vertices que contiene el grafo
  126.    
  127.     int numVertices;
  128.     TVertice *adjInicio;//puntero al primer vertice
  129.     TVertice *adjFin;//puntero al ultimo vertice
  130. }TGrafo;
  131.  
  132.  
  133. void Grafo_CrearVacio(TGrafo *G){
  134.     G->adjInicio = NULL;
  135.     G->adjFin = NULL;
  136.     G->numVertices = 0;
  137. }
  138. TVertice* Grafo_ExisteVertice(TGrafo *G, TElemento v){
  139.     if (G->adjInicio != NULL){
  140.         TVertice *ptrRec = G->adjInicio;
  141.         while (ptrRec!=NULL){
  142.             if (ptrRec->info == v)
  143.                 return ptrRec;
  144.             ptrRec = ptrRec->sig;
  145.         }
  146.     }
  147.     return NULL;
  148. }
  149.  
  150. int Grafo_ExisteArista(TGrafo *G, TElemento u, TElemento v){
  151.     TVertice *ptrVert = Grafo_ExisteVertice(G, u);
  152.     if (ptrVert!=NULL){
  153.         TArista *ptrArt = ptrVert->aristasInicio;
  154.         while (ptrArt!=NULL){
  155.             if (ptrArt->info == v)
  156.                 return 1;
  157.             ptrArt = ptrArt->sig;
  158.         }
  159.     }
  160.     return 0;
  161. }
  162. void Grafo_InsertarVertice(TGrafo *G, TElemento v){
  163.     if (Grafo_ExisteVertice(G,v) == NULL){//verifica si ya existe un vertice con ese valor en el grafo
  164.         TVertice *ptrNuevo = malloc(sizeof(TVertice));
  165.         if (ptrNuevo){
  166.             ptrNuevo->info = v;
  167.             ptrNuevo->aristasInicio=NULL;
  168.             ptrNuevo->aristasFin=NULL;
  169.             ptrNuevo->sig=NULL;
  170.             if (G->adjInicio==NULL)
  171.                 G->adjInicio=ptrNuevo;
  172.             else
  173.                 G->adjFin->sig=ptrNuevo;
  174.             G->adjFin = ptrNuevo;
  175.             G->numVertices++;
  176.         }
  177.     }
  178. }
  179.  
  180. void Grafo_InsertarArista(TGrafo *G, TElemento u, TElemento v, float peso){
  181.     TVertice *ptrVert = Grafo_ExisteVertice(G,u);//devuelve un puntero al vertice que tiene el valor del parametro u
  182.     if (ptrVert!=NULL && Grafo_ExisteVertice(G, v) && !Grafo_ExisteArista(G,u,v)){
  183.         //tiene que asegurarse de que existe el vertice con el valor u, el vertice con el valor v, y de que no exista ya
  184.         //una arista entre esos valores
  185.         TArista *ptrNuevo = malloc(sizeof(TArista));
  186.         ptrNuevo->info=v;
  187.         ptrNuevo->peso=peso;
  188.         ptrNuevo->sig=NULL;
  189.         if (ptrVert->aristasInicio==NULL)
  190.             ptrVert->aristasInicio=ptrNuevo;
  191.         else
  192.             ptrVert->aristasFin->sig=ptrNuevo;
  193.         ptrVert->aristasFin=ptrNuevo;
  194.     }
  195. }
  196.  
  197. int vertAdyacentes(TElemento hijos[MAX],TGrafo* g,TElemento p){
  198.     TVertice* v=Grafo_ExisteVertice(g,p);
  199.    
  200.     TArista* currhijo=v->aristasInicio;
  201.     int i=0;
  202.     while (currhijo!=NULL){
  203.         hijos[i]=currhijo->info;
  204.         currhijo=currhijo->sig;
  205.         i++;
  206.     }
  207.     return i;
  208.    
  209.    
  210.    
  211. }
  212. void DFS(TGrafo* G,TPila* por_visit,TElemento vini,TElemento vfin){
  213.     Pila_init(por_visit);
  214.     Tlist visitados;
  215.     list_init(&visitados);
  216.     Pila_push(por_visit, vini);
  217.     printf("Los vertices ordenados en Depth Fist Search son: ");
  218.     while (Pila_isEmpty(por_visit)==0 && por_visit->top->info!=vfin){
  219.         TElemento p;
  220.         p=Pila_pop(por_visit);
  221.         printf("%d",p);
  222.         insert_tail(&visitados, p);
  223.         TElemento hijos[MAX];
  224.        
  225.         int n=vertAdyacentes(hijos,G,p);
  226.         int i=0;
  227.         while (i<n && !search_element(&visitados, hijos[i])){
  228.             Pila_push(por_visit, hijos[i]);
  229.             i++;
  230.            
  231.         }
  232.                
  233.        
  234.                
  235.                
  236.        
  237.        
  238.        
  239.     }
  240.     printf("%d\n",vfin);
  241. }
  242.  
  243. int main(int argc, char** argv)
  244. {
  245.     TGrafo G;
  246.     TPila P;
  247.    
  248.     Grafo_CrearVacio(&G);
  249.     Grafo_InsertarVertice(&G, 1);
  250.     Grafo_InsertarVertice(&G, 2);
  251.     Grafo_InsertarVertice(&G, 3);
  252.     Grafo_InsertarVertice(&G, 4);
  253.     Grafo_InsertarVertice(&G, 5);
  254.     Grafo_InsertarVertice(&G, 6);
  255.     Grafo_InsertarArista(&G, 1, 2, 1.0);
  256.     Grafo_InsertarArista(&G, 2, 3, 2.0);
  257.     Grafo_InsertarArista(&G, 3, 5, 5.0);
  258.     Grafo_InsertarArista(&G, 3, 4, 4.0);
  259.     Grafo_InsertarArista(&G, 1, 6, 6.0);
  260.    
  261.    
  262.      DFS(&G,&P,1,5);
  263.    
  264.    
  265.    
  266.    
  267.    
  268.    
  269.    
  270.  
  271.     return (EXIT_SUCCESS);
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement