图的着色问题

Posted xinyi-blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图的着色问题相关的知识,希望对你有一定的参考价值。

1.假设可以用color种颜色,如果不满足则增大color的量

import java.util.*;
public class Main{
    static int[] state;
    static int color=3;
    static int len;
    public static void main(String[] args) {  
        int[][] e={{0,0,0,0,0},{0,0,1,1,1},{0,1,0,1,0},{0,1,1,0,0},{0,1,0,0,0}};
        len=e.length;
        state=new int[len];
        if(deepFun(0,e)){
            System.out.println("OK");
            for(int i=1;i<len;i++){
                System.out.println(state[i]);
            }
        }else{
            System.out.println("NOT OK");
        }
    }  
    public static boolean deepFun(int index,int[][] e){
        if(isOk(index,e)){
            if(index==len-1){
                return true;
            }else{
                for(int i=1;i<=color;i++){
                    state[index+1]=i;
                    if(deepFun(index+1,e)){
                        return true;
                    }
                    state[index+1]=0;
                }
                return false;
            }
        }else{
            return false;
        }
    }
    public static boolean isOk(int index,int[][] e){
        for(int i=1;i<index;i++){
            if(e[index][i]==1 && state[i]==state[index]){
                return false;
            }
        }
        return true;
    }
}

 

以上是关于图的着色问题的主要内容,如果未能解决你的问题,请参考以下文章