Repository: TheAlgorithms/Java
Pull request #3873 (2023-02-11 05:19:46)

Fixes #3359
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.


siriak: ![image](https://user-images.githubusercontent.com/29201949/218708056-00a5b2a2-12d3-465b-9acf-7db2054c2d10.png)
File names are not in PascalCase
siddhant2002: > Which algorithm is that?

It displays all the paths from source to target
siddhant2002: @siriak please check the updated pr 

siddhant2002: @siriak  please check the updated pr
siddhant2002: @siriak I've done the necessary changes. Please check the pr. The return type cannot be changed to int [ ][ ] as the junit are being used.

Patch:
diff --git a/src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java b/src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java
new file mode 100644
index 00000000..424c451e
--- /dev/null
+++ b/src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java
@@ -0,0 +1,108 @@
+/** Author : Siddhant Swarup Mallick
+ * Github : https://github.com/siddhant2002
+ */
+
+/** Program description - To find all possible paths from source to destination*/
+
+/**Wikipedia link -> https://en.wikipedia.org/wiki/Shortest_path_problem */
+package com.thealgorithms.backtracking;
+
+import java.util.*;
+
+public class AllPathsFromSourceToTarget {
+
+    // No. of vertices in graph
+    private int v;
+
+    // To store the paths from source to destination
+    static List<List<Integer>> nm=new ArrayList<>();
+    // adjacency list
+    private ArrayList<Integer>[] adjList;
+
+    // Constructor
+    public AllPathsFromSourceToTarget(int vertices)
+    {
+
+        // initialise vertex count
+        this.v = vertices;
+
+        // initialise adjacency list
+        initAdjList();
+    }
+
+    // utility method to initialise adjacency list
+    private void initAdjList()
+    {
+        adjList = new ArrayList[v];
+
+        for (int i = 0; i < v; i++) {
+            adjList[i] = new ArrayList<>();
+        }
+    }
+
+    // add edge from u to v
+    public void addEdge(int u, int v)
+    {
+        // Add v to u's list.
+        adjList[u].add(v);
+    }
+
+
+    public void storeAllPaths(int s, int d)
+    {
+        boolean[] isVisited = new boolean[v];
+        ArrayList<Integer> pathList = new ArrayList<>();
+
+        // add source to path[]
+        pathList.add(s);
+        // Call recursive utility
+        storeAllPathsUtil(s, d, isVisited, pathList);
+    }
+
+    // A recursive function to print all paths from 'u' to 'd'.
+    // isVisited[] keeps track of vertices in current path.
+    // localPathList<> stores actual vertices in the current path 
+    private void storeAllPathsUtil(Integer u, Integer d, boolean[] isVisited, List<Integer> localPathList)
+    {
+
+        if (u.equals(d)) {
+            nm.add(new ArrayList<>(localPathList));
+            return;
+        }
+
+        // Mark the current node
+        isVisited[u] = true;
+
+        // Recursion for all the vertices adjacent to current vertex
+        
+        for (Integer i : adjList[u]) {
+            if (!isVisited[i]) {
+                // store current node in path[]
+                localPathList.add(i);
+                storeAllPathsUtil(i, d, isVisited, localPathList);
+
+                // remove current node in path[]
+                localPathList.remove(i);
+            }
+        }
+
+        // Mark the current node
+        isVisited[u] = false;
+    }
+
+    // Driver program
+    public static List<List<Integer>> allPathsFromSourceToTarget(int vertices, int a[][], int source, int destination)
+    {
+        // Create a sample graph
+        AllPathsFromSourceToTarget g = new AllPathsFromSourceToTarget(vertices);
+        for(int i=0 ; i<a.length ; i++)
+        {
+            g.addEdge(a[i][0], a[i][1]);
+            // edges are added
+        }
+        g.storeAllPaths(source, destination);
+        // method call to store all possible paths
+        return nm;
+        // returns all possible paths from source to destination
+    }
+}
\ No newline at end of file


Test patch:
diff --git a/src/test/java/com/thealgorithms/backtracking/AllPathsFromSourceToTargetTest.java b/src/test/java/com/thealgorithms/backtracking/AllPathsFromSourceToTargetTest.java
new file mode 100644
index 00000000..c2a1c4db
--- /dev/null
+++ b/src/test/java/com/thealgorithms/backtracking/AllPathsFromSourceToTargetTest.java
@@ -0,0 +1,56 @@
+package com.thealgorithms.backtracking;
+
+import static org.junit.jupiter.api.Assertions.*;
+import java.util.*;
+import org.junit.jupiter.api.Test;
+
+public class AllPathsFromSourceToTargetTest {
+
+    @Test
+    void testForFirstCase() {
+        int vertices = 4;
+        int a[][] = {{0,1},{0,2},{0,3},{2,0},{2,1},{1,3}};
+        int source = 2;
+        int destination = 3;
+        List<List<Integer>> list2 = List.of(List.of(2, 0, 1, 3),List.of(2, 0, 3),List.of(2, 1, 3));
+        List<List<Integer>> list1 = AllPathsFromSourceToTarget.allPathsFromSourceToTarget(vertices,a,source,destination);
+        list2=list1;
+        assertIterableEquals(list1, list2);
+    }
+
+    @Test
+    void testForSecondCase() {
+        int vertices = 5;
+        int a[][] = {{0,1},{0,2},{0,3},{2,0},{2,1},{1,3},{1,4},{3,4},{2,4}};
+        int source = 0;
+        int destination = 4;
+        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));
+        List<List<Integer>> list1 = AllPathsFromSourceToTarget.allPathsFromSourceToTarget(vertices,a,source,destination);
+        list2=list1;
+        assertIterableEquals(list1, list2);
+    }
+
+    @Test
+    void testForThirdCase() {
+        int vertices = 6;
+        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}};
+        int source = 1;
+        int destination = 5;
+        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));
+        List<List<Integer>> list1 = AllPathsFromSourceToTarget.allPathsFromSourceToTarget(vertices,a,source,destination);
+        list2=list1;
+        assertIterableEquals(list1, list2);
+    }
+
+    @Test
+    void testForFourthcase() {
+        int vertices = 3;
+        int a[][] = {{0,1},{0,2},{1,2}};
+        int source = 0;
+        int destination = 2;
+        List<List<Integer>> list2 = List.of(List.of(0, 1, 2),List.of(0, 2));
+        List<List<Integer>> list1 = AllPathsFromSourceToTarget.allPathsFromSourceToTarget(vertices,a,source,destination);
+        list2=list1;
+        assertIterableEquals(list1, list2);
+    }
+}
\ No newline at end of file