patternjavaMinor
Yet another graph search
Viewed 0 times
yetgraphanothersearch
Problem
I noticed there were already countless implementations of graphsearch procedures on here, but there were a couple of features of my approach that I haven't seen in the others, plus I would greatly appreciate some experienced eyes on my code (my university is pretty miserly with constructive criticism).
I wrote this as an assignment (already submitted for marking) so I had a few constraints. The most interesting (?) was that we were told to create a single method which could do DFS, BFS, A/A* and Uniform Cost Search - in other words, they wanted us to see that the difference lay in the ordering of the open set, not in the program logic.
```
/**
* Constructor for objects of class Search
*
* @param strat strategy to use
* @param iterations diagnostic iterations
* @param newGraph the graph we're going to search.
*/
public Search(String strat, int iterations, Graph newGraph) {
// initialise instance variables
strategy = strat;
diagIterations = iterations; // how many 'turns' to give full diag for.
currentIterations = 0;
graph = newGraph;
open = new LinkedList<>();
closed = new LinkedHashSet<>();
output = new OutputBlock(strategy, diagIterations, graph.getGridSize());
diagQueue = new LinkedList<>();
position = graph.findStart().getCoords();
// path = new StringBuilder("S");
// We can add the starting node to the open set now.
open.add(graph.findStart());
}
/**
* When this method is called, we start the search for a path. DiagBlocks are generated and kept
* in an OutputBlock, which is returned by the method at the end.
*
* @return an outputblock containing the output of the search
*
*/
public OutputBlock run() {
long startTime = System.currentTimeMillis();
boolean diag = false; // We'll flip this back if we need to.
switch (strategy) {
case "D":
System.out.println("Initiating Depth-First search...");
break;
case "B":
I wrote this as an assignment (already submitted for marking) so I had a few constraints. The most interesting (?) was that we were told to create a single method which could do DFS, BFS, A/A* and Uniform Cost Search - in other words, they wanted us to see that the difference lay in the ordering of the open set, not in the program logic.
```
/**
* Constructor for objects of class Search
*
* @param strat strategy to use
* @param iterations diagnostic iterations
* @param newGraph the graph we're going to search.
*/
public Search(String strat, int iterations, Graph newGraph) {
// initialise instance variables
strategy = strat;
diagIterations = iterations; // how many 'turns' to give full diag for.
currentIterations = 0;
graph = newGraph;
open = new LinkedList<>();
closed = new LinkedHashSet<>();
output = new OutputBlock(strategy, diagIterations, graph.getGridSize());
diagQueue = new LinkedList<>();
position = graph.findStart().getCoords();
// path = new StringBuilder("S");
// We can add the starting node to the open set now.
open.add(graph.findStart());
}
/**
* When this method is called, we start the search for a path. DiagBlocks are generated and kept
* in an OutputBlock, which is returned by the method at the end.
*
* @return an outputblock containing the output of the search
*
*/
public OutputBlock run() {
long startTime = System.currentTimeMillis();
boolean diag = false; // We'll flip this back if we need to.
switch (strategy) {
case "D":
System.out.println("Initiating Depth-First search...");
break;
case "B":
Solution
In my option it would be nicer to have an enum for the strategy rather than a String. This would only allow valid input and any developer using your class would know what strategys are supported without digging around in your code since the supported values are not part of the javadocs.
Also it is somewhat useless to use a string for the strategy while it is only one character long. A simple byte/char would minimize memory usage.
Also it is somewhat useless to use a string for the strategy while it is only one character long. A simple byte/char would minimize memory usage.
Context
StackExchange Code Review Q#87630, answer score: 2
Revisions (0)
No revisions yet.