0% found this document useful (0 votes)
11 views3 pages

Ds 20

Uploaded by

anavadyapradeep
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Ds 20

Uploaded by

anavadyapradeep
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Program

/* Experiment No.20 Graph


Write a program to represent any given graph and to do the
following
operations:
a. Compute adjacency list and adjacency matrix.
b. Perform a depth first search
c. Perform a breadth first search

17.Anavadya Pradeep.04/11/2024 */

#include <stdio.h>
#include <stdlib.h>

#define MAX_NODES 100

typedef struct Graph {


int adj[MAX_NODES][MAX_NODES]; // Adjacency matrix
int num_nodes; // Number of nodes
} Graph;

// Function to create a graph


Graph* create_graph(int nodes) {
Graph* g = (Graph*)malloc(sizeof(Graph));
g->num_nodes = nodes;

// Initialize adjacency matrix to 0


for (int i = 0; i < nodes; i++) {
for (int j = 0; j < nodes; j++) {
g->adj[i][j] = 0;
}
}
return g;
}

// Function to add an edge to the graph


void add_edge(Graph* g, int u, int v) {
g->adj[u][v] = 1; // For directed graph
g->adj[v][u] = 1; // Uncomment for undirected graph
}

// Function to print the adjacency list


void print_adjacency_list(Graph* g) {
printf("Adjacency List:\n");
for (int i = 0; i < g->num_nodes; i++) {
printf("%d: ", i);
for (int j = 0; j < g->num_nodes; j++) {
if (g->adj[i][j]) {
printf("%d ", j);
}
}
printf("\n");
}
}

// Function to print the adjacency matrix


void print_adjacency_matrix(Graph* g) {
printf("Adjacency Matrix:\n");
for (int i = 0; i < g->num_nodes; i++) {
for (int j = 0; j < g->num_nodes; j++) {
printf("%d ", g->adj[i][j]);
}
printf("\n");
}
}
// Depth First Search
void dfs(Graph* g, int start, int visited[]) {
visited[start] = 1;
printf("%d ", start);

for (int i = 0; i < g->num_nodes; i++) {


if (g->adj[start][i] && !visited[i]) {
dfs(g, i, visited);
}
}
}

// Breadth First Search


void bfs(Graph* g, int start) {
int visited[MAX_NODES] = {0};
int queue[MAX_NODES];
int front = 0, rear = 0;

visited[start] = 1;
queue[rear++] = start;

while (front < rear) {


int vertex = queue[front++];
printf("%d ", vertex);

for (int i = 0; i < g->num_nodes; i++) {


if (g->adj[vertex][i] && !visited[i]) {
visited[i] = 1;
queue[rear++] = i;
}
}
}
}

int main() {
int nodes = 5; // Number of nodes
Graph* g = create_graph(nodes);

// Adding edges
add_edge(g, 0, 1);
add_edge(g, 0, 2);
add_edge(g, 1, 2);
add_edge(g, 1, 3);
add_edge(g, 2, 4);

print_adjacency_list(g);
printf("\n");
print_adjacency_matrix(g);
printf("\nDepth First Search (starting from vertex 0):\n");

int visited[MAX_NODES] = {0}; // Initialize visited array for DFS


dfs(g, 0, visited);

printf("\n\nBreadth First Search (starting from vertex 0):\n");


bfs(g, 0);
printf("\n");
// Clean up
free(g);
return 0;
}
Sample Output

Result
The program was executed and the output verified.

You might also like