
CPSC 5115 Algorithm Analysis and Design
Fall 2008 Program 1 Due Thursday, September 11
This first program can be written in Java, C, C++, or Visual Basic, as can all programs for this
course. Other languages may be permitted; consult the instructor.
Your instructor has implemented this code in Java, so the description is for a Java class.
The goal of this assignment is to create and test a Java class that can be used to represent a graph.
1. The graph will have at least two vertices and not more than thirty–two vertices.
2. The graph will be undirected, with weighted edges. Unweighted graphs will
be represented by assigning each edge a weight of 1.
3. The graph class will be a public class, written to support future coding of graph
search algorithms such as Breadth First Search.
4. All vertices will be identified by an integer in the range 1 … N inclusive, where
N is the number of vertices. All edge weights will be positive integers.
5. The graph class must support the following public methods.
a) A constructor that allows specification of the vertex count. In my code, my constructor
is invoked as Graph g1 = new Graph(10) to create a graph with ten vertices.
b) A method addEdge(v1, v2, w) to add the edge (v1, v2) with weight w to the
graph. Optionally, this can be overloaded with the method addEdge(v1, v2)
to add an unweighted edge, which is an edge of weight 1.
c) Methods getVertexCount() and getEdgeCount() to return the number of
vertices in the graph and number of edges in the graph, respectively.
d) A method getVertexDegree(v) to return the degree of a vertex.
e) A method getEdgeWeight(v1, v2) to get the weight of the edge if it exists.
Return a weight of 0 if the edge does not exist.
f) A method getAdjacencyList(v) to return the list of vertices adjacent to
a given vertex. You may use either an array or a linked list as is easiest for you.
I can send you an example of using a function to return a fixed length array.
6. You must write another program to test the class and show that it works.
At a minimum, construct a graph with six vertices and ten edges.
Then report the vertex count, edge count, degree of each vertex, and weight of each edge.