0% found this document useful (0 votes)
7 views16 pages

8 Graph

Uploaded by

karanjaiswalf39
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)
7 views16 pages

8 Graph

Uploaded by

karanjaiswalf39
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/ 16

Assignment no.

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

d. Find the degrees of all ver ces


e. For any given vertex print all its neighbors
f. For any given vertex print all the ver ces that are neighbors of its neighbors (but not its
direct neighbors)
g. For any two/three given ver ces print all their common neighbors
h. Find the diameter of the graph
Find all prime numbered ver ces

• 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>

typedef struct Node { int


vertex; struct Node*
next;
} Node;

typedef struct Graph { int


numVer ces; Node* *
adjLists;
} Graph;
// Func on to create a new node
Node* createNode(int vertex) {

Node* newNode = (Node*)malloc(sizeof(Node)); newNode-


>vertex = vertex; newNode->next = NULL; return newNode;

// Func on to create a graph


Graph* createGraph(int ver ces) {

Graph* graph = (Graph*)malloc(sizeof(Graph)); graph->numVer ces =


ver ces; graph->adjLists = (Node**)malloc(ver ces * sizeof(Node*)); for (int
i = O; i < ver ces; i++) { graph->adjLists[i] = NULL;

return graph;

// Func on to add an edge to the graph void


addEdge(Graph* graph, int src, int dest) { Node* newNode
= createNode(dest); newNode->next = graph-
>adjLists[src]; graph->adjLists[src] = newNode;

// Add edge from dest to src as well for undirected graph newNode =
createNode(src); newNode->next = graph->adjLists[dest]; graph-
>adjLists[dest] = newNode;

// Func on to add edges based on divisibility void


addEdgesDivisibility(Graph* graph) { for (int x = 1; x < graph-
>numVer ces; x++) { for (int y = 1; y < graph->numVer ces;
Y++) { if (x != y % y O) {
addEdge(graph, x, y);

// Func on to calculate degree of a vertex int


calculateDegree(Graph* graph, int vertex) { int degree = O;
Node* temp = graph->adjLists[vertex]; while
(temp) { degree++; temp = temp->next;
return degree;

// Func on to print neighbors of a vertex void


printNeighbors(Graph* graph, int vertex) { Node* temp =
graph->adjLists[vertex]; prin ("Neighbors of vertex %d: ",
vertex); while (temp) { prin ("%d ", temp->vertex); temp =
temp->next;

prin ("\n");

// Func on to print second-degree neighbors void printSecondDegreeNeighbors(Graph*


graph, int vertex) { bool* firstDegree = (bool*)calloc(graph->numVer ces, sizeof(bool));
Node* temp = graph->adjLists[vertex]; while
(temp) { firstDegree[temp->vertex] = true; temp
= temp->next; prin ("Second-degree neighbors
of vertex %d: ", vertex); temp = graph-
>adjLists[vertex]; while (temp) {
Node* secondNeighbor = graph->adjLists[temp->vertex]; while (secondNeighbor) { if (secondNeighbor-
>vertex != vertex && !firstDegree[secondNeighbor->vertex]) { prin ("%d ", secondNeighbor->vertex);
firstDegree[secondNeighbor->vertex] = true; // To avoid duplicates

secondNeighbor = secondNeighbor->next;

temp = temp->next;

prin ("\n"); free(firstDegree);

// Func on to find common neighbors void printCommonNeighbors(Graph* graph, int


VI, int v2) { bool* neighbors = (bool*)calloc(graph->numVer ces, sizeof(bool));
Node* temp = graph->adjLists[v1]; prin ("Common neighbors of
%d and %d: 'I , VI, V2); while (temp) { neighbors[temp->vertex] =
true; temp = temp->next;
temp = graph->adjLists[v2]; while (temp)
{

if (neighbors[temp->vertex]) { prin ("%d ",

temp->vertex); temp = temp->next;

prin ("\n"); free(neighbors);

// Func on to find all prime-numbered ver ces void findPrimes(int n)


{ bool* isPrime = (bool*)malloc((n + 1) * sizeof(bool)); for (int i = O; i
<= n; i++) isPrime[i] = true; isPrime[O] = isPrime[1] = false;

for (int i = 2; i <= sqrt(n); i++) { if


(isPrime[i]) { for (int j = i * i; j n; j i)
{ isPrime[j] = false;

prin ("Prime-numbered ver ces: ' for (int i


= 1; i n; i++) { if (isPrime[i]) prin ("%d ",
prin ("\n"); free(isPrime);

// Main func on int main() { int N; prin ("Enter the number


of nodes (>IOO): "); scanf("%d", &N);

if (N 100) { prin ("Number of nodes must be greater than 100.\n"); return


1;

Graph* graph = createGraph(N + 1);

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));

// Test other opera ons as needed int vertex;


prin ("Enter a vertex to find its neighbors: ' scanf("%d",
&vertex);
printNeighbors(graph,

prin ("Enter a vertex to find its second-degree neighbors: ' scanf("%d",


&vertex);
printSecondDegreeNeighbors(graph, vertex);

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

Degree of vertex 1: 100


Degree of vertex 2. Degree
of vertex 3: 33 Degree of
vertex 4: 26
Degree of vertex 5: 20 Degree of

vertex 6: 18

Degree of vertex 7: 14

Degree of vertex 8: 14

Degree of vertex 9: 12

Degree of vertex 10: 12

Degree of vertex 11

Degree of vertex 12: 12

Degree of vertex 13: 7


Degree of vertex 14.
Degree of vertex 15.• 8
Degree of vertex 16: 9

Degree of vertex 17: 5


Degree of vertex 18: 9

Degree of vertex 19: 5

Degree of vertex 20.

Degree of vertex 21

Degree of vertex 22.• 6 23: 4

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

Degree of vertex 33: 5


Degree of vertex 34: 4 Degree of
vertex 35. Degree of vertex 36:
9

Degree of vertex 37.• 2


Degree of vertex 38: 4

Degree of vertex 39: 4

Degree of vertex 40.• 8


Degree of vertex 41 Degree of
vertex 42: 8
Degree of vertex 43: 2
Degree of vertex 44.• 6
Degree of vertex 45.• 6
Degree of vertex 46: 4

Degree of vertex 47.• 2

Degree of vertex 48: 10

Degree of vertex 49.• 3


Degree of vertex 50.• 6
Degree of vertex 51

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

Degree of vertex 61: 1


Degree of vertex 62.• 3 Degree of
vertex 63: 5
Degree of vertex 64.• 6
Degree of vertex 65.• 3
Degree of vertex 66: 7

Degree of vertex 67: 1


Degree of vertex 68.• 5
Degree of vertex 69.• 3 Degree of
vertex 70: 7
Degree of vertex 71: 1

Degree of vertex 72: 11

Degree of vertex 73: 1


Degree of vertex 74.• 3 Degree of
vertex 75: 5
Degree of vertex 76.• 5
Degree of vertex 77.• 3
Degree of vertex 78: 7

Degree of vertex 79: 1

Degree of vertex 80. 81

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

Degree of vertex 96: 11

Degree of vertex 97: 1 Degree of


vertex 98.• 5
Degree of vertex 99: 5 Degree of
vertex 100: 8
Degree of vertex 101: 1
Enter a vertex to find its neighbors:
Part 2
#include <stdio.h> #include
<stdlib.h> #include <stdbool.h>
#include <limits.h>

// Node structure for adjacency list typedef


struct Node { int vertex; int weight; struct
Node* next; } Node;

// Graph structure typedef


struct Graph { int
numVer ces; Node* *
adjLists;
} Graph;

// Func on to create a new node


Node* createNode(int vertex, int weight) { Node* newNode =
(Node*)malloc(sizeof(Node)); newNode->vertex = vertex;
newNode->weight = weight; newNode->next = NULL; return
newNode;

// Func on to create a graph


Graph* createGraph(int ver ces) {

Graph* graph = (Graph*)malloc(sizeof(Graph)); graph->numVer ces =


ver ces; graph->adjLists = (Node**)malloc(ver ces * sizeof(Node*)); for
(int i = O; i < ver ces; i++) { graph->adjLists[i] = NULL;

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;

// Recursive Depth-First Search (DFS) void recursiveDFS(Graph* graph, int


vertex, bool visited[]) { visited[vertex] = true; prin ("%d ", vertex);

Node* temp = graph->adjLists[vertex]; while


(temp) { int adjVertex = temp->vertex;
if (!visited[adjVertex]) { recursiveDFS(graph, adjVertex,
visited);

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;

while (top int vertex =


stack[top--]; if
(!visited[vertex]) {
visited[vertex] = true;
prin ("%d ", vertex);

Node* temp = graph->adjLists[vertex]; while


(temp) { if (!visited[temp->vertex]) { stack[++top]
= temp->vertex;

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;

visited[startVertex] = true; queue[rear++] =


startVertex;

while (front != rear) { int vertex =


queue[front++]; prin ("%d ",
vertex);

Node* temp = graph->adjLists[vertex]; while


(temp) { if (!visited[temp->vertex]) {
queue[rear++] = temp->vertex; visited[temp-
>vertex] = true;

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));

for (int i = O; i < graph->numVer ces,


distances[i] = INT MAX; visited[i] = false;

distances[startVertex] = O;

for (int i = O; i < graph->numVer ces - 1; i++) { int minDist


= INT MAX; int minVertex = -1;
for (int v = O; v < graph->numVer ces; v++) { if
(!visited[v] && distances[v] < minDist) { minDist =
distances[v]; minVertex = v;

visited[minVertex] = true;

Node* temp = graph->adjLists[minVertex]; while (temp) {


int altDist = distances[minVertex] + temp->weight; if (altDist <
distances[temp->vertex]) { distances[temp->vertex] = altDist;

temp = temp->next;

prin ("Vertex\tDistance from Source\n"); for (int i = O; i < graph-


>numVer ces; i++) { prin ("%d\t%d\n", i, distances[i]);

free(dista nces); free(visited);

// Main func on to run all


algorithms int main() { int ver ces =
5;

Graph* graph = createGraph(ver ces);

// Adding edges (Example Graph)

addEdge(graph, addEdge(graph,
o, addEdge(graph,

addEdge(graph, addEdge(graph,

2,

addEdge(graph, 3,

// Running DFS (Recursive) prin ("DFS (Recursive) star ng from


vertex O: "); bool* visited = (bool*)calloc(ver ces, sizeof(bool));
recursiveDFS(graph, O, visited); prin ("\n");

// Running DFS (Itera ve) prin ("DFS (Itera ve) star ng


from vertex O: ' itera veDFS(graph, O); prin ("\n");

// Running BFS prin ("BFS star ng from vertex


O: ' bfs(graph, O); prin ("\n");

// Running Dijkstra's Algorithm prin ("Dijkstra's Algorithm (Shortest Path) star ng

from vertex dijkstra(graph, O);

// Free allocated memory


for (int i = O; i < ver ces; i++) { Node* temp
= graph->adjLists[i]; while (temp) {
Node* toFree = temp; temp =
temp->next;
free(toFree);

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

=== Code Execu on Successful

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy