8 Graph
8 Graph
8
Title: Graph
• Part 1
1. Graph Representa on: Implement a program to represent a graph using adjacency matrix and
adjacency list representa ons. Provide opera ons for adding edges and ver ces.
2. Create the following (using any graph representa on of your choice)
a. N (number of nodes in graph): Take input from user (should be >100)
b. Add n ver ces to the graph numbered from 1 to N
c. Add an edge between two ver ces x and y if x is divisible by y, for all x, y
• Part 2
1. Depth-First Search (DFS): Write a program to perform depth-first search on a graph. Implement
both recursive and itera ve versions. Print the order in which nodes are visited.
2. Breadth-First Search (BFS): Create a program to perform breadth-first search on a graph. Plint
the order in which nodes are visited.
3. Shortest Path (Dijkstra's Algorithm): Implement Dijkstra's algorithm to find the shortest path
from a source node to all other nodes in a weighted graph. Print the shortest distances.
• Part 1
#include <stdio.h> #include
<stdlib.h>
#include <math.h> #include
<stdbool.h>
return graph;
// Add edge from dest to src as well for undirected graph newNode =
createNode(src); newNode->next = graph->adjLists[dest]; graph-
>adjLists[dest] = newNode;
prin ("\n");
secondNeighbor = secondNeighbor->next;
temp = temp->next;
addEdgesDivisibility(graph);
// Degrees of all ver ces for (int i = 1; i N; i++) { prin ("Degree of vertex %d: %d\n",
i, calculateDegree(graph, i));
int VI, v2; prin ("Enter two ver ces to find their common neighbors: '
scanf("%d %d", &vl, &v2);
printCommonNeighbors(graph, VI, v2);
findPrimes(N);
vertex);
// Free memory for (int i = O; i
N; i++) {
Node* temp = graph->adjLists[i]; while
(temp) { Node* toFree = temp; temp =
temp->next;
free(toFree);
free(graph->adjLists); free(graph);
return O;
OUTPUT
Enter the number of nodes (>IOO): 101
vertex 6: 18
Degree of vertex 7: 14
Degree of vertex 8: 14
Degree of vertex 9: 12
Degree of vertex 11
Degree of vertex 21
24: 10
25: 5
26.• 5
27: 5
Degree of vertex
Degree of vertex
Degree of vertex
Degree of vertex
Degree of vertex
Degree of vertex
Degree of vertex
28: 7
29.• 3
Degree of vertex 30. Degree of
vertex 31 Degree of vertex 32:
7
52.• 5
53: 1
54.
55.• 3
56: 7
57.• 3
58.• 3 Degree
of vertex 59: 1
Degree of vertex
Degree of vertex
Degree of vertex
Degree of vertex
Degree of vertex
Degree of vertex
Degree of vertex
Degree of vertex 60: 11
82.• 3
83: 1
84: 11
85.• 3
86: 3
87.• 3 Degree
of vertex 88: 7
Degree of vertex 89: 1
Degree of vertex 90: 11
Degree of vertex 91
Degree of vertex
Degree of vertex
Degree of vertex
Degree of vertex
Degree of vertex
Degree of vertex
Degree of vertex
Degree of vertex 92.• 5 Degree of
vertex 93: 3
Degree of vertex 94.• 3
Degree of vertex 95.• 3
return graph;
// Func on to add an edge void addEdge(Graph* graph, int src, int dest,
int weight) { Node* newNode = createNode(dest, weight); newNode-
>next = graph->adjLists[src]; graph->adjLists[src] = newNode;
newNode = createNode(src, weight); newNode-
>next = graph->adjLists[dest]; graph-
>adjLists[dest] = newNode;
temp = temp->next;
// Itera ve Depth-First Search (DFS) void itera veDFS(Graph* graph, int startVertex)
{ bool* visited = (bool*)calloc(graph->numVer ces, sizeof(bool)); int* stack =
(int*)malloc(graph->numVer ces * sizeof(int)); int top = -1;
stack[++top] = startVertex;
temp = temp->next;
free(stack); free(visited);
// Breadth-First Search (BFS) void bfs(Graph* graph, int startVertex) { bool* visited
= (bool*)calloc(graph->numVer ces, sizeof(bool)); int* queue = (int*)malloc(graph-
>numVer ces * sizeof(int)); int front = O, rear = O;
temp = temp->next;
free(queue);
free(visited);
// Dijkstra's Algorithm for Shortest Path void dijkstra(Graph* graph, int startVertex) {
int* distances = (int*)malloc(graph->numVer ces * sizeof(int)); bool* visited =
(bool*)malloc(graph->numVer ces * sizeof(bool));
distances[startVertex] = O;
visited[minVertex] = true;
temp = temp->next;
addEdge(graph, addEdge(graph,
o, addEdge(graph,
addEdge(graph, addEdge(graph,
2,
addEdge(graph, 3,
free(graph->adjLists);
free(graph); free(visited);
return O;
OUTPUT
DFS (Recursive) star ng from vertex O: 0 2 4 3 1
DFS (Itera ve) star ng from vertex O: 0 1 24 3
BFS star ng from vertex O: 0 2 1 4 3
Dijkstra's Algorithm (Shortest Path) star ng from vertex O:
Vertex Distance from Source
o o
1 2
2 3
3 8
4 6