0% found this document useful (0 votes)
19 views3 pages

Cyber Expr1

The document outlines an experiment focused on finding the shortest path for data travel using Dijkstra's algorithm. It explains the theory behind shortest path algorithms and provides a C program that implements Dijkstra's algorithm to determine the shortest paths from a source vertex to all other vertices in a graph. The results show the distances from the source vertex to each vertex in the graph.
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)
19 views3 pages

Cyber Expr1

The document outlines an experiment focused on finding the shortest path for data travel using Dijkstra's algorithm. It explains the theory behind shortest path algorithms and provides a C program that implements Dijkstra's algorithm to determine the shortest paths from a source vertex to all other vertices in a graph. The results show the distances from the source vertex to each vertex in the graph.
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/ 3

EXPERIMENT NO.

OBJECTIVE:- Program to find shortest path for the most efficient route for data travel.

THEORY:- In networking, shortest path algorithms help in determining the most efficient path
for data packets from one node (or device) to another in a network. These algorithms are crucial
for routing in both small and large networks.

(1) Dijkstra's Algorithm:-Dijkstra's algorithm finds the shortest path from a single source vertex
to all other vertices in a graph with non-negative edge weights.

(2)Warshall's Algorithm:- Warshall's algorithm computes the transitive closure of a directed


graph, determining if there is a path between any pair of vertices.

PROGRAM:- shortest path using Dijkstra's algorithm.

#include <stdio.h> #include <limits.h>

int minDistance(int dist[], int sptSet[])

{ int min = INT_MAX, min_index;

for (int v = 0; v < V; v++) { if

(sptSet[v] == 0 && dist[v] <= min) {

min = dist[v]; min_index = v; }

} return min_index;

void dijkstra(int graph[V][V], int src) {

int dist[V]; int

sptSet[V]; for (int i =


0; i < V; i++) {

dist[i] = INT_MAX;

sptSet[i] = 0;

dist[src] = 0; for (int count = 0; count < V - 1; count++) { int u = minDistance(dist,

sptSet); sptSet[u] = 1; for (int v = 0; v < V; v++) { if (!sptSet[v] &&

graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) { dist[v]

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

printf("Vertex \t Distance from Source\n");

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

printf("%d \t %d\n", i, dist[i]);

int main() { int

graph[V][V] = {

{0, 10, 0, 0, 5},

{10, 0, 10, 30, 0},

{0, 10, 0, 50, 0},

{0, 30, 50, 0, 20},


{5, 0, 0, 20, 0}

};

dijkstra(graph, 0);

return 0;

RESULT:- we found shortest path i.e.

Vertex Distance from Source

0 0

1 10

2 20

3 40

4 5

You might also like