Skip to content

Commit

Permalink
Implement template pattern for graph search algorithms.
Browse files Browse the repository at this point in the history
  • Loading branch information
theViz343 committed Nov 29, 2023
1 parent 4478848 commit 3c6fa30
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 65 deletions.
73 changes: 8 additions & 65 deletions src/main/java/GraphData.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,75 +141,18 @@ public boolean addEdge(String srcLabel, String dstLabel) {
}

public Path GraphSearch(String src, String dst, Algorithm algo) {
Path p = new Path();
String source = src;
HashMap<String, Boolean> visited = new HashMap<>();
HashMap<String, String> parent = new HashMap<>();

// Select algorithm and apply it
GraphSearchAlgorithm searchAlgorithm;
switch(algo) {
case Algorithm.BFS: {
System.out.println("Using BFS");
LinkedList<String> queue = new LinkedList<>();
visited.put(src, true);
queue.add(src);

while (!queue.isEmpty()) {

src = queue.poll();
if (Objects.equals(src, dst)) {
break;
}
List<String> successors = Graphs.neighborListOf(graphObject,src);
for (String node : successors) {

if (visited.get(node) == null) {
visited.put(node, true);
parent.put(node, src);
queue.add(node);
}
}
}
case BFS:
searchAlgorithm = new BFS(src, dst, graphObject);
break;
}
case Algorithm.DFS: {
System.out.println("Using DFS");
Stack<String> stack = new Stack<>();
visited.put(src, true);
stack.push(src);

while (!stack.isEmpty()) {

src = stack.pop();
if (Objects.equals(src, dst)) {
break;
}
List<String> successors = Graphs.neighborListOf(graphObject,src);
for (String node : successors) {

if (visited.get(node) == null) {
visited.put(node, true);
parent.put(node, src);
stack.push(node);
}
}
}
case DFS:
searchAlgorithm = new DFS(src, dst, graphObject);
break;
}
}
String node = dst;
p.add(node);
if (visited.get(dst) != null) {
while (true) {
if (Objects.equals(node, source)) {
return p;
}
node = parent.get(node);
p.add(node);
}
} else {
return null;
default:
throw new IllegalArgumentException("Invalid choice of algorithm");
}
return searchAlgorithm.graphSearch();
}
public void removeEdge(String srcLabel, String dstLabel) throws Exception {
DefaultEdge edgeexisting = graphObject.getEdge(srcLabel, dstLabel);
Expand Down
130 changes: 130 additions & 0 deletions src/main/java/GraphSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import org.jgrapht.Graph;
import org.jgrapht.Graphs;

import java.util.*;

abstract class GraphSearchAlgorithm {
protected HashMap<String, Boolean> visited;
protected HashMap<String, String> parent;
protected Path path;
protected String source;
protected String destination;
protected Graph graphObject;

public GraphSearchAlgorithm(String src, String dst, Graph graph) {
source = src;
destination = dst;
graphObject = graph;
visited = new HashMap<>();
parent = new HashMap<>();
path = new Path();
}

// Template method defining the common steps
public Path graphSearch() {
selectAlgorithm();
executeAlgorithm();
return getPath();
}

// Abstract methods to be implemented by subclasses
protected abstract void selectAlgorithm();
protected abstract void executeAlgorithm();
protected abstract Path getPath();
}

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

public BFS(String src, String dst, Graph graph) {
super(src, dst, graph);
queue = new LinkedList<>();
}

protected void selectAlgorithm() {
System.out.println("Using BFS");
visited.put(source, true);
queue.add(source);
}

protected void executeAlgorithm() {
while (!queue.isEmpty()) {
String src = queue.poll();
if (src.equals(destination)) {
break;
}
List<String> successors = Graphs.neighborListOf(graphObject, src);
for (String node : successors) {
if (visited.get(node) == null) {
visited.put(node, true);
parent.put(node, src);
queue.add(node);
}
}
}
}

protected Path getPath() {
String node = destination;
path.add(node);
if (visited.get(destination) != null) {
while (true) {
if (node.equals(source)) {
return path;
}
node = parent.get(node);
path.add(node);
}
} else {
return null;
}
}
}

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

public DFS(String src, String dst, Graph graph) {
super(src, dst, graph);
stack = new Stack<>();
}

protected void selectAlgorithm() {
System.out.println("Using DFS");
visited.put(source, true);
stack.push(source);
}

protected void executeAlgorithm() {
while (!stack.isEmpty()) {
String src = stack.pop();
if (src.equals(destination)) {
break;
}
List<String> successors = Graphs.neighborListOf(graphObject, src);
for (String node : successors) {
if (visited.get(node) == null) {
visited.put(node, true);
parent.put(node, src);
stack.push(node);
}
}
}
}

protected Path getPath() {
String node = destination;
path.add(node);
if (visited.get(destination) != null) {
while (true) {
if (node.equals(source)) {
return path;
}
node = parent.get(node);
path.add(node);
}
} else {
return null;
}
}
}

0 comments on commit 3c6fa30

Please sign in to comment.