0% found this document useful (0 votes)
2 views9 pages

Ada Report 2

Dijkstra's Algorithm is a graph traversal technique for finding the shortest paths from a source vertex to all other vertices in a weighted graph with non-negative edge weights. It is classified as a greedy algorithm and has various applications in routing, navigation, and optimization. The document includes an algorithm description, a program implementation in C, and a mathematical analysis of its time and space complexities.

Uploaded by

hemanthpushpa808
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Ada Report 2

Dijkstra's Algorithm is a graph traversal technique for finding the shortest paths from a source vertex to all other vertices in a weighted graph with non-negative edge weights. It is classified as a greedy algorithm and has various applications in routing, navigation, and optimization. The document includes an algorithm description, a program implementation in C, and a mathematical analysis of its time and space complexities.

Uploaded by

hemanthpushpa808
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

CONTENTS:

1. INTRODUCTION
2. ALGORITHM AND FLOWCHART
3. PROGRAM
4. MATHEMATICAL ANALYSIS
5. INPUT AND OUTPUT SPECIFICATION
6. CODE DEMONTRATION
DIJKSTRA algorithm
INTRODUCTION:
Dijkstra’s Algorithm is a fundamental graph traversal technique used to find the shortest path
between a source vertex and all other vertices in a weighted graph with non-negative edge
weights. Developed by Edsger W. Dijkstra in 1956, this algorithm is widely used in routing and
navigation systems, network optimization, and AI pathfinding.
In the context of Analysis and Design of Algorithms, Dijkstra’s Algorithm is classified as a
greedy algorithm because it makes the locally optimal choice at each step by selecting the node
with the minimum tentative distance. It gradually builds the shortest path tree by updating the
shortest distance from the source to all other nodes.
Its importance lies in its efficiency and practical relevance for problems modeled as weighted
graphs, such as road networks, data routing, and scheduling.
The single-source shortest-paths problem:
for a given vertex called the source in a weighted connected graph, find shortest paths to all its
other vertices. It is important to stress that we are not interested here in asingle shortest path that
starts at the source and visits all the other vertices. This would have been a much more difficult
problem .The single-source shortest-paths problem asks for a family of paths, each leading from
the source to a different vertex in the graph, though some paths may, of course, have edges in
common.
A variety of practical applications of the shortest-paths problem have made the problem a very
popular object of study. The obvious but probably most widely used applications are
transportation planning and packet routing in communication networks, including the Internet.
Multitudes of less obvious applications include finding shortest paths in social networks, speech
recognition, document formatting, robotics, compilers, and airline crew scheduling. In the world
of entertainment , one can mention pathfinding in video games and finding best solutions to
puzzles using their state-space graphs .
This algorithm is applicable to undirected and directed graphs with nonnegative weights only.
Since in most applications this condition is satisfied, the limitation has not impaired popularity of
Dijkstra’s algorithm.
ALGORITHM:
//Dijkstra’s algorithm for single-source shortest paths
//Input: A weighted connected graph G = V,Ewith nonnegative weigh// and its vertex s
//Output: The length dv of a shortest path from s to v
// and its penultimate vertex pv for every vertex v in V
Initialize(Q) //initialize priority queue to empty
for every vertex v in V
dv ← ∞; pv ← null
Insert(Q, v, dv) //initialize vertex priority in the priority queue
ds ← 0; Decrease(Q, s, ds) //update priority of s with ds
VT ← ∅
for i ← 0 to |V | − 1 do
u∗ ← DeleteMin(Q) //delete the minimum priority element
VT ← VT ∪ {u∗}
for every vertex u in V − VT that is adjacent to u∗ do
if du∗ + w(u∗, u) < du
du ← du∗ + w(u∗, u); pu ← u∗
Decrease(Q, u, du)
PROBLEM:
PROGRAM:
#include <stdio.h>

