Skip to content

Commit a2da328

Browse files
author
Samuel Facchinello
committed
refactor
1 parent 732f9d9 commit a2da328

File tree

2 files changed

+72
-74
lines changed

2 files changed

+72
-74
lines changed
Lines changed: 67 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,100 @@
1-
/**
2-
* Author : Siddhant Swarup Mallick
3-
* Github : https://github.com/siddhant2002
4-
*/
5-
6-
/** Program description - To find all possible paths from source to destination*/
7-
8-
/**Wikipedia link -> https://en.wikipedia.org/wiki/Shortest_path_problem */
91
package com.thealgorithms.backtracking;
102

113
import java.util.ArrayList;
124
import java.util.List;
135

6+
/**
7+
* To find all possible paths from source to destination. See
8+
* <a href="https://en.wikipedia.org/wiki/Shortest_path_problem">Wikipedia</a>
9+
* (Note: This algorithm finds all paths, not the shortest paths).
10+
*/
1411
public class AllPathsFromSourceToTarget {
1512

16-
// No. of vertices in graph
17-
private final int v;
18-
19-
// To store the paths from source to destination
20-
static List<List<Integer>> nm = new ArrayList<>();
21-
// adjacency list
22-
private ArrayList<Integer>[] adjList;
23-
24-
// Constructor
25-
public AllPathsFromSourceToTarget(int vertices) {
13+
private final int vertexCount;
14+
private final List<List<Integer>> allPaths;
15+
private final List<Integer>[] adjList;
2616

27-
// initialise vertex count
28-
this.v = vertices;
29-
30-
// initialise adjacency list
31-
initAdjList();
17+
private AllPathsFromSourceToTarget(int vertices) {
18+
this.vertexCount = vertices;
19+
this.allPaths = new ArrayList<>();
20+
this.adjList = new ArrayList[vertices];
21+
initializeAdjacencyList();
3222
}
3323

34-
// utility method to initialise adjacency list
35-
private void initAdjList() {
36-
adjList = new ArrayList[v];
37-
38-
for (int i = 0; i < v; i++) {
24+
/**
25+
* Initializes the adjacency list to represent a graph with the specified number of vertices.
26+
*/
27+
private void initializeAdjacencyList() {
28+
for (int i = 0; i < vertexCount; i++) {
3929
adjList[i] = new ArrayList<>();
4030
}
4131
}
4232

43-
// add edge from u to v
44-
public void addEdge(int u, int v) {
45-
// Add v to u's list.
46-
adjList[u].add(v);
33+
/**
34+
* Adds a directed edge from source vertex to destination vertex in the graph.
35+
* @param source The starting vertex of the edge.
36+
* @param destination The ending vertex of the edge.
37+
*/
38+
private void addEdge(int source, int destination) {
39+
adjList[source].add(destination);
4740
}
4841

49-
public void storeAllPaths(int s, int d) {
50-
boolean[] isVisited = new boolean[v];
51-
ArrayList<Integer> pathList = new ArrayList<>();
52-
53-
// add source to path[]
54-
pathList.add(s);
55-
// Call recursive utility
56-
storeAllPathsUtil(s, d, isVisited, pathList);
42+
/**
43+
* Finds all possible paths from a given source vertex to a destination vertex in the graph.
44+
* This method uses a backtracking approach to explore all possible paths.
45+
* @param source The starting vertex of the path search.
46+
* @param destination The ending vertex of the path search.
47+
* @return A list containing all the found paths represented as lists of vertex indices.
48+
*/
49+
private List<List<Integer>> findAllPaths(int source, int destination) {
50+
boolean[] visited = new boolean[vertexCount];
51+
List<Integer> currentPath = new ArrayList<>();
52+
currentPath.add(source);
53+
findAllPathsUtil(source, destination, visited, currentPath);
54+
return allPaths;
5755
}
5856

59-
// A recursive function to print all paths from 'u' to 'd'.
60-
// isVisited[] keeps track of vertices in current path.
61-
// localPathList<> stores actual vertices in the current path
62-
private void storeAllPathsUtil(Integer u, Integer d, boolean[] isVisited, List<Integer> localPathList) {
63-
64-
if (u.equals(d)) {
65-
nm.add(new ArrayList<>(localPathList));
57+
/**
58+
* A recursive helper function that implements the backtracking logic to find all paths.
59+
* @param source The current vertex being explored in the path search.
60+
* @param destination The ending vertex of the path search.
61+
* @param visited A boolean array to keep track of visited vertices.
62+
* @param currentPath A list representing the current path being constructed.
63+
*/
64+
private void findAllPathsUtil(int source, int destination, boolean[] visited, List<Integer> currentPath) {
65+
if (source == destination) {
66+
allPaths.add(new ArrayList<>(currentPath));
6667
return;
6768
}
6869

69-
// Mark the current node
70-
isVisited[u] = true;
70+
visited[source] = true;
7171

72-
// Recursion for all the vertices adjacent to current vertex
73-
74-
for (Integer i : adjList[u]) {
75-
if (!isVisited[i]) {
76-
// store current node in path[]
77-
localPathList.add(i);
78-
storeAllPathsUtil(i, d, isVisited, localPathList);
79-
80-
// remove current node in path[]
81-
localPathList.remove(i);
72+
for (int neighbor : adjList[source]) {
73+
if (!visited[neighbor]) {
74+
currentPath.add(neighbor);
75+
findAllPathsUtil(neighbor, destination, visited, currentPath);
76+
currentPath.removeLast();
8277
}
8378
}
8479

85-
// Mark the current node
86-
isVisited[u] = false;
80+
visited[source] = false;
8781
}
8882

89-
// Driver program
90-
public static List<List<Integer>> allPathsFromSourceToTarget(int vertices, int[][] a, int source, int destination) {
91-
// Create a sample graph
83+
84+
/**
85+
* Finds all possible paths from a given source vertex to a destination vertex in the graph.
86+
* This method uses a backtracking approach to explore all possible paths.
87+
* The found paths are stored in the `allPaths` member variable.
88+
*
89+
* @param source The starting vertex of the path search.
90+
* @param destination The ending vertex of the path search.
91+
* @return A list containing all the found paths represented as lists of vertex indices.
92+
*/
93+
public static List<List<Integer>> findAllPaths(int vertices, int[][] a, int source, int destination) {
9294
AllPathsFromSourceToTarget g = new AllPathsFromSourceToTarget(vertices);
9395
for (int[] i : a) {
9496
g.addEdge(i[0], i[1]);
95-
// edges are added
9697
}
97-
g.storeAllPaths(source, destination);
98-
// method call to store all possible paths
99-
return nm;
100-
// returns all possible paths from source to destination
98+
return g.findAllPaths(source, destination);
10199
}
102100
}

src/test/java/com/thealgorithms/backtracking/AllPathsFromSourceToTargetTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void testForFirstCase() {
1414
int source = 2;
1515
int destination = 3;
1616
List<List<Integer>> expected = List.of(List.of(2, 0, 1, 3), List.of(2, 0, 3), List.of(2, 1, 3));
17-
List<List<Integer>> actual = AllPathsFromSourceToTarget.allPathsFromSourceToTarget(vertices, a, source, destination);
17+
List<List<Integer>> actual = AllPathsFromSourceToTarget.findAllPaths(vertices, a, source, destination);
1818

1919
assertIterableEquals(expected, actual);
2020
}
@@ -26,7 +26,7 @@ void testForSecondCase() {
2626
int source = 0;
2727
int destination = 4;
2828
List<List<Integer>> expected = 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));
29-
List<List<Integer>> actual = AllPathsFromSourceToTarget.allPathsFromSourceToTarget(vertices, a, source, destination);
29+
List<List<Integer>> actual = AllPathsFromSourceToTarget.findAllPaths(vertices, a, source, destination);
3030

3131
assertIterableEquals(expected, actual);
3232
}
@@ -38,19 +38,19 @@ void testForThirdCase() {
3838
int source = 1;
3939
int destination = 5;
4040
List<List<Integer>> expected = List.of(List.of(1, 0, 2, 5), List.of(1, 0, 5), List.of(1, 5), List.of(1, 2, 5));
41-
List<List<Integer>> actual = AllPathsFromSourceToTarget.allPathsFromSourceToTarget(vertices, a, source, destination);
41+
List<List<Integer>> actual = AllPathsFromSourceToTarget.findAllPaths(vertices, a, source, destination);
4242

4343
assertIterableEquals(expected, actual);
4444
}
4545

4646
@Test
47-
void testForFourthcase() {
47+
void testForFourthCase() {
4848
int vertices = 3;
4949
int[][] a = {{0, 1}, {0, 2}, {1, 2}};
5050
int source = 0;
5151
int destination = 2;
5252
List<List<Integer>> expected = List.of(List.of(0, 1, 2), List.of(0, 2));
53-
List<List<Integer>> actual = AllPathsFromSourceToTarget.allPathsFromSourceToTarget(vertices, a, source, destination);
53+
List<List<Integer>> actual = AllPathsFromSourceToTarget.findAllPaths(vertices, a, source, destination);
5454

5555
assertIterableEquals(expected, actual);
5656
}

0 commit comments

Comments
 (0)