Advertisement
akosiraff

Adjacency Matrix

Oct 9th, 2013
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.16 KB | None | 0 0
  1.  
  2. Download: http://solutionzip.com/downloads/adjacency-matrix/
  3. Ex 1: Adjacency Matrix
  4. Although adjacency lists are simple, an adjacency matrix representation is easier for representing graphs with weighted edges.
  5. We will implement an adjacency matrix representation for weighted graphs, and then use this representation for running some weighted graph algorithms.
  6. 1
  7. Make a new project in Eclipse, called WeightedGraph.
  8. 2
  9. To save time, we won’t implement a Node ADT. Instead, add a class Node and give it the following structure:
  10. import java.util.List;
  11. public class Node {
  12. // The value in the node itself
  13. public Object value;
  14. // The distance from this node to every other node
  15. public List edges;
  16. public Node(Object value);
  17. }
  18. Fill in the constructor for the Node class to just set value to the one passed in, and initialise edges to an empty ArrayList.
  19. 3
  20. Add the following WeightedGraph ADT to your project:
  21. import java.util.List;
  22. public interface WeightedGraph {
  23. public int size();
  24. public boolean isEmpty();
  25. public List getNodes();
  26. public void addNode(Node n);
  27. public void removeNode(Node n);
  28. public void addEdge(Node source, Node destination, int weight);
  29. }
  30. 4
  31. Create a class WeightedDirectedGraph that implements WeightedGraph.
  32. Now let’s go through and implement each of the methods:
  33. a
  34. Member variables
  35. In an adjacency matrix representation, each nodes stores its neighbours as a list of integers (with each integer representing the weight of the edge between those two nodes).
  36. For example, in a graph containing 3 nodes A, B and C, the matrix of neighbours might look like:
  37. (
  38. 0 3 0
  39. 0 0 1
  40. 0 0 0
  41. )
  42. These weights correspond to the following edges:
  43. (
  44. A?A A?B A?C
  45. B?A B?B B?C
  46. C?A C?B C?C
  47. )
  48. This means that this graph only has two edges:
  49. A?B, with a weight of 3
  50. B?C, with a weight of 1
  51. To represent this in Java, we can store each row in this matrix in a Node object, but we still need a list somewhere (of all the nodes) to use as a reference. This list (which is (A,B,C) in the example above) will be used to determine which index of the matrix each node corresponds to (which, in the example above, is 0 for A, 1 for B and 2 for C).
  52. So we just need a list of nodes in our WeightedDirectedGraph object:
  53. private List nodes;
  54. The order of nodes in this list is important, so we have to be careful to update the neighbours for each node whenever we make changes to this list.
  55. b
  56. Constructor
  57. Make the constructor take no arguments, and just set the nodes list to a new ArrayList.
  58. c
  59. size() and isEmpty()
  60. Just call the corresponding size() and isEmpty() on the list of nodes.
  61. d
  62. getNodes()
  63. Just return the list of nodes.
  64. e
  65. addNode()
  66. For the addNode method, we need to do 3 things:
  67. Add an edge with weight 0 (i.e. no edge) from the node we’re about to add to every other node (including itself)
  68. Add an edge with weight 0 from every other node to this new node
  69. Add this node to the list of nodes
  70. Let’s go through each of these methods one-at-a-time.
  71. Firstly, we can add an edge from this node to every other node with weight 0:
  72. for (Node m : nodes) {
  73. n.edges.add(0);
  74. }
  75. We then also need to add an edge from this node to itself:
  76. n.edges.add(0);
  77. Now we need to do the reverse – add an edge from every other node back to this node:
  78. for (Node m : nodes) {
  79. m.edges.add(0);
  80. }
  81. Finally, with all the edges added, we can add this new node to our list of nodes:
  82. nodes.add(n);
  83. Note
  84. Sometimes, when Eclipse warns you that the value of a local variable is not used, its a hint that your method could use some refactoring. In this case, you can improve it to only use a single for-loop (instead of two for-loops).
  85. See if you can refactor your addNode() method to use only a single for-loop.
  86. f
  87. removeNode()
  88. Removing a node is similar to adding one, except slightly simpler.
  89. To remove a node, we need to:
  90. Remove the index entry corresponding to that node from every other node’s list of edges
  91. Remove the node itself from our list of nodes
  92. First, we should find the index of the node we want to remove:
  93. int index = this.nodes.indexOf(n);
  94. Now we can remove that index from every other node (which corresponds to removing the edge that links those two nodes together):
  95. for (Node node : nodes) {
  96. node.edges.remove(index);
  97. }
  98. Lastly, we can remove the node itself:
  99. this.nodes.remove(index);
  100. Now the node has been removed from the graph, and all other nodes contain correct lists of edge weights to all other nodes.
  101. g
  102. addEdge()
  103. Similarly to removeNode(), we need to first find the index of the node we want to the edge to point towards (the destination of the edge).
  104. Then, once we have this index, set the value in source.edges at that index to weight.
  105. Hint
  106. You can use List.set() to set the value of an item at a particular index in a list.
  107. For example, in this case, you could use:
  108. source.edges.set(i, 2);
  109. to set the value at index i to 2.
  110. Great! You should now have a working graph with an adjacency matrix representation.
  111. 5
  112. To test our WeightedDirectedGraph, we can use very similar tests to those we used when we wrote our Graph class using an adjacency-list representation. However, we will need to slightly modify them to use weights as well (since our new WeightedGraph implementation supports edges of differing weights).
  113. a
  114. testConstruction()
  115. Add a test that ensures that, for a newly constructed WeightedDirectedGraph:
  116. The size() method returns 0
  117. The isEmpty() method returns true
  118. The getNodes() method returns an empty list
  119. b
  120. testSize()
  121. Create a graph of 3 nodes, ensuring size() returns the correct value at each step, and then slowly remove nodes and ensure size() remains correct at every stage.
  122. c
  123. testIsEmpty()
  124. The same as testSize(), but ensure the results of isEmpty() are correct.
  125. d
  126. testAddNode()
  127. Create a graph of 3 nodes, and ensure that getNodes() returns the correct values (in the correct order – the order that they were added).
  128. e
  129. testRemoveNode()
  130. Create a graph of 3 nodes, remove the second one, and ensure that getNodes() returns the correct nodes.
  131. Then try and remove the other 2 nodes, and check that getNodes() returns an empty list.
  132. f
  133. testSmallGraph()
  134. Create the example weighted graph from the Weighted Graphs section by constructing each node and adding edges between them like so:
  135. WeightedGraph g = new WeightedDirectedGraph();
  136. Node A = new Node(“A”);
  137. Node B = new Node(“B”);
  138. Node C = new Node(“C”);
  139. Node D = new Node(“D”);
  140. Node E = new Node(“E”);
  141. g.addNode(A);
  142. g.addNode(B);
  143. g.addNode(C);
  144. g.addNode(D);
  145. g.addNode(E);
  146. g.addEdge(A, B, 2);
  147. g.addEdge(A, C, 5);
  148. g.addEdge(B, D, 3);
  149. g.addEdge(C, D, 6);
  150. g.addEdge(D, E, 9);
  151. Then look at the edges member variable on each node and ensure they are correct.
  152. For example, to test the edges member variable for node A, you could use:
  153. assertEquals(Arrays.asList(0, 2, 5, 0, 0), A.edges);
  154. Note
  155. You may find it helpful to work out the adjacency matrix on pen-and-paper before writing the test for each node.
  156. Great job! You should now have a fully working directed graph implementation using an adjacency matrix.
  157. You can now use WeightedDirectedGraph objects to represent weighted graphs, which will help you complete the following exercises.
  158.  
  159. Download: http://solutionzip.com/downloads/adjacency-matrix/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement