-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
130 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,5 @@ | |
*.exe | ||
*.out | ||
*.app | ||
|
||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"C_Cpp.errorSquiggles": "disabled" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
|
||
# Set the project name | ||
project(DijkstraCpp) | ||
|
||
# Specify the C++ standard | ||
set(CMAKE_CXX_STANDARD 17) | ||
set(CMAKE_CXX_STANDARD_REQUIRED True) | ||
|
||
# Add include directories | ||
include_directories(include) | ||
|
||
# Add the executable | ||
file(GLOB SOURCES "src/*.cc") | ||
add_executable(DijkstraCpp ${SOURCES}) | ||
|
||
# Optionally, you can add any required libraries here | ||
# target_link_libraries(DijkstraCpp <library_name>) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <queue> | ||
#include <limits> | ||
|
||
using namespace std; | ||
|
||
const int INF = numeric_limits<int>::max(); | ||
|
||
struct Edge | ||
{ | ||
int to; | ||
int weight; | ||
}; | ||
|
||
class Dijkstra | ||
{ | ||
public: | ||
Dijkstra(int n, const vector<vector<Edge>> &graph) : n(n), graph(graph) {} | ||
vector<int> shortest_path(int start); | ||
|
||
private: | ||
struct Node | ||
{ | ||
int vertex; | ||
int cost; | ||
bool operator<(const Node &other) const | ||
{ | ||
return cost > other.cost; | ||
} | ||
}; | ||
int n; | ||
const vector<vector<Edge>> &graph; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include "dijkstra.hpp" | ||
|
||
vector<int> Dijkstra::shortest_path(int start) | ||
{ | ||
vector<int> dist(n, INF); | ||
dist[start] = 0; | ||
|
||
priority_queue<Node> pq; | ||
pq.push({start, 0}); | ||
|
||
while (!pq.empty()) | ||
{ | ||
Node current = pq.top(); | ||
pq.pop(); | ||
|
||
int current_vertex = current.vertex; | ||
int current_cost = current.cost; | ||
|
||
if (current_cost > dist[current_vertex]) | ||
continue; | ||
|
||
for (const Edge &edge : graph[current_vertex]) | ||
{ | ||
int next_vertex = edge.to; | ||
int next_cost = current_cost + edge.weight; | ||
|
||
if (next_cost < dist[next_vertex]) | ||
{ | ||
dist[next_vertex] = next_cost; | ||
pq.push({next_vertex, next_cost}); | ||
} | ||
} | ||
} | ||
|
||
return dist; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <queue> | ||
#include <limits> | ||
#include "dijkstra.hpp" | ||
|
||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int n = 5; | ||
int start = 0; | ||
|
||
vector<vector<Edge>> graph(n); | ||
graph[0] = {{1, 10}, {3, 30}, {4, 100}}; | ||
graph[1] = {{2, 50}}; | ||
graph[2] = {{4, 10}}; | ||
graph[3] = {{2, 20}, {4, 60}}; | ||
graph[4] = {}; | ||
Dijkstra d(n, graph); | ||
vector<int> shortest_paths = d.shortest_path(start); | ||
|
||
cout << "Shortest distances from vertex " << start << ":\n"; | ||
for (int i = 0; i < n; ++i) | ||
{ | ||
if (shortest_paths[i] == INF) | ||
{ | ||
cout << "Vertex " << i << ": INF\n"; | ||
} | ||
else | ||
{ | ||
cout << "Vertex " << i << ": " << shortest_paths[i] << "\n"; | ||
} | ||
} | ||
|
||
return 0; | ||
} |