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

Introduce Welsh Powell Algorithm #383

Closed
ZigRazor opened this issue Jan 8, 2024 · 4 comments · Fixed by #395
Closed

Introduce Welsh Powell Algorithm #383

ZigRazor opened this issue Jan 8, 2024 · 4 comments · Fixed by #395
Labels
core something about core development Development of new Functionalities enhancement New feature or request Priority:Medium Priority Label for medium priority issue

Comments

@ZigRazor
Copy link
Owner

ZigRazor commented Jan 8, 2024

Please introduce Welsh Powell Coloring Algorithm

@ZigRazor ZigRazor added enhancement New feature or request development Development of new Functionalities core something about core Priority:Medium Priority Label for medium priority issue labels Jan 8, 2024
@ZigRazor ZigRazor added this to the Algorithm Implementation milestone Jan 8, 2024
@badumbatish
Copy link
Contributor

badumbatish commented Jan 20, 2024

Hi there, I'm working on this problem on my branch https://github.com/badumbatish/CXXGraph/tree/welsh_powell_coloring and hit an obstacle.

The algorithm returns an unordered_map of shared_ptrs of nodes where the value is the color order of each node.

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

  // Find 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 "color", and the value is a vector of node that belongs to that color.
  std::unordered_map<std::shared_ptr<const Node<T>>, int> mapOfColoring = {};

  for (auto &[nodeFrom, _] : adjMatrix) {
    mapOfColoring[nodeFrom] = 0;
  }
  // Going down the list of vertex based on degrees
  for (int i = 0; i < degreeOfVertexVector.size(); i++) {

    // Current vertex being considered for coloring
    auto node = degreeOfVertexVector[i].first;


    // 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;
}

When coloring, for each vertex, I'm trying to access its neighbor's color value in mapOfColoring with mapOfColoring[neighbor] to calculate the minimum color order needed for each vertex but the problem is that mapOfColoring is populated with source vertices, where neighbors are destination vertices. So none of the neighbor is actually in mapOfColoring.

I'm wondering if there is a way to approach this problem?

@badumbatish
Copy link
Contributor

Hi there, I went back to the repository and took some inspiration from the breadth_first_search algorithm where it returns a vector of raw nodes.

I finally implemented the correct changes and created a pull request #391 , can you help me take a look @ZigRazor ?

@badumbatish
Copy link
Contributor

Hi there, please don't close this issue yet, I noticed that there is some improvement to be made for my code to be more elegant.

@ZigRazor
Copy link
Owner Author

Ok, @badumbatish

@ZigRazor ZigRazor linked a pull request Feb 2, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core something about core development Development of new Functionalities enhancement New feature or request Priority:Medium Priority Label for medium priority issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants