Skip to content

Commit

Permalink
Merge pull request #1 from bobluppes/main
Browse files Browse the repository at this point in the history
Update to newest version of graaf
  • Loading branch information
FlorianXXIV authored Jun 18, 2024
2 parents 1662c21 + c096baf commit a0c89f9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
55 changes: 26 additions & 29 deletions include/graaflib/algorithm/coloring/welsh_powell.tpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,46 @@
#pragma once
#include <graaflib/algorithm/coloring/welsh_powell.h>
#include <graaflib/properties/vertex_properties.h>
#include <unordered_map>
#include <vector>

#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

namespace graaf::algorithm {

template <typename GRAPH>
std::unordered_map<vertex_id_t, int> welsh_powell_coloring(const GRAPH& graph) {

using degree_vertex_pair = std::pair<int, vertex_id_t>;

// Step 1: Sort vertices by degree in descending order
std::vector<degree_vertex_pair> degree_vertex_pairs;
for (const auto& [vertex_id, _] : graph.get_vertices()) {

int degree = properties::vertex_degree(graph, vertex_id);
degree_vertex_pairs.emplace_back(degree, vertex_id);

}
using degree_vertex_pair = std::pair<int, vertex_id_t>;

std::sort(degree_vertex_pairs.rbegin(), degree_vertex_pairs.rend());
// Step 1: Sort vertices by degree in descending order
std::vector<degree_vertex_pair> degree_vertex_pairs;
for (const auto& [vertex_id, _] : graph.get_vertices()) {
int degree = properties::vertex_degree(graph, vertex_id);
degree_vertex_pairs.emplace_back(degree, vertex_id);
}

// Step 2: Assign colors to vertices
std::unordered_map<vertex_id_t, int> color_map;
std::sort(degree_vertex_pairs.rbegin(), degree_vertex_pairs.rend());

for (const auto [_, current_vertex] : degree_vertex_pairs) {
// Step 2: Assign colors to vertices
std::unordered_map<vertex_id_t, int> color_map;

int color = 0; // Start with color 0
for (const auto [_, current_vertex] : degree_vertex_pairs) {
int color = 0; // Start with color 0

// Check colors of adjacent vertices
for (const auto& neighbor : graph.get_neighbors(current_vertex)) {
// If neighbor is already colored with this color, increment the color
if (color_map.contains(neighbor) && color_map[neighbor] == color) {
color++;
}
}

// Assign the color to the current vertex
color_map[current_vertex] = color;
// Check colors of adjacent vertices
for (const auto& neighbor : graph.get_neighbors(current_vertex)) {
// If neighbor is already colored with this color, increment the color
if (color_map.contains(neighbor) && color_map[neighbor] == color) {
color++;
}
}

return color_map;
// Assign the color to the current vertex
color_map[current_vertex] = color;
}

return color_map;
}

} // namespace graaf::algorithm
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
namespace graaf::algorithm {

template <typename V, typename E>
[[nodiscard]] sccs_t
tarjans_strongly_connected_components(
[[nodiscard]] sccs_t tarjans_strongly_connected_components(
const graph<V, E, graph_type::DIRECTED>& graph) {
// Vector to store strongly connected components
sccs_t sccs{};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <gtest/gtest.h>
#include <utils/fixtures/fixtures.h>

#include <algorithm>
#include <vector>

namespace graaf::algorithm {
Expand Down Expand Up @@ -299,4 +300,4 @@ TYPED_TEST(TarjansStronglyConnectedComponentsTest, TarjansComplexGraphTest) {
ASSERT_TRUE(are_set_vectors_equal(sccs, expected_sccs));
}

} // namespace graaf::algorithm
} // namespace graaf::algorithm

0 comments on commit a0c89f9

Please sign in to comment.