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 000000000000..d9e6d191b236 --- /dev/null +++ b/src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java @@ -0,0 +1,125 @@ +package main.java.com.thealgorithms.backtracking; + +// JAVA program to print all +// paths from a source to +// destination. +import java.util.ArrayList; +import java.util.List; + +// A directed graph using +// adjacency list representation +public class AllPathsFromSourceToTarget { + + // No. of vertices in graph + private int v; + + // adjacency list + private ArrayList[] adjList; + + // Constructor + public AllPathsFromSourceToTarget(int vertices) + { + + // initialise vertex count + this.v = vertices; + + // initialise adjacency list + initAdjList(); + } + + // utility method to initialise + // adjacency list + @SuppressWarnings("unchecked") + 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); + } + + // Prints all paths from + // 's' to 'd' + public void printAllPaths(int s, int d) + { + boolean[] isVisited = new boolean[v]; + ArrayList pathList = new ArrayList<>(); + + // add source to path[] + pathList.add(s); + + // Call recursive utility + printAllPathsUtil(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 printAllPathsUtil(Integer u, Integer d, + boolean[] isVisited, + List localPathList) + { + + if (u.equals(d)) { + System.out.println(localPathList); + // if match found then no need to traverse more till depth + return; + } + + // Mark the current node + isVisited[u] = true; + + // Recur for all the vertices + // adjacent to current vertex + for (Integer i : adjList[u]) { + if (!isVisited[i]) { + // store current node + // in path[] + localPathList.add(i); + printAllPathsUtil(i, d, isVisited, localPathList); + + // remove current node + // in path[] + localPathList.remove(i); + } + } + + // Mark the current node + isVisited[u] = false; + } + + // Driver program + public static void main(String[] args) + { + // Create a sample graph + AllPathsFromSourceToTarget g = new AllPathsFromSourceToTarget(4); + g.addEdge(0, 1); + g.addEdge(0, 2); + g.addEdge(0, 3); + g.addEdge(2, 0); + g.addEdge(2, 1); + g.addEdge(1, 3); + + // arbitrary source + int s = 2; + + // arbitrary destination + int d = 3; + + System.out.println( + "Following are all different paths from " + + s + " to " + d); + g.printAllPaths(s, d); + } +} \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java b/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java index 1cfe8b63af62..5990c4604f2e 100644 --- a/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java +++ b/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java @@ -16,17 +16,28 @@ * be changed according to the user preference. */ + public class SquareRootWithNewtonRaphsonMethod { - public static double squareRoot(int n) { - double x = n; //initially taking a guess that x = n. - double root = 0.5 * (x + n / x); //applying Newton-Raphson Method. + public static void main(String[] args) { + Scanner sc =new Scanner(System.in); + System.out.println("enter the number"); + int a = sc.nextInt(); + System.out.println(squareRoot(a)); + sc.close(); + } + + public static double squareRoot (int n) { + + double x = n; //initially taking a guess that x = n. + double root = 0.5 * (x + n/x); //applying Newton-Raphson Method. + + while (Math.abs(root - x) > 0.0000001) { //root - x = error and error < 0.0000001, 0.0000001 is the precision value taken over here. - while (Math.abs(root - x) > 0.0000001) { //root - x = error and error < 0.0000001, 0.0000001 is the precision value taken over here. - x = root; //decreasing the value of x to root, i.e. decreasing the guess. - root = 0.5 * (x + n / x); + x = root; //decreasing the value of x to root, i.e. decreasing the guess. + root = 0.5 * (x + n/x); } return root; } -} +} \ No newline at end of file