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

Refactor path reconstruction for shortest path #43

Merged
merged 4 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions include/graaflib/algorithm/shortest_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ struct GraphPath {
}
};

template <typename E>
struct PathVertex {
vertex_id_t id;
E dist_from_start;
vertex_id_t prev_id;
};

joweich marked this conversation as resolved.
Show resolved Hide resolved
/**
* @brief calculates the shortest path between on start_vertex and one
* end_vertex.
Expand Down
104 changes: 39 additions & 65 deletions include/graaflib/algorithm/shortest_path.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,37 @@ namespace graaf::algorithm {

namespace detail {

template <typename WEIGHT_T>
std::optional<GraphPath<WEIGHT_T>> reconstruct_path(
vertex_id_t start_vertex, vertex_id_t end_vertex,
std::unordered_map<vertex_id_t, PathVertex<WEIGHT_T>>& vertex_info) {
if (!vertex_info.contains(end_vertex)) {
return std::nullopt;
}

GraphPath<WEIGHT_T> path;
auto current = end_vertex;

while (current != start_vertex) {
path.vertices.push_front(current);
current = vertex_info[current].prev_id;
}

path.vertices.push_front(start_vertex);
path.total_weight = vertex_info[end_vertex].dist_from_start;
return path;
}

template <typename V, typename E, graph_spec S, typename WEIGHT_T>
std::optional<GraphPath<WEIGHT_T>> get_unweighted_shortest_path(
const graph<V, E, S>& graph, vertex_id_t start_vertex,
vertex_id_t end_vertex) {
std::unordered_set<vertex_id_t> seen_vertices{};
std::unordered_map<vertex_id_t, vertex_id_t> prev_vertex{};
std::unordered_map<vertex_id_t, PathVertex<WEIGHT_T>> vertex_info;
std::queue<vertex_id_t> to_explore{};

vertex_info[start_vertex] = {start_vertex, 1, start_vertex};
to_explore.push(start_vertex);
seen_vertices.insert(start_vertex);

// TODO: align/merge with implementation of do_bfs in graph_traversal.tpp
while (!to_explore.empty()) {
auto current{to_explore.front()};
to_explore.pop();
Expand All @@ -31,53 +50,29 @@ std::optional<GraphPath<WEIGHT_T>> get_unweighted_shortest_path(
}

for (const auto& neighbor : graph.get_neighbors(current)) {
if (!seen_vertices.contains(neighbor)) {
seen_vertices.insert(neighbor);
prev_vertex[neighbor] = current;
if (!vertex_info.contains(neighbor)) {
vertex_info[neighbor] = {
neighbor, vertex_info[current].dist_from_start + 1, current};
to_explore.push(neighbor);
}
}
}

const auto reconstruct_path = [&start_vertex, &end_vertex, &prev_vertex]() {
GraphPath<int> path;
auto current{end_vertex};

while (current != start_vertex) {
path.vertices.push_front(current);
current = prev_vertex[current];
}

path.vertices.push_front(start_vertex);
path.total_weight = path.vertices.size();

return path;
};

if (seen_vertices.contains(end_vertex)) {
return reconstruct_path();
} else {
return std::nullopt;
}
return reconstruct_path(start_vertex, end_vertex, vertex_info);
}

template <typename V, typename E, graph_spec S, typename WEIGHT_T>
std::optional<GraphPath<WEIGHT_T>> get_weighted_shortest_path(
const graph<V, E, S>& graph, vertex_id_t start_vertex,
vertex_id_t end_vertex) {
struct DijkstraVertex {
vertex_id_t id;
WEIGHT_T distance;
vertex_id_t previous;
};

std::unordered_map<vertex_id_t, DijkstraVertex> vertex_info;
std::priority_queue<
DijkstraVertex, std::vector<DijkstraVertex>,
std::function<bool(const DijkstraVertex&, const DijkstraVertex&)>>
to_explore([](const DijkstraVertex& v1, const DijkstraVertex& v2) {
return v1.distance > v2.distance;
});
std::unordered_map<vertex_id_t, PathVertex<WEIGHT_T>> vertex_info;
std::priority_queue<PathVertex<WEIGHT_T>, std::vector<PathVertex<WEIGHT_T>>,
std::function<bool(const PathVertex<WEIGHT_T>&,
const PathVertex<WEIGHT_T>&)>>
to_explore(
[](const PathVertex<WEIGHT_T>& v1, const PathVertex<WEIGHT_T>& v2) {
return v1.dist_from_start > v2.dist_from_start;
});
joweich marked this conversation as resolved.
Show resolved Hide resolved

vertex_info[start_vertex] = {start_vertex, 0, start_vertex};
to_explore.push(vertex_info[start_vertex]);
Expand All @@ -91,39 +86,18 @@ std::optional<GraphPath<WEIGHT_T>> get_weighted_shortest_path(
}

for (const auto& neighbor : graph.get_neighbors(current.id)) {
WEIGHT_T distance =
current.distance + graph.get_edge(current.id, neighbor)->get_weight();
WEIGHT_T distance = current.dist_from_start +
graph.get_edge(current.id, neighbor)->get_weight();

if (!vertex_info.contains(neighbor) ||
distance < vertex_info[neighbor].distance) {
distance < vertex_info[neighbor].dist_from_start) {
vertex_info[neighbor] = {neighbor, distance, current.id};
to_explore.push(vertex_info[neighbor]);
}
}
}

const auto reconstruct_path = [&start_vertex, &end_vertex, &vertex_info]() {
GraphPath<WEIGHT_T> path;
auto current = end_vertex;

while (current != start_vertex) {
path.vertices.push_back(current);
current = vertex_info[current].previous;
}

path.vertices.push_back(start_vertex);
path.total_weight = vertex_info[end_vertex].distance;

std::reverse(path.vertices.begin(), path.vertices.end());

return path;
};

if (vertex_info.contains(end_vertex)) {
return reconstruct_path();
} else {
return std::nullopt;
}
return reconstruct_path(start_vertex, end_vertex, vertex_info);
}

} // namespace detail
Expand Down