Skip to content

Commit 56817b6

Browse files
committed
Added solution to find the path in graph
1 parent 9db4e19 commit 56817b6

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

src/main/java/com/eprogrammerz/examples/algorithm/graphs/TestUndirectedGraph.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import org.junit.Test;
44

55
import java.util.Arrays;
6+
import java.util.List;
67

78
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertTrue;
810

911
public class TestUndirectedGraph {
1012
@Test
@@ -15,7 +17,7 @@ public void testBFS() {
1517
* 6
1618
* / \
1719
* 4 5
18-
* / \ \
20+
* / \ / \
1921
* 1 2 3
2022
*
2123
*/
@@ -26,10 +28,39 @@ public void testBFS() {
2628
Node d = new Node(4);
2729
d.connections = Arrays.asList(a, b);
2830
Node e = new Node(5);
29-
e.connections = Arrays.asList(c);
31+
e.connections = Arrays.asList(b, c);
3032
Node f = new Node(6);
3133
f.connections = Arrays.asList(d,e);
3234

3335
assertEquals(2, graph.findNode(f, 2).id);
3436
}
37+
38+
@Test
39+
public void testPath() {
40+
/**
41+
*
42+
* /**
43+
* 6
44+
* / \
45+
* 4 5
46+
* / \ / \
47+
* 1 2 3
48+
*
49+
*/
50+
UndirectedGraph graph = new UndirectedGraph();
51+
Node a = new Node(1);
52+
Node b = new Node(2);
53+
Node c = new Node(3);
54+
Node d = new Node(4);
55+
d.connections = Arrays.asList(a, b);
56+
Node e = new Node(5);
57+
e.connections = Arrays.asList(b, c);
58+
Node f = new Node(6);
59+
f.connections = Arrays.asList(d, e);
60+
61+
List<Integer> actual = graph.findPath(f, 2);
62+
63+
List<List<Integer>> expected = Arrays.asList(Arrays.asList(6, 4, 2), Arrays.asList(6, 5, 2));
64+
assertTrue(expected.contains(actual));
65+
}
3566
}

src/main/java/com/eprogrammerz/examples/algorithm/graphs/UndirectedGraph.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ public Node findNode(Node start, int targetId) {
1919
throw new IllegalArgumentException("Graph should have at least one Node.");
2020
}
2121

22-
if (start.id == targetId) {
23-
return start;
24-
}
25-
2622
Queue<Node> neighbors = new LinkedList<>();
2723
neighbors.add(start);
2824

@@ -43,4 +39,36 @@ public Node findNode(Node start, int targetId) {
4339

4440
return null;
4541
}
42+
43+
public List<Integer> findPath(Node start, int targetId) {
44+
List<List<Integer>> paths = new ArrayList<>();
45+
findPath(start, targetId, paths, new ArrayList<>());
46+
47+
List<Integer> shortest = paths.stream().min(Comparator.comparingInt(List::size)).orElse(null);
48+
return shortest;
49+
}
50+
51+
private void findPath(Node node, int end, List<List<Integer>> paths, List<Integer> temp) {
52+
if (node == null) return;
53+
54+
temp.add(node.id);
55+
node.visited = true;
56+
57+
if (node.id == end) {
58+
paths.add(new ArrayList<>(temp));
59+
return;
60+
}
61+
62+
List<Node> neighbors = node.connections;
63+
64+
if (neighbors != null) {
65+
for (Node neighbor: neighbors) {
66+
if (!neighbor.visited) {
67+
findPath(neighbor, end, paths, temp);
68+
temp.remove(temp.size() - 1);
69+
}
70+
}
71+
}
72+
73+
}
4674
}

0 commit comments

Comments
 (0)