CSIT 254 Project 4 – Graphs using Linked Lists
Purpose
To implement a Graph using a Linked List for the neighbors of a vertex.
Overview
For this project you will create a Graph as a collection of Nodes/Vertexes as ‘Locations’ and the
Links/Edges will be implemented as a Linked List of Locations from a Location
The user interface for the Project will be similar to the demo
“UndirectedNonWeightedExampleSlide19House.java” that is in the “Programming Lab 09 Graphs” in
Canvas. However in that demo the room numbers were fixed, here the rooms from the neighbors
Linked List will be numbered on output from 1 to n based on the neighbors that a room has.
The “HouseGraph.java” file is provided in Canvas. It extends the LinkedGraph.java that you will write.
The UML that follows describes the classes and after the UML are some descriptions of the methods.
The addVertex ( ) method that is in LinkedGraph is to add a vertex to the ‘known’ graph. The addEdge( )
method in LinkedGraph calls the addNeighbor( ) method in Location for the source Location.
1
CSIT 254 Project 4 – Graphs using Linked Lists
UML Diagrams
LinkedGraph
– vertexList : LinkedBag
+ LinkedGraph()
+ addVertex(newVertex: Location) : void
+ size():int
+ addEdge(source: Location, destination: Location): void
+ isEdge(source: Location, destination: Location): boolean
+ neighbors(vertex: Location) : Lister
HouseGraph
-kitchen : Location
-pantry : Location
-diningRoom : Location
-backFoyer : Location
-frontFoyer : Location
-study : Location
-livingRoom : Location
-stairs : Location
-upstairsHallway : Location
-homeLocation : Location
*
+HouseGraph( )
+getHomeLocation( ) : Location
* in this context, ‘home’ means starting Location
Location *
– description : String
– neighbors : LinkedBag
+ Location(initDescription: String)
+ getDescription() : String
+ isNeighbor(otherLocation: Location) : boolean
+ compareTo(otherLocation: Location) : int
+ equals(otherLocation: Location) : boolean
+ addNeighbor(neighbor: Location) : void
+ toString() : String
+ numNeighbors() : int
+ neighbors() : Lister
* Location implements Comparable
2
CSIT 254 Project 4 – Graphs using Linked Lists
Method/Constructor descriptions
LinkedGraph Class
+ LinkedGraph()
Constructor – initializes the vertexList linked list
+ addVertex(newVertex: Location) : void
addVertex method adds a new Location to known graph by adding it to the vertexList
+ size():int
size method returns the number of vertices in the known graph
+ addEdge(source: Location, destination: Location): void
addEdge method receives 2 locations. if the source and destination are in the known list of
vertices, the second location is added as a neighbor to the first Location passed.
(note: addNeighbor in location is called )
+ isEdge(source: Location, destination: Location): boolean
isEdge method receives 2 Locations. if the source and destination are in the known list of
vertices, the second Location is checked to see if it is a neighbor of the first Location.
(note: isNeighbor in Location is called)
+ neighbors(vertex: Location) : Lister
the neighbors method returns a Lister of Location which are the neighbors of the Location
passed.
(note: neighbors in location is called and passed on )
Method/Constructor descriptions
HouseGraph Class (extends LinkedGraph)
+ HouseGraph( )
Constructor – adds the ‘locations’ to the known graph and adds the ‘edges’ between the
locations
+ getHomeLocation( ) : Location
the getHomeLocation method returns “a” location as a starting point for the graph
3
CSIT 254 Project 4 – Graphs using Linked Lists
Method/Constructor descriptions
Location Class
+ Location(initDescription: String)
Constructor – initializes description and neighbors linked list
+ getDescription() : String
the getDescription method returns the description of the location
+ isNeighbor(otherLocation: Location) : boolean
the isNeighbor method returns a boolean that indicates if the otherLocation is a neighbor of this
location (does it exist in the linked list of locations?)
+ compareTo(otherLocation: Location) : int
Because our LinkedBag from Lab 5 is for comparable objects, we need to implement compareTo.
the compareTo method compares description fields.
+ equals(otherLocation: Location) : boolean
The equals method compares the description for this location to another
+ addNeighbor(neighbor: Location) : void
the addNeighbor method receives a location and adds it to the neighbors linked list
+ toString() : String
toString method returns a textual representation of this location using just the description
+ numNeighbors() : int
numNeighbors method returns the length of the neighbors linked list
+ neighbors() : Lister
the neighbors method returns the Lister from the neighbors linked list
4
CSIT 254 Project 4 – Graphs using Linked Lists
LinkedGraph
Javadoc (6) /name (1)
Private field vertexList
Constructor
+ addVertex(newVertex: Location)
+ size():int
+ addEdge(source: Location, destination: Location): void
+ isEdge(source: Location, destination: Location): boolean
+ neighbors(vertex: Location) : Lister
Naming guidelines
Indenting
Organization
Project will not be accepted late and must compile and run in order to receive credit
(50 points)
7 points
2 points
4 points
5 points
2 points
5 points
5 points
5 points
5 points
5 points
5 points
Location ( Comparable )
(67 points)
Javadoc (9) /name (1)
10 points
Private fields: – description : String, – neighbors : LinkedBag
5 points
Constructor – sets description, neighbors
4 points
+ getDescription() : String
2 points
+ isNeighbor(otherLocation: Location) : boolean
5 points
+ compareTo(otherLocation: Location) : int
5 points
+ equals(otherLocation: Location) : boolean
5 points
+ addNeighbor(neighbor: Location) : void
5 points
+ toString() : String
2 points
+ numNeighbors() : int
4 points
+ neighbors() : Lister
5 points
Naming guidelines
5 points
Indenting
5 points
Organization
5 points
Project will not be accepted late and must compile and run in order to receive credit
Driver
(75 points)
name (1)
1 points
Establishing House using HouseGraph extended from LinkedGraph
10 points
Sentinel controlled loop
10 points
Inside loop, display current location
5 points
Inside loop, display neighbors of current location
10 points
Inside loop, getting user input
5 points
Inside loop, displaying error if bad input
5 points
Inside loop, switching to new valid location
10 points
Naming guidelines
5 points
Indenting
5 points
Organization
5 points
Project will not be accepted late and must compile and run in order to receive credit
5
CSIT 254 Project 4 – Graphs using Linked Lists
Sample Run
run:
You are currently in room Front Foyer
neighbors of Front Foyer are:
1-Back Foyer, 2-Dining Room, 3-Living Room, 4-Stairs
Where would you like to go? (-1 to exit): 0
oops!
not a neighbor
You are currently in room Front Foyer
neighbors of Front Foyer are:
1-Back Foyer, 2-Dining Room, 3-Living Room, 4-Stairs
Where would you like to go? (-1 to exit): 2
You are currently in room Dining Room
neighbors of Dining Room are:
1-Front Foyer, 2-Pantry
Where would you like to go? (-1 to exit): 3
oops!
not a neighbor
You are currently in room Dining Room
neighbors of Dining Room are:
1-Front Foyer, 2-Pantry
Where would you like to go? (-1 to exit): 2
You are currently in room Pantry
neighbors of Pantry are:
1-Dining Room, 2-Kitchen
Where would you like to go? (-1 to exit): 2
You are currently in room Kitchen
neighbors of Kitchen are:
1-Back Foyer, 2-Pantry
Where would you like to go? (-1 to exit): 1
You are currently in room Back Foyer
neighbors of Back Foyer are:
1-Front Foyer, 2-Kitchen, 3-Study
Where would you like to go? (-1 to exit): 3
You are currently in room Study
neighbors of Study are:
6
CSIT 254 Project 4 – Graphs using Linked Lists
1-Back Foyer
Where would you like to go? (-1 to exit): 1
You are currently in room Back Foyer
neighbors of Back Foyer are:
1-Front Foyer, 2-Kitchen, 3-Study
Where would you like to go? (-1 to exit): 1
You are currently in room Front Foyer
neighbors of Front Foyer are:
1-Back Foyer, 2-Dining Room, 3-Living Room, 4-Stairs
Where would you like to go? (-1 to exit): 4
You are currently in room Stairs
neighbors of Stairs are:
1-Front Foyer, 2-Upstairs Stairs Hallway
Where would you like to go? (-1 to exit): 2
You are currently in room Upstairs Stairs Hallway
neighbors of Upstairs Stairs Hallway are:
1-Stairs
Where would you like to go? (-1 to exit): -2
oops!
not a neighbor
You are currently in room Upstairs Stairs Hallway
neighbors of Upstairs Stairs Hallway are:
1-Stairs
Where would you like to go? (-1 to exit): -1
Good bye!
BUILD SUCCESSFUL (total time: 52 seconds)
7