admin管理员组

文章数量:1294460

I'm searching for an algorithm to solve a graphing problem. I would like to code it in C++ and it should have an acceptable runtime

The problem has the following setup:

  • We have a directed acyclic graph of around 1000 nodes
  • Every node has a certain type. We could also call them colours in a general sense
  • I would like to partition the graph into a minimal amount of subgraph. Or at least do a best effort/greedy approach to minimise the subgraphs
  • Every subgraph can only contain nodes of the same type/colour
  • The new graph consisting of these subgraph should also be acyclic. Meaning that I don't want any circular dependencies between these subgraph
  • Small detail is that the Graph only has 1 root node

I was thinking about the following approach:

  • Start from the topologically sorted graph
  • Iterate over all the nodes starting from the root node
    • Try to add the node to the last subgraph of that colour
    • Check if that node has any connection to nodes outside the current subgraph. If so, check that we don't introduce any cycle. This check become tricky since we have to traverse the leftover nodes together with the already existing subgraphs. But I assume an adapted cycle detection algorithm will do the trick
    • If we can't add the node to the current subgraph, we create a new subgraph for that type/colour of node

本文标签: