samedi 20 novembre 2021

problem of 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. A node is colored if it is an element of 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). 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.next() is empty) {
                    #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)?

Aucun commentaire:

Enregistrer un commentaire