Question: In this assignment, you'll be implementing a graph capable of doing depth first search and breadth first search algorithms. You'll also be creating a class
In this assignment, you'll be implementing a graph capable of doing depth first search and breadth first search algorithms. You'll also be creating a class with a main method for testing these graph algorithms. Upload the code as a Java program using the JavaSE JRE in Eclipse. Specifically, you need to create three classes: 'Node', 'Graph', and a third class with a main method for testing:
Node class, to store augmented information about one node. It should store all data fields to support both breadth first search and depth first search algorithms.
Private data fields of Node:
name, an int. So think of nodes being named ie numbered from to n for a size n graph.
parent, an int. means no parent.
dTime and fTime serving as DFS timestamps, which are ints. means the timestamp hasn't been set yet.
dist, an int to serve as BFS distances. A value of Integer.MINVALUE signifies the initial "negative infinity."
color, an int. Value means the node is white unvisited, means gray, and means black.
Methods of Node:
Constructor, which takes a parameter that gets copied into the name data field. It should also set the parent to dist to Integer.MINVALUE, and the other data fields to
toString method, to create a nicely labeled String with all the data field information and return it
Getters and setters for the data fields, though the name doesn't need a setter.
Graph class, which can store a graph and can perform BFS and DFS algorithms on it
Private data fields of Graph:
n an int. The number of nodes in the graph
time, an int used in DFS Starts at During DFS increment and assign from this data field to set a node's dTime or fTime timestamp.
edges, a d boolean array. This is an adjacency matrix storing edge information neighbors of every node. So entry ij being true or false tells you whether or not node i has an edge to node j
nodes, an array of Node objects. This stores all the augmented information about every node in the graph.
Class methods of Graph:
Constructor with a d boolean array parameter. The parameter should be assigned to edges data field, and its length should be assigned to n Initialize time to Allocate the nodes array as a length n array of Node handles, then use a loop to iterate through the nodes and assign each one a newly constructed Node object. A node's index in the nodes array is also its name, so node is the one at index
A public method depthFirstSearch with no parameters. Make sure to reinitialize all relevant Node data fields as appropriate for starting a DFSTo see if a node is white, look at one of the Node objects in the nodes array, and invoke its color getter method.
A private helper method dfsVisit with one parameter u for the node to start from. To iterate through the neighbors of some node j go through row j of the edges array, and each entry that's true is a neighbor.
A public method breadthFirstSearch with one parameter: an int node number for the source of the BFS Make sure to reinitialize all relevant Node data fields as appropriate for starting a DFS The queue used in the BFS can be a local variable here; feel free to use a standard library queue.
toString method to return a string describing the graph. This should include the entire contents of the edges adjacency matrix d array. Also you should display the node info you can loop through the nodes and use the Node toString.
Another class with a main method, for testing.
For at least three different graphs of size nodes, create the graph, perform a BFS algorithm, and display the results using the Graph's toString.
Then, create three more graphs of size nodes to perform the DFS algorithms on them and display the results.
Note: To construct a Graph object, you'll need to write down a graph as a D array with true and false values, before giving that array as argument to the Graph constructor.
You must implement your methods and classes described above from scratch, but you may use standard library console IO as well as a standard library class solely for queue operations.
Do not copy a solution from any person or third party source, but you may use the pseudocode in the textbook and the course webpage. Design and write the code yourself.
You can define a Java toString class method like this:
@Override
public String toString
return a String with nicely labeled, concatenated data fields
Remember to comment your code! You should have a header comment with a brief description of what your code does. Every nonprivate class method should have full comments above it describing the return type, name, parameters if any, and exactly what the method does.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
