From d45647dae910bfbbe2e99c910ae08d6cbedaac3d Mon Sep 17 00:00:00 2001 From: H Rohitha Aiswarya Date: Thu, 7 Nov 2024 22:04:58 +0530 Subject: [PATCH 1/2] Create dijkstra-algo.md This PR adds an implementation of Dijkstra's Algorithm in C, C++, Python, and Java --- docs/graphs/dijkstra-algo.md | 242 +++++++++++++++++++++++++++++++++++ 1 file changed, 242 insertions(+) create mode 100644 docs/graphs/dijkstra-algo.md diff --git a/docs/graphs/dijkstra-algo.md b/docs/graphs/dijkstra-algo.md new file mode 100644 index 000000000..6bc9eefbc --- /dev/null +++ b/docs/graphs/dijkstra-algo.md @@ -0,0 +1,242 @@ +--- +id: dijkstra-algo +title: Dijkstra's Algorithm +sidebar_label: Dijkstra's Algorithm +description: "In this blog post, we'll explore Dijkstra's Algorithm, an efficient method to find the shortest path from a source to all other nodes in a graph." +tags: [dsa, algorithms, shortest path] +--- + +# Dijkstra's Algorithm + +Dijkstra's Algorithm is used to find the shortest path from a source node to all other nodes in a weighted graph, where all edge weights are non-negative. It is widely used in network routing and GPS applications. + +## Key Features: +- **Time Complexity**: O(V²) for a simple implementation with an adjacency matrix, or O(E log V) using a priority queue (min-heap). +- **Space Complexity**: O(V), where V is the number of vertices. +- Suitable for graphs with non-negative edge weights. + +## Applications: +- Shortest path calculations in GPS and network routing. +- Optimal path planning in games and simulations. +- Analyzing transportation or logistics networks. + +# Code in C + +```c +#include +#include + +#define V 5 + +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] = {0}; + + for (int i = 0; i < V; i++) + dist[i] = INT_MAX; + 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\tDistance from Source\n"); + for (int i = 0; i < V; i++) + printf("%d\t%d\n", i, dist[i]); +} + +int main() { + int graph[V][V]; + int src; + + printf("Enter the adjacency matrix (use 0 for no direct connection):\n"); + for (int i = 0; i < V; i++) { + for (int j = 0; j < V; j++) { + scanf("%d", &graph[i][j]); + } + } + + printf("Enter the source vertex: "); + scanf("%d", &src); + + dijkstra(graph, src); + return 0; +} +``` + +# Code in Cpp + +```cpp +#include +#include +#include +#include +using namespace std; + +void dijkstra(vector>& graph, int src, int V) { + vector dist(V, INT_MAX); + priority_queue, vector>, greater>> pq; + + dist[src] = 0; + pq.push({0, src}); + + while (!pq.empty()) { + int u = pq.top().second; + pq.pop(); + + for (int v = 0; v < V; v++) { + if (graph[u][v] && dist[u] + graph[u][v] < dist[v]) { + dist[v] = dist[u] + graph[u][v]; + pq.push({dist[v], v}); + } + } + } + + cout << "Vertex\tDistance from Source\n"; + for (int i = 0; i < V; i++) + cout << i << "\t" << dist[i] << "\n"; +} + +int main() { + int V; + cout << "Enter the number of vertices: "; + cin >> V; + vector> graph(V, vector(V)); + + cout << "Enter the adjacency matrix (use 0 for no direct connection):\n"; + for (int i = 0; i < V; i++) + for (int j = 0; j < V; j++) + cin >> graph[i][j]; + + int src; + cout << "Enter the source vertex: "; + cin >> src; + + dijkstra(graph, src, V); + return 0; +} +``` + +# Code in Python + +```python +import heapq + +def dijkstra(graph, src, V): + dist = [float("inf")] * V + dist[src] = 0 + min_heap = [(0, src)] + + while min_heap: + current_distance, u = heapq.heappop(min_heap) + + for v in range(V): + if graph[u][v] > 0 and current_distance + graph[u][v] < dist[v]: + dist[v] = current_distance + graph[u][v] + heapq.heappush(min_heap, (dist[v], v)) + + print("Vertex\tDistance from Source") + for i in range(V): + print(f"{i}\t{dist[i]}") + +V = int(input("Enter the number of vertices: ")) +graph = [] +print("Enter the adjacency matrix (use 0 for no direct connection):") +for i in range(V): + graph.append(list(map(int, input().split()))) + +src = int(input("Enter the source vertex: ")) +dijkstra(graph, src, V) +``` + +# Code in Java + +```java +import java.util.Arrays; +import java.util.PriorityQueue; +import java.util.Scanner; + +public class Dijkstra { + public static void dijkstra(int[][] graph, int src, int V) { + int[] dist = new int[V]; + Arrays.fill(dist, Integer.MAX_VALUE); + dist[src] = 0; + + PriorityQueue pq = new PriorityQueue<>((a, b) -> a[1] - b[1]); + pq.add(new int[]{src, 0}); + + while (!pq.isEmpty()) { + int[] current = pq.poll(); + int u = current[0]; + + for (int v = 0; v < V; v++) { + if (graph[u][v] > 0 && dist[u] + graph[u][v] < dist[v]) { + dist[v] = dist[u] + graph[u][v]; + pq.add(new int[]{v, dist[v]}); + } + } + } + + System.out.println("Vertex\tDistance from Source"); + for (int i = 0; i < V; i++) { + System.out.println(i + "\t" + dist[i]); + } + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + System.out.print("Enter the number of vertices: "); + int V = sc.nextInt(); + + int[][] graph = new int[V][V]; + System.out.println("Enter the adjacency matrix (use 0 for no direct connection):"); + for (int i = 0; i < V; i++) { + for (int j = 0; j < V; j++) { + graph[i][j] = sc.nextInt(); + } + } + + System.out.print("Enter the source vertex: "); + int src = sc.nextInt(); + + dijkstra(graph, src, V); + } +} +``` + +### Example: +#### Input: +```mathmatica +Enter the number of vertices: 5 +Enter the adjacency matrix (use 0 for no direct connection): +0 10 20 0 0 +10 0 30 50 10 +20 30 0 20 0 +0 50 20 0 20 +0 10 0 20 0 +Enter the source vertex: 0 +``` +#### Output: +```csharp +Vertex Distance from Source +0 0 +1 10 +2 20 +3 40 +4 20 +``` From bd2edcd91d7c022de6a0ef3488944f7ffe2d1a31 Mon Sep 17 00:00:00 2001 From: H Rohitha Aiswarya Date: Sat, 9 Nov 2024 13:21:34 +0530 Subject: [PATCH 2/2] Update dijkstra-algo.md Thank you for reviewing, I have made the suggested changes, please let me know if there are more to do. Thank you --- docs/graphs/dijkstra-algo.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/graphs/dijkstra-algo.md b/docs/graphs/dijkstra-algo.md index 6bc9eefbc..0ca13de03 100644 --- a/docs/graphs/dijkstra-algo.md +++ b/docs/graphs/dijkstra-algo.md @@ -1,7 +1,7 @@ --- id: dijkstra-algo -title: Dijkstra's Algorithm -sidebar_label: Dijkstra's Algorithm +title: "Dijkstra's Algorithm" +sidebar_label: "Dijkstra's Algorithm" description: "In this blog post, we'll explore Dijkstra's Algorithm, an efficient method to find the shortest path from a source to all other nodes in a graph." tags: [dsa, algorithms, shortest path] ---