diff --git a/Anagram.java b/Algorithms/Anagram.java similarity index 100% rename from Anagram.java rename to Algorithms/Anagram.java diff --git a/Factorial.java b/Algorithms/Factorial.java similarity index 100% rename from Factorial.java rename to Algorithms/Factorial.java diff --git a/Gcd.java b/Algorithms/Gcd.java similarity index 100% rename from Gcd.java rename to Algorithms/Gcd.java diff --git a/Algorithms/LongestPallindromicSubsequenc.java b/Algorithms/LongestPallindromicSubsequenc.java new file mode 100644 index 0000000..00dc870 --- /dev/null +++ b/Algorithms/LongestPallindromicSubsequenc.java @@ -0,0 +1,55 @@ +import java.util.*; +import java.lang.*; +import java.io.*; + +class LongestPallindromicSubsequenc{ + + int max(int a, int b) + { + return (a>b ? a:b); + } + + int LCS(int n, char[] arr, char[] b){ + + int[][] dp=new int[n+1][n+1]; + + for(int i=0;i0){ + String str=sc.next(); + int n=str.length(); + char[] arr=str.toCharArray(); + char[] b=new char[n]; + + for(int i=0;iui)return -1; - if(array[curindex]==key)return key; - else if(key>array[curindex])return search(array,key,curindex+1,ui); - else - return search(array,key,li,curindex-1); - } - public static void main(String[] args) { - // TODO Auto-generated method stub - int n=sc.nextInt(); - int array[]=new int[n]; - for(int i=0;iui)return -1; + if(array[curindex]==key)return key; + else if(key>array[curindex])return search(array,key,curindex+1,ui); + else + return search(array,key,li,curindex-1); + } + public static void main(String[] args) { + // TODO Auto-generated method stub + int n=sc.nextInt(); + int array[]=new int[n]; + for(int i=0;iui)break; - else if(search>array[curin]) - li=curin+1; - else - ui=curin-1; - } - if(found)System.out.println("Yes"); - else - System.out.println("No"); - } - -} diff --git a/Bubble.java b/Bubble.java deleted file mode 100644 index c8827f0..0000000 --- a/Bubble.java +++ /dev/null @@ -1,27 +0,0 @@ -package Sorting; -import java.util.Scanner; -public class Bubble { -static Scanner sc=new Scanner(System.in); - void sort(int array[]) { - int n=array.length; - for(int i=n-1;i>1;i--) { - for(int j=0;jarray[j+1]) { - int temp=array[j]; - array[j]=array[j+1]; - array[j+1]=temp; - } - } - } - } - public static void main(String[] args) { - // TODO Auto-generated method stub - int n=sc.nextInt(); - int[] array=new int[n]; - for(int i=0;i adj[]; - - @SuppressWarnings("unchecked") - Graph(int v) - { - V = v; - adj = new LinkedList[v]; - for (int i = 0; i < v; ++i) - adj[i] = new LinkedList<>(); - } - - // Method to add an edge into the graph - void addEdge(int v, int w) - { - - // Add w to v's list. - adj[v].add(w); - } - - // A recursive method to count - // all paths from 'u' to 'd'. - int countPathsUtil(int u, int d, - int pathCount) - { - - // If current vertex is same as - // destination, then increment count - if (u == d) { - pathCount++; - } - - // Recur for all the vertices - // adjacent to this vertex - else { - Iterator i = adj[u].listIterator(); - while (i.hasNext()) { - int n = i.next(); - pathCount = countPathsUtil(n, d, pathCount); - } - } - return pathCount; - } - - // Returns count of - // paths from 's' to 'd' - int countPaths(int s, int d) - { - - // Call the recursive method - // to count all paths - int pathCount = 0; - pathCount = countPathsUtil(s, d, - pathCount); - return pathCount; - } - - // Driver Code - public static void main(String args[]) - { - Graph g = new Graph(5); - g.addEdge(0, 1); - g.addEdge(0, 2); - g.addEdge(0, 3); - g.addEdge(1, 3); - g.addEdge(2, 3); - g.addEdge(1, 4); - g.addEdge(2, 4); - - int s = 0, d = 3; - System.out.println(g.countPaths(s, d)); - } -} - - diff --git a/BFS/BFS.java b/Graph/BFS/BFS.java similarity index 100% rename from BFS/BFS.java rename to Graph/BFS/BFS.java diff --git a/BFSgraph.java b/Graph/BFSgraph.java similarity index 100% rename from BFSgraph.java rename to Graph/BFSgraph.java diff --git a/Graph Algorithms/Cyclic_detection b/Graph/Cyclic_detection similarity index 100% rename from Graph Algorithms/Cyclic_detection rename to Graph/Cyclic_detection diff --git a/DFS/DFS.java b/Graph/DFS/DFS.java similarity index 100% rename from DFS/DFS.java rename to Graph/DFS/DFS.java diff --git a/DFSgraph.java b/Graph/DFSgraph.java similarity index 100% rename from DFSgraph.java rename to Graph/DFSgraph.java diff --git a/Graph Algorithms/Description b/Graph/Description similarity index 100% rename from Graph Algorithms/Description rename to Graph/Description diff --git a/Graph.java b/Graph/Graph.java similarity index 100% rename from Graph.java rename to Graph/Graph.java diff --git a/Graph Algorithms/Single_source_Shortest Path b/Graph/Single_source_Shortest Path similarity index 100% rename from Graph Algorithms/Single_source_Shortest Path rename to Graph/Single_source_Shortest Path diff --git a/Graph Algorithms/k_cores.java b/Graph/k_cores.java similarity index 95% rename from Graph Algorithms/k_cores.java rename to Graph/k_cores.java index eecba82..314b08d 100644 --- a/Graph Algorithms/k_cores.java +++ b/Graph/k_cores.java @@ -1,170 +1,170 @@ -// Java program to find K-Cores of a graph -import java.util.*; - -class k_cores -{ - - // This class represents a undirected graph using adjacency - // list representation - static class Graph - { - int V; // No. of vertices - - // Pointer to an array containing adjacency lists - Vector[] adj; - - @SuppressWarnings("unchecked") - Graph(int V) - { - this.V = V; - this.adj = new Vector[V]; - - for (int i = 0; i < V; i++) - adj[i] = new Vector<>(); - } - - // function to add an edge to graph - void addEdge(int u, int v) - { - this.adj[u].add(v); - this.adj[v].add(u); - } - - // A recursive function to print DFS starting from v. - // It returns true if degree of v after processing is less - // than k else false - // It also updates degree of adjacent if degree of v - // is less than k. And if degree of a processed adjacent - // becomes less than k, then it reduces of degree of v also, - boolean DFSUtil(int v, boolean[] visited, int[] vDegree, int k) - { - - // Mark the current node as visited and print it - visited[v] = true; - - // Recur for all the vertices adjacent to this vertex - for (int i : adj[v]) - { - - // degree of v is less than k, then degree of adjacent - // must be reduced - if (vDegree[v] < k) - vDegree[i]--; - - // If adjacent is not processed, process it - if (!visited[i]) - { - - // If degree of adjacent after processing becomes - // less than k, then reduce degree of v also. - if (DFSUtil(i, visited, vDegree, k)) - vDegree[v]--; - } - } - - // Return true if degree of v is less than k - return (vDegree[v] < k); - } - - // Prints k cores of an undirected graph - void printKCores(int k) - { - - // INITIALIZATION - // Mark all the vertices as not visited and not - // processed. - boolean[] visited = new boolean[V]; - boolean[] processed = new boolean[V]; - Arrays.fill(visited, false); - Arrays.fill(processed, false); - - int mindeg = Integer.MAX_VALUE; - int startvertex = 0; - - // Store degrees of all vertices - int[] vDegree = new int[V]; - for (int i = 0; i < V; i++) - { - vDegree[i] = adj[i].size(); - - if (vDegree[i] < mindeg) - { - mindeg = vDegree[i]; - startvertex = i; - } - } - DFSUtil(startvertex, visited, vDegree, k); - - // DFS traversal to update degrees of all - // vertices. - for (int i = 0; i < V; i++) - if (!visited[i]) - DFSUtil(i, visited, vDegree, k); - - // PRINTING K CORES - System.out.println("K-Cores : "); - for (int v = 0; v < V; v++) - { - - // Only considering those vertices which have degree - // >= K after BFS - if (vDegree[v] >= k) - { - System.out.printf("\n[%d]", v); - - // Traverse adjacency list of v and print only - // those adjacent which have vDegree >= k after - // BFS. - for (int i : adj[v]) - if (vDegree[i] >= k) - System.out.printf(" -> %d", i); - } - } - } - } - - // Driver Code - public static void main(String[] args) - { - - // Create a graph given in the above diagram - int k = 3; - Graph g1 = new Graph(9); - g1.addEdge(0, 1); - g1.addEdge(0, 2); - g1.addEdge(1, 2); - g1.addEdge(1, 5); - g1.addEdge(2, 3); - g1.addEdge(2, 4); - g1.addEdge(2, 5); - g1.addEdge(2, 6); - g1.addEdge(3, 4); - g1.addEdge(3, 6); - g1.addEdge(3, 7); - g1.addEdge(4, 6); - g1.addEdge(4, 7); - g1.addEdge(5, 6); - g1.addEdge(5, 8); - g1.addEdge(6, 7); - g1.addEdge(6, 8); - g1.printKCores(k); - - System.out.println(); - - Graph g2 = new Graph(13); - g2.addEdge(0, 1); - g2.addEdge(0, 2); - g2.addEdge(0, 3); - g2.addEdge(1, 4); - g2.addEdge(1, 5); - g2.addEdge(1, 6); - g2.addEdge(2, 7); - g2.addEdge(2, 8); - g2.addEdge(2, 9); - g2.addEdge(3, 10); - g2.addEdge(3, 11); - g2.addEdge(3, 12); - g2.printKCores(k); - } -} - +// Java program to find K-Cores of a graph +import java.util.*; + +class k_cores +{ + + // This class represents a undirected graph using adjacency + // list representation + static class Graph + { + int V; // No. of vertices + + // Pointer to an array containing adjacency lists + Vector[] adj; + + @SuppressWarnings("unchecked") + Graph(int V) + { + this.V = V; + this.adj = new Vector[V]; + + for (int i = 0; i < V; i++) + adj[i] = new Vector<>(); + } + + // function to add an edge to graph + void addEdge(int u, int v) + { + this.adj[u].add(v); + this.adj[v].add(u); + } + + // A recursive function to print DFS starting from v. + // It returns true if degree of v after processing is less + // than k else false + // It also updates degree of adjacent if degree of v + // is less than k. And if degree of a processed adjacent + // becomes less than k, then it reduces of degree of v also, + boolean DFSUtil(int v, boolean[] visited, int[] vDegree, int k) + { + + // Mark the current node as visited and print it + visited[v] = true; + + // Recur for all the vertices adjacent to this vertex + for (int i : adj[v]) + { + + // degree of v is less than k, then degree of adjacent + // must be reduced + if (vDegree[v] < k) + vDegree[i]--; + + // If adjacent is not processed, process it + if (!visited[i]) + { + + // If degree of adjacent after processing becomes + // less than k, then reduce degree of v also. + if (DFSUtil(i, visited, vDegree, k)) + vDegree[v]--; + } + } + + // Return true if degree of v is less than k + return (vDegree[v] < k); + } + + // Prints k cores of an undirected graph + void printKCores(int k) + { + + // INITIALIZATION + // Mark all the vertices as not visited and not + // processed. + boolean[] visited = new boolean[V]; + boolean[] processed = new boolean[V]; + Arrays.fill(visited, false); + Arrays.fill(processed, false); + + int mindeg = Integer.MAX_VALUE; + int startvertex = 0; + + // Store degrees of all vertices + int[] vDegree = new int[V]; + for (int i = 0; i < V; i++) + { + vDegree[i] = adj[i].size(); + + if (vDegree[i] < mindeg) + { + mindeg = vDegree[i]; + startvertex = i; + } + } + DFSUtil(startvertex, visited, vDegree, k); + + // DFS traversal to update degrees of all + // vertices. + for (int i = 0; i < V; i++) + if (!visited[i]) + DFSUtil(i, visited, vDegree, k); + + // PRINTING K CORES + System.out.println("K-Cores : "); + for (int v = 0; v < V; v++) + { + + // Only considering those vertices which have degree + // >= K after BFS + if (vDegree[v] >= k) + { + System.out.printf("\n[%d]", v); + + // Traverse adjacency list of v and print only + // those adjacent which have vDegree >= k after + // BFS. + for (int i : adj[v]) + if (vDegree[i] >= k) + System.out.printf(" -> %d", i); + } + } + } + } + + // Driver Code + public static void main(String[] args) + { + + // Create a graph given in the above diagram + int k = 3; + Graph g1 = new Graph(9); + g1.addEdge(0, 1); + g1.addEdge(0, 2); + g1.addEdge(1, 2); + g1.addEdge(1, 5); + g1.addEdge(2, 3); + g1.addEdge(2, 4); + g1.addEdge(2, 5); + g1.addEdge(2, 6); + g1.addEdge(3, 4); + g1.addEdge(3, 6); + g1.addEdge(3, 7); + g1.addEdge(4, 6); + g1.addEdge(4, 7); + g1.addEdge(5, 6); + g1.addEdge(5, 8); + g1.addEdge(6, 7); + g1.addEdge(6, 8); + g1.printKCores(k); + + System.out.println(); + + Graph g2 = new Graph(13); + g2.addEdge(0, 1); + g2.addEdge(0, 2); + g2.addEdge(0, 3); + g2.addEdge(1, 4); + g2.addEdge(1, 5); + g2.addEdge(1, 6); + g2.addEdge(2, 7); + g2.addEdge(2, 8); + g2.addEdge(2, 9); + g2.addEdge(3, 10); + g2.addEdge(3, 11); + g2.addEdge(3, 12); + g2.printKCores(k); + } +} + diff --git a/Insertion.java b/Insertion.java deleted file mode 100644 index 6a4752a..0000000 --- a/Insertion.java +++ /dev/null @@ -1,24 +0,0 @@ -package Sorting; - -import java.util.Scanner; - -public class Insertion { - static Scanner sc=new Scanner(System.in); - public static void sort(int [] array) { - int n=array.length,j; - for(int i=1;i=0 && temp main() in Java is the entry point for any Java program. It is always written as public static void main(String[] args). + * public: Public is an access modifier, which is used to specify who can access this method. Public means that this Method will be accessible by any Class. + * static: It is a keyword in java which identifies it is class-based. main() is made static in Java so that it can be accessed without creating the instance of a Class. In case, main is not made static then the compiler will throw an error as main() is called by the JVM before any objects are made and only static methods can be directly invoked via the class. + * void: It is the return type of the method. Void defines the method which will not return any value. + * main: It is the name of the method which is searched by JVM as a starting point for an application with a particular signature only. It is the method where the main execution occurs. + * String args[]: It is the parameter passed to the main method. \ No newline at end of file diff --git a/Queue.java b/Queue/Queue.java similarity index 100% rename from Queue.java rename to Queue/Queue.java diff --git a/Selection.java b/Selection.java deleted file mode 100644 index 6df8ce4..0000000 --- a/Selection.java +++ /dev/null @@ -1,28 +0,0 @@ -package Sorting; -import java.util.Scanner; -public class Selection { -static Scanner sc=new Scanner(System.in); - public static void sort(int array[]) { - int n=array.length;int min; - for(int i=0;i