简体   繁体   English

递归着色图的所有节点

[英]coloring all nodes of a graph recursively

I have this problem where i want to color all nodes of a given graph,i create the below method (pseudo-code) to color one node at a time, the current browsed one(through the nodeIterator), (if a certain condition is met) whenever a neighbor of it is not colored.我有这个问题,我想为给定图形的所有节点着色,我创建以下方法(伪代码)一次着色一个节点,当前浏览的节点(通过 nodeIterator),(如果某个条件是遇到)只要它的邻居没有颜色。 A node is colored if it is an element of vector .如果节点是vector的元素,则节点被着色。 A node that is colored (with its pre-defined color), is a processed node.着色的节点(具有预定义的颜色)是已处理的节点。 And if a node is processed then it should not be included in the coloring check of another neighbor (a colored node is removed from the process of coloring node).如果一个节点被处理,那么它不应该被包括在另一个邻居的着色检查中(着色节点从着色节点的过程中删除)。 To note that conditionBetweenNodeAndNeighbor is checked between the current yet uncolored node, and a neigbor that is necessary not colored.请注意,在当前尚未着色的节点和必须不着色的邻居之间检查conditionBetweenNodeAndNeighbor here's my pseudo-code:这是我的伪代码:

  #Recursive method colorNodes
    colorNodes(Graph graph,Iterator<Node> nodeIterator, Vector vector)
        if (vector.size() == graph.size())
            return true;
        node = nodeIterator.next();
        nodeNeighbors = node.getNeighbors();
        while(nodeNeighbors.hasnext()) {
            neighbor = nodeNeighbors.next();
            if (!nodeIsColored(vector, neighbor)) {
                if(conditionBetweenNodeAndNeighbor is true) {
                    vector.add(node) #color current node 
                    colorNodes(graph, nodeIterator,vector)#call recursively the method
                }
            }  
            else if (!nodeNeighbors.hasNext()) {
                    #potential last node or isolated node (having one neighbor only)
                  if(conditionBetweenNodeAndNeighbor is true) {
                    vector.add(node) #color last node anyway 
                    colorNodes(graph, nodeIterator,vector)#call recursively the method
                    }
                   }
            else {
                    continue;
                }
        }

Could anyone clarify how to approach this problematic and if my approach is correct (especially the cases differentiation)?谁能澄清如何解决这个问题以及我的方法是否正确(尤其是案例区分)?

The main thing that stands out to me about your cases is that nodeNeighbors.next() is empty could cause problems.关于您的案例,对我来说最突出的主要事情是nodeNeighbors.next() is empty可能会导致问题。 This is because nodeNeighbors seems to be an Iterator, which means that every time nodeNeighbors.next() is called, it will return the next object from the iterator and effect every future call of the function.这是因为 nodeNeighbors 似乎是一个迭代器,这意味着每次调用 nodeNeighbors.next() 时,它都会从迭代器返回下一个 object 并影响 function 的每次调用。 It would be a good idea to use .nodeNeighbors.hasNext() instead.改用.nodeNeighbors.hasNext()是个好主意。

If you want to color (as in add to the Vector) every node in a graph, recursively, including unconnected nodes, then you can actually use a much simpler algorithm.如果您想对图中的每个节点(包括未连接的节点)递归着色(如添加到向量中),那么您实际上可以使用更简单的算法。

colorNodes(Graph graph, Iterator<Node> nodeIterator, Vector vector) {
    if (nodeIterator.hasNext()) {
        node = nodeIterator.next();
        vector.add(node);
        colorNodes(graph, nodeIterator, vector);
    }
}

Although I suspect that you might want to only color nodes which are connected to other colored nodes, which would probably require a different approach.虽然我怀疑您可能只想为连接到其他彩色节点的节点着色,这可能需要不同的方法。 The algorithm doesn't need to be given an Iterator<Node>, just a single node.该算法不需要给定一个 Iterator<Node>,只需一个节点。 If the node is uncolored, then it colors that node and then evaluates the function on each uncolored neighbor.如果节点是未着色的,则它 colors 该节点,然后评估每个未着色邻居上的 function。 If you need to use the algorithm with some of the nodes already colored in, then you could write a external function that calls the recursive algorithm on each node already in a given Vector.如果您需要对一些已经着色的节点使用该算法,那么您可以编写一个外部 function,它在给定向量中的每个节点上调用递归算法。

Note that the explanation in the paragraph above doesn't refer to the pseduocode sample above it, but instead to a hypothetical function/algorithm(s) that you could use if it was what you needed.请注意,上面段落中的解释不是指它上面的伪代码示例,而是指一个假设的函数/算法,如果它是你需要的,你可以使用它。

I hope this was helpful.我希望这可以帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM