Skip to content

Commit

Permalink
Add strategy pattern to graph search functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
theViz343 committed Nov 29, 2023
1 parent 3c6fa30 commit e170257
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
16 changes: 16 additions & 0 deletions src/main/java/Context.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
9 changes: 5 additions & 4 deletions src/main/java/GraphData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 14 additions & 9 deletions src/main/java/GraphSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,22 @@ public Path graphSearch() {
protected abstract Path getPath();
}

class BFS extends GraphSearchAlgorithm {
class BFS extends GraphSearchAlgorithm implements SearchStrategy {
private LinkedList<String> queue;

public BFS(String src, String dst, Graph graph) {
super(src, dst, 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)) {
Expand All @@ -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) {
Expand All @@ -81,21 +83,23 @@ protected Path getPath() {
}
}

class DFS extends GraphSearchAlgorithm {
class DFS extends GraphSearchAlgorithm implements SearchStrategy {
private Stack<String> stack;

public DFS(String src, String dst, Graph graph) {
super(src, dst, 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)) {
Expand All @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/SearchStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public interface SearchStrategy {
void selectAlgorithm();
void executeAlgorithm();
Path getPath();
}

0 comments on commit e170257

Please sign in to comment.