void dijkstra(int c[10][10], int n, int s, int d[10]) {

int v[10], i, j, u, min;

for (i = 0; i < n; i++) {

d[i] = c[s][i];

v[i] = 0;

v[s] = 1;

for (i = 1; i < n; i++) {

min = 999;

for (j = 0; j < n; j++) {

if (v[j] == 0 && d[j] < min) {

min = d[j];

u = j;

}v[u] = 1;

for (j = 0; j < n; j++) {

if (v[j] == 0 && (d[u] + c[u][j]) < d[j]) {

d[j] = d[u] + c[u][j];

}
}

d[j] = d[u] + c[u][j];

int main() {

int c[10][10], d[10], i, j, s, sum, n;

printf("\nEnter n values: \n");

scanf("%d", &n);

printf("\nEnter the graph data: \n");

for (i = 0; i < n; i++) {

for (j = 0; j < n; j++) {

scanf("%d", &c[i][j]);

printf("\nEnter the source node: \n");

scanf("%d", &s);

dijkstra(c, n, s, d);

for (i = 0; i < n; i++) {

printf("\nShortest distance from node %d to %d is %d", s, i, d[i]);

return 0;

}
OUTPUT:
Enter n values:
4
Enter the graph data:
0 1 999 3
1 0 1 999
999 1 0 1
3 999 1 0

Enter the source node:


0

Code Demonstration:
🧮 Step-by-Step Execution:

We start from node 0, so s = 0.


1. Initialize:
CopyEdit
d[] = {0, 1, 999, 3}
v[] = {1, 0, 0, 0}
2. Find min in unvisited → node 1 (d[1] = 1)
o Mark 1 visited: v[1] = 1

o Update:

 Node 2: d[2] = min(999, 1 + 1) = 2


3. Find min in unvisited → node 2 (d[2] = 2)
o Mark 2 visited

o Update:

 Node 3: d[3] = min(3, 2 + 1) = 3 (no change)


4. Find min in unvisited → node 3 (d[3] = 3)
o Mark 3 visited

MATHEMATICAL ANALYSIS:
Mathematical Formula:

d[v]=min(d[v],d[u]+w(u,v))for all (u,v)∈E

Where d[v]d[v]d[v] is the current known shortest distance to vertex vvv.

Case Graph Type Time Complexity (Binary Heap)

Best Sparse Graph (few edges) O((n+m)log⁡n)O((n + m) \log n)

Worst Dense Graph (m=n2m = n^2) O(n2log⁡n)O(n^2 \log n)

Average General Graph O((n+m)log⁡n)O((n + m) \log n)

Time Complexity Analysis:


a) Without Priority Queue (Simple Array)
 Selecting the minimum distance vertex: O(n)O(n)O(n) per selection

 Total selections: O(n2)O(n^2)O(n2)

 Relaxation: O(m)O(m)O(m)

 Total Time: O(n2+m)=O(n2)O(n^2 + m) = O(n^2)O(n2+m)=O(n2)


b) Using Binary Heap
 Insert / Extract-Min: O(log⁡n)O(\log n)O(logn)

 For nnn vertices: O(nlog⁡n)O(n \log n)O(nlogn)

 For mmm edges (relaxation): O(mlog⁡n)O(m \log n)O(mlogn)

 Total Time: O((n+m)log⁡n)O((n + m) \log n)O((n+m)logn)


c) Using Fibonacci Heap (theoretical)
 Extract-Min: O(log⁡n)O(\log n)O(logn)

 Decrease-Key: O(1)O(1)O(1) amortized

 Total Time: O(nlog⁡n+m)O(n \log n + m)O(nlogn+m)


4. Space Complexity
 Distance array d[]d[ ]d[]: O(n)O(n)O(n)
 Predecessor array π[]\pi[ ]π[]: O(n)O(n)O(n)
 Priority queue: O(n)O(n)O(n)
 Total Space: O(n+m)O(n + m)O(n+m)

You might also like