mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-22 11:46:34 +08:00
@ -1,4 +1,5 @@
|
||||
/** Author : Siddhant Swarup Mallick
|
||||
/**
|
||||
* Author : Siddhant Swarup Mallick
|
||||
* Github : https://github.com/siddhant2002
|
||||
*/
|
||||
|
||||
@ -15,13 +16,12 @@ public class AllPathsFromSourceToTarget {
|
||||
private int v;
|
||||
|
||||
// To store the paths from source to destination
|
||||
static List<List<Integer>> nm=new ArrayList<>();
|
||||
static List<List<Integer>> nm = new ArrayList<>();
|
||||
// adjacency list
|
||||
private ArrayList<Integer>[] adjList;
|
||||
|
||||
// Constructor
|
||||
public AllPathsFromSourceToTarget(int vertices)
|
||||
{
|
||||
public AllPathsFromSourceToTarget(int vertices) {
|
||||
|
||||
// initialise vertex count
|
||||
this.v = vertices;
|
||||
@ -31,8 +31,7 @@ public class AllPathsFromSourceToTarget {
|
||||
}
|
||||
|
||||
// utility method to initialise adjacency list
|
||||
private void initAdjList()
|
||||
{
|
||||
private void initAdjList() {
|
||||
adjList = new ArrayList[v];
|
||||
|
||||
for (int i = 0; i < v; i++) {
|
||||
@ -41,15 +40,12 @@ public class AllPathsFromSourceToTarget {
|
||||
}
|
||||
|
||||
// add edge from u to v
|
||||
public void addEdge(int u, int 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)
|
||||
{
|
||||
public void storeAllPaths(int s, int d) {
|
||||
boolean[] isVisited = new boolean[v];
|
||||
ArrayList<Integer> pathList = new ArrayList<>();
|
||||
|
||||
@ -61,9 +57,9 @@ public class AllPathsFromSourceToTarget {
|
||||
|
||||
// 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)
|
||||
{
|
||||
// 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));
|
||||
@ -74,7 +70,7 @@ public class AllPathsFromSourceToTarget {
|
||||
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[]
|
||||
@ -91,12 +87,11 @@ public class AllPathsFromSourceToTarget {
|
||||
}
|
||||
|
||||
// Driver program
|
||||
public static List<List<Integer>> allPathsFromSourceToTarget(int vertices, int[][] a, int source, int destination)
|
||||
{
|
||||
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++)
|
||||
{
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
g.addEdge(a[i][0], a[i][1]);
|
||||
// edges are added
|
||||
}
|
||||
|
Reference in New Issue
Block a user