Skip to content

Commit

Permalink
feat: split class
Browse files Browse the repository at this point in the history
  • Loading branch information
jwsung91 committed Jan 27, 2025
1 parent 58b7522 commit fe93d7e
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 76 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@
*.exe
*.out
*.app

build
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"C_Cpp.errorSquiggles": "disabled"
}
18 changes: 18 additions & 0 deletions CMakeLists.txt
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>)
34 changes: 34 additions & 0 deletions include/dijkstra.hpp
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;
};
76 changes: 0 additions & 76 deletions main.cc

This file was deleted.

36 changes: 36 additions & 0 deletions src/dijkstra.cc
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;
}
37 changes: 37 additions & 0 deletions src/main.cc
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;
}

0 comments on commit fe93d7e

Please sign in to comment.