Rotate your phone or change to desktop for better experience

Rotate your phone or change to desktop for better experience

1. Write C program that accepts the vertices and edges for a graph and stores it as an adjacency matrix. || DAA

 #include <stdio.h> 

#define MAX_VERTICES 100  

typedef struct { 

int adjacencyMatrix[MAX_VERTICES][MAX_VERTICES]; 

int numVertices; 

} Graph; 

void initGraph(Graph* g, int numVertices) { 

g->numVertices = numVertices; 

for (int i = 0; i < numVertices; i++) { 

for (int j = 0; j < numVertices; j++) { 

g->adjacencyMatrix[i][j] = 0; 

void addEdge(Graph* g, int start, int end) { 

g->adjacencyMatrix[start][end] = 1; 

}  

void displayGraph(Graph* g) { 

printf("Adjacency Matrix:\n"); 

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

for (int j = 0; j < g->numVertices; j++) { 

printf("%d ", g->adjacencyMatrix[i][j]); 

printf("\n");

int main() { Graph g; 

int numVertices, numEdges; 

printf("Enter the number of vertices: "); 

scanf("%d", &numVertices); 

initGraph(&g, numVertices); 

printf("Enter the number of edges: "); 

scanf("%d", &numEdges); 

printf("Enter the edges (start vertex end vertex):\n"); 

for (int i = 0; i < numEdges; i++) { 

int start, end;  

scanf("%d %d", &start, &end); 

addEdge(&g, start, end); 

displayGraph(&g); 

return 0; 

}

Post a Comment

0 Comments