0% found this document useful (0 votes)
2 views

ADA REPORT 032

The document is a pedagogy report on Dijkstra's Algorithm, detailing its principles, implementation in Java, and applications in various fields such as navigation and network routing. It covers the algorithm's working mechanism, time complexity, and advantages and limitations. The report concludes that Dijkstra's Algorithm is essential for finding the shortest path in weighted graphs, though it has limitations regarding negative weights and memory usage.

Uploaded by

megha200520
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)
2 views

ADA REPORT 032

The document is a pedagogy report on Dijkstra's Algorithm, detailing its principles, implementation in Java, and applications in various fields such as navigation and network routing. It covers the algorithm's working mechanism, time complexity, and advantages and limitations. The report concludes that Dijkstra's Algorithm is essential for finding the shortest path in weighted graphs, though it has limitations regarding negative weights and memory usage.

Uploaded by

megha200520
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/ 12

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

“JnanaSangama”, Machhe, Belagavi, Karnataka-590018

2024 – 2025
Pedagogy Report
On

“Analysis and Design of Algorithms”

Submitted in partial fulfilment of the requirements for the award of the degree of

Bachelor of Engineering
in
Computer Science and Engineering
Submitted by

Megha.R

(4VM23CS032)

4th Semester
Under the Supervision of
Prof. Vinutha N
Assistant Professor
Dept. of CSE, VVIET

DEPARTMENT OF COMPUTER SCIENCE


VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY

1
#127-128, Mysore - Bannur Road, Alanahally, Mysuru, Karnataka 570028

TABLE OF CONTENTS:

SL CONTENTS PAGE
NO NO

01 INTRODUCTION 01

02 WORKING PRINCIPLE 02

03 DIKSTRA’s ALGORITHM 03

04 TIME AND SPACE COMPLEXITY 04

05 EXAMPLE PROBLEM 05

06 JAVA PROGRAM 06-08

07 APPLICATIONS, ADVANTAGES & 08-09


LIMITATIONS

08 CONCLUSION 10

2
DESIGN AND IMPLEMENTATION OF DIJKSTRA’s ALGORITHM IN
JAVA.

INTRODUCTION:
DIJKSTRA’s ALGORITHM: Graph theory is a vital branch of discrete mathematics that deals with
the study of graphs—structures made up of vertices (also called nodes) and edges (lines connecting the
nodes). Graphs can be classified as directed or undirected, and weighted or unweighted, depending on the
nature of the connections. They are widely used to model relationships and networks in various fields such
as computer science, transportation, telecommunications, and social networks. In the realm of computer
science, algorithms—defined as step-by-step procedures to solve problems—play a key role in analyzing
and manipulating such graph structures efficiently.
One of the most important algorithms in graph theory is Dijkstra’s algorithm, developed by Edsger W.
Dijkstra in 1956. It is designed to compute the shortest paths from a single source vertex to all other vertices
in a weighted, connected graph with non-negative edge weights. The algorithm operates using a greedy
approach and often employs a priority queue to select the next vertex with the minimum tentative distance.
Due to its efficiency and reliability, Dijkstra’s algorithm is widely used in real-world applications such as
GPS navigation systems, network routing, and other optimization tasks involving pathfinding.

DEFINITION: Dijkstra’s Algorithm is a shortest path algorithm used to find the minimum distance
between a source vertex and all other vertices in a weighted, connected graph. It is applicable only when all
edge weights are non-negative. The result is a shortest-path tree rooted at the source vertex, where the path
to each vertex is the shortest possible based on the given edge weights.

DESCRIPTION: Dijkstra’s algorithm operates on a graph by initializing the distance to all vertices as
infinity, except the source vertex, which is set to zero. It then explores the graph by selecting the vertex with
the smallest known distance and relaxing the edges (i.e., updating the distances to its neighboring vertices if
shorter paths are found through it). This process continues until the shortest distance to all vertices is
determined. The algorithm typically uses a priority queue (such as a min-heap) to efficiently retrieve the
vertex with the smallest tentative distance. The algorithm is efficient, with a time complexity of O((V + E)
log V) when implemented using a binary heap and adjacency list. It is widely used in real-world applications
such as GPS systems, internet routing, and network optimization.

3
GRAPH REPRESENTATIONS IN JAVA:
In Java, graphs can be represented in multiple ways, depending on the requirements of the algorithm and the
nature of the graph. The two most common representations are:
Adjacency Matrix
An adjacency matrix is a 2D array of size V x V, where V is the number of vertices in the graph. Each cell
matrix[i][j] stores the weight of the edge from vertex i to vertex j. If no edge exists, the cell is filled with a
default value (often 0 or Integer.MAX_VALUE for infinity). This representation is easy to implement but
consumes more memory, especially for sparse graphs.
Adjacency List
An adjacency list is more efficient for sparse graphs. It is implemented using an array or ArrayList of lists,
where each index corresponds to a vertex, and each list at that index contains Node or Edge objects
representing neighboring vertices and edge weights. This representation is memory-efficient and faster for
traversals and is preferred for implementing Dijkstra’s algorithm in Java.

WORKING PRINCIPLE OF DIJKSTRA’s ALGORITHM:


For a weighted, connected graph G(V, E) with n vertices:
1.Initialize a distance array dist[] with infinity for all vertices, except the source vertex s which is set to 0.
2.Create a visited set S to keep track of vertices whose minimum distances from the source are finalized.
3.While there are still unvisited vertices:
1. Select the vertex u not in S with the smallest tentative distance dist[u].
2. Add u to the set S.
3. For each neighbor v of u, update the distance using the formula:
4. dist[v] = min(dist[v], dist[u] + weight(u, v))
5. This checks if the path from the source to v through u is shorter than the currently known distance.
4.Repeat the process until all vertices have been added to S or the shortest paths are found.
This approach ensures that the shortest path from the source to every other vertex is computed efficiently
using a greedy strategy.

4
DIKSTRA’s ALGORITHM:
Input: A weighted, connected graph G(V, E) and a source vertex s
Output: Shortest distance from source s to all other vertices in V
1.Initialize:
• Let dist[] be an array of size |V|, initialized to ∞ (infinity) for all vertices except dist[s] = 0.
• Create a set or priority queue Q that contains all vertices in V.
2.While Q is not empty:
• Extract the vertex u from Q with the minimum dist[u].
• For each neighbor v of u:
• If dist[v] > dist[u] + weight(u, v):
• Update dist[v] = dist[u] + weight(u, v) (this is called relaxation).
• Mark u as visited (i.e., do not revisit it).
3.Repeat step 2 until all vertices have been visited or the shortest distances are finalized.
4.Output:
• The array dist[] now contains the shortest path distances from source s to all vertices.

JAVA CODE ALGORITHM FOR DIJKSTRA’s ALGORITHM


for (int i = 0; i < n; ++i)
dist[i] = Integer.MAX_VALUE; // Initialize distances to infinity
dist[source] = 0; // Distance to source is 0
for (int i = 0; i < n - 1; ++i) {
int u = -1;
for (int j = 0; j < n; ++j)
if (!processed[j] && (u == -1 || dist[j] < dist[u])) u = j;
processed[u] = true; // Mark u as processed
for (int v = 0; v < n; ++v)
if (!processed[v] && graph[u][v] != Integer.MAX_VALUE) // Update neighbors
if (dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v]; }

5
TIME COMPLEXITY FOR DIJKSTRA’s ALGORITHM:
1. Using a Simple Array / Linear Search (as in your pattern)
Time Complexity:
1. Selecting the minimum distance node: O(n) for each of the n nodes → total O(n²)
2. Updating all neighbors: takes O(n) in each iteration
→ Total Time Complexity: O(n²)
Space Complexity:
1. Distance array: O(n)
2. Processed (visited) array: O(n)
→ Total Space Complexity: O(n)

2. Using a Min-Heap / Priority Queue (Efficient implementation)


Time Complexity:
1. Each extract-min operation: O(log n) (heap)
2. Each edge relaxation (update): O(log n) and happens up to E times
→ Total Time Complexity: O((n + E) log n)
Space Complexity:
1. Distance array: O(n)
2. Priority queue: O(n)
→ Total Space Complexity: O(n)

6
EXAMPLE PROBLEM:
Problem:
Given a graph with 5 vertices and the following edge weights, find the shortest distances from source vertex
0 to all other vertices using Dijkstra’s Algorithm.
GRAPH:
(0)---10---(1)
|\ |
4 5 2
| \ |
(2)---3---(3)---6---(4)

ADJACENCY MATRIX:
0 1 2 3 4
0 [0, 10, 4, 5, ∞]
1 [10, 0, ∞, 2, ∞]
2 [4, ∞, 0, 3, ∞]
3 [5, 2, 3, 0, 6]
4 [∞, ∞, ∞, 6, 0]
From Source =0:

Step ChosenNode Updated Distances

1 0 dist = [0, 10, 4, 5, ∞]


2 2 dist = [0, 10, 4, 5, ∞]
3 3 dist = [0, 8, 4, 5, 11]
4 1 dist = [0, 8, 4, 5, 11]
5 4 dist = [0, 8, 4, 5, 11]

7
JAVA PROGRAM FOR PROBLEM :
import java.util.*;
public class DijkstraExample {
static final int INF = Integer.MAX_VALUE;

public static void dijkstra(int[][] graph, int source) {


int n = graph.length;
int[] dist = new int[n];
boolean[] visited = new boolean[n];

// Initialize distances
Arrays.fill(dist, INF);
dist[source] = 0;

for (int i = 0; i < n - 1; i++) {


int u = -1;

// Pick the unvisited node with the smallest distance


for (int j = 0; j < n; j++) {
if (!visited[j] && (u == -1 || dist[j] < dist[u])) {
u = j;
}
}

visited[u] = true;

// Update distances of neighbors


for (int v = 0; v < n; v++) {
if (graph[u][v] != 0 && graph[u][v] != INF && !visited[v]) {
if (dist[u] + graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];

8
}
}
}
}

// Print result
System.out.println("Vertex \t Distance from Source " + source);
for (int i = 0; i < n; i++) {
System.out.println(i + " \t\t " + dist[i]);
}
}
public static void main(String[] args) {
int[][] graph = {
{0, 10, 4, 5, INF},
{10, 0, INF, 2, INF},
{4, INF, 0, 3, INF},
{5, 2, 3, 0, 6},
{INF, INF, INF, 6, 0}
};

dijkstra(graph, 0);
}
}

SAMPLE OUTPUT:
Vertex Distance from Source 0
0 0
1 8
2 4
3 5
4 11

9
APPLICATIONS OF DIKSTRA’s ALGORITHM:
1. Shortest Path in Maps / Navigation Systems
Used in Google Maps, GPS, and other navigation software to find the shortest route between two places.
Example: Finding the quickest driving route from Point A to Point B.
2. Network Routing Protocols
Used in network routing to determine the shortest path for data packets.
Example: OSPF (Open Shortest Path First) uses Dijkstra’s algorithm.
3. Telecommunication & Data Packet Switching
Helps in optimizing call routing and data transfer through the most efficient path in communication
networks.
4. Urban Traffic Systems
Used in traffic light control and road planning to reduce congestion by calculating shortest paths and travel
times.
5. Game Development (AI Pathfinding)
Helps game characters find shortest and obstacle-free paths.
Example: NPC movement in a maze or open-world game.
6. Railway/Flight Route Planning
Airlines or train systems use it to suggest the shortest or cheapest route between stations or cities.
7. Cost Optimization in Logistics
Used in supply chain and transportation industries to minimize delivery costs and time.
8. Robot Motion Planning
Helps autonomous robots find the optimal path to reach a destination safely and efficiently.

ADVANTAGES AND LIMITATIONS OF DIJKSTRA’s ALGORITHM:


Advantages of Dijkstra's Algorithm
1.Guaranteed Optimal Solution
Always finds the shortest path from the source to all nodes (if weights are non-negative).
2.Works for All Graph Types
Can be applied to both directed and undirected graphs.
3.Simple and Understandable
Conceptually easy to implement using arrays or heaps.
4.Efficient with Min-Heap

10
With priority queues, it performs well on sparse graphs (Time complexity: O((V + E) log V)).
5.Supports Multiple Applications
Used in routing, maps, games, AI pathfinding, etc.

Limitations of Dijkstra's Algorithm


1.Does Not Handle Negative Weights
Fails or gives incorrect results if the graph contains negative edge weights.
2.Inefficient for Dense Graphs (Without Heap)
Time complexity becomes O(V²) with basic implementation.
3.Doesn’t Track All Paths
Only gives the shortest path, not all possible shortest paths or alternate routes.
4.Memory Usage
Storing large graphs (especially as adjacency matrices) consumes a lot of memory.
5.Single-Source Only
Must be run separately from each node to get all-pairs shortest paths (unlike Floyd-Warshall).

11
CONCLUSION:
Dijkstra’s Algorithm is a powerful and widely used technique for finding the shortest path in a weighted
graph with non-negative edges. It plays a crucial role in various real-world applications such as navigation
systems, network routing, and AI pathfinding. The algorithm guarantees optimal solutions and can be
efficiently implemented using priority queues. However, it is not suitable for graphs with negative weights
and can become resource-intensive in dense networks. Despite its limitations, Dijkstra’s Algorithm remains
a fundamental concept in computer science and graph theory, forming the basis for many advanced
algorithms and systems.

12

You might also like