Indagamos en diferentes espacios y así traerte la solución para tu problema, en caso de preguntas puedes dejar tu inquietud y te contestaremos sin falta, porque estamos para servirte.
Ejemplo: bfs transversal del gráfico en c
// BFS algorithm in C#include #include #defineSIZE40structqueueint items[SIZE];int front;int rear;;structqueue*createQueue();voidenqueue(structqueue* q,int);intdequeue(structqueue* q);voiddisplay(structqueue* q);intisEmpty(structqueue* q);voidprintQueue(structqueue* q);structnodeint vertex;structnode* next;;structnode*createNode(int);structGraphint numVertices;structnode** adjLists;int* visited;;// BFS algorithmvoidbfs(structGraph* graph,int startVertex)structqueue* q =createQueue();
graph->visited[startVertex]=1;enqueue(q, startVertex);while(!isEmpty(q))printQueue(q);int currentVertex =dequeue(q);printf("Visited %dn", currentVertex);structnode* temp = graph->adjLists[currentVertex];while(temp)int adjVertex = temp->vertex;if(graph->visited[adjVertex]==0)
graph->visited[adjVertex]=1;enqueue(q, adjVertex);
temp = temp->next;// Creating a nodestructnode*createNode(int v)structnode* newNode =malloc(sizeof(structnode));
newNode->vertex = v;
newNode->next =NULL;return newNode;// Creating a graphstructGraph*createGraph(int vertices)structGraph* graph =malloc(sizeof(structGraph));
graph->numVertices = vertices;
graph->adjLists =malloc(vertices *sizeof(structnode*));
graph->visited =malloc(vertices *sizeof(int));int i;for(i =0; i < vertices; i++)
graph->adjLists[i]=NULL;
graph->visited[i]=0;return graph;// Add edgevoidaddEdge(structGraph* graph,int src,int dest)// Add edge from src to deststructnode* newNode =createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src]= newNode;// Add edge from dest to src
newNode =createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest]= newNode;// Create a queuestructqueue*createQueue()structqueue* q =malloc(sizeof(structqueue));
q->front =-1;
q->rear =-1;return q;// Check if the queue is emptyintisEmpty(structqueue* q)if(q->rear ==-1)return1;elsereturn0;// Adding elements into queuevoidenqueue(structqueue* q,int value)if(q->rear == SIZE -1)printf("nQueue is Full!!");elseif(q->front ==-1)
q->front =0;
q->rear++;
q->items[q->rear]= value;// Removing elements from queueintdequeue(structqueue* q)int item;if(isEmpty(q))printf("Queue is empty");
item =-1;else
item = q->items[q->front];
q->front++;if(q->front > q->rear)printf("Resetting queue ");
q->front = q->rear =-1;return item;// Print the queuevoidprintQueue(structqueue* q)int i = q->front;if(isEmpty(q))printf("Queue is empty");elseprintf("nQueue contains n");for(i = q->front; i < q->rear +1; i++)printf("%d ", q->items[i]);intmain()structGraph* graph =createGraph(6);addEdge(graph,0,1);addEdge(graph,0,2);addEdge(graph,1,2);addEdge(graph,1,4);addEdge(graph,1,3);addEdge(graph,2,4);addEdge(graph,3,4);bfs(graph,0);return0;
Calificaciones y comentarios
Puedes asentar nuestro ensayo exponiendo un comentario y valorándolo te estamos agradecidos.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)