Skip to content

Commit

Permalink
Merge commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
theViz343 committed Nov 3, 2023
2 parents 2200302 + 3b37cee commit aac797b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 18 deletions.
63 changes: 49 additions & 14 deletions src/main/java/GraphData.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
public class GraphData {
Graph<String, DefaultEdge> graphObject = new DefaultDirectedGraph<>(DefaultEdge.class);

enum Algorithm{
BFS,
DFS
}

public boolean parseGraph(String filepath) {

// Import the graph from file
Expand Down Expand Up @@ -125,30 +130,60 @@ public boolean addEdge(String srcLabel, String dstLabel) {
}
}

public Path GraphSearch(String src, String dst) {
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<>();

LinkedList<String> queue = new LinkedList<>();
visited.put(src, true);
queue.add(src);
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()) {
while (!queue.isEmpty()) {

src = queue.poll();
if (Objects.equals(src, dst)) {
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);
}
}
}
break;
}
List<String> successors = Graphs.neighborListOf(graphObject,src);
for (String node : successors) {
case Algorithm.DFS: {
System.out.println("Using DFS");
Stack<String> stack = new Stack<>();
visited.put(src, true);
stack.push(src);

if (visited.get(node)==null) {
visited.put(node, true);
parent.put(node,src);
queue.add(node);
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);
}
}
}
break;
}
}
String node = dst;
Expand Down Expand Up @@ -210,7 +245,7 @@ public static void main(String[] args) {
graphApi.addNode("E");
graphApi.addEdge("D","E");
System.out.println(graphApi.toString());
Path path = graphApi.GraphSearch("C", "D");
Path path = graphApi.GraphSearch("C","D", Algorithm.BFS);
path.printPath();
// graphApi.outputGraph("src/main/resources/output.txt");
// graphApi.addNodes(new String[]{"Z", "X"});
Expand Down
23 changes: 19 additions & 4 deletions src/test/java/GraphDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,31 @@ public void TestRemoveEdgeIfExists(){

@Test
@DisplayName("Test bfs graph search api")
public void TestGraphSearch() {
Path path = graphApi.GraphSearch("C", "D");
public void TestGraphSearchBFS() {
Path path = graphApi.GraphSearch("C", "D", GraphData.Algorithm.BFS);
List<String> expected = List.of(new String[]{"D", "A", "C"});
assertEquals(path.path, expected);
}

@Test
@DisplayName("Test bfs graph search api (node does not exist)")
public void TestGraphSearchNotExist() {
Path path = graphApi.GraphSearch("C", "X");
public void TestGraphSearchNotExistBFS() {
Path path = graphApi.GraphSearch("C", "X", GraphData.Algorithm.BFS);
assertNull(path);
}

@Test
@DisplayName("Test dfs graph search api")
public void TestGraphSearchDFS() {
Path path = graphApi.GraphSearch("C", "D", GraphData.Algorithm.DFS);
List<String> expected = List.of(new String[]{"D", "A", "C"});
assertEquals(path.path, expected);
}

@Test
@DisplayName("Test dfs graph search api (node does not exist)")
public void TestGraphSearchNotExistDFS() {
Path path = graphApi.GraphSearch("C", "X", GraphData.Algorithm.DFS);
assertNull(path);
}

Expand Down

0 comments on commit aac797b

Please sign in to comment.