1 / 20

CS216: Program and Data Representation University of Virginia Computer Science

CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans. Lecture 26. http://www.cs.virginia.edu/cs216. Bad News/Good News. Bad News 54 people listed review as their most-preferred class, but only 1 person sent in any review questions!

flovejoy
Télécharger la présentation

CS216: Program and Data Representation University of Virginia Computer Science

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. CS216: Program and Data Representation University of Virginia Computer Science Spring 2006David Evans Lecture 26 http://www.cs.virginia.edu/cs216

  2. Bad News/Good News • Bad News • 54 people listed review as their most-preferred class, but only 1 person sent in any review questions! • Good News • This means we have plenty of time to finish the randomized graph algorithm!

  3. Minimum Cut Problem • Input: an undirected, connected multigraph G = (V,E) • Output: A cut (V1,V2 where V1∩V2 = V and V1 V2 = ) such that number of edges between V1 and V2 is the fewest possible. Why might this be useful? Equivalent: fewest edges that can be removed to disconnect G.

  4. Minimum Cut C A B D Size of the min cut must be no larger than the smallest node degree in graph

  5. Internet Minimum Cut June 1999 Internet graph, Bill Cheswick http://research.lumeta.com/ches/map/gallery/index.html

  6. Randomized Algorithm • While |V| > 2: • Pick a random edge (x, y) from E • Contract the edge: • Keep multi-edges, remove self-loops • Combine nodes • The two remaining nodes represent reasonable choices for the minimum cut sets

  7. Analysis • Suppose Cis a minimum cut (set of edges that disconnects G) • When we contract edge e: • Unlikely that e  C • So, C is likely to be preserved What is the probability a randomly choosen edge is in C?

  8. Analysis • Is the final result a cut of G? • What is the probability we find a minimum cut?

  9. Random Edge in C? • |C| must be  degree of every node in G • How many edges in G: |E| = sum of all node degrees / 2  n |C| / 2 Probability a random edge is in C  2/n

  10. Iteration • How many iterations? • Probability for first iteration: Prob(e1 C)  1 – 2/n • Probability for second iteration: Prob(e2 C | e1 C)  1 – 2/(n-1) • ... • Probability for last iteration: Prob(en-2 C)  1 – 2/(n-(n-2-1))  1 – 2/3 n - 2

  11. Probability of finding C?  (1 – 2/n) * (1 – 2/(n – 1)) * (1 – 2/(n – 2)) ... * (1 – 2/3) = (n – 2 / n) * (n – 3/(n – 1)) * (n – 4/(n – 2)) * ...* (2/4) * (1/3) = 2 / (n * (n – 1)) Probability of not finding C = 1 – 2/(n*(n-1))

  12. Is this good enough? Probability of not finding C on one trial: 1 – 2/(n*(n-1))  1 – 2/n2 Probability of not finding C on k trials:  [1 – 2/n2]k If k = cn2, Prob failure  (1/e)c Recall: lim (1 – 1/x)x = 1/e x 

  13. Review Questions • Explain problem 7 from problem set 2 • Explain problem 7 from problem set 4, specifically why since "depth < n" it is not considered in the running time. 

  14. PS2, Problem 7 Consider this code excerpt that prints out all possible partitions of the list s: for p1, p2 in allPossiblePartitions (s): print p1, p2 Use n to represent the number of elements in s. You may assume print is O(1). • What is its asymptotic running time? • What is its memory usage?

  15. def allPossiblePartitions (items): if len(items) == 1: yield [items[0]], [] yield [], [items[0]] else: for left, right in allPossiblePartitions (items[1:]): lplus = left[:] lplus.insert (0, items[0]) yield lplus, right rplus = right[:] rplus.insert (0, items[0]) yield left, rplus

  16. PS 4, Question 7 What is the asymptotic running time of our htree_encodeChars procedure? You may assume the input string is long enough that the time taken to produce the Huffman encoding tree does not matter (so you do not have to consider the running time of htree_buildTree and htree_unparse in your answer).

  17. htree_encodeChars void htree_encodeChars (char *s, FILE *outfile) { /* first, output the htree encoding */ htree h; h = htree_buildTree (s); fprintf (outfile, "%s\n", htree_unparse (h)); while (*s != '\0') { char c = *s++; char *bits = htree_encodeChar (h, c); fprintf (outfile, "%s", bits); } }

  18. htree_encodeChar char *htree_encodeChar (htree h, char c) { if (h == NULL) { return NULL; } else if (htree_isLeaf (h)) { if (h->letter == c) { ... /* elided for now */ } else { char *res = htree_encodeChar (h->left, c); if (res == NULL) { res = htree_encodeChar (h->right, c); } return res; } } How many calls to htree_encodeChar?

  19. htree_encodeChar } else if (htree_isLeaf (h)) { if (h->letter == c) { int depth = 0; char *res; htree ht = h; while (ht->parent != NULL) { depth++; ht = ht->parent; } res = (char *) malloc (sizeof (*res) * (depth + 1)); if (res == NULL) { ...; exit (EXIT_FAILURE); } res[depth] = '\0'; ht = h->parent; while (depth > 0) { if (ht->left == h) { res[depth - 1] = '0'; } else if (ht->right == h) { res[depth - 1] = '1'; } else { fprintf (stderr, "Error! Bad tree!"); ...; } h = ht; ht = ht->parent; depth--; } return res; } else { return NULL; }

  20. Charge • Final Exam out now • Return before 4:59pm on Monday, May 8 • Turn in to me at my office or folder in Brenda Perkins’ office (front of Olsson Hall)

More Related