Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.LinkedList;
  5.  
  6. public class DM {
  7.  
  8. private static int incedenceMatrix[][];
  9. private static LinkedList<Integer> currentResultList;
  10. private static Node [] nodesArray;
  11.  
  12. public static void main(String args[]){
  13. clean();
  14. incedenceMatrix = readIncedenceMatrix("input.txt");
  15. printMatrix(incedenceMatrix);
  16. addNodeToResult(1);
  17. work(1);
  18. System.out.println(currentResultList);
  19. }
  20. public static int[][] readIncedenceMatrix(String inputFileName) {
  21. try {
  22. BufferedReader in = new BufferedReader(new FileReader(inputFileName));
  23. String Line="";
  24. int nodesCount = Integer.parseInt(in.readLine());
  25. nodesArray = new Node [nodesCount];
  26. int Matr[][] = new int [nodesCount][nodesCount];
  27. int i=0;
  28. while((Line = in.readLine() )!=null){
  29. nodesArray[i] = new Node(i+1);
  30. String temp[]=Line.trim().split("\s+");
  31. for(int n=0; n<temp.length; n++)
  32. Matr[i][n]=Integer.parseInt(temp[n]);
  33. i++;
  34. }
  35. return Matr;
  36. }
  37. catch (IOException e) {
  38. throw new RuntimeException("Ошибка при работа с файлом: "+inputFileName,e);
  39. }
  40. }
  41. public static void addNodeToResult(int what){
  42. // добавляем вершину в список рассматриваемых:
  43. nodesArray[what].isVisited=true;
  44. currentResultList.add(what);
  45. }
  46. public static void printMatrix(int[][] Matrix) {
  47. String comma = "";
  48. for (int i = 0; i < Matrix.length; i++) {
  49. for (int j = 0; j < Matrix[0].length; j++) {
  50. if (i == Matrix.length - 1 && j == Matrix[0].length - 1)
  51. comma = ".";
  52. else
  53. comma = ",";
  54. System.out.print("{" + Matrix[i][j] + "}" + comma);
  55. }
  56. System.out.println();
  57. }
  58. }
  59. static int work(int currentVertex){
  60. LinkedList<Integer> childs = getChilds(currentVertex);
  61. if (childs.size() >= 1) {
  62. for (int child : childs) {
  63. if (!nodesArray[child].isVisited){
  64. currentResultList.add(new Integer(child));
  65. nodesArray[child].isVisited=true;
  66. work(child);
  67. }
  68. }
  69. }
  70. else {
  71. return currentVertex;
  72. }
  73. return 0;
  74. }
  75.  
  76. public static void clean() {
  77. nodesArray = null;
  78. incedenceMatrix = null;
  79. currentResultList = new LinkedList<Integer>();
  80. }
  81. private static LinkedList<Integer> getChilds(int from) {
  82. LinkedList<Integer> childs = new LinkedList<Integer>();
  83. for (int j = 0; j < incedenceMatrix[from].length; j++) {
  84. if(incedenceMatrix[from][j] == 1){
  85. childs.add(j);
  86. }
  87. }
  88. return childs;
  89. }
  90. }
  91.  
  92. public class Node{
  93. //поля
  94. public boolean isVisited=false;
  95. public String Label="";
  96. Node(int label){ // конструктор класса - вершины графа
  97. this.Label="Node "+label+"";
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement