Loading…
DAA · Greedy · Program 17 · In the record
Write a program in c to construct an optimal prefix code using Huffman coding (greedy method) and find its execution time using the time function.
Algorithm: Huffman(C, f, n) 1. Start 2. Q <- min-priority queue of all n characters, keyed on frequency f 3. For i <- 1 to (n-1) do 4. allocate a new node z 5. x <- ExtractMin(Q) 6. y <- ExtractMin(Q) 7. left[z] <- x 8. right[z] <- y 9. f[z] <- f[x] + f[y] 10. Insert(Q, z) 11. root <- ExtractMin(Q) 12. PrintCodes(root, "") 13. Stop Algorithm: PrintCodes(z, code) 1. If z is a leaf then 2. print character of z and code 3. Return 4. PrintCodes(left[z], code + "0") 5. PrintCodes(right[z], code + "1")
1#include<stdio.h>2#include<time.h>3 4char ch[100];5int f[100],lft[100],rgt[100];6int Q[100],qsize=0,comp=0;7int code[100];8 9void heapify(int i) {10 int smallest=i,l=2*i+1,r=2*i+2;11 if(l<qsize) {12 comp++;13 if(f[Q[l]]<f[Q[smallest]])14 smallest=l;15 }16 if(r<qsize) {17 comp++;18 if(f[Q[r]]<f[Q[smallest]])19 smallest=r;20 }21 if(smallest!=i) {22 int t=Q[i];23 Q[i]=Q[smallest];24 Q[smallest]=t;25 heapify(smallest);26 }27}28 29int extractMin() {30 int min=Q[0];31 Q[0]=Q[qsize-1];32 qsize--;33 heapify(0);34 return min;35}36 37void insert(int z) {38 int i=qsize;39 Q[qsize++]=z;40 while(i>0) {41 int p=(i-1)/2;42 comp++;43 if(f[Q[i]]>=f[Q[p]])44 break;45 int t=Q[i];46 Q[i]=Q[p];47 Q[p]=t;48 i=p;49 }50}51 52void printCodes(int z,int depth) {53 int i;54 if(lft[z]==-1 && rgt[z]==-1) {55 printf("\n%c\t%d\t",ch[z],f[z]);56 if(depth==0)57 printf("0");58 for(i=0;i<depth;i++)59 printf("%d",code[i]);60 return;61 }62 code[depth]=0;63 printCodes(lft[z],depth+1);64 code[depth]=1;65 printCodes(rgt[z],depth+1);66}67 68int main() {69 int n,i;70 printf("Enter the number of characters:");71 scanf("%d",&n);72 printf("Enter the %d characters:",n);73 for(i=0;i<n;i++)74 scanf(" %c",&ch[i]);75 printf("Enter the frequencies of %d characters:",n);76 for(i=0;i<n;i++) {77 scanf("%d",&f[i]);78 lft[i]=rgt[i]=-1;79 }80 81 clock_t start=clock();82 for(i=0;i<n;i++)83 insert(i);84 int next=n;85 for(i=1;i<=n-1;i++) {86 int z=next++;87 int x=extractMin();88 int y=extractMin();89 lft[z]=x;90 rgt[z]=y;91 f[z]=f[x]+f[y];92 insert(z);93 }94 int root=extractMin();95 clock_t end=clock();96 double time_taken=(double)(end-start)/CLOCKS_PER_SEC;97 98 printf("\nHuffman codes:\nChar\tFreq\tCode");99 printCodes(root,0);100 printf("\nTotal frequency at root:%d",f[root]);101 printf("\nNumber of comparisons:%d",comp);102 printf("\nTime taken:%f seconds",time_taken);103 printf("\nTime complexity: O(n log n)");104 printf("\nSpace complexity: O(n)");105 printf("\n");106 return 0;107}6 ⏎ a b c d e f ⏎ 45 13 12 16 9 5Enter the number of characters:6 Enter the 6 characters:a b c d e f Enter the frequencies of 6 characters:45 13 12 16 9 5 Huffman codes: Char Freq Code a 45 0 c 12 100 b 13 101 f 5 1100 e 9 1101 d 16 111 Total frequency at root:100 Number of comparisons:27 Time taken:0.000005 seconds Time complexity: O(n log n) Space complexity: O(n)
Run program 17 in your browser
C · clang → WebAssembly. Change the code or the input, run it, break it, and see what happens.