0% found this document useful (0 votes)
9 views2 pages

Experiment No 10

Gjk
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)
9 views2 pages

Experiment No 10

Gjk
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/ 2

Experiment No.

10
Name:- Geetanjali Jotiram Deshmukh Roll No:- CS2055
Code:-

#include <stdio.h> // Create an array of adjacency


#include <stdlib.h> lists. Size of array will be `vertices`
graph->adjLists = (struct
// Define a structure for an Node**)malloc(vertices *
adjacency list node sizeof(struct Node*));
struct Node {
int vertex; // Initialize each adjacency list as
struct Node* next; empty by making all heads NULL
}; for ( i = 0; i < vertices; i++) {
graph->adjLists[i] = NULL;
// Define a structure for an }
adjacency list return graph;
struct Graph { }
int numVertices;
struct Node** adjLists; // Array
// Function to add an edge to an
of adjacency lists undirected graph
}; void addEdge(struct Graph*
graph, int src, int dest) {
// Function to create a node // Add edge from src to dest
struct Node* createNode(int v) { struct Node* newNode =
struct Node* newNode = (struct createNode(dest);
Node*)malloc(sizeof(struct newNode->next = graph-
Node));
newNode->vertex = v; >adjLists[src];
newNode->next = NULL; graph->adjLists[src] = newNode;
return newNode;
} // Since it's an undirected graph,
add edge from dest to src as well
// Function to create a graph newNode = createNode(src);
struct Graph* createGraph(int newNode->next = graph-
vertices) { >adjLists[dest];
int i; graph->adjLists[dest] =
struct Graph* graph = (struct newNode;
Graph*)malloc(sizeof(struct }
Graph)); // Function to print the graph
graph->numVertices = vertices; void printGraph(struct Graph*
graph) {

int v; addEdge(graph, 0, 1);


for ( v = 0; v < graph- addEdge(graph, 0, 4);
>numVertices; v++) { addEdge(graph, 1, 2);
struct Node* temp = graph- addEdge(graph, 1, 3);
>adjLists[v]; addEdge(graph, 1, 4);
printf("Vertex %d: ", v); addEdge(graph, 2, 3);
while (temp) { addEdge(graph, 3, 4);
printf("%d -> ", temp-
>vertex); printf("\nGeetanjali
temp = temp->next; Deshmukh");
} printf("\nCS2055");
printf("NULL\n"); // Print the adjacency list
} representation of the graph
} printGraph(graph);

// Main function to test the graph return 0;


implementation }
int main() {
int vertices = 5;
struct Graph* graph =
createGraph(vertices);

You might also like