Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. public class main {
  2.  
  3. static Vertex verts[];
  4. static String brokenPath = ""; // this is used to record when a path breaks
  5.  
  6. public static String getColorName(int b) {
  7. if (b==1)
  8. return "red";
  9. else
  10. return "blue";
  11. }
  12.  
  13. public static boolean DFS(int src, boolean b) {
  14. // Assign a color to the source index
  15. if (verts[src] == null) {
  16. verts[src] = new Vertex();
  17. }
  18. Vertex vert = verts[src];
  19. vert.setColor(b);
  20. // caching color
  21. int col = vert.getColor();
  22.  
  23. // update path with this vertex
  24. //path = path + src + "("+ getColorName(col)+ "), ";
  25. LinkedList<Integer> LL = vert.getList();
  26. for (int i : LL){
  27. // color 0 means undiscovered
  28. int col2 = verts[i].getColor();
  29. if (col2 == 0) {
  30. if (!DFS(i,!b)) {
  31. brokenPath = (i+1) + "(" + getColorName(verts[i].getColor()) + "), " + brokenPath;
  32. return false;
  33. }
  34. }
  35. // we found an invalid cycle, return here and provide appropriate broken string
  36. else if(col2 > 0 && col == col2) {
  37. brokenPath = "=> " + (i+1) + "(" + getColorName(col2) + "), ";
  38. return false;
  39. }
  40. }
  41.  
  42. return true;
  43. }
  44.  
  45. public static void main(String[] args) {
  46. if (args.length > 0 && args[0] != null) {
  47. try {
  48. FileReader r = new FileReader(args[0]);
  49. LineNumberReader lnr = new LineNumberReader(r);
  50.  
  51. // by convention the # of vertices is included as the first entry
  52.  
  53. try {
  54. int size = Integer.parseInt(lnr.readLine());
  55. verts = new Vertex[size];
  56. while(lnr.ready()) {
  57. String split[] = lnr.readLine().split(" ");
  58. //adds the vertex in
  59. int index1 = Integer.parseInt(split[0])-1;
  60. int index2 = Integer.parseInt(split[1])-1;// has to be corrected since arrays are zero'd
  61. // since edges are undirected, add them to each vertex, since traversal can happen either way
  62. if (verts[index1] == null)
  63. verts[index1] = new Vertex();
  64. if (verts[index2] == null)
  65. verts[index2] = new Vertex();
  66. verts[index2].add(index1);
  67. verts[index1].add(index2);
  68. }
  69. try {
  70. r.close();
  71. lnr.close();
  72. } catch (IOException e) {
  73. e.printStackTrace();
  74. }
  75. // Write output file
  76. try {
  77. FileOutputStream db = new FileOutputStream(args[1]);
  78. PrintWriter out = new PrintWriter(db);
  79. String start = "";
  80. boolean twoColorable = true;
  81. // loop through and find verticies that aren't discovered
  82. for (int s = 0; s<verts.length; s++) {
  83. if (verts[s] == null || verts[s].getColor() == 0) {
  84. start = (s+1) + "(red), ";
  85. if (!DFS(s, true)) {
  86. twoColorable = false;
  87. break;
  88. }
  89. else {
  90. start = "";
  91. }
  92. }
  93. }
  94.  
  95. System.out.println(twoColorable);
  96. // write our output file according to the result
  97. out.println("Two colorable : " + twoColorable);
  98. if (twoColorable) {
  99. for (int i = 0; i<verts.length; i++) {
  100. if (verts[i] != null && verts[i].getColor() == 1) {
  101. out.println((i+1) + " = red");
  102. }
  103. else {
  104. out.println((i+1) + " = blue");
  105. }
  106. }
  107. }
  108. else {
  109. out.println(start + brokenPath);
  110. }
  111. out.flush();
  112. out.close();
  113. }
  114. catch(Exception e) {e.printStackTrace();}// errorr-ed
  115.  
  116. // God... Could java get anymore messy?
  117.  
  118. } catch (NumberFormatException e1) {
  119. e1.printStackTrace();
  120. } catch (IOException e1) {
  121. e1.printStackTrace();
  122. }
  123. } catch (FileNotFoundException e) {
  124. e.printStackTrace();
  125. }
  126. }
  127. else
  128. System.out.println("arguments <inputfile> <outputfile>");
  129. }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement