0% found this document useful (0 votes)
12 views4 pages

Oep (DS)

..

Uploaded by

kunjiram2004
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)
12 views4 pages

Oep (DS)

..

Uploaded by

kunjiram2004
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/ 4

OPEN ENDED PROJECT

(DATA STRUCTURES)

TOPIC : Graph Traversal And Shortest Path Finder

SUBMITTED BY

1.KUNJIRAM R.K MENON (33)


2.NANDANA C.S (42)
3.NANDHANA BALAN (43)
4.NAVYA V.S (44)
5.SYAMNATH C.V (52)
PROGRAM

#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
#define MAX_NODES 100
#define INF INT_MAX
typedef struct {
int from;
int to;
int weight;
} Edge;
typedef struct {
int numNodes;
int numEdges;
Edge edges[MAX_NODES * MAX_NODES];
} Graph;
void initGraph(Graph *graph, int numNodes) {
graph->numNodes = numNodes;
graph->numEdges = 0;
}
void addEdge(Graph *graph, int from, int to, int weight) {
graph->edges[graph->numEdges].from = from;
graph->edges[graph->numEdges].to = to;
graph->edges[graph->numEdges].weight = weight;
graph->numEdges++;
}
void printShortestPath(int parent[], int destination) {
if (parent[destination] == -1) {
printf("No path to destination.\n");
return;
}
int path[MAX_NODES];
int pathLength = 0;
while (destination != -1) {
path[pathLength++] = destination;
destination = parent[destination];
}
printf("Shortest path: ");
for (int i = pathLength - 1; i >= 0; i--) {
printf("%d ", path[i]);
}printf("\n");
}
void dijkstra(Graph *graph, int source, int destination) {
int distance[MAX_NODES];
int parent[MAX_NODES];
bool visited[MAX_NODES];OUTPUT
for (int i = 0; i < graph->numNodes; i++) {
distance[i] = INF;
parent[i] = -1;
visited[i] = false;
}
distance[source] = 0;
for (int count = 0; count < graph->numNodes - 1; count++) {
int minDistance = INF;
int minIndex = -1;
for (int i = 0; i < graph->numNodes; i++) {
if (!visited[i] && distance[i] < minDistance) {
minDistance = distance[i];
minIndex = i;
}
}
visited[minIndex] = true;
for (int i = 0; i < graph->numEdges; i++) {
if (graph->edges[i].from == minIndex) {
int to = graph->edges[i].to;
int weight = graph->edges[i].weight;
if (!visited[to] && distance[minIndex] + weight < distance[to]) {
distance[to] = distance[minIndex] + weight;
parent[to] = minIndex;
}
}
}
}
printf("Shortest distance from %d to %d: %d\n", source, destination,
distance[destination]);
printShortestPath(parent, destination);
}
int main() {
Graph graph;
int numNodes, numEdges;int source, destination;
printf("Enter the number of nodes in the graph: ");
scanf("%d", &numNodes);
printf("Enter the number of edges in the graph: ");
scanf("%d", &numEdges);
initGraph(&graph, numNodes);
printf("Enter the edges in the format 'from to weight':\n");
for (int i = 0; i < numEdges; i++) {
int from, to, weight;
scanf("%d %d %d", &from, &to, &weight);
addEdge(&graph, from, to, weight);
}
printf("Enter the source node: ");
scanf("%d", &source);
printf("Enter the destination node: ");
scanf("%d", &destination);
dijkstra(&graph, source, destination);
return 0;
}

OUTPUT

You might also like