Advertisement
samham1218

assignment

Oct 31st, 2018
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. Write a program that will compute the chromatic number of a graph and color the vertices using the chromatic number of colors. The input to the program is a file containing the graph. The input file contains the number of vertices and the number of edges at the top and the list of edges at in the subsequent lines. The output file contains the chromatic number at the top and the coloring of vertices using the chromatic number of colors in the subsequent lines. The vertices are named 1,2,3... and the colors are also named 1,2,3...
  2. A brute force algorithm to solve the coloring problem is given. Translate the algorithm into a Java program. The program must be based on the algorithm.
  3. pseudo code is pasted below
  4. //graph G, vertices n, vertices are numbered 0,1..n-1
  5. //G is stored in adjacency matrix p (MUST BE STORED IN ADJACENCY MATRIX)
  6. //colors are stored in array q
  7. //q[i] has color of vertex i, initially all 0
  8.  
  9. //color G using minimum number of colors, returns chromatic number
  10. int color()
  11. {
  12. for(i=1 to n)
  13. {
  14. //if G can be colored using i colors starting at vertex 0
  15. if (color(0,i))
  16. chromatic number is i, return it
  17. }
  18. }
  19.  
  20. //colors G using m colors starting at vertex v
  21. boolean color(v, m)
  22. {
  23. if (v exceeds n-1)
  24. all vertices have been colored, success
  25. else
  26. {
  27. for (i=1 to m)
  28. {
  29. assign color i to vertex v
  30.  
  31. check whether colors of v and its neighbors conflict
  32.  
  33. if (colors do not conflict)
  34. {
  35. color the rest of G using m colors starting at vertex v+1, done by recursive call color(v+1, m)
  36.  
  37. if (the rest of G can be colored)
  38. all vertices have been colored, success
  39. }
  40. }
  41. assign color 0 to vertex v and fail, this happens when none of the m colors can be assigned to vertex v
  42. }
  43. }
  44.  
  45. //in color() method chromatic numbers can be tried in ascending order
  46. //(i=1 to n) or descending order (i=n to 1) and both orders can be
  47. //used to find a range of possible values of the chromatic number
  48.  
  49. the input file (a .txt file) -----> output file
  50.  
  51. 5 7                                 3
  52.  
  53. 4 2                                 1 1
  54.  
  55. 1 5                                 2 2
  56.  
  57. 2 3                                 3 3
  58.  
  59. 4 3                                 4 1
  60.  
  61. 5 4                                 5 3
  62.  
  63. 1 2
  64.  
  65. 2 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement