Advertisement
Guest User

Untitled

a guest
Jun 15th, 2021
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1.  
  2. /**
  3. * Write a description of class Graph here.
  4. *
  5. * @author (your name)
  6. * @version (a version number or a date)
  7. */
  8. public class Graph {
  9.  
  10. private int numOfNodes;
  11. private boolean directed;
  12. private boolean weighted;
  13. private float[][] matrix;
  14.  
  15. /*
  16. This will allow us to safely add weighted graphs in our class since
  17. we will be able to check whether an edge exists without relying
  18. on specific special values (like 0)
  19. */
  20. private boolean[][] isSetMatrix;
  21.  
  22. public Graph(int numOfNodes, boolean directed, boolean weighted) {
  23.  
  24. this.directed = directed;
  25. this.weighted = weighted;
  26. this.numOfNodes = numOfNodes;
  27.  
  28. // Simply initializes our adjacency matrix to the appropriate size
  29. matrix = new float[numOfNodes][numOfNodes];
  30. isSetMatrix = new boolean[numOfNodes][numOfNodes];
  31. }
  32.  
  33. public void addEdge(int source, int destination) {
  34.  
  35. int valueToAdd = 1;
  36.  
  37. if (weighted) {
  38. valueToAdd = 0;
  39. }
  40.  
  41. matrix[source][destination] = valueToAdd;
  42. isSetMatrix[source][destination] = true;
  43.  
  44. if (!directed) {
  45. matrix[destination][source] = valueToAdd;
  46. isSetMatrix[destination][source] = true;
  47. }
  48. }
  49.  
  50. public void addEdge(int source, int destination, float weight) {
  51.  
  52. float valueToAdd = weight;
  53.  
  54. if (!weighted) {
  55. valueToAdd = 1;
  56. }
  57.  
  58. matrix[source][destination] = valueToAdd;
  59. isSetMatrix[source][destination] = true;
  60.  
  61. if (!directed) {
  62. matrix[destination][source] = valueToAdd;
  63. isSetMatrix[destination][source] = true;
  64. }
  65. }
  66.  
  67. public void printMatrix() {
  68. for (int i = 0; i < numOfNodes; i++) {
  69. for (int j = 0; j < numOfNodes; j++) {
  70. // We only want to print the values of those positions that have been marked as set
  71. if (isSetMatrix[i][j])
  72. System.out.format("%8s", String.valueOf(matrix[i][j]));
  73. else System.out.format("%8s", "/ ");
  74. }
  75. System.out.println();
  76. }
  77. }
  78.  
  79. public void printEdges() {
  80. for (int i = 0; i < numOfNodes; i++) {
  81. System.out.print("Node " + i + " is connected to: ");
  82. for (int j = 0; j < numOfNodes; j++) {
  83. if (isSetMatrix[i][j]) {
  84. System.out.print(j + " ");
  85. }
  86. }
  87. System.out.println();
  88. }
  89. }
  90.  
  91. public boolean hasEdge(int source, int destination) {
  92. return isSetMatrix[source][destination];
  93. }
  94.  
  95. public Float getEdgeValue(int source, int destination) {
  96. if (!weighted || !isSetMatrix[source][destination])
  97. return null;
  98. return matrix[source][destination];
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement