将色数缩小至范围

时间:2018-11-10 20:09:12

标签: java arrays

我需要编写一个程序,该程序接受一个输入文件(代表一个图形),并输出色数和顶点颜色。我编写了一个可以成功完成此操作的程序,但是却得到了一个很大的输入文件,并且该程序无限期地运行。我需要使程序将色度数缩小到十个值的范围。谁能给我一些有关如何完成此工作的建议。我将在下面发布我的代码以供参考。

任何帮助将不胜感激。谢谢!

import java.util.*;
import java.io.*;

public class chromatic_num {
    public static int n;
    public static int q[];
    public static int matrix[][];
    public static List<Integer> list;

    public static void main(String[] args) throws IOException {
        //reads input from file
        BufferedReader br;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream("file1.txt")));
        } catch (FileNotFoundException e) {
            throw new RuntimeException();
        }
        String line;
        boolean readFirstLine = false;
        // new array list to store input
        list = new ArrayList<Integer>();
        // read file line by line
        while ((line = br.readLine()) != null)   {
            // prints content
            String[] toks = line.split(" ");
            if (readFirstLine) {
                list.add(Integer.parseInt(toks[0]));
                list.add(Integer.parseInt(toks[1]));
            }
            else {
                readFirstLine = true;
                n = Integer.parseInt(toks[0]);
            }
        }
        // finds max edges
        n = Collections.max(list);
        // for adjacency matrix
        matrix = new int[n][n];
        // constructs adj. matrix
        int count = 0;
        Integer v1 = 0, v2 = 0;
        for (Integer v : list) {
            if (count % 2 == 0) {
                v1 = v - 1;
            }
            else {
                v2 = v - 1;
                matrix[v1][v2] = 1;
                matrix[v2][v1] = 1;
            }
            count++;
        }
        // new array to store colors
        q = new int[n]; 
        chromatic_num chromatic = new chromatic_num();
        //print solution to screen
        System.out.println(chromatic.color());       
        for (int i = 0; i < n; i++) {
            System.out.println("" + (i + 1) + " " + q[i]);
        }
        //print solution to file
        try {
          System.setOut(new PrintStream(new FileOutputStream("chromatic_num.txt")));
        } catch (FileNotFoundException e) {
            throw new RuntimeException();
        }

        System.out.println(chromatic.color());
        for (int i = 0; i < n; i++) {
            System.out.println("" + (i + 1) + " " + q[i]);
        }
        br.close();     
    }
    //color G using minimum number of colors, returns chromatic number
    public int color()
    {
        //for (int t = 0
        for (int i = 1; i <= n; i++)
        {
            //if G can be colored using i colors starting at vertex 0
            //System.out.println("colored using " + i + " colors starting at vertex 1");
            if (color(0,i)) {
                //chromatic number is i, return it
                return i;
            }
            else {
                for (int j = 0; j < n; j++) {
                    q[j] = 0;
                }
            }
        }
        return 0;
    }

    //colors G using m colors starting at vertex v
    public boolean color(int v, int m)
    {
        if (v > n - 1) {
            //all vertices have been colored, success
            return true;
        }
        else
        {
            for (int i = 1; i <= m; i++)
            {
                //assign color i to vertex v
                q[v] = i;
                //System.out.println("color " + i + " to vertex " + (v + 1));

                //check whether colors of v and its neighbors conflict
                boolean hasConflicted = hasConflicted(v);
                //System.out.println("hasConflicted = " + hasConflicted);
                if (hasConflicted == false)
                {
                    //color the rest of G using m colors starting at vertex v+1, done by recursive call color(v+1, m)
                    boolean success = color(v + 1, m);

                    //if (the rest of G can be colored)
                    //  all vertices have been colored, success
                    if (success) {
                        return true;
                    }
                }
            }
            //assign color 0 to vertex v and fail, this happens when none of the m colors can be assigned to vertex v
            q[v] = 0;
            return false;
        }
    }
    public boolean hasConflicted(int v) {
        for (int i = 0; i < n; i++)
        {
            if (i != v && matrix[i][v] == 1 && q[i] == q[v]) {
                return true;
            }
        }
        return false;
    }
}

0 个答案:

没有答案