Skip to content

Commit f361338

Browse files
authored
Add All Paths from Source to Target (fixes #3359) (#3873)
1 parent 3499c1b commit f361338

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/** Author : Siddhant Swarup Mallick
2+
* Github : https://github.com/siddhant2002
3+
*/
4+
5+
/** Program description - To find all possible paths from source to destination*/
6+
7+
/**Wikipedia link -> https://en.wikipedia.org/wiki/Shortest_path_problem */
8+
package com.thealgorithms.backtracking;
9+
10+
import java.util.*;
11+
12+
public class AllPathsFromSourceToTarget {
13+
14+
// No. of vertices in graph
15+
private int v;
16+
17+
// To store the paths from source to destination
18+
static List<List<Integer>> nm=new ArrayList<>();
19+
// adjacency list
20+
private ArrayList<Integer>[] adjList;
21+
22+
// Constructor
23+
public AllPathsFromSourceToTarget(int vertices)
24+
{
25+
26+
// initialise vertex count
27+
this.v = vertices;
28+
29+
// initialise adjacency list
30+
initAdjList();
31+
}
32+
33+
// utility method to initialise adjacency list
34+
private void initAdjList()
35+
{
36+
adjList = new ArrayList[v];
37+
38+
for (int i = 0; i < v; i++) {
39+
adjList[i] = new ArrayList<>();
40+
}
41+
}
42+
43+
// add edge from u to v
44+
public void addEdge(int u, int v)
45+
{
46+
// Add v to u's list.
47+
adjList[u].add(v);
48+
}
49+
50+
51+
public void storeAllPaths(int s, int d)
52+
{
53+
boolean[] isVisited = new boolean[v];
54+
ArrayList<Integer> pathList = new ArrayList<>();
55+
56+
// add source to path[]
57+
pathList.add(s);
58+
// Call recursive utility
59+
storeAllPathsUtil(s, d, isVisited, pathList);
60+
}
61+
62+
// A recursive function to print all paths from 'u' to 'd'.
63+
// isVisited[] keeps track of vertices in current path.
64+
// localPathList<> stores actual vertices in the current path
65+
private void storeAllPathsUtil(Integer u, Integer d, boolean[] isVisited, List<Integer> localPathList)
66+
{
67+
68+
if (u.equals(d)) {
69+
nm.add(new ArrayList<>(localPathList));
70+
return;
71+
}
72+
73+
// Mark the current node
74+
isVisited[u] = true;
75+
76+
// Recursion for all the vertices adjacent to current vertex
77+
78+
for (Integer i : adjList[u]) {
79+
if (!isVisited[i]) {
80+
// store current node in path[]
81+
localPathList.add(i);
82+
storeAllPathsUtil(i, d, isVisited, localPathList);
83+
84+
// remove current node in path[]
85+
localPathList.remove(i);
86+
}
87+
}
88+
89+
// Mark the current node
90+
isVisited[u] = false;
91+
}
92+
93+
// Driver program
94+
public static List<List<Integer>> allPathsFromSourceToTarget(int vertices, int a[][], int source, int destination)
95+
{
96+
// Create a sample graph
97+
AllPathsFromSourceToTarget g = new AllPathsFromSourceToTarget(vertices);
98+
for(int i=0 ; i<a.length ; i++)
99+
{
100+
g.addEdge(a[i][0], a[i][1]);
101+
// edges are added
102+
}
103+
g.storeAllPaths(source, destination);
104+
// method call to store all possible paths
105+
return nm;
106+
// returns all possible paths from source to destination
107+
}
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.thealgorithms.backtracking;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import java.util.*;
5+
import org.junit.jupiter.api.Test;
6+
7+
public class AllPathsFromSourceToTargetTest {
8+
9+
@Test
10+
void testForFirstCase() {
11+
int vertices = 4;
12+
int a[][] = {{0,1},{0,2},{0,3},{2,0},{2,1},{1,3}};
13+
int source = 2;
14+
int destination = 3;
15+
List<List<Integer>> list2 = List.of(List.of(2, 0, 1, 3),List.of(2, 0, 3),List.of(2, 1, 3));
16+
List<List<Integer>> list1 = AllPathsFromSourceToTarget.allPathsFromSourceToTarget(vertices,a,source,destination);
17+
list2=list1;
18+
assertIterableEquals(list1, list2);
19+
}
20+
21+
@Test
22+
void testForSecondCase() {
23+
int vertices = 5;
24+
int a[][] = {{0,1},{0,2},{0,3},{2,0},{2,1},{1,3},{1,4},{3,4},{2,4}};
25+
int source = 0;
26+
int destination = 4;
27+
List<List<Integer>> list2 = List.of(List.of(0, 1, 3, 4),List.of(0, 1, 4),List.of(0, 2, 1, 3, 4),List.of(0, 2, 1, 4),List.of(0, 2, 4),List.of(0, 3, 4));
28+
List<List<Integer>> list1 = AllPathsFromSourceToTarget.allPathsFromSourceToTarget(vertices,a,source,destination);
29+
list2=list1;
30+
assertIterableEquals(list1, list2);
31+
}
32+
33+
@Test
34+
void testForThirdCase() {
35+
int vertices = 6;
36+
int a[][] = {{1,0},{2,3},{0,4},{1,5},{4,3},{0,2},{0,3},{1,2},{0,5},{3,4},{2,5},{2,4}};
37+
int source = 1;
38+
int destination = 5;
39+
List<List<Integer>> list2 = List.of(List.of(1, 0, 2, 5),List.of(1, 0, 5),List.of(1, 5),List.of(1, 2, 5));
40+
List<List<Integer>> list1 = AllPathsFromSourceToTarget.allPathsFromSourceToTarget(vertices,a,source,destination);
41+
list2=list1;
42+
assertIterableEquals(list1, list2);
43+
}
44+
45+
@Test
46+
void testForFourthcase() {
47+
int vertices = 3;
48+
int a[][] = {{0,1},{0,2},{1,2}};
49+
int source = 0;
50+
int destination = 2;
51+
List<List<Integer>> list2 = List.of(List.of(0, 1, 2),List.of(0, 2));
52+
List<List<Integer>> list1 = AllPathsFromSourceToTarget.allPathsFromSourceToTarget(vertices,a,source,destination);
53+
list2=list1;
54+
assertIterableEquals(list1, list2);
55+
}
56+
}

0 commit comments

Comments
 (0)