From ca231e43fe73e9be2df0d18cdd38f5de62dc71dd Mon Sep 17 00:00:00 2001 From: AMBATI KRISHNA TEJA Date: Mon, 2 Oct 2023 08:38:31 +0530 Subject: [PATCH] Create DFS.c --- algorithms/c/DFS.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 algorithms/c/DFS.c diff --git a/algorithms/c/DFS.c b/algorithms/c/DFS.c new file mode 100644 index 0000000..75c2bf3 --- /dev/null +++ b/algorithms/c/DFS.c @@ -0,0 +1,64 @@ +#include +#include + +#define MAX_VERTICES 100 + +// Structure to represent a graph +struct Graph { + int vertices; + int** adjMatrix; + int* visited; +}; + +// Function to create a new graph with 'vertices' number of vertices +struct Graph* createGraph(int vertices) { + struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph)); + graph->vertices = vertices; + + graph->adjMatrix = (int**)malloc(vertices * sizeof(int*)); + graph->visited = (int*)malloc(vertices * sizeof(int)); + + for (int i = 0; i < vertices; i++) { + graph->adjMatrix[i] = (int*)malloc(vertices * sizeof(int)); + for (int j = 0; j < vertices; j++) { + graph->adjMatrix[i][j] = 0; + } + graph->visited[i] = 0; + } + + return graph; +} + +// Function to add an edge to the graph +void addEdge(struct Graph* graph, int source, int destination) { + graph->adjMatrix[source][destination] = 1; + // For an undirected graph, you can add the reverse edge as well + // graph->adjMatrix[destination][source] = 1; +} + +// Depth-First Search traversal +void DFS(struct Graph* graph, int vertex) { + printf("%d ", vertex); + graph->visited[vertex] = 1; + + for (int i = 0; i < graph->vertices; i++) { + if (!graph->visited[i] && graph->adjMatrix[vertex][i]) { + DFS(graph, i); + } + } +} + +int main() { + struct Graph* graph = createGraph(6); + + addEdge(graph, 0, 1); + addEdge(graph, 0, 2); + addEdge(graph, 1, 3); + addEdge(graph, 2, 4); + addEdge(graph, 3, 5); + + printf("Depth-First Search starting from vertex 0:\n"); + DFS(graph, 0); + + return 0; +}