From b74c8ae9a4083c71864fb442eafa065591a4d5e9 Mon Sep 17 00:00:00 2001 From: Mayank Kumar Jha Date: Tue, 29 Aug 2017 19:42:53 +0530 Subject: [PATCH 1/3] Add files via upload --- max_sub_array.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 max_sub_array.py diff --git a/max_sub_array.py b/max_sub_array.py new file mode 100644 index 00000000..653bb66e --- /dev/null +++ b/max_sub_array.py @@ -0,0 +1,59 @@ +""" +author : Mayank Kumar Jha (mk9440) +""" + +import time +import matplotlib.pyplot as plt +from random import randint +def find_max_sub_array(A,low,high): + if low==high: + return low,high,A[low] + else : + mid=(low+high)//2 + left_low,left_high,left_sum=find_max_sub_array(A,low,mid) + right_low,right_high,right_sum=find_max_sub_array(A,mid+1,high) + cross_left,cross_right,cross_sum=find_max_cross_sum(A,low,mid,high) + if left_sum>=right_sum and left_sum>=cross_sum: + return left_low,left_high,left_sum + elif right_sum>=left_sum and right_sum>=cross_sum : + return right_low,right_high,right_sum + else: + return cross_left,cross_right,cross_sum + +def find_max_cross_sum(A,low,mid,high): + left_sum,max_left=-999999999,-1 + right_sum,max_right=-999999999,-1 + summ=0 + for i in range(mid,low-1,-1): + summ+=A[i] + if summ > left_sum: + left_sum=summ + max_left=i + summ=0 + for i in range(mid+1,high+1): + summ+=A[i] + if summ > right_sum: + right_sum=summ + max_right=i + return max_left,max_right,(left_sum+right_sum) + + +if __name__=='__main__': + inputs=[10,100,1000,10000,50000,100000,200000,300000,400000,500000] + tim=[] + for i in inputs: + li=[randint(1,i) for j in range(i)] + strt=time.time() + (find_max_sub_array(li,0,len(li)-1)) + end=time.time() + tim.append(end-strt) + print("No of Inputs Time Taken") + for i in range(len(inputs)): + print(inputs[i],'\t\t',tim[i]) + plt.plot(inputs,tim) + plt.xlabel("Number of Inputs");plt.ylabel("Time taken in seconds ") + plt.show() + + + + From 86f5fd28c2ce79e73c3d66ea33b2fdbe93595b39 Mon Sep 17 00:00:00 2001 From: Mayank Kumar Jha Date: Tue, 29 Aug 2017 19:43:25 +0530 Subject: [PATCH 2/3] Delete max_sub_array.py --- max_sub_array.py | 59 ------------------------------------------------ 1 file changed, 59 deletions(-) delete mode 100644 max_sub_array.py diff --git a/max_sub_array.py b/max_sub_array.py deleted file mode 100644 index 653bb66e..00000000 --- a/max_sub_array.py +++ /dev/null @@ -1,59 +0,0 @@ -""" -author : Mayank Kumar Jha (mk9440) -""" - -import time -import matplotlib.pyplot as plt -from random import randint -def find_max_sub_array(A,low,high): - if low==high: - return low,high,A[low] - else : - mid=(low+high)//2 - left_low,left_high,left_sum=find_max_sub_array(A,low,mid) - right_low,right_high,right_sum=find_max_sub_array(A,mid+1,high) - cross_left,cross_right,cross_sum=find_max_cross_sum(A,low,mid,high) - if left_sum>=right_sum and left_sum>=cross_sum: - return left_low,left_high,left_sum - elif right_sum>=left_sum and right_sum>=cross_sum : - return right_low,right_high,right_sum - else: - return cross_left,cross_right,cross_sum - -def find_max_cross_sum(A,low,mid,high): - left_sum,max_left=-999999999,-1 - right_sum,max_right=-999999999,-1 - summ=0 - for i in range(mid,low-1,-1): - summ+=A[i] - if summ > left_sum: - left_sum=summ - max_left=i - summ=0 - for i in range(mid+1,high+1): - summ+=A[i] - if summ > right_sum: - right_sum=summ - max_right=i - return max_left,max_right,(left_sum+right_sum) - - -if __name__=='__main__': - inputs=[10,100,1000,10000,50000,100000,200000,300000,400000,500000] - tim=[] - for i in inputs: - li=[randint(1,i) for j in range(i)] - strt=time.time() - (find_max_sub_array(li,0,len(li)-1)) - end=time.time() - tim.append(end-strt) - print("No of Inputs Time Taken") - for i in range(len(inputs)): - print(inputs[i],'\t\t',tim[i]) - plt.plot(inputs,tim) - plt.xlabel("Number of Inputs");plt.ylabel("Time taken in seconds ") - plt.show() - - - - From ac24f7d6f4d97899794120b2f9035a5372f6246c Mon Sep 17 00:00:00 2001 From: Mayank Kumar Jha Date: Fri, 22 Sep 2017 01:21:56 +0530 Subject: [PATCH 3/3] Add files via upload Huffman Encoding with Proper comments --- Huffman.java | 158 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 Huffman.java diff --git a/Huffman.java b/Huffman.java new file mode 100644 index 00000000..b93b7e7c --- /dev/null +++ b/Huffman.java @@ -0,0 +1,158 @@ + +import java.util.Comparator; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Scanner; +import java.util.Stack; +/** + * + * @author Mayank Kumar (mk9440) + */ +/* +Output : + +Enter number of distinct letters +6 +Enter letters with its frequncy to encode +Enter letter : a +Enter frequncy : 45 + +Enter letter : b +Enter frequncy : 13 + +Enter letter : c +Enter frequncy : 12 + +Enter letter : d +Enter frequncy : 16 + +Enter letter : e +Enter frequncy : 9 + +Enter letter : f +Enter frequncy : 5 + +Letter Encoded Form +a 0 +b 1 0 1 +c 1 0 0 +d 1 1 1 +e 1 1 0 1 +f 1 1 0 0 + +*/ + +class Node{ +String letr=""; +int freq=0,data=0; +Node left=null,right=null; +} + +//A comparator class to sort list on the basis of their frequency +class comp implements Comparator{ + @Override + public int compare(Node o1, Node o2) { + if(o1.freq>o2.freq){return 1;} + else if(o1.freq it=li.iterator(); + while(it.hasNext()){Node n=it.next();System.out.print(n.freq+" ");}System.out.println(); + } + + //Function for making tree (Huffman Tree) + public static Node make_huffmann_tree(List li){ + //Sorting list in increasing order of its letter frequency + li.sort(new comp()); + Node temp=null; + Iterator it=li.iterator(); + //System.out.println(li.size()); + //Loop for making huffman tree till only single node remains in list + while(true){ + temp=new Node(); + //a and b are Node which are to be combine to make its parent + Node a=new Node(),b=new Node(); + a=null;b=null; + //checking if list is eligible for combining or not + //here first assignment of it.next in a will always be true as list till end will + //must have atleast one node + a=(Node)it.next(); + //Below condition is to check either list has 2nd node or not to combine + //If this condition will be false, then it means construction of huffman tree is completed + if(it.hasNext()){b=(Node)it.next();} + //Combining first two smallest nodes in list to make its parent whose frequncy + //will be equals to sum of frequency of these two nodes + if(b!=null){ + temp.freq=a.freq+b.freq;a.data=0;b.data=1;//assigining 0 and 1 to left and right nodes + temp.left=a;temp.right=b; + //after combing, removing first two nodes in list which are already combined + li.remove(0);//removes first element which is now combined -step1 + li.remove(0);//removes 2nd element which comes on 1st position after deleting first in step1 + li.add(temp);//adding new combined node to list + //print_list(li); //For visualizing each combination step + } + //Sorting after combining to again repeat above on sorted frequency list + li.sort(new comp()); + it=li.iterator();//resetting list pointer to first node (head/root of tree) + if(li.size()==1){return (Node)it.next();} //base condition ,returning root of huffman tree + } +} + + //Function for finding path between root and given letter ch + public static void dfs(Node n,String ch){ + Stack st=new Stack(); // stack for storing path + int freq=n.freq; // recording root freq to avoid it adding in path encoding + find_path_and_encode(st,n,ch,freq); + } + + //A simple utility function to print stack (Used for printing path) + public static void print_path(Stack st){ + for(int i=0;i st,Node root,String s,int f){ + //Base condition + if(root!= null){ + if(root.freq!=f){st.push(root);} // avoiding root to add in path/encoding bits + if(root.letr.equals(s)){print_path(st);return;} // Recursion stopping condition when path gets founded + find_path_and_encode(st,root.left,s,f); + find_path_and_encode(st,root.right,s,f); + //Popping if path not found in right or left of this node,because we previously + //pushed this node in taking a mindset that it might be in path + st.pop(); + } + } + + public static void main(String args[]){ + List li=new LinkedList<>(); + Scanner in=new Scanner(System.in); + System.out.println("Enter number of distinct letters "); + int n=in.nextInt(); + String s[]=new String[n]; + System.out.print("Enter letters with its frequncy to encode\n"); + for(int i=0;i