From 627115b3368f38e45f9a2b0928cab0ad8d790cf4 Mon Sep 17 00:00:00 2001 From: Ritik2604 <49342895+Ritik2604@users.noreply.github.com> Date: Fri, 22 May 2020 16:23:41 +0530 Subject: [PATCH 01/16] Created Graph Algos it contains Dijkstra, Prims, dft, bft, dfs, bfs and many more. --- DataStructures/Graphs/GraphAlgos | 437 +++++++++++++++++++++++++++++++ 1 file changed, 437 insertions(+) create mode 100644 DataStructures/Graphs/GraphAlgos diff --git a/DataStructures/Graphs/GraphAlgos b/DataStructures/Graphs/GraphAlgos new file mode 100644 index 000000000000..780a73e45540 --- /dev/null +++ b/DataStructures/Graphs/GraphAlgos @@ -0,0 +1,437 @@ +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 Date: Sat, 23 May 2020 05:30:43 +0530 Subject: [PATCH 02/16] Create SinglyLinkedList --- DataStructures/Lists/SinglyLinkedList | 101 ++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 DataStructures/Lists/SinglyLinkedList diff --git a/DataStructures/Lists/SinglyLinkedList b/DataStructures/Lists/SinglyLinkedList new file mode 100644 index 000000000000..0b51813037f4 --- /dev/null +++ b/DataStructures/Lists/SinglyLinkedList @@ -0,0 +1,101 @@ +package LinkedList; +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); + } + } From 4842a4ca61d4721aa48af740e483569d3c304a07 Mon Sep 17 00:00:00 2001 From: Ritik2604 <49342895+Ritik2604@users.noreply.github.com> Date: Sat, 23 May 2020 05:32:30 +0530 Subject: [PATCH 03/16] Update and rename SinglyLinkedList to ListAddnFun --- DataStructures/Lists/{SinglyLinkedList => ListAddnFun} | 1 - 1 file changed, 1 deletion(-) rename DataStructures/Lists/{SinglyLinkedList => ListAddnFun} (99%) diff --git a/DataStructures/Lists/SinglyLinkedList b/DataStructures/Lists/ListAddnFun similarity index 99% rename from DataStructures/Lists/SinglyLinkedList rename to DataStructures/Lists/ListAddnFun index 0b51813037f4..47553e184358 100644 --- a/DataStructures/Lists/SinglyLinkedList +++ b/DataStructures/Lists/ListAddnFun @@ -1,4 +1,3 @@ -package LinkedList; import java.util.*; import java.lang.*; import java.io.*; From ef2be071b09617df3f4354920d99d17dc9561feb Mon Sep 17 00:00:00 2001 From: Ritik2604 <49342895+Ritik2604@users.noreply.github.com> Date: Sat, 23 May 2020 05:33:40 +0530 Subject: [PATCH 04/16] Create AVLSimple --- DataStructures/Trees/AVLSimple | 106 +++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 DataStructures/Trees/AVLSimple diff --git a/DataStructures/Trees/AVLSimple b/DataStructures/Trees/AVLSimple new file mode 100644 index 000000000000..1f2c2bd6aba2 --- /dev/null +++ b/DataStructures/Trees/AVLSimple @@ -0,0 +1,106 @@ +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; + + } + +} From 07aee8ba8129af1310b9e4fd08834214439f940c Mon Sep 17 00:00:00 2001 From: Ritik2604 <49342895+Ritik2604@users.noreply.github.com> Date: Sat, 23 May 2020 05:34:56 +0530 Subject: [PATCH 05/16] Create BoardPath --- DynamicProgramming/BoardPath | 52 ++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 DynamicProgramming/BoardPath diff --git a/DynamicProgramming/BoardPath b/DynamicProgramming/BoardPath new file mode 100644 index 000000000000..4b471a88ed64 --- /dev/null +++ b/DynamicProgramming/BoardPath @@ -0,0 +1,52 @@ + +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 Date: Sat, 23 May 2020 05:35:47 +0530 Subject: [PATCH 06/16] Create CountNumBinaryStrings --- DynamicProgramming/CountNumBinaryStrings | 69 ++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 DynamicProgramming/CountNumBinaryStrings diff --git a/DynamicProgramming/CountNumBinaryStrings b/DynamicProgramming/CountNumBinaryStrings new file mode 100644 index 000000000000..c71fbd63c682 --- /dev/null +++ b/DynamicProgramming/CountNumBinaryStrings @@ -0,0 +1,69 @@ +public class CountNumBinaryStr { + 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 numStrIS(int n) { + int[] zeros=new int[n]; + int []ones=new int[n]; + //seed + zeros[0]=1; + ones[0]=1; + for(int i=1;i Date: Sat, 23 May 2020 05:54:34 +0530 Subject: [PATCH 07/16] Delete LinkedList.java --- .../HashMap/Hashing/LinkedList.java | 63 ------------------- 1 file changed, 63 deletions(-) delete mode 100644 DataStructures/HashMap/Hashing/LinkedList.java diff --git a/DataStructures/HashMap/Hashing/LinkedList.java b/DataStructures/HashMap/Hashing/LinkedList.java deleted file mode 100644 index 4953551f7040..000000000000 --- 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 From 3a234f0914198b6477445426a565c99e3dacae34 Mon Sep 17 00:00:00 2001 From: Ritik2604 <49342895+Ritik2604@users.noreply.github.com> Date: Sat, 23 May 2020 05:56:23 +0530 Subject: [PATCH 08/16] Delete Node.java --- DataStructures/HashMap/Hashing/Node.java | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 DataStructures/HashMap/Hashing/Node.java diff --git a/DataStructures/HashMap/Hashing/Node.java b/DataStructures/HashMap/Hashing/Node.java deleted file mode 100644 index 74ab01f9d2a9..000000000000 --- 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 From 01d5814557dbe674886def8ceef86208c8994bbc Mon Sep 17 00:00:00 2001 From: Ritik2604 <49342895+Ritik2604@users.noreply.github.com> Date: Sat, 23 May 2020 05:57:55 +0530 Subject: [PATCH 09/16] Create Intersection --- DataStructures/HashMap/Hashing/Intersection | 37 +++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 DataStructures/HashMap/Hashing/Intersection diff --git a/DataStructures/HashMap/Hashing/Intersection b/DataStructures/HashMap/Hashing/Intersection new file mode 100644 index 000000000000..cfe1f74d73eb --- /dev/null +++ b/DataStructures/HashMap/Hashing/Intersection @@ -0,0 +1,37 @@ +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() { + + } + + + +} From 1ef46dbfd49245ba7c6b5adf29b914e3d4db8b2f Mon Sep 17 00:00:00 2001 From: Ritik2604 <49342895+Ritik2604@users.noreply.github.com> Date: Mon, 25 May 2020 02:50:24 +0530 Subject: [PATCH 10/16] Update GraphAlgos --- DataStructures/Graphs/GraphAlgos | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/DataStructures/Graphs/GraphAlgos b/DataStructures/Graphs/GraphAlgos index 780a73e45540..19938c48f042 100644 --- a/DataStructures/Graphs/GraphAlgos +++ b/DataStructures/Graphs/GraphAlgos @@ -1,3 +1,53 @@ +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; From f710f3aafac8be42219c1be4ed7dbfa03c878b4b Mon Sep 17 00:00:00 2001 From: Ritik2604 <49342895+Ritik2604@users.noreply.github.com> Date: Mon, 25 May 2020 03:02:15 +0530 Subject: [PATCH 11/16] Update ListAddnFun --- DataStructures/Lists/ListAddnFun | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/DataStructures/Lists/ListAddnFun b/DataStructures/Lists/ListAddnFun index 47553e184358..afbd2ae431d8 100644 --- a/DataStructures/Lists/ListAddnFun +++ b/DataStructures/Lists/ListAddnFun @@ -1,3 +1,44 @@ +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.*; From 82e2132557bbf295f2f1d3b03bc41092bd736f78 Mon Sep 17 00:00:00 2001 From: Ritik2604 <49342895+Ritik2604@users.noreply.github.com> Date: Mon, 25 May 2020 03:06:36 +0530 Subject: [PATCH 12/16] Update AVLSimple --- DataStructures/Trees/AVLSimple | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/DataStructures/Trees/AVLSimple b/DataStructures/Trees/AVLSimple index 1f2c2bd6aba2..ace0c46b9374 100644 --- a/DataStructures/Trees/AVLSimple +++ b/DataStructures/Trees/AVLSimple @@ -1,3 +1,34 @@ + +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; From 920852aa0e41015a05fe72976675b4c439560bab Mon Sep 17 00:00:00 2001 From: Ritik2604 <49342895+Ritik2604@users.noreply.github.com> Date: Mon, 25 May 2020 03:11:14 +0530 Subject: [PATCH 13/16] Update BoardPath --- DynamicProgramming/BoardPath | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/DynamicProgramming/BoardPath b/DynamicProgramming/BoardPath index 4b471a88ed64..91f1f011a6a0 100644 --- a/DynamicProgramming/BoardPath +++ b/DynamicProgramming/BoardPath @@ -1,4 +1,29 @@ +package DynamicProgramming.BoardPath; +/* +* 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; From 94cfab0cfc060685c2f899d876b15be9cd279757 Mon Sep 17 00:00:00 2001 From: Ritik2604 <49342895+Ritik2604@users.noreply.github.com> Date: Mon, 25 May 2020 03:12:16 +0530 Subject: [PATCH 14/16] Update BoardPath --- DynamicProgramming/BoardPath | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DynamicProgramming/BoardPath b/DynamicProgramming/BoardPath index 91f1f011a6a0..5ca14d1fab9b 100644 --- a/DynamicProgramming/BoardPath +++ b/DynamicProgramming/BoardPath @@ -1,4 +1,4 @@ -package DynamicProgramming.BoardPath; +package DynamicProgramming; /* * this is an important Algo in which * we have starting and ending of board and we have to reach From ec4f6a11102f9419eeaa36042fa265a95bf5e4e2 Mon Sep 17 00:00:00 2001 From: Ritik2604 <49342895+Ritik2604@users.noreply.github.com> Date: Mon, 25 May 2020 03:17:26 +0530 Subject: [PATCH 15/16] Update CountNumBinaryStrings --- DynamicProgramming/CountNumBinaryStrings | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/DynamicProgramming/CountNumBinaryStrings b/DynamicProgramming/CountNumBinaryStrings index c71fbd63c682..3dd0bf7ded9c 100644 --- a/DynamicProgramming/CountNumBinaryStrings +++ b/DynamicProgramming/CountNumBinaryStrings @@ -1,3 +1,31 @@ +package DynamicProgramming; +/* +* here is a important algo in this we have to count +* maximum no. of different binary strings which doesnot have +* consectuive 1s + + + +Test Case: + +int n=30; + + startAlgo(); + System.out.println(numStrIS(n)); + System.out.println(endAlgo()+"ms"); + + startAlgo(); + CountNumBinaryStr out=new CountNumBinaryStr(); + System.out.println(out.numStrR(n).ans); + System.out.println(endAlgo()+"ms"); + + startAlgo(); + System.out.println(countStrings(n,0)); + System.out.println(endAlgo()+"ms"); + + + +*/ public class CountNumBinaryStr { public static long startTime; public static long endTime; From a23a17ba6540818b266ad56a2fb181512dd4f1b5 Mon Sep 17 00:00:00 2001 From: Ritik2604 <49342895+Ritik2604@users.noreply.github.com> Date: Mon, 25 May 2020 03:22:27 +0530 Subject: [PATCH 16/16] Update Intersection --- DataStructures/HashMap/Hashing/Intersection | 27 +++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/DataStructures/HashMap/Hashing/Intersection b/DataStructures/HashMap/Hashing/Intersection index cfe1f74d73eb..8b54eeae1a95 100644 --- a/DataStructures/HashMap/Hashing/Intersection +++ b/DataStructures/HashMap/Hashing/Intersection @@ -1,3 +1,30 @@ +package DataStructures.HashMap.Hashing; +/* +* this is algo which implies common mathematical set theory concept +* called intersection in which result is common values of both the sets +* here metaphor of sets is HashMap + + +Test Case: + Scanner scn=new Scanner(System.in); + int len =scn.nextInt(); + int arr[]=new int[len]; + int arr2[]=new int[len]; + + for(int i=0;i<2*len;i++) { + + if(i=len) { + arr2[i-len]=scn.nextInt(); + } + } + System.out.println(Main(arr,arr2)); + + + +*/ + import java.util.ArrayList; import java.util.HashMap; import java.util.Map;