2009-12-03 55 views
4

我回过一个类似的问题。我目前正在研究一个Java程序,它将检查一个图是否可着色,即它是否不包含奇数周期(奇数长度的周期)。整个算法应该在O(V + E)时间内运行(V代表所有顶点,E代表图中的所有边)。我当前的算法执行深度优先搜索,记录所有路径中的所有顶点,然后查找后沿,然后记录边之间的顶点。接下来,它追踪从后边的一端开始的路径,直到它碰到边的另一端的另一个顶点,从而回退后边完成的循环。检查无向图中的奇数周期

我的印象是,这种遍历可以在O(V + E)时间内对我图中存在的所有循环完成,但我必须缺少一些东西,因为我的算法运行时间很长时间很长的图(10k节点,不知道有多少边)。

我的算法是完全错误的吗?如果是这样,任何人都可以指出我正确的方向来更好地记录这些周期,或者可能知道它们是否有奇数个顶点?感谢您提供的任何和所有帮助。如果您需要,代码如下。

加法:对不起,我忘记了,如果图不是2色的,我需要提供一个奇怪的循环,证明它不是。

package algorithms311; 

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

public class CS311 { 

public static LinkedList[] DFSIter(Vertex[] v) { 
    LinkedList[] VOandBE = new LinkedList[2]; 
    VOandBE[0] = new LinkedList(); 
    VOandBE[1] = new LinkedList(); 

    Stack stack = new Stack(); 

    stack.push(v[0]); 
    v[0].setColor("gray"); 

    while(!stack.empty()) { 
     Vertex u = (Vertex) stack.peek(); 
     LinkedList adjList = u.getAdjList(); 
     VOandBE[0].add(u.getId()); 

     boolean allVisited = true; 
     for(int i = 0; i < adjList.size(); i++) { 
      if(v[(Integer)adjList.get(i)].getColor().equals("white")) { 
       allVisited = false; 
       break; 
      } 
      else if(v[(Integer)adjList.get(i)].getColor().equals("gray") && u.getPrev() != (Integer)adjList.get(i)) { 
       int[] edge = new int[2]; //pair of vertices 
       edge[0] = u.getId(); //from u 
       edge[1] = (Integer)adjList.get(i); //to v 
       VOandBE[1].add(edge); 
      } 
     } 
     if(allVisited) { 
      u.setColor("black"); 
      stack.pop(); 
     } 
     else { 
      for(int i = 0; i < adjList.size(); i++) { 
       if(v[(Integer)adjList.get(i)].getColor().equals("white")) { 
        stack.push(v[(Integer)adjList.get(i)]); 
        v[(Integer)adjList.get(i)].setColor("gray"); 
        v[(Integer)adjList.get(i)].setPrev(u.getId()); 
        break; 
       } 
      } 
     } 
    } 
    return VOandBE; 
} 

public static void checkForTwoColor(String g) { //input is a graph formatted as assigned 

    String graph = g; 

    try { 

     // --Read First Line of Input File 
     // --Find Number of Vertices 

     FileReader file1 = new FileReader("W:\\Documents\\NetBeansProjects\\algorithms311\\src\\algorithms311\\" + graph); 
     BufferedReader bReaderNumEdges = new BufferedReader(file1); 

     String numVertS = bReaderNumEdges.readLine(); 
     int numVert = Integer.parseInt(numVertS); 

     System.out.println(numVert + " vertices"); 





     // --Make Vertices 

     Vertex vertex[] = new Vertex[numVert]; 

     for(int k = 0; k <= numVert - 1; k++) { 
      vertex[k] = new Vertex(k); 
     } 

     // --Adj Lists 


     FileReader file2 = new FileReader("W:\\Documents\\NetBeansProjects\\algorithms311\\src\\algorithms311\\" + graph); 
     BufferedReader bReaderEdges = new BufferedReader(file2); 
     bReaderEdges.readLine(); //skip first line, that's how many vertices there are 

     String edge; 

     while((edge = bReaderEdges.readLine()) != null) { 

      StringTokenizer ST = new StringTokenizer(edge); 

      int vArr[] = new int[2]; 
      for(int j = 0; ST.hasMoreTokens(); j++) { 
       vArr[j] = Integer.parseInt(ST.nextToken()); 
      } 


      vertex[vArr[0]-1].addAdj(vArr[1]-1); 
      vertex[vArr[1]-1].addAdj(vArr[0]-1); 

     } 

     LinkedList[] l = new LinkedList[2]; 

     l = DFSIter(vertex);//DFS(vertex); 

     System.out.println(l[0]); 



     for(int i = 0; i < l[1].size(); i++) { 
      int[] j = (int[])l[1].get(i); 
      System.out.print(" [" + j[0] + ", " + j[1] + "] "); 
     } 



     LinkedList oddCycle = new LinkedList(); 
     boolean is2Colorable = true; 


     //System.out.println("iterate through list of back edges"); 

     for(int i = 0; i < l[1].size(); i++) { //iterate through the list of back edges 
      //System.out.println(i); 
      int[] q = (int[])(l[1].get(i)); // q = pair of vertices that make up a back edge 
      int u = q[0]; // edge (u,v) 
      int v = q[1]; 

      LinkedList cycle = new LinkedList(); 

      if(l[0].indexOf(u) < l[0].indexOf(v)) { //check if u is before v 
       for(int z = l[0].indexOf(u); z <= l[0].indexOf(v); z++) { //if it is, look for u first; from u to v 
        cycle.add(l[0].get(z)); 
       } 
      } 
      else if(l[0].indexOf(v) < l[0].indexOf(u)) { 
       for(int z = l[0].indexOf(v); z <= l[0].indexOf(u); z++) { //if it is, look for u first; from u to v 
        cycle.add(l[0].get(z)); 
       } 
      } 



      if((cycle.size() & 1) != 0) { //if it has an odd cycle, print out the cyclic nodes or write them to a file 

       is2Colorable = false; 

       oddCycle = cycle; 

       break; 
      } 
     } 
     if(!is2Colorable) { 
      System.out.println("Graph is not 2-colorable, odd cycle exists"); 
      if(oddCycle.size() <= 50) { 
       System.out.println(oddCycle); 
      } 
      else { 
       try { 
        BufferedWriter outFile = new BufferedWriter(new FileWriter("W:\\Documents\\NetBeansProjects\\algorithms311\\src\\algorithms311\\" + graph + "OddCycle.txt")); 
        String cyc = oddCycle.toString(); 
        outFile.write(cyc); 
        outFile.close(); 
       } 
       catch (IOException e) { 
        System.out.println("Could not write file"); 
       } 
      } 
     } 
    } 
    catch (IOException e) { 
     System.out.println("Could not open file"); 
    } 
    System.out.println("Done!"); 
} 

public static void main(String[] args) { 
     //checkForTwoColor("smallgraph1"); 
     //checkForTwoColor("smallgraph2"); 
     //checkForTwoColor("smallgraph3"); 
     //checkForTwoColor("smallgraph4"); 
     checkForTwoColor("smallgraph5"); 

     //checkForTwoColor("largegraph1"); 
    } 
} 

顶点类

package algorithms311; 

import java.util.*; 

public class Vertex implements Comparable { 

    public int id; 
    public LinkedList adjVert = new LinkedList(); 
    public String color = "white"; 
    public int dTime; 
    public int fTime; 
    public int prev; 
    public boolean visited = false; 

    public Vertex(int idnum) { 
     id = idnum; 
    } 

    public int getId() { 
     return id; 
    } 

    public int compareTo(Object obj) { 
     Vertex vert = (Vertex) obj; 
     return id-vert.getId(); 
    } 

    @Override public String toString(){ 
     return "Vertex # " + id; 
    } 

    public void setColor(String newColor) { 
     color = newColor; 
    } 

    public String getColor() { 
     return color; 
    } 

    public void setDTime(int d) { 
     dTime = d; 
    } 

    public void setFTime(int f) { 
     fTime = f; 
    } 

    public int getDTime() { 
     return dTime; 
    } 

    public int getFTime() { 
     return fTime; 
    } 

    public void setPrev(int v) { 
     prev = v; 
    } 

    public int getPrev() { 
     return prev; 
    } 

    public LinkedList getAdjList() { 
     return adjVert; 
    } 

    public void addAdj(int a) { //adds a vertex id to this vertex's adj list 
     adjVert.add(a); 
    } 

    public void visited() { 
     visited = true; 
    } 

    public boolean wasVisited() { 
     return visited; 
    } 
} 

回答

4

我的印象是,这种穿越的可能在O完成(V + E)的时间存在于我的图表中的所有周期下

在图中可能有比O(V + E)多得多的周期。如果你迭代所有这些,你会跑很长时间。回到最初的想法,你可以尝试实现一种简单的算法,以两种颜色对图进行着色(将任意节点标记为黑色,将所有邻居标记为黑色,将所有邻居标记为黑色,等等;这将是广度优先搜索)。这确实是在O(V + E)时间完成的。如果你成功了,那么图形是可着色的。如果你失败了,事实并非如此。

编辑:如果你需要证明图不是2着色一个周期,只是记录了每一个节点你穿越到它的顶点。当你碰巧从黑色顶点A以黑色顶点B遍历(因此需要以色黑B为白色,证明你的图形是不2-着色),您可以通过回想父母那里得到的循环:

X -> Y -> Z -> U -> V -> P -> Q -> A 
        \-> D -> E -> B 

然后,A-B-E-D-V-P-Q(到他们共同的祖先的路径)是你需要的循环。

请注意,在此版本中,您不必检查所有周期,您只需输出第一个周期,其中树中的后端都具有以相同颜色着色的顶点。

+0

增加了一个编辑...如果图不是2-colorable,我需要提供一个奇怪的周期,证明它​​不是2-colorable .. – devers 2009-12-03 10:15:11

+0

我编辑我的答案以及。 – 2009-12-03 10:19:22

2

您正在描述一个二部图。二部图是可着色的,它不包含奇数长度的周期。您可以使用BFS来证明图形是双向的。希望这可以帮助。