Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX 100
- /*
- *
- */
- typedef int TElemento;
- //ESTRUCTURAS PARA LISTAS
- typedef struct node
- {
- TElemento value;
- struct node* next;
- }TNodoL;
- typedef struct list
- {
- TNodoL* head;
- int nElements;
- }Tlist;
- void list_init(Tlist* l)
- {
- l -> head = NULL;
- l -> nElements = 0;
- }
- void insert_tail(Tlist* l, TElemento val)
- {
- TNodoL* auxNode = malloc(sizeof(TNodoL));
- auxNode -> value = val;
- auxNode -> next = NULL;
- TNodoL* currNode;
- currNode = l -> head;
- if (currNode == NULL)
- l -> head = auxNode;
- else
- {
- while (currNode -> next != NULL)
- currNode = currNode -> next;
- currNode -> next = auxNode;
- }
- l -> nElements ++;
- }
- int search_element(Tlist* l, TElemento val)
- {
- int found = 0;
- TNodoL* currNode;
- currNode = l -> head;
- while (currNode != NULL && found == 0)
- {
- if (currNode -> value == val)
- found = 1;
- currNode = currNode -> next;
- }
- if (found == 1)
- return 1;
- else
- return 0;
- }
- //ESTRUCTURAS PARA PILAS
- typedef struct nodo {
- TElemento info;
- struct nodo *sig;
- } TNodoP;
- typedef struct {
- TNodoP *top;
- } TPila;
- void Pila_init(TPila *P){
- P->top = NULL;
- }
- void Pila_push(TPila *P, TElemento nuevoElem){
- TNodoP *ptr_nuevoElem;
- ptr_nuevoElem = malloc(sizeof(TNodoP));
- if (ptr_nuevoElem){
- ptr_nuevoElem->info = nuevoElem;
- ptr_nuevoElem->sig = P->top;
- P->top = ptr_nuevoElem;
- }
- }
- int Pila_isEmpty(TPila *P){
- return (P->top == NULL);
- }
- TElemento Pila_pop(TPila *P){
- TElemento top_info; TNodoP *ptr_top;
- if (!Pila_isEmpty(P)){
- ptr_top = P->top;
- top_info = ptr_top->info;
- P->top = P->top->sig;
- free(ptr_top);
- return top_info;
- }
- return -1;
- }
- //ESTRUCTURAS PARA GRAFOS
- typedef struct arista{//esta estructura sirve para ser conectada a la estructura vertice, son como nodos
- TElemento info; //los cuales formaran parte de una lista de nodos aristas
- float peso;//informa el coste que cuesta llegar desde el vertice origen a este nodo arista
- struct arista *sig;//es un puntero al siguiente nodo arista correpondiente a determinado vertice origen
- }TArista;
- typedef struct vertice{//estas estructuras forman parte de una lista de vertices que son indicadas por la estructura grafo
- TElemento info;//contiene la informacion del contenido de cada vertice
- TArista *aristasInicio;//es un puntero al primer nodo arista al cual esta vinculado
- TArista *aristasFin;//es un puntero al ultimo nodo arista al cual esta vinculado
- struct vertice *sig;// es un puntero al siguiente vertice del grafo
- }TVertice;
- typedef struct grafo{// esta estructura tiene informacion del numero de vertices que contiene el grafo
- int numVertices;
- TVertice *adjInicio;//puntero al primer vertice
- TVertice *adjFin;//puntero al ultimo vertice
- }TGrafo;
- void Grafo_CrearVacio(TGrafo *G){
- G->adjInicio = NULL;
- G->adjFin = NULL;
- G->numVertices = 0;
- }
- TVertice* Grafo_ExisteVertice(TGrafo *G, TElemento v){
- if (G->adjInicio != NULL){
- TVertice *ptrRec = G->adjInicio;
- while (ptrRec!=NULL){
- if (ptrRec->info == v)
- return ptrRec;
- ptrRec = ptrRec->sig;
- }
- }
- return NULL;
- }
- int Grafo_ExisteArista(TGrafo *G, TElemento u, TElemento v){
- TVertice *ptrVert = Grafo_ExisteVertice(G, u);
- if (ptrVert!=NULL){
- TArista *ptrArt = ptrVert->aristasInicio;
- while (ptrArt!=NULL){
- if (ptrArt->info == v)
- return 1;
- ptrArt = ptrArt->sig;
- }
- }
- return 0;
- }
- void Grafo_InsertarVertice(TGrafo *G, TElemento v){
- if (Grafo_ExisteVertice(G,v) == NULL){//verifica si ya existe un vertice con ese valor en el grafo
- TVertice *ptrNuevo = malloc(sizeof(TVertice));
- if (ptrNuevo){
- ptrNuevo->info = v;
- ptrNuevo->aristasInicio=NULL;
- ptrNuevo->aristasFin=NULL;
- ptrNuevo->sig=NULL;
- if (G->adjInicio==NULL)
- G->adjInicio=ptrNuevo;
- else
- G->adjFin->sig=ptrNuevo;
- G->adjFin = ptrNuevo;
- G->numVertices++;
- }
- }
- }
- void Grafo_InsertarArista(TGrafo *G, TElemento u, TElemento v, float peso){
- TVertice *ptrVert = Grafo_ExisteVertice(G,u);//devuelve un puntero al vertice que tiene el valor del parametro u
- if (ptrVert!=NULL && Grafo_ExisteVertice(G, v) && !Grafo_ExisteArista(G,u,v)){
- //tiene que asegurarse de que existe el vertice con el valor u, el vertice con el valor v, y de que no exista ya
- //una arista entre esos valores
- TArista *ptrNuevo = malloc(sizeof(TArista));
- ptrNuevo->info=v;
- ptrNuevo->peso=peso;
- ptrNuevo->sig=NULL;
- if (ptrVert->aristasInicio==NULL)
- ptrVert->aristasInicio=ptrNuevo;
- else
- ptrVert->aristasFin->sig=ptrNuevo;
- ptrVert->aristasFin=ptrNuevo;
- }
- }
- int vertAdyacentes(TElemento hijos[MAX],TGrafo* g,TElemento p){
- TVertice* v=Grafo_ExisteVertice(g,p);
- TArista* currhijo=v->aristasInicio;
- int i=0;
- while (currhijo!=NULL){
- hijos[i]=currhijo->info;
- currhijo=currhijo->sig;
- i++;
- }
- return i;
- }
- void DFS(TGrafo* G,TPila* por_visit,TElemento vini,TElemento vfin){
- Pila_init(por_visit);
- Tlist visitados;
- list_init(&visitados);
- Pila_push(por_visit, vini);
- printf("Los vertices ordenados en Depth Fist Search son: ");
- while (Pila_isEmpty(por_visit)==0 && por_visit->top->info!=vfin){
- TElemento p;
- p=Pila_pop(por_visit);
- printf("%d",p);
- insert_tail(&visitados, p);
- TElemento hijos[MAX];
- int n=vertAdyacentes(hijos,G,p);
- int i=0;
- while (i<n && !search_element(&visitados, hijos[i])){
- Pila_push(por_visit, hijos[i]);
- i++;
- }
- }
- printf("%d\n",vfin);
- }
- int main(int argc, char** argv)
- {
- TGrafo G;
- TPila P;
- Grafo_CrearVacio(&G);
- Grafo_InsertarVertice(&G, 1);
- Grafo_InsertarVertice(&G, 2);
- Grafo_InsertarVertice(&G, 3);
- Grafo_InsertarVertice(&G, 4);
- Grafo_InsertarVertice(&G, 5);
- Grafo_InsertarVertice(&G, 6);
- Grafo_InsertarArista(&G, 1, 2, 1.0);
- Grafo_InsertarArista(&G, 2, 3, 2.0);
- Grafo_InsertarArista(&G, 3, 5, 5.0);
- Grafo_InsertarArista(&G, 3, 4, 4.0);
- Grafo_InsertarArista(&G, 1, 6, 6.0);
- DFS(&G,&P,1,5);
- return (EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement