ADA REPORT 032
ADA REPORT 032
2024 – 2025
Pedagogy Report
On
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
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
05 EXAMPLE PROBLEM 05
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.
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.
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)
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:
7
JAVA PROGRAM FOR PROBLEM :
import java.util.*;
public class DijkstraExample {
static final int INF = Integer.MAX_VALUE;
// Initialize distances
Arrays.fill(dist, INF);
dist[source] = 0;
visited[u] = true;
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.
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.
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