diff --git a/DataStructures/Graphs/GraphAlgos b/DataStructures/Graphs/GraphAlgos new file mode 100644 index 000000000..19938c48f --- /dev/null +++ b/DataStructures/Graphs/GraphAlgos @@ -0,0 +1,487 @@ +package DataStructures.Graphs; +/* +Implementation of graph by using hashmap for vertices of class which contains hashmap for vertex and then algos like prims dijktsra ,depth for search and traversal ,breadth for search and traversal ,algo for cycle present or not ,connected or not ,if not connected then connect it +Test case +Graph gp=new Graph(); + gp.addVertex("A"); + gp.addVertex("B"); + gp.addVertex("C"); + gp.addVertex("D"); + gp.addVertex("E"); + gp.addVertex("F"); + gp.addVertex("G"); + gp.addEdge("A", "B", 2); + gp.addEdge("A", "D", 10); + gp.addEdge("B", "C", 3); + gp.addEdge("C", "D", 1); + gp.addEdge("D", "E", 8); + gp.addEdge("E", "F", 5); + gp.addEdge("E", "G", 6); + gp.addEdge("F", "G", 4); + +// gp.display(); +// System.out.println(gp.numVertex()); +// System.out.println(gp.numEdge()); +// System.out.println(gp.containsEdge("A", "C")); +// +// System.out.println(gp.containsEdge("E", "F")); +// gp.removeEdge("D", "E"); +// gp.display(); +// gp.removeVertex("F"); +// gp.addVertex("F"); +// gp.display(); +// System.out.println(gp.hasPath("A", "F", new HashMap<>())); +// System.out.println(gp.dfs("A", "F")); +// gp.bft(); +// gp.dft(); +// gp.removeEdge("B","C"); +// gp.removeEdge("F","G"); +// System.out.println(gp.isConnected()); +// System.out.println(gp.isCyclic()); +// System.out.println(gp.isTree()); +// System.out.println(gp.getConnectedComp()); +// gp.prims().display(); + System.out.println(gp.Dijktsra("A")); + + + +*/ + + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedList; +import Heaps.GenericHeap; +public class Graph { + private class vertex{ + HashMap nbrs=new HashMap<>(); + } + HashMap vertcs; + public Graph(){ + vertcs=new HashMap<>(); + } + public int numVertex() { + return this.vertcs.size(); + } + public boolean containsVertex(String vname) { + return this.vertcs.containsKey(vname); + } + public void addVertex(String vname) { + + vertex vtx=new vertex(); + this.vertcs.put(vname,vtx); + } + public void removeVertex(String vname) { + vertex vtx=this.vertcs.get(vname); + ArrayList keys=new ArrayList<>(vtx.nbrs.keySet()); + for(String key:keys) { + vertex nbrvtx=this.vertcs.get(key); + nbrvtx.nbrs.remove(vname); + } + this.vertcs.remove(vname); + } + + public int numEdge() { + int count=0; + ArrayList keys=new ArrayList<>(this.vertcs.keySet()); + for(String key:keys) { + vertex vtx=this.vertcs.get(key); + count+=vtx.nbrs.size(); + } + return count/2; + } + public boolean containsEdge(String vname1,String vname2) { + vertex vtx1=this.vertcs.get(vname1); + vertex vtx2=this.vertcs.get(vname2); + if(vtx1==null||vtx2==null||!vtx1.nbrs.containsKey(vname2)) + return false; + return true; + } + public void addEdge(String vname1,String vname2,int cost) { + vertex vtx1=this.vertcs.get(vname1); + vertex vtx2=this.vertcs.get(vname2); + if(vtx1==null||vtx2==null||vtx1.nbrs.containsKey(vname2)) + return; + vtx1.nbrs.put(vname2,cost); + vtx2.nbrs.put(vname1,cost); + } + public void removeEdge(String vname1,String vname2) { + vertex vtx1=this.vertcs.get(vname1); + vertex vtx2=this.vertcs.get(vname2); + if(vtx1==null||vtx2==null||!vtx1.nbrs.containsKey(vname2)) + return; + vtx1.nbrs.remove(vname2); + vtx2.nbrs.remove(vname1); + } + + public void display() { + ArrayList keys=new ArrayList<>(this.vertcs.keySet()); + for(String key:keys) { + vertex vtx=this.vertcs.get(key); + System.out.println(key+" := "+vtx.nbrs); + } + } + + public boolean hasPath(String source ,String dest,HashMap processed) { + processed.put(source, true); + if(containsEdge(source,dest)) { + return true; + } + vertex vtx=this.vertcs.get(source); + ArrayList keys=new ArrayList<>(vtx.nbrs.keySet()); + for(String key:keys) { + if(!processed.containsKey(key) && hasPath(key,dest,processed)) + return true; + } + return false; + + } + private class pair{ + String vname; + String psf; + } + public boolean bfs(String source,String dest) { // breadth first search + HashMap processed=new HashMap<>(); + + LinkedList queue=new LinkedList<>(); + pair sp=new pair(); + sp.vname=source; + sp.psf=source; + queue.addLast(sp); + + while(!queue.isEmpty()) { + pair rp=queue.removeFirst(); + if(processed.containsKey(rp.vname)) + continue; + processed.put(rp.vname,true); + + if(containsEdge(rp.vname,dest)) { + System.out.println(rp.psf+dest); + return true; + } + vertex vtx=this.vertcs.get(rp.vname); + ArrayList nbrs=new ArrayList<>(vtx.nbrs.keySet()); + for(String nbr:nbrs) { + if(!processed.containsKey(nbr)) { + pair np=new pair(); + np.vname=nbr; + np.psf=rp.psf+nbr; + queue.addLast(np); + } + } + } + return false; + } + public boolean dfs(String source,String dest) { //deapth first search + LinkedList stack=new LinkedList<>(); + HashMap processed=new HashMap<>(); + pair sp=new pair(); + sp.vname=source; + sp.psf=source; + stack.addFirst(sp); + while(!stack.isEmpty()) { + pair rp=stack.removeFirst(); + if(processed.containsKey(rp.vname)) + continue; + processed.put(rp.vname,true); + if(containsEdge(rp.vname,dest)) { + System.out.println(rp.psf+dest); + return true; + } + vertex vtx=this.vertcs.get(rp.vname); + ArrayList nbrs=new ArrayList<>(vtx.nbrs.keySet()); + for(String nbr:nbrs) { + if(!processed.containsKey(nbr)) { + pair np=new pair(); + np.vname=nbr; + np.psf=rp.psf+nbr; + stack.addFirst(np); + } + } + + } + return false; + } + public void bft() { //breadth first traversal + HashMap processed=new HashMap<>(); + LinkedList queue=new LinkedList<>(); + ArrayList keys=new ArrayList<>(this.vertcs.keySet()); + for(String key:keys) { + if(processed.containsKey(key)) + continue; + pair sp=new pair(); + sp.vname=key; + sp.psf=key; + queue.addLast(sp); + + while(!queue.isEmpty()) { + pair rp=queue.removeFirst(); + if(processed.containsKey(rp.vname)) + continue; + processed.put(rp.vname,true); + + System.out.println(rp.vname+" via "+rp.psf); + + vertex vtx=this.vertcs.get(rp.vname); + ArrayList nbrs=new ArrayList<>(vtx.nbrs.keySet()); + for(String nbr:nbrs) { + if(!processed.containsKey(nbr)) { + pair np=new pair(); + np.vname=nbr; + np.psf=rp.psf+nbr; + queue.addLast(np); + } + } + } + } + } + public void dft() { //deapt first traversal + HashMap processed=new HashMap<>(); + LinkedList stack=new LinkedList<>(); + ArrayList keys=new ArrayList<>(this.vertcs.keySet()); + for(String key:keys) { + pair sp=new pair(); + sp.vname=key; + sp.psf=key; + stack.addFirst(sp); + + while(!stack.isEmpty()) { + pair rp=stack.removeFirst(); + if(processed.containsKey(rp.vname)) + continue; + processed.put(rp.vname,true); + + System.out.println(rp.vname+" via "+rp.psf); + + vertex vtx=this.vertcs.get(rp.vname); + ArrayList nbrs=new ArrayList<>(vtx.nbrs.keySet()); + for(String nbr:nbrs) { + if(!processed.containsKey(nbr)) { + pair np=new pair(); + np.vname=nbr; + np.psf=rp.psf+nbr; + stack.addFirst(np); + } + } + } + } + } + + + public boolean isCyclic() { + HashMap processed=new HashMap<>(); + LinkedList queue=new LinkedList<>(); + ArrayList keys=new ArrayList<>(this.vertcs.keySet()); + for(String key:keys) { + if(processed.containsKey(key)) + continue; + pair sp=new pair(); + sp.vname=key; + sp.psf=key; + queue.addLast(sp); + + while(!queue.isEmpty()) { + pair rp=queue.removeFirst(); + if(processed.containsKey(rp.vname)) + return true; + processed.put(rp.vname,true); + + vertex vtx=this.vertcs.get(rp.vname); + ArrayList nbrs=new ArrayList<>(vtx.nbrs.keySet()); + for(String nbr:nbrs) { + if(!processed.containsKey(nbr)) { + pair np=new pair(); + np.vname=nbr; + np.psf=rp.psf+nbr; + queue.addLast(np); + } + } + } + } + return false; + } + public boolean isConnected() { + int flag=0; + HashMap processed=new HashMap<>(); + LinkedList queue=new LinkedList<>(); + ArrayList keys=new ArrayList<>(this.vertcs.keySet()); + for(String key:keys) { + if(processed.containsKey(key)) + continue; + flag++; + pair sp=new pair(); + sp.vname=key; + sp.psf=key; + queue.addLast(sp); + + while(!queue.isEmpty()) { + pair rp=queue.removeFirst(); + if(processed.containsKey(rp.vname)) + continue; + processed.put(rp.vname,true); + + vertex vtx=this.vertcs.get(rp.vname); + ArrayList nbrs=new ArrayList<>(vtx.nbrs.keySet()); + for(String nbr:nbrs) { + if(!processed.containsKey(nbr)) { + pair np=new pair(); + np.vname=nbr; + np.psf=rp.psf+nbr; + queue.addLast(np); + } + } + } + } + if(flag>=2) + return false; + else + return true; + } + public boolean isTree() { + return !isCyclic()&&isConnected(); + } + public ArrayList> getConnectedComp() { + ArrayList> ans=new ArrayList<>(); + HashMap processed=new HashMap<>(); + LinkedList queue=new LinkedList<>(); + ArrayList keys=new ArrayList<>(this.vertcs.keySet()); + for(String key:keys) { + if(processed.containsKey(key)) + continue; + ArrayList subans=new ArrayList<>(); + pair sp=new pair(); + sp.vname=key; + sp.psf=key; + queue.addLast(sp); + + while(!queue.isEmpty()) { + pair rp=queue.removeFirst(); + if(processed.containsKey(rp.vname)) + continue; + processed.put(rp.vname,true); + + subans.add(rp.vname); + + vertex vtx=this.vertcs.get(rp.vname); + ArrayList nbrs=new ArrayList<>(vtx.nbrs.keySet()); + for(String nbr:nbrs) { + if(!processed.containsKey(nbr)) { + pair np=new pair(); + np.vname=nbr; + np.psf=rp.psf+nbr; + queue.addLast(np); + } + } + } + ans.add(subans); + } + return ans; + } + private class PrimsPair implements Comparable{ + String vname; + String acqvname; + int cost; + public int compareTo(PrimsPair o) { + return o.cost-this.cost; + } + + } + public Graph prims() { + HashMap map=new HashMap<>(); + GenericHeap heap=new GenericHeap<>(); + Graph mst=new Graph(); + for(String vrtx:this.vertcs.keySet()) { + PrimsPair np=new PrimsPair(); + np.acqvname=null; + np.vname=vrtx; + np.cost=Integer.MAX_VALUE; + heap.add(np); + map.put(vrtx, np); + } + while(!heap.isEmpty()) { + PrimsPair rp=heap.remove(); + map.remove(rp.vname); + + if(rp.acqvname==null) { + mst.addVertex(rp.vname); + }else { + mst.addVertex(rp.vname); + mst.addEdge(rp.vname, rp.acqvname, rp.cost); + } + + for(String nbr:this.vertcs.get(rp.vname).nbrs.keySet()) { + if(map.containsKey(nbr)) { + //old cost that is from diff path stored in map + int oc=map.get(nbr).cost; + // cost that present vname need cost to go to nbr + int nc=this.vertcs.get(rp.vname).nbrs.get(nbr); + if(nc{ + String vname; + String psf; + int cost; + public int compareTo(DijktsraPair o) { + return o.cost-this.cost; + } + + } + public HashMap Dijktsra(String source) { + HashMap map=new HashMap<>(); + GenericHeap heap=new GenericHeap<>(); + HashMap ans =new HashMap<>(); + for(String vrtx:this.vertcs.keySet()) { + DijktsraPair np=new DijktsraPair(); + np.psf=""; + np.vname=vrtx; + np.cost=Integer.MAX_VALUE; + if(vrtx==source) { + np.cost=0; + np.psf=source; + } + heap.add(np); + map.put(vrtx, np); + } + while(!heap.isEmpty()) { + DijktsraPair rp=heap.remove(); + map.remove(rp.vname); + + ans.put(rp.vname,rp.cost); + + for(String nbr:this.vertcs.get(rp.vname).nbrs.keySet()) { + if(map.containsKey(nbr)) { + //old cost that is from diff path stored in map + int oc=map.get(nbr).cost; + // cost that present vname need cost to go to nbr + int nc=rp.cost+this.vertcs.get(rp.vname).nbrs.get(nbr); + if(nc=len) { + arr2[i-len]=scn.nextInt(); + } + } + System.out.println(Main(arr,arr2)); + + + +*/ + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; +import java.util.Set; + +public class Intersection { + + public static ArrayList Main(int arr[],int arr2[]) { + HashMap hmap=new HashMap<>(); + HashMap hmap2=new HashMap<>(); + for(int i=0;i res=new ArrayList<>(); + for(int i=0;i0) { + int val=hmap.get(arr2[i]); + hmap.put(arr2[i],val-1); + res.add(arr2[i]); + } + + } + return res; + } + public Intersection() { + + } + + + +} diff --git a/DataStructures/HashMap/Hashing/LinkedList.java b/DataStructures/HashMap/Hashing/LinkedList.java deleted file mode 100644 index 4953551f7..000000000 --- a/DataStructures/HashMap/Hashing/LinkedList.java +++ /dev/null @@ -1,63 +0,0 @@ -package DataStructures.HashMap.Hashing; - -class LinkedList { - - private Node Head; - private int size; - - public LinkedList() { - Head = null; - size = 0; - } - - public void insert(int data) { - - Node newnode = new Node(data); - - size++; - - if(Head == null) { - Head = newnode; - } - else { - newnode.next = Head; - Head = newnode; - } - } - - public void delete(int data) { - if(size == 0) { - System.out.println("UnderFlow!"); - return; - } - - else { - Node curr = Head; - if (curr.data == data) { - Head = curr.next; - size--; - return; - } - else { - - while(curr.next.next != null) { - if(curr.next.data == data){ - curr.next = curr.next.next; - return; - } - } - - System.out.println("Key not Found"); - } - } - } - - public void display() { - Node temp = Head; - while(temp != null) { - System.out.printf("%d ",temp.data); - temp = temp.next; - } - System.out.println(); - } -} \ No newline at end of file diff --git a/DataStructures/HashMap/Hashing/Node.java b/DataStructures/HashMap/Hashing/Node.java deleted file mode 100644 index 74ab01f9d..000000000 --- a/DataStructures/HashMap/Hashing/Node.java +++ /dev/null @@ -1,11 +0,0 @@ -package DataStructures.HashMap.Hashing; - -class Node { - int data; - Node next; - - public Node(int data) { - this.data = data; - this.next = null; - } -} \ No newline at end of file diff --git a/DataStructures/Lists/ListAddnFun b/DataStructures/Lists/ListAddnFun new file mode 100644 index 000000000..afbd2ae43 --- /dev/null +++ b/DataStructures/Lists/ListAddnFun @@ -0,0 +1,141 @@ +package DataStructures.Lists; + +/* + * This class implements a SinglyLinked List. + * A linked list is similar to an array, it hold values. + * However, links in a linked list do not have indexes. With + * a linked list you do not need to predetermine it's size as + * it grows and shrinks as it is edited. + *it has functions called mid that gives node at mid + * in addn to linked list there is algo that + * construct a linked list with alternate sums of linked list + * and added to new one and add mid value + * i.e sum of first and last value of inital list + + Test Case: + + + LinkedList LL1 = new LinkedList(); + Scanner scn=new Scanner(System.in); + int numNodes=scn.nextInt(); + for(int i=0;i<2*numNodes;i++) { + LL1.addLast(scn.nextInt()); + } + LL1.display(); + LinkedList LL2=new LinkedList(); + LL2.formLL2(LL1); + LL2.display(); + LinkedList LL3=new LinkedList(); + LL3.formLL3(LL1); + LL3.display(); + Node MID=LL1.midValue(); + System.out.println(MID.data); + LinkedList updLL1=new LinkedList(); + updLL1.formRes(LL1,LL2,LL3,MID); + updLL1.display(); + updLL1.Size(); + + */ + + + +import java.util.*; +import java.lang.*; +import java.io.*; +class LinkedList { + private class Node{ + int data; + Node next; + + Node(int data) { + this.data = data; + this.next = null; + } + } + public Node head = null; + public Node tail = null; + private int size=0; + + public void addLast(int data) { + Node newNode = new Node(data); + + if(this.head == null) { + this.head = newNode; + this.tail = newNode; + this.size++; + } + else { + this.tail.next = newNode; + this.tail = newNode; + this.size++; + } + } + + + public void display() { + Node current = this.head; + if(this.head == null) { + return; + } + while(current != null) { + System.out.print(current.data + " "); + current = current.next; + } + System.out.println(); + } + + public void formLL2(LinkedList LL1) { + Node current=LL1.head; + while(current.next!=null&¤t.next.next!=null) { + int sum=current.data+current.next.next.data; + this.addLast(sum); + current=current.next.next; + } + } + public void formLL3(LinkedList LL1) { + Node current=LL1.head.next; + while(current.next!=null&¤t.next.next!=null) { + int sum=current.data+current.next.next.data; + this.addLast(sum); + current=current.next.next; + } + } + public Node mid() { + Node slow=this.head; + Node fast=this.head; + while(fast.next!=null && fast.next.next!=null) { + slow=slow.next; + fast=fast.next.next; + } + return slow; + } + public Node midValue() { + int sum=this.head.data+this.tail.data; + Node mid=new Node(sum); + return mid; + } + public void formRes(LinkedList LL1,LinkedList LL2,LinkedList LL3,Node MID) { + Node LL1mid=LL1.mid(); + Node currentLL1=LL1.head; + Node currentLL2=LL2.head; + Node currentLL3=LL3.head; + while(currentLL1!=null) { + this.addLast(currentLL1.data); + + if(currentLL2!=null) { + this.addLast(currentLL2.data); + currentLL2=currentLL2.next; + }else if(currentLL1.equals(LL1mid)) { + this.addLast(MID.data); + } + else if(currentLL2==null&¤tLL3!=null) { + this.addLast(currentLL3.data); + currentLL3=currentLL3.next; + } + currentLL1=currentLL1.next; + } + } + public void Size() { + System.out.println(this.size); + } + } diff --git a/DataStructures/Trees/AVLSimple b/DataStructures/Trees/AVLSimple new file mode 100644 index 000000000..ace0c46b9 --- /dev/null +++ b/DataStructures/Trees/AVLSimple @@ -0,0 +1,137 @@ + +package DataStructures.Trees; + +/* +* Avl is algo that balance itself while adding new alues to tree +* by rotating branches of binary tree and make itself Binary seaarch tree +* there are four cases which has to tackle +* rotating - left right ,left left,right right,right left + +Test Case: + +AVLTree tree=new AVLTree(); + tree.insert(20); + tree.insert(25); + tree.insert(30); + tree.insert(10); + tree.insert(5); + tree.insert(15); + tree.insert(27); + tree.insert(19); + tree.insert(16); + + tree.display(); + + + + +*/ + + + +public class AVLTree { + private class Node{ + int data; + int height; + Node left; + Node right; + Node(int data){ + this.data=data; + this.height=1; + } + + } + private Node root; + public void insert(int data) { + this.root=insert(this.root,data); + } + private Node insert(Node node,int item) { + if(node==null) { + Node add=new Node(item); + return add; + } + if(node.data>item) { + node.left=insert(node.left,item); + } + if(node.data1&&itemnode.right.data) + return leftRotate(node); + //RL case + if(bf<-1&&item1&&item>node.left.data) { + node.left=leftRotate(node.left); + return rightRotate(node); + } + + return node; + } + public void display() { + this.display(this.root); + System.out.println(this.root.height); + } + private void display (Node node) { + String str=""; + if(node.left!=null) + str+=node.left.data+"=>"; + else + str+="END=>"; + str+=node.data+""; + if(node.right!=null) + str+="<="+node.right.data; + else + str+="<=END"; + System.out.println(str); + if(node.left!=null) + display(node.left); + if(node.right!=null) + display(node.right); + } + private int height(Node node) { + if(node==null) { + return 0; + } + return node.height; + + } + private int bf(Node node) { + if(node==null) + return 0; + return height(node.left)-height(node.right); + } + + private Node rightRotate(Node c) { + Node b=c.left; + Node T3=b.right; + + b.right=c; + c.left=T3; + c.height=Math.max(height(c.left),height(c.right))+1; + b.height=Math.max(height(b.left),height(b.right))+1; + return b; + + } + private Node leftRotate(Node c) { + Node b=c.right; + Node T3=b.left; + + b.left=c; + c.right=T3; + c.height=Math.max(height(c.left),height(c.right))+1; + b.height=Math.max(height(b.left),height(b.right))+1; + return b; + + } + +} diff --git a/DynamicProgramming/BoardPath b/DynamicProgramming/BoardPath new file mode 100644 index 000000000..5ca14d1fa --- /dev/null +++ b/DynamicProgramming/BoardPath @@ -0,0 +1,77 @@ +package DynamicProgramming; +/* +* this is an important Algo in which +* we have starting and ending of board and we have to reach +* we have to count no. of ways +* that help to reach end point i.e number by rolling dice +* which have 1 to 6 digits + +Test Case: +here target is 10 + +int n=10; + startAlgo(); + System.out.println(bpR(0,n)); + System.out.println(endAlgo()+"ms"); + int[] strg=new int [n+1]; + startAlgo(); + System.out.println(bpRS(0,n,strg)); + System.out.println(endAlgo()+"ms"); + startAlgo(); + System.out.println(bpIS(0,n,strg)); + System.out.println(endAlgo()+"ms"); + + + +*/ +public class BoardPath { + public static long startTime; + public static long endTime; + public static void startAlgo() { + startTime=System.currentTimeMillis(); + } + public static long endAlgo() { + endTime=System.currentTimeMillis(); + return endTime-startTime; + } + public static int bpR(int start,int end){ + if(start==end) { + return 1; + } + else if(start>end) + return 0; + int count=0; + for(int dice=1;dice<=6;dice++) { + count+=bpR(start+dice,end); + } + return count; + } + public static int bpRS(int curr,int end,int strg[]){ + if(curr==end) { + return 1; + } + else if(curr>end) + return 0; + if(strg[curr]!=0) + return strg[curr]; + int count=0; + for(int dice=1;dice<=6;dice++) { + count+=bpRS(curr+dice,end,strg); + } + strg[curr]=count; + return count; + } + public static int bpIS(int curr,int end,int[]strg){ + strg[end]=1; + for(int i=end-1;i>=0;i--) { + int count=0; + for(int dice=1;dice<=6&&dice+i