You are on page 1of 3

U NIVERSITY OF THE W ITWATERSRAND , J OHANNESBURG

COMS2003: Application and Analysis of Algorithms II


A3 Laboratory 15 2012

Weighted Graphs

Last weeks lab focussed on unweighted graphs. Today we will make changes to the representation used last week in order to accomodate for weighted graphs. We will also implement the minimum weighted spanning tree algorithm. 1. This weeks lab will use your code from Lab 13. Open this code in Netbeans. 2. Some modications need to be made in order to handle weighted graphs. 3. In last weeks lab, we represented the adjacencies as a LinkedList of Vertex objects. This does not allow us to assign weights to the edges, as we are storing the vertices themselves in the adjacency lists. In order to get around this, we introduce the WeightedEdge class.

WeightedEdge class
1. You need to create a class that can store an edge. It must thus have the following two properties. destination - which is a Vertex object storing the destination of the edge weight - which is an integer representing the cost of the edge Note that it doesnt need to store both vertices that the edge is incident to, it only keeps the destination Vertex. This is because the WeightedEdge object will be added to the adjacency list of the source vertex. 2. In addition to these properties, the class must have the folowing methods a constructor, which takes in a Vertex object and an integer, representing the destination and weight of the WeightedEdge respectively. getWeight - takes in no parameters and returns the weight of the edge. getDestination - takes no parameters and returns the destination vertex.

Modications
1. The Vertex class must be modied so that the adjacencies LinkedList contains WeightedEdge objects instead of Vertex objects. This means that the addAdjacency method must also be modied so that it takes in a WeightedEdge object instead of a Vertex object. 2. The addEdge method in the Graph class must also be modied so that it takes in two vertex numbers AND an integer representing the weight rather than just taking in the vertex numbers as previously. Since this is an undirected graph, it must create two WeightedEdges, one leading to the rst Vertex, and another leading to the second Vertex and it should add them to each others adjacency lists. Both WeightedEdges should have the weight that comes from the method. 3. The main program must be changed so that it outputs the total weight of each edge using the format described in the next section.

Input and Output


As preparation for implementation of the minimum weighted spanning tree algorithm, your program must prove that it can handle weights. The input to the program looks similar to last week, but with an added piece of information, being the weight of the edge. 11 0,1,7 0,2,2 0,3,8 2,4,9 3,5,17 3,6,3 3,7,6 7,8,11 7,9,1 9,10,6 -1 The program must output the total weight all edges from each vertex 0:17 1:7 2:11 3:34 2

4:9 5:17 6:3 7:18 8:11 9:7 10:6 Note that this program will form the basis of Mondays lab, so be sure to get it right.

You might also like