CSIT 254 – Programming Lab 9 – GraphsPurpose
To work with an adjacency matrix for a Weighted Graph
A Working ‘Model’ of a Non-Weighted Graph
In the .zip files ‘ProgLab09GraphsSum18.zip’ and ‘ProgLab09GraphsSum18_non_netbeans.zip’ are a
series of files.
•
•
•
Graph.java is an implementation of a non-weighted graph using an adjacency matrix
UndirectedNonWeightedExampleSlide17.java is a ‘driver’ that has implemented the undirected
non-weighted graph from slide 17 (see a later page) and uses Graph. It allows the ‘user’ to
meander around the Graph.
UndirectedNonWeightedExampleSlide19House.java is a ‘driver’ that has implemented the
undirected non-weighted graph from slide 19 (see a later page) and uses Graph. It allows the
‘user’ to meander around the Graph.
Implement WeightedGraph and use it in a program.
In the .zip files is WeightedGraph.java and DirectedWeightedExampleSlide18.java.
The WeightedGraph.java has only stubs implemented.
•
•
•
Add the fields
Fill in the constructor
Complete the 9 methods
The DirectedWeightedExampleSlide18.java has the statements to create the directed weighted graph
from slide 18
•
Add the logic to allow the ‘user’ to meander around the graph
1
CSIT 254 – Programming Lab 9 – Graphs
Grading Rubric (WeightedGraph) (56 points)
Item
Comment in program with name (2 points)
Fields (5 points)
Constructor (allocate graph) (10 points)
addEdge ( 3 points)
getLabel ( 3 points)
isEdge ( 3 points)
neighbors ( 15 points)
removeEdge ( 3 points)
setLabel ( 3 points)
size ( 3 points)
getWeight ( 3 points)
setWeight ( 3 points)
Grading Rubric (Program that Interacts with user) (61 points)
Item
Comment in program with name (2 points)
Creation of Graph and establishment of edges(already done)
Sentinel controlled loop based on int (10 points)
Displaying current vertex (already done)
Displaying neighbors (already done)
Accepting int command from user(5 points)
gracefully exit on request -1
[ programs that stop looping for invalid commands will not be accepted] (5 points)
Reporting a value as too large or too small
(5 points)
Reporting an invalid neighbor as invalid
(5 points)
Changing current vertex to valid selection
(5 points)
following the naming guidelines for Classes and identifiers (8 points)
proper indenting (methods indented inside class, statements indented inside method) (8
points)
organization (variables declared up top) (8 points)
Lab must compile and run in order to receive credit
See late policy in syllabus.
2
CSIT 254 – Programming Lab 9 – Graphs
UndirectedNonWeightedExampleSlide17.java
run:
You are currently at node 2-Spot two
neighbors of 2 are:
0-Spot zero
3-Spot three
Where would you like to go? (-1 to exit): 3
You are currently at node 3-Spot three
neighbors of 3 are:
0-Spot zero
1-Spot one
2-Spot two
Where would you like to go? (-1 to exit): 1
You are currently at node 1-Spot one
neighbors of 1 are:
0-Spot zero
3-Spot three
Where would you like to go? (-1 to exit): 0
You are currently at node 0-Spot zero
neighbors of 0 are:
1-Spot one
2-Spot two
3-Spot three
Where would you like to go? (-1 to exit): 2
You are currently at node 2-Spot two
neighbors of 2 are:
0-Spot zero
3-Spot three
Where would you like to go? (-1 to exit): -1
Good bye!
BUILD SUCCESSFUL (total time: 35 seconds)
3
CSIT 254 – Programming Lab 9 – Graphs
UndirectedNonWeightedExampleSlide19House.java
run:
You are currently in room 4-Front Foyer
neighbors of 4 are:
2-Dining Room
3-Back Foyer
6-Living Room
Where would you like to go? (-1 to exit): 5
You can’t go to 5-Study from 4-Front Foyer
You are currently in room 4-Front Foyer
neighbors of 4 are:
2-Dining Room
3-Back Foyer
6-Living Room
Where would you like to go? (-1 to exit): 3
You are currently in room 3-Back Foyer
neighbors of 3 are:
0-Kitchen
4-Front Foyer
5-Study
Where would you like to go? (-1 to exit): 5
You are currently in room 5-Study
neighbors of 5 are:
3-Back Foyer
Where would you like to go? (-1 to exit): 33
33 is not a valid value
Where would you like to go? (-1 to exit): 3
You are currently in room 3-Back Foyer
4
CSIT 254 – Programming Lab 9 – Graphs
neighbors of 3 are:
0-Kitchen
4-Front Foyer
5-Study
Where would you like to go? (-1 to exit): -1
Good bye!
BUILD SUCCESSFUL (total time: 37 seconds)
5
CSIT 254 – Programming Lab 9 – Graphs
DirectedWeightedExampleSlide18.java
run:
so far you have accrued 0 cost
You are currently at vertex 2-Spot two
neighbors of 2 are:
# Neighbor
Cost
=== ========== ====
3 Spot three
12
Where would you like to go? (-1 to exit)2
You can’t go to 2 from 2
so far you have accrued 0 cost
You are currently at vertex 2-Spot two
neighbors of 2 are:
# Neighbor
Cost
=== ========== ====
3 Spot three
12
Where would you like to go? (-1 to exit)3
so far you have accrued 12 cost
You are currently at vertex 3-Spot three
neighbors of 3 are:
# Neighbor
Cost
=== ========== ====
0 Spot zero
15
1 Spot one
6
Where would you like to go? (-1 to exit)2
You can’t go to 2 from 3
6
CSIT 254 – Programming Lab 9 – Graphs
so far you have accrued 12 cost
You are currently at vertex 3-Spot three
neighbors of 3 are:
# Neighbor
Cost
=== ========== ====
0 Spot zero
15
1 Spot one
6
Where would you like to go? (-1 to exit)0
so far you have accrued 27 cost
You are currently at vertex 0-Spot zero
neighbors of 0 are:
# Neighbor
Cost
=== ========== ====
2 Spot two
9
Where would you like to go? (-1 to exit)-1
Good bye!
BUILD SUCCESSFUL (total time: 28 seconds)
7