Skip to content

Commit

Permalink
Sync from fork
Browse files Browse the repository at this point in the history
  • Loading branch information
badumbatish committed Jan 23, 2024
1 parent 823ef8f commit 206bc51
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
56 changes: 53 additions & 3 deletions include/CXXGraph/Graph/Algorithm/welshPowellColoring_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,57 @@
// Created by jjsm on 1/22/24.
//

#ifndef CXXGRAPH_WELSHPOWELLCOLORING_H
#define CXXGRAPH_WELSHPOWELLCOLORING_H
#ifndef CXXGRAPH_WELSHPOWELLCOLORING_IMPL_H__
#define CXXGRAPH_WELSHPOWELLCOLORING_IMPL_H__

#endif // CXXGRAPH_WELSHPOWELLCOLORING_H
#pragma once

#include "CXXGraph/Graph/Graph_decl.h"

namespace CXXGraph {
template <typename T>
std::map<Node<T>, int> Graph<T>::welshPowellColoring() const {
auto adjMatrix = *getAdjMatrix();
std::unordered_map<std::shared_ptr<const Node<T>>, int> degreeOfVertexMap = {};

// Find the degree of each vertex and put them in a map
for (auto &[nodeFrom, nodeToEdgeVec] : adjMatrix) {
degreeOfVertexMap[nodeFrom] = nodeToEdgeVec.size();
}

// Transform the map to the vector to sort
std::vector<std::pair<std::shared_ptr<const Node<T>>, int>> degreeOfVertexVector(degreeOfVertexMap.begin(), degreeOfVertexMap.end());

// Sort them based on the vertex degree
std::sort(degreeOfVertexVector.begin(), degreeOfVertexVector.end(), [](const auto& left, const auto& right) {
return left.second > right.second;
});

// Create a new map of coloring, where the keys are the nodes, and the value is the color order (assigned by integer)
std::map<Node<T>, int> mapOfColoring;
for (auto &[nodeFrom, _] : adjMatrix) {
mapOfColoring[*nodeFrom] = 0;
}

// Going down the list of vertex based on degrees
for (const auto& [node, _] : degreeOfVertexVector) {
// Find the smallest unused color for the current vertex
std::vector<int> usedColors(degreeOfVertexVector.size() + 1, 0);

for (const auto &[neighbor, _] : adjMatrix[node]) {
usedColors[mapOfColoring[*neighbor]] = 1;
}

// Assign the smallest unused color to the current vertex
for (int c = 1; c < usedColors.size(); c++) {
if (usedColors[c] == 0) {
mapOfColoring[*node] = c;
break;
}
}
}

return mapOfColoring;
}
}
#endif // CXXGRAPH_WELSHPOWELLCOLORING_IMPL_H__
1 change: 1 addition & 0 deletions include/CXXGraph/Graph/Graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "CXXGraph/Graph/Algorithm/Tarjan_impl.hpp"
#include "CXXGraph/Graph/Algorithm/TopologicalSort_impl.hpp"
#include "CXXGraph/Graph/Algorithm/TransitiveReduction_impl.hpp"
#include "CXXGraph/Graph/Algorithm/welshPowellColoring_impl.hpp"

// IO Operation
#include "CXXGraph/Graph/IO/IOUtility_impl.hpp"
Expand Down
11 changes: 11 additions & 0 deletions include/CXXGraph/Graph/Graph_decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,17 @@ class Graph {
virtual double fordFulkersonMaxFlow(const Node<T> &source,
const Node<T> &target) const;

/**
* @brief Welsh-Powell Coloring algorithm
* @return a std::map of keys being the nodes and the values being the color
* order (by integer) starting from 1.
* Source :
* https://www.youtube.com/watch?v=SLkyDuG1Puw&ab_channel=TheLogicalLearning
* https://www.geeksforgeeks.org/welsh-powell-graph-colouring-algorithm/
* https://www.tutorialspoint.com/de-powell-graph-colouring-algorithm
*/
virtual std::map<Node<T>, int> welshPowellColoring() const;

/**
* \brief
* This function writes the graph to an output file
Expand Down

0 comments on commit 206bc51

Please sign in to comment.