diff --git a/topics/dp/topic.iml b/topics/dp/topic.iml deleted file mode 100644 index b107a2d..0000000 --- a/topics/dp/topic.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/topics/dp/dp/knapsack/Knapsack.java b/topics/topics/dp/knapsack/Knapsack.java similarity index 100% rename from topics/dp/dp/knapsack/Knapsack.java rename to topics/topics/dp/knapsack/Knapsack.java diff --git a/topics/dp/dp/knapsack/SubsetPartition.java b/topics/topics/dp/knapsack/SubsetPartition.java similarity index 100% rename from topics/dp/dp/knapsack/SubsetPartition.java rename to topics/topics/dp/knapsack/SubsetPartition.java diff --git a/topics/dp/dp/knapsack/SubsetSum.java b/topics/topics/dp/knapsack/SubsetSum.java similarity index 100% rename from topics/dp/dp/knapsack/SubsetSum.java rename to topics/topics/dp/knapsack/SubsetSum.java diff --git a/topics/dp/dp/knapsack/idea.png b/topics/topics/dp/knapsack/idea.png similarity index 100% rename from topics/dp/dp/knapsack/idea.png rename to topics/topics/dp/knapsack/idea.png diff --git a/topics/dp/dp/lcs/LongestCommonSubsequence.java b/topics/topics/dp/lcs/LongestCommonSubsequence.java similarity index 100% rename from topics/dp/dp/lcs/LongestCommonSubsequence.java rename to topics/topics/dp/lcs/LongestCommonSubsequence.java diff --git a/topics/dp/dp/lcs/LongestCommonSubstring.java b/topics/topics/dp/lcs/LongestCommonSubstring.java similarity index 100% rename from topics/dp/dp/lcs/LongestCommonSubstring.java rename to topics/topics/dp/lcs/LongestCommonSubstring.java diff --git a/topics/dp/dp/lcs/PrintLCS.java b/topics/topics/dp/lcs/PrintLCS.java similarity index 100% rename from topics/dp/dp/lcs/PrintLCS.java rename to topics/topics/dp/lcs/PrintLCS.java diff --git a/topics/dp/dp/lcs/ShortestCommonSuperSequence.java b/topics/topics/dp/lcs/ShortestCommonSuperSequence.java similarity index 100% rename from topics/dp/dp/lcs/ShortestCommonSuperSequence.java rename to topics/topics/dp/lcs/ShortestCommonSuperSequence.java diff --git a/topics/dp/dp/problems/LongestPallindromicSubsequence.java b/topics/topics/dp/problems/LongestPallindromicSubsequence.java similarity index 100% rename from topics/dp/dp/problems/LongestPallindromicSubsequence.java rename to topics/topics/dp/problems/LongestPallindromicSubsequence.java diff --git a/topics/dp/dp/problems/LongestRepeatedSequence.java b/topics/topics/dp/problems/LongestRepeatedSequence.java similarity index 100% rename from topics/dp/dp/problems/LongestRepeatedSequence.java rename to topics/topics/dp/problems/LongestRepeatedSequence.java diff --git a/topics/dp/dp/problems/MCMMemo.java b/topics/topics/dp/problems/MCMMemo.java similarity index 100% rename from topics/dp/dp/problems/MCMMemo.java rename to topics/topics/dp/problems/MCMMemo.java diff --git a/topics/dp/dp/problems/MCMRecursive.java b/topics/topics/dp/problems/MCMRecursive.java similarity index 100% rename from topics/dp/dp/problems/MCMRecursive.java rename to topics/topics/dp/problems/MCMRecursive.java diff --git a/topics/dp/dp/problems/MinOperationsAddAndDelete.java b/topics/topics/dp/problems/MinOperationsAddAndDelete.java similarity index 100% rename from topics/dp/dp/problems/MinOperationsAddAndDelete.java rename to topics/topics/dp/problems/MinOperationsAddAndDelete.java diff --git a/topics/dp/dp/problems/NumberOfDeletionsToMakeItPallindrome.java b/topics/topics/dp/problems/NumberOfDeletionsToMakeItPallindrome.java similarity index 100% rename from topics/dp/dp/problems/NumberOfDeletionsToMakeItPallindrome.java rename to topics/topics/dp/problems/NumberOfDeletionsToMakeItPallindrome.java diff --git a/topics/dp/dp/problems/PallindromePartitioning.java b/topics/topics/dp/problems/PallindromePartitioning.java similarity index 100% rename from topics/dp/dp/problems/PallindromePartitioning.java rename to topics/topics/dp/problems/PallindromePartitioning.java diff --git a/topics/dp/dp/problems/PartitionSumEqualSubsetBottomUp.java b/topics/topics/dp/problems/PartitionSumEqualSubsetBottomUp.java similarity index 100% rename from topics/dp/dp/problems/PartitionSumEqualSubsetBottomUp.java rename to topics/topics/dp/problems/PartitionSumEqualSubsetBottomUp.java diff --git a/topics/dp/dp/problems/PartitionSumEqualSubsetRecursion.java b/topics/topics/dp/problems/PartitionSumEqualSubsetRecursion.java similarity index 100% rename from topics/dp/dp/problems/PartitionSumEqualSubsetRecursion.java rename to topics/topics/dp/problems/PartitionSumEqualSubsetRecursion.java diff --git a/topics/dp/dp/problems/ShortestCommonSSPrinted.java b/topics/topics/dp/problems/ShortestCommonSSPrinted.java similarity index 100% rename from topics/dp/dp/problems/ShortestCommonSSPrinted.java rename to topics/topics/dp/problems/ShortestCommonSSPrinted.java diff --git a/topics/dp/dp/unboundedknapsack/CoinChange.java b/topics/topics/dp/unboundedknapsack/CoinChange.java similarity index 100% rename from topics/dp/dp/unboundedknapsack/CoinChange.java rename to topics/topics/dp/unboundedknapsack/CoinChange.java diff --git a/topics/dp/dp/unboundedknapsack/MinNumberOfCoins.java b/topics/topics/dp/unboundedknapsack/MinNumberOfCoins.java similarity index 100% rename from topics/dp/dp/unboundedknapsack/MinNumberOfCoins.java rename to topics/topics/dp/unboundedknapsack/MinNumberOfCoins.java diff --git a/topics/dp/dp/unboundedknapsack/RodCutting.java b/topics/topics/dp/unboundedknapsack/RodCutting.java similarity index 100% rename from topics/dp/dp/unboundedknapsack/RodCutting.java rename to topics/topics/dp/unboundedknapsack/RodCutting.java diff --git a/topics/dp/dp/unboundedknapsack/readme.md b/topics/topics/dp/unboundedknapsack/readme.md similarity index 100% rename from topics/dp/dp/unboundedknapsack/readme.md rename to topics/topics/dp/unboundedknapsack/readme.md diff --git a/topics/topics/graph/Cycle.java b/topics/topics/graph/Cycle.java new file mode 100644 index 0000000..e927539 --- /dev/null +++ b/topics/topics/graph/Cycle.java @@ -0,0 +1,46 @@ +package graph; + +import java.util.ArrayDeque; +import java.util.ArrayList; + +public class Cycle { + + public void traverseBfs(ArrayList> list, int nodes){ + boolean[] visited = new boolean[nodes]; + for(int i=0;i> list, int curr, boolean[] visited) { + + ArrayDeque queue = new ArrayDeque<>(); + queue.add(curr); + visited[curr] = true; + + while(!queue.isEmpty()) + { + curr = queue.remove(); + System.out.println(curr); + for(int neigh: list.get(curr)) + { + if(!visited[neigh]){ + visited[neigh] = true; + queue.add(neigh); + } + else{ + System.out.println("cycle."); + } + } + } + } + + public static void main(String[] args) { + + Graph g = new Graph(); + ArrayList> graph = g.sampleGraph(); + + } +} diff --git a/topics/topics/graph/Graph.java b/topics/topics/graph/Graph.java new file mode 100644 index 0000000..783dbbf --- /dev/null +++ b/topics/topics/graph/Graph.java @@ -0,0 +1,27 @@ +package graph; + +import java.util.ArrayList; + +public class Graph { + + public ArrayList> sampleGraph() { + + int[][] graph = new int[][]{{0, 1}, {0, 2}, {1, 2}, {1, 3}, {3, 4}, {2, 4}}; + return createGraph(graph, 5, true); + } + + private ArrayList> createGraph(int[][] graph, int nodes, boolean bidirectional) { + ArrayList> list = new ArrayList<>(); + for (int i = 0; i < nodes; i++) { + list.add(new ArrayList<>()); + } + for(int[] vertex: graph){ + list.get(vertex[0]).add(vertex[1]); + if(bidirectional) { + list.get(vertex[1]).add(vertex[0]); + } + } + return list; + } + +} diff --git a/topics/topics/graph/Traversal.java b/topics/topics/graph/Traversal.java new file mode 100644 index 0000000..efcdc0a --- /dev/null +++ b/topics/topics/graph/Traversal.java @@ -0,0 +1,76 @@ +package graph; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Stack; + +public class Traversal { + + public void traverseBfs(ArrayList> list, int nodes){ + boolean[] visited = new boolean[nodes]; + for(int i=0;i> list, int curr, boolean[] visited) { + + ArrayDeque queue = new ArrayDeque<>(); + queue.add(curr); + visited[curr] = true; + + while(!queue.isEmpty()) + { + curr = queue.remove(); + System.out.println(curr); + for(int neigh: list.get(curr)) + { + if(!visited[neigh]){ + visited[neigh] = true; + queue.add(neigh); + } + } + } + } + + private void traverseDfs(ArrayList> list, int nodes){ + boolean[] visited = new boolean[nodes]; + for(int i=0;i> list, int curr, boolean[] visited) { + Stack stack = new Stack<>(); + stack.add(curr); + visited[curr] = true; + + while(!stack.isEmpty()){ + curr = stack.pop(); + System.out.println(curr); + for(int neigh: list.get(curr)){ + if(!visited[neigh]){ + visited[neigh] = true; + stack.add(neigh); + } + } + } + } + + + public static void main(String[] args) { + + Graph g = new Graph(); + ArrayList> graph = g.sampleGraph(); + Traversal traversal = new Traversal(); + System.out.println("BFS"); + traversal.traverseBfs(graph, 5); + System.out.println("\nDFS"); + traversal.traverseDfs(graph, 5); + } + +} diff --git a/topics/topics/graph/problems/PathWithMaxProbability.java b/topics/topics/graph/problems/PathWithMaxProbability.java new file mode 100644 index 0000000..80adf0e --- /dev/null +++ b/topics/topics/graph/problems/PathWithMaxProbability.java @@ -0,0 +1,46 @@ +package graph.problems; + +import java.util.ArrayDeque; + +public class PathWithMaxProbability { + + public static void main(String[] args) { + + int[][] graph = {{0, 1}, {1, 2}, {0, 2}}; + int n = 3; + double[] succProb = new double[]{0.5, 0.5, 0.2}; + int startNode = 0; + int endNode = 3; + + traversal(graph, n, succProb, startNode, endNode); + + } + + private static void traversal(int[][] graph, int n, double[] succProb, int startNode, int endNode) { + + boolean[] visited = new boolean[n]; + traversal(graph, startNode, endNode, visited); + + } + + //written a plain dfs.. to compute + private static void traversal(int[][] graph, int startNode, int endNode, boolean[] visited) { + + ArrayDeque queue = new ArrayDeque<>(); + queue.add(startNode); + visited[startNode] = true; + + while(!queue.isEmpty()){ + Integer top = queue.remove(); + System.out.println(top+" "); + for(int neigh: graph[top]){ + if(!visited[neigh]){ + visited[neigh] = true; + queue.add(neigh); + } + } + } + + } + +} diff --git a/topics/dp/trees/Traversal.java b/topics/topics/trees/Traversal.java similarity index 100% rename from topics/dp/trees/Traversal.java rename to topics/topics/trees/Traversal.java diff --git a/topics/dp/trees/TreeNode.java b/topics/topics/trees/TreeNode.java similarity index 100% rename from topics/dp/trees/TreeNode.java rename to topics/topics/trees/TreeNode.java diff --git a/topics/dp/trees/bst/BinarySearchTree.java b/topics/topics/trees/bst/BinarySearchTree.java similarity index 100% rename from topics/dp/trees/bst/BinarySearchTree.java rename to topics/topics/trees/bst/BinarySearchTree.java diff --git a/topics/dp/trees/tree/CreateTree.java b/topics/topics/trees/tree/CreateTree.java similarity index 100% rename from topics/dp/trees/tree/CreateTree.java rename to topics/topics/trees/tree/CreateTree.java diff --git a/topics/dp/trees/tree/PathSum.java b/topics/topics/trees/tree/PathSum.java similarity index 100% rename from topics/dp/trees/tree/PathSum.java rename to topics/topics/trees/tree/PathSum.java diff --git a/topics/dp/trees/tree/Tree.java b/topics/topics/trees/tree/Tree.java similarity index 100% rename from topics/dp/trees/tree/Tree.java rename to topics/topics/trees/tree/Tree.java