-
Notifications
You must be signed in to change notification settings - Fork 41
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
2 changed files
with
27 additions
and
31 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 |
---|---|---|
@@ -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 |
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