Last edited November 30, 2016
Assignment 4 - ITSC 2214 - Fall 2016
Objectives
At the completion of this assignment, students will have gained experience using adjacency
matrix to store an undirected graph.
Before you work on the project, make sure you read the appropriate material from Lewis &
Chase.
Instructions
This assignment creates an adjacency matrix for an undirected graph that represents the
connections between locations. The edges have a weight (cost) associated with them. An
adjacency matrix captures the connections between locations in a square matrix. The
elements of the matrix indicate whether pairs of vertices are adjacent or not in the graph. For
example, the graph below with 4 nodes (0-3) has edges with costs as shown in the figure.
0
2
1
1
2
3
3
2
The adjacency matrix shown below captures the connections of all the nodes. For example,
node 0 has an edge to node 1 with a cost of 2. This is shown in position [0][1] with a value of
2. And node 1 does not have a path to node 3, shown in the adjacency matrix as -1.
0
1
2
3
0
0
2
2
1
1
2
0
3
-1
2
2
3
0
-1
3
1
-1
-1
0
•
•
v1.2
0 = no cost of traversing
-1 = no path between two
x = cost of traversing
This is an undirected graph, the triangle above the diagonal is the same as the triangle
below the diagonal.
The adjacency matrix is a two dimensional int array. You create a 2D matrix of int
entries as follows:
Last edited November 30, 2016
int matrix[][]= new int[num][num];
For this assignment you have to implement several algorithms that compute interesting
properties of a graph. Note that many of the computations mentioned below can be done
with a simple look up into the adjacency matrix (array).
Number of Nodes: this is simply the number of nodes read in. You can also think about it as
the side of one of the dimensions of the adjacency matrix. In the example above, the number
of nodes is 4.
Number of Edges: this is the number of entries in the adjacency matrix that have a > 0 value.
In the example above, the number of edges is 4. Note that the adjacency matrix might have
twice that many because the edges are undirected (no direction) and thus are typically
repeated across the diagonal. Still, the number of edges is still 4.
Degree of a node: the degree of a node in a graph is the number of edges connected to that
node. In the example above, node 0 has a degree of 3 and node 1 has a degree 2.
Is there an edge between any two given nodes? If there is a value > 0 in the cell that
intersects the two nodes, then there is an edge between the two nodes. This routine checks
to see if there is a direct edge between the two nodes.
Is there a path between any two given nodes? Returns true if there is a path (a series of
edges) that connects two given nodes. This checks a path of any length.
Is the graph complete? An undirected graph is complete if every pair of distinct nodes is
connected by an edge. This means that for every node there is a path to every other node in
the graph. The example above is not a complete graph as there is no edge that connects
node 3 to node 2. There are several other connections missing, but just missing one is
enough to determine that the graph is not complete.
Is the graph connected? A graph is connected if there is a path between every pair of
vertices. A disconnected graph typically has some subset of the graph that does not connect
to the rest of the graph. In the example above, the graph is connected, as there is a path for
every pair of vertices.
Input to the Program
Typically we give nodes names with letters, A, B, etc. Often when we are using locations, the
names become names of buildings or cities. In the example above, the node name is simply a
number. Your program reads a file that contains all the information needed to build an
adjacency matrix. The code is given to you. The format of the file is very simple with two types
of lines. The node: line contains the names of the nodes. The edge: line contains the name of
two nodes and the weight of the edge between them. There can be many input lines of each
type but all node: lines must appear before the first edge: line.
node: name
v1.2
Last edited November 30, 2016
edge: name,name,cost
As example, the graph in the example above is represented with this file:
node:
node:
node:
node:
edge:
edge:
edge:
edge:
0
1
2
3
0,1,2
0,2,2
0,3,1
1,2,3
Note that the input has a node name (String) but the adjacency matrix must be index by
numbers. You must store the node names in some data structure and convert a node name to
an index. Hint: put node names in an ArrayList and use the indexOf() routine to do the
conversion.
Files available for you
1. Assignment4.java – contains a sample main program that calls different methods in
the AdjacencyGraph class.
2. Graph.java – abstract class that contains the readInData() method and other
methods that you must implement (see next section for more details). Do not modify
this file.
3. AdjacencyGraph.java is your implementation of the Graph.java class. You must
provide code to implement the functionality described above and the documented in
the Graph.java file.
Requirements
1. Reuse the DS_Library you used in Assignment 3.
2. Create a NetBeans project and call it Assignment4. Add the files:
Assignment4.java, IllegalStateForMatrix.java, and Graph.java. Make
sure you right click over the Library folder and select “Add Project.” Then select the
DS_library project as the library for your Assignment4.
3. Create a new class in the Assignment4 project and name it AdjacencyGraph. This
class should implement the Graph. The class definition looks like this:
package Assignment4;
…
class AdjacencyGraph extends Graph { … }
AdjacencyGraph
This class is an extension of Graph. You must implement several methods that are specified
as abstract in the class Graph.
v1.2
Last edited November 30, 2016
The buildGraph() method reads the file and calls other methods to build the adjancency
matrix. This method is given to you. You must implement the following methods:
void createAdjacencyMatrix();
void addNode(String nodeName);
void addEdge(String fromNode, String toNode, int weight)
throws ElementNotFoundException, IllegalStateForMatrix;
int getNumberOfNodes();
int getNumberOfEdges();
throws IllegalStateForMatrix;
int getDegree(String nodeName)
throws ElementNotFoundException, IllegalStateForMatrix;
boolean hasEdgeBetween(String fromName, String toName)
throws ElementNotFoundException, IllegalStateForMatrix;
boolean isGraphComplete()
throws IllegalStateForMatrix;
boolean isGraphConnected()
throws IllegalStateForMatrix;
ArrayList<String> getPathBetween(String fromName, String toName)
throws ElementNotFoundException, IllegalStateForMatrix;
Exceptions
Two exceptions are used in this program. ElementNotFoundException is thrown
whenever a method is called with a node name that does not exist. Calling
getDegree(“A”), for example, on a graph that does not have the node A will produce an
exception.
The second exception, IllegalStateForMatrix, is thrown if the
createAdjacencyMatrix is not called before other routines. Note that the code in
buildGraph first reads all the node names and then creates the matrix. If you attempt to call
most other methods before createAdjacencyMatrix is called, you should get this
exception.
Testing
To test this program, we test files that you can use for your own testing. If you plan to read
any of these files from your main program, place the file at the root of your NetBeans project
folder. If you want to read these files from your JUnit tests, place the files inside of the test
folder in your NetBeans project folder. Note that you must move these files with your file
manager, not within NetBeans.
We strongly suggest you create data files of your own so that you can be sure that your code
works for other examples. In particular, create some graphs that are not connected or that are
complete so you can test those properly.
Test Data Files Provided
File: data0.txt, the example from above, 4 nodes and 4 edges
File: data1.txt, the partial UNCC campus map shown at the end, 10 nodes, 24 edges
v1.2
Last edited November 30, 2016
Submission
You must submit this project to Web-CAT using the Submitter plugin that you installed in
NetBeans. Web-CAT will be grading your project and giving you feedback as to where you
are missing points.
• You will have only 30 submissions to use for the assignment, so use them wisely.
• With each submission you are restricted to get only 5 hints. Note that if you have more
than 5 errors in your code, you still will get only 5 hints per submission.
Grading
Web-CAT assigns grades based on how closely your project complies with the specifications.
We have basically written a series of JUnit test cases and when you submit your project,
Web-CAT runs your code against these cases. That score counts towards the Functional
requirement and it is 60% of your score. Your coding style (indentation, proper variable
names, etc.) and comments count for 40% of your grade.
Due Date
The project is due on Wednesday December 7th, 2016 by 11:45pm (almost midnight). We
will accept late submissions until Friday December 9th by 11:45pm.
Bonus Points
We will give bonus points for early final submissions. You will get 5 points per day for early
submission for up to 10 total points.
v1.2
Last edited November 30, 2016
Campus Building Example
6
4
3
1
3
8
1
2
1
1
2
9
1
10
2
1
1
1
1
5
2
4
2
1
1
7
v1.2
1
2
2
1
2
© Copyright 2026 Paperzz