|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | + |
| 4 | +struct Node { |
| 5 | + int vertex; |
| 6 | + struct Node* next; |
| 7 | +}; |
| 8 | + |
| 9 | +struct Graph { |
| 10 | + struct Node* adjacencyList[100]; |
| 11 | + int visited[100]; |
| 12 | +}; |
| 13 | + |
| 14 | +void initializeGraph(struct Graph* graph, int numVertices) { |
| 15 | + for (int i = 0; i < numVertices; ++i) { |
| 16 | + graph->adjacencyList[i] = NULL; |
| 17 | + graph->visited[i] = 0; |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +void addEdge(struct Graph* graph, int src, int dest) { |
| 22 | + struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); |
| 23 | + newNode->vertex = dest; |
| 24 | + newNode->next = graph->adjacencyList[src]; |
| 25 | + graph->adjacencyList[src] = newNode; |
| 26 | + |
| 27 | + newNode = (struct Node*)malloc(sizeof(struct Node)); |
| 28 | + newNode->vertex = src; |
| 29 | + newNode->next = graph->adjacencyList[dest]; |
| 30 | + graph->adjacencyList[dest] = newNode; |
| 31 | +} |
| 32 | + |
| 33 | +void dfs(struct Graph* graph, int startVertex) { |
| 34 | + int stack[100]; |
| 35 | + int top = -1; |
| 36 | + stack[++top] = startVertex; |
| 37 | + graph->visited[startVertex] = 1; |
| 38 | + while (top != -1) { |
| 39 | + int currentVertex = stack[top--]; |
| 40 | + printf("%d ", currentVertex); |
| 41 | + struct Node* current = graph->adjacencyList[currentVertex]; |
| 42 | + while (current != NULL) { |
| 43 | + int neighbor = current->vertex; |
| 44 | + if (!graph->visited[neighbor]) { |
| 45 | + stack[++top] = neighbor; |
| 46 | + graph->visited[neighbor] = 1; |
| 47 | + } |
| 48 | + current = current->next; |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +void bfs(struct Graph* graph, int startVertex) { |
| 54 | + int queue[100]; |
| 55 | + int front = 0, rear = -1; |
| 56 | + queue[++rear] = startVertex; |
| 57 | + graph->visited[startVertex] = 1; |
| 58 | + while (front <= rear) { |
| 59 | + int currentVertex = queue[front++]; |
| 60 | + printf("%d ", currentVertex); |
| 61 | + struct Node* current = graph->adjacencyList[currentVertex]; |
| 62 | + while (current != NULL) { |
| 63 | + int neighbor = current->vertex; |
| 64 | + if (!graph->visited[neighbor]) { |
| 65 | + queue[++rear] = neighbor; |
| 66 | + graph->visited[neighbor] = 1; |
| 67 | + } |
| 68 | + current = current->next; |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +int main() { |
| 74 | + struct Graph graph; |
| 75 | + int numVertices, numEdges; |
| 76 | + printf("Enter the number of vertices: "); |
| 77 | + scanf("%d", &numVertices); |
| 78 | + initializeGraph(&graph, numVertices); |
| 79 | + printf("Enter the number of edges: "); |
| 80 | + scanf("%d", &numEdges); |
| 81 | + printf("Enter the edges (format: src dest):\n"); |
| 82 | + for (int i = 0; i < numEdges; ++i) { |
| 83 | + int src, dest; |
| 84 | + scanf("%d %d", &src, &dest); |
| 85 | + addEdge(&graph, src, dest); |
| 86 | + } |
| 87 | + printf("\n\nDepth-First Search (DFS) starting from vertex 0:\n"); |
| 88 | + dfs(&graph, 0); |
| 89 | + |
| 90 | + // Resetting visited array for BFS |
| 91 | + for (int i = 0; i < numVertices; ++i) { |
| 92 | + graph.visited[i] = 0; |
| 93 | + } |
| 94 | + |
| 95 | + printf("\n\nBreadth-First Search (BFS) starting from vertex 0:\n"); |
| 96 | + bfs(&graph, 0); |
| 97 | + |
| 98 | + return 0; |
| 99 | +} |
0 commit comments