Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.2 kB
4
Indexable
Never
public void colorGraph(){
        int[][] matranKe = this.graphMatrix;
        int deg = 0;
        for(int i=0;i<matranKe.length;i++){
            for(int j=0;j<matranKe[0].length;j++){
                deg += matranKe[i][j];
            }
            listVertex.get(i).setDeg(deg);
        }
        for(int i=0;i<listVertex.size()-1;i++){
            for(int j=i;j<listVertex.size();j++){
                if(listVertex.get(j).deg > listVertex.get(j).deg){
                    Vertex temp = listVertex.get(i);
                    listVertex.set(i,listVertex.get(j));
                    listVertex.set(j,temp);
                }
            }
        }
        for(Vertex v : listVertex){
            if(v.color == null){
                String color = v.listColorAvailable.get(0);
                ArrayList<Vertex> neighbor = v.getNeighbor();
                for(Vertex n : neighbor){
                    if(n.color == null){
                        n.listColorAvailable.remove(color);
                    }
                }
                v.color = color;
                System.out.println("Đã tô màu " + color + " cho con vợ đỉnh tên là đỉnh " + v.name);
            }
        }
    }
Leave a Comment