Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

This code is an implementation of Dijkstra's algorithm for finding the shortest path in a graph.

Let's go
through the code line by line to understand its functionality:

Copy code

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#include <limits.h>

#define MAX_NODES 100

These lines include necessary header files and define a constant MAX_NODES as the maximum
number of nodes in the graph.

typedef struct {

int vertex;

int weight;

} Edge;

This defines a structure Edge that represents an edge in the graph, consisting of a destination vertex
and a weight.

typedef struct {

Edge* edges;

int numEdges;

} Graph;

This defines a structure Graph that represents the entire graph. It contains an array of edges (edges)
and the number of edges (numEdges) for each node.

Graph* createGraph(int numNodes) {

Graph* graph = (Graph*)malloc(sizeof(Graph));


graph->nodes = (GraphNode*)malloc(numNodes * sizeof(GraphNode));

graph->numNodes = numNodes;

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

graph->nodes[i].edges = NULL;

graph->nodes[i].numEdges = 0;

return graph;

This function creates a graph with the given number of nodes and initializes each node with no edges.

void addEdge(Graph* graph, int src, int dest, int weight) {

// Update source node

graph->nodes[src].edges = (Edge*)realloc(graph->nodes[src].edges, (graph->nodes[src].numEdges + 1)


* sizeof(Edge));

graph->nodes[src].edges[graph->nodes[src].numEdges].vertex = dest;

graph->nodes[src].edges[graph->nodes[src].numEdges].weight = weight;

graph->nodes[src].numEdges++;

// Update destination node

graph->nodes[dest].edges = (Edge*)realloc(graph->nodes[dest].edges, (graph->nodes[dest].numEdges


+ 1) * sizeof(Edge));

graph->nodes[dest].edges[graph->nodes[dest].numEdges].vertex = src;

graph->nodes[dest].edges[graph->nodes[dest].numEdges].weight = weight;

graph->nodes[dest].numEdges++;

This function adds an edge to the graph by updating both the source and destination nodes with the
given weight.

int findMinDistanceNode(bool visited[], int dist[], int numNodes) {

int minDist = INT_MAX;


int minIndex = -1;

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

if (visited[i] && dist[i] < minDist) {

minDist = dist[i];

minIndex = i;

return minIndex;

This function finds the index of the node with the minimum distance value among the nodes that have
not been visited.

void dijsktra(Graph* graph, int src, int dest) {

int dist[MAX_NODES]; // Distance from source to each node

int prev[MAX_NODES]; // Previous node in the shortest path

bool visited[MAX_NODES]; // Array to keep track of visited nodes

// Initialize distances and visited array

for (int i = 0; i < graph->numNodes; i++) {

dist[i] = INT_MAX;

prev[i] = -1;

visited[i] = false;

dist[src] = 0; // Distance from source to itself is 0

// Find shortest path for all nodes

for (int i = 0; i < graph->numNodes - 1; i++) {

int u = findMinDistanceNode(visited, dist, graph->numNodes);

visited[u] = true;
// Update distances of adjacent nodes

for (int j = 0; j < graph->nodes[u].numEdges; j++) {

int v = graph->nodes[u].edges[j].vertex;

int weight = graph->nodes[u].edges[j].weight;

if (!visited[v] && dist[u] != INT_MAX && dist[u] + weight < dist[v]) {

dist[v] = dist[u] + weight;

prev[v] = u;

// Print the shortest path

printf("Shortest path from node %d to node %d: ", src, dest);

if (dist[dest] != INT_MAX) {

int currentNode = dest;

printf("%d", currentNode);

while (currentNode != src) {

printf("<-%d", prev[currentNode]);

currentNode = prev[currentNode];

printf("\nTotal distance: %d\n", dist[dest]);

} else {

printf("Path does not exist.\n");

This function implements Dijkstra's algorithm to find the shortest path from the source node to the
destination node in the graph.

int main() {
int numNodes = 7;

Graph* graph = createGraph(numNodes);

// Adding edges to the graph

addEdge(graph, 0, 1, 2);

addEdge(graph, 0, 2, 4);

addEdge(graph, 1, 3, 6);

addEdge(graph, 2, 4, 3);

addEdge(graph, 3, 4, 1);

addEdge(graph, 3, 5, 5);

addEdge(graph, 4, 6, 4);

addEdge(graph, 5, 6, 2);

// Running Dijkstra’s algorithm to find the shortest path

int source = 0;

int destination = 6;

dijsktra(graph, source, destination);

return 0;

This is the main function where a graph is created, edges are added, and Dijkstra's algorithm is applied
to find the shortest path from a source node to a destination node. The result is printed in the console.
Note that there are some typos in the code (e.g., "Int" instead of "int", "Void" instead of "void", etc.),
and these should be corrected for the code to compile successfully.

You might also like