Skip to content

Commit 9aa4868

Browse files
committed
Fixes (#3359)
1 parent e237ad3 commit 9aa4868

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
8+
package main.java.com.thealgorithms.backtracking;
9+
10+
// JAVA program to print all paths from a source to destination.
11+
import java.util.*;
12+
13+
// A directed graph using adjacency list representation
14+
15+
16+
public class All_Paths_From_Source_To_Target {
17+
18+
// No. of vertices in graph
19+
private int v;
20+
21+
// adjacency list
22+
private ArrayList<Integer>[] adjList;
23+
24+
// Constructor
25+
public All_Paths_From_Source_To_Target(int vertices)
26+
{
27+
28+
// initialise vertex count
29+
this.v = vertices;
30+
31+
// initialise adjacency list
32+
initAdjList();
33+
}
34+
35+
// utility method to initialise adjacency list
36+
37+
@SuppressWarnings("unchecked")
38+
private void initAdjList()
39+
{
40+
adjList = new ArrayList[v];
41+
42+
for (int i = 0; i < v; i++) {
43+
adjList[i] = new ArrayList<>();
44+
}
45+
}
46+
47+
// add edge from u to v
48+
public void addEdge(int u, int v)
49+
{
50+
// Add v to u's list.
51+
adjList[u].add(v);
52+
}
53+
54+
// Prints all paths from 's' to 'd'
55+
56+
public void printAllPaths(int s, int d, int c)
57+
{
58+
boolean[] isVisited = new boolean[v];
59+
ArrayList<Integer> pathList = new ArrayList<>();
60+
61+
// add source to path[]
62+
pathList.add(s);
63+
64+
// Call recursive utility
65+
printAllPathsUtil(s, d, isVisited, pathList);
66+
return a[0];
67+
}
68+
69+
// A recursive function to print all paths from 'u' to 'd'.
70+
// isVisited[] keeps track of vertices in current path.
71+
// localPathList<> stores actual vertices in the current path
72+
private int printAllPathsUtil(Integer u, Integer d, boolean[] isVisited, List<Integer> localPathList, int a[])
73+
{
74+
75+
if (u.equals(d)) {
76+
System.out.println(localPathList);
77+
a[0]++;
78+
// if match found then no need to traverse more till depth
79+
return a[0];
80+
}
81+
82+
// Mark the current node
83+
isVisited[u] = true;
84+
85+
// Recursion for all the vertices adjacent to current vertex
86+
87+
for (Integer i : adjList[u]) {
88+
if (!isVisited[i]) {
89+
// store current node in path[]
90+
localPathList.add(i);
91+
printAllPathsUtil(i, d, isVisited, localPathList);
92+
93+
// remove current node in path[]
94+
localPathList.remove(i);
95+
}
96+
}
97+
98+
// Mark the current node
99+
isVisited[u] = false;
100+
return a[0];
101+
// returns number of paths from source to destination
102+
}
103+
104+
// Driver program
105+
public static boolean all_Paths_From_Source_To_Target(int vertices, int a[][], int source, int destination, int num_of_paths)
106+
{
107+
// Create a sample graph
108+
All_Paths_From_Source_To_Target g = new All_Paths_From_Source_To_Target(vertices);
109+
for(int i=0 ; i<a.length ; i++)
110+
{
111+
g.addEdge(a[i][0], a[i][1]);
112+
// edges are added
113+
}
114+
System.out.println("Following are all different paths from "+ source + " to " + destination);
115+
int c = g.printAllPaths(source, destination);
116+
// method call to find number of paths
117+
return c == num_of_paths;
118+
// returns true if number of paths calculated from source to destination matches with given paths
119+
}
120+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.thealgorithms.backtracking;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import com.thealgorithms.backtracking.All_Paths_From_Source_To_Target;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class All_Paths_From_Source_To_Target_Test {
9+
10+
@Test
11+
void testForFirstCase() {
12+
int vertices = 4;
13+
int a[][] = {{0,1},{0,2},{0,3},{2,0},{2,1},{1,3}};
14+
int source = 2;
15+
int destination = 3;
16+
int num_of_paths = 3;
17+
assertTrue(All_Paths_From_Source_To_Target.all_Paths_From_Source_To_Target (vertices,a,source,destination,num_of_paths));
18+
}
19+
20+
@Test
21+
void testForSecondCase() {
22+
int vertices = 5;
23+
int a[][] = {{0,1},{0,2},{0,3},{2,0},{2,1},{1,3},{1,4},{3,4},{2,4}};
24+
int source = 0;
25+
int destination = 4;
26+
int num_of_paths = 6;
27+
assertTrue(All_Paths_From_Source_To_Target.all_Paths_From_Source_To_Target (vertices,a,source,destination,num_of_paths));
28+
}
29+
30+
@Test
31+
void testForThirdCase() {
32+
int vertices = 6;
33+
int a[][] = {{1,0},{2,3},{0,4},{1,5},{4,3},{0,2},{0,3},{1,2},{0,5}};
34+
int source = 1;
35+
int destination = 5;
36+
int num_of_paths = 2;
37+
assertTrue(All_Paths_From_Source_To_Target.all_Paths_From_Source_To_Target (vertices,a,source,destination,num_of_paths));
38+
}
39+
40+
@Test
41+
void testForFourthcase() {
42+
int vertices = 3;
43+
int a[][] = {{0,1},{0,2},{1,2}};
44+
int source = 0;
45+
int destination = 2;
46+
int num_of_paths = 2;
47+
assertTrue(All_Paths_From_Source_To_Target.all_Paths_From_Source_To_Target (vertices,a,source,destination,num_of_paths));
48+
}
49+
}

0 commit comments

Comments
 (0)