Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Shortest Path Algorithms Using C++ #279

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


116 changes: 116 additions & 0 deletions ShortestPathAlgos/Dijkstra_Algo.cpp
Original file line number Diff line number Diff line change
@@ -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<bits/stdc++.h>
using namespace std;

// Function to implement Dijkstra Algorithm
vector <int> dijkstra(int V, vector<vector<int>> adj[], int S)
{
set<pair<int,int>> st; // set to store the distance and node pair
st.insert({0,S}); // insert the source node with distance 0

vector<int> 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<vector<int>> 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<int> 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<vector<int>> 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<int> 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)


81 changes: 81 additions & 0 deletions ShortestPathAlgos/Test_Cases.md
Original file line number Diff line number Diff line change
@@ -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
```