1 / 39

ACM notes

ACM notes. Why… ? Not always an easy question!. Entropy problem -- Huffman Codes. bits used in ASCII. Input. Output. A..AB..BC..CD..DE..EF. 784 224 3.5. 1. 9. 40. 20. 16. 12. compression ratio, to 1 place of precision. bits used in an optimal “prefix-free” encoding. 98 chars.

nishan
Download Presentation

ACM notes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ACM notes • Why… ? Not always an easy question! Entropy problem -- Huffman Codes bits used in ASCII Input Output A..AB..BC..CD..DE..EF 784 224 3.5 1 9 40 20 16 12 compression ratio, to 1 place of precision bits used in an optimal “prefix-free” encoding 98 chars

  2. Prefix-free Codes via binary trees A..AB..BC..CD..DE..EF 1 9 40 20 16 12 ABCDEF 98 AE 49 BCDF 49 BF CD 21 E A 28 40 9 C D F B 12 20 16 1 49*2 + 49*3 = 245

  3. Prefix-free Codes via binary trees A..AB..BC..CD..DE..EF 1 9 40 20 16 12 ABCDEF 98 0 1 AE 49 BCDF 49 1 1 0 0 BF CD 21 E A 28 40 9 1 0 1 0 01 00 C D F B 12 20 16 1 111 110 101 100 codewords are read down the paths 49*2 + 49*3 = 245

  4. Huffman Codes Building the tree from the bottom up… A..AB..BC..CD..DE..EF 1 9 40 20 16 12 EF 10 1 0 A..AB..BC..CD..DE..F F E 10 40 20 16 12 9 1

  5. Huffman Codes Building the tree from the bottom up… A..AB..BC..CD..DE..F 10 40 20 16 12 DEF 22 0 D EF 10 12 1 A..AD..E..FB..BC..C 0 40 22 20 16 F E 9 1

  6. Huffman Codes Building the tree from the bottom up… A..AD..E..FB..BC..C 40 22 20 16 BC DEF 36 22 1 0 1 0 D EF C 10 B 12 20 16 1 A..AB..CD..E..F 0 40 36 22 F E 9 1

  7. Huffman Codes Building the tree from the bottom up… A..AB..CD..E..F 40 36 22 A 40 BCDEF 58 1 0 BC DEF 36 22 1 0 1 0 D EF C 10 B 12 20 16 1 0 A..AB..C..D..E..F F E 40 58 9 1

  8. Huffman Codes Building the tree from the bottom up… A..AB..C..D..E..F 40 58 ABCDEF 98 1 0 A 40 BCDEF 58 1 0 BC DEF 36 22 1 0 1 0 D EF C 10 B 12 20 16 1 0 A..B..C..D..E..F 58 F E 9 1

  9. Huffman Codes Total number of bits needed... 40*1 + 36*3 + 12*3 + 10*4 = 224 ABCDEF 98 1 0 A 40 BCDEF 58 1 0 BC DEF 36 22 1 0 1 0 D EF C 10 B 12 20 16 1 0 F E 9 1

  10. Graphs [A,E], [A,B], [A,C], [B,D], [B,E], [C,B], [D,A], [D,C], [E,D] B Adjacency List Representation A (vector or array of pairs) C D E

  11. 0 8 13 - 1 - 0 - 6 12 - 9 0 - - 7 - 0 0 - - - - 11 0 Graphs [A,E], [A,B], [A,C], [B,D], [B,E], [C,B], [D,A], [D,C], [E,D] B Adjacency List Representation 8 9 A (vector or array of pairs ?) 13 C 6 0 7 1 TO 12 D A B C D E E A 11 B C FROM D Matrix representation E (2d array)

  12. Problems Roman Forts (forts.cc) J C A D I F G B E K H fortify the most vulnerable...

  13. Problems Single Points of Failure (spf.cc) 1 2 5 4 3 1 3 2 3 4 3 5 0 Network #1 SPF node 3 leaves 2 subnets Output 1 Input 1 Graph 1 Graph 2 Output 2 Network #2 No SPF nodes

  14. 0 0 8 8 13 13 - - 1 1 - - 0 0 - - 6 6 12 12 - - 9 9 0 0 - - - - 7 7 - - 0 0 0 0 - - - - - - - - 11 11 0 0 All-pairs shortest paths... “Floyd-Warshall algorithm” A k B dij = C D E 0 D0 = (dij ) A A 0 8 13 - 1 B B - 0 - 6 12 C C - 9 0 - - D D 7 15 0 0 8 E E - - - 11 0 1 D1 = (dij ) k dij = shortest distance from i to j through {1, …, k}

  15. 0 0 8 8 13 13 - - 1 1 - - 0 0 - - 6 6 12 12 - - 9 9 0 0 - - - - 7 7 - - 0 0 0 0 - - - - - - - - 11 11 0 0 All-pairs shortest paths... “Floyd-Warshall algorithm” A k B dij = C D E 0 D0 = (dij ) A A 0 8 13 - 1 B B - 0 - 6 12 C C - 9 0 - - D D 7 15 0 0 8 E E - - - 11 0 1 D1 = (dij ) k dij = shortest distance from i to j through {1, …, k}

  16. Geometric Problems [A,E], [A,B], [A,C], [B,D], [B,E], [C,B], [D,A], [D,C], [E,D] B Adjacency List Representation A (vector or array of pairs) C D code resources to keep in mind E

  17. All-pairs shortest paths... A 0 8 13 14 1 A 0 8 13 14 1 B 13 0 6 6 12 B - 0 - 6 12 C 22 9 0 15 21 C - 9 0 15 21 D 7 9 0 0 8 D 7 15 0 0 8 E 18 20 11 11 0 E - - - 11 0 4 D4 = (dij ) 2 D2 = (dij ) A 0 8 12 12 1 A 0 8 13 14 1 B 13 0 6 6 12 B - 0 - 6 12 C 22 9 0 15 21 C - 9 0 15 21 D 7 9 0 0 8 D 7 9 0 0 8 E 18 20 11 11 0 E - - - 11 0 5 3 D5 = (dij ) D3 = (dij ) to store the path, another matrix can track the last intermediate vertex

  18. Graphs Representations bits used in ASCII Input Output AAAAABCD 64 13 4.9 compression ratio, to 1 place of precision bits used in an optimal “prefix-free” encoding

  19. Graphs Representations bits used in ASCII Input Output AAAAABCD 64 13 4.9 compression ratio, to 1 place of precision bits used in an optimal “prefix-free” encoding

  20. Problems Entropy bits used in ASCII Input Output AAAAABCD 64 13 4.9 compression ratio, to 1 place of precision bits used in an optimal “prefix-free” encoding A 0 B 10 C 110 D 111

  21. Problems N-Credible Mazes dimensions Input Output 2 0 0 2 2 0 0 0 1 0 1 0 2 0 2 1 2 0 2 0 3 1 2 2 2 -1 start end Maze #1 can be travelled (or not…) edge start edge end

  22. C++ STL www.dinkumware.com/htm_cpl/index.html www.sgi.com/tech/stl/ set<int> s; // basically a bin. tree s.size(); // returns an int s.insert(14); // adds 14 s.insert(-9); // adds -9 s.insert(42); // adds 42 set<int>::iterator i; // may want to typedef // think of an iterator as a pointer i = s.find(42); // return 42’s iterator cout << (*i) << endl; // prints 42 cout << (*--i)); // prints ... i = s.find(43); // not there ! // at this point ( i == s.end() ) is true s.erase(-9); // removing elements multiset<int> m; // holds multiple copies set #include <set> multiset #include <set>

  23. Breadth-first search • algorithm data structures queue, deque, hashtable (map)

  24. C++ STL www.dinkumware.com/htm_cpl/index.html www.sgi.com/tech/stl/ vector<int> v; // basically an int array v.reserve(10); // assure 10 spots v.push_back(42); // adds 42 to the end v.back(); // returns 42 v.pop_back(); // removes 42 v.size(); // # of elements v[i]; // ith element sort( v.begin(), v.end() ); // default sort sort( v.begin(), v.end(), mycompare ); deque<int> d; // double-ended queue d.push_front(42); // add to front d.front(42); // return front element d.pop_front(42); // remove from front vector #include <vector> sort #include <algorithm> last time deque #include <deque>

  25. Other problems • Change counting input: output: 1.00 0.06 0 There are 292 ways to make $1.00 There are 2 ways to make $0.06 • Sigma series Shortest sequences from 1 to N such that each element is the sum of two previous elements. input: output: 3 4 87 99 -1 1 2 3 1 2 4 1 2 4 8 16 24 28 29 58 87 1 2 4 8 16 32 33 66 99

  26. Useful C functions int atoi(char* s); double atof(char* s); int strcasecomp(char* s1, char* s2); long strtol(char* s, NULL, int base) converts C strings to ints atoi(“100”) == 100 converts C strings to doubles atoi(“100.0”) == 100.0 case-insensitive C string comparison strcasecmp(“aCm”,“ACm”) == 0 arbitrary conversion from a string in bases (2-36) to a long int strtol(“Charlie”, NULL, 36)== 2147483647L use man for more...

  27. sprintf int sprintf(char* str, char* format, ...); prints anything to the string str char str[100]; sprintf(str,“%d”,42); // str is “42” sprintf(str,“%f”,42.0); // str is “42.0” sprintf(str,“%10d”,42); // str is “ 42” sprintf(str,“%-10d”,42); // str is “42 ” flexible formatting: right/left justify:

  28. Two ACM programming skills A chance to “improve” your C/C++ … Preparation for the ACM competition ... Problem Insight and Execution ... 2 1 Anxiety! Get into the minds of the judges

  29. Get into the minds of the judges Key Skill #1: mindreading 100% 0% “What cases should I handle?” spectrum

  30. Key Skill #2: anxiety Anxiety!

  31. Dynamic Programming Strategy: create a table of partial results & build on it. divis.cc T(n) = number of steps yet to go T(n) = T(3n+1) + 1 if n odd T(n) = T(n/2) + 1 if n even

  32. Dynamic Programming Keys: create a table of partial results, articulate what each table cell means, then build it up... divis.cc j = items considered so far Table T 0 1 2 3 4 5 6 1 1 6 2 -3 0 the list 1 4 i = possible remainder the divisor 2 3 T[i][j] is 1 if i is a possible remainder using the first j items in the list.

  33. Dynamic programs can be short #include <cstdio> #include <iostream> #include <vector> vector<int> v(10000); vector<bool> m(100); // old mods vector<bool> m2(100); // new mods int n, k; bool divisible() { fill(m.begin(),m.end(),false); m[0] = true; for (int i=0; i<n; i++) { /* not giving away all of the code */ /* here the table is built (6 lines) */ } return m[0]; } int main() { cin >> n; // garbage while (cin >> n) { cin >> k; for (int i=0; i<n; i++) { cin >> v[i]; v[i] = abs(v[i]); v[i] %= k; } cout << (divisible() ? "D" : "Not d") << "ivisible\n"; } cout << endl; } acknowledgment: Matt Brubeck STL: http://www.sgi.com/Technology/STL

  34. General ACM Programming Try brute force first (or at least consider it) -- sometimes it will work fine… -- sometimes it will take a _bit_ too long -- sometimes it will take _way_ too long Best bugs from last week: getting the input in the “pea” problem: filling in the table in the “divis” problem: for (int j=1 ; j<N ; ++j) { cin >> Array[i]; } Table[i + n % k] = 1; Table[i - n % k] = 1;

  35. New Problem Word Chains hertz jazz hajj zeroth doze aplomb ceded dozen envy ballistic yearn Input A list of words Output yes or no -- can these words be chained together such that the last letter of one is the first letter of the next… ?

  36. Knapsack Problem objectwt.val. 1 3 8 2 2 5 3 1 1 4 2 5 Maximize loot w/ weight limit of 4. w Weight available for use Number of objects considered 0 1 2 3 4 1 V(n,w) = max value stealable w/ ‘n’ objects & ‘w’ weight 2 3 4 n V(n,w) =

  37. C Output printf, fprintf, sprintf(char* s, const char* format, …) the destination the format string the values possible format strings h % -#0 12 .4 d d decimal integers u unsigned (decimal) ints o octal integers x hexadecimal integers f doubles (floats are cast) e doubles (exp. notation) g f or e, if exp < -3 or -4 c character s string n outputs # of chars written !! % two of these print a ‘%’ type size modifier minimum field width precision allowed size modifiers flags - left-justify 0 pad w/ zeros + use sign (+ or -) (space) use sign ( or -) # deviant operation h short l long (lowercase L) L long double start character

  38. C Output value = 42 value = -42 %10.4d 0042 -0042 %-#12x 0x2a 0xffffffd6 value = 42 value = -42.419 %+10.4g +42 -42.42 %- 10.4g 42 -42.42 %-#10.4g 42.00 -42.42 value = “forty-two” %10.5s forty

  39. Change const int p=1,n=5,d=10,q=25,h=50; int counter=0; int pn, nn, dn, qn, hn; for (hn = 0; hn*h <= num; hn++) for (qn = 0; hn*h + qn*q <= num; qn++) for (dn = 0; hn*h + qn*q + dn*d <= num; dn++) for (nn = 0; hn*h + qn*q + dn*d + nn*n <= num; nn++) for (pn = 0; hn*h + qn*q + dn*d + nn*n + pn*p <= num; pn++) { if (hn*h + qn*q + dn*d + nn*n + pn*p == num) counter++; } Brute Force Dynamic Programming total 5¢ 9¢ 0¢ 1¢ 6¢ 7¢ 10¢ 11¢ 2¢ 3¢ 8¢ 12¢ 4¢ using 1¢ 1 1 1 1 1 1 1 1 1 1 1 1 1 1¢, 5¢ 1 2 1 1 2 2 1 2 3 1 2 3 3 1 2 1 1 2 2 1 2 4 1¢, 5¢, 10¢ 1 2 4 4

More Related