diff --git a/src/main/java/Context.java b/src/main/java/Context.java new file mode 100644 index 0000000..93608ce --- /dev/null +++ b/src/main/java/Context.java @@ -0,0 +1,16 @@ +import org.jgrapht.Graph; +import org.jgrapht.graph.DefaultEdge; + +public class Context { + SearchStrategy strategy; + Context(SearchStrategy strategy) + { + this.strategy = strategy; + } + + Path searchByStrategy(){ + strategy.selectAlgorithm(); + strategy.executeAlgorithm(); + return strategy.getPath(); + } +} \ No newline at end of file diff --git a/src/main/java/GraphData.java b/src/main/java/GraphData.java index 108883a..de639b5 100644 --- a/src/main/java/GraphData.java +++ b/src/main/java/GraphData.java @@ -141,18 +141,19 @@ public boolean addEdge(String srcLabel, String dstLabel) { } public Path GraphSearch(String src, String dst, Algorithm algo) { - GraphSearchAlgorithm searchAlgorithm; + SearchStrategy strategy; switch(algo) { case BFS: - searchAlgorithm = new BFS(src, dst, graphObject); + strategy = new BFS(src, dst, graphObject); break; case DFS: - searchAlgorithm = new DFS(src, dst, graphObject); + strategy = new DFS(src, dst, graphObject); break; default: throw new IllegalArgumentException("Invalid choice of algorithm"); } - return searchAlgorithm.graphSearch(); + Context searchContext = new Context(strategy); + return searchContext.searchByStrategy(); } public void removeEdge(String srcLabel, String dstLabel) throws Exception { DefaultEdge edgeexisting = graphObject.getEdge(srcLabel, dstLabel); diff --git a/src/main/java/GraphSearch.java b/src/main/java/GraphSearch.java index 72eb14a..3f4eacf 100644 --- a/src/main/java/GraphSearch.java +++ b/src/main/java/GraphSearch.java @@ -33,7 +33,7 @@ public Path graphSearch() { protected abstract Path getPath(); } -class BFS extends GraphSearchAlgorithm { +class BFS extends GraphSearchAlgorithm implements SearchStrategy { private LinkedList queue; public BFS(String src, String dst, Graph graph) { @@ -41,13 +41,14 @@ public BFS(String src, String dst, Graph graph) { queue = new LinkedList<>(); } - protected void selectAlgorithm() { + @Override + public void selectAlgorithm() { System.out.println("Using BFS"); visited.put(source, true); queue.add(source); } - - protected void executeAlgorithm() { + @Override + public void executeAlgorithm() { while (!queue.isEmpty()) { String src = queue.poll(); if (src.equals(destination)) { @@ -64,7 +65,8 @@ protected void executeAlgorithm() { } } - protected Path getPath() { + @Override + public Path getPath() { String node = destination; path.add(node); if (visited.get(destination) != null) { @@ -81,7 +83,7 @@ protected Path getPath() { } } -class DFS extends GraphSearchAlgorithm { +class DFS extends GraphSearchAlgorithm implements SearchStrategy { private Stack stack; public DFS(String src, String dst, Graph graph) { @@ -89,13 +91,15 @@ public DFS(String src, String dst, Graph graph) { stack = new Stack<>(); } - protected void selectAlgorithm() { + @Override + public void selectAlgorithm() { System.out.println("Using DFS"); visited.put(source, true); stack.push(source); } - protected void executeAlgorithm() { + @Override + public void executeAlgorithm() { while (!stack.isEmpty()) { String src = stack.pop(); if (src.equals(destination)) { @@ -112,7 +116,8 @@ protected void executeAlgorithm() { } } - protected Path getPath() { + @Override + public Path getPath() { String node = destination; path.add(node); if (visited.get(destination) != null) { diff --git a/src/main/java/SearchStrategy.java b/src/main/java/SearchStrategy.java new file mode 100644 index 0000000..251dba3 --- /dev/null +++ b/src/main/java/SearchStrategy.java @@ -0,0 +1,6 @@ +public interface SearchStrategy { + void selectAlgorithm(); + void executeAlgorithm(); + Path getPath(); +} +