diff --git a/Readme.md b/Readme.md index 3866700..609886f 100644 --- a/Readme.md +++ b/Readme.md @@ -88,3 +88,7 @@ Basically these are supposed to be my notes, but feel free to use them as you wi - [Welsh Powell Algorithm](graphColoringAlgo/welshPowellAlgo) +### [Shortest Path Algorithms](ShortestPathAlgos) +- [Dijkstra's Algorithm](ShortestPathAlgos/Dijkstra_Algo.cpp) + + diff --git a/ShortestPathAlgos/Dijkstra_Algo.cpp b/ShortestPathAlgos/Dijkstra_Algo.cpp new file mode 100644 index 0000000..64a7bd4 --- /dev/null +++ b/ShortestPathAlgos/Dijkstra_Algo.cpp @@ -0,0 +1,116 @@ +// GFG: https://practice.geeksforgeeks.org/problems/implementing-dijkstra-set-1-adjacency-matrix/1?utm_source=gfg&utm_medium=article&utm_campaign=bottom_sticky_on_article + +/*Algorithm : Dijkstra's algorithm is a graph traversal and shortest path-finding algorithm. It finds the shortest path between a starting node and all other nodes in a weighted graph, where each edge has a non-negative weight. + + Logic: he algorithm maintains a set of visited nodes and uses a priority queue to explore nodes in order of their current distance from the start node. It iteratively selects the node with the smallest distance, updates the distances to its neighbors if a shorter path is found, and continues until all nodes have been visited or the destination node is reached. + + */ + +#include +using namespace std; + +// Function to implement Dijkstra Algorithm +vector dijkstra(int V, vector> adj[], int S) +{ + set> st; // set to store the distance and node pair + st.insert({0,S}); // insert the source node with distance 0 + + vector dist(V, 1e9); // vector to store the distance of each node from source + + dist[S] = 0; // distance of source node from itself is 0 + + while(!st.empty()) + { + // find the node with minimum distance + auto it = *(st.begin()); + int dis = it.first; + int node = it.second; + + st.erase(it); + + + // traverse the adjacency list of the node + for(auto x: adj[node]) + { + int adjNode = x[0]; + int adjWt = x[1]; + + // if the distance of adjacent node is greater than the distance of current node + weight of edge between them + if(dis + adjWt < dist[adjNode]) + { + // if the adjacent node is already visited then erase it from the set + if(dist[adjNode] != 1e9) + { + st.erase({dist[adjNode],adjNode}); + } + + // update the distance of adjacent node + dist[adjNode] = dis + adjWt; + st.insert({dist[adjNode],adjNode}); + } + } + } + // return the distance vector + return dist; +} + +int main() +{ + // Test case 1 (same as before) + int V1 = 5; // Number of vertices for Test case 1 + int E1 = 7; // Number of edges for Test case 1 + int S1 = 0; // Source node for Test case 1 + + // Define the adjacency list for Test case 1 + vector> adj1[V1]; + + // Add edges to the adjacency list for Test case 1 + adj1[0].push_back({1, 2}); + adj1[0].push_back({2, 4}); + adj1[1].push_back({2, 1}); + adj1[1].push_back({3, 7}); + adj1[2].push_back({3, 3}); + adj1[2].push_back({4, 5}); + adj1[3].push_back({4, 2}); + + // Call the dijkstra function for Test case 1 + vector res1 = dijkstra(V1, adj1, S1); + + // Print the result for Test case 1 + cout << "Test case 1 result: "; + for (int i = 0; i < V1; i++) + cout << res1[i] << " "; + cout << endl; + + // Test case 2 + int V2 = 3; // Number of vertices for Test case 2 + int E2 = 3; // Number of edges for Test case 2 + int S2 = 1; // Source node for Test case 2 + + // Define the adjacency list for Test case 2 + vector> adj2[V2]; + + // Add edges to the adjacency list for Test case 2 + adj2[0].push_back({1, 2}); + adj2[1].push_back({2, 3}); + adj2[2].push_back({0, 1}); + + // Call the dijkstra function for Test case 2 + vector res2 = dijkstra(V2, adj2, S2); + + // Print the result for Test case 2 + cout << "Test case 2 result: "; + for (int i = 0; i < V2; i++) + cout << res2[i] << " "; + cout << endl; + + return 0; +} + + + +// Time and Space Complexity for Best Average and Worst cases: +// Time Complexity: O(ElogV) +// Time Complexity: O(V) + + diff --git a/ShortestPathAlgos/Test_Cases.md b/ShortestPathAlgos/Test_Cases.md new file mode 100644 index 0000000..9a99313 --- /dev/null +++ b/ShortestPathAlgos/Test_Cases.md @@ -0,0 +1,81 @@ +Dijkstra's algorithm Test Cases: + +Test Case 1: +```plaintext +V = 4 +adj = {{{1, 1}, {2, 4}, {3, 3}}, {{0, 1}, {2, 2}, {3, 5}}, {{0, 4}, {1, 2}, {3, 6}}, {{0, 3}, {1, 5}, {2, 6}}} +S = 0 +Output: 0 1 3 3 +``` + +Test Case 2: +```plaintext +V = 3 +adj = {{{1, 2}, {2, 3}}, {{0, 2}, {2, 5}}, {{0, 3}, {1, 5}}} +S = 1 +Output: 2 0 5 +``` + +Test Case 3: +```plaintext +V = 5 +adj = {{{1, 4}, {3, 1}}, {{0, 4}, {2, 2}, {3, 5}, {4, 3}}, {{1, 2}, {4, 1}}, {{0, 1}, {1, 5}, {4, 6}}, {{1, 3}, {2, 1}, {3, 6}}} +S = 0 +Output: 0 4 6 1 7 +``` + +Test Case 4: +```plaintext +V = 4 +adj = {{{1, 10}}, {{0, 10}, {2, 5}, {3, 1}}, {{1, 5}, {3, 2}}, {{1, 1}, {2, 2}}} +S = 0 +Output: 0 8 7 3 +``` + +Test Case 5: +```plaintext +V = 3 +adj = {{{1, 7}, {2, 9}}, {{0, 7}, {2, 10}}, {{0, 9}, {1, 10}}} +S = 1 +Output: 7 0 10 +``` + +Test Case 6: +```plaintext +V = 6 +adj = {{{1, 2}, {2, 5}}, {{0, 2}, {2, 6}, {3, 7}}, {{0, 5}, {1, 6}, {3, 3}, {4, 8}}, {{1, 7}, {2, 3}, {4, 1}, {5, 4}}, {{2, 8}, {3, 1}, {5, 9}}, {{3, 4}, {4, 9}}} +S = 0 +Output: 0 2 5 10 11 20 +``` + +Test Case 7: +```plaintext +V = 2 +adj = {{{1, 3}}, {{0, 3}}} +S = 1 +Output: 3 0 +``` + +Test Case 8: +```plaintext +V = 1 +adj = {} +S = 0 +Output: 0 +``` + +Test Case 9: +```plaintext +V = 4 +adj = {{{1, 5}}, {{0, 5}, {2, 2}}, {{1, 2}, {3, 1}}, {{2, 1}}} +S = 0 +Output: 0 5 7 8 +``` + +Test Case 10: +```plaintext +V = 5 +adj = {{{1, 3}, {2, 6}}, {{0, 3}, {3, 2}}, {{0, 6}, {4, 7}}, {{1, 2}, {4, 5}}, {{2, 7}, {3, 5}}} +S = 2 +Output: 6 3 0 2 7 +``` \ No newline at end of file