https://algorist.com//algowiki/api.php?action=feedcontributions&user=Algowikiadmin&feedformat=atom The Algorithm Design Manual Solution Wiki - User contributions [en] 2021-01-16T05:43:15Z User contributions MediaWiki 1.34.2 https://algorist.com//algowiki/index.php?title=Solution_Wiki,_The_Algorithm_Design_Manual,_3rd_Edition&diff=468 Solution Wiki, The Algorithm Design Manual, 3rd Edition 2020-11-09T16:39:54Z <p>Algowikiadmin: </p> <hr /> <div><br /> The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others.<br /> <br /> Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution.<br /> <br /> &lt;big&gt;Chapters&lt;/big&gt;<br /> <br /> #[[Chapter 1|Introduction to Algorithms]]<br /> #[[Chapter 2|Algorithm Analysis]]<br /> #[[Chapter 3|Data Structures]]<br /> #[[Chapter 4|Sorting]]<br /> #[[Chapter 5|Divide and Conquer]]<br /> #[[Chapter 6|Hashing and Randomized Algorithms]]<br /> #[[Chapter 7|Graph Traversal]]<br /> #[[Chapter 8|Weighted Graph Algorithms]]<br /> #[[Chapter 9|Combinatorial Search]]<br /> #[[Chapter 10|Dynamic Programming]]<br /> #[[Chapter 11|NP-Completeness]]<br /> #[[Chapter 12|Dealing with Hard Problems]]<br /> <br /> <br /> The second edition of the solution wiki can be found [https://www.algorist.com/algowiki_v2/index.php/The_Algorithms_Design_Manual_(Second_Edition) here].<br /> <br /> == Getting started ==<br /> ; Editing<br /> * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula]<br /> * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples]<br /> <br /> ; Configuration<br /> * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list]<br /> <br /> ; Mediawiki General<br /> * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ]<br /> * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]<br /> <br /> ; Questions<br /> *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems)<br /> * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic)</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Solution_Wiki,_The_Algorithm_Design_Manual,_3rd_Edition&diff=467 Solution Wiki, The Algorithm Design Manual, 3rd Edition 2020-10-28T18:14:32Z <p>Algowikiadmin: </p> <hr /> <div><br /> The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others.<br /> <br /> Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution.<br /> <br /> &lt;big&gt;Chapters&lt;/big&gt;<br /> <br /> #[[Chapter 1|Introduction to Algorithms]]<br /> #[[Chapter 2|Algorithm Analysis]]<br /> #[[Chapter 3|Data Structures]]<br /> #[[Chapter 4|Sorting]]<br /> #[[Chapter 5|Divide and Conquer]]<br /> #[[Chapter 6|Hashing and Randomized Algorithms]]<br /> #[[Chapter 7|Graph Traversal]]<br /> #[[Chapter 8|Weighted Graph Algorithms]]<br /> #[[Chapter 9|Combinatorial Search]]<br /> #[[Chapter 10|Dynamic Programming]]<br /> #[[Chapter 11|NP-Completeness]]<br /> #[[Chapter 12|Dealing with Hard Problems]]<br /> <br /> <br /> The second edition of the solution wiki can be found [[Second Edition|here]].<br /> <br /> == Getting started ==<br /> ; Editing<br /> * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula]<br /> * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples]<br /> <br /> ; Configuration<br /> * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list]<br /> <br /> ; Mediawiki General<br /> * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ]<br /> * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]<br /> <br /> ; Questions<br /> *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems)<br /> * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic)</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Solution_Wiki,_The_Algorithm_Design_Manual,_3rd_Edition&diff=466 Solution Wiki, The Algorithm Design Manual, 3rd Edition 2020-10-28T18:14:21Z <p>Algowikiadmin: </p> <hr /> <div><br /> The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others.<br /> <br /> Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution.<br /> <br /> &lt;big&gt;Chapters&lt;/big&gt;<br /> <br /> #[[Chapter 1|Introduction to Algorithms]]<br /> #[[Chapter 2|Algorithm Analysis]]<br /> #[[Chapter 3|Data Structures]]<br /> #[[Chapter 4|Sorting]]<br /> #[[Chapter 5|Divide and Conquer]]<br /> #[[Chapter 6|Hashing and Randomized Algorithms]]<br /> #[[Chapter 7|Graph Traversal]]<br /> #[[Chapter 8|Weighted Graph Algorithms]]<br /> #[[Chapter 9|Combinatorial Search]]<br /> #[[Chapter 10|Dynamic Programming]]<br /> #[[Chapter 11|NP-Completeness]]<br /> #[[Chapter 12|Dealing with Hard Problems]]<br /> <br /> <br /> The second edition of the solution wiki can be found [[Second Edition|here]].<br /> <br /> <br /> == Getting started ==<br /> ; Editing<br /> * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula]<br /> * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples]<br /> <br /> ; Configuration<br /> * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list]<br /> <br /> ; Mediawiki General<br /> * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ]<br /> * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]<br /> <br /> ; Questions<br /> *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems)<br /> * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic)</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Solution_Wiki,_The_Algorithm_Design_Manual,_3rd_Edition&diff=465 Solution Wiki, The Algorithm Design Manual, 3rd Edition 2020-10-28T18:09:13Z <p>Algowikiadmin: Created page with &quot; The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students...&quot;</p> <hr /> <div><br /> The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others.<br /> <br /> Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution.<br /> <br /> &lt;big&gt;Chapters&lt;/big&gt;<br /> <br /> #[[Chapter 1|Introduction to Algorithms]]<br /> #[[Chapter 2|Algorithm Analysis]]<br /> #[[Chapter 3|Data Structures]]<br /> #[[Chapter 4|Sorting]]<br /> #[[Chapter 5|Divide and Conquer]]<br /> #[[Chapter 6|Hashing and Randomized Algorithms]]<br /> #[[Chapter 7|Graph Traversal]]<br /> #[[Chapter 8|Weighted Graph Algorithms]]<br /> #[[Chapter 9|Combinatorial Search]]<br /> #[[Chapter 10|Dynamic Programming]]<br /> #[[Chapter 11|NP-Completeness]]<br /> #[[Chapter 12|Dealing with Hard Problems]]<br /> <br /> == Getting started ==<br /> ; Editing<br /> * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula]<br /> * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples]<br /> <br /> ; Configuration<br /> * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list]<br /> <br /> ; Mediawiki General<br /> * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ]<br /> * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]<br /> <br /> ; Questions<br /> *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems)<br /> * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic)</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Main_Page&diff=464 Main Page 2020-10-28T18:09:06Z <p>Algowikiadmin: Changed redirect target from Solution Wiki, TheAlgorithm Design Manual, 3rd Edition to Solution Wiki, The Algorithm Design Manual, 3rd Edition</p> <hr /> <div>#REDIRECT [[Solution Wiki, The Algorithm Design Manual, 3rd Edition]]<br /> <br /> The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others.<br /> <br /> Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution.<br /> <br /> &lt;big&gt;Chapters&lt;/big&gt;<br /> <br /> #[[Chapter 1|Introduction to Algorithms]]<br /> #[[Chapter 2|Algorithm Analysis]]<br /> #[[Chapter 3|Data Structures]]<br /> #[[Chapter 4|Sorting]]<br /> #[[Chapter 5|Divide and Conquer]]<br /> #[[Chapter 6|Hashing and Randomized Algorithms]]<br /> #[[Chapter 7|Graph Traversal]]<br /> #[[Chapter 8|Weighted Graph Algorithms]]<br /> #[[Chapter 9|Combinatorial Search]]<br /> #[[Chapter 10|Dynamic Programming]]<br /> #[[Chapter 11|NP-Completeness]]<br /> #[[Chapter 12|Dealing with Hard Problems]]<br /> <br /> == Getting started ==<br /> ; Editing<br /> * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula]<br /> * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples]<br /> <br /> ; Configuration<br /> * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list]<br /> <br /> ; Mediawiki General<br /> * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ]<br /> * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]<br /> <br /> ; Questions<br /> *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems)<br /> * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic)</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Main_Page&diff=462 Main Page 2020-10-26T14:37:22Z <p>Algowikiadmin: Changed redirect target from Solution Wiki, TheAlgorithm Design Manual, 3rd Edition&quot; to Solution Wiki, TheAlgorithm Design Manual, 3rd Edition</p> <hr /> <div>#REDIRECT [[Solution Wiki, TheAlgorithm Design Manual, 3rd Edition]]<br /> <br /> The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others.<br /> <br /> Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution.<br /> <br /> &lt;big&gt;Chapters&lt;/big&gt;<br /> <br /> #[[Chapter 1|Introduction to Algorithms]]<br /> #[[Chapter 2|Algorithm Analysis]]<br /> #[[Chapter 3|Data Structures]]<br /> #[[Chapter 4|Sorting]]<br /> #[[Chapter 5|Divide and Conquer]]<br /> #[[Chapter 6|Hashing and Randomized Algorithms]]<br /> #[[Chapter 7|Graph Traversal]]<br /> #[[Chapter 8|Weighted Graph Algorithms]]<br /> #[[Chapter 9|Combinatorial Search]]<br /> #[[Chapter 10|Dynamic Programming]]<br /> #[[Chapter 11|NP-Completeness]]<br /> #[[Chapter 12|Dealing with Hard Problems]]<br /> <br /> == Getting started ==<br /> ; Editing<br /> * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula]<br /> * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples]<br /> <br /> ; Configuration<br /> * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list]<br /> <br /> ; Mediawiki General<br /> * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ]<br /> * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]<br /> <br /> ; Questions<br /> *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems)<br /> * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic)</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Main_Page&diff=460 Main Page 2020-10-26T14:35:55Z <p>Algowikiadmin: Redirected page to Solution Wiki, TheAlgorithm Design Manual, 3rd Edition&quot;</p> <hr /> <div>#REDIRECT [[Solution Wiki, TheAlgorithm Design Manual, 3rd Edition&quot;]]<br /> <br /> The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others.<br /> <br /> Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution.<br /> <br /> &lt;big&gt;Chapters&lt;/big&gt;<br /> <br /> #[[Chapter 1|Introduction to Algorithms]]<br /> #[[Chapter 2|Algorithm Analysis]]<br /> #[[Chapter 3|Data Structures]]<br /> #[[Chapter 4|Sorting]]<br /> #[[Chapter 5|Divide and Conquer]]<br /> #[[Chapter 6|Hashing and Randomized Algorithms]]<br /> #[[Chapter 7|Graph Traversal]]<br /> #[[Chapter 8|Weighted Graph Algorithms]]<br /> #[[Chapter 9|Combinatorial Search]]<br /> #[[Chapter 10|Dynamic Programming]]<br /> #[[Chapter 11|NP-Completeness]]<br /> #[[Chapter 12|Dealing with Hard Problems]]<br /> <br /> == Getting started ==<br /> ; Editing<br /> * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula]<br /> * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples]<br /> <br /> ; Configuration<br /> * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list]<br /> <br /> ; Mediawiki General<br /> * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ]<br /> * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]<br /> <br /> ; Questions<br /> *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems)<br /> * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic)</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Chapter_2&diff=459 Chapter 2 2020-10-01T18:11:11Z <p>Algowikiadmin: Protected &quot;Chapter 2&quot; ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))</p> <hr /> <div>=Algorithm Analysis=<br /> <br /> ===Program Analysis===<br /> <br /> :[[2.1]]. What value is returned by the following function? Express your answer as a function of &lt;math&gt;n&lt;/math&gt;. Give the worst-case running time using the Big Oh notation.<br /> mystery(''n'')<br /> r:=0<br /> ''for'' i:=1 ''to'' n-1 ''do''<br /> ''for'' j:=i+1 ''to'' n ''do''<br /> ''for'' k:=1 ''to'' j ''do''<br /> r:=r+1<br /> ''return''(r)<br /> <br /> [[2.1|Solution]]<br /> <br /> <br /> :2.2. What value is returned by the following function? Express your answer as a function of &lt;math&gt;n&lt;/math&gt;. Give the worst-case running time using Big Oh notation.<br /> pesky(n)<br /> r:=0<br /> ''for'' i:=1 ''to'' n ''do''<br /> ''for'' j:=1 ''to'' i ''do''<br /> ''for'' k:=j ''to'' i+j ''do''<br /> r:=r+1<br /> ''return''(r)<br /> <br /> <br /> :[[2.3]]. What value is returned by the following function? Express your answer as a function of &lt;math&gt;n&lt;/math&gt;. Give the worst-case running time using Big Oh notation.<br /> prestiferous(n)<br /> r:=0<br /> ''for'' i:=1 ''to'' n ''do''<br /> ''for'' j:=1 ''to'' i ''do''<br /> ''for'' k:=j ''to'' i+j ''do''<br /> ''for'' l:=1 ''to'' i+j-k ''do''<br /> r:=r+1<br /> ''return''(r) <br /> <br /> [[2.3|Solution]]<br /> <br /> <br /> :2.4. What value is returned by the following function? Express your answer as a function of &lt;math&gt;n&lt;/math&gt;. Give the worst-case running time using Big Oh notation.<br /> conundrum(&lt;math&gt;n&lt;/math&gt;)<br /> &lt;math&gt;r:=0&lt;/math&gt;<br /> ''for'' &lt;math&gt;i:=1&lt;/math&gt; ''to'' &lt;math&gt;n&lt;/math&gt; ''do''<br /> ''for'' &lt;math&gt;j:=i+1&lt;/math&gt; ''to'' &lt;math&gt;n&lt;/math&gt; ''do''<br /> ''for'' &lt;math&gt;k:=i+j-1&lt;/math&gt; ''to'' &lt;math&gt;n&lt;/math&gt; ''do''<br /> &lt;math&gt;r:=r+1&lt;/math&gt;<br /> ''return''(r)<br /> <br /> <br /> :[[2.5]]. Consider the following algorithm: (the print operation prints a single asterisk; the operation &lt;math&gt;x = 2x&lt;/math&gt; doubles the value of the variable &lt;math&gt;x&lt;/math&gt;).<br /> ''for'' &lt;math&gt; k = 1&lt;/math&gt; to &lt;math&gt;n&lt;/math&gt;<br /> &lt;math&gt;x = k&lt;/math&gt;<br /> ''while'' (&lt;math&gt;x &lt; n&lt;/math&gt;):<br /> ''print'' '*'<br /> &lt;math&gt;x = 2x&lt;/math&gt;<br /> :Let &lt;math&gt;f(n)&lt;/math&gt; be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for &lt;math&gt; O(f(n))&lt;/math&gt;, and &lt;math&gt;\Theta(f(n))&lt;/math&gt;, ideally converging on &lt;math&gt;\Theta(f(n))&lt;/math&gt;.<br /> <br /> [[2.5|Solution]]<br /> <br /> <br /> :2.6. Suppose the following algorithm is used to evaluate the polynomial<br /> ::::::&lt;math&gt; p(x)=a_n x^n +a_{n-1} x^{n-1}+ \ldots + a_1 x +a_0&lt;/math&gt;<br /> &lt;math&gt;p:=a_0;&lt;/math&gt;<br /> &lt;math&gt;xpower:=1;&lt;/math&gt;<br /> for &lt;math&gt;i:=1&lt;/math&gt; to &lt;math&gt;n&lt;/math&gt; do<br /> &lt;math&gt;xpower:=x*xpower;&lt;/math&gt;<br /> &lt;math&gt;p:=p+a_i * xpower&lt;/math&gt;<br /> #How many multiplications are done in the worst-case? How many additions?<br /> #How many multiplications are done on the average?<br /> #Can you improve this algorithm?<br /> <br /> <br /> :2.7. Prove that the following algorithm for computing the maximum value in an array &lt;math&gt;A[1..n]&lt;/math&gt; is correct.<br /> max(A)<br /> &lt;math&gt;m:=A&lt;/math&gt;<br /> ''for'' &lt;math&gt;i:=2&lt;/math&gt; ''to'' n ''do''<br /> ''if'' &lt;math&gt;A[i] &gt; m&lt;/math&gt; ''then'' &lt;math&gt;m:=A[i]&lt;/math&gt;<br /> ''return'' (m)<br /> <br /> [[2.7|Solution]]<br /> <br /> ===Big Oh===<br /> <br /> <br /> :2.8. True or False?<br /> #Is &lt;math&gt;2^{n+1} = O (2^n)&lt;/math&gt;?<br /> #Is &lt;math&gt;2^{2n} = O(2^n)&lt;/math&gt;?<br /> <br /> <br /> :[[2.9]]. For each of the following pairs of functions, either &lt;math&gt;f(n )&lt;/math&gt; is in &lt;math&gt;O(g(n))&lt;/math&gt;, &lt;math&gt;f(n)&lt;/math&gt; is in &lt;math&gt;\Omega(g(n))&lt;/math&gt;, or &lt;math&gt;f(n)=\Theta(g(n))&lt;/math&gt;. Determine which relationship is correct and briefly explain why.<br /> #&lt;math&gt;f(n)=\log n^2&lt;/math&gt;; &lt;math&gt;g(n)=\log n&lt;/math&gt; + &lt;math&gt;5&lt;/math&gt;<br /> #&lt;math&gt;f(n)=\sqrt n&lt;/math&gt;; &lt;math&gt;g(n)=\log n^2&lt;/math&gt;<br /> #&lt;math&gt;f(n)=\log^2 n&lt;/math&gt;; &lt;math&gt;g(n)=\log n&lt;/math&gt;<br /> #&lt;math&gt;f(n)=n&lt;/math&gt;; &lt;math&gt;g(n)=\log^2 n&lt;/math&gt;<br /> #&lt;math&gt;f(n)=n \log n + n&lt;/math&gt;; &lt;math&gt;g(n)=\log n&lt;/math&gt;<br /> #&lt;math&gt;f(n)=10&lt;/math&gt;; &lt;math&gt;g(n)=\log 10&lt;/math&gt;<br /> #&lt;math&gt;f(n)=2^n&lt;/math&gt;; &lt;math&gt;g(n)=10 n^2&lt;/math&gt;<br /> #&lt;math&gt;f(n)=2^n&lt;/math&gt;; &lt;math&gt;g(n)=3^n&lt;/math&gt;<br /> <br /> [[2.9|Solution]]<br /> <br /> <br /> :2.10. For each of the following pairs of functions &lt;math&gt;f(n)&lt;/math&gt; and &lt;math&gt;g(n)&lt;/math&gt;, determine whether &lt;math&gt;f(n) = O(g(n))&lt;/math&gt;, &lt;math&gt;g(n) = O(f(n))&lt;/math&gt;, or both.<br /> #&lt;math&gt;f(n) = (n^2 - n)/2&lt;/math&gt;, &lt;math&gt;g(n) =6n&lt;/math&gt;<br /> #&lt;math&gt;f(n) = n +2 \sqrt n&lt;/math&gt;, &lt;math&gt;g(n) = n^2&lt;/math&gt;<br /> #&lt;math&gt;f(n) = n \log n&lt;/math&gt;, &lt;math&gt;g(n) = n \sqrt n /2&lt;/math&gt;<br /> #&lt;math&gt;f(n) = n + \log n&lt;/math&gt;, &lt;math&gt;g(n) = \sqrt n&lt;/math&gt;<br /> #&lt;math&gt;f(n) = 2(\log n)^2&lt;/math&gt;, &lt;math&gt;g(n) = \log n + 1&lt;/math&gt;<br /> #&lt;math&gt;f(n) = 4n\log n + n&lt;/math&gt;, &lt;math&gt;g(n) = (n^2 - n)/2&lt;/math&gt;<br /> <br /> <br /> :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for &lt;math&gt;f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))&lt;/math&gt;?<br /> #&lt;math&gt;f(n) = 3n^2, g(n) = n^2&lt;/math&gt;<br /> #&lt;math&gt;f(n) = 2n^4 - 3n^2 + 7, g(n) = n^5&lt;/math&gt;<br /> #&lt;math&gt;f(n) = log n, g(n) = log n + 1/n&lt;/math&gt;<br /> #&lt;math&gt;f(n) = 2^{klog n}, g(n) = n^k&lt;/math&gt;<br /> #&lt;math&gt;f(n) = 2^n, g(n) = 2^{2n}&lt;/math&gt;<br /> <br /> [[2.11|Solution]]<br /> <br /> <br /> :2.12. Prove that &lt;math&gt;n^3 - 3n^2-n+1 = \Theta(n^3)&lt;/math&gt;.<br /> <br /> <br /> :2.13. Prove that &lt;math&gt;n^2 = O(2^n)&lt;/math&gt;.<br /> <br /> [[2.13|Solution]]<br /> <br /> <br /> :2.14. Prove or disprove: &lt;math&gt;\Theta(n^2) = \Theta(n^2+1)&lt;/math&gt;.<br /> <br /> <br /> :[[2.15]]. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one?<br /> ::(a) &lt;math&gt;n^2&lt;/math&gt; (b) &lt;math&gt;n^3&lt;/math&gt; (c) &lt;math&gt;100n^2&lt;/math&gt; (d) &lt;math&gt;nlogn&lt;/math&gt; (e) &lt;math&gt;2^n&lt;/math&gt;<br /> <br /> [[2.15|Solution]]<br /> <br /> <br /> :2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size &lt;math&gt;n&lt;/math&gt;.)Suppose you have a computer that can perform &lt;math&gt;10^10&lt;/math&gt; operations per second. For each algorithm, what is the largest input size n that you can complete within an hour?<br /> ::(a) &lt;math&gt;n^2&lt;/math&gt; (b) &lt;math&gt;n^3&lt;/math&gt; (c) &lt;math&gt;100n^2&lt;/math&gt; (d) &lt;math&gt;nlogn&lt;/math&gt; (e) &lt;math&gt;2^n&lt;/math&gt; (f) &lt;math&gt;2^{2^n}&lt;/math&gt;<br /> <br /> <br /> :[[2.17]]. For each of the following pairs of functions &lt;math&gt;f(n)&lt;/math&gt; and &lt;math&gt;g(n)&lt;/math&gt;, give an appropriate positive constant &lt;math&gt;c&lt;/math&gt; such that &lt;math&gt;f(n) \leq c \cdot g(n)&lt;/math&gt; for all &lt;math&gt;n &gt; 1&lt;/math&gt;.<br /> #&lt;math&gt;f(n)=n^2+n+1&lt;/math&gt;, &lt;math&gt;g(n)=2n^3&lt;/math&gt;<br /> #&lt;math&gt;f(n)=n \sqrt n + n^2&lt;/math&gt;, &lt;math&gt;g(n)=n^2&lt;/math&gt;<br /> #&lt;math&gt;f(n)=n^2-n+1&lt;/math&gt;, &lt;math&gt;g(n)=n^2/2&lt;/math&gt;<br /> <br /> [[2.17|Solution]]<br /> <br /> <br /> :2.18. Prove that if &lt;math&gt;f_1(n)=O(g_1(n))&lt;/math&gt; and &lt;math&gt;f_2(n)=O(g_2(n))&lt;/math&gt;, then &lt;math&gt;f_1(n)+f_2(n) = O(g_1(n)+g_2(n))&lt;/math&gt;.<br /> <br /> <br /> :[[2.19]]. Prove that if &lt;math&gt;f_1(N)=\Omega(g_1(n))&lt;/math&gt; and &lt;math&gt;f_2(n)=\Omega(g_2(n) &lt;/math&gt;, then &lt;math&gt;f_1(n)+f_2(n)=\Omega(g_1(n)+g_2(n))&lt;/math&gt;.<br /> <br /> [[2.19|Solution]]<br /> <br /> <br /> :2.20. Prove that if &lt;math&gt;f_1(n)=O(g_1(n))&lt;/math&gt; and &lt;math&gt;f_2(n)=O(g_2(n))&lt;/math&gt;, then &lt;math&gt;f_1(n) \cdot f_2(n) = O(g_1(n) \cdot g_2(n))&lt;/math&gt;<br /> <br /> <br /> :[[2.21]]. Prove for all &lt;math&gt;k \geq 1&lt;/math&gt; and all sets of constants &lt;math&gt;\{a_k, a_{k-1}, \ldots, a_1,a_0\} \in R&lt;/math&gt;, &lt;math&gt; a_k n^k + a_{k-1}n^{k-1}+....+a_1 n + a_0 = O(n^k)&lt;/math&gt;<br /> <br /> [[2.21|Solution]]<br /> <br /> <br /> :2.22. Show that for any real constants &lt;math&gt;a&lt;/math&gt; and &lt;math&gt;b&lt;/math&gt;, &lt;math&gt;b &gt; 0&lt;/math&gt;<br /> &lt;center&gt;&lt;math&gt;(n + a)^b = \Omega (n^b)&lt;/math&gt;&lt;/center&gt;<br /> <br /> <br /> :[[2.23]]. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which.<br /> &lt;center&gt;<br /> &lt;math&gt;\begin{array}{llll}<br /> n &amp; 2^n &amp; n \lg n &amp; \ln n \\<br /> n-n^3+7n^5 &amp; \lg n &amp; \sqrt n &amp; e^n \\<br /> n^2+\lg n &amp; n^2 &amp; 2^{n-1} &amp; \lg \lg n \\<br /> n^3 &amp; (\lg n)^2 &amp; n! &amp; n^{1+\varepsilon} where 0&lt; \varepsilon &lt;1<br /> \\<br /> \end{array}&lt;/math&gt;<br /> &lt;/center&gt;<br /> <br /> [[2.23|Solution]]<br /> <br /> <br /> :2.24. List the functions below from lowest to highest order. If any two or more are of the same order, indicate which.<br /> &lt;center&gt;<br /> &lt;math&gt;\begin{array}{llll}<br /> n^{\pi} &amp; \pi^n &amp; \binom{n}{5} &amp; \sqrt{2\sqrt{n}} \\<br /> \binom{n}{n-4} &amp; 2^{log^4n} &amp; n^{5(logn)^2} &amp; n^4\binom{n}{n-4}<br /> \\<br /> \end{array}&lt;/math&gt;<br /> &lt;/center&gt;<br /> <br /> <br /> :[[2.25]]. List the functions below from lowest to highest order. If any two or more are of the same order, indicate which.<br /> &lt;center&gt;<br /> &lt;math&gt;\begin{array}{llll}<br /> \sum_{i=1}^n i^i &amp; n^n &amp; (log n)^{log n} &amp; 2^{(log n^2)}\\<br /> n! &amp; 2^{log^4n} &amp; n^{(log n)^2} &amp; n^4 \binom{n}{n-4}\\<br /> \end{array}&lt;/math&gt;<br /> &lt;/center&gt;<br /> <br /> <br /> [[2.25|Solution]]<br /> <br /> <br /> :2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which.<br /> &lt;center&gt;<br /> &lt;math&gt;\begin{array}{lll}<br /> \sqrt{n} &amp; n &amp; 2^n \\<br /> n \log n &amp; n - n^3 + 7n^5 &amp; n^2 + \log n \\<br /> n^2 &amp; n^3 &amp; \log n \\<br /> n^{\frac{1}{3}} + \log n &amp; (\log n)^2 &amp; n! \\<br /> \ln n &amp; \frac{n}{\log n} &amp; \log \log n \\<br /> ({1}/{3})^n &amp; ({3}/{2})^n &amp; 6 \\<br /> \end{array}&lt;/math&gt;<br /> &lt;/center&gt;<br /> <br /> <br /> :[[2.27]]. Find two functions &lt;math&gt;f(n)&lt;/math&gt; and &lt;math&gt;g(n)&lt;/math&gt; that satisfy the following relationship. If no such &lt;math&gt;f&lt;/math&gt; and &lt;math&gt;g&lt;/math&gt; exist, write ''None.''<br /> #&lt;math&gt;f(n)=o(g(n))&lt;/math&gt; and &lt;math&gt;f(n) \neq \Theta(g(n))&lt;/math&gt;<br /> #&lt;math&gt;f(n)=\Theta(g(n))&lt;/math&gt; and &lt;math&gt;f(n)=o(g(n))&lt;/math&gt;<br /> #&lt;math&gt;f(n)=\Theta(g(n))&lt;/math&gt; and &lt;math&gt;f(n) \neq O(g(n))&lt;/math&gt;<br /> #&lt;math&gt;f(n)=\Omega(g(n))&lt;/math&gt; and &lt;math&gt;f(n) \neq O(g(n))&lt;/math&gt;<br /> <br /> [[2.27|Solution]]<br /> <br /> <br /> :2.28. True or False?<br /> #&lt;math&gt;2n^2+1=O(n^2)&lt;/math&gt;<br /> #&lt;math&gt;\sqrt n= O(\log n)&lt;/math&gt;<br /> #&lt;math&gt;\log n = O(\sqrt n)&lt;/math&gt;<br /> #&lt;math&gt;n^2(1 + \sqrt n) = O(n^2 \log n)&lt;/math&gt;<br /> #&lt;math&gt;3n^2 + \sqrt n = O(n^2)&lt;/math&gt;<br /> #&lt;math&gt;\sqrt n \log n= O(n) &lt;/math&gt;<br /> #&lt;math&gt;\log n=O(n^{-1/2})&lt;/math&gt;<br /> <br /> <br /> :[[2.29]]. For each of the following pairs of functions &lt;math&gt;f(n)&lt;/math&gt; and &lt;math&gt;g(n)&lt;/math&gt;, state whether &lt;math&gt;f(n)=O(g(n))&lt;/math&gt;, &lt;math&gt;f(n)=\Omega(g(n))&lt;/math&gt;, &lt;math&gt;f(n)=\Theta(g(n))&lt;/math&gt;, or none of the above.<br /> #&lt;math&gt;f(n)=n^2+3n+4&lt;/math&gt;, &lt;math&gt;g(n)=6n+7&lt;/math&gt;<br /> #&lt;math&gt;f(n)=n \sqrt n&lt;/math&gt;, &lt;math&gt;g(n)=n^2-n&lt;/math&gt;<br /> #&lt;math&gt;f(n)=2^n - n^2&lt;/math&gt;, &lt;math&gt;g(n)=n^4+n^2&lt;/math&gt;<br /> <br /> [[2.29|Solution]]. <br /> <br /> <br /> :2.30. For each of these questions, briefly explain your answer.<br /> ::(a) If I prove that an algorithm takes &lt;math&gt;O(n^2)&lt;/math&gt; worst-case time, is it possible that it takes &lt;math&gt;O(n)&lt;/math&gt; on some inputs?<br /> ::(b) If I prove that an algorithm takes &lt;math&gt;O(n^2)&lt;/math&gt; worst-case time, is it possible that it takes &lt;math&gt;O(n)&lt;/math&gt; on all inputs?<br /> ::(c) If I prove that an algorithm takes &lt;math&gt;\Theta(n^2)&lt;/math&gt; worst-case time, is it possible that it takes &lt;math&gt;O(n)&lt;/math&gt; on some inputs?<br /> ::(d) If I prove that an algorithm takes &lt;math&gt;\Theta(n^2)&lt;/math&gt; worst-case time, is it possible that it takes &lt;math&gt;O(n)&lt;/math&gt; on all inputs?<br /> ::(e) Is the function &lt;math&gt;f(n) = \Theta(n^2)&lt;/math&gt;, where &lt;math&gt;f(n) = 100 n^2&lt;/math&gt; for even &lt;math&gt;n&lt;/math&gt; and &lt;math&gt;f(n) = 20 n^2 - n \log_2 n&lt;/math&gt; for odd &lt;math&gt;n&lt;/math&gt;?<br /> <br /> <br /> :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning.<br /> ::(a) Is &lt;math&gt;3^n = O(2^n)&lt;/math&gt;?<br /> ::(b) Is &lt;math&gt;\log 3^n = O( \log 2^n )&lt;/math&gt;?<br /> ::(c) Is &lt;math&gt;3^n = \Omega(2^n)&lt;/math&gt;?<br /> ::(d) Is &lt;math&gt;\log 3^n = \Omega( \log 2^n )&lt;/math&gt;?<br /> <br /> [[2.31|Solution]]<br /> <br /> <br /> :2.32. For each of the following expressions &lt;math&gt;f(n)&lt;/math&gt; find a simple &lt;math&gt;g(n)&lt;/math&gt; such that &lt;math&gt;f(n)=\Theta(g(n))&lt;/math&gt;.<br /> #&lt;math&gt;f(n)=\sum_{i=1}^n {1\over i}&lt;/math&gt;.<br /> #&lt;math&gt;f(n)=\sum_{i=1}^n \lceil {1\over i}\rceil&lt;/math&gt;.<br /> #&lt;math&gt;f(n)=\sum_{i=1}^n \log i&lt;/math&gt;.<br /> #&lt;math&gt;f(n)=\log (n!)&lt;/math&gt;.<br /> <br /> <br /> :[[2.33]]. Place the following functions into increasing asymptotic order.<br /> ::&lt;math&gt;f_1(n) = n^2\log_2n&lt;/math&gt;, &lt;math&gt;f_2(n) = n(\log_2n)^2&lt;/math&gt;, &lt;math&gt;f_3(n) = \sum_{i=0}^n 2^i&lt;/math&gt;, &lt;math&gt;f_4(n) = \log_2(\sum_{i=0}^n 2^i)&lt;/math&gt;.<br /> <br /> [[2.33|Solution]]<br /> <br /> <br /> :2.34. Which of the following are true?<br /> #&lt;math&gt;\sum_{i=1}^n 3^i = \Theta(3^{n-1})&lt;/math&gt;.<br /> #&lt;math&gt;\sum_{i=1}^n 3^i = \Theta(3^n)&lt;/math&gt;.<br /> #&lt;math&gt;\sum_{i=1}^n 3^i = \Theta(3^{n+1})&lt;/math&gt;.<br /> <br /> <br /> :[[2.35]]. For each of the following functions &lt;math&gt;f&lt;/math&gt; find a simple function &lt;math&gt;g&lt;/math&gt; such that &lt;math&gt;f(n)=\Theta(g(n))&lt;/math&gt;.<br /> #&lt;math&gt;f_1(n)= (1000)2^n + 4^n&lt;/math&gt;.<br /> #&lt;math&gt;f_2(n)= n + n\log n + \sqrt n&lt;/math&gt;.<br /> #&lt;math&gt;f_3(n)= \log (n^{20}) + (\log n)^{10}&lt;/math&gt;.<br /> #&lt;math&gt;f_4(n)= (0.99)^n + n^{100}.&lt;/math&gt;<br /> <br /> [[2.35|Solution]]<br /> <br /> <br /> :2.36. For each pair of expressions &lt;math&gt;(A,B)&lt;/math&gt; below, indicate whether &lt;math&gt;A&lt;/math&gt; is &lt;math&gt;O&lt;/math&gt;, &lt;math&gt;o&lt;/math&gt;, &lt;math&gt;\Omega&lt;/math&gt;, &lt;math&gt;\omega&lt;/math&gt;, or &lt;math&gt;\Theta&lt;/math&gt; of &lt;math&gt;B&lt;/math&gt;. Note that zero, one or more of these relations may hold for a given pair; list all correct ones.<br /> &lt;br&gt;&lt;center&gt;&lt;math&gt;<br /> \begin{array}{lcc}<br /> &amp; A &amp; B \\<br /> (a) &amp; n^{100} &amp; 2^n \\<br /> (b) &amp; (\lg n)^{12} &amp; \sqrt{n} \\<br /> (c) &amp; \sqrt{n} &amp; n^{\cos (\pi n/8)} \\<br /> (d) &amp; 10^n &amp; 100^n \\<br /> (e) &amp; n^{\lg n} &amp; (\lg n)^n \\<br /> (f) &amp; \lg{(n!)} &amp; n \lg n<br /> \end{array}<br /> &lt;/math&gt;&lt;/center&gt;<br /> <br /> ===Summations===<br /> <br /> <br /> :[[2.37]]. Find an expression for the sum of the &lt;math&gt;i&lt;/math&gt;th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0.<br /> &lt;center&gt;<br /> &lt;math&gt;\begin{array}{ccccccccc}<br /> &amp;&amp;&amp;&amp;1&amp;&amp;&amp;&amp; \\<br /> &amp;&amp;&amp;1&amp;1&amp;1&amp;&amp;&amp;\\<br /> &amp;&amp;1&amp;2&amp;3&amp;2&amp;1&amp;&amp;\\<br /> &amp;1&amp;3&amp;6&amp;7&amp;6&amp;3&amp;1&amp;\\<br /> 1&amp;4&amp;10&amp;16&amp;19&amp;16&amp;10&amp;4&amp;1\\<br /> \end{array}&lt;/math&gt;<br /> &lt;/center&gt;<br /> <br /> [[2.37|Solution]]<br /> <br /> <br /> :2.38. Assume that Christmas has &lt;math&gt;n&lt;/math&gt; days. Exactly how many presents did my ''true love'' send me? (Do some research if you do not understand this question.)<br /> <br /> <br /> :[[2.39]]<br /> <br /> [[2.39|Solution]]<br /> <br /> <br /> :2.40. Consider the following code fragment.<br /> &lt;tt&gt;<br /> for i=1 to n do<br /> for j=i to 2*i do<br /> output ''foobar''<br /> &lt;/tt&gt;<br /> :Let &lt;math&gt;T(n)&lt;/math&gt; denote the number of times foobar' is printed as a function of &lt;math&gt;n&lt;/math&gt;.<br /> #Express &lt;math&gt;T(n)&lt;/math&gt; as a summation (actually two nested summations).<br /> #Simplify the summation. Show your work.<br /> <br /> <br /> :[[2.41]].Consider the following code fragment.<br /> &lt;tt&gt;<br /> for i=1 to n/2 do<br /> for j=i to n-i do<br /> for k=1 to j do<br /> output ''foobar''<br /> &lt;/tt&gt;<br /> :Assume &lt;math&gt;n&lt;/math&gt; is even. Let &lt;math&gt;T(n)&lt;/math&gt; denote the number of times foobar' is printed as a function of &lt;math&gt;n&lt;/math&gt;.<br /> #Express &lt;math&gt;T(n)&lt;/math&gt; as three nested summations.<br /> #Simplify the summation. Show your work.<br /> <br /> [[2.41|Solution]]<br /> <br /> <br /> :2.42. When you first learned to multiply numbers, you were told that &lt;math&gt;x \times y&lt;/math&gt; means adding &lt;math&gt;x&lt;/math&gt; a total of &lt;math&gt;y&lt;/math&gt; times, so &lt;math&gt;5 \times 4 = 5+5+5+5 = 20&lt;/math&gt;. What is the time complexity of multiplying two &lt;math&gt;n&lt;/math&gt;-digit numbers in base &lt;math&gt;b&lt;/math&gt; (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of &lt;math&gt;n&lt;/math&gt; and &lt;math&gt;b&lt;/math&gt;. Assume that single-digit by single-digit addition or multiplication takes &lt;math&gt;O(1)&lt;/math&gt; time. (Hint: how big can &lt;math&gt;y&lt;/math&gt; be as a function of &lt;math&gt;n&lt;/math&gt; and &lt;math&gt;b&lt;/math&gt;?)<br /> <br /> <br /> :[[2.43]]. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that &lt;math&gt;127 \times 211 = 127 \times 1 + 127 \times 10 + 127 \times 200 = 26,397&lt;/math&gt;. Analyze the time complexity of multiplying two &lt;math&gt;n&lt;/math&gt;-digit numbers with this method as a function of &lt;math&gt;n&lt;/math&gt; (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes &lt;math&gt;O(1)&lt;/math&gt; time.<br /> <br /> [[2.43|Solution]]<br /> <br /> ===Logartihms===<br /> <br /> <br /> :2.44. Prove the following identities on logarithms:<br /> #Prove that &lt;math&gt;\log_a (xy) = \log_a x + \log_a y&lt;/math&gt;<br /> #Prove that &lt;math&gt;\log_a x^y = y \log_a x&lt;/math&gt;<br /> #Prove that &lt;math&gt;\log_a x = \frac{\log_b x}{\log_b a}&lt;/math&gt;<br /> #Prove that &lt;math&gt;x^{\log_b y} = y^{\log_b x}&lt;/math&gt;<br /> <br /> <br /> :[[2.45]]. Show that &lt;math&gt;\lceil \lg(n+1) \rceil = \lfloor \lg n \rfloor +1&lt;/math&gt;<br /> <br /> [[2.45|Solution]]<br /> <br /> <br /> :2.46. Prove that that the binary representation of &lt;math&gt;n \geq 1&lt;/math&gt; has &lt;math&gt;\lfloor \lg_2 n \rfloor&lt;/math&gt; + &lt;math&gt;1&lt;/math&gt; bits.<br /> <br /> <br /> :[[2.47]]. In one of my research papers I give a comparison-based sorting algorithm that runs in &lt;math&gt;O( n \log (\sqrt n) )&lt;/math&gt;. Given the existence of an &lt;math&gt;\Omega(n \log n)&lt;/math&gt; lower bound for sorting, how can this be possible?<br /> <br /> <br /> [[2.47|Solution]]<br /> <br /> ===Interview Problems===<br /> <br /> <br /> :2.48. You are given a set &lt;math&gt;S&lt;/math&gt; of &lt;math&gt;n&lt;/math&gt; numbers. You must pick a subset &lt;math&gt;S'&lt;/math&gt; of &lt;math&gt;k&lt;/math&gt; numbers from &lt;math&gt;S&lt;/math&gt; such that the probability of each element of &lt;math&gt;S&lt;/math&gt; occurring in &lt;math&gt;S'&lt;/math&gt; is equal (i.e., each is selected with probability &lt;math&gt;k/n&lt;/math&gt;). You may make only one pass over the numbers. What if &lt;math&gt;n&lt;/math&gt; is unknown?<br /> <br /> <br /> :[[2.49]]. We have 1,000 data items to store on 1,000 nodes. Each node can store copies of exactly three different items. Propose a replication scheme to minimize data loss as nodes fail. What is the expected number of data entries that get lost when three random nodes fail?<br /> <br /> [[2.49|Solution]]<br /> <br /> <br /> :2.50. Consider the following algorithm to find the minimum element in an array of numbers &lt;math&gt;A[0, \ldots, n]&lt;/math&gt;. One extra variable &lt;math&gt;tmp&lt;/math&gt; is allocated to hold the current minimum value. Start from A; &quot;tmp&quot; is compared against &lt;math&gt;A&lt;/math&gt;, <br /> &lt;math&gt;A&lt;/math&gt;, &lt;math&gt;\ldots&lt;/math&gt;, &lt;math&gt;A[N]&lt;/math&gt; in order. When &lt;math&gt;A[i]&lt;tmp&lt;/math&gt;, &lt;math&gt;tmp = A[i]&lt;/math&gt;. What is the expected number of times that the assignment operation &lt;math&gt;tmp = A[i]&lt;/math&gt; is performed?<br /> <br /> <br /> :[[2.51]]. You are given ten bags of gold coins. Nine bags contain coins that each weigh 10 grams. One bag contains all false coins that weigh 1 gram less. You must identify this bag in just one weighing. You have a digital balance that reports the weight of what is placed on it.<br /> <br /> [[2.51|Solution]]<br /> <br /> <br /> :2.52. You have eight balls all of the same size. Seven of them weigh the same, and one of them weighs slightly more. How can you find the ball that is heavier by using a balance and only two weightings?<br /> <br /> <br /> :[[2.53]]. Suppose we start with &lt;math&gt;n&lt;/math&gt; companies that eventually merge into one big company. How many different ways are there for them to merge?<br /> <br /> [[2.53|Solution]]<br /> <br /> <br /> :2.54. Six pirates must divide $300 among themselves. The division is to proceed as follows. The senior pirate proposes a way to divide the money. Then the pirates vote. If the senior pirate gets at least half the votes he wins, and that division remains. If he doesn’t, he is killed and then the next senior-most pirate gets a chance to propose the division. Now tell what will happen and why (i.e. how many pirates survive and how the division is done)? All the pirates are intelligent and the first priority is to stay alive and the next priority is to get as much money as possible.<br /> <br /> <br /> :[[2.55]]. Reconsider the pirate problem above, where we start with only one indivisible dollar. Who gets the dollar, and how many are killed?<br /> <br /> [[2.55|Solution]]<br /> <br /> <br /> Back to [[Chapter List]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Chapter_12&diff=458 Chapter 12 2020-10-01T18:11:07Z <p>Algowikiadmin: Protected &quot;Chapter 12&quot; ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))</p> <hr /> <div>=Dealing with Hard Problems=<br /> <br /> ===Special Cases of Hard Problems===<br /> <br /> :[[12.1]]. Dominos are tiles represented by integer pairs &lt;math&gt;(x_i, y_i)&lt;/math&gt;, where each of the values &lt;math&gt;x_i&lt;/math&gt; and &lt;math&gt;y_i&lt;/math&gt; are integers between 1 and &lt;math&gt;n&lt;/math&gt;. Let &lt;math&gt;S&lt;/math&gt; be a sequence of m integer pairs &lt;math&gt;[(x_1, y_1),(x_2, y_2), ...,(x_m, y_m)]&lt;/math&gt;. The goal of the game is to create long chains &lt;math&gt;[(x_{i1}, y_{i1}),(x_{i2}, y_{i2}), ...,(x_{it}, y_{it})]&lt;/math&gt; such that &lt;math&gt;y_{ij} = x_{i(j+1)}&lt;/math&gt;. Dominos can be flipped, so &lt;math&gt;(x_i, y_i)&lt;/math&gt; equivalent to &lt;math&gt;(y_i, x_i)&lt;/math&gt;. For &lt;math&gt;S = [(1, 3),(4, 2),(3, 5),(2, 3),(3, 8)]&lt;/math&gt;, the longest domino sequences include &lt;math&gt;[(4, 2),(2, 3),(3, 8)]&lt;/math&gt; and &lt;math&gt;[(1, 3),(3, 2),(2, 4)]&lt;/math&gt;.<br /> ::(a) Prove that finding the longest domino chain is NP-complete.<br /> ::(b) Give an efficient algorithm to find the longest domino chain where the numbers increase along the chain. For S above, the longest such chains are &lt;math&gt;[(1, 3),(3, 5)]&lt;/math&gt; and &lt;math&gt;[(2, 3),(3, 5)]&lt;/math&gt;.<br /> [[12.1|Solution]]<br /> <br /> <br /> :12.2. Let &lt;math&gt;G = (V, E)&lt;/math&gt; be a graph and &lt;math&gt;x&lt;/math&gt; and &lt;math&gt;y&lt;/math&gt; be two distinct vertices of &lt;math&gt;G&lt;/math&gt;. Each vertex &lt;math&gt;v&lt;/math&gt; contains a given number of tokens &lt;math&gt;t(v)&lt;/math&gt; that you can collect if you visit &lt;math&gt;v&lt;/math&gt;.<br /> ::(a) Prove that it is NP-complete to find the path from &lt;math&gt;x&lt;/math&gt; to &lt;math&gt;y&lt;/math&gt; where you can collect the greatest possible number of tokens.<br /> ::(b) Give an efficient algorithm if &lt;math&gt;G&lt;/math&gt; is a directed acyclic graph (DAG).<br /> <br /> <br /> :[[12.3]]. The ''Hamiltonian completion problem'' takes a given graph &lt;math&gt;G&lt;/math&gt; and seeks an algorithm to add the smallest number of edges to &lt;math&gt;G&lt;/math&gt; so that it contains a Hamiltonian cycle. This problem is NP-complete for general graphs; however, it has an efficient algorithm if &lt;math&gt;G&lt;/math&gt; is a tree. Give an efficient and provably correct algorithm to add the minimum number of possible edges to tree &lt;math&gt;T&lt;/math&gt; so that &lt;math&gt;T&lt;/math&gt; plus these edges is Hamiltonian.<br /> [[12.3|Solution]]<br /> <br /> ===Approximation Algorithms===<br /> <br /> :12.4. In the ''maximum satisfiability problem'', we seek a truth assignment that satisfies as many clauses as possible. Give an heuristic that always satisfies at least half as many clauses as the optimal solution.<br /> <br /> <br /> :[[12.5]]. Consider the following heuristic for vertex cover. Construct a DFS tree of the graph, and delete all the leaves from this tree. What remains must be a vertex cover of the graph. Prove that the size of this cover is at most twice as large as optimal.<br /> [[12.5|Solution]]<br /> <br /> <br /> :12.6. The ''maximum cut problem'' for a graph &lt;math&gt;G = (V, E)&lt;/math&gt; seeks to partition the vertices &lt;math&gt;V&lt;/math&gt; into disjoint sets &lt;math&gt;A&lt;/math&gt; and &lt;math&gt;B&lt;/math&gt; so as to maximize the number of edges &lt;math&gt;(a, b) \in E&lt;/math&gt; such that &lt;math&gt;a \in A&lt;/math&gt; and &lt;math&gt;b \in B&lt;/math&gt;. Consider the following heuristic for maximum cut. First assign &lt;math&gt;v_1&lt;/math&gt; to &lt;math&gt;A&lt;/math&gt; and &lt;math&gt;v_2&lt;/math&gt; to &lt;math&gt;B&lt;/math&gt;. For each remaining vertex, assign it to the side that adds the most edges to the cut. Prove that this cut is at least half as large as the optimal cut.<br /> <br /> <br /> :[[12.7]].  In the ''bin-packing problem'', we are given n objects with weights &lt;math&gt;w_1, w_2, ..., w_n&lt;/math&gt;, respectively. Our goal is to find the smallest number of bins that will hold the &lt;math&gt;n&lt;/math&gt; objects, where each bin has a capacity of at most one kilogram.<br /> :The ''first-fit heuristic'' considers the objects in the order in which they are given. For each object, place it into the first bin that has room for it. If no such bin exists, start a new bin. Prove that this heuristic uses at most twice as many bins as the optimal solution.<br /> [[12.7|Solution]]<br /> <br /> <br /> :12.8. For the first-fit heuristic described just above, give an example where the packing it finds uses at least 5/3 times as many bins as optimal.<br /> <br /> <br /> :[[12.9]]. Given an undirected graph &lt;math&gt;G = (V, E)&lt;/math&gt; in which each node has degree ≤ d, show how to efficiently find an independent set whose size is at least &lt;math&gt;1/(d + 1)&lt;/math&gt; times that of the largest independent set.<br /> [[12.9|Solution]]<br /> <br /> <br /> :12.10. A vertex coloring of graph &lt;math&gt;G = (V, E)&lt;/math&gt; is an assignment of colors to vertices of &lt;math&gt;V&lt;/math&gt; such that each edge &lt;math&gt;(x, y)&lt;/math&gt; implies that vertices &lt;math&gt;x&lt;/math&gt; and &lt;math&gt;y&lt;/math&gt; are assigned different colors. Give an algorithm for vertex coloring &lt;math&gt;G&lt;/math&gt; using at most &lt;math&gt;\Delta + 1&lt;/math&gt; colors, where &lt;math&gt;\Delta&lt;/math&gt; is the maximum vertex degree of &lt;math&gt;G&lt;/math&gt;.<br /> <br /> <br /> :[[12.11]]. Show that you can solve any given Sudoku puzzle by finding the minimum vertex coloring of a specific, appropriately constructed (9×9)+9 vertex graph.<br /> [[12.11|Solution]]<br /> <br /> ===Combinatorial Optimization===<br /> For each of the problems below, design and implement a simulated annealing heuristic to get reasonable solutions. How well does your program perform in practice?<br /> <br /> <br /> :12.12. Design and implement a heuristic for the bandwidth minimization problem discussed in Section 16.2 (page 470).<br /> <br /> <br /> :[[12.13]]. Design and implement a heuristic for the maximum satisfiability problem discussed in Section 17.10 (page 537).<br /> [[12.13|Solution]]<br /> <br /> <br /> :12.14. Design and implement a heuristic for the maximum clique problem discussed in Section 19.1 (page 586).<br /> <br /> <br /> :[[12.15]]. Design and implement a heuristic for the minimum vertex coloring problem discussed in Section 19.7 (page 604).<br /> [[12.15|Solution]]<br /> <br /> <br /> :12.16. Design and implement a heuristic for the minimum edge coloring problem discussed in Section 19.8 (page 608).<br /> <br /> <br /> :[[12.17]]. Design and implement a heuristic for the minimum feedback vertex set problem discussed in Section 19.11 (page 618).<br /> [[12.17|Solution]]<br /> <br /> <br /> :12.18. Design and implement a heuristic for the set cover problem discussed in Section 21.1 (page 678).<br /> <br /> ===&quot;Quantum&quot; Computing===<br /> <br /> :[[12.19]]. Consider an &lt;math&gt;n&lt;/math&gt; qubit “quantum” system &lt;math&gt;Q&lt;/math&gt;, where each of the &lt;math&gt;N = 2^n&lt;/math&gt; states start out with equal probability &lt;math&gt;p(i) = 1/2^n&lt;/math&gt;. Say the ''Jack''&lt;math&gt;(Q, 0^n)&lt;/math&gt; operation doubles the probability of the state where all qubits are zero. How many calls to this ''Jack'' operation are necessary until the probability of sampling this null state becomes ≥ 1/2?<br /> [[12.19|Solution]]<br /> <br /> <br /> :12.20. For the satisfiability problem, construct (a) an instance on &lt;math&gt;n&lt;/math&gt; variables that has exactly one solution, and (b) an instance on &lt;math&gt;n&lt;/math&gt; variables that has exactly &lt;math&gt;2^n&lt;/math&gt; different solutions.<br /> <br /> <br /> :[[12.21]]. Consider the first ten multiples of 11, namely 11, 22, . . . 110. Pick two of them (&lt;math&gt;x&lt;/math&gt; and &lt;math&gt;y&lt;/math&gt;) at random. What is the probability that ''gcd''&lt;math&gt;(x, y) = 11&lt;/math&gt;?<br /> [[12.21|Solution]]<br /> <br /> <br /> :12.22. IBM quantum computing (https://www.ibm.com/quantum-computing/) offers the opportunity to program a quantum computing simulator. Take a look at an example quantum computing program and run it to see what happens.<br /> <br /> <br /> Back to [[Chapter List]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Chapter_11&diff=457 Chapter 11 2020-10-01T18:10:47Z <p>Algowikiadmin: Protected &quot;Chapter 11&quot; ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))</p> <hr /> <div>=NP-Completeness=<br /> <br /> ===Transformations and Satisfiability===<br /> <br /> :[[11.1]]. Give the 3-SAT formula that results from applying the reduction of SAT to 3-SAT for the formula: <br /> :::&lt;math&gt; (x\or y \or \overline z \or w \or u \or \overline v) \and (\overline x \or \overline <br /> y \or z \or \overline w \or u \or v) \and (x \or \overline y \or \overline z \or w \or u \or \overline v)\and (x \or \overline y) &lt;/math&gt;<br /> [[11.1|Solution]]<br /> <br /> <br /> :11.2. Draw the graph that results from the reduction of 3-SAT to vertex cover for the expression <br /> :::&lt;math&gt;(x \or \overline y \or z) \and (\overline x \or y \or \overline z) \and(\overline x \or y \or z) \and (x \or \overline y \or \overline x) &lt;/math&gt;<br /> <br /> <br /> :[[11.3]]. Prove that 4-SAT is NP-hard.<br /> [[11.3|Solution]]<br /> <br /> <br /> :11.4. ''Stingy'' SAT is the following problem: given a set of clauses (each a disjunction of literals) and an integer &lt;math&gt;k&lt;/math&gt;, find a satisfying assignment in which at most &lt;math&gt;k&lt;/math&gt; variables are true, if such an assignment exists. Prove that stingy SAT is NP-hard.<br /> <br /> <br /> :[[11.5]]. The ''Double SAT'' problem asks whether a given satisfiability problem has '''at least two different satisfying assignments'''. For example, the problem &lt;math&gt;{{v1, v2}, {v_1, v_2}, {v_1, v_2}}&lt;/math&gt; is satisfiable, but has only one solution &lt;math&gt;(v_1 =F, v_2 = T)&lt;/math&gt;. In contrast, &lt;math&gt;{{v_1, v_2}, {v_1, v_2}}&lt;/math&gt; has exactly two solutions. Show that Double-SAT is NP-hard.<br /> [[11.5|Solution]]<br /> <br /> <br /> :11.6. Suppose we are given a subroutine that can solve the traveling salesman decision problem on page 357 in (say) linear time. Give an efficient algorithm to find the actual TSP tour by making a polynomial number of calls to this subroutine.<br /> <br /> <br /> :[[11.7]]. Implement a SAT to 3-SAT reduction that translates satisfiability instances into equivalent 3-SAT instances.<br /> [[11.7|Solution]]<br /> <br /> <br /> :11.8. Design and implement a backtracking algorithm to test whether a set of clause sets is satisfiable. What criteria can you use to prune this search?<br /> <br /> <br /> :[[11.9]]. Implement the vertex cover to satisfiability reduction, and run the resulting clauses through a satisfiability solver code. Does this seem like a practical way to compute things?<br /> [[11.9|Solution]]<br /> <br /> ===Basic Reductions===<br /> <br /> :11.10. An instance of the ''set cover'' problem consists of a set &lt;math&gt;X&lt;/math&gt; of &lt;math&gt;n&lt;/math&gt; elements, a family &lt;math&gt;F&lt;/math&gt; of subsets of &lt;math&gt;X&lt;/math&gt;, and an integer &lt;math&gt;k&lt;/math&gt;. The question is, does there exist &lt;math&gt;k&lt;/math&gt; subsets from &lt;math&gt;F&lt;/math&gt; whose union is &lt;math&gt;X&lt;/math&gt;? For example, if &lt;math&gt;X = \{1,2,3,4\}&lt;/math&gt; and &lt;math&gt;F = \{ \{1,2\}, \{2,3\}, \{4\}, \{2,4\} \}&lt;/math&gt;, there does not exist a solution for &lt;math&gt;k=2&lt;/math&gt;, but there does for &lt;math&gt;k=3&lt;/math&gt; (for example, &lt;math&gt; \{1,2\}, \{2,3\}, \{4\}&lt;/math&gt;). Prove that set cover is NP-complete with a reduction from vertex cover.<br /> <br /> <br /> :[[11.11]]. The ''baseball card collector problem'' is as follows. Given packets &lt;math&gt;P_1, \ldots, P_m&lt;/math&gt;, each of which contains a subset of this year's baseball cards, is it possible to collect all the year's cards by buying &lt;math&gt;\leq k&lt;/math&gt; packets? For example, if the players are &lt;math&gt; \{Aaron, Mays, Ruth, Steven \} &lt;/math&gt; and the packets are<br /> :::&lt;math&gt; \{ \{Aaron,Mays\}, \{Mays,Ruth\}, \{Steven\}, \{Mays,Steven\} \}, &lt;/math&gt; <br /> :there does not exist a solution for &lt;math&gt;k=2&lt;/math&gt;, but there does for &lt;math&gt;k=3&lt;/math&gt;, such as <br /> :::&lt;math&gt; \{Aaron,Mays\}, \{Mays,Ruth\}, \{Steven\} &lt;/math&gt; <br /> :Prove that the baseball card collector problem is NP hard using a reduction from vertex cover.<br /> [[11.11|Solution]]<br /> <br /> <br /> :11.12. The ''low-degree spanning tree problem'' is as follows. Given a graph &lt;math&gt;G&lt;/math&gt; and an integer &lt;math&gt;k&lt;/math&gt;, does &lt;math&gt;G&lt;/math&gt; contain a spanning tree such that all vertices in the tree have degree ''at most'' &lt;math&gt;k&lt;/math&gt; (obviously, only tree edges count towards the degree)? For example, in the following graph, there is no spanning tree such that all vertices have a degree less than three.<br /> \fixedfigsize{pictures/lowdegree.png}{1.0in}<br /> #Prove that the low-degree spanning tree problem is NP-hard with a reduction from Hamiltonian ''path''. <br /> #Now consider the ''high-degree spanning tree problem'', which is as follows. Given a graph &lt;math&gt;G&lt;/math&gt; and an integer &lt;math&gt;k&lt;/math&gt;, does &lt;math&gt;G&lt;/math&gt; contain a spanning tree whose highest degree vertex is ''at least'' &lt;math&gt;k&lt;/math&gt;? In the previous example, there exists a spanning tree with a highest degree of 8. Give an efficient algorithm to solve the high-degree spanning tree problem, and an analysis of its time complexity.<br /> <br /> <br /> :[[11.13]].In the minimum element set cover problem, we seek a set cover &lt;math&gt;S \subseteq C&lt;/math&gt; of a universal set &lt;math&gt;U = {1, . . . , n}&lt;/math&gt; such that sum of the sizes of the subsets in &lt;math&gt;S&lt;/math&gt; is at most &lt;math&gt;k&lt;/math&gt;. <br /> ::(a) Show that &lt;math&gt;C = {{1, 2, 3}, {1, 3, 4}, {2, 3, 4}, {3, 4, 5}}&lt;/math&gt; has a cover of size 6, but none of size 5 because of a repeated element. <br /> ::(b) Prove that this problem is NP-hard. (Hint: set cover remains hard if all subsets are of the same size.)<br /> [[11.13|Solution]]<br /> <br /> <br /> :11.14. The ''half-Hamiltonian cycle problem'' is, given a graph &lt;math&gt;G&lt;/math&gt; with &lt;math&gt;n&lt;/math&gt; vertices, determine whether &lt;math&gt;G&lt;/math&gt; has a simple cycle of length exactly &lt;math&gt;[n/2]&lt;/math&gt;, where the floor function rounds its input down to the nearest integer. Prove that this problem is NP-hard.<br /> <br /> <br /> :[[11.15]]. The 3-phase power balance problem asks for a way to partition a set of n positive integers into three sets &lt;math&gt;A&lt;/math&gt;, &lt;math&gt;B&lt;/math&gt;, or &lt;math&gt;C&lt;/math&gt; such that &lt;math&gt;\sum_{i} a_i = \sum_{i} b_i = \sum_{i} c_i&lt;/math&gt;. Prove that this problem is NP-hard using a reduction from integer partition or subset sum (see Section 10.5 (page 329)).<br /> [[11.15|Solution]]<br /> <br /> <br /> :11.16. Show that the following problem is NP-complete:<br /> :* ''Problem:'' Dense subgraph<br /> :* ''Input:'' A graph &lt;math&gt;G&lt;/math&gt;, and integers &lt;math&gt;k&lt;/math&gt; and &lt;math&gt;y&lt;/math&gt;.<br /> :* ''Output:'' Does &lt;math&gt;G&lt;/math&gt; contain a subgraph with exactly &lt;math&gt;k&lt;/math&gt; vertices and at least &lt;math&gt;y&lt;/math&gt; edges?<br /> <br /> <br /> :[[11.17]]. Show that the following problem is NP-complete:<br /> :* ''Problem:'' Clique, no-clique<br /> :* ''Input:'' An undirected graph &lt;math&gt;G=(V,E)&lt;/math&gt; and an integer &lt;math&gt;k&lt;/math&gt;.<br /> :* ''Output:'' Does &lt;math&gt;G&lt;/math&gt; contain both a clique of size &lt;math&gt;k&lt;/math&gt; and an independent set of size &lt;math&gt;k&lt;/math&gt;.<br /> [[11.17|Solution]]<br /> <br /> <br /> :11.18. n ''Eulerian cycle'' is a tour that visits every edge in a graph exactly once. An ''Eulerian subgraph'' is a subset of the edges and vertices of a graph that has an Eulerian cycle. Prove that the problem of finding the number of edges in the largest Eulerian subgraph of a graph is NP-hard. (Hint: the Hamiltonian circuit problem is NP-hard even if each vertex in the graph is incident upon exactly three edges.)<br /> <br /> <br /> :[[11.19]]. Show that the following problem is NP-hard:<br /> :*''Problem:'' Maximum Common Subgraph<br /> :*''Input:'' Two graphs &lt;math&gt;G_1 = (V_1, E_1)&lt;/math&gt; and &lt;math&gt;G_2 = (V_2, E_2)&lt;/math&gt;, and a budget &lt;math&gt;b&lt;/math&gt;.<br /> :*Output: Two sets of nodes &lt;math&gt;S_1 \subseteq V_1&lt;/math&gt; and &lt;math&gt;S_2 \subseteq V_2&lt;/math&gt; whose deletion leaves at least &lt;math&gt;b&lt;/math&gt; nodes in each graph, and makes the two graphs identical.<br /> [[11.19|Solution]]<br /> <br /> <br /> :11.20. A ''strongly independent'' set is a subset of vertices &lt;math&gt;S&lt;/math&gt; in a graph &lt;math&gt;G&lt;/math&gt; such that for any two vertices in &lt;math&gt;S&lt;/math&gt;, there is no path of length two in &lt;math&gt;G&lt;/math&gt;. Prove that strongly independent set is NP-hard.<br /> <br /> <br /> :[[11.21]]. A ''kite'' is a graph on an even number of vertices, say &lt;math&gt;2n&lt;/math&gt;, in which &lt;math&gt;n&lt;/math&gt; of the vertices form a clique and the remaining &lt;math&gt;n&lt;/math&gt; vertices are connected in a tail that consists of a path joined to one of the vertices of the clique. Given a graph and a goal g, the ''max kite'' problem asks for a subgraph that is a kite and contains &lt;math&gt;2g&lt;/math&gt; nodes. Prove that ''max kite'' is NP-hard.<br /> [[11.21|Solution]]<br /> <br /> ===Creative Reductions===<br /> <br /> :11.22. Prove that the following problem is NP-complete:<br /> :* ''Problem:'' Hitting Set<br /> :* ''Input:'' A collection &lt;math&gt;C&lt;/math&gt; of subsets of a set &lt;math&gt;S&lt;/math&gt;, positive integer &lt;math&gt;k&lt;/math&gt;.<br /> :* ''Output:'' Does &lt;math&gt;S&lt;/math&gt; contain a subset &lt;math&gt;S'&lt;/math&gt; such that &lt;math&gt;|S'| \leq k&lt;/math&gt; and each subset in &lt;math&gt;C&lt;/math&gt; contains at least one element from &lt;math&gt;S'&lt;/math&gt;?<br /> <br /> <br /> :[[11.23]]. Prove that the following problem is NP-complete:<br /> :* ''Problem:'' Knapsack<br /> :* ''Input:'' A set &lt;math&gt;S&lt;/math&gt; of &lt;math&gt;n&lt;/math&gt; items, such that the &lt;math&gt;i&lt;/math&gt;th item has value &lt;math&gt;v_i&lt;/math&gt; and weight &lt;math&gt;w_i&lt;/math&gt;. Two positive integers: weight limit &lt;math&gt;W&lt;/math&gt; and value requirement &lt;math&gt;V&lt;/math&gt;.<br /> :* ''Output:'' Does there exist a subset &lt;math&gt;S' \in S&lt;/math&gt; such that &lt;math&gt;\sum_{i \in S'} w_i \leq W&lt;/math&gt; and &lt;math&gt;\sum_{i \in S'} v_i \geq V&lt;/math&gt;? <br /> :(Hint: start from integer partition.)<br /> [[11.23|Solution]]<br /> <br /> <br /> :11.24. Prove that the following problem is NP-complete:<br /> :* ''Problem:'' Hamiltonian Path<br /> :* ''Input:'' A graph &lt;math&gt;G&lt;/math&gt;, and vertices &lt;math&gt;s&lt;/math&gt; and &lt;math&gt;t&lt;/math&gt;.<br /> :* ''Output:'' Does &lt;math&gt;G&lt;/math&gt; contain a path which starts from &lt;math&gt;s&lt;/math&gt;, ends at &lt;math&gt;t&lt;/math&gt;, and visits all vertices without visiting any vertex more than once? (Hint: start from Hamiltonian cycle.)<br /> <br /> <br /> :[[11.25]]. Prove that the following problem is NP-complete:<br /> :* ''Problem:'' Longest Path<br /> :* ''Input:'' A graph &lt;math&gt;G&lt;/math&gt; and positive integer &lt;math&gt;k&lt;/math&gt;.<br /> :* ''Output:'' Does &lt;math&gt;G&lt;/math&gt; contain a path that visits at least &lt;math&gt;k&lt;/math&gt; different vertices without visiting any vertex more than once?<br /> [[11.25|Solution]]<br /> <br /> <br /> :11.26. Prove that the following problem is NP-complete:<br /> :* ''Problem:'' Dominating Set<br /> :* ''Input:'' A graph &lt;math&gt;G=(V,E)&lt;/math&gt; and positive integer &lt;math&gt;k&lt;/math&gt;.<br /> :* ''Output:'' Is there a subset &lt;math&gt;V' \in V&lt;/math&gt; such that &lt;math&gt;|V'|\leq k&lt;/math&gt; where for each vertex &lt;math&gt;x \in V&lt;/math&gt; either &lt;math&gt;x \in V'&lt;/math&gt; or there exists an edge &lt;math&gt;(x,y)&lt;/math&gt;, where &lt;math&gt;y \in V'&lt;/math&gt;.<br /> <br /> <br /> :[[11.27]]. Prove that the vertex cover problem (does there exist a subset &lt;math&gt;S&lt;/math&gt; of &lt;math&gt;k&lt;/math&gt; vertices in a graph &lt;math&gt;G&lt;/math&gt; such that every edge in &lt;math&gt;G&lt;/math&gt; is incident upon at least one vertex in &lt;math&gt;S&lt;/math&gt;?) remains NP-complete even when all the vertices in the graph are restricted to have even degrees.<br /> [[11.27|Solution]]<br /> <br /> <br /> :11.28. Prove that the following problem is NP-complete:<br /> :* ''Problem:'' Set Packing<br /> :* ''Input:'' A collection &lt;math&gt;C&lt;/math&gt; of subsets of a set &lt;math&gt;S&lt;/math&gt;, positive integer &lt;math&gt;k&lt;/math&gt;.<br /> :* ''Output:'' Does &lt;math&gt;S&lt;/math&gt; contain at least &lt;math&gt;k&lt;/math&gt; disjoint subsets (i.e., such that none of these subsets have any elements in common?)<br /> <br /> <br /> :[[11.29]]. Prove that the following problem is NP-complete:<br /> :* ''Problem:'' Feedback Vertex Set<br /> :* ''Input:'' A directed graph &lt;math&gt;G=(V,A)&lt;/math&gt; and positive integer &lt;math&gt;k&lt;/math&gt;.<br /> :* ''Output:'' Is there a subset &lt;math&gt;V' \in V&lt;/math&gt; such that &lt;math&gt;|V'|\leq k&lt;/math&gt;, such that deleting the vertices of &lt;math&gt;V'&lt;/math&gt; from &lt;math&gt;G&lt;/math&gt; leaves a DAG?<br /> [[11.29|Solution]]<br /> <br /> <br /> :11.30. Give a reduction from Sudoku to the vertex coloring problem in graphs. Specifically, describe how to take any partially filled Sudoku board and construct a graph that can be colored with nine colors iff the Sudoku board is solvable.<br /> <br /> ===Algorithms for Special Cases===<br /> <br /> :[[11.31]]. A Hamiltonian path &lt;math&gt;P&lt;/math&gt; is a path that visits each vertex exactly once. The problem of testing whether a graph &lt;math&gt;G&lt;/math&gt; contains a Hamiltonian path is NP-complete. There does not have to be an edge in &lt;math&gt;G&lt;/math&gt; from the ending vertex to the starting vertex of &lt;math&gt;P&lt;/math&gt;, unlike in the Hamiltonian cycle problem. Give an &lt;math&gt;O(n+m)&lt;/math&gt;-time algorithm to test whether a directed acyclic graph &lt;math&gt;G&lt;/math&gt; (a DAG) contains a Hamiltonian path. (Hint: think about topological sorting and DFS.)<br /> [[11.31|Solution]]<br /> <br /> <br /> :11.32. Consider the ''k''-clique problem, which is the general clique problem restricted to graphs in which every vertex has degree at most &lt;math&gt;k&lt;/math&gt;. Prove that ''k''-clique has an efficient algorithm for any given &lt;math&gt;k&lt;/math&gt;, meaning that &lt;math&gt;k&lt;/math&gt; is a constant.<br /> <br /> <br /> :[[11.33]]. The &lt;math&gt;2&lt;/math&gt;-SAT problem is, given a Boolean formula in 2-conjunctive normal form (CNF), to decide whether the formula is satisfiable. &lt;math&gt;2&lt;/math&gt;-SAT is like &lt;math&gt;3&lt;/math&gt;-SAT, except that each clause can have only two literals. For example, the following formula is in &lt;math&gt;2&lt;/math&gt;-CNF: &lt;math&gt; (x_1 \or x_2) \and (\bar{x}_2 \or x_3) \and (x_1 \or \bar{x}_3) &lt;/math&gt; <br /> :Give a polynomial-time algorithm to solve &lt;math&gt;2&lt;/math&gt;-SAT.<br /> [[11.33|Solution]]<br /> <br /> <br /> :11.34, Show that the following problems are in NP:<br /> #Does graph &lt;math&gt;G&lt;/math&gt; have a simple path (i.e., with no vertex repeated) of length &lt;math&gt;k&lt;/math&gt;? \#Is integer &lt;math&gt;n&lt;/math&gt; composite (i.e., not prime)? <br /> #Does graph &lt;math&gt;G&lt;/math&gt; have a vertex cover of size &lt;math&gt;k&lt;/math&gt;?<br /> <br /> <br /> :[[11.35]]. Until 2002, it was an open question whether the decision problem ''Is integer &lt;math&gt;n&lt;/math&gt; a composite number, in other words, not prime?'' can be computed in time polynomial in the size of its input. Why doesn't the following algorithm suffice to prove it is in P, since it runs in &lt;math&gt;O(n)&lt;/math&gt; time?<br /> PrimalityTesting(&lt;math&gt;n&lt;/math&gt;)<br /> composite := &lt;math&gt;false&lt;/math&gt;<br /> for i := 2 to &lt;math&gt;n-1&lt;/math&gt; do<br /> if &lt;math&gt;(n\,\bmod\,i) = 0&lt;/math&gt; then<br /> composite := &lt;math&gt;true&lt;/math&gt;<br /> [[11.35|Solution]]<br /> <br /> <br /> Back to [[Chapter List]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Chapter_10&diff=456 Chapter 10 2020-10-01T18:10:29Z <p>Algowikiadmin: Protected &quot;Chapter 10&quot; ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))</p> <hr /> <div>=Dynamic Programming=<br /> <br /> ===Elementary Recurrences===<br /> <br /> :[[10.1]]. Up to &lt;math&gt;k&lt;/math&gt; steps in a single bound! A child is running up a staircase with &lt;math&gt;n&lt;/math&gt; steps and can hop between 1 and &lt;math&gt;k&lt;/math&gt; steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of &lt;math&gt;n&lt;/math&gt; and &lt;math&gt;k&lt;/math&gt;. What is the running time of your algorithm?<br /> [[10.1|Solution]]<br /> <br /> <br /> :10.2. Imagine you are a professional thief who plans to rob houses along a street of &lt;math&gt;n&lt;/math&gt; homes. You know the loot at house &lt;math&gt;i&lt;/math&gt; is worth &lt;math&gt;m_i&lt;/math&gt;, for &lt;math&gt;1 \le i \le n&lt;/math&gt;, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police.<br /> <br /> <br /> :[[10.3]]. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given &lt;math&gt;n&lt;/math&gt;. For &lt;math&gt;n&lt;/math&gt; = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1).<br /> [[10.3|Solution]]<br /> <br /> <br /> :10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given &lt;math&gt;n&lt;/math&gt;. For &lt;math&gt;n&lt;/math&gt; = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1.<br /> <br /> <br /> :[[10.5]]. Given an &lt;math&gt;s * t&lt;/math&gt; grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time.<br /> ::(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of &lt;math&gt;s&lt;/math&gt; and &lt;math&gt;t&lt;/math&gt;?<br /> ::(b) Give a solution based on dynamic programming. What is its time complexity as a function of &lt;math&gt;s&lt;/math&gt; and &lt;math&gt;t&lt;/math&gt;?<br /> [[10.5|Solution]]<br /> <br /> ===Edit Distance===<br /> <br /> :10.6. Typists often make transposition errors exchanging neighboring characters, such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance.<br /> :Incorporate a swap operation into our edit distance function, so that such neighboring transposition errors can be fixed at the cost of one operation.<br /> <br /> <br /> :[[10.7]]. Suppose you are given three strings of characters: &lt;math&gt;X&lt;/math&gt;, &lt;math&gt;Y&lt;/math&gt;, and &lt;math&gt;Z&lt;/math&gt;, where &lt;math&gt;|X| = n&lt;/math&gt;, &lt;math&gt;|Y| = m&lt;/math&gt;, and &lt;math&gt;|Z| = n + m&lt;/math&gt;. &lt;math&gt;Z&lt;/math&gt; is said to be a shuffle of &lt;math&gt;X&lt;/math&gt; and &lt;math&gt;Y&lt;/math&gt; iff &lt;math&gt;Z&lt;/math&gt; can be formed by interleaving the characters from &lt;math&gt;X&lt;/math&gt; and &lt;math&gt;Y&lt;/math&gt; in a way that maintains the left-to-right ordering of the characters from each string. <br /> :(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not.<br /> :(b) Give an efficient dynamic programming algorithm that determines whether &lt;math&gt;Z&lt;/math&gt; is a shuffle of &lt;math&gt;X&lt;/math&gt; and &lt;math&gt;Y&lt;/math&gt;. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.)<br /> [[10.7|Solution]]<br /> <br /> <br /> :10.8. The longest common substring (not subsequence) of two strings &lt;math&gt;X&lt;/math&gt; and &lt;math&gt;Y&lt;/math&gt; is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph.<br /> :(a) Let &lt;math&gt;n = |X|&lt;/math&gt; and &lt;math&gt;m = |Y|&lt;/math&gt;. Give a &lt;math&gt;\Theta (nm)&lt;/math&gt; dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm.<br /> :(b) Give a simpler &lt;math&gt;\Theta (nm)&lt;/math&gt; algorithm that does not rely on dynamic programming.<br /> <br /> <br /> :[[10.9]]. The ''longest common subsequence (LCS)'' of two sequences &lt;math&gt;T&lt;/math&gt; and &lt;math&gt;P&lt;/math&gt; is the longest sequence &lt;math&gt;L&lt;/math&gt; such that &lt;math&gt;L&lt;/math&gt; is a subsequence of both &lt;math&gt;T&lt;/math&gt; and &lt;math&gt;P&lt;/math&gt;. The ''shortest common supersequence (SCS)'' of &lt;math&gt;T&lt;/math&gt; and &lt;math&gt;P&lt;/math&gt; is the smallest sequence &lt;math&gt;L&lt;/math&gt; such that both &lt;math&gt;T&lt;/math&gt; and &lt;math&gt;P&lt;/math&gt; are a subsequence of &lt;math&gt;L&lt;/math&gt;.<br /> :(a) Give efficient algorithms to find the LCS and SCS of two given sequences.<br /> :(b) Let &lt;math&gt;d(T, P)&lt;/math&gt; be the minimum edit distance between &lt;math&gt;T&lt;/math&gt; and &lt;math&gt;P&lt;/math&gt; when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that &lt;math&gt;d(T, P) = |SCS(T, P)| - |LCS(T, P)|&lt;/math&gt; where &lt;math&gt;|SCS(T, P)| (|LCS(T, P)|)&lt;/math&gt; is the size of the shortest SCS (longest LCS) of &lt;math&gt;T&lt;/math&gt; and &lt;math&gt;P&lt;/math&gt;.<br /> [[10.9|Solution]]<br /> <br /> <br /> :10.10. Suppose you are given &lt;math&gt;n&lt;/math&gt; poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone. <br /> :For example, consider the stacks &lt;math&gt;(RRGG, GBBB)&lt;/math&gt;. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an &lt;math&gt;O(n^2)&lt;/math&gt; dynamic programming algorithm to find the best strategy for a given pair of chip piles.<br /> <br /> ===Greedy Algorithms===<br /> <br /> :[[10.11]]. Let &lt;math&gt;P_1, P_2, . . . , P_n&lt;/math&gt; be &lt;math&gt;n&lt;/math&gt; programs to be stored on a disk with capacity &lt;math&gt;D&lt;/math&gt; megabytes. Program &lt;math&gt;P_i&lt;/math&gt; requires &lt;math&gt;s_i&lt;/math&gt; megabytes of storage. We cannot store them all because &lt;math&gt;D &lt; \sum_{i=1}^n s_i &lt;/math&gt;<br /> :(a) Does a greedy algorithm that selects programs in order of non-decreasing &lt;math&gt;s_i&lt;/math&gt; maximize the number of programs held on the disk? Prove or give a counter-example.<br /> :(b) Does a greedy algorithm that selects programs in order of non-increasing &lt;math&gt;s_i&lt;/math&gt; use as much of the capacity of the disk as possible? Prove or give a counter-example.<br /> [[10.11|Solution]]<br /> <br /> <br /> :10.12. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of &lt;math&gt;{d_1, . . . , d_k}&lt;/math&gt; units. We seek an algorithm to make change of &lt;math&gt;n&lt;/math&gt; units using the minimum number of this country’s coins.<br /> :(a) The greedy algorithm repeatedly selects the biggest coin no bigger than the amount to be changed and repeats until it is zero. Show that the greedy algorithm does not always use the minimum number of coins in a country whose denominations are {1, 6, 10}.<br /> :(b) Give an efficient algorithm that correctly determines the minimum number of coins needed to make change of &lt;math&gt;n&lt;/math&gt; units using denominations &lt;math&gt;{d_1, . . . , d_k}&lt;/math&gt;. Analyze its running time.<br /> <br /> <br /> :[[10.13]]. Coins in the United States are minted with denominations of 1, 5, 10, 25, and 50 cents. Now consider a country whose coins are minted with denominations of &lt;math&gt;{d_1, . . . , d_k}&lt;/math&gt; units. We want to count how many distinct ways &lt;math&gt;C(n)&lt;/math&gt; there are to make change of &lt;math&gt;n&lt;/math&gt; units. For example, in a country whose denominations are {1, 6, 10}, &lt;math&gt;C(5) = 1&lt;/math&gt;, &lt;math&gt;C(6)&lt;/math&gt; to &lt;math&gt;C(9) = 2&lt;/math&gt;, &lt;math&gt;C(10) = 3&lt;/math&gt;, and &lt;math&gt;C(12) = 4&lt;/math&gt;.<br /> :(a) How many ways are there to make change of 20 units from {1, 6, 10}?<br /> :(b) Give an efficient algorithm to compute &lt;math&gt;C(n)&lt;/math&gt;, and analyze its complexity. (Hint: think in terms of computing &lt;math&gt;C(n, d)&lt;/math&gt;, the number of ways to make change of &lt;math&gt;n&lt;/math&gt; units with highest denomination &lt;math&gt;d&lt;/math&gt;. Be careful to avoid overcounting.)<br /> [[10.13|Solution]]<br /> <br /> <br /> :10.14. In the single-processor scheduling problem, we are given a set of &lt;math&gt;n&lt;/math&gt; jobs &lt;math&gt;J&lt;/math&gt;. Each job &lt;math&gt;i&lt;/math&gt; has a processing time &lt;math&gt;t_i&lt;/math&gt;, and a deadline &lt;math&gt;d_i&lt;/math&gt;. A feasible schedule is a permutation of the jobs such that when the jobs are performed in that order, every job is finished before its deadline. The greedy algorithm for single-processor scheduling selects the job with the earliest deadline first.<br /> :Show that if a feasible schedule exists, then the schedule produced by this greedy algorithm is feasible.<br /> <br /> ===Number Problems===<br /> <br /> :[[10.15]]. You are given a rod of length &lt;math&gt;n&lt;/math&gt; inches and a table of prices obtainable for rod-pieces of size &lt;math&gt;n&lt;/math&gt; or smaller. Give an efficient algorithm to find the maximum value obtainable by cutting up the rod and selling the pieces. For example, if &lt;math&gt;n=8&lt;/math&gt; and the values of different pieces are:<br /> &lt;center&gt;&lt;math&gt; <br /> \begin{array}{|c|c|c|c|c|c|c|c||c|} length &amp; 1 &amp; 2 &amp; 3 &amp; 4 &amp; 5 &amp; 6 &amp; 7 &amp; 8\\<br /> \hline<br /> price&amp;1&amp;5&amp;8&amp;9&amp;10&amp;17&amp;17&amp;20\\<br /> \end{array}&lt;/math&gt;<br /> &lt;/center&gt;<br /> :then the maximum obtainable value is 22, by cutting into pieces of lengths 2 and 6.<br /> [[10.15|Solution]]<br /> <br /> <br /> :10.16. Your boss has written an arithmetic expression of n terms to compute your annual bonus, but permits you to parenthesize it however you wish. Give an efficient algorithm to design the parenthesization to maximize the value. For the expression:<br /> &lt;center&gt;&lt;math&gt;6 + 2 * 0 - 4&lt;/math&gt;&lt;/center&gt;<br /> :there exist parenthesizations with values ranging from −32 to 2.<br /> <br /> <br /> :[[10.17]]. Given a positive integer &lt;math&gt;n&lt;/math&gt;, find an efficient algorithm to compute the smallest number of perfect squares (e.g. 1, 4, 9, 16, . . .) that sum to &lt;math&gt;n&lt;/math&gt;. What is the running time of your algorithm?<br /> [[10.17|Solution]]<br /> <br /> <br /> :10.18. Given an array &lt;math&gt;A&lt;/math&gt; of &lt;math&gt;n&lt;/math&gt; integers, find an efficient algorithm to compute the largest sum of a continuous run. For &lt;math&gt;A = [-3, 2, 7, -3, 4, -2, 0, 1]&lt;/math&gt;, the largest such sum is 10, from the second through fifth positions.<br /> <br /> <br /> :[[10.19]]. Two drivers have to divide up &lt;math&gt;m&lt;/math&gt; suitcases between them, where the weight of the &lt;math&gt;ith&lt;/math&gt; suitcase is &lt;math&gt;w_i&lt;/math&gt;. Give an efficient algorithm to divide up the loads so the two drivers carry equal weight, if possible.<br /> [[10.19|Solution]]<br /> <br /> <br /> :10.20. The ''knapsack problem'' is as follows: given a set of integers &lt;math&gt;S = {s_1, s_2, . . . , s_n}&lt;/math&gt;, and a given target number &lt;math&gt;T&lt;/math&gt;, find a subset of &lt;math&gt;S&lt;/math&gt; that adds up exactly to &lt;math&gt;T&lt;/math&gt;. For example, within &lt;math&gt;S = {1, 2, 5, 9, 10}&lt;/math&gt; there is a subset that adds up to &lt;math&gt;T = 22&lt;/math&gt; but not &lt;math&gt;T = 23&lt;/math&gt;.<br /> :Give a dynamic programming algorithm for knapsack that runs in &lt;math&gt;O(nT)&lt;/math&gt; time.<br /> <br /> <br /> :[[10.21]]. The integer partition takes a set of positive integers &lt;math&gt;S = {s_1, . . . , s_n}&lt;/math&gt; and seeks a subset &lt;math&gt;I \subset S&lt;/math&gt; such that<br /> &lt;center&gt;&lt;math&gt; \sum_{i \in I} s_i = \sum_{i \notin I} s_i&lt;/math&gt;&lt;/center&gt;<br /> :Let &lt;math&gt; \sum_{i \in S} s_i = M &lt;/math&gt;. Give an &lt;math&gt;O(nM)&lt;/math&gt; dynamic programming algorithm to solve the integer partition problem.<br /> [[10.21|Solution]]<br /> <br /> <br /> :10.22. Assume that there are n numbers (some possibly negative) on a circle, and we wish to find the maximum contiguous sum along an arc of the circle. Give an efficient algorithm for solving this problem.<br /> <br /> <br /> :[[10.23]]. A certain string processing language allows the programmer to break a string into two pieces. It costs &lt;math&gt;n&lt;/math&gt; units of time to break a string of &lt;math&gt;n&lt;/math&gt; characters into two pieces, since this involves copying the old string. A programmer wants to break a string into many pieces, and the order in which the breaks are made can affect the total amount of time used. For example, suppose we wish to break a 20-character string after characters 3, 8, and 10. If the breaks are made in left-to-right order, then the first break costs 20 units of time, the second break costs 17 units of time, and the third break costs 12 units of time, for a total of 49 units. If the breaks are made in right-to-left order, the first break costs 20 units of time, the second break costs 10 units of time, and the third break costs 8 units of time, for a total of only 38 units.<br /> :Give a dynamic programming algorithm that takes a list of character positions after which to break and determines the cheapest break cost in &lt;math&gt;O(n^3)&lt;/math&gt; time.<br /> [[10.23|Solution]]<br /> <br /> <br /> :10.24. Consider the following data compression technique. We have a table of &lt;math&gt;m&lt;/math&gt; text strings, each at most &lt;math&gt;k&lt;/math&gt; in length. We want to encode a data string &lt;math&gt;D&lt;/math&gt; of length &lt;math&gt;n&lt;/math&gt; using as few text strings as possible. For example, if our table contains &lt;math&gt;(a,ba,abab,b)&lt;/math&gt; and the data string is &lt;math&gt;bababbaababa&lt;/math&gt;, the best way to encode it is &lt;math&gt;(b,abab,ba,abab,a)-a&lt;/math&gt; total of five code words. Give an &lt;math&gt;O(nmk)&lt;/math&gt; algorithm to find the length of the best encoding. You may assume that every string has at least one encoding in terms of the table.<br /> <br /> <br /> :[[10.25]]. The traditional world chess championship is a match of 24 games. The current champion retains the title in case the match is a tie. Each game ends in a win, loss, or draw (tie) where wins count as 1, losses as 0, and draws as &lt;math&gt;1/2&lt;/math&gt;. The players take turns playing white and black. White plays first and so has an advantage. The champion plays white in the first game. The champ has probabilities &lt;math&gt;w_w&lt;/math&gt;, &lt;math&gt;w_d&lt;/math&gt;, and &lt;math&gt;w_l&lt;/math&gt; of winning, drawing, and losing playing white, and has probabilities &lt;math&gt;b_w&lt;/math&gt;, &lt;math&gt;b_d&lt;/math&gt;, and &lt;math&gt;b_l&lt;/math&gt; of winning, drawing, and losing playing black.<br /> :(a) Write a recurrence for the probability that the champion retains the title. Assume that there are &lt;math&gt;g&lt;/math&gt; games left to play in the match and that the champion needs to get &lt;math&gt;i&lt;/math&gt; points (which may be a multiple of &lt;math&gt;1/2&lt;/math&gt;).<br /> :(b) Based on your recurrence, give a dynamic programming algorithm to calculate the champion’s probability of retaining the title.<br /> :(c) Analyze its running time for an &lt;math&gt;n&lt;/math&gt; game match.<br /> [[10.25|Solution]]<br /> <br /> <br /> :10.26. Eggs break when dropped from great enough height. Specifically, there must be a floor &lt;math&gt;f&lt;/math&gt; in any sufficiently tall building such that an egg dropped from the &lt;math&gt;f&lt;/math&gt;th floor breaks, but one dropped from the &lt;math&gt;(f - 1)&lt;/math&gt;st floor will not. If the egg always breaks, then &lt;math&gt;f = 1&lt;/math&gt;. If the egg never breaks, then &lt;math&gt;f = n + 1&lt;/math&gt;. <br /> :You seek to find the critical floor &lt;math&gt;f&lt;/math&gt; using an &lt;math&gt;n-floor&lt;/math&gt; building. The only operation you can perform is to drop an egg off some floor and see what happens. You start out with &lt;math&gt;k&lt;/math&gt; eggs, and seek to make as few drops as possible. Broken eggs cannot be reused. Let &lt;math&gt;E(k, n)&lt;/math&gt; be the minimum number of egg drops that will always suffice.<br /> :(a) Show that &lt;math&gt;E(1, n) = n&lt;/math&gt;.<br /> :(b) Show that &lt;math&gt;E(k, n) = \Theta (n^{1/k})&lt;/math&gt;.<br /> :(c) Find a recurrence for &lt;math&gt;E(k, n)&lt;/math&gt;. What is the running time of the dynamic program to find &lt;math&gt;E(k, n)&lt;/math&gt;?<br /> <br /> ===Graphing Problem===<br /> <br /> :[[10.27]]. Consider a city whose streets are defined by an &lt;math&gt;X * Y&lt;/math&gt; grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. <br /> :Unfortunately, the city has bad neighborhoods, whose intersections we do not want to walk in. We are given an &lt;math&gt;X * Y&lt;/math&gt; matrix bad, where &lt;math&gt;bad[i,j] =&lt;/math&gt; “yes” iff the intersection between streets &lt;math&gt;i&lt;/math&gt; and &lt;math&gt;j&lt;/math&gt; is in a neighborhood to avoid.<br /> :(a) Give an example of the contents of bad such that there is no path across the grid avoiding bad neighborhoods.<br /> :(b) Give an &lt;math&gt;O(XY)&lt;/math&gt; algorithm to find a path across the grid that avoids bad neighborhoods.<br /> :(c) Give an &lt;math&gt;O(XY)&lt;/math&gt; algorithm to find the shortest path across the grid that avoids bad neighborhoods. You may assume that all blocks are of equal length. For partial credit, give an &lt;math&gt;O(X^2Y^2)&lt;/math&gt; algorithm.<br /> [[10.27|Solution]]<br /> <br /> <br /> :10.28. Consider the same situation as the previous problem. We have a city whose streets are defined by an &lt;math&gt;X * Y&lt;/math&gt; grid. We are interested in walking from the upper left-hand corner of the grid to the lower right-hand corner. We are given an &lt;math&gt;X * Y&lt;/math&gt; matrix bad, where &lt;math&gt;bad[i,j] =&lt;/math&gt; “yes” iff the intersection between streets &lt;math&gt;i&lt;/math&gt; and &lt;math&gt;j&lt;/math&gt; is somewhere we want to avoid.<br /> :If there were no bad neighborhoods to contend with, the shortest path across the grid would have length &lt;math&gt;(X - 1) + (Y - 1)&lt;/math&gt; blocks, and indeed there would be many such paths across the grid. Each path would consist of only rightward and downward moves.<br /> :Give an algorithm that takes the array bad and returns the number of safe paths of length &lt;math&gt;X + Y - 2&lt;/math&gt;. For full credit, your algorithm must run in &lt;math&gt;O(XY)&lt;/math&gt;.<br /> <br /> <br /> :[[10.29]]. You seek to create a stack out of &lt;math&gt;n&lt;/math&gt; boxes, where box &lt;math&gt;i&lt;/math&gt; has width &lt;math&gt;w_i&lt;/math&gt;, height &lt;math&gt;h_i&lt;/math&gt;, and depth &lt;math&gt;d_i&lt;/math&gt;. The boxes cannot be rotated, and can only be stacked on top of one another when each box in the stack is strictly larger than the box above it in width, height, and depth. Give an efficient algorithm to construct the tallest possible stack, where the height is the sum of the heights of each box in the stack.<br /> [[10.29|Solution]]<br /> <br /> ===Design Problems===<br /> <br /> :10.30. Consider the problem of storing &lt;math&gt;n&lt;/math&gt; books on shelves in a library. The order of the books is fixed by the cataloging system and so cannot be rearranged. Therefore, we can speak of a book &lt;math&gt;b_i&lt;/math&gt;, where &lt;math&gt;1 \leq i \leq n&lt;/math&gt;, that has a thickness &lt;math&gt;t_i&lt;/math&gt; and height &lt;math&gt;h_i&lt;/math&gt;. The length of each bookshelf at this library is &lt;math&gt;L&lt;/math&gt;.<br /> :Suppose all the books have the same height &lt;math&gt;h&lt;/math&gt; (i.e., &lt;math&gt;h = h_i&lt;/math&gt; for all &lt;math&gt;i&lt;/math&gt;) and the shelves are all separated by a distance greater than &lt;math&gt;h&lt;/math&gt;, so any book fits on any shelf. The greedy algorithm would fill the first shelf with as many books as we can until we get the smallest &lt;math&gt;i&lt;/math&gt; such that &lt;math&gt;b_i&lt;/math&gt; does not fit, and then repeat with subsequent shelves. Show that the greedy algorithm always finds the book placement that uses the minimum number of shelves, and analyze its time complexity.<br /> <br /> <br /> :[[10.31]]. This is a generalization of the previous problem. Now consider the case where the height of the books is not constant, but we have the freedom to adjust the height of each shelf to that of the tallest book on the shelf. Here the cost of a particular layout is the sum of the heights of the largest book on each shelf.<br /> :#Give an example to show that the greedy algorithm of stuffing each shelf as full as possible does not always give the minimum overall height.<br /> :#Give an algorithm for this problem, and analyze its time complexity. (Hint: use dynamic programming.)<br /> [[10.31|Solution]]<br /> <br /> <br /> :10.32. Consider a linear keyboard of lowercase letters and numbers, where the left-most 26 keys are the letters A–Z in order, followed by the digits 0–9 in order, followed by the 30 punctuation characters in a prescribed order, and ended on a blank. Assume you start with your left index finger on the “A” and your right index finger on the blank.<br /> :Give a dynamic programming algorithm that finds the most efficient way to type a given text of length &lt;math&gt;n&lt;/math&gt;, in terms of minimizing total movement of the fingers involved. For the text &lt;math&gt;ABABABAB . . . ABAB&lt;/math&gt;, this would involve shifting both fingers all the way to the left side of the keyboard. Analyze the complexity of your algorithm as a function of &lt;math&gt;n&lt;/math&gt; and &lt;math&gt;k&lt;/math&gt;, the number of keys on the keyboard.<br /> <br /> <br /> :[[10.33]]. You have come back from the future with an array &lt;math&gt;G&lt;/math&gt;, where &lt;math&gt;G[i]&lt;/math&gt; tells you the price of Google stock &lt;math&gt;i&lt;/math&gt; days from now, for &lt;math&gt;1 \leq i \leq n&lt;/math&gt;. You seek to use this information to maximize your profit, but are only permitted to complete at most one transaction (i.e. either buy one or sell one share of the stock) per day. Design an efficient algorithm to construct the buy–sell sequence to maximize your profit. Note that you cannot sell a share unless you currently own one.<br /> [[10.33|Solution]]<br /> <br /> <br /> :10.34. You are given a string of &lt;math&gt;n&lt;/math&gt; characters &lt;math&gt;S = s_1 . . . s_n&lt;/math&gt;, which you believe to be a compressed text document in which all spaces have been removed, like '''itwasthebestoftimes'''.<br /> :(a) You seek to reconstruct the document using a dictionary, which is available in the form of a Boolean function &lt;math&gt;dict(w)&lt;/math&gt;, where &lt;math&gt;dict(w)&lt;/math&gt; is true iff string &lt;math&gt;w&lt;/math&gt; is a valid word in the language. Give an &lt;math&gt;O(n^2)&lt;/math&gt; algorithm to determine whether string &lt;math&gt;S&lt;/math&gt; can be reconstituted as a sequence of valid words, assuming calls to &lt;math&gt;dict(w &lt;/math&gt; take unit time.<br /> :(b) Now assume you are given the dictionary as a set of &lt;math&gt;m&lt;/math&gt; words each of length at most l. Give an efficient algorithm to determine whether string &lt;math&gt;S&lt;/math&gt; can be reconstituted as a sequence of valid words, and its running time.<br /> <br /> <br /> :[[10.35]]. Consider the following two-player game, where you seek to get the biggest score. You start with an n-digit integer &lt;math&gt;N&lt;/math&gt;. With each move, you get to take either the first digit or the last digit from what is left of &lt;math&gt;N&lt;/math&gt;, and add that to your score, with your opponent then doing the same thing to the now smaller number. You continue taking turns removing digits until none are left. Give an efficient algorithm that finds the best possible score that the first player can get for a given digit string &lt;math&gt;N&lt;/math&gt;, assuming the second player is as smart as can be.<br /> [[10.35|Solution]]<br /> <br /> <br /> :10.36. Given an array of &lt;math&gt;n&lt;/math&gt; real numbers, consider the problem of finding the maximum sum in any contiguous subarray of the input. For example, in the array<br /> &lt;center&gt;&lt;math&gt;[31, -41, 59, 26, -53, 58, 97, -93, -23, 84]&lt;/math&gt;&lt;/center&gt;<br /> :the maximum is achieved by summing the third through seventh elements, where 59 + 26 + (−53) + 58 + 97 = 187. When all numbers are positive, the entire array is the answer, while when all numbers are negative, the empty array maximizes the total at 0.<br /> #Give a simple and clear &lt;math&gt; \Theta (n^2)&lt;/math&gt;-time algorithm to find the maximum contiguous subarray.<br /> #Now give a &lt;math&gt;\Theta (n)&lt;/math&gt;-time dynamic programming algorithm for this problem. To get partial credit, you may instead give a correct &lt;math&gt;O(n log n)&lt;/math&gt; divide-and-conquer algorithm.<br /> <br /> <br /> :[[10.37]]. Consider the problem of examining a string &lt;math&gt;x = x_1 x_2 \ldots x_n&lt;/math&gt; from an alphabet of &lt;math&gt;k&lt;/math&gt; symbols, and a multiplication table over this alphabet. Decide whether or not it is possible to parenthesize &lt;math&gt;x&lt;/math&gt; in such a way that the value of the resulting expression is &lt;math&gt;a&lt;/math&gt;, where &lt;math&gt;a&lt;/math&gt; belongs to the alphabet. The multiplication table is neither commutative or associative, so the order of multiplication matters.<br /> &lt;center&gt;<br /> &lt;math&gt;\begin{array}{c|ccc}<br /> &amp; a &amp; b &amp; c \\<br /> \hline<br /> a &amp; a &amp; c &amp; c \\<br /> b &amp; a &amp; a &amp; b \\<br /> c &amp; c &amp; c &amp; c \\<br /> \end{array}&lt;/math&gt;<br /> &lt;/center&gt;<br /> :For example, consider the above multiplication table and the string &lt;math&gt;bbbba&lt;/math&gt;. Parenthesizing it &lt;math&gt;(b(bb))(ba)&lt;/math&gt; gives &lt;math&gt;a&lt;/math&gt;, but &lt;math&gt;((((bb)b)b)a)&lt;/math&gt; gives &lt;math&gt;c&lt;/math&gt;. <br /> :Give an algorithm, with time polynomial in &lt;math&gt;n&lt;/math&gt; and &lt;math&gt;k&lt;/math&gt;, to decide whether such a parenthesization exists for a given string, multiplication table, and goal element.<br /> [[10.37|Solution]]<br /> <br /> <br /> :10.38. Let α and β be constants. Assume that it costs α to go left in a binary search tree, and β to go right. Devise an algorithm that builds a tree with optimal expected query cost, given keys &lt;math&gt;k_1, . . . , k_n&lt;/math&gt; and the probabilities that each will be searched &lt;math&gt;p_1, . . . , p_n&lt;/math&gt;.<br /> <br /> ===Interview Problems===<br /> <br /> :[[10.39]]. Given a set of coin denominations, find the minimum number of coins to make a certain amount of change.<br /> [[10.39|Solution]]<br /> <br /> <br /> :10.40. You are given an array of n numbers, each of which may be positive, negative, or zero. Give an efficient algorithm to identify the index positions &lt;math&gt;i&lt;/math&gt; and &lt;math&gt;j&lt;/math&gt; to obtain the maximum sum of the &lt;math&gt;i&lt;/math&gt;th through &lt;math&gt;j&lt;/math&gt;th numbers.<br /> <br /> <br /> :[[10.41]]. Observe that when you cut a character out of a magazine, the character on the reverse side of the page is also removed. Give an algorithm to determine whether you can generate a given string by pasting cutouts from a given magazine. Assume that you are given a function that will identify the character and its position on the reverse side of the page for any given character position.<br /> [[10.41|Solution]]<br /> <br /> <br /> Back to [[Chapter List]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Chapter_9&diff=455 Chapter 9 2020-10-01T18:10:14Z <p>Algowikiadmin: Protected &quot;Chapter 9&quot; ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))</p> <hr /> <div>=Combinatorial Search=<br /> <br /> ===Permutations===<br /> <br /> :[[9.1]]. A ''derangement'' is a permutation &lt;math&gt;p&lt;/math&gt; of &lt;math&gt;{1, . . . , n}&lt;/math&gt; such that no item is in its proper position, that is, &lt;math&gt;p_i 6= i&lt;/math&gt; for all &lt;math&gt;1 \leq i \leq n&lt;/math&gt;. Write an efficient backtracking program with pruning that constructs all the derangements of &lt;math&gt;n&lt;/math&gt; items.<br /> [[9.1|Soluiton]]<br /> <br /> <br /> :9.2. ''Multisets'' are allowed to have repeated elements. A multiset of n items may thus have fewer than &lt;math&gt;n!&lt;/math&gt; distinct permutations. For example, {1, 1, 2, 2} has only six distinct permutations: [1, 1, 2, 2], [1, 2, 1, 2], [1, 2, 2, 1], [2, 1, 1, 2], [2, 1, 2, 1], and [2, 2, 1, 1]. Design and implement an efficient algorithm for constructing all permutations of a multiset.<br /> <br /> <br /> :[[9.3]]. For a given a positive integer &lt;math&gt;n&lt;/math&gt;, find all permutations of the &lt;math&gt;2n&lt;/math&gt; elements of the multiset &lt;math&gt;S = {1, 1, 2, 2, 3, 3, . . . , n, n}&lt;/math&gt; such that for each integer from 1 to &lt;math&gt;n&lt;/math&gt; the number of intervening elements between its two appearances is equal to value of the element. For example, when &lt;math&gt;n = 3&lt;/math&gt; the two possible solutions are [3, 1, 2, 1, 3, 2] and [2, 3, 1, 2, 1, 3].<br /> [[9.3|Solution]]<br /> <br /> <br /> :9.4. Design and implement an algorithm for testing whether two graphs are isomorphic. The graph isomorphism problem is discussed in Section 19.9 (page 610). With proper pruning, graphs on hundreds of vertices can be tested in a reasonable time.<br /> <br /> <br /> :[[9.5]]. The set &lt;math&gt;{1, 2, 3, ..., n}&lt;/math&gt; contains a total of &lt;math&gt;n!&lt;/math&gt; distinct permutations. By listing and labeling all of the permutations in ascending lexicographic order, we get the following sequence for &lt;math&gt;n = 3&lt;/math&gt;: <br /> &lt;center&gt;&lt;math&gt;[123, 132, 213, 231, 312, 321]&lt;/math&gt;&lt;/center&gt;<br /> :Give an efficient algorithm that returns the &lt;math&gt;k&lt;/math&gt;th of &lt;math&gt;n!&lt;/math&gt; permutations in this see quence, for inputs &lt;math&gt;n&lt;/math&gt; and &lt;math&gt;k&lt;/math&gt;. For efficiency it should not construct the first &lt;math&gt;k - 1&lt;/math&gt; permutations in the process.<br /> [[9.5|Solution]]<br /> <br /> ===Backtracking===<br /> <br /> :9.6. Generate all structurally distinct binary search trees that store values &lt;math&gt;1 . . . n&lt;/math&gt;, for a given value of &lt;math&gt;n&lt;/math&gt;.<br /> <br /> <br /> :[[9.7]]. Implement an algorithm to print all valid (meaning properly opened and closed) sequences of n pairs of parentheses.<br /> [[9.7|Solution]]<br /> <br /> <br /> :9.8. Generate all possible topological orderings of a given DAG.<br /> <br /> <br /> :[[9.9]]. Given a specified total t and a multiset &lt;math&gt;S&lt;/math&gt; of &lt;math&gt;n&lt;/math&gt; integers, find all distinct subsets from &lt;math&gt;S&lt;/math&gt; whose elements add up to &lt;math&gt;t&lt;/math&gt;. For example, if &lt;math&gt;t = 4&lt;/math&gt; and &lt;math&gt;S = {4, 3, 2, 2, 1, 1}&lt;/math&gt;, then there are four different sums that equal &lt;math&gt;t: 4, 3 + 1, 2 + 2&lt;/math&gt;, and &lt;math&gt;2 + 1 + 1&lt;/math&gt;. A number can be used within a sum up to the number of times it appears in &lt;math&gt;S&lt;/math&gt;, and a single number counts as a sum.<br /> [[9.9|Solution]]<br /> <br /> <br /> :9.10. Given a specified total &lt;math&gt;t&lt;/math&gt; and a multiset &lt;math&gt;S&lt;/math&gt; of &lt;math&gt;n&lt;/math&gt; integers, find all distinct subsets from &lt;math&gt;S&lt;/math&gt; whose elements add up to &lt;math&gt;t&lt;/math&gt;. For example, if &lt;math&gt;t = 4&lt;/math&gt; and &lt;math&gt;S = {4, 3, 2, 2, 1, 1}&lt;/math&gt;, then there are four different sums that equal &lt;math&gt;t: 4, 3 + 1, 2 + 2&lt;/math&gt;, and &lt;math&gt;2 + 1 + 1&lt;/math&gt;. A number can be used within a sum up to the number of times it appears in &lt;math&gt;S&lt;/math&gt;, and a single number counts as a sum.<br /> <br /> <br /> :[[9.11]]. A team assignment of &lt;math&gt;n = 2k&lt;/math&gt; players is a partitioning of them into two teams with exactly &lt;math&gt;k&lt;/math&gt; people per team. For example, if the players are named &lt;math&gt;{A, B, C, D}&lt;/math&gt;, there are three distinct ways to partition them into two equal teams: &lt;math&gt;{{A, B}, {C, D}}, {{A, C}, {B, D}}, and {{A, D}, {B, C}}&lt;/math&gt;. <br /> :(a) List the 10 possible team assignments for &lt;math&gt;n = 6&lt;/math&gt; players.<br /> :(b) Give an efficient back-tracking algorithm to construct all possible team assignments. Be sure to avoid repeating any solution.<br /> [[9.11|Solution]]<br /> <br /> <br /> :9.12. Given an alphabet &lt;math&gt;\Sigma&lt;/math&gt;, a set of forbidden strings &lt;math&gt;S&lt;/math&gt;, and a target length &lt;math&gt;n&lt;/math&gt;, give an algorithm to construct a string of length &lt;math&gt;n&lt;/math&gt; on &lt;math&gt;\Sigma&lt;/math&gt; without any element of &lt;math&gt;S&lt;/math&gt; as a substring. For &lt;math&gt;\Sigma = {0, 1}, S = {01, 10}&lt;math&gt;, and &lt;math&gt;n = 4&lt;/math&gt;, the two possible solutions are 0000 and 1111. For &lt;math&gt;S = {0, 11}&lt;/math&gt; and &lt;math&gt;n = 4&lt;/math&gt;, no such string exists.<br /> <br /> <br /> :[[9.13]]. In the &lt;math&gt;k&lt;/math&gt;-partition problem, we need to partition a multiset of positive integers into &lt;math&gt;k&lt;/math&gt; disjoint subsets that have equal sum. Design and implement an algorithm for solving the &lt;math&gt;k&lt;/math&gt;-partition problem.<br /> [[9.13|Solution]]<br /> <br /> <br /> :9.14. You are given a weighted directed graph &lt;math&gt;G&lt;/math&gt; with &lt;math&gt;n&lt;/math&gt; vertices and &lt;math&gt;m&lt;/math&gt; edges. The mean weight of a cycle is the sum of its edge weights divided by the number of its edges. Find a cycle in &lt;math&gt;G&lt;/math&gt; of minimum mean weight.<br /> <br /> <br /> :[[9.15]]. In the turnpike reconstruction problem, you are given a multiset &lt;math&gt;D&lt;/math&gt; of &lt;math&gt;n(n - 1)/2&lt;/math&gt; distances. The problem is to place &lt;math&gt;n&lt;/math&gt; points on the line such that their pairwise distances are &lt;math&gt;D&lt;/math&gt;. For example, the distances &lt;math&gt;D = {1, 2, 3, 4, 5, 6}&lt;/math&gt; can be obtained by placing the second point 1 unit from the first, the third point 3 from the second, and the fourth point 2 from the third. Design and implement an efficient algorithm to find all solutions to the turnpike reconstruction problem. Exploit additive constraints when possible to accelerate the search. With proper pruning, problems with hundreds of points can be solved in reasonable time.<br /> [[9.15|Solution]]<br /> <br /> ===Games and Puzzles===<br /> <br /> :9.16. Anagrams are rearrangements of the letters of a word or phrase into a different word or phrase. Sometimes the results are quite striking. For example, “MANY VOTED BUSH RETIRED” is an anagram of “TUESDAY NOVEMBER THIRD,” which correctly predicted the result of the 1992 US presidential election. Design and implement an algorithm for finding anagrams using combinatorial search and a dictionary.<br /> <br /> <br /> :[[9.17]]. Construct all sequences of moves that a knight on an &lt;math&gt;n * n&lt;/math&gt; chessboard can make where the knight visits every square only once.<br /> [[9.17|Solution]]<br /> <br /> <br /> :9.18. A Boggle board is an &lt;math&gt;n * m&lt;/math&gt; grid of characters. For a given board, we seek to find all possible words that can be formed by a sequence of adjacent characters on the board, without repetition. For example, the board:<br /> &lt;center&gt;&lt;math&gt;\begin{matrix}<br /> e &amp; t &amp; h &amp; t\\<br /> n &amp; d &amp; t &amp; i\\<br /> a &amp; i &amp; h &amp; n\\<br /> r &amp; h &amp; u &amp; b<br /> \end{matrix}&lt;/math&gt;&lt;/center&gt;<br /> :contains words like tide, dent, raid, and hide. Design an algorithm to construct the most words for a given board &lt;math&gt;B&lt;/math&gt; consistent with a dictionary &lt;math&gt;D&lt;/math&gt;.<br /> <br /> <br /> :[[9.19]]. A Babbage square is a grid of words that reads the same across as it does down. Given a &lt;math&gt;k&lt;/math&gt;-letter word &lt;math&gt;w&lt;/math&gt; and a dictionary of &lt;math&gt;n&lt;/math&gt; words, find all Babbage squares starting with that word. For example, two squares for the word hair are:<br /> &lt;center&gt;&lt;math&gt;\begin{vmatrix}<br /> h &amp; a &amp; i &amp; r\\<br /> a &amp; i &amp; d &amp; e\\<br /> i &amp; d &amp; l &amp; e\\<br /> r &amp; e &amp; e &amp; f<br /> \end{vmatrix}\begin{vmatrix}<br /> h &amp; a &amp; i &amp; r\\<br /> a &amp; l &amp; t &amp; o\\<br /> i &amp; t &amp; e &amp; m\\<br /> r &amp; o &amp; m &amp; b<br /> \end{vmatrix}<br /> &lt;/math&gt;&lt;/center&gt;<br /> [[9.19|Solution]]<br /> <br /> <br /> :9.20. Show that you can solve any given Sudoku puzzle by finding the minimum vertex coloring of a specific, appropriately constructed 9 × 9 + 9 vertex graph.<br /> <br /> ===Combinatorial Optimization=== <br /> For problems 9-21 to 9-27, implement a combinatorial search program to solve it for small instances. How well does your program perform in practice?<br /> <br /> <br /> :[[9.21]]. Design and implement an algorithm for solving the bandwidth minimization problem discussed in Section 16.2 (page 470).<br /> [[9.21|Solution]]<br /> <br /> <br /> :9.22. Design and implement an algorithm for solving the maximum satisfiability problem discussed in Section 17.10 (page 537).<br /> <br /> <br /> :[[9.23]]. Design and implement an algorithm for solving the maximum clique problem discussed in Section 19.1 (page 586).<br /> [[9.23|Solution]]<br /> <br /> <br /> :9.24. Design and implement an algorithm for solving the minimum vertex coloring problem discussed in Section 19.7 (page 604).<br /> <br /> <br /> :[[9.25]]. Design and implement an algorithm for solving the minimum edge coloring problem discussed in Section 19.8 (page 608).<br /> [[9.25|Solution]]<br /> <br /> <br /> :9.26. Design and implement an algorithm for solving the minimum feedback vertex set problem discussed in Section 19.11 (page 618).<br /> <br /> <br /> :[[9.27]]. Design and implement an algorithm for solving the set cover problem discussed in Section 21.1 (page 678).<br /> [[9.27|Solution]]<br /> <br /> ===Interview Problems===<br /> <br /> :9.28. Write a function to find all permutations of the letters in a given string.<br /> <br /> <br /> :[[9.29]]. Implement an efficient algorithm for listing all &lt;math&gt;k&lt;/math&gt;-element subsets of &lt;math&gt;n&lt;/math&gt; items.<br /> [[9.29|Solution]]<br /> <br /> <br /> :9.30. An anagram is a rearrangement of the letters in a given string into a sequence of dictionary words, like ''Steven Skiena'' into ''Vainest Knees''. Propose an algorithm to construct all the anagrams of a given string.<br /> <br /> <br /> :[[9.31]]. Telephone keypads have letters on each numerical key. Write a program that generates all possible words resulting from translating a given digit sequence (e.g. 145345) into letters.<br /> [[9.31|Solution]]<br /> <br /> <br /> :9.32. You start with an empty room and a group of &lt;math&gt;n&lt;/math&gt; people waiting outside. At each step, you may either admit one person into the room, or let one out. Can you arrange a sequence of &lt;math&gt;2n&lt;/math&gt; steps, so that every possible combination of people is achieved exactly once?<br /> <br /> <br /> :[[9.33]]. Use a random number generator (rng04) that generates numbers from {0, 1, 2, 3, 4} with equal probability to write a random number generator that generates numbers from 0 to 7 (rng07) with equal probability. What is the expected number of calls to rng04 per call of rng07?<br /> [[9.33|Solution]]<br /> <br /> <br /> <br /> Back to [[Chapter List]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Chapter_8&diff=454 Chapter 8 2020-10-01T18:09:48Z <p>Algowikiadmin: Protected &quot;Chapter 8&quot; ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))</p> <hr /> <div>=Weighted Graph Algorithms=<br /> <br /> ===Simulating Graph Algorithms===<br /> <br /> :[[8.1]]. For the graphs in Problem 7-1:<br /> :(a) Draw the spanning forest after every iteration of the main loop in Kruskal’s algorithm.<br /> :(b) Draw the spanning forest after every iteration of the main loop in Prim’s algorithm.<br /> :(c) Find the shortest-path spanning tree rooted in &lt;math&gt;A&lt;/math&gt;.<br /> :(d) Compute the maximum flow from &lt;math&gt;A&lt;/math&gt; to &lt;math&gt;H&lt;/math&gt;.<br /> [[8.1|Solution]]<br /> <br /> ===Minimum Spanning Tree===<br /> <br /> :8.2. Is the path between two vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample.<br /> <br /> <br /> :[[8.3]]. Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the path between a pair of vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample.<br /> [[8.3|Solution]]<br /> <br /> <br /> :8.4. Can Prim’s and Kruskal’s algorithms yield different minimum spanning trees? Explain why or why not.<br /> <br /> <br /> :[[8.5]]. Does either Prim’s or Kruskal’s algorithm work if there are negative edge weights? Explain why or why not.<br /> [[8.5|Solution]]<br /> <br /> <br /> :8.6. (a) Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the ''minimum spanning tree'' of this graph unique? Give a proof or a counterexample.<br /> :(b) Again, assume that all edges in the graph have distinct edge weights (i.e. no pair of edges have the same weight). Is the ''shortest-path spanning tree'' of this graph unique? Give a proof or a counterexample.<br /> <br /> <br /> :[[8.7]]. Suppose we are given the minimum spanning tree &lt;math&gt;T&lt;/math&gt; of a given graph &lt;math&gt;G&lt;/math&gt; (with &lt;math&gt;n&lt;/math&gt; vertices and &lt;math&gt;m&lt;/math&gt; edges) and a new edge &lt;math&gt;e = (u, v)&lt;/math&gt; of weight &lt;math&gt;w&lt;/math&gt; that we will add to &lt;math&gt;G&lt;/math&gt;. Give an efficient algorithm to find the minimum spanning tree of the graph &lt;math&gt;G + e&lt;/math&gt;. Your algorithm should run in &lt;math&gt;O(n)&lt;/math&gt; time to receive full credit.<br /> [[8.7|Solution]]<br /> <br /> <br /> :8.8. (a) Let &lt;math&gt;T&lt;/math&gt; be a minimum spanning tree of a weighted graph &lt;math&gt;G&lt;/math&gt;. Construct a new graph &lt;math&gt;G'&lt;/math&gt; by adding a weight of &lt;math&gt;k&lt;/math&gt; to every edge of &lt;math&gt;G&lt;/math&gt;. Do the edges of &lt;math&gt;T&lt;/math&gt; form a minimum spanning tree of &lt;math&gt;G'&lt;/math&gt;? Prove the statement or give a counterexample.<br /> :(b) Let &lt;math&gt;P = {s, . . . , t}&lt;/math&gt; describe a shortest path between vertices &lt;math&gt;s&lt;/math&gt; and &lt;math&gt;t&lt;/math&gt; of a weighted graph &lt;math&gt;G&lt;/math&gt;. Construct a new graph &lt;math&gt;G'&lt;/math&gt; by adding a weight of &lt;math&gt;k&lt;/math&gt; to every edge of &lt;math&gt;G&lt;/math&gt;. Does &lt;math&gt;P&lt;/math&gt; describe a shortest path from &lt;math&gt;s&lt;/math&gt; to &lt;math&gt;t&lt;/math&gt; in &lt;math&gt;G'&lt;/math&gt;? Prove the statement or give a counterexample.<br /> <br /> <br /> :[[8.9]]. Devise and analyze an algorithm that takes a weighted graph &lt;math&gt;G&lt;/math&gt; and finds the smallest change in the cost to a non-minimum spanning tree edge that would cause a change in the minimum spanning tree of &lt;math&gt;G&lt;/math&gt;. Your algorithm must be correct and run in polynomial time.<br /> [[8.9|Solution]]<br /> <br /> <br /> :8.10. Consider the problem of finding a minimum-weight connected subset &lt;math&gt;T&lt;/math&gt; of edges from a weighted connected graph &lt;math&gt;G&lt;/math&gt;. The weight of &lt;math&gt;T&lt;/math&gt; is the sum of all the edge weights in &lt;math&gt;T&lt;/math&gt;.<br /> :(a) Why is this problem not just the minimum spanning tree problem? (Hint: think negative weight edges.)<br /> :(b) Give an efficient algorithm to compute the minimum-weight connected subset &lt;math&gt;T&lt;/math&gt;.<br /> <br /> <br /> :[[8.11]]. Let &lt;math&gt;T = (V, E')&lt;/math&gt; be a minimum spanning tree of a given graph &lt;math&gt;G = (V, E)&lt;/math&gt; with positive edge weights. Now suppose the weight of a particular edge &lt;math&gt;e \in E&lt;/math&gt; is modified from &lt;math&gt;w(e)&lt;/math&gt; to a new value &lt;math&gt;\hat{w}(e)&lt;/math&gt;. We seek to update the minimum spanning tree &lt;math&gt;T&lt;/math&gt; to reflect this change without recomputing the entire tree from scratch. For each of the following four cases, give a linear-time algorithm to update the tree:<br /> :(a)&lt;math&gt;e \notin E'&lt;/math&gt; and &lt;math&gt; \hat{w}(e) &gt; w(e)&lt;/math&gt;<br /> :(b) &lt;math&gt;e \notin E'&lt;/math&gt; and &lt;math&gt;\hat{w}(e) &lt; w(e)&lt;/math&gt;<br /> :(c) &lt;math&gt;e \in E'&lt;/math&gt; and &lt;math&gt;\hat{w}(e) &lt; w(e)&lt;/math&gt;<br /> :(d) &lt;math&gt;e \in E' &lt;/math&gt; and &lt;math&gt;\hat{w}(e) &gt; w(e)&lt;/math&gt;<br /> [[8.11|Solution]]<br /> <br /> <br /> :8.12. Let &lt;math&gt;G=(V,E)&lt;/math&gt; be an undirected graph. A set &lt;math&gt;F \subseteq E&lt;/math&gt; of edges is called a ''feedback-edge set'' if every cycle of &lt;math&gt;G&lt;/math&gt; has at least one edge in &lt;math&gt;F&lt;/math&gt;.<br /> :(a)Suppose that &lt;math&gt;G&lt;/math&gt; is unweighted. Design an efficient algorithm to find a minimum-size feedback-edge set.<br /> :(b)Suppose that &lt;math&gt;G&lt;/math&gt; is a weighted undirected graph with positive edge weights. Design an efficient algorithm to find a minimum-weight feedback-edge set.<br /> <br /> ===Union Find===<br /> <br /> :[[8.13]]. Devise an efficient data structure to handle the following operations on a weighted directed graph:<br /> :(a)Merge two given components.<br /> :(b)Locate which component contains a given vertex &lt;math&gt;v&lt;/math&gt;.<br /> :(c)Retrieve a minimum edge from a given component.<br /> [[8.13|Solution]]<br /> <br /> <br /> :8.14. Design a data structure that can perform a sequence of, &lt;math&gt;m&lt;/math&gt; ''union'' and ''find'' operations on a universal set of &lt;math&gt;n&lt;/math&gt; elements, consisting of a sequence of all ''unions'' followed by a sequence of all ''finds'', in time &lt;math&gt;O(m+n)&lt;/math&gt;.<br /> <br /> ===Shortest Paths===<br /> <br /> :[[8.15]]. The ''single-destination shortest path'' problem for a directed graph seeks the shortest path ''from'' every vertex to a specified vertex &lt;math&gt;v&lt;/math&gt;. Give an efficient algorithm to solve the single-destination shortest paths problem.<br /> [[8.15|Solution]]<br /> <br /> <br /> :8.16. Let &lt;math&gt;G=(V,E)&lt;/math&gt; be an undirected weighted graph, and let &lt;math&gt;T&lt;/math&gt; be the shortest-path spanning tree rooted at a vertex &lt;math&gt;v&lt;/math&gt;. Suppose now that all the edge weights in &lt;math&gt;G&lt;/math&gt; are increased by a constant number &lt;math&gt;k&lt;/math&gt;. Is &lt;math&gt;T&lt;/math&gt; still the shortest-path spanning tree from &lt;math&gt;v&lt;/math&gt;?<br /> <br /> <br /> :[[8.17]]. (a)Give an example of a weighted connected graph &lt;math&gt;G=(V,E)&lt;/math&gt; and a vertex &lt;math&gt;v&lt;/math&gt;, such that the minimum spanning tree of &lt;math&gt;G&lt;/math&gt; is the same as the shortest-path spanning tree rooted at &lt;math&gt;v&lt;/math&gt;.<br /> :(b)Give an example of a weighted connected directed graph &lt;math&gt;G=(V,E)&lt;/math&gt; and a vertex &lt;math&gt;v&lt;/math&gt;, such that the minimum-cost spanning tree of &lt;math&gt;G&lt;/math&gt; is very different from the shortest-path spanning tree rooted at &lt;math&gt;v&lt;/math&gt;.<br /> :(c)Can the two trees be completely disjointed?<br /> [[8.17|Solution]]<br /> <br /> <br /> :8.18. Either prove the following or give a counterexample:<br /> :(a)Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path?<br /> :(b)Suppose that the minimum spanning tree of the graph is unique. Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path?<br /> <br /> <br /> :[[8.19]]. Give an efficient algorithm to find the shortest path from &lt;math&gt;x&lt;/math&gt; to &lt;math&gt;y&lt;/math&gt; in an undirected weighted graph &lt;math&gt;G = (V, E)&lt;/math&gt; with positive edge weights, subject to the constraint that this path must pass through a particular vertex &lt;math&gt;z&lt;/math&gt;.<br /> [[8.19|Solution]]<br /> <br /> <br /> :8.20. In certain graph problems, vertices have can have weights instead of or in addition to the weights of edges. Let &lt;math&gt;C_v&lt;/math&gt; be the cost of vertex &lt;math&gt;v&lt;/math&gt;, and &lt;math&gt;C_{(x,y)}&lt;/math&gt; the cost of the edge &lt;math&gt;(x,y)&lt;/math&gt;. This problem is concerned with finding the cheapest path between vertices &lt;math&gt;a&lt;/math&gt; and &lt;math&gt;b&lt;/math&gt; in a graph &lt;math&gt;G&lt;/math&gt;. The cost of a path is the sum of the costs of the edges and vertices encountered on the path.<br /> :(a)Suppose that each edge in the graph has a weight of zero (while non-edges have a cost of &lt;math&gt;\infty&lt;/math&gt;). Assume that &lt;math&gt;C_v = 1&lt;/math&gt; for all vertices &lt;math&gt;1 \leq v \leq n&lt;/math&gt; (i.e., all vertices have the same cost). Give an ''efficient'' algorithm to find the cheapest path from &lt;math&gt;a&lt;/math&gt; to &lt;math&gt;b&lt;/math&gt; and its time complexity.<br /> :(b)Now suppose that the vertex costs are not constant (but are all positive) and the edge costs remain as above. Give an ''efficient'' algorithm to find the cheapest path from &lt;math&gt;a&lt;/math&gt; to &lt;math&gt;b&lt;/math&gt; and its time complexity.<br /> :(c)Now suppose that both the edge and vertex costs are not constant (but are all positive). Give an ''efficient'' algorithm to find the cheapest path from &lt;math&gt;a&lt;/math&gt; to &lt;math&gt;b&lt;/math&gt; and its time complexity.<br /> <br /> <br /> :[[8.21]]. Give an &lt;math&gt;O(n^3)&lt;/math&gt; algorithm that takes an &lt;math&gt;n&lt;/math&gt;-vertex directed graph &lt;math&gt;G&lt;/math&gt; with positive edge lengths, and returns the length of the shortest cycle in the graph. This length is &lt;math&gt;\infty&lt;/math&gt; in the case of an acyclic graph.<br /> [[8.21|Solution]]<br /> <br /> <br /> :8.22. A highway network is represented by a weighted graph &lt;math&gt;G&lt;/math&gt;, with edges corresponding to roads and vertices corresponding to road intersections. Each road is labeled with the maximum possible height of vehicles that can pass through the road. Give an efficient algorithm to compute the maximum possible height of vehicles that can successfully travel from &lt;math&gt;s&lt;/math&gt; to &lt;math&gt;t&lt;/math&gt;. What is the runtime of your algorithm?<br /> <br /> <br /> :[[8.23]]. You are given a directed graph &lt;math&gt;G&lt;/math&gt; with possibly negative weighted edges, in which the shortest path between any two vertices is guaranteed to have at most &lt;math&gt;k&lt;/math&gt; edges. Give an algorithm that finds the shortest path between two vertices &lt;math&gt;u&lt;/math&gt; and &lt;math&gt;v&lt;/math&gt; in &lt;math&gt;O(k * (n + m))&lt;/math&gt; time.<br /> [[8.23|Solution]]<br /> <br /> <br /> :8.24. Can we solve the single-source ''longest''-path problem by changing ''minimum'' to ''maximum'' in Dijkstra’s algorithm? If so, then prove your algorithm correct. If not, then provide a counterexample.<br /> <br /> <br /> :[[8.25]]. Let &lt;math&gt;G = (V, E)&lt;/math&gt; be a weighted acyclic directed graph with possibly negative edge weights. Design a linear-time algorithm to solve the single-source shortest-path problem from a given source &lt;math&gt;v&lt;/math&gt;.<br /> [[8.25|Solution]]<br /> <br /> <br /> :8.26. Let &lt;math&gt;G=(V,E)&lt;/math&gt; be a directed weighted graph such that all the weights are positive. Let &lt;math&gt;v&lt;/math&gt; and &lt;math&gt;w&lt;/math&gt; be two vertices in &lt;math&gt;G&lt;/math&gt; and &lt;math&gt;k \leq |V|&lt;/math&gt; be an integer. Design an algorithm to find the shortest path from &lt;math&gt;v&lt;/math&gt; to &lt;math&gt;w&lt;/math&gt; that contains exactly &lt;math&gt;k&lt;/math&gt; edges. Note that the path need not be simple.<br /> <br /> <br /> :[[8.27]]. ''Arbitrage'' is the use of discrepancies in currency-exchange rates to make a profit. For example, there may be a small window of time during which 1 U.S. dollar buys 0.75 British pounds, 1 British pound buys 2 Australian dollars, and 1 Australian dollar buys 0.70 U.S. dollars. At such a time, a smart trader can trade one U.S. dollar and end up with &lt;math&gt;0.75 \times 2 \times 0.7 = 1.05&lt;/math&gt; U.S. dollars---a profit of 5%. Suppose that there are &lt;math&gt;n&lt;/math&gt; currencies &lt;math&gt;c_1,...,c_n&lt;/math&gt; and an &lt;math&gt;n \times n&lt;/math&gt; table &lt;math&gt;R&lt;/math&gt; of exchange rates, such that one unit of currency &lt;math&gt;c_i&lt;/math&gt; buys &lt;math&gt;R[i,j]&lt;/math&gt; units of currency &lt;math&gt;c_j&lt;/math&gt;. Devise and analyze an algorithm to determine the maximum value of &lt;math&gt; R[c_1,c_{i1}] \cdot R[c_{i1},c_{i2}] \cdots R[c_{i{k-1}},c_{ik}] \cdot R[c_{ik},c_1] &lt;/math&gt; Hint: think all-pairs shortest path.<br /> [[8.27|Solution]]<br /> <br /> ===Network Flow and Matching===<br /> <br /> :8.28. A matching in a graph is a set of disjoint edges—that is, edges that do not have common vertices. Give a linear-time algorithm to find a maximum matching in a tree.<br /> <br /> <br /> :[[8.29]]. An edge cover of an undirected graph &lt;math&gt;G = (V, E)&lt;/math&gt; is a set of edges such that each vertex in the graph is incident to at least one edge from the set. Give an efficient algorithm, based on matching, to find the minimum-size edge cover for &lt;math&gt;G&lt;/math&gt;.<br /> [[8.29|Solution]]<br /> <br /> <br /> Back to [[Chapter List]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Chapter_7&diff=453 Chapter 7 2020-10-01T18:09:33Z <p>Algowikiadmin: /* Simulating Graph Algorithms */</p> <hr /> <div>=Graph Traversal=<br /> <br /> ===Simulating Graph Algorithms===<br /> <br /> :[[7.1]]. For the following graphs &lt;math&gt;G_1&lt;/math&gt; (left) and &lt;math&gt;G_2&lt;/math&gt; (right):<br /> :(see book for figures)<br /> <br /> :(a) Report the order of the vertices encountered on a breadth-first search starting from vertex &lt;math&gt;A&lt;/math&gt;. Break all ties by picking the vertices in alphabetical order (i.e., &lt;math&gt;A&lt;/math&gt; before &lt;math&gt;Z&lt;/math&gt;).<br /> :(b) Report the order of the vertices encountered on a depth-first search starting from vertex &lt;math&gt;A&lt;/math&gt;. Break all ties by picking the vertices in alphabetical order (i.e., &lt;math&gt;A&lt;/math&gt; before &lt;math&gt;Z&lt;/math&gt;).<br /> [[7.1|Solution]]<br /> <br /> <br /> :7.2. Do a topological sort of the following graph &lt;math&gt;G&lt;/math&gt;:<br /> :(see book for figure)<br /> <br /> ===Traversal===<br /> <br /> :[[7.3]]. Prove that there is a unique path between any pair of vertices in a tree.<br /> [[7.3|Solution]]<br /> <br /> <br /> :7.4. Prove that in a breadth-first search on a undirected graph &lt;math&gt;G&lt;/math&gt;, every edge is either a tree edge or a cross edge, where &lt;math&gt;x&lt;/math&gt; is neither an ancestor nor descendant of &lt;math&gt;y&lt;/math&gt; in cross edge &lt;math&gt;(x, y)&lt;/math&gt;.<br /> <br /> <br /> :[[7.5]]. Give a linear algorithm to compute the chromatic number of graphs where each vertex has degree at most 2. Any bipartite graph has a chromatic number of 2. Must such graphs be bipartite?<br /> [[7.5|Solution]]<br /> <br /> <br /> :7.6. You are given a connected, undirected graph &lt;math&gt;G&lt;/math&gt; with &lt;math&gt;n&lt;/math&gt; vertices and &lt;math&gt;m&lt;/math&gt; edges. Give an &lt;math&gt;O(n + m)&lt;/math&gt; algorithm to identify an edge you can remove from &lt;math&gt;G&lt;/math&gt; while still leaving &lt;math&gt;G&lt;/math&gt; connected, if one exists. Can you reduce the running time to &lt;math&gt;O(n)&lt;/math&gt;?<br /> <br /> <br /> :[[7.7]]. In breadth-first and depth-first search, an undiscovered node is marked ''discovered'' when it is first encountered, and marked ''processed'' when it has been completely searched. At any given moment, several nodes might be simultaneously in the ''discovered'' state.<br /> :(a) Describe a graph on &lt;math&gt;n&lt;/math&gt; vertices and a particular starting vertex &lt;math&gt;v&lt;/math&gt; such that &lt;math&gt;\Theta(n)&lt;/math&gt; nodes are simultaneously in the ''discovered'' state during a ''breadth-first search'' starting from &lt;math&gt;v&lt;/math&gt;.<br /> :(b) Describe a graph on &lt;math&gt;n&lt;/math&gt; vertices and a particular starting vertex &lt;math&gt;v&lt;/math&gt; such that &lt;math&gt;\Theta(n)&lt;/math&gt; nodes are simultaneously in the ''discovered'' state during a ''depth-first search'' starting from &lt;math&gt;v&lt;/math&gt;.<br /> :(c) Describe a graph on &lt;math&gt;n&lt;/math&gt; vertices and a particular starting vertex &lt;math&gt;v&lt;/math&gt; such that at some point &lt;math&gt;\Theta(n)&lt;/math&gt; nodes remain ''undiscovered'', while &lt;math&gt;\Theta(n)&lt;/math&gt; nodes have been ''processed'' during a ''depth-first search'' starting from &lt;math&gt;v&lt;/math&gt;.(Note, there may also be ''discovered'' nodes.)<br /> [[7.7|Solution]]<br /> <br /> <br /> :7.8. Given pre-order and in-order traversals of a binary tree (discussed in Section 3.4.1), is it possible to reconstruct the tree? If so, sketch an algorithm to do it. If not, give a counterexample. Repeat the problem if you are given the pre-order and post-order traversals.<br /> <br /> <br /> :[[7.9]]. Present correct and efficient algorithms to convert an undirected graph &lt;math&gt;G&lt;/math&gt; between the following graph data structures. You must give the time complexity of each algorithm, assuming &lt;math&gt;n&lt;/math&gt; vertices and &lt;math&gt;m&lt;/math&gt; edges.<br /> :(a)Convert from an adjacency matrix to adjacency lists.<br /> :(b)Convert from an adjacency list to an incidence matrix. An incidence matrix &lt;math&gt;M&lt;/math&gt; has a row for each vertex and a column for each edge, such that &lt;math&gt;M[i,j]=1&lt;/math&gt; if vertex &lt;math&gt;i&lt;/math&gt; is part of edge &lt;math&gt;j&lt;/math&gt;, otherwise &lt;math&gt;M[i,j] = 0&lt;/math&gt;.<br /> :(c)Convert from an incidence matrix to adjacency lists.<br /> [[7.9|Solution]]<br /> <br /> <br /> :7.10. Suppose an arithmetic expression is given as a tree. Each leaf is an integer and each internal node is one of the standard arithmetical operations &lt;math&gt;(+, -, *, /)&lt;/math&gt;. For example, the expression &lt;math&gt;2+3*4+(3*4)/5&lt;/math&gt; is represented by the tree in Figure 7.17(a). Give an &lt;math&gt;O(n)&lt;/math&gt; algorithm for evaluating such an expression, where there are &lt;math&gt;n&lt;/math&gt; nodes in the tree.<br /> <br /> <br /> :[[7.11]]. Suppose an arithmetic expression is given as a DAG (directed acyclic graph) with common subexpressions removed. Each leaf is an integer and each internal node is one of the standard arithmetical operations &lt;math&gt;(+,-,*,/)&lt;/math&gt;. For example, the expression &lt;math&gt;2+3*4+(3*4)/5&lt;/math&gt; is represented by the DAG in Figure (see book)(b). Give an &lt;math&gt;O(n+m)&lt;/math&gt; algorithm for evaluating such a DAG, where there are &lt;math&gt;n&lt;/math&gt; nodes and &lt;math&gt;m&lt;/math&gt; edges in the DAG. Hint: modify an algorithm for the tree case to achieve the desired efficiency.<br /> [[7.11|Solution]]<br /> <br /> <br /> :7.12. The war story of Section 7.4 (page 210) describes an algorithm for constructing the dual graph of the triangulation efficiently, although it does not guarantee linear time. Give a worst-case linear algorithm for the problem.<br /> <br /> ===Applications===<br /> <br /> :[[7.13]]. The Chutes and Ladders game has a board with n cells where you seek to travel from cell 1 to cell &lt;math&gt;n&lt;/math&gt;. To move, a player throws a six-sided dice to determine how many cells forward they move. This board also contains chutes and ladders that connect certain pairs of cells. A player who lands on the mouth of a chute immediately falls back down to the cell at the other end. A player who lands on the base of a ladder immediately travels up to the cell at the top of the ladder. Suppose you have rigged the dice to give you full control of the number for each roll. Give an efficient algorithm to find the minimum number of dice throws to win.<br /> [[7.13|Solution]]<br /> <br /> <br /> :7.14. Plum blossom poles are a Kung Fu training technique, consisting of &lt;math&gt;n&lt;/math&gt; large posts partially sunk into the ground, with each pole &lt;math&gt;p_i&lt;/math&gt; at position &lt;math&gt;(x_i, y_i)&lt;/math&gt;. Students practice martial arts techniques by stepping from the top of one pole to the top of another pole. In order to keep balance, each step must be more than &lt;math&gt;d&lt;/math&gt; meters but less than &lt;math&gt;2d&lt;/math&gt; meters. Give an efficient algorithm to find a safe path from pole &lt;math&gt;p_s&lt;/math&gt; to &lt;math&gt;p_t&lt;/math&gt; if it exists.<br /> <br /> <br /> :[[7.15]]. You are planning the seating arrangement for a wedding given a list of guests, &lt;math&gt;V&lt;/math&gt;. For each guest &lt;math&gt;g&lt;/math&gt; you have a list of all other guests who are on bad terms with them. Feelings are reciprocal: if &lt;math&gt;h&lt;/math&gt; is on bad terms with &lt;math&gt;g&lt;/math&gt;, then &lt;math&gt;g&lt;/math&gt; is on bad terms with &lt;math&gt;h&lt;/math&gt;. Your goal is to arrange the seating such that no pair of guests sitting at the same table are on bad terms with each other. There will be only two tables at the wedding. Give an efficient algorithm to find an acceptable seating arrangement if one exists.<br /> [[7.15|Solution]]<br /> <br /> ===Algorithm Design===<br /> <br /> :7.16. The ''square'' of a directed graph &lt;math&gt;G=(V,E)&lt;/math&gt; is the graph &lt;math&gt;G^2=(V,E^2)&lt;/math&gt; such that &lt;math&gt;(u,w) \in E^2&lt;/math&gt; iff there exists &lt;math&gt;v \in V&lt;/math&gt; such that &lt;math&gt;(u,v) \in E&lt;/math&gt; and &lt;math&gt;(v,w) \in E&lt;/math&gt;; i.e., there is a path of exactly two edges from &lt;math&gt;u&lt;/math&gt; to &lt;math&gt;w&lt;/math&gt;.<br /> :Give efficient algorithms for both adjacency lists and matrices.<br /> <br /> <br /> :[[7.17]]. A ''vertex cover'' of a graph &lt;math&gt;G=(V,E)&lt;/math&gt; is a subset of vertices &lt;math&gt;V'&lt;/math&gt; such that each edge in &lt;math&gt;E&lt;/math&gt; is incident on at least one vertex of &lt;math&gt;V'&lt;/math&gt;.<br /> :(a)Give an efficient algorithm to find a minimum-size vertex cover if &lt;math&gt;G&lt;/math&gt; is a tree.<br /> :(b)Let &lt;math&gt;G=(V,E)&lt;/math&gt; be a tree such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a minimum-weight vertex cover of &lt;math&gt;G&lt;/math&gt;.<br /> :(c)Let &lt;math&gt;G=(V,E)&lt;/math&gt; be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a minimum-weight vertex cover of &lt;math&gt;G&lt;/math&gt;.<br /> [[7.17|Solution]]<br /> <br /> <br /> :7.18. A ''vertex cover'' of a graph &lt;math&gt;G=(V,E)&lt;/math&gt; is a subset of vertices &lt;math&gt;V' \in V&lt;/math&gt; such that every edge in &lt;math&gt;E&lt;/math&gt; contains at least one vertex from &lt;math&gt;V'&lt;/math&gt;. Delete all the leaves from any depth-first search tree of &lt;math&gt;G&lt;/math&gt;. Must the remaining vertices form a vertex cover of &lt;math&gt;G&lt;/math&gt;? Give a proof or a counterexample.<br /> <br /> <br /> :[[7.19]]. An ''independent set'' of an undirected graph &lt;math&gt;G=(V,E)&lt;/math&gt; is a set of vertices$U$such that no edge in &lt;math&gt;E&lt;/math&gt; is incident on two vertices of &lt;math&gt;U&lt;/math&gt;.<br /> :(a)Give an efficient algorithm to find a maximum-size independent set if &lt;math&gt;G&lt;/math&gt; is a tree.<br /> :(b)Let &lt;math&gt;G=(V,E)&lt;/math&gt; be a tree with weights associated with the vertices such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a maximum independent set of &lt;math&gt;G&lt;/math&gt;.<br /> :(c)Let &lt;math&gt;G=(V,E)&lt;/math&gt; be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a maximum independent set of &lt;math&gt;G&lt;/math&gt;.<br /> [[7.19|Solution]]<br /> <br /> <br /> :7.20. A ''vertex cover'' of a graph &lt;math&gt;G=(V,E)&lt;/math&gt; is a subset of vertices &lt;math&gt;V' \in V&lt;/math&gt; such that every edge in &lt;math&gt;E&lt;/math&gt; contains ''at least one'' vertex from &lt;math&gt;V'&lt;/math&gt;. An ''independent set'' of graph &lt;math&gt;G=(V,E)&lt;/math&gt; is a subset of vertices &lt;math&gt;V' \in V&lt;/math&gt; such that no edge in &lt;math&gt;E&lt;/math&gt; contains both vertices from &lt;math&gt;V'&lt;/math&gt;.<br /> :An ''independent vertex cover'' is a subset of vertices that is both an independent set and a vertex cover of &lt;math&gt;G&lt;/math&gt;. Give an efficient algorithm for testing whether &lt;math&gt;G&lt;/math&gt; contains an independent vertex cover. What classical graph problem does this reduce to?<br /> <br /> <br /> :[[7.21]]. Consider the problem of determining whether a given undirected graph &lt;math&gt;G=(V,E)&lt;/math&gt; contains a ''triangle'' or cycle of length 3. <br /> :(a)Give an &lt;math&gt;O(|V|^3)&lt;/math&gt; to find a triangle if one exists.<br /> :(b)Improve your algorithm to run in time &lt;math&gt;O(|V| \cdot |E|)&lt;/math&gt;. You may assume &lt;math&gt;|V| \leq |E|&lt;/math&gt;. <br /> :Observe that these bounds gives you time to convert between the adjacency matrix and adjacency list representations of &lt;math&gt;G&lt;/math&gt;.<br /> [[7.21|Solution]]<br /> <br /> <br /> :7.22. Consider a set of movies &lt;math&gt;M_1, M_2, \ldots, M_k&lt;/math&gt;. There is a set of customers, each one of which indicates the two movies they would like to see this weekend. Movies are shown on Saturday evening and Sunday evening. Multiple movies may be screened at the same time. <br /> :You must decide which movies should be televised on Saturday and which on Sunday, so that every customer gets to see the two movies they desire. Is there a schedule where each movie is shown at most once? Design an efficient algorithm to find such a schedule if one exists.<br /> <br /> <br /> :[[7.23]].The ''diameter'' of a tree &lt;math&gt;T=(V,E)&lt;/math&gt; is given by <br /> &lt;center&gt;&lt;math&gt;\max_{u,v\in V} \delta(u,v)&lt;/math&gt;&lt;/center&gt;<br /> :(where &lt;math&gt;\delta(u,v)&lt;/math&gt; is the number of edges on the path from &lt;math&gt;u&lt;/math&gt; to &lt;math&gt;v&lt;/math&gt;). Describe an efficient algorithm to compute the diameter of a tree, and show the correctness and analyze the running time of your algorithm.<br /> [[7.23|Solution]]<br /> <br /> <br /> :7.24. Given an undirected graph &lt;math&gt;G&lt;/math&gt; with &lt;math&gt;n&lt;/math&gt; vertices and &lt;math&gt;m&lt;/math&gt; edges, and an integer &lt;math&gt;k&lt;/math&gt;, give an &lt;math&gt;O(m+n)&lt;/math&gt; algorithm that finds the maximum induced subgraph &lt;math&gt;H&lt;/math&gt; of &lt;math&gt;G&lt;/math&gt; such that each vertex in &lt;math&gt;H&lt;/math&gt; has degree &lt;math&gt;\geq k&lt;/math&gt;, or prove that no such graph exists. An induced subgraph &lt;math&gt;F=(U,R)&lt;/math&gt; of a graph &lt;math&gt;G=(V,E)&lt;/math&gt; is a subset of &lt;math&gt;U&lt;/math&gt; of the vertices &lt;math&gt;V&lt;/math&gt; of &lt;math&gt;G&lt;/math&gt;, and all edges &lt;math&gt;R&lt;/math&gt; of &lt;math&gt;G&lt;/math&gt; such that both vertices of each edge are in &lt;math&gt;U&lt;/math&gt;.<br /> <br /> <br /> :[[7.25]]. Let &lt;math&gt;v&lt;/math&gt; and &lt;math&gt;w&lt;/math&gt; be two vertices in a directed graph &lt;math&gt;G=(V,E &lt;/math&gt;. Design a linear-time algorithm to find the ''number'' of different shortest paths (not necessarily vertex disjoint) between &lt;math&gt;v&lt;/math&gt; and &lt;math&gt;w&lt;/math&gt;. Note: the edges in &lt;math&gt;G&lt;/math&gt; are unweighted.<br /> [[7.25|Solution]]<br /> <br /> <br /> :7.26. Design a linear-time algorithm to eliminate each vertex &lt;math&gt;v&lt;/math&gt; of degree 2 from a graph by replacing edges &lt;math&gt;(u,v)&lt;/math&gt; and &lt;math&gt;(v,w)&lt;/math&gt; by an edge &lt;math&gt;(u,w)&lt;/math&gt;. We also seek to eliminate multiple copies of edges by replacing them with a single edge. Note that removing multiple copies of an edge may create a new vertex of degree 2, which has to be removed, and that removing a vertex of degree 2 may create multiple edges, which also must be removed.<br /> <br /> ===Directed Graphs===<br /> <br /> :[[7.27]]. The reverse of a directed graph &lt;math&gt;G = (V, E)&lt;/math&gt; is another directed graph &lt;math&gt;G^R = (V, E^R)&lt;/math&gt; on the same vertex set, but with all edges reversed; that is, &lt;math&gt;E^R = {(v, u) : (u, v) \in E}&lt;/math&gt;. Give an &lt;math&gt;O(n + m)&lt;/math&gt; algorithm for computing the reverse of an &lt;math&gt;n&lt;/math&gt;-vertex &lt;math&gt;m&lt;/math&gt;-edge graph in adjacency list format.<br /> [[7.27|Solution]]<br /> <br /> <br /> :7.28. Your job is to arrange &lt;math&gt;n&lt;/math&gt; ill-behaved children in a straight line, facing front. You are given a list of &lt;math&gt;m&lt;/math&gt; statements of the form “&lt;math&gt;i&lt;/math&gt; hates &lt;math&gt;j&lt;/math&gt;.” If &lt;math&gt;i&lt;/math&gt; hates &lt;math&gt;j&lt;/math&gt;, then you do not want to put &lt;math&gt;i&lt;/math&gt; somewhere behind &lt;math&gt;j&lt;/math&gt;, because then &lt;math&gt;i&lt;/math&gt; is capable of throwing something at &lt;math&gt;j&lt;/math&gt;.<br /> :(a) Give an algorithm that orders the line (or says that it is not possible) in &lt;math&gt;O(m + n)&lt;/math&gt; time.<br /> :(b) Suppose instead you want to arrange the children in rows such that if &lt;math&gt;i&lt;/math&gt; hates &lt;math&gt;j&lt;/math&gt;, then &lt;math&gt;i&lt;/math&gt; must be in a lower numbered row than &lt;math&gt;j&lt;/math&gt;. Give an efficient algorithm to find the minimum number of rows needed, if it is possible.<br /> <br /> <br /> :[[7.29]]. A particular academic program has &lt;math&gt;n&lt;/math&gt; required courses, certain pairs of which have prerequisite relations so that &lt;math&gt;(x, y)&lt;/math&gt; means you must take course &lt;math&gt;x&lt;/math&gt; before &lt;math&gt;y&lt;/math&gt;. How would you analyze the prerequisite pairs to make sure it is possible for people to complete the program?<br /> [[7.29|Solution]]<br /> <br /> <br /> :7.30. Gotcha-solitaire is a game on a deck with &lt;math&gt;n&lt;/math&gt; distinct cards (all face up) and &lt;math&gt;m&lt;/math&gt; gotcha pairs &lt;math&gt;(i, j)&lt;/math&gt; such that card &lt;math&gt;i&lt;/math&gt; must be played sometime before card &lt;math&gt;j&lt;/math&gt;. You play by sequentially choosing cards, and win if you pick up the entire deck without violating any gotcha pair constraints. Give an efficient algorithm to find a winning pickup order if one exists.<br /> <br /> <br /> :[[7.31]]. You are given a list of &lt;math&gt;n&lt;/math&gt; words each of length &lt;math&gt;k&lt;/math&gt; in a language you don’t know, although you are told that words are sorted in lexicographic (alphabetical) order. Reconstruct the order of the α alphabet letters (characters) in that language. <br /> :For example, if the strings are &lt;math&gt;{QQZ, QZZ, XQZ, XQX, XXX}&lt;/math&gt;, the character order must be &lt;math&gt;Q&lt;/math&gt; before &lt;math&gt;Z&lt;/math&gt; before &lt;math&gt;X&lt;/math&gt;.<br /> :(a) Give an algorithm to efficiently reconstruct this character order. (Hint: use a graph structure, where each node represents one letter.)<br /> :(b) What is its running time, as a function of &lt;math&gt;n, k&lt;/math&gt;, and &lt;math&gt;\alpha&lt;/math&gt;?<br /> [[7.31|Solution]]<br /> <br /> <br /> :7.32. A ''weakly connected component'' in a directed graph is a connected component ignoring the direction of the edges. Adding a single directed edge to a directed graph can reduce the number of weakly connected components, but by at most how many components? What about the number of strongly connected components?<br /> <br /> <br /> :[[7.33]]. Design a linear-time algorithm that, given an undirected graph &lt;math&gt;G&lt;/math&gt; and a particular edge &lt;math&gt;e&lt;/math&gt; in it, determines whether &lt;math&gt;G&lt;/math&gt; has a cycle containing &lt;math&gt;e&lt;/math&gt;.<br /> [[7.33|Solution]]<br /> <br /> <br /> :7.34. An ''arborescence'' of a directed graph &lt;math&gt;G&lt;/math&gt; is a rooted tree such that there is a directed path from the root to every other vertex in the graph. Give an efficient and correct algorithm to test whether &lt;math&gt;G&lt;/math&gt; contains an arborescence, and its time complexity.<br /> <br /> <br /> :[[7.35]]. A mother vertex in a directed graph &lt;math&gt;G = (V, E)&lt;/math&gt; is a vertex &lt;math&gt;v&lt;/math&gt; such that all other vertices &lt;math&gt;G&lt;/math&gt; can be reached by a directed path from &lt;math&gt;v&lt;/math&gt;. <br /> :(a) Give an &lt;math&gt;O(n + m)&lt;/math&gt; algorithm to test whether a given vertex &lt;math&gt;v&lt;/math&gt; is a mother of &lt;math&gt;G&lt;/math&gt;, where &lt;math&gt;n = |V|&lt;/math&gt; and &lt;math&gt;m = |E|&lt;/math&gt;. <br /> :(b) Give an &lt;math&gt;O(n + m)&lt;/math&gt; algorithm to test whether graph &lt;math&gt;G&lt;/math&gt; contains a mother vertex.<br /> [[7.35|Solution]]<br /> <br /> <br /> :7.36. Let &lt;math&gt;G&lt;/math&gt; be a directed graph. We say that &lt;math&gt;G&lt;/math&gt; is ''k-cyclic'' if every (not necessarily simple) cycle in &lt;math&gt;G&lt;/math&gt; contains at most &lt;math&gt;k&lt;/math&gt; distinct nodes. Give a linear-time algorithm to determine if a directed graph &lt;math&gt;G&lt;/math&gt; is &lt;math&gt;k&lt;/math&gt;-cyclic, where &lt;math&gt;G&lt;/math&gt; and &lt;math&gt;k&lt;/math&gt; are given as inputs. Justify the correctness and running time of your algorithm.<br /> <br /> <br /> :[[7.37]]. A ''tournament'' is a directed graph formed by taking the complete undirected graph and assigning arbitrary directions on the edges—that is, a graph &lt;math&gt;G = (V, E)&lt;/math&gt; such that for all &lt;math&gt;u,v \in V&lt;/math&gt;, exactly one of &lt;math&gt;(u, v)&lt;/math&gt; or &lt;math&gt;(v, u)&lt;/math&gt; is in &lt;math&gt;E&lt;/math&gt;. Show that every tournament has a Hamiltonian path—that is, a path that visits every vertex exactly once. Give an algorithm to find this path.<br /> [[7.37|Solution]]<br /> <br /> ===Articulation Vertices===<br /> <br /> :7.38. An articulation vertex of a connected graph &lt;math&gt;G&lt;/math&gt; is a vertex whose deletion disconnects &lt;math&gt;G&lt;/math&gt;. Let &lt;math&gt;G&lt;/math&gt; be a graph with &lt;math&gt;n&lt;/math&gt; vertices and &lt;math&gt;m&lt;/math&gt; edges. Give a simple &lt;math&gt;O(n+m)&lt;/math&gt; algorithm for finding a vertex of &lt;math&gt;G&lt;/math&gt; that is not an articulation vertex—that is, whose deletion does not disconnect &lt;math&gt;G&lt;/math&gt;.<br /> <br /> <br /> :[[7.39]]. Following up on the previous problem, give an &lt;math&gt;O(n+m)&lt;/math&gt; algorithm that finds a deletion order for the &lt;math&gt;n&lt;/math&gt; vertices such that no deletion disconnects the graph. (Hint: think DFS/BFS.)<br /> [[7.39|Solution]]<br /> <br /> <br /> :7.40. Suppose &lt;math&gt;G&lt;/math&gt; is a connected undirected graph. An edge &lt;math&gt;e&lt;/math&gt; whose removal disconnects the graph is called a bridge. Must every bridge &lt;math&gt;e&lt;/math&gt; be an edge in a depth-first search tree of &lt;math&gt;G&lt;/math&gt;? Give a proof or a counterexample.<br /> <br /> <br /> :[[7.41]]. A city that only has two-way streets has decided to change them all into one-way streets. They want to ensure that the new network is strongly connected so everyone can legally drive anywhere in the city and back.<br /> :(a) Let &lt;math&gt;G&lt;/math&gt; be the original undirected graph. Prove that there is a way to properly orient/direct the edges of &lt;math&gt;G&lt;/math&gt; provided &lt;math&gt;G&lt;/math&gt; does not contain a bridge.<br /> :(b) Give an efficient algorithm to orient the edges of a bridgeless graph &lt;math&gt;G&lt;/math&gt; so the result is strongly connected.<br /> [[7.41|Solution]]<br /> <br /> ===Interview Problems===<br /> <br /> :7.42. Which data structures are used in depth-first and breath-first search?<br /> <br /> <br /> :[[7.43]]. Write a function to traverse binary search tree and return the ith node in sorted order.<br /> [[7.43|Solution]]<br /> <br /> <br /> Back to [[Chapter List]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Chapter_7&diff=452 Chapter 7 2020-10-01T18:08:55Z <p>Algowikiadmin: Protected &quot;Chapter 7&quot; ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))</p> <hr /> <div>=Graph Traversal=<br /> <br /> ===Simulating Graph Algorithms===<br /> <br /> :[[7.1]]. For the following graphs &lt;math&gt;G_1&lt;/math&gt; (left) and &lt;math&gt;G_2&lt;/math&gt; (right):<br /> :(see book for figures)<br /> <br /> :(a)Report the order of the vertices encountered on a breadth-first search starting from vertex &lt;math&gt;A&lt;/math&gt;. Break all ties by picking the vertices in alphabetical order (i.e., &lt;math&gt;A&lt;/math&gt; before &lt;math&gt;Z&lt;/math&gt;).<br /> :(b)Report the order of the vertices encountered on a depth-first search starting from vertex &lt;math&gt;A&lt;/math&gt;. Break all ties by picking the vertices in alphabetical order (i.e., &lt;math&gt;A&lt;/math&gt; before &lt;math&gt;Z&lt;/math&gt;).<br /> [[7.1|Solution]]<br /> <br /> <br /> :7.2. Do a topological sort of the following graph &lt;math&gt;G&lt;/math&gt;:<br /> :(see book for figure)<br /> <br /> ===Traversal===<br /> <br /> :[[7.3]]. Prove that there is a unique path between any pair of vertices in a tree.<br /> [[7.3|Solution]]<br /> <br /> <br /> :7.4. Prove that in a breadth-first search on a undirected graph &lt;math&gt;G&lt;/math&gt;, every edge is either a tree edge or a cross edge, where &lt;math&gt;x&lt;/math&gt; is neither an ancestor nor descendant of &lt;math&gt;y&lt;/math&gt; in cross edge &lt;math&gt;(x, y)&lt;/math&gt;.<br /> <br /> <br /> :[[7.5]]. Give a linear algorithm to compute the chromatic number of graphs where each vertex has degree at most 2. Any bipartite graph has a chromatic number of 2. Must such graphs be bipartite?<br /> [[7.5|Solution]]<br /> <br /> <br /> :7.6. You are given a connected, undirected graph &lt;math&gt;G&lt;/math&gt; with &lt;math&gt;n&lt;/math&gt; vertices and &lt;math&gt;m&lt;/math&gt; edges. Give an &lt;math&gt;O(n + m)&lt;/math&gt; algorithm to identify an edge you can remove from &lt;math&gt;G&lt;/math&gt; while still leaving &lt;math&gt;G&lt;/math&gt; connected, if one exists. Can you reduce the running time to &lt;math&gt;O(n)&lt;/math&gt;?<br /> <br /> <br /> :[[7.7]]. In breadth-first and depth-first search, an undiscovered node is marked ''discovered'' when it is first encountered, and marked ''processed'' when it has been completely searched. At any given moment, several nodes might be simultaneously in the ''discovered'' state.<br /> :(a) Describe a graph on &lt;math&gt;n&lt;/math&gt; vertices and a particular starting vertex &lt;math&gt;v&lt;/math&gt; such that &lt;math&gt;\Theta(n)&lt;/math&gt; nodes are simultaneously in the ''discovered'' state during a ''breadth-first search'' starting from &lt;math&gt;v&lt;/math&gt;.<br /> :(b) Describe a graph on &lt;math&gt;n&lt;/math&gt; vertices and a particular starting vertex &lt;math&gt;v&lt;/math&gt; such that &lt;math&gt;\Theta(n)&lt;/math&gt; nodes are simultaneously in the ''discovered'' state during a ''depth-first search'' starting from &lt;math&gt;v&lt;/math&gt;.<br /> :(c) Describe a graph on &lt;math&gt;n&lt;/math&gt; vertices and a particular starting vertex &lt;math&gt;v&lt;/math&gt; such that at some point &lt;math&gt;\Theta(n)&lt;/math&gt; nodes remain ''undiscovered'', while &lt;math&gt;\Theta(n)&lt;/math&gt; nodes have been ''processed'' during a ''depth-first search'' starting from &lt;math&gt;v&lt;/math&gt;.(Note, there may also be ''discovered'' nodes.)<br /> [[7.7|Solution]]<br /> <br /> <br /> :7.8. Given pre-order and in-order traversals of a binary tree (discussed in Section 3.4.1), is it possible to reconstruct the tree? If so, sketch an algorithm to do it. If not, give a counterexample. Repeat the problem if you are given the pre-order and post-order traversals.<br /> <br /> <br /> :[[7.9]]. Present correct and efficient algorithms to convert an undirected graph &lt;math&gt;G&lt;/math&gt; between the following graph data structures. You must give the time complexity of each algorithm, assuming &lt;math&gt;n&lt;/math&gt; vertices and &lt;math&gt;m&lt;/math&gt; edges.<br /> :(a)Convert from an adjacency matrix to adjacency lists.<br /> :(b)Convert from an adjacency list to an incidence matrix. An incidence matrix &lt;math&gt;M&lt;/math&gt; has a row for each vertex and a column for each edge, such that &lt;math&gt;M[i,j]=1&lt;/math&gt; if vertex &lt;math&gt;i&lt;/math&gt; is part of edge &lt;math&gt;j&lt;/math&gt;, otherwise &lt;math&gt;M[i,j] = 0&lt;/math&gt;.<br /> :(c)Convert from an incidence matrix to adjacency lists.<br /> [[7.9|Solution]]<br /> <br /> <br /> :7.10. Suppose an arithmetic expression is given as a tree. Each leaf is an integer and each internal node is one of the standard arithmetical operations &lt;math&gt;(+, -, *, /)&lt;/math&gt;. For example, the expression &lt;math&gt;2+3*4+(3*4)/5&lt;/math&gt; is represented by the tree in Figure 7.17(a). Give an &lt;math&gt;O(n)&lt;/math&gt; algorithm for evaluating such an expression, where there are &lt;math&gt;n&lt;/math&gt; nodes in the tree.<br /> <br /> <br /> :[[7.11]]. Suppose an arithmetic expression is given as a DAG (directed acyclic graph) with common subexpressions removed. Each leaf is an integer and each internal node is one of the standard arithmetical operations &lt;math&gt;(+,-,*,/)&lt;/math&gt;. For example, the expression &lt;math&gt;2+3*4+(3*4)/5&lt;/math&gt; is represented by the DAG in Figure (see book)(b). Give an &lt;math&gt;O(n+m)&lt;/math&gt; algorithm for evaluating such a DAG, where there are &lt;math&gt;n&lt;/math&gt; nodes and &lt;math&gt;m&lt;/math&gt; edges in the DAG. Hint: modify an algorithm for the tree case to achieve the desired efficiency.<br /> [[7.11|Solution]]<br /> <br /> <br /> :7.12. The war story of Section 7.4 (page 210) describes an algorithm for constructing the dual graph of the triangulation efficiently, although it does not guarantee linear time. Give a worst-case linear algorithm for the problem.<br /> <br /> ===Applications===<br /> <br /> :[[7.13]]. The Chutes and Ladders game has a board with n cells where you seek to travel from cell 1 to cell &lt;math&gt;n&lt;/math&gt;. To move, a player throws a six-sided dice to determine how many cells forward they move. This board also contains chutes and ladders that connect certain pairs of cells. A player who lands on the mouth of a chute immediately falls back down to the cell at the other end. A player who lands on the base of a ladder immediately travels up to the cell at the top of the ladder. Suppose you have rigged the dice to give you full control of the number for each roll. Give an efficient algorithm to find the minimum number of dice throws to win.<br /> [[7.13|Solution]]<br /> <br /> <br /> :7.14. Plum blossom poles are a Kung Fu training technique, consisting of &lt;math&gt;n&lt;/math&gt; large posts partially sunk into the ground, with each pole &lt;math&gt;p_i&lt;/math&gt; at position &lt;math&gt;(x_i, y_i)&lt;/math&gt;. Students practice martial arts techniques by stepping from the top of one pole to the top of another pole. In order to keep balance, each step must be more than &lt;math&gt;d&lt;/math&gt; meters but less than &lt;math&gt;2d&lt;/math&gt; meters. Give an efficient algorithm to find a safe path from pole &lt;math&gt;p_s&lt;/math&gt; to &lt;math&gt;p_t&lt;/math&gt; if it exists.<br /> <br /> <br /> :[[7.15]]. You are planning the seating arrangement for a wedding given a list of guests, &lt;math&gt;V&lt;/math&gt;. For each guest &lt;math&gt;g&lt;/math&gt; you have a list of all other guests who are on bad terms with them. Feelings are reciprocal: if &lt;math&gt;h&lt;/math&gt; is on bad terms with &lt;math&gt;g&lt;/math&gt;, then &lt;math&gt;g&lt;/math&gt; is on bad terms with &lt;math&gt;h&lt;/math&gt;. Your goal is to arrange the seating such that no pair of guests sitting at the same table are on bad terms with each other. There will be only two tables at the wedding. Give an efficient algorithm to find an acceptable seating arrangement if one exists.<br /> [[7.15|Solution]]<br /> <br /> ===Algorithm Design===<br /> <br /> :7.16. The ''square'' of a directed graph &lt;math&gt;G=(V,E)&lt;/math&gt; is the graph &lt;math&gt;G^2=(V,E^2)&lt;/math&gt; such that &lt;math&gt;(u,w) \in E^2&lt;/math&gt; iff there exists &lt;math&gt;v \in V&lt;/math&gt; such that &lt;math&gt;(u,v) \in E&lt;/math&gt; and &lt;math&gt;(v,w) \in E&lt;/math&gt;; i.e., there is a path of exactly two edges from &lt;math&gt;u&lt;/math&gt; to &lt;math&gt;w&lt;/math&gt;.<br /> :Give efficient algorithms for both adjacency lists and matrices.<br /> <br /> <br /> :[[7.17]]. A ''vertex cover'' of a graph &lt;math&gt;G=(V,E)&lt;/math&gt; is a subset of vertices &lt;math&gt;V'&lt;/math&gt; such that each edge in &lt;math&gt;E&lt;/math&gt; is incident on at least one vertex of &lt;math&gt;V'&lt;/math&gt;.<br /> :(a)Give an efficient algorithm to find a minimum-size vertex cover if &lt;math&gt;G&lt;/math&gt; is a tree.<br /> :(b)Let &lt;math&gt;G=(V,E)&lt;/math&gt; be a tree such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a minimum-weight vertex cover of &lt;math&gt;G&lt;/math&gt;.<br /> :(c)Let &lt;math&gt;G=(V,E)&lt;/math&gt; be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a minimum-weight vertex cover of &lt;math&gt;G&lt;/math&gt;.<br /> [[7.17|Solution]]<br /> <br /> <br /> :7.18. A ''vertex cover'' of a graph &lt;math&gt;G=(V,E)&lt;/math&gt; is a subset of vertices &lt;math&gt;V' \in V&lt;/math&gt; such that every edge in &lt;math&gt;E&lt;/math&gt; contains at least one vertex from &lt;math&gt;V'&lt;/math&gt;. Delete all the leaves from any depth-first search tree of &lt;math&gt;G&lt;/math&gt;. Must the remaining vertices form a vertex cover of &lt;math&gt;G&lt;/math&gt;? Give a proof or a counterexample.<br /> <br /> <br /> :[[7.19]]. An ''independent set'' of an undirected graph &lt;math&gt;G=(V,E)&lt;/math&gt; is a set of vertices$U\$ such that no edge in &lt;math&gt;E&lt;/math&gt; is incident on two vertices of &lt;math&gt;U&lt;/math&gt;.<br /> :(a)Give an efficient algorithm to find a maximum-size independent set if &lt;math&gt;G&lt;/math&gt; is a tree.<br /> :(b)Let &lt;math&gt;G=(V,E)&lt;/math&gt; be a tree with weights associated with the vertices such that the weight of each vertex is equal to the degree of that vertex. Give an efficient algorithm to find a maximum independent set of &lt;math&gt;G&lt;/math&gt;.<br /> :(c)Let &lt;math&gt;G=(V,E)&lt;/math&gt; be a tree with arbitrary weights associated with the vertices. Give an efficient algorithm to find a maximum independent set of &lt;math&gt;G&lt;/math&gt;.<br /> [[7.19|Solution]]<br /> <br /> <br /> :7.20. A ''vertex cover'' of a graph &lt;math&gt;G=(V,E)&lt;/math&gt; is a subset of vertices &lt;math&gt;V' \in V&lt;/math&gt; such that every edge in &lt;math&gt;E&lt;/math&gt; contains ''at least one'' vertex from &lt;math&gt;V'&lt;/math&gt;. An ''independent set'' of graph &lt;math&gt;G=(V,E)&lt;/math&gt; is a subset of vertices &lt;math&gt;V' \in V&lt;/math&gt; such that no edge in &lt;math&gt;E&lt;/math&gt; contains both vertices from &lt;math&gt;V'&lt;/math&gt;.<br /> :An ''independent vertex cover'' is a subset of vertices that is both an independent set and a vertex cover of &lt;math&gt;G&lt;/math&gt;. Give an efficient algorithm for testing whether &lt;math&gt;G&lt;/math&gt; contains an independent vertex cover. What classical graph problem does this reduce to?<br /> <br /> <br /> :[[7.21]]. Consider the problem of determining whether a given undirected graph &lt;math&gt;G=(V,E)&lt;/math&gt; contains a ''triangle'' or cycle of length 3. <br /> :(a)Give an &lt;math&gt;O(|V|^3)&lt;/math&gt; to find a triangle if one exists.<br /> :(b)Improve your algorithm to run in time &lt;math&gt;O(|V| \cdot |E|)&lt;/math&gt;. You may assume &lt;math&gt;|V| \leq |E|&lt;/math&gt;. <br /> :Observe that these bounds gives you time to convert between the adjacency matrix and adjacency list representations of &lt;math&gt;G&lt;/math&gt;.<br /> [[7.21|Solution]]<br /> <br /> <br /> :7.22. Consider a set of movies &lt;math&gt;M_1, M_2, \ldots, M_k&lt;/math&gt;. There is a set of customers, each one of which indicates the two movies they would like to see this weekend. Movies are shown on Saturday evening and Sunday evening. Multiple movies may be screened at the same time. <br /> :You must decide which movies should be televised on Saturday and which on Sunday, so that every customer gets to see the two movies they desire. Is there a schedule where each movie is shown at most once? Design an efficient algorithm to find such a schedule if one exists.<br /> <br /> <br /> :[[7.23]].The ''diameter'' of a tree &lt;math&gt;T=(V,E)&lt;/math&gt; is given by <br /> &lt;center&gt;&lt;math&gt;\max_{u,v\in V} \delta(u,v)&lt;/math&gt;&lt;/center&gt;<br /> :(where &lt;math&gt;\delta(u,v)&lt;/math&gt; is the number of edges on the path from &lt;math&gt;u&lt;/math&gt; to &lt;math&gt;v&lt;/math&gt;). Describe an efficient algorithm to compute the diameter of a tree, and show the correctness and analyze the running time of your algorithm.<br /> [[7.23|Solution]]<br /> <br /> <br /> :7.24. Given an undirected graph &lt;math&gt;G&lt;/math&gt; with &lt;math&gt;n&lt;/math&gt; vertices and &lt;math&gt;m&lt;/math&gt; edges, and an integer &lt;math&gt;k&lt;/math&gt;, give an &lt;math&gt;O(m+n)&lt;/math&gt; algorithm that finds the maximum induced subgraph &lt;math&gt;H&lt;/math&gt; of &lt;math&gt;G&lt;/math&gt; such that each vertex in &lt;math&gt;H&lt;/math&gt; has degree &lt;math&gt;\geq k&lt;/math&gt;, or prove that no such graph exists. An induced subgraph &lt;math&gt;F=(U,R)&lt;/math&gt; of a graph &lt;math&gt;G=(V,E)&lt;/math&gt; is a subset of &lt;math&gt;U&lt;/math&gt; of the vertices &lt;math&gt;V&lt;/math&gt; of &lt;math&gt;G&lt;/math&gt;, and all edges &lt;math&gt;R&lt;/math&gt; of &lt;math&gt;G&lt;/math&gt; such that both vertices of each edge are in &lt;math&gt;U&lt;/math&gt;.<br /> <br /> <br /> :[[7.25]]. Let &lt;math&gt;v&lt;/math&gt; and &lt;math&gt;w&lt;/math&gt; be two vertices in a directed graph &lt;math&gt;G=(V,E &lt;/math&gt;. Design a linear-time algorithm to find the ''number'' of different shortest paths (not necessarily vertex disjoint) between &lt;math&gt;v&lt;/math&gt; and &lt;math&gt;w&lt;/math&gt;. Note: the edges in &lt;math&gt;G&lt;/math&gt; are unweighted.<br /> [[7.25|Solution]]<br /> <br /> <br /> :7.26. Design a linear-time algorithm to eliminate each vertex &lt;math&gt;v&lt;/math&gt; of degree 2 from a graph by replacing edges &lt;math&gt;(u,v)&lt;/math&gt; and &lt;math&gt;(v,w)&lt;/math&gt; by an edge &lt;math&gt;(u,w)&lt;/math&gt;. We also seek to eliminate multiple copies of edges by replacing them with a single edge. Note that removing multiple copies of an edge may create a new vertex of degree 2, which has to be removed, and that removing a vertex of degree 2 may create multiple edges, which also must be removed.<br /> <br /> ===Directed Graphs===<br /> <br /> :[[7.27]]. The reverse of a directed graph &lt;math&gt;G = (V, E)&lt;/math&gt; is another directed graph &lt;math&gt;G^R = (V, E^R)&lt;/math&gt; on the same vertex set, but with all edges reversed; that is, &lt;math&gt;E^R = {(v, u) : (u, v) \in E}&lt;/math&gt;. Give an &lt;math&gt;O(n + m)&lt;/math&gt; algorithm for computing the reverse of an &lt;math&gt;n&lt;/math&gt;-vertex &lt;math&gt;m&lt;/math&gt;-edge graph in adjacency list format.<br /> [[7.27|Solution]]<br /> <br /> <br /> :7.28. Your job is to arrange &lt;math&gt;n&lt;/math&gt; ill-behaved children in a straight line, facing front. You are given a list of &lt;math&gt;m&lt;/math&gt; statements of the form “&lt;math&gt;i&lt;/math&gt; hates &lt;math&gt;j&lt;/math&gt;.” If &lt;math&gt;i&lt;/math&gt; hates &lt;math&gt;j&lt;/math&gt;, then you do not want to put &lt;math&gt;i&lt;/math&gt; somewhere behind &lt;math&gt;j&lt;/math&gt;, because then &lt;math&gt;i&lt;/math&gt; is capable of throwing something at &lt;math&gt;j&lt;/math&gt;.<br /> :(a) Give an algorithm that orders the line (or says that it is not possible) in &lt;math&gt;O(m + n)&lt;/math&gt; time.<br /> :(b) Suppose instead you want to arrange the children in rows such that if &lt;math&gt;i&lt;/math&gt; hates &lt;math&gt;j&lt;/math&gt;, then &lt;math&gt;i&lt;/math&gt; must be in a lower numbered row than &lt;math&gt;j&lt;/math&gt;. Give an efficient algorithm to find the minimum number of rows needed, if it is possible.<br /> <br /> <br /> :[[7.29]]. A particular academic program has &lt;math&gt;n&lt;/math&gt; required courses, certain pairs of which have prerequisite relations so that &lt;math&gt;(x, y)&lt;/math&gt; means you must take course &lt;math&gt;x&lt;/math&gt; before &lt;math&gt;y&lt;/math&gt;. How would you analyze the prerequisite pairs to make sure it is possible for people to complete the program?<br /> [[7.29|Solution]]<br /> <br /> <br /> :7.30. Gotcha-solitaire is a game on a deck with &lt;math&gt;n&lt;/math&gt; distinct cards (all face up) and &lt;math&gt;m&lt;/math&gt; gotcha pairs &lt;math&gt;(i, j)&lt;/math&gt; such that card &lt;math&gt;i&lt;/math&gt; must be played sometime before card &lt;math&gt;j&lt;/math&gt;. You play by sequentially choosing cards, and win if you pick up the entire deck without violating any gotcha pair constraints. Give an efficient algorithm to find a winning pickup order if one exists.<br /> <br /> <br /> :[[7.31]]. You are given a list of &lt;math&gt;n&lt;/math&gt; words each of length &lt;math&gt;k&lt;/math&gt; in a language you don’t know, although you are told that words are sorted in lexicographic (alphabetical) order. Reconstruct the order of the α alphabet letters (characters) in that language. <br /> :For example, if the strings are &lt;math&gt;{QQZ, QZZ, XQZ, XQX, XXX}&lt;/math&gt;, the character order must be &lt;math&gt;Q&lt;/math&gt; before &lt;math&gt;Z&lt;/math&gt; before &lt;math&gt;X&lt;/math&gt;.<br /> :(a) Give an algorithm to efficiently reconstruct this character order. (Hint: use a graph structure, where each node represents one letter.)<br /> :(b) What is its running time, as a function of &lt;math&gt;n, k&lt;/math&gt;, and &lt;math&gt;\alpha&lt;/math&gt;?<br /> [[7.31|Solution]]<br /> <br /> <br /> :7.32. A ''weakly connected component'' in a directed graph is a connected component ignoring the direction of the edges. Adding a single directed edge to a directed graph can reduce the number of weakly connected components, but by at most how many components? What about the number of strongly connected components?<br /> <br /> <br /> :[[7.33]]. Design a linear-time algorithm that, given an undirected graph &lt;math&gt;G&lt;/math&gt; and a particular edge &lt;math&gt;e&lt;/math&gt; in it, determines whether &lt;math&gt;G&lt;/math&gt; has a cycle containing &lt;math&gt;e&lt;/math&gt;.<br /> [[7.33|Solution]]<br /> <br /> <br /> :7.34. An ''arborescence'' of a directed graph &lt;math&gt;G&lt;/math&gt; is a rooted tree such that there is a directed path from the root to every other vertex in the graph. Give an efficient and correct algorithm to test whether &lt;math&gt;G&lt;/math&gt; contains an arborescence, and its time complexity.<br /> <br /> <br /> :[[7.35]]. A mother vertex in a directed graph &lt;math&gt;G = (V, E)&lt;/math&gt; is a vertex &lt;math&gt;v&lt;/math&gt; such that all other vertices &lt;math&gt;G&lt;/math&gt; can be reached by a directed path from &lt;math&gt;v&lt;/math&gt;. <br /> :(a) Give an &lt;math&gt;O(n + m)&lt;/math&gt; algorithm to test whether a given vertex &lt;math&gt;v&lt;/math&gt; is a mother of &lt;math&gt;G&lt;/math&gt;, where &lt;math&gt;n = |V|&lt;/math&gt; and &lt;math&gt;m = |E|&lt;/math&gt;. <br /> :(b) Give an &lt;math&gt;O(n + m)&lt;/math&gt; algorithm to test whether graph &lt;math&gt;G&lt;/math&gt; contains a mother vertex.<br /> [[7.35|Solution]]<br /> <br /> <br /> :7.36. Let &lt;math&gt;G&lt;/math&gt; be a directed graph. We say that &lt;math&gt;G&lt;/math&gt; is ''k-cyclic'' if every (not necessarily simple) cycle in &lt;math&gt;G&lt;/math&gt; contains at most &lt;math&gt;k&lt;/math&gt; distinct nodes. Give a linear-time algorithm to determine if a directed graph &lt;math&gt;G&lt;/math&gt; is &lt;math&gt;k&lt;/math&gt;-cyclic, where &lt;math&gt;G&lt;/math&gt; and &lt;math&gt;k&lt;/math&gt; are given as inputs. Justify the correctness and running time of your algorithm.<br /> <br /> <br /> :[[7.37]]. A ''tournament'' is a directed graph formed by taking the complete undirected graph and assigning arbitrary directions on the edges—that is, a graph &lt;math&gt;G = (V, E)&lt;/math&gt; such that for all &lt;math&gt;u,v \in V&lt;/math&gt;, exactly one of &lt;math&gt;(u, v)&lt;/math&gt; or &lt;math&gt;(v, u)&lt;/math&gt; is in &lt;math&gt;E&lt;/math&gt;. Show that every tournament has a Hamiltonian path—that is, a path that visits every vertex exactly once. Give an algorithm to find this path.<br /> [[7.37|Solution]]<br /> <br /> ===Articulation Vertices===<br /> <br /> :7.38. An articulation vertex of a connected graph &lt;math&gt;G&lt;/math&gt; is a vertex whose deletion disconnects &lt;math&gt;G&lt;/math&gt;. Let &lt;math&gt;G&lt;/math&gt; be a graph with &lt;math&gt;n&lt;/math&gt; vertices and &lt;math&gt;m&lt;/math&gt; edges. Give a simple &lt;math&gt;O(n+m)&lt;/math&gt; algorithm for finding a vertex of &lt;math&gt;G&lt;/math&gt; that is not an articulation vertex—that is, whose deletion does not disconnect &lt;math&gt;G&lt;/math&gt;.<br /> <br /> <br /> :[[7.39]]. Following up on the previous problem, give an &lt;math&gt;O(n+m)&lt;/math&gt; algorithm that finds a deletion order for the &lt;math&gt;n&lt;/math&gt; vertices such that no deletion disconnects the graph. (Hint: think DFS/BFS.)<br /> [[7.39|Solution]]<br /> <br /> <br /> :7.40. Suppose &lt;math&gt;G&lt;/math&gt; is a connected undirected graph. An edge &lt;math&gt;e&lt;/math&gt; whose removal disconnects the graph is called a bridge. Must every bridge &lt;math&gt;e&lt;/math&gt; be an edge in a depth-first search tree of &lt;math&gt;G&lt;/math&gt;? Give a proof or a counterexample.<br /> <br /> <br /> :[[7.41]]. A city that only has two-way streets has decided to change them all into one-way streets. They want to ensure that the new network is strongly connected so everyone can legally drive anywhere in the city and back.<br /> :(a) Let &lt;math&gt;G&lt;/math&gt; be the original undirected graph. Prove that there is a way to properly orient/direct the edges of &lt;math&gt;G&lt;/math&gt; provided &lt;math&gt;G&lt;/math&gt; does not contain a bridge.<br /> :(b) Give an efficient algorithm to orient the edges of a bridgeless graph &lt;math&gt;G&lt;/math&gt; so the result is strongly connected.<br /> [[7.41|Solution]]<br /> <br /> ===Interview Problems===<br /> <br /> :7.42. Which data structures are used in depth-first and breath-first search?<br /> <br /> <br /> :[[7.43]]. Write a function to traverse binary search tree and return the ith node in sorted order.<br /> [[7.43|Solution]]<br /> <br /> <br /> Back to [[Chapter List]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Chapter_6&diff=451 Chapter 6 2020-10-01T18:08:44Z <p>Algowikiadmin: Protected &quot;Chapter 6&quot; ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))</p> <hr /> <div>=Hashing and Randomized Algorithms=<br /> <br /> ===Probability===<br /> <br /> :[[6.1]]. You are given &lt;math&gt;n&lt;/math&gt; unbiased coins, and perform the following process to generate all heads. Toss all &lt;math&gt;n&lt;/math&gt; coins independently at random onto a table. Each round consists of picking up all the tails-up coins and tossing them onto the table again. You repeat until all coins are heads.<br /> :(a) What is the expected number of rounds performed by the process?<br /> :(b) What is the expected number of coin tosses performed by the process?<br /> [[6.1|Solution]]<br /> <br /> <br /> :6.2. Suppose we flip &lt;math&gt;n&lt;/math&gt; coins each of known bias, such that &lt;math&gt;p_i&lt;/math&gt; is the probability of the &lt;math&gt;i&lt;/math&gt;th coin being a head. Present an efficient algorithm to determine the exact probability of getting exactly &lt;math&gt;k&lt;/math&gt; heads given &lt;math&gt;p_1, . . . , p_n \in [0, 1]&lt;/math&gt;.<br /> <br /> <br /> :[[6.3]]. An inversion of a permutation is a pair of elements that are out of order.<br /> :(a) Show that a permutation of &lt;math&gt;n&lt;/math&gt; items has at most &lt;math&gt;n(n - 1)/2&lt;/math&gt; inversions. Which permutation(s) have exactly n(n - 1)/2 inversions?<br /> :(b) Let P be a permutation and &lt;math&gt;P^r&lt;/math&gt; be the reversal of this permutation. Show that &lt;math&gt;P&lt;/math&gt; and &lt;math&gt;P^r&lt;/math&gt; have a total of exactly &lt;math&gt;n(n - 1)/2&lt;/math&gt; inversions.<br /> :(c) Use the previous result to argue that the expected number of inversions in a random permutation is &lt;math&gt;n(n - 1)/4&lt;/math&gt;.<br /> [[6.3|Solution]]<br /> <br /> <br /> :6.4. A derangement is a permutation &lt;math&gt;p&lt;/math&gt; of &lt;math&gt;{1, . . . , n}&lt;/math&gt; such that no item is in its proper position, that is, &lt;math&gt;p_i \neq i&lt;/math&gt; for all &lt;math&gt;1 \leq i \leq n&lt;/math&gt;. What is the probability that a random permutation is a derangement?<br /> <br /> ===Hashing===<br /> <br /> :[[6.5]]. An all-Beatles radio station plays nothing but recordings by the Beatles, selecting the next song at random (uniformly with replacement). They get through about ten songs per hour. I listened for 90 minutes before hearing a repeated song. Estimate how many songs the Beatles recorded.<br /> [[6.5|Solution]]<br /> <br /> <br /> :6.6. Given strings &lt;math&gt;S&lt;/math&gt; and &lt;math&gt;T&lt;/math&gt; of length &lt;math&gt;n&lt;/math&gt; and &lt;math&gt;m&lt;/math&gt; respectively, find the shortest window in &lt;math&gt;S&lt;/math&gt; that contains all the characters in &lt;math&gt;T&lt;/math&gt; in expected &lt;math&gt;O(n + m)&lt;/math&gt; time.<br /> <br /> <br /> :[[6.7]]. Design and implement an efficient data structure to maintain a ''least recently used'' (LRU) cache of &lt;math&gt;n&lt;/math&gt; integer elements. A LRU cache will discard the least recently accessed element once the cache has reached its capacity, supporting the following operations:<br /> :• ''get(k)''– Return the value associated with the key &lt;math&gt;k&lt;/math&gt; if it currently exists in the cache, otherwise return -1.<br /> :• ''put(k,v)'' – Set the value associated with key &lt;math&gt;k&lt;/math&gt; to &lt;math&gt;v&lt;/math&gt;, or insert if &lt;math&gt;k&lt;/math&gt; is not already present. If there are already &lt;math&gt;n&lt;/math&gt; items in the queue, delete the least recently used item before inserting &lt;math&gt;(k, v)&lt;/math&gt;. Both operations should be done in &lt;math&gt;O(1)&lt;/math&gt; expected time.<br /> [[6.7|Solution]]<br /> <br /> ===Randomized Algorithms===<br /> <br /> :6.8. A pair of English words &lt;math&gt;(w_1, w_2)&lt;/math&gt; is called a ''rotodrome'' if one can be circularly shifted (rotated) to create the other word. For example, the words (windup, upwind) are a rotodrome pair, because we can rotate “windup” two positions to the right to get “upwind.”<br /> :Give an efficient algorithm to find all rotodrome pairs among &lt;math&gt;n&lt;/math&gt; words of length &lt;math&gt;k&lt;/math&gt;, with a worst-case analysis. Also give a faster expected-time algorithm based on hashing.<br /> <br /> <br /> :[[6.9]]. Given an array &lt;math&gt;w&lt;/math&gt; of positive integers, where &lt;math&gt;w[i]&lt;/math&gt; describes the weight of index &lt;math&gt;i&lt;/math&gt;, propose an algorithm that randomly picks an index in proportion to its weight.<br /> [[6.9|Solution]]<br /> <br /> <br /> :6.10. You are given a function ''rand7'', which generates a uniform random integer in the range 1 to 7. Use it to produce a function ''rand10'', which generates a uniform random integer in the range 1 to 10.<br /> <br /> <br /> :[[6.11]]. Let &lt;math&gt;0 &lt; \alpha &lt; 1/2&lt;/math&gt; be some constant, independent of the input array length &lt;math&gt;n&lt;/math&gt;. What is the probability that, with a randomly chosen pivot element, the partition subroutine from quicksort produces a split in which the size of both the resulting subproblems is at least &lt;math&gt;\alpha&lt;/math&gt; times the size of the original array?<br /> [[6.11|Solution]]<br /> <br /> <br /> :6.12. Show that for any given load &lt;math&gt;m/n&lt;/math&gt;, the error probability of a Bloom filter is minimized when the number of hash functions is &lt;math&gt;k = exp(-1)/(m/n)&lt;/math&gt;.<br /> <br /> <br /> <br /> Back to [[Chapter List]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Chapter_5&diff=450 Chapter 5 2020-10-01T18:08:24Z <p>Algowikiadmin: Protected &quot;Chapter 5&quot; ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))</p> <hr /> <div>=Divide and Conquer=<br /> <br /> ===Binary Search===<br /> <br /> :[[5.1]]. Suppose you are given a sorted array &lt;math&gt;A&lt;/math&gt; of size n that has been circularly shifted &lt;math&gt;k&lt;/math&gt; positions to the right. For example, [35, 42, 5, 15, 27, 29] is a sorted array that has been circularly shifted &lt;math&gt;k = 2&lt;/math&gt; positions, while [27, 29, 35, 42, 5, 15] has been shifted &lt;math&gt;k = 4&lt;/math&gt; positions.<br /> :• Suppose you know what &lt;math&gt;k&lt;/math&gt; is. Give an &lt;math&gt;O(1)&lt;/math&gt; algorithm to find the largest number in &lt;math&gt;A&lt;/math&gt;.<br /> :• Suppose you do not know what &lt;math&gt;k&lt;/math&gt; is. Give an &lt;math&gt;O(lg n)&lt;/math&gt; algorithm to find the largest number in &lt;math&gt;A&lt;/math&gt;. For partial credit, you may give an &lt;math&gt;O(n)&lt;/math&gt; algorithm.<br /> [[5.1|Solution]]<br /> <br /> <br /> :5.2. A sorted array of size n contains distinct integers between &lt;math&gt;1&lt;/math&gt; and &lt;math&gt;n + 1&lt;/math&gt; , with one element missing. Give an &lt;math&gt;O(log n)&lt;/math&gt; algorithm to find the missing integer, without using any extra space.<br /> <br /> <br /> :[[5.3]] Consider the numerical Twenty Questions game. In this game, the first player thinks of a number in the range 1 to &lt;math&gt;n&lt;/math&gt;. The second player has to figure out this number by asking the fewest number of true/false questions. Assume that nobody cheats.<br /> :(a) What is an optimal strategy if &lt;math&gt;n&lt;/math&gt; in known?<br /> :(b) What is a good strategy if &lt;math&gt;n&lt;/math&gt; is not known?<br /> [[5.3|Solution]]<br /> <br /> <br /> :5.4. You are given a unimodal array of &lt;math&gt;n&lt;/math&gt; distinct elements, meaning that its entries are in increasing order up until its maximum element, after which its elements are in decreasing order. Give an algorithm to compute the maximum element of a unimodal array that runs in &lt;math&gt;O(log n)&lt;/math&gt; time.<br /> <br /> <br /> :[[5.5]]. Suppose that you are given a sorted sequence of distinct integers &lt;math&gt;[a_1, a_2, . . . , a_n]&lt;/math&gt;. Give an &lt;math&gt;O(lg n)&lt;/math&gt; algorithm to determine whether there exists an index &lt;math&gt;i&lt;/math&gt; such that &lt;math&gt;a_i = i&lt;/math&gt;. For example, in [-10, -3, 3, 5, 7], &lt;math&gt;a_3 = 3&lt;/math&gt;. In [2, 3, 4, 5, 6, 7], there is no such &lt;math&gt;i&lt;/math&gt;.<br /> [[5.5|Solution]]<br /> <br /> <br /> :5.6. Suppose that you are given a sorted sequence of distinct integers &lt;math&gt;a = [a_1, a_2, . . . , a_n]&lt;/math&gt;, drawn from 1 to &lt;math&gt;m&lt;/math&gt; where &lt;math&gt;n &lt; m&lt;/math&gt;. Give an &lt;math&gt;O(lg n)&lt;/math&gt; algorithm to find an integer &lt;math&gt;\leq m&lt;/math&gt; that is not present in &lt;math&gt;a&lt;/math&gt;. For full credit, find the smallest such integer &lt;math&gt;x&lt;/math&gt; such that &lt;math&gt;1 \leq x \leq m&lt;/math&gt;.<br /> <br /> <br /> :[[5.7]]. Let &lt;math&gt;M&lt;/math&gt; be an &lt;math&gt;n * m&lt;/math&gt; integer matrix in which the entries of each row are sorted in increasing order (from left to right) and the entries in each column are in increasing order (from top to bottom). Give an efficient algorithm to find the position of an integer &lt;math&gt;x&lt;/math&gt; in &lt;math&gt;M&lt;/math&gt;, or to determine that &lt;math&gt;x&lt;/math&gt; is not there. How many comparisons of &lt;math&gt;x&lt;/math&gt; with matrix entries does your algorithm use in worst case?<br /> [[5.7|Solution]]<br /> <br /> ===Divide and Conquer Algorithms===<br /> <br /> :5.8. Given two sorted arrays &lt;math&gt;A&lt;/math&gt; and &lt;math&gt;B&lt;/math&gt; of size &lt;math&gt;n&lt;/math&gt; and &lt;math&gt;m&lt;/math&gt; respectively, find the median of the &lt;math&gt;n + m&lt;/math&gt; elements. The overall run time complexity should be &lt;math&gt;O(log(m + n))&lt;/math&gt;.<br /> <br /> <br /> :[[5.9]]. The largest subrange problem, discussed in Section 5.6, takes an array &lt;math&gt;A&lt;/math&gt; of &lt;math&gt;n&lt;/math&gt; numbers, and asks for the index pair &lt;math&gt;i&lt;/math&gt; and &lt;math&gt;j&lt;/math&gt; that maximizes &lt;math&gt;S = \sum_{k=1}^j A[k]&lt;/math&gt;. Give an &lt;math&gt;O(n)&lt;/math&gt; algorithm for largest subrange.<br /> [[5.9|Solution]]<br /> <br /> <br /> :5.10. We are given &lt;math&gt;n&lt;/math&gt; wooden sticks, each of integer length, where the &lt;math&gt;i&lt;/math&gt;th piece has length &lt;math&gt;L[i]&lt;/math&gt;. We seek to cut them so that we end up with &lt;math&gt;k&lt;/math&gt; pieces of exactly the same length, in addition to other fragments. Furthermore, we want these &lt;math&gt;k&lt;/math&gt; pieces to be as large as possible.<br /> :(a) Given four wood sticks, of lengths &lt;math&gt;L = {10, 6, 5, 3}&lt;/math&gt;, what are the largest sized pieces you can get for &lt;math&gt;k = 4&lt;/math&gt;? (Hint: the answer is not 3).<br /> :(b) Give a correct and efficient algorithm that, for a given &lt;math&gt;L&lt;/math&gt; and &lt;math&gt;k&lt;/math&gt;, returns the maximum possible length of the &lt;math&gt;k&lt;/math&gt; equal pieces cut from the initial &lt;math&gt;n&lt;/math&gt; sticks.<br /> <br /> <br /> :[[5.11]]. Extend the convolution-based string-matching algorithm described in the text to the case of pattern matching with wildcard characters “*”, which match any character. For example, “sh*t” should match both “shot” and “shut”.<br /> [[5.11|Solution]]<br /> <br /> ===Recurrence Relations===<br /> <br /> :5.12. In Section 5.3, it is asserted that any polynomial can be represented by a recurrence. Find a recurrence relation that represents the polynomial &lt;math&gt;a_n = n^2&lt;/math&gt;.<br /> <br /> <br /> :[[5.13]]. Suppose you are choosing between the following three algorithms:<br /> :• Algorithm &lt;math&gt;A&lt;/math&gt; solves problems by dividing them into five subproblems of half the size, recursively solving each subproblem, and then combining the solutions in linear time.<br /> :• Algorithm &lt;math&gt;B&lt;/math&gt; solves problems of size &lt;math&gt;n&lt;/math&gt; by recursively solving two subproblems of size &lt;math&gt;n - 1&lt;/math&gt; and then combining the solutions in constant time.<br /> :• Algorithm &lt;math&gt;C&lt;/math&gt; solves problems of size &lt;math&gt;n&lt;/math&gt; by dividing them into nine subproblems of size &lt;math&gt;n/3&lt;/math&gt;, recursively solving each subproblem, and then combining the solutions in &lt;math&gt;\Theta(n^2)&lt;/math&gt; time.<br /> :What are the running times of each of these algorithms (in big O notation), and which would you choose?<br /> [[5.13|Solution]]<br /> <br /> <br /> :5.14. Solve the following recurrence relations and give a &lt;math&gt;\Theta&lt;/math&gt; bound for each of them:<br /> :(a) &lt;math&gt;T(n) = 2T(n/3) + 1&lt;/math&gt;<br /> :(b) &lt;math&gt;T(n) = 5T(n/4) + n&lt;/math&gt;<br /> :(c) &lt;math&gt;T(n) = 7T(n/7) + n&lt;/math&gt;<br /> :(d) &lt;math&gt;T(n) = 9T(n/3) + n^2&lt;/math&gt;<br /> <br /> <br /> :[[5.15]]. Use the master theorem to solve the following recurrence relations:<br /> :(a) &lt;math&gt;T(n) = 64T(n/4) + n^4&lt;/math&gt;<br /> :(b) &lt;math&gt;T(n) = 64T(n/4) + n^3&lt;/math&gt;<br /> :(c) &lt;math&gt;T(n) = 64T(n/4) + 128&lt;/math&gt;<br /> [[5.15|Solution]]<br /> <br /> <br /> :5.16. Give asymptotically tight upper (Big Oh) bounds for &lt;math&gt;T(n)&lt;/math&gt; in each of the following recurrences. Justify your solutions by naming the particular case of the master theorem, by iterating the recurrence, or by using the substitution method:<br /> :(a) &lt;math&gt;T(n) = T(n - 2) + 1&lt;/math&gt;.<br /> :(b) &lt;math&gt;T(n) = 2T(n/2) + n lg^2 n&lt;/math&gt;.<br /> :(c) &lt;math&gt;T(n) = 9T(n/4) + n^2&lt;/math&gt;.<br /> <br /> <br /> Back to [[Chapter List]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Chapter_4&diff=449 Chapter 4 2020-10-01T18:06:49Z <p>Algowikiadmin: Protected &quot;Chapter 4&quot; ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))</p> <hr /> <div>=Sorting=<br /> <br /> ===Applications of Sorting: Numbers===<br /> <br /> :[[4.1]]. The Grinch is given the job of partitioning &lt;math&gt;2n&lt;/math&gt; players into two teams of &lt;math&gt;n&lt;/math&gt; players each. Each player has a numerical rating that measures how good he or she is at the game. The Grinch seeks to divide the players as ''unfairly'' as possible, so as to create the biggest possible talent imbalance between the teams. Show how the Grinch can do the job in &lt;math&gt;O(n log n)&lt;/math&gt; time.<br /> [[4.1|Solution]]<br /> <br /> <br /> :4.2. For each of the following problems, give an algorithm that finds the desired numbers within the given amount of time. To keep your answers brief, feel free to use algorithms from the book as subroutines. For the example, &lt;math&gt;S = {6, 13, 19, 3, 8}&lt;/math&gt;, 19 - 3 maximizes the difference, while 8 - 6 minimizes the difference.<br /> :(a) Let &lt;math&gt;S&lt;/math&gt; be an ''unsorted'' array of &lt;math&gt;n&lt;/math&gt; integers. Give an algorithm that finds the pair &lt;math&gt;x, y \in S&lt;/math&gt; that ''maximizes'' &lt;math&gt;|x-y|&lt;/math&gt;. Your algorithm must run in &lt;math&gt;O(n)&lt;/math&gt; worst-case time.<br /> :(b) Let &lt;math&gt;S&lt;/math&gt; be a ''sorted'' array of &lt;math&gt;n&lt;/math&gt; integers. Give an algorithm that finds the pair &lt;math&gt;x, y \in S&lt;/math&gt; that ''maximizes'' &lt;math&gt;|x - y|&lt;/math&gt;. Your algorithm must run in &lt;math&gt;O(1)&lt;/math&gt; worst-case time.<br /> :(c) Let &lt;math&gt;S&lt;/math&gt; be an ''unsorted'' array of &lt;math&gt;n&lt;/math&gt; integers. Give an algorithm that finds the pair &lt;math&gt;x, y \in S&lt;/math&gt; that ''minimizes'' &lt;math&gt;|x - y|&lt;/math&gt;, for &lt;math&gt;x \neq y&lt;/math&gt;. Your algorithm must run in &lt;math&gt;O(n log n)&lt;/math&gt; worst-case time.<br /> :(d) Let &lt;math&gt;S&lt;/math&gt; be a ''sorted'' array of &lt;math&gt;n&lt;/math&gt; integers. Give an algorithm that finds the pair &lt;math&gt;x, y \in S&lt;/math&gt; that ''minimizes'' &lt;math&gt;|x - y|&lt;/math&gt;, for &lt;math&gt;x \neq y&lt;/math&gt;. Your algorithm must run in &lt;math&gt;O(n)&lt;/math&gt; worst-case time.<br /> <br /> <br /> :[[4.3]]. Take a list of &lt;math&gt;2n&lt;/math&gt; real numbers as input. Design an &lt;math&gt;O(n log n)&lt;/math&gt; algorithm that partitions the numbers into &lt;math&gt;n&lt;/math&gt; pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus, the third partition has 10 as its maximum sum, which is the minimum over the three partitions.<br /> [[4.3|Solution]]<br /> <br /> <br /> :4.4. Assume that we are given &lt;math&gt;n&lt;/math&gt; pairs of items as input, where the first item is a number and the second item is one of three colors (red, blue, or yellow). Further assume that the items are sorted by number. Give an &lt;math&gt;O(n)&lt;/math&gt; algorithm to sort the items by color (all reds before all blues before all yellows) such that the numbers for identical colors stay sorted.<br /> :For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow).<br /> <br /> <br /> :[[4.5]]. The ''mode'' of a bag of numbers is the number that occurs most frequently in the set. The set {4, 6, 2, 4, 3, 1} has a mode of 4. Give an efficient and correct algorithm to compute the mode of a bag of &lt;math&gt;n&lt;/math&gt; numbers.<br /> [[4.5|Solution]]<br /> <br /> <br /> :4.6. Given two sets &lt;math&gt;S_1&lt;/math&gt; and &lt;math&gt;S_2&lt;/math&gt; (each of size &lt;math&gt;n&lt;/math&gt;), and a number &lt;math&gt;x&lt;/math&gt;, describe an &lt;math&gt;O(n log n)&lt;/math&gt; algorithm for finding whether there exists a pair of elements, one from &lt;math&gt;S_1&lt;/math&gt; and one from &lt;math&gt;S_2&lt;/math&gt;, that add up to &lt;math&gt;x&lt;/math&gt;. (For partial credit, give a &lt;math&gt;\Theta(n^2)&lt;/math&gt; algorithm for this problem.)<br /> <br /> <br /> :[[4.7]]. Give an efficient algorithm to take the array of citation counts (each count is a non-negative integer) of a researcher’s papers, and compute the researcher’s &lt;math&gt;h&lt;/math&gt;-index. By definition, a scientist has index &lt;math&gt;h&lt;/math&gt; if &lt;math&gt;h&lt;/math&gt; of his or her &lt;math&gt;n&lt;/math&gt; papers have been cited at least &lt;math&gt;h&lt;/math&gt; times, while the other &lt;math&gt;n-h&lt;/math&gt; papers each have no more than &lt;math&gt;h&lt;/math&gt; citations.<br /> [[4.7|Solution]]<br /> <br /> <br /> :4.8. Outline a reasonable method of solving each of the following problems. Give the order of the worst-case complexity of your methods.<br /> :(a) You are given a pile of thousands of telephone bills and thousands of checks sent in to pay the bills. Find out who did not pay.<br /> :(b) You are given a printed list containing the title, author, call number, and publisher of all the books in a school library and another list of thirty publishers. Find out how many of the books in the library were published by each company.<br /> :(c) You are given all the book checkout cards used in the campus library during the past year, each of which contains the name of the person who took out the book. Determine how many distinct people checked out at least one book.<br /> <br /> <br /> :[[4.9]]. Given a set &lt;math&gt;S&lt;/math&gt; of &lt;math&gt;n&lt;/math&gt; integers and an integer &lt;math&gt;T&lt;/math&gt;, give an &lt;math&gt;O(n^{k-1}log n)&lt;/math&gt; algorithm to test whether &lt;math&gt;k&lt;/math&gt; of the integers in &lt;math&gt;S&lt;/math&gt; add up to &lt;math&gt;T&lt;/math&gt;.<br /> [[4.9|Solution]]<br /> <br /> <br /> :4.10. We are given a set of &lt;math&gt;S&lt;/math&gt; containing &lt;math&gt;n&lt;/math&gt; real numbers and a real number &lt;math&gt;x&lt;/math&gt;, and seek efficient algorithms to determine whether two elements of &lt;math&gt;S&lt;/math&gt; exist whose sum is exactly &lt;math&gt;x&lt;/math&gt;.<br /> :(a) Assume that &lt;math&gt;S&lt;/math&gt; is unsorted. Give an &lt;math&gt;O(n log n)&lt;/math&gt; algorithm for the problem.<br /> :(b) Assume that &lt;math&gt;S&lt;/math&gt; is sorted. Give an &lt;math&gt;O(n)&lt;/math&gt; algorithm for the problem.<br /> <br /> <br /> :[[4.11]]. Design an &lt;math&gt;O(n)&lt;/math&gt; algorithm that, given a list of &lt;math&gt;n&lt;/math&gt; elements, finds all the elements that appear more than &lt;math&gt;n/2&lt;/math&gt; times in the list. Then, design an &lt;math&gt;O(n)&lt;/math&gt; algorithm that, given a list of &lt;math&gt;n&lt;/math&gt; elements, finds all the elements that appear more than &lt;math&gt;n/4&lt;/math&gt; times.<br /> [[4.11|Solution]]<br /> <br /> ===Applications of Sorting: Intervals and Sets===<br /> <br /> :4.12. Give an efficient algorithm to compute the union of sets &lt;math&gt;A&lt;/math&gt; and &lt;math&gt;B&lt;/math&gt;, where &lt;math&gt;n = max(|A|, |B|)&lt;/math&gt;. The output should be an array of distinct elements that form the union of the sets.<br /> :(a) Assume that &lt;math&gt;A&lt;/math&gt; and &lt;math&gt;B&lt;/math&gt; are unsorted arrays. Give an &lt;math&gt;O(n log n)&lt;/math&gt; algorithm for the problem.<br /> :(b) Assume that &lt;math&gt;A&lt;/math&gt; and &lt;math&gt;B&lt;/math&gt; are sorted arrays. Give an &lt;math&gt;O(n)&lt;/math&gt; algorithm for the problem.<br /> <br /> <br /> :[[4.13]]. A camera at the door tracks the entry time &lt;math&gt;a_i&lt;/math&gt; and exit time &lt;math&gt;b_i&lt;/math&gt; (assume &lt;math&gt;b_i &gt; a_i&lt;/math&gt;) for each of &lt;math&gt;n&lt;/math&gt; persons &lt;math&gt;p_i&lt;/math&gt; attending a party. Give an &lt;math&gt;O(n log n)&lt;/math&gt; algorithm that analyzes this data to determine the time when the most people were simultaneously present at the party. You may assume that all entry and exit times are distinct (no ties).<br /> [[4.13|Solution]]<br /> <br /> <br /> :4.14. Given a list &lt;math&gt;I&lt;/math&gt; of &lt;math&gt;n&lt;/math&gt; intervals, specified as &lt;math&gt;(x_i, y_i)&lt;/math&gt; pairs, return a list where the overlapping intervals are merged. For &lt;math&gt;I = {(1, 3),(2, 6),(8, 10),(7, 18)}&lt;/math&gt; the output should be &lt;math&gt;{(1, 6),(7, 18)}&lt;/math&gt;. Your algorithm should run in worst-case &lt;math&gt;O(n log n)&lt;/math&gt; time complexity.<br /> <br /> <br /> :[[4.15]]. You are given a set &lt;math&gt;S&lt;/math&gt; of &lt;math&gt;n&lt;/math&gt; intervals on a line, with the &lt;math&gt;i&lt;/math&gt;th interval described by its left and right endpoints &lt;math&gt;(l_i, r_i)&lt;/math&gt;. Give an &lt;math&gt;O(n log n)&lt;/math&gt; algorithm to identify a point &lt;math&gt;p&lt;/math&gt; on the line that is in the largest number of intervals.<br /> :As an example, for &lt;math&gt;S = {(10, 40),(20, 60),(50, 90),(15, 70)}&lt;/math&gt; no point exists in all four intervals, but &lt;math&gt;p = 50&lt;/math&gt; is an example of a point in three intervals. You can assume an endpoint counts as being in its interval.<br /> [[4.15|Solution]]<br /> <br /> <br /> :4.16. You are given a set &lt;math&gt;S&lt;/math&gt; of &lt;math&gt;n&lt;/math&gt; segments on the line, where segment &lt;math&gt;S_i&lt;/math&gt; ranges from &lt;math&gt;l_i&lt;/math&gt; to &lt;math&gt;r_i&lt;/math&gt;. Give an efficient algorithm to select the fewest number of segments whose union completely covers the interval from 0 to &lt;math&gt;m&lt;/math&gt;.<br /> <br /> ===Heaps===<br /> <br /> :[[4.17]]. Devise an algorithm for finding the &lt;math&gt;k&lt;/math&gt; smallest elements of an unsorted set of &lt;math&gt;n&lt;/math&gt; integers in &lt;math&gt;O(n + k log n)&lt;/math&gt;.<br /> [[4.17|Solution]]<br /> <br /> <br /> :4.18. Give an &lt;math&gt;O(n log k)&lt;/math&gt;-time algorithm that merges &lt;math&gt;k&lt;/math&gt; sorted lists with a total of &lt;math&gt;n&lt;/math&gt; elements into one sorted list. (Hint: use a heap to speed up the obvious &lt;math&gt;O(know)&lt;/math&gt;-time algorithm).<br /> <br /> <br /> :[[4.19]]. You wish to store a set of &lt;math&gt;n&lt;/math&gt; numbers in either a max-heap or a sorted array. For each application below, state which data structure is better, or if it does not matter. Explain your answers.<br /> :(a) Find the maximum element quickly.<br /> :(b) Delete an element quickly.<br /> :(c) Form the structure quickly.<br /> :(d) Find the minimum element quickly.<br /> [[4.19|Solution]]<br /> <br /> <br /> :4.20. (a) Give an efficient algorithm to find the second-largest key among &lt;math&gt;n&lt;/math&gt; keys. You can do better than &lt;math&gt;2n - 3&lt;/math&gt; comparisons.<br /> :(b) Then, give an efficient algorithm to find the third-largest key among &lt;math&gt;n&lt;/math&gt; keys. How many key comparisons does your algorithm do in the worst case? Must your algorithm determine which key is largest and second-largest in the process?<br /> <br /> ===Quicksort===<br /> <br /> :[[4.21]]. Use the partitioning idea of quicksort to give an algorithm that finds the median element of an array of &lt;math&gt;n&lt;/math&gt; integers in expected &lt;math&gt;O(n)&lt;/math&gt; time. (Hint: must you look at both sides of the partition?)<br /> [[4.21|Solution]]<br /> <br /> <br /> :4.22. The ''median'' of a set of &lt;math&gt;n&lt;/math&gt; values is the &lt;math&gt;[n/2]&lt;/math&gt;th smallest value.<br /> :(a) Suppose quicksort always pivoted on the median of the current sub-array. How many comparisons would quicksort make then in the worst case?<br /> :(b) Suppose quicksort always pivoted on the &lt;math&gt;[n/3]&lt;/math&gt;th smallest value of the current sub-array. How many comparisons would be made then in the worst case?<br /> <br /> <br /> :[[4.23]]. Suppose an array &lt;math&gt;A&lt;/math&gt; consists of &lt;math&gt;n&lt;/math&gt; elements, each of which is ''red'', ''white'', or ''blue'. We seek to sort the elements so that all the ''reds'' come before all the ''whites'', which come before all the ''blues''. The only operations permitted on the keys are:<br /> ::• ''Examine''(&lt;math&gt;A,i&lt;/math&gt;) – report the color of the &lt;math&gt;i&lt;/math&gt;th element of &lt;math&gt;A&lt;/math&gt;.<br /> ::• ''Swap''(&lt;math&gt;A,i,j&lt;/math&gt;) – swap the &lt;math&gt;i&lt;/math&gt;th element of &lt;math&gt;A&lt;/math&gt; with the &lt;math&gt;j&lt;/math&gt;th element.<br /> :Find a correct and efficient algorithm for red–white–blue sorting. There is a linear-time solution.<br /> [[4.23|Solution]]<br /> <br /> <br /> :4.24. Give an efficient algorithm to rearrange an array of &lt;math&gt;n&lt;/math&gt; keys so that all the negative keys precede all the non-negative keys. Your algorithm must be in-place, meaning you cannot allocate another array to temporarily hold the items. How fast is your algorithm?<br /> <br /> <br /> :[[4.25]]. Consider a given pair of different elements in an input array to be sorted, say &lt;math&gt;z_i&lt;/math&gt; and &lt;math&gt;z_j&lt;/math&gt; . What is the most number of times &lt;math&gt;z_i&lt;/math&gt; and &lt;math&gt;z_j&lt;/math&gt; might be compared with each other during an execution of quicksort?<br /> [[4.25|Solution]]<br /> <br /> <br /> :4.26. Define the recursion depth of quicksort as the maximum number of successive recursive calls it makes before hitting the base case. What are the minimum and maximum possible recursion depths for randomized quicksort?<br /> <br /> <br /> :[[4.27]]. Suppose you are given a permutation &lt;math&gt;p&lt;/math&gt; of the integers 1 to &lt;math&gt;n&lt;/math&gt;, and seek to sort them to be in increasing order &lt;math&gt;[1, . . . , n]&lt;/math&gt;. The only operation at your disposal is ''reverse''&lt;math&gt;(p,i,j)&lt;/math&gt;, which reverses the elements of a subsequence &lt;math&gt;p_i, . . . , p_j&lt;/math&gt; in the permutation. For the permutation [1, 4, 3, 2, 5] one reversal (of the second through fourth elements) suffices to sort.<br /> :• Show that it is possible to sort any permutation using &lt;math&gt;O(n)&lt;/math&gt; reversals.<br /> :• Now suppose that the cost of ''reverse''&lt;math&gt;(p,i,j)&lt;/math&gt; is equal to its length, the number of elements in the range, &lt;math&gt;|j - i| + 1&lt;/math&gt;. Design an algorithm that sorts &lt;math&gt;p&lt;/math&gt; in &lt;math&gt;O(n log^2 n)&lt;/math&gt; cost. Analyze the running time and cost of your algorithm and prove correctness.<br /> [[4.27|Solution]]<br /> <br /> ===Mergesort===<br /> <br /> :4.28. Consider the following modification to merge sort: divide the input array into thirds (rather than halves), recursively sort each third, and finally combine the results using a three-way merge subroutine. What is the worst-case running time of this modified merge sort?<br /> <br /> <br /> :[[4.29]]. Suppose you are given &lt;math&gt;k&lt;/math&gt; sorted arrays, each with &lt;math&gt;n&lt;/math&gt; elements, and you want to combine them into a single sorted array of &lt;math&gt;kn&lt;/math&gt; elements. One approach is to use the merge subroutine repeatedly, merging the first two arrays, then merging the result with the third array, then with the fourth array, and so on until you merge in the &lt;math&gt;k&lt;/math&gt;th and final input array. What is the running time?<br /> [[4.29|Solution]]<br /> <br /> <br /> :4.30. Consider again the problem of merging &lt;math&gt;k&lt;/math&gt; sorted length-&lt;math&gt;n&lt;/math&gt; arrays into a single sorted length-&lt;math&gt;kn&lt;/math&gt; array. Consider the algorithm that first divides the &lt;math&gt;k&lt;/math&gt; arrays into &lt;math&gt;k/2&lt;/math&gt; pairs of arrays, and uses the merge subroutine to combine each pair, resulting in &lt;math&gt;k/2&lt;/math&gt; sorted length-&lt;math&gt;2n&lt;/math&gt; arrays. The algorithm repeats this step until there is only one length-&lt;math&gt;know&lt;/math&gt; sorted array. What is the running time as a function of &lt;math&gt;n&lt;/math&gt; and &lt;math&gt;k&lt;/math&gt;?<br /> <br /> ===Other Sorting Alogrithims===<br /> <br /> :[[4.31]]. Stable sorting algorithms leave equal-key items in the same relative order as in the original permutation. Explain what must be done to ensure that mergesort is a stable sorting algorithm.<br /> [[4.31|Solution]]<br /> <br /> <br /> :4.32. Wiggle sort: Given an unsorted array &lt;math&gt;A&lt;/math&gt;, reorder it such that &lt;math&gt;A &lt; A &gt; A &lt; A . . . .&lt;/math&gt; For example, one possible answer for input [3, 1, 4, 2, 6, 5] is [1, 3, 2, 5, 4, 6]. Can you do it in &lt;math&gt;O(n)&lt;/math&gt; time using only &lt;math&gt;O(1)&lt;/math&gt; space?<br /> <br /> <br /> :[[4.33]]. Show that &lt;math&gt;n&lt;/math&gt; positive integers in the range 1 to &lt;math&gt;k&lt;/math&gt; can be sorted in &lt;math&gt;O(n log k)&lt;/math&gt; time. The interesting case is when &lt;math&gt;k \ll n&lt;/math&gt;.<br /> [[4.33|Solution]]<br /> <br /> <br /> :4.34. Consider a sequence &lt;math&gt;S&lt;/math&gt; of &lt;math&gt;n&lt;/math&gt; integers with many duplications, such that the number of distinct integers in &lt;math&gt;S&lt;/math&gt; is &lt;math&gt;O(log n)&lt;/math&gt;. Give an &lt;math&gt;O(n log log n)&lt;/math&gt; worst-case time algorithm to sort such sequences.<br /> <br /> <br /> :[[4.35]]. Let &lt;math&gt;A[1..n]&lt;/math&gt; be an array such that the first &lt;math&gt;n -<br /> \sqrt n&lt;/math&gt; elements are already sorted (though we know nothing about the remaining elements). Give an algorithm that sorts &lt;math&gt;A&lt;/math&gt; in substantially better than &lt;math&gt;n log n&lt;/math&gt; steps.<br /> [[4.35|Solution]]<br /> <br /> <br /> :4.36. Assume that the array &lt;math&gt;A[1..n]&lt;/math&gt; only has numbers from &lt;math&gt;{1, . . . , n^2}&lt;/math&gt; but that at most &lt;math&gt;log log n&lt;/math&gt; of these numbers ever appear. Devise an algorithm that sorts &lt;math&gt;A&lt;/math&gt; in substantially less than &lt;math&gt;O(n log n)&lt;/math&gt;.<br /> <br /> <br /> :[[4.37]]. Consider the problem of sorting a sequence of &lt;math&gt;n&lt;/math&gt; 0’s and 1’s using comparisons. For each comparison of two values &lt;math&gt;x&lt;/math&gt; and &lt;math&gt;y&lt;/math&gt;, the algorithm learns which of &lt;math&gt;x &lt; y, x = y&lt;/math&gt;, or &lt;math&gt;x &gt; y&lt;/math&gt; holds.<br /> :(a) Give an algorithm to sort in &lt;math&gt;n - 1&lt;/math&gt; comparisons in the worst case. Show that your algorithm is optimal.<br /> :(b) Give an algorithm to sort in &lt;math&gt;2n/3&lt;/math&gt; comparisons in the average case (assuming each of the &lt;math&gt;n&lt;/math&gt; inputs is 0 or 1 with equal probability). Show that your algorithm is optimal.<br /> [[4.37|Solution]]<br /> <br /> <br /> :4.38. Let &lt;math&gt;P&lt;/math&gt; be a simple, but not necessarily convex, &lt;math&gt;n&lt;/math&gt;-sided polygon and &lt;math&gt;q&lt;/math&gt; an arbitrary point not necessarily in &lt;math&gt;P&lt;/math&gt;. Design an efficient algorithm to find a line segment originating from &lt;math&gt;q&lt;/math&gt; that intersects the maximum number of edges of &lt;math&gt;P&lt;/math&gt;. <br /> :In other words, if standing at point &lt;math&gt;q&lt;/math&gt;, in what direction should you aim a gun so the bullet will go through the largest number of walls. A bullet through a vertex of &lt;math&gt;P&lt;/math&gt; gets credit for only one wall. An &lt;math&gt;O(n log n)&lt;/math&gt; algorithm is possible.<br /> <br /> ===Lower Bounds===<br /> <br /> :[[4.39]]. In one of my research papers [Ski88], I discovered a comparison-based sorting algorithm that runs in &lt;math&gt;O(n log(\sqrt n))&lt;/math&gt;. Given the existence of an &lt;math&gt;\Omega(n log n)&lt;/math&gt; lower bound for sorting, how can this be possible?<br /> [[4.39|Solution]]<br /> <br /> <br /> :4.40. Mr. B. C. Dull claims to have developed a new data structure for priority queues that supports the operations ''Insert'', ''Maximum'', and ''Extract-Max''—all in &lt;math&gt;O(1)&lt;/math&gt; worst-case time. Prove that he is mistaken. (Hint: the argument does not involve a lot of gory details—just think about what this would imply about the &lt;math&gt;\Omega (n log n)&lt;/math&gt; lower bound for sorting.)<br /> <br /> ===Searching===<br /> <br /> :[[4.41]]. A company database consists of 10,000 sorted names, 40% of whom are known as good customers and who together account for 60% of the accesses to the database. There are two data structure options to consider for representing the database:<br /> :• Put all the names in a single array and use binary search.<br /> :• Put the good customers in one array and the rest of them in a second array. Only if we do not find the query name on a binary search of the first array do we do a binary search of the second array.<br /> :Demonstrate which option gives better expected performance. Does this change if linear search on an unsorted array is used instead of binary search for both options?<br /> [[4.41|Solution]]<br /> <br /> <br /> :4.42. A ''Ramanujan number'' can be written two different ways as the sum of two cubes—meaning there exist distinct positive integers &lt;math&gt;a, b, c&lt;/math&gt;, and &lt;math&gt;d&lt;/math&gt; such that &lt;math&gt;a^3 + b^3 = c^3 + d^3&lt;/math&gt;. For example, 1729 is a Ramanujan number because &lt;math&gt;1729 = 1^3 + 12^3 = 9^3 + 10^3&lt;/math&gt;.<br /> :(a) Give an efficient algorithm to test whether a given single integer &lt;math&gt;n&lt;/math&gt; is a Ramanujan number, with an analysis of the algorithm’s complexity.<br /> :(b) Now give an efficient algorithm to generate all the Ramanujan numbers between 1 and &lt;math&gt;n&lt;/math&gt;, with an analysis of its complexity.<br /> <br /> ===Implementaion Challenges===<br /> <br /> :[[4.43]]. Consider an n×n array &lt;math&gt;A&lt;/math&gt; containing integer elements (positive, negative, and zero). Assume that the elements in each row of &lt;math&gt;A&lt;/math&gt; are in strictly increasing order, and the elements of each column of &lt;math&gt;A&lt;/math&gt; are in strictly decreasing order. (Hence there cannot be two zeros in the same row or the same column.) Describe an efficient algorithm that counts the number of occurrences of the element 0 in &lt;math&gt;A&lt;/math&gt;. Analyze its running time.<br /> [[4.43|Solution]]<br /> <br /> <br /> :4.44. Implement versions of several different sorting algorithms, such as selection sort, insertion sort, heapsort, mergesort, and quicksort. Conduct experiments to assess the relative performance of these algorithms in a simple application that reads a large text file and reports exactly one instance of each word that appears within it. This application can be efficiently implemented by sorting all the words that occur in the text and then passing through the sorted sequence to identify one instance of each distinct word. Write a brief report with your conclusions.<br /> <br /> <br /> :[[4.45]]. Implement an external sort, which uses intermediate files to sort files bigger than main memory. Mergesort is a good algorithm to base such an implementation on. Test your program both on files with small records and on files with large records.<br /> [[4.45|Solution]]<br /> <br /> <br /> :4.46. Design and implement a parallel sorting algorithm that distributes data across several processors. An appropriate variation of mergesort is a likely candidate. Measure the speedup of this algorithm as the number of processors increases. Then compare the execution time to that of a purely sequential mergesort implementation. What are your experiences?<br /> <br /> ===Interview Problems===<br /> <br /> :[[4.47]]. If you are given a million integers to sort, what algorithm would you use to sort them? How much time and memory would that consume?<br /> [[4.47|Solution]]<br /> <br /> <br /> :4.48. Describe advantages and disadvantages of the most popular sorting algorithms.<br /> <br /> <br /> :[[4.49]]. Implement an algorithm that takes an input array and returns only the unique elements in it.<br /> [[4.49|Solution]]<br /> <br /> <br /> :4.50. You have a computer with only 4 GB of main memory. How do you use it to sort a large file of 500 GB that is on disk?<br /> <br /> <br /> :[[4.51]]. Design a stack that supports push, pop, and retrieving the minimum element in constant time.<br /> [[4.51|Solution]]<br /> <br /> <br /> :4.52. Given a search string of three words, find the smallest snippet of the document that contains all three of the search words—that is, the snippet with the smallest number of words in it. You are given the index positions where these words occur in the document, such as ''word1'': &lt;math&gt;(1, 4, 5)&lt;/math&gt;, ''word2'': &lt;math&gt;(3, 9, 10)&lt;/math&gt;, and ''word3'': &lt;math&gt;(2, 6, 15)&lt;/math&gt;. Each of the lists are in sorted order, as above.<br /> <br /> <br /> :[[4.53]]. You are given twelve coins. One of them is heavier or lighter than the rest. Identify this coin in just three weighings with a balance scale.<br /> [[4.53|Solution]]<br /> <br /> <br /> Back to [[Chapter List]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Chapter_3&diff=448 Chapter 3 2020-10-01T18:06:35Z <p>Algowikiadmin: Protected &quot;Chapter 3&quot; ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))</p> <hr /> <div>=Data Structures=<br /> <br /> ===Stacks, Queues, and Lists===<br /> <br /> :[[3.1]]. A common problem for compilers and text editors is determining whether the parentheses in a string are balanced and properly nested. For example, the string &lt;math&gt;((())())()&lt;/math&gt; contains properly nested pairs of parentheses, which the strings &lt;math&gt;)()(&lt;/math&gt; and &lt;math&gt;())&lt;/math&gt; do not. Give an algorithm that returns true if a string contains properly nested and balanced parentheses, and false if otherwise. For full credit, identify the position of the first offending parenthesis if the string is not properly nested and balanced.<br /> [[3.1|Solution]]<br /> <br /> <br /> :3.2. Give an algorithm that takes a string &lt;math&gt;S&lt;/math&gt; consisting of opening and closing parentheses, say )()(())()()))())))(, and finds the length of the longest balanced parentheses in &lt;math&gt;S&lt;/math&gt;, which is 12 in the example above. (Hint: The solution is not necessarily a contiguous run of parenthesis from &lt;math&gt;S&lt;/math&gt;.)<br /> <br /> <br /> :[[3.3]]. Give an algorithm to reverse the direction of a given singly linked list. In other words, after the reversal all pointers should now point backwards. Your algorithm should take linear time.<br /> [[3.3|Solution]]<br /> <br /> <br /> :3.4. Design a stack &lt;math&gt;S&lt;/math&gt; that supports ''S.push(x)'', ''S.pop()'', and ''S.findmin()'', which returns the minimum element of &lt;math&gt;S&lt;/math&gt;. All operations should run in constant time.<br /> <br /> <br /> :[[3.5]]. We have seen how dynamic arrays enable arrays to grow while still achieving constant-time amortized performance. This problem concerns extending dynamic arrays to let them both grow and shrink on demand.<br /> :(a) Consider an underflow strategy that cuts the array size in half whenever the array falls below half full. Give an example sequence of insertions and deletions where this strategy gives a bad amortized cost.<br /> :(b) Then, give a better underflow strategy than that suggested above, one that achieves constant amortized cost per deletion.<br /> [[3.5|Solution]]<br /> <br /> <br /> :3.6. Suppose you seek to maintain the contents of a refrigerator so as to minimize food spoilage. What data structure should you use, and how should you use it?<br /> <br /> <br /> :[[3.7]]. Work out the details of supporting constant-time deletion from a singly linked list as per the footnote from page 79, ideally to an actual implementation. Support the other operations as efficiently as possible. <br /> [[3.7|Solution]]<br /> <br /> ===Elementary Data Structures===<br /> <br /> :3.8. Tic-tac-toe is a game played on an &lt;math&gt;n * n&lt;/math&gt; board (typically &lt;math&gt;n = 3&lt;/math&gt;) where two players take consecutive turns placing “O” and “X” marks onto the board cells. The game is won if n consecutive “O” or ‘X” marks are placed in a row, column, or diagonal. Create a data structure with &lt;math&gt;O(n)&lt;/math&gt; space that accepts a sequence of moves, and reports in constant time whether the last move won the game.<br /> <br /> <br /> :[[3.9]]. Write a function which, given a sequence of digits 2–9 and a dictionary of &lt;math&gt;n&lt;/math&gt; words, reports all words described by this sequence when typed in on a standard telephone keypad. For the sequence 269 you should return any, box, boy, and cow, among other words.<br /> [[3.9|Solution]]<br /> <br /> <br /> :3.10. Two strings &lt;math&gt;X&lt;/math&gt; and &lt;math&gt;Y&lt;/math&gt; are anagrams if the letters of &lt;math&gt;X&lt;/math&gt; can be rearranged to form &lt;math&gt;Y&lt;/math&gt; . For example, silent/listen, and incest/insect are anagrams. Give an efficient algorithm to determine whether strings &lt;math&gt;X&lt;/math&gt; and &lt;math&gt;Y&lt;/math&gt; are anagrams.<br /> <br /> ===Trees and Other Dictionary Structures===<br /> <br /> :[[3.11]]. Design a dictionary data structure in which search, insertion, and deletion can all be processed in &lt;math&gt;O(1)&lt;/math&gt; time in the worst case. You may assume the set elements are integers drawn from a finite set &lt;math&gt;1, 2, .., n&lt;/math&gt;, and initialization can take &lt;math&gt;O(n)&lt;/math&gt; time.<br /> [[3.11|Solution]]<br /> <br /> <br /> :3.12. The maximum depth of a binary tree is the number of nodes on the path from the root down to the most distant leaf node. Give an &lt;math&gt;O(n)&lt;/math&gt; algorithm to find the maximum depth of a binary tree with &lt;math&gt;n&lt;/math&gt; nodes.<br /> <br /> <br /> :[[3.13]]. Two elements of a binary search tree have been swapped by mistake. Give an &lt;math&gt;O(n)&lt;/math&gt; algorithm to identify these two elements so they can be swapped back.<br /> [[3.13|Solution]]<br /> <br /> <br /> :3.14. Given two binary search trees, merge them into a doubly linked list in sorted order.<br /> <br /> <br /> :[[3.15]]. Describe an &lt;math&gt;O(n)&lt;/math&gt;-time algorithm that takes an &lt;math&gt;n&lt;/math&gt;-node binary search tree and constructs an equivalent height-balanced binary search tree. In a height-balanced binary search tree, the difference between the height of the left and right subtrees of every node is never more than 1.<br /> [[3.15|Solution]]<br /> <br /> <br /> :3.16. Find the storage efficiency ratio (the ratio of data space over total space) for each of the following binary tree implementations on n nodes:<br /> :(a) All nodes store data, two child pointers, and a parent pointer. The data field requires 4 bytes and each pointer requires 4 bytes.<br /> :(b) Only leaf nodes store data; internal nodes store two child pointers. The data field requires four bytes and each pointer requires two bytes.<br /> <br /> <br /> :[[3.17]]. Give an &lt;math&gt;O(n)&lt;/math&gt; algorithm that determines whether a given &lt;math&gt;n&lt;/math&gt;-node binary tree is height-balanced (see Problem 3-15).<br /> [[3.17|Solution]]<br /> <br /> <br /> :3.18. Describe how to modify any balanced tree data structure such that search, insert, delete, minimum, and maximum still take &lt;math&gt;O(log n)&lt;/math&gt; time each, but successor and predecessor now take &lt;math&gt;O(1)&lt;/math&gt; time each. Which operations have to be modified to support this?<br /> <br /> <br /> :[[3.19]]. Suppose you have access to a balanced dictionary data structure that supports each of the operations search, insert, delete, minimum, maximum, successor, and predecessor in &lt;math&gt;O(log n)&lt;/math&gt; time. Explain how to modify the insert and delete operations so they still take &lt;math&gt;O(log n)&lt;/math&gt; but now minimum and maximum take &lt;math&gt;O(1)&lt;/math&gt; time. (Hint: think in terms of using the abstract dictionary operations, instead of mucking about with pointers and the like.)<br /> [[3.19|Solution]]<br /> <br /> <br /> :3.20. Design a data structure to support the following operations:<br /> :• ''insert''(&lt;math&gt;x,T&lt;/math&gt;) – Insert item &lt;math&gt;x&lt;/math&gt; into the set &lt;math&gt;T&lt;/math&gt;.<br /> :• ''delete''(&lt;math&gt;k,T&lt;/math&gt;) – Delete the &lt;math&gt;k&lt;/math&gt;th smallest element from &lt;math&gt;T&lt;/math&gt;.<br /> :• ''member''(&lt;math&gt;x,T&lt;/math&gt;) – Return true iff &lt;math&gt;x \in T&lt;/math&gt;.<br /> :All operations must take &lt;math&gt;O(log n)&lt;/math&gt; time on an &lt;math&gt;n&lt;/math&gt;-element set.<br /> <br /> <br /> :[[3.21]]. A ''concatenate operation'' takes two sets &lt;math&gt;S_1&lt;/math&gt; and &lt;math&gt;S_2&lt;/math&gt;, where every key in &lt;math&gt;S_1&lt;/math&gt; is smaller than any key in &lt;math&gt;S_2&lt;/math&gt;, and merges them. Give an algorithm to concatenate two binary search trees into one binary search tree. The worst-case running time should be &lt;math&gt;O(h)&lt;/math&gt;, where &lt;math&gt;h&lt;/math&gt; is the maximal height of the two trees.<br /> [[3.21|Solution]]<br /> <br /> ===Applications of Tree Structures===<br /> <br /> :3.22. Design a data structure that supports the following two operations:<br /> :• ''insert''(&lt;math&gt;x&lt;/math&gt;) – Insert item &lt;math&gt;x&lt;/math&gt; from the data stream to the data structure.<br /> :• ''median''() – Return the median of all elements so far.<br /> :All operations must take &lt;math&gt;O(log n)&lt;/math&gt; time on an &lt;math&gt;n&lt;/math&gt;-element set.<br /> <br /> <br /> :[[3.23]]. Assume we are given a standard dictionary (balanced binary search tree) defined on a set of &lt;math&gt;n&lt;/math&gt; strings, each of length at most &lt;math&gt;l&lt;/math&gt;. We seek to print out all strings beginning with a particular prefix &lt;math&gt;p&lt;/math&gt;. Show how to do this in &lt;math&gt;O(ml log n)&lt;/math&gt; time, where &lt;math&gt;m&lt;/math&gt; is the number of strings.<br /> [[3.23|Solution]]<br /> <br /> <br /> :3.24. An array &lt;math&gt;A&lt;/math&gt; is called &lt;math&gt;k&lt;/math&gt;-unique if it does not contain a pair of duplicate elements within &lt;math&gt;k&lt;/math&gt; positions of each other, that is, there is no &lt;math&gt;i&lt;/math&gt; and &lt;math&gt;j&lt;/math&gt; such that &lt;math&gt;A[i] = A[j]&lt;/math&gt; and &lt;math&gt;|j - i| \leq k&lt;/math&gt;. Design a worst-case &lt;math&gt;O(n log k)&lt;/math&gt; algorithm to test if &lt;math&gt;A&lt;/math&gt; is &lt;math&gt;k&lt;/math&gt;-unique.<br /> <br /> <br /> :[[3.25]]. In the ''bin-packing problem'', we are given &lt;math&gt;n&lt;/math&gt; objects, each weighing at most 1 kilogram. Our goal is to find the smallest number of bins that will hold the &lt;math&gt;n&lt;/math&gt; objects, with each bin holding 1 kilogram at most.<br /> :• The ''best-fit heuristic'' for bin packing is as follows. Consider the objects in the order in which they are given. For each object, place it into the partially filled bin with the smallest amount of extra room after the object is inserted. If no such bin exists, start a new bin. Design an algorithm that implements the best-fit heuristic (taking as input the &lt;math&gt;n&lt;/math&gt; weights &lt;math&gt;w_1, w_2, ..., w_n&lt;/math&gt; and outputting the number of bins used) in &lt;math&gt;O(n log n)&lt;/math&gt; time.<br /> :• Repeat the above using the ''worst-fit heuristic'', where we put the next object into the partially filled bin with the largest amount of extra room after the object is inserted.<br /> [[3.25|Solution]]<br /> <br /> <br /> :3.26. Suppose that we are given a sequence of &lt;math&gt;n&lt;/math&gt; values &lt;math&gt;x_1, x_2, ..., x_n&lt;/math&gt; and seek to quickly answer repeated queries of the form: given &lt;math&gt;i&lt;/math&gt; and &lt;math&gt;j&lt;/math&gt;, find the smallest value in &lt;math&gt;x_i, . . . , x_j&lt;/math&gt;.<br /> :(a) Design a data structure that uses &lt;math&gt;O(n^2)&lt;/math&gt; space and answers queries in &lt;math&gt;O(1)&lt;/math&gt; time.<br /> :(b) Design a data structure that uses &lt;math&gt;O(n)&lt;/math&gt; space and answers queries in &lt;math&gt;O(log n)&lt;/math&gt; time. For partial credit, your data structure can use &lt;math&gt;O(n log n)&lt;/math&gt; space and have &lt;math&gt;O(log n)&lt;/math&gt; query time.<br /> <br /> <br /> :[[3.27]]. Suppose you are given an input set &lt;math&gt;S&lt;/math&gt; of &lt;math&gt;n&lt;/math&gt; integers, and a black box that if given any sequence of integers and an integer &lt;math&gt;k&lt;/math&gt; instantly and correctly answers whether there is a subset of the input sequence whose sum is exactly &lt;math&gt;k&lt;/math&gt;. Show how to use the black box &lt;math&gt;O(n)&lt;/math&gt; times to find a subset of &lt;math&gt;S&lt;/math&gt; that adds up to &lt;math&gt;k&lt;/math&gt;.<br /> [[3.27|Solution]]<br /> <br /> <br /> :3.28. Let &lt;math&gt;A[1..n]&lt;/math&gt; be an array of real numbers. Design an algorithm to perform any sequence of the following operations:<br /> :• ''Add''(&lt;math&gt;i,y&lt;/math&gt;) – Add the value &lt;math&gt;y&lt;/math&gt; to the &lt;math&gt;i&lt;/math&gt;th number.<br /> :• ''Partial-sum''(&lt;math&gt;i&lt;/math&gt;) – Return the sum of the first &lt;math&gt;i&lt;/math&gt; numbers, that is, &lt;math&gt;\sum_{j=1}^i A[j]&lt;/math&gt;.<br /> :There are no insertions or deletions; the only change is to the values of the numbers. Each operation should take &lt;math&gt;O(log n)&lt;/math&gt; steps. You may use one additional array of size &lt;math&gt;n&lt;/math&gt; as a work space.<br /> <br /> <br /> :[[3.29]]. Extend the data structure of the previous problem to support insertions and deletions. Each element now has both a ''key'' and a ''value''. An element is accessed by its key, but the addition operation is applied to the values. The ''Partial_sum'' operation is different.<br /> :• ''Add''(&lt;math&gt;k,y&lt;/math&gt;) – Add the value &lt;math&gt;y&lt;/math&gt; to the item with key &lt;math&gt;k&lt;/math&gt;.<br /> :• ''Insert''(&lt;math&gt;k,y&lt;/math&gt;) – Insert a new item with key &lt;math&gt;k&lt;/math&gt; and value &lt;math&gt;y&lt;/math&gt;.<br /> :• ''Delete''(&lt;math&gt;k&lt;/math&gt;) – Delete the item with key &lt;math&gt;k&lt;/math&gt;.<br /> :• ''Partial-sum''(&lt;math&gt;k&lt;/math&gt;) – Return the sum of all the elements currently in the set whose key is less than &lt;math&gt;k&lt;/math&gt;, that is, &lt;math&gt;\sum_{i &lt; k} x_i&lt;/math&gt;. <br /> :The worst-case running time should still be &lt;math&gt;O(n log n)&lt;/math&gt; for any sequence of &lt;math&gt;O(n)&lt;/math&gt; operations.<br /> [[3.29|Solution]]<br /> <br /> <br /> :3.30. You are consulting for a hotel that has &lt;math&gt;n&lt;/math&gt; one-bed rooms. When a guest checks in, they ask for a room whose number is in the range &lt;math&gt;[l, h]&lt;/math&gt;. Propose a data structure that supports the following data operations in the allotted time:<br /> :(a) ''Initialize''(&lt;math&gt;n&lt;/math&gt;): Initialize the data structure for empty rooms numbered &lt;math&gt;1, 2, . . . , n&lt;/math&gt;, in polynomial time.<br /> :(b) ''Count''(&lt;math&gt;l, h&lt;/math&gt;): Return the number of available rooms in &lt;math&gt;[l, h]&lt;/math&gt;, in &lt;math&gt;O(log n)&lt;/math&gt; time.<br /> :(c) ''Checkin''(&lt;math&gt;l, h&lt;/math&gt;): In &lt;math&gt;O(log n)&lt;/math&gt; time, return the first empty room in &lt;math&gt;[l, h]&lt;/math&gt; and mark it occupied, or return NIL if all the rooms in &lt;math&gt;[l, h]&lt;/math&gt; are occupied.<br /> :(d) ''Checkout''(&lt;math&gt;x&lt;/math&gt;): Mark room &lt;math&gt;x&lt;/math&gt; as unoccupied, in &lt;math&gt;O(log n)&lt;/math&gt; time.<br /> <br /> <br /> :[[3.31]]. Design a data structure that allows one to search, insert, and delete an integer &lt;math&gt;X&lt;/math&gt; in &lt;math&gt;O(1)&lt;/math&gt; time (i.e., constant time, independent of the total number of integers stored). Assume that &lt;math&gt;1 \leq X \leq n&lt;/math&gt; and that there are &lt;math&gt;m + n&lt;/math&gt; units of space available, where &lt;math&gt;m&lt;/math&gt; is the maximum number of integers that can be in the table at any one time. (Hint: use two arrays &lt;math&gt;A[1..n]&lt;/math&gt; and &lt;math&gt;B[1..m]&lt;/math&gt;.) You are not allowed to initialize either &lt;math&gt;A&lt;/math&gt; or &lt;math&gt;B&lt;/math&gt;, as that would take &lt;math&gt;O(m)&lt;/math&gt; or &lt;math&gt;O(n)&lt;/math&gt; operations. This means the arrays are full of random garbage to begin with, so you must be very careful.<br /> [[3.31|Solution]]<br /> <br /> ===Implementation Projects===<br /> <br /> :3.32. Implement versions of several different dictionary data structures, such as linked lists, binary trees, balanced binary search trees, and hash tables. Conduct experiments to assess the relative performance of these data structures in a simple application that reads a large text file and reports exactly one instance of each word that appears within it. This application can be efficiently implemented by maintaining a dictionary of all distinct words that have appeared thus far in the text and inserting/reporting each new word that appears in the stream. Write a brief report with your conclusions.<br /> <br /> <br /> :[[3.33]]. A Caesar shift (see Section 21.6 (page 697)) is a very simple class of ciphers for secret messages. Unfortunately, they can be broken using statistical properties of English. Develop a program capable of decrypting Caesar shifts of sufficiently long texts.<br /> [[3.33|Solution]]<br /> <br /> ===Interview Problems===<br /> <br /> :3.34. What method would you use to look up a word in a dictionary?<br /> <br /> <br /> :[[3.35]]. Imagine you have a closet full of shirts. What can you do to organize your shirts for easy retrieval?<br /> [[3.35|Solution]]<br /> <br /> <br /> :3.36. Write a function to find the middle node of a singly linked list.<br /> <br /> <br /> :[[3.37]]. Write a function to determine whether two binary trees are identical. Identical trees have the same key value at each position and the same structure.<br /> [[3.37|Solution]]<br /> <br /> <br /> :3.38. Write a program to convert a binary search tree into a linked list.<br /> <br /> <br /> :[[3.39]]. Implement an algorithm to reverse a linked list. Now do it without recursion.<br /> [[3.39|Solution]]<br /> <br /> <br /> :3.40. What is the best data structure for maintaining URLs that have been visited by a web crawler? Give an algorithm to test whether a given URL has already been visited, optimizing both space and time.<br /> <br /> <br /> :[[3.41]]. You are given a search string and a magazine. You seek to generate all the characters in the search string by cutting them out from the magazine. Give an algorithm to efficiently determine whether the magazine contains all the letters in the search string.<br /> [[3.41|Solution]]<br /> <br /> <br /> :3.42. Reverse the words in a sentence—that is, “My name is Chris” becomes “Chris is name My.” Optimize for time and space.<br /> <br /> <br /> :[[3.43]]. Determine whether a linked list contains a loop as quickly as possible without using any extra storage. Also, identify the location of the loop.<br /> [[3.43|Solution]]<br /> <br /> <br /> :3.44. You have an unordered array &lt;math&gt;X&lt;/math&gt; of &lt;math&gt;n&lt;/math&gt; integers. Find the array &lt;math&gt;M&lt;/math&gt; containing &lt;math&gt;n&lt;/math&gt; elements where &lt;math&gt;M_i&lt;/math&gt; is the product of all integers in &lt;math&gt;X&lt;/math&gt; except for &lt;math&gt;X_i&lt;/math&gt;. You may not use division. You can use extra memory. (Hint: there are solutions faster than &lt;math&gt;O(n^2)&lt;/math&gt;.)<br /> <br /> <br /> :[[3.45]]. Give an algorithm for finding an ordered word pair (e.g. “New York”) occurring with the greatest frequency in a given webpage. Which data structures would you use? Optimize both time and space.<br /> [[3.45|Solution]]<br /> <br /> <br /> Back to [[Chapter List]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Chapter_List&diff=447 Chapter List 2020-10-01T18:06:08Z <p>Algowikiadmin: Protected &quot;Chapter List&quot; ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))</p> <hr /> <div>Chapters<br /> <br /> #[[Chapter 1|Introduction to Algorithms]]<br /> #[[Chapter 2|Algorithm Analysis]]<br /> #[[Chapter 3|Data Structures]]<br /> #[[Chapter 4|Sorting]]<br /> #[[Chapter 5|Divide and Conquer]]<br /> #[[Chapter 6|Hashing and Randomized Algorithms]]<br /> #[[Chapter 7|Graph Traversal]]<br /> #[[Chapter 8|Weighted Graph Algorithms]]<br /> #[[Chapter 9|Combinatorial Search]]<br /> #[[Chapter 10|Dynamic Programming]]<br /> #[[Chapter 11|NP-Completeness]]<br /> #[[Chapter 12|Dealing with Hard Problems]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Chapter_1&diff=446 Chapter 1 2020-10-01T18:05:51Z <p>Algowikiadmin: Protected &quot;Chapter 1&quot; ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))</p> <hr /> <div>Below are the exercises at the end of chapter 1 of the third edition of the Algorithm Design Manual by Steven Skiena. Student proposed answers to odd number questions are available by clicking on the question number.<br /> <br /> =Introduction to Algorithms=<br /> <br /> ===Finding Counter Examples===<br /> <br /> :[[1.1]]. Show that &lt;math&gt;a + b&lt;/math&gt; can be less than &lt;math&gt;\min(a,b)&lt;/math&gt;.<br /> [[1.1|Solution]]<br /> <br /> <br /> :1.2. Show that &lt;math&gt;a \times b&lt;/math&gt; can be less than &lt;math&gt;\min(a,b)&lt;/math&gt;.<br /> <br /> <br /> :[[1.3]]. Design/draw a road network with two points &lt;math&gt;a&lt;/math&gt; and &lt;math&gt;b&lt;/math&gt; such that the fastest route between &lt;math&gt;a&lt;/math&gt; and &lt;math&gt;b&lt;/math&gt; is not the shortest route.<br /> [[1.3|Solution]]<br /> <br /> <br /> :1.4. Design/draw a road network with two points &lt;math&gt;a&lt;/math&gt; and &lt;math&gt;b&lt;/math&gt; such that the shortest route between &lt;math&gt;a&lt;/math&gt; and &lt;math&gt;b&lt;/math&gt; is not the route with the fewest turns.<br /> <br /> <br /> :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers &lt;math&gt;S = \{s_1, s_2, \ldots, s_n\}&lt;/math&gt;, and a target number &lt;math&gt;T&lt;/math&gt;, find a subset of &lt;math&gt;S&lt;/math&gt; which adds up exactly to &lt;math&gt;T&lt;/math&gt;. For example, there exists a subset within &lt;math&gt;S = \{1, 2, 5, 9, 10\}&lt;/math&gt; that adds up to &lt;math&gt;T=22&lt;/math&gt; but not &lt;math&gt;T=23&lt;/math&gt;.<br /> :Find counterexamples to each of the following algorithms for the knapsack problem. That is, giving an &lt;math&gt;S&lt;/math&gt; and &lt;math&gt;T&lt;/math&gt; such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists.<br /> #Put the elements of &lt;math&gt;S&lt;/math&gt; in the knapsack in left to right order if they fit, i.e. the first-fit algorithm.<br /> #Put the elements of &lt;math&gt;S&lt;/math&gt; in the knapsack from smallest to largest, i.e. the best-fit algorithm.<br /> #Put the elements of &lt;math&gt;S&lt;/math&gt; in the knapsack from largest to smallest.<br /> [[1.5|Solution]]<br /> <br /> <br /> :1.6. The ''set cover problem'' is as follows: given a set &lt;math&gt;S&lt;/math&gt; of subsets &lt;math&gt; S_1, ..., S_m&lt;/math&gt; of the universal set &lt;math&gt;U = \{1,...,n\}&lt;/math&gt;, find the smallest subset of subsets &lt;math&gt;T \subset S&lt;/math&gt; such that &lt;math&gt;\cup_{t_i \in T} t_i = U&lt;/math&gt;.For example, there are the following subsets, &lt;math&gt;S_1 = \{1, 3, 5\}&lt;/math&gt;, &lt;math&gt;S_2 = \{2,4\}&lt;/math&gt;, &lt;math&gt;S_3 = \{1,4\}&lt;/math&gt;, and &lt;math&gt;S_4 = \{2,5\}&lt;/math&gt; The set cover would then be &lt;math&gt;S_1&lt;/math&gt; and &lt;math&gt;S_2&lt;/math&gt;.<br /> <br /> :Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered.<br /> <br /> <br /> :[[1.7]]. The ''maximum clique problem'' in a graph &lt;math&gt;G = (V, E)&lt;/math&gt; asks for the largest subset &lt;math&gt;C&lt;/math&gt; of vertices &lt;math&gt;V&lt;/math&gt; such that there is an edge in &lt;math&gt;E&lt;/math&gt; between every pair of vertices in &lt;math&gt;C&lt;/math&gt;. Find a counterexample for the following algorithm: Sort the vertices of &lt;math&gt;G&lt;/math&gt; from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered.<br /> [[1.7|Solution]]<br /> <br /> <br /> ===Proofs of Correctness===<br /> <br /> :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants &lt;math&gt; c \geq 2&lt;/math&gt;.<br /> multiply(&lt;math&gt;y,z&lt;/math&gt;)<br /> #Return the product &lt;math&gt;yz&lt;/math&gt;.<br /> ''if'' &lt;math&gt;z=0&lt;/math&gt; ''then'' return(0) ''else''<br /> return(multiply(&lt;math&gt;cy,\lfloor z/c \rfloor)+y \cdot (z\,\bmod\,c&lt;/math&gt;))<br /> <br /> <br /> <br /> :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial. P(x) = &lt;math&gt;a_nx^n + a_{n-1}x^{n-1} + \dots + a_1x + a_0&lt;/math&gt;<br /> horner(&lt;math&gt;A,x&lt;/math&gt;)<br /> &lt;math&gt;p = A_n&lt;/math&gt;<br /> for &lt;math&gt;i&lt;/math&gt; from &lt;math&gt;n-1&lt;/math&gt; to &lt;math&gt;0&lt;/math&gt;<br /> &lt;math&gt;p = p*x+A_i&lt;/math&gt;<br /> return &lt;math&gt;p&lt;/math&gt;<br /> [[1.9|Solution]]<br /> <br /> <br /> :1.10. Prove the correctness of the following sorting algorithm.<br /> bubblesort (&lt;math&gt;A&lt;/math&gt; : list[&lt;math&gt;1 \dots n&lt;/math&gt;])<br /> for &lt;math&gt;i&lt;/math&gt; from &lt;math&gt;n&lt;/math&gt; to &lt;math&gt;1&lt;/math&gt;<br /> for &lt;math&gt;j&lt;/math&gt; from &lt;math&gt;1&lt;/math&gt; to &lt;math&gt;i-1&lt;/math&gt;<br /> if (&lt;math&gt;A[j] &gt; A[j+1]&lt;/math&gt;)<br /> swap the values of &lt;math&gt;A[j]&lt;/math&gt; and &lt;math&gt;A[j+1]&lt;/math&gt;<br /> <br /> <br /> :[[1.11]]. The ''greatest common divisor of positive'' integers &lt;math&gt;x&lt;/math&gt; and &lt;math&gt;y&lt;/math&gt; is the largest integer &lt;math&gt;d&lt;/math&gt; such that &lt;math&gt;d&lt;/math&gt; divides &lt;math&gt;x&lt;/math&gt; and &lt;math&gt;d&lt;/math&gt; divides &lt;math&gt;y&lt;/math&gt;. Euclid’s algorithm to compute &lt;math&gt;gcd(x, y)&lt;/math&gt; where &lt;math&gt;x &gt; y&lt;/math&gt; reduces the task to a smaller problem:<br /> <br /> :::::&lt;math&gt;gcd(x, y) = gcd(y, x mod y)&lt;/math&gt;<br /> <br /> :Prove that Euclid’s algorithm is correct.<br /> [[1.11|Solution]]<br /> <br /> <br /> ===Induction===<br /> <br /> :1.12. Prove that &lt;math&gt;\sum_{i=1}^n i&lt;/math&gt;=&lt;math&gt;n(n+1)/2&lt;/math&gt; for &lt;math&gt;n \geq 0&lt;/math&gt;, by induction.<br /> <br /> <br /> :[[1.13]]. Prove that &lt;math&gt;\sum_{i=1}^n i^2&lt;/math&gt;=&lt;math&gt;n(n+1)(2n+1)/6&lt;/math&gt; for &lt;math&gt;n \geq\ 0&lt;/math&gt;, by induction.<br /> [[1.13|Solution]]<br /> <br /> <br /> :1.14. Prove that &lt;math&gt;\sum_{i=1}^n i^3&lt;/math&gt;=&lt;math&gt;n^2(n+1)^2/4&lt;/math&gt; for &lt;math&gt;n \geq 0&lt;/math&gt;, by induction.<br /> <br /> <br /> :[[1.15]]. Prove that &lt;math&gt; \sum_{i=1}^n i(i+1)(i+2)=n(n+1)(n+2)(n+3)/4 &lt;/math&gt;<br /> [[1.15|Solution]]<br /> <br /> <br /> :1.16. Prove by induction on &lt;math&gt;n \geq 1&lt;/math&gt; that for every &lt;math&gt;a \neq 1&lt;/math&gt;, &lt;math&gt; \sum_{i=0}^n a^i =\frac{a^{n+1}-1}{a-1}&lt;/math&gt;<br /> <br /> <br /> :[[1.17]]. Prove by induction that for &lt;math&gt;n \geq 1&lt;/math&gt;, &lt;math&gt; \sum_{i=1}^n \frac{1}{i(i+1)} = \frac{n}{n+1} &lt;/math&gt;<br /> [[1.17|Solution]]<br /> <br /> <br /> :1.18. Prove by induction that &lt;math&gt;n^3+2n&lt;/math&gt; is divisible by &lt;math&gt;3&lt;/math&gt; for all &lt;math&gt;n \geq 0&lt;/math&gt;.<br /> <br /> <br /> :[[1.19]]. Prove by induction that a tree with &lt;math&gt;n&lt;/math&gt; vertices has exactly &lt;math&gt;n-1&lt;/math&gt; edges.<br /> [[1.19|Solution]]<br /> <br /> <br /> :1.20. Prove by mathematical induction that the sum of the cubes of the first &lt;math&gt;n&lt;/math&gt; positive integers is equal to the square of the sum of these integers, i.e.<br /> <br /> ::::::::::::&lt;math&gt; \sum_{i=1}^n i^3 = ( \sum_{i=1}^n i )^2 &lt;/math&gt;<br /> <br /> <br /> ===Estimation===<br /> <br /> :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library?<br /> [[1.21|Solution]]<br /> <br /> <br /> :1.22. How many words are there in this textbook?<br /> <br /> <br /> :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head.<br /> [[1.23|Solution]]<br /> <br /> <br /> :1.24. Estimate how many cities and towns there are in the United States.<br /> <br /> <br /> :[[1.25]]. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer.<br /> [[1.25|Solution]]<br /> <br /> <br /> :1.26. How many Starbucks or McDonald’s locations are there in your country?<br /> <br /> <br /> :[[1.27]]. How long would it take to empty a bathtub with a drinking straw?<br /> [[1.27|Solution]]<br /> <br /> <br /> :1.28. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time?<br /> <br /> <br /> :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . .<br /> ::(a) if you believe that the algorithm takes time proportional to n2, and<br /> ::(b) if you believe that the algorithm takes time roughly proportional to n log n?<br /> [[1.29|Solution]]<br /> <br /> <br /> ===Implementation Projects===<br /> <br /> :1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them?<br /> <br /> <br /> :[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets.<br /> [[1.31|Solution]]<br /> <br /> <br /> ===Interview Problems===<br /> <br /> :1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it.<br /> <br /> <br /> :[[1.33]]. There are twenty-five horses. At most, five horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done.<br /> [[1.33|Solution]]<br /> <br /> <br /> :1.34. How many piano tuners are there in the entire world?<br /> <br /> <br /> :[[1.35]]. How many gas stations are there in the United States?<br /> [[1.35|Solution]]<br /> <br /> <br /> :1.36. How much does the ice in a hockey rink weigh?<br /> <br /> <br /> :[[1.37]]. How many miles of road are there in the United States?<br /> [[1.37|Solution]]<br /> <br /> <br /> :1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name?<br /> <br /> <br /> <br /> Back to [[Chapter List]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Main_Page&diff=445 Main Page 2020-10-01T18:05:31Z <p>Algowikiadmin: Protected &quot;Main Page&quot; ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))</p> <hr /> <div>The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others.<br /> <br /> Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution.<br /> <br /> &lt;big&gt;Chapters&lt;/big&gt;<br /> <br /> #[[Chapter 1|Introduction to Algorithms]]<br /> #[[Chapter 2|Algorithm Analysis]]<br /> #[[Chapter 3|Data Structures]]<br /> #[[Chapter 4|Sorting]]<br /> #[[Chapter 5|Divide and Conquer]]<br /> #[[Chapter 6|Hashing and Randomized Algorithms]]<br /> #[[Chapter 7|Graph Traversal]]<br /> #[[Chapter 8|Weighted Graph Algorithms]]<br /> #[[Chapter 9|Combinatorial Search]]<br /> #[[Chapter 10|Dynamic Programming]]<br /> #[[Chapter 11|NP-Completeness]]<br /> #[[Chapter 12|Dealing with Hard Problems]]<br /> <br /> == Getting started ==<br /> ; Editing<br /> * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula]<br /> * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples]<br /> <br /> ; Configuration<br /> * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list]<br /> <br /> ; Mediawiki General<br /> * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ]<br /> * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]<br /> <br /> ; Questions<br /> *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems)<br /> * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic)</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Main_Page&diff=444 Main Page 2020-10-01T18:01:44Z <p>Algowikiadmin: </p> <hr /> <div>The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others.<br /> <br /> Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution.<br /> <br /> &lt;big&gt;Chapters&lt;/big&gt;<br /> <br /> #[[Chapter 1|Introduction to Algorithms]]<br /> #[[Chapter 2|Algorithm Analysis]]<br /> #[[Chapter 3|Data Structures]]<br /> #[[Chapter 4|Sorting]]<br /> #[[Chapter 5|Divide and Conquer]]<br /> #[[Chapter 6|Hashing and Randomized Algorithms]]<br /> #[[Chapter 7|Graph Traversal]]<br /> #[[Chapter 8|Weighted Graph Algorithms]]<br /> #[[Chapter 9|Combinatorial Search]]<br /> #[[Chapter 10|Dynamic Programming]]<br /> #[[Chapter 11|NP-Completeness]]<br /> #[[Chapter 12|Dealing with Hard Problems]]<br /> <br /> == Getting started ==<br /> ; Editing<br /> * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula]<br /> * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples]<br /> <br /> ; Configuration<br /> * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list]<br /> <br /> ; Mediawiki General<br /> * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ]<br /> * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]<br /> <br /> ; Questions<br /> *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems)<br /> * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic)</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Main_Page&diff=443 Main Page 2020-10-01T18:01:33Z <p>Algowikiadmin: /* Chapters */</p> <hr /> <div>The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others.<br /> <br /> Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution.<br /> <br /> <br /> &lt;big&gt;Chapters&lt;/big&gt;<br /> <br /> #[[Chapter 1|Introduction to Algorithms]]<br /> #[[Chapter 2|Algorithm Analysis]]<br /> #[[Chapter 3|Data Structures]]<br /> #[[Chapter 4|Sorting]]<br /> #[[Chapter 5|Divide and Conquer]]<br /> #[[Chapter 6|Hashing and Randomized Algorithms]]<br /> #[[Chapter 7|Graph Traversal]]<br /> #[[Chapter 8|Weighted Graph Algorithms]]<br /> #[[Chapter 9|Combinatorial Search]]<br /> #[[Chapter 10|Dynamic Programming]]<br /> #[[Chapter 11|NP-Completeness]]<br /> #[[Chapter 12|Dealing with Hard Problems]]<br /> <br /> == Getting started ==<br /> ; Editing<br /> * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula]<br /> * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples]<br /> <br /> ; Configuration<br /> * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list]<br /> <br /> ; Mediawiki General<br /> * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ]<br /> * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]<br /> <br /> ; Questions<br /> *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems)<br /> * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic)</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Main_Page&diff=442 Main Page 2020-10-01T18:00:51Z <p>Algowikiadmin: </p> <hr /> <div>The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others.<br /> <br /> Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution.<br /> <br /> <br /> ==Chapters==<br /> <br /> #[[Chapter 1|Introduction to Algorithms]]<br /> #[[Chapter 2|Algorithm Analysis]]<br /> #[[Chapter 3|Data Structures]]<br /> #[[Chapter 4|Sorting]]<br /> #[[Chapter 5|Divide and Conquer]]<br /> #[[Chapter 6|Hashing and Randomized Algorithms]]<br /> #[[Chapter 7|Graph Traversal]]<br /> #[[Chapter 8|Weighted Graph Algorithms]]<br /> #[[Chapter 9|Combinatorial Search]]<br /> #[[Chapter 10|Dynamic Programming]]<br /> #[[Chapter 11|NP-Completeness]]<br /> #[[Chapter 12|Dealing with Hard Problems]]<br /> <br /> <br /> == Getting started ==<br /> ; Editing<br /> * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula]<br /> * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples]<br /> <br /> ; Configuration<br /> * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list]<br /> <br /> ; Mediawiki General<br /> * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ]<br /> * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]<br /> <br /> ; Questions<br /> *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems)<br /> * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic)</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Main_Page&diff=441 Main Page 2020-09-28T17:55:00Z <p>Algowikiadmin: </p> <hr /> <div>The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others.<br /> <br /> Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution.<br /> <br /> <br /> The chapters and their corresponding exercises can be found [[Chapter List|here]].<br /> <br /> == Getting started ==<br /> ; Editing<br /> * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula]<br /> * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples]<br /> <br /> ; Configuration<br /> * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list]<br /> <br /> ; Mediawiki General<br /> * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ]<br /> * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]<br /> <br /> ; Questions<br /> *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems)<br /> * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic)</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Main_Page&diff=440 Main Page 2020-09-28T17:54:50Z <p>Algowikiadmin: </p> <hr /> <div>The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others.<br /> <br /> Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution.<br /> <br /> The chapters and their corresponding exercises can be found [[Chapter List|here]].<br /> <br /> == Getting started ==<br /> ; Editing<br /> * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula]<br /> * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples]<br /> <br /> ; Configuration<br /> * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list]<br /> <br /> ; Mediawiki General<br /> * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ]<br /> * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]<br /> <br /> ; Questions<br /> *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems)<br /> * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic)</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Chapter_List&diff=439 Chapter List 2020-09-28T17:39:25Z <p>Algowikiadmin: </p> <hr /> <div>Chapters<br /> <br /> #[[Chapter 1|Introduction to Algorithms]]<br /> #[[Chapter 2|Algorithm Analysis]]<br /> #[[Chapter 3|Data Structures]]<br /> #[[Chapter 4|Sorting]]<br /> #[[Chapter 5|Divide and Conquer]]<br /> #[[Chapter 6|Hashing and Randomized Algorithms]]<br /> #[[Chapter 7|Graph Traversal]]<br /> #[[Chapter 8|Weighted Graph Algorithms]]<br /> #[[Chapter 9|Combinatorial Search]]<br /> #[[Chapter 10|Dynamic Programming]]<br /> #[[Chapter 11|NP-Completeness]]<br /> #[[Chapter 12|Dealing with Hard Problems]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Chapter_3&diff=438 Chapter 3 2020-09-28T17:37:22Z <p>Algowikiadmin: /* Data Structure */</p> <hr /> <div>=Data Structures=<br /> <br /> ===Stacks, Queues, and Lists===<br /> <br /> :[[3.1]]. A common problem for compilers and text editors is determining whether the parentheses in a string are balanced and properly nested. For example, the string &lt;math&gt;((())())()&lt;/math&gt; contains properly nested pairs of parentheses, which the strings &lt;math&gt;)()(&lt;/math&gt; and &lt;math&gt;())&lt;/math&gt; do not. Give an algorithm that returns true if a string contains properly nested and balanced parentheses, and false if otherwise. For full credit, identify the position of the first offending parenthesis if the string is not properly nested and balanced.<br /> [[3.1|Solution]]<br /> <br /> <br /> :3.2. Give an algorithm that takes a string &lt;math&gt;S&lt;/math&gt; consisting of opening and closing parentheses, say )()(())()()))())))(, and finds the length of the longest balanced parentheses in &lt;math&gt;S&lt;/math&gt;, which is 12 in the example above. (Hint: The solution is not necessarily a contiguous run of parenthesis from &lt;math&gt;S&lt;/math&gt;.)<br /> <br /> <br /> :[[3.3]]. Give an algorithm to reverse the direction of a given singly linked list. In other words, after the reversal all pointers should now point backwards. Your algorithm should take linear time.<br /> [[3.3|Solution]]<br /> <br /> <br /> :3.4. Design a stack &lt;math&gt;S&lt;/math&gt; that supports ''S.push(x)'', ''S.pop()'', and ''S.findmin()'', which returns the minimum element of &lt;math&gt;S&lt;/math&gt;. All operations should run in constant time.<br /> <br /> <br /> :[[3.5]]. We have seen how dynamic arrays enable arrays to grow while still achieving constant-time amortized performance. This problem concerns extending dynamic arrays to let them both grow and shrink on demand.<br /> :(a) Consider an underflow strategy that cuts the array size in half whenever the array falls below half full. Give an example sequence of insertions and deletions where this strategy gives a bad amortized cost.<br /> :(b) Then, give a better underflow strategy than that suggested above, one that achieves constant amortized cost per deletion.<br /> [[3.5|Solution]]<br /> <br /> <br /> :3.6. Suppose you seek to maintain the contents of a refrigerator so as to minimize food spoilage. What data structure should you use, and how should you use it?<br /> <br /> <br /> :[[3.7]]. Work out the details of supporting constant-time deletion from a singly linked list as per the footnote from page 79, ideally to an actual implementation. Support the other operations as efficiently as possible. <br /> [[3.7|Solution]]<br /> <br /> ===Elementary Data Structures===<br /> <br /> :3.8. Tic-tac-toe is a game played on an &lt;math&gt;n * n&lt;/math&gt; board (typically &lt;math&gt;n = 3&lt;/math&gt;) where two players take consecutive turns placing “O” and “X” marks onto the board cells. The game is won if n consecutive “O” or ‘X” marks are placed in a row, column, or diagonal. Create a data structure with &lt;math&gt;O(n)&lt;/math&gt; space that accepts a sequence of moves, and reports in constant time whether the last move won the game.<br /> <br /> <br /> :[[3.9]]. Write a function which, given a sequence of digits 2–9 and a dictionary of &lt;math&gt;n&lt;/math&gt; words, reports all words described by this sequence when typed in on a standard telephone keypad. For the sequence 269 you should return any, box, boy, and cow, among other words.<br /> [[3.9|Solution]]<br /> <br /> <br /> :3.10. Two strings &lt;math&gt;X&lt;/math&gt; and &lt;math&gt;Y&lt;/math&gt; are anagrams if the letters of &lt;math&gt;X&lt;/math&gt; can be rearranged to form &lt;math&gt;Y&lt;/math&gt; . For example, silent/listen, and incest/insect are anagrams. Give an efficient algorithm to determine whether strings &lt;math&gt;X&lt;/math&gt; and &lt;math&gt;Y&lt;/math&gt; are anagrams.<br /> <br /> ===Trees and Other Dictionary Structures===<br /> <br /> :[[3.11]]. Design a dictionary data structure in which search, insertion, and deletion can all be processed in &lt;math&gt;O(1)&lt;/math&gt; time in the worst case. You may assume the set elements are integers drawn from a finite set &lt;math&gt;1, 2, .., n&lt;/math&gt;, and initialization can take &lt;math&gt;O(n)&lt;/math&gt; time.<br /> [[3.11|Solution]]<br /> <br /> <br /> :3.12. The maximum depth of a binary tree is the number of nodes on the path from the root down to the most distant leaf node. Give an &lt;math&gt;O(n)&lt;/math&gt; algorithm to find the maximum depth of a binary tree with &lt;math&gt;n&lt;/math&gt; nodes.<br /> <br /> <br /> :[[3.13]]. Two elements of a binary search tree have been swapped by mistake. Give an &lt;math&gt;O(n)&lt;/math&gt; algorithm to identify these two elements so they can be swapped back.<br /> [[3.13|Solution]]<br /> <br /> <br /> :3.14. Given two binary search trees, merge them into a doubly linked list in sorted order.<br /> <br /> <br /> :[[3.15]]. Describe an &lt;math&gt;O(n)&lt;/math&gt;-time algorithm that takes an &lt;math&gt;n&lt;/math&gt;-node binary search tree and constructs an equivalent height-balanced binary search tree. In a height-balanced binary search tree, the difference between the height of the left and right subtrees of every node is never more than 1.<br /> [[3.15|Solution]]<br /> <br /> <br /> :3.16. Find the storage efficiency ratio (the ratio of data space over total space) for each of the following binary tree implementations on n nodes:<br /> :(a) All nodes store data, two child pointers, and a parent pointer. The data field requires 4 bytes and each pointer requires 4 bytes.<br /> :(b) Only leaf nodes store data; internal nodes store two child pointers. The data field requires four bytes and each pointer requires two bytes.<br /> <br /> <br /> :[[3.17]]. Give an &lt;math&gt;O(n)&lt;/math&gt; algorithm that determines whether a given &lt;math&gt;n&lt;/math&gt;-node binary tree is height-balanced (see Problem 3-15).<br /> [[3.17|Solution]]<br /> <br /> <br /> :3.18. Describe how to modify any balanced tree data structure such that search, insert, delete, minimum, and maximum still take &lt;math&gt;O(log n)&lt;/math&gt; time each, but successor and predecessor now take &lt;math&gt;O(1)&lt;/math&gt; time each. Which operations have to be modified to support this?<br /> <br /> <br /> :[[3.19]]. Suppose you have access to a balanced dictionary data structure that supports each of the operations search, insert, delete, minimum, maximum, successor, and predecessor in &lt;math&gt;O(log n)&lt;/math&gt; time. Explain how to modify the insert and delete operations so they still take &lt;math&gt;O(log n)&lt;/math&gt; but now minimum and maximum take &lt;math&gt;O(1)&lt;/math&gt; time. (Hint: think in terms of using the abstract dictionary operations, instead of mucking about with pointers and the like.)<br /> [[3.19|Solution]]<br /> <br /> <br /> :3.20. Design a data structure to support the following operations:<br /> :• ''insert''(&lt;math&gt;x,T&lt;/math&gt;) – Insert item &lt;math&gt;x&lt;/math&gt; into the set &lt;math&gt;T&lt;/math&gt;.<br /> :• ''delete''(&lt;math&gt;k,T&lt;/math&gt;) – Delete the &lt;math&gt;k&lt;/math&gt;th smallest element from &lt;math&gt;T&lt;/math&gt;.<br /> :• ''member''(&lt;math&gt;x,T&lt;/math&gt;) – Return true iff &lt;math&gt;x \in T&lt;/math&gt;.<br /> :All operations must take &lt;math&gt;O(log n)&lt;/math&gt; time on an &lt;math&gt;n&lt;/math&gt;-element set.<br /> <br /> <br /> :[[3.21]]. A ''concatenate operation'' takes two sets &lt;math&gt;S_1&lt;/math&gt; and &lt;math&gt;S_2&lt;/math&gt;, where every key in &lt;math&gt;S_1&lt;/math&gt; is smaller than any key in &lt;math&gt;S_2&lt;/math&gt;, and merges them. Give an algorithm to concatenate two binary search trees into one binary search tree. The worst-case running time should be &lt;math&gt;O(h)&lt;/math&gt;, where &lt;math&gt;h&lt;/math&gt; is the maximal height of the two trees.<br /> [[3.21|Solution]]<br /> <br /> ===Applications of Tree Structures===<br /> <br /> :3.22. Design a data structure that supports the following two operations:<br /> :• ''insert''(&lt;math&gt;x&lt;/math&gt;) – Insert item &lt;math&gt;x&lt;/math&gt; from the data stream to the data structure.<br /> :• ''median''() – Return the median of all elements so far.<br /> :All operations must take &lt;math&gt;O(log n)&lt;/math&gt; time on an &lt;math&gt;n&lt;/math&gt;-element set.<br /> <br /> <br /> :[[3.23]]. Assume we are given a standard dictionary (balanced binary search tree) defined on a set of &lt;math&gt;n&lt;/math&gt; strings, each of length at most &lt;math&gt;l&lt;/math&gt;. We seek to print out all strings beginning with a particular prefix &lt;math&gt;p&lt;/math&gt;. Show how to do this in &lt;math&gt;O(ml log n)&lt;/math&gt; time, where &lt;math&gt;m&lt;/math&gt; is the number of strings.<br /> [[3.23|Solution]]<br /> <br /> <br /> :3.24. An array &lt;math&gt;A&lt;/math&gt; is called &lt;math&gt;k&lt;/math&gt;-unique if it does not contain a pair of duplicate elements within &lt;math&gt;k&lt;/math&gt; positions of each other, that is, there is no &lt;math&gt;i&lt;/math&gt; and &lt;math&gt;j&lt;/math&gt; such that &lt;math&gt;A[i] = A[j]&lt;/math&gt; and &lt;math&gt;|j - i| \leq k&lt;/math&gt;. Design a worst-case &lt;math&gt;O(n log k)&lt;/math&gt; algorithm to test if &lt;math&gt;A&lt;/math&gt; is &lt;math&gt;k&lt;/math&gt;-unique.<br /> <br /> <br /> :[[3.25]]. In the ''bin-packing problem'', we are given &lt;math&gt;n&lt;/math&gt; objects, each weighing at most 1 kilogram. Our goal is to find the smallest number of bins that will hold the &lt;math&gt;n&lt;/math&gt; objects, with each bin holding 1 kilogram at most.<br /> :• The ''best-fit heuristic'' for bin packing is as follows. Consider the objects in the order in which they are given. For each object, place it into the partially filled bin with the smallest amount of extra room after the object is inserted. If no such bin exists, start a new bin. Design an algorithm that implements the best-fit heuristic (taking as input the &lt;math&gt;n&lt;/math&gt; weights &lt;math&gt;w_1, w_2, ..., w_n&lt;/math&gt; and outputting the number of bins used) in &lt;math&gt;O(n log n)&lt;/math&gt; time.<br /> :• Repeat the above using the ''worst-fit heuristic'', where we put the next object into the partially filled bin with the largest amount of extra room after the object is inserted.<br /> [[3.25|Solution]]<br /> <br /> <br /> :3.26. Suppose that we are given a sequence of &lt;math&gt;n&lt;/math&gt; values &lt;math&gt;x_1, x_2, ..., x_n&lt;/math&gt; and seek to quickly answer repeated queries of the form: given &lt;math&gt;i&lt;/math&gt; and &lt;math&gt;j&lt;/math&gt;, find the smallest value in &lt;math&gt;x_i, . . . , x_j&lt;/math&gt;.<br /> :(a) Design a data structure that uses &lt;math&gt;O(n^2)&lt;/math&gt; space and answers queries in &lt;math&gt;O(1)&lt;/math&gt; time.<br /> :(b) Design a data structure that uses &lt;math&gt;O(n)&lt;/math&gt; space and answers queries in &lt;math&gt;O(log n)&lt;/math&gt; time. For partial credit, your data structure can use &lt;math&gt;O(n log n)&lt;/math&gt; space and have &lt;math&gt;O(log n)&lt;/math&gt; query time.<br /> <br /> <br /> :[[3.27]]. Suppose you are given an input set &lt;math&gt;S&lt;/math&gt; of &lt;math&gt;n&lt;/math&gt; integers, and a black box that if given any sequence of integers and an integer &lt;math&gt;k&lt;/math&gt; instantly and correctly answers whether there is a subset of the input sequence whose sum is exactly &lt;math&gt;k&lt;/math&gt;. Show how to use the black box &lt;math&gt;O(n)&lt;/math&gt; times to find a subset of &lt;math&gt;S&lt;/math&gt; that adds up to &lt;math&gt;k&lt;/math&gt;.<br /> [[3.27|Solution]]<br /> <br /> <br /> :3.28. Let &lt;math&gt;A[1..n]&lt;/math&gt; be an array of real numbers. Design an algorithm to perform any sequence of the following operations:<br /> :• ''Add''(&lt;math&gt;i,y&lt;/math&gt;) – Add the value &lt;math&gt;y&lt;/math&gt; to the &lt;math&gt;i&lt;/math&gt;th number.<br /> :• ''Partial-sum''(&lt;math&gt;i&lt;/math&gt;) – Return the sum of the first &lt;math&gt;i&lt;/math&gt; numbers, that is, &lt;math&gt;\sum_{j=1}^i A[j]&lt;/math&gt;.<br /> :There are no insertions or deletions; the only change is to the values of the numbers. Each operation should take &lt;math&gt;O(log n)&lt;/math&gt; steps. You may use one additional array of size &lt;math&gt;n&lt;/math&gt; as a work space.<br /> <br /> <br /> :[[3.29]]. Extend the data structure of the previous problem to support insertions and deletions. Each element now has both a ''key'' and a ''value''. An element is accessed by its key, but the addition operation is applied to the values. The ''Partial_sum'' operation is different.<br /> :• ''Add''(&lt;math&gt;k,y&lt;/math&gt;) – Add the value &lt;math&gt;y&lt;/math&gt; to the item with key &lt;math&gt;k&lt;/math&gt;.<br /> :• ''Insert''(&lt;math&gt;k,y&lt;/math&gt;) – Insert a new item with key &lt;math&gt;k&lt;/math&gt; and value &lt;math&gt;y&lt;/math&gt;.<br /> :• ''Delete''(&lt;math&gt;k&lt;/math&gt;) – Delete the item with key &lt;math&gt;k&lt;/math&gt;.<br /> :• ''Partial-sum''(&lt;math&gt;k&lt;/math&gt;) – Return the sum of all the elements currently in the set whose key is less than &lt;math&gt;k&lt;/math&gt;, that is, &lt;math&gt;\sum_{i &lt; k} x_i&lt;/math&gt;. <br /> :The worst-case running time should still be &lt;math&gt;O(n log n)&lt;/math&gt; for any sequence of &lt;math&gt;O(n)&lt;/math&gt; operations.<br /> [[3.29|Solution]]<br /> <br /> <br /> :3.30. You are consulting for a hotel that has &lt;math&gt;n&lt;/math&gt; one-bed rooms. When a guest checks in, they ask for a room whose number is in the range &lt;math&gt;[l, h]&lt;/math&gt;. Propose a data structure that supports the following data operations in the allotted time:<br /> :(a) ''Initialize''(&lt;math&gt;n&lt;/math&gt;): Initialize the data structure for empty rooms numbered &lt;math&gt;1, 2, . . . , n&lt;/math&gt;, in polynomial time.<br /> :(b) ''Count''(&lt;math&gt;l, h&lt;/math&gt;): Return the number of available rooms in &lt;math&gt;[l, h]&lt;/math&gt;, in &lt;math&gt;O(log n)&lt;/math&gt; time.<br /> :(c) ''Checkin''(&lt;math&gt;l, h&lt;/math&gt;): In &lt;math&gt;O(log n)&lt;/math&gt; time, return the first empty room in &lt;math&gt;[l, h]&lt;/math&gt; and mark it occupied, or return NIL if all the rooms in &lt;math&gt;[l, h]&lt;/math&gt; are occupied.<br /> :(d) ''Checkout''(&lt;math&gt;x&lt;/math&gt;): Mark room &lt;math&gt;x&lt;/math&gt; as unoccupied, in &lt;math&gt;O(log n)&lt;/math&gt; time.<br /> <br /> <br /> :[[3.31]]. Design a data structure that allows one to search, insert, and delete an integer &lt;math&gt;X&lt;/math&gt; in &lt;math&gt;O(1)&lt;/math&gt; time (i.e., constant time, independent of the total number of integers stored). Assume that &lt;math&gt;1 \leq X \leq n&lt;/math&gt; and that there are &lt;math&gt;m + n&lt;/math&gt; units of space available, where &lt;math&gt;m&lt;/math&gt; is the maximum number of integers that can be in the table at any one time. (Hint: use two arrays &lt;math&gt;A[1..n]&lt;/math&gt; and &lt;math&gt;B[1..m]&lt;/math&gt;.) You are not allowed to initialize either &lt;math&gt;A&lt;/math&gt; or &lt;math&gt;B&lt;/math&gt;, as that would take &lt;math&gt;O(m)&lt;/math&gt; or &lt;math&gt;O(n)&lt;/math&gt; operations. This means the arrays are full of random garbage to begin with, so you must be very careful.<br /> [[3.31|Solution]]<br /> <br /> ===Implementation Projects===<br /> <br /> :3.32. Implement versions of several different dictionary data structures, such as linked lists, binary trees, balanced binary search trees, and hash tables. Conduct experiments to assess the relative performance of these data structures in a simple application that reads a large text file and reports exactly one instance of each word that appears within it. This application can be efficiently implemented by maintaining a dictionary of all distinct words that have appeared thus far in the text and inserting/reporting each new word that appears in the stream. Write a brief report with your conclusions.<br /> <br /> <br /> :[[3.33]]. A Caesar shift (see Section 21.6 (page 697)) is a very simple class of ciphers for secret messages. Unfortunately, they can be broken using statistical properties of English. Develop a program capable of decrypting Caesar shifts of sufficiently long texts.<br /> [[3.33|Solution]]<br /> <br /> ===Interview Problems===<br /> <br /> :3.34. What method would you use to look up a word in a dictionary?<br /> <br /> <br /> :[[3.35]]. Imagine you have a closet full of shirts. What can you do to organize your shirts for easy retrieval?<br /> [[3.35|Solution]]<br /> <br /> <br /> :3.36. Write a function to find the middle node of a singly linked list.<br /> <br /> <br /> :[[3.37]]. Write a function to determine whether two binary trees are identical. Identical trees have the same key value at each position and the same structure.<br /> [[3.37|Solution]]<br /> <br /> <br /> :3.38. Write a program to convert a binary search tree into a linked list.<br /> <br /> <br /> :[[3.39]]. Implement an algorithm to reverse a linked list. Now do it without recursion.<br /> [[3.39|Solution]]<br /> <br /> <br /> :3.40. What is the best data structure for maintaining URLs that have been visited by a web crawler? Give an algorithm to test whether a given URL has already been visited, optimizing both space and time.<br /> <br /> <br /> :[[3.41]]. You are given a search string and a magazine. You seek to generate all the characters in the search string by cutting them out from the magazine. Give an algorithm to efficiently determine whether the magazine contains all the letters in the search string.<br /> [[3.41|Solution]]<br /> <br /> <br /> :3.42. Reverse the words in a sentence—that is, “My name is Chris” becomes “Chris is name My.” Optimize for time and space.<br /> <br /> <br /> :[[3.43]]. Determine whether a linked list contains a loop as quickly as possible without using any extra storage. Also, identify the location of the loop.<br /> [[3.43|Solution]]<br /> <br /> <br /> :3.44. You have an unordered array &lt;math&gt;X&lt;/math&gt; of &lt;math&gt;n&lt;/math&gt; integers. Find the array &lt;math&gt;M&lt;/math&gt; containing &lt;math&gt;n&lt;/math&gt; elements where &lt;math&gt;M_i&lt;/math&gt; is the product of all integers in &lt;math&gt;X&lt;/math&gt; except for &lt;math&gt;X_i&lt;/math&gt;. You may not use division. You can use extra memory. (Hint: there are solutions faster than &lt;math&gt;O(n^2)&lt;/math&gt;.)<br /> <br /> <br /> :[[3.45]]. Give an algorithm for finding an ordered word pair (e.g. “New York”) occurring with the greatest frequency in a given webpage. Which data structures would you use? Optimize both time and space.<br /> [[3.45|Solution]]<br /> <br /> <br /> Back to [[Chapter List]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=User:Algowikiadmin&diff=437 User:Algowikiadmin 2020-09-28T17:32:43Z <p>Algowikiadmin: Created page with &quot;This admin account belongs to those that created and moderate this Wiki.&quot;</p> <hr /> <div>This admin account belongs to those that created and moderate this Wiki.</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Main_Page&diff=436 Main Page 2020-09-28T17:29:58Z <p>Algowikiadmin: </p> <hr /> <div>The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with the third edition of Steven Skiena's ''The Algorithm Design Manual''. Students and other readers are encouraged to contribute hints and answers to all odd-numbered problems in the book, or expand/improve the solution contributed by others.<br /> <br /> Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution.<br /> <br /> <br /> [[Chapter List]]<br /> <br /> == Getting started ==<br /> ; Editing<br /> * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula]<br /> * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples]<br /> <br /> ; Configuration<br /> * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list]<br /> <br /> ; Mediawiki General<br /> * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ]<br /> * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]<br /> <br /> ; Questions<br /> *[[User: Algowikiadmin| The Admin]] (editing fixes for the problems)<br /> * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic)</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Main_Page&diff=435 Main Page 2020-09-28T17:27:37Z <p>Algowikiadmin: </p> <hr /> <div>The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with Steven Skiena's The Algorithm Design Manual. Students and other readers are encouraged to contribute hints and answers to all odd numbered problems in the book, or expand/improve the solution contributed by others.<br /> <br /> Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution.<br /> <br /> [[Chapter List]]<br /> <br /> == Getting started ==<br /> ; Editing<br /> * [http://meta.wikimedia.org/wiki/Help:Formula MediaWiki Help:Formula]<br /> * [http://meta.wikimedia.org/wiki/Help:Wikitext_examples Help:Wikitext Examples]<br /> <br /> ; Configuration<br /> * [http://www.mediawiki.org/wiki/Manual:Configuration_settings Configuration settings list]<br /> <br /> ; Mediawiki General<br /> * [http://www.mediawiki.org/wiki/Manual:FAQ MediaWiki FAQ]<br /> * [http://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]<br /> <br /> ; Questions<br /> * [http://www.cs.sunysb.edu/~skiena/ Steven Skiena] (for additional hints and algorithm magic)</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Main_Page&diff=434 Main Page 2020-09-28T17:26:46Z <p>Algowikiadmin: </p> <hr /> <div>The Wiki is an experiment, a grass-roots effort to create an answer key to aid self-study with Steven Skiena's The Algorithm Design Manual. Students and other readers are encouraged to contribute hints and answers to all odd numbered problems in the book, or expand/improve the solution contributed by others.<br /> <br /> Please do not use this resource to cheat on your class homework. Recognize that no authority certifies the correctness of these solutions; they could well have been submitted by the idiot who sits in the back row of your class. Also recognize that other students in your class have equal access to these solutions, and it is typically easy for professors to recognize when two students submit the same solution.<br /> <br /> [[Chapter List]]<br /> <br /> == Getting started ==<br /> * [https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:Configuration_settings Configuration settings list]<br /> * [https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:FAQ MediaWiki FAQ]<br /> * [https://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]<br /> * [https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation#Translation_resources Localise MediaWiki for your language]<br /> * [https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:Combating_spam Learn how to combat spam on your wiki]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Chapter_8&diff=433 Chapter 8 2020-09-21T14:13:01Z <p>Algowikiadmin: /* Shortest Paths */</p> <hr /> <div>=Weighted Graph Algorithms=<br /> <br /> ===Simulating Graph Algorithms===<br /> <br /> :[[8.1]]. For the graphs in Problem 7-1:<br /> :(a) Draw the spanning forest after every iteration of the main loop in Kruskal’s algorithm.<br /> :(b) Draw the spanning forest after every iteration of the main loop in Prim’s algorithm.<br /> :(c) Find the shortest-path spanning tree rooted in &lt;math&gt;A&lt;/math&gt;.<br /> :(d) Compute the maximum flow from &lt;math&gt;A&lt;/math&gt; to &lt;math&gt;H&lt;/math&gt;.<br /> [[8.1|Solution]]<br /> <br /> ===Minimum Spanning Tree===<br /> <br /> :8.2. Is the path between two vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample.<br /> <br /> <br /> :[[8.3]]. Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the path between a pair of vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample.<br /> [[8.3|Solution]]<br /> <br /> <br /> :8.4. Can Prim’s and Kruskal’s algorithms yield different minimum spanning trees? Explain why or why not.<br /> <br /> <br /> :[[8.5]]. Does either Prim’s or Kruskal’s algorithm work if there are negative edge weights? Explain why or why not.<br /> [[8.5|Solution]]<br /> <br /> <br /> :8.6. (a) Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the ''minimum spanning tree'' of this graph unique? Give a proof or a counterexample.<br /> :(b) Again, assume that all edges in the graph have distinct edge weights (i.e. no pair of edges have the same weight). Is the ''shortest-path spanning tree'' of this graph unique? Give a proof or a counterexample.<br /> <br /> <br /> :[[8.7]]. Suppose we are given the minimum spanning tree &lt;math&gt;T&lt;/math&gt; of a given graph &lt;math&gt;G&lt;/math&gt; (with &lt;math&gt;n&lt;/math&gt; vertices and &lt;math&gt;m&lt;/math&gt; edges) and a new edge &lt;math&gt;e = (u, v)&lt;/math&gt; of weight &lt;math&gt;w&lt;/math&gt; that we will add to &lt;math&gt;G&lt;/math&gt;. Give an efficient algorithm to find the minimum spanning tree of the graph &lt;math&gt;G + e&lt;/math&gt;. Your algorithm should run in &lt;math&gt;O(n)&lt;/math&gt; time to receive full credit.<br /> [[8.7|Solution]]<br /> <br /> <br /> :8.8. (a) Let &lt;math&gt;T&lt;/math&gt; be a minimum spanning tree of a weighted graph &lt;math&gt;G&lt;/math&gt;. Construct a new graph &lt;math&gt;G'&lt;/math&gt; by adding a weight of &lt;math&gt;k&lt;/math&gt; to every edge of &lt;math&gt;G&lt;/math&gt;. Do the edges of &lt;math&gt;T&lt;/math&gt; form a minimum spanning tree of &lt;math&gt;G'&lt;/math&gt;? Prove the statement or give a counterexample.<br /> :(b) Let &lt;math&gt;P = {s, . . . , t}&lt;/math&gt; describe a shortest path between vertices &lt;math&gt;s&lt;/math&gt; and &lt;math&gt;t&lt;/math&gt; of a weighted graph &lt;math&gt;G&lt;/math&gt;. Construct a new graph &lt;math&gt;G'&lt;/math&gt; by adding a weight of &lt;math&gt;k&lt;/math&gt; to every edge of &lt;math&gt;G&lt;/math&gt;. Does &lt;math&gt;P&lt;/math&gt; describe a shortest path from &lt;math&gt;s&lt;/math&gt; to &lt;math&gt;t&lt;/math&gt; in &lt;math&gt;G'&lt;/math&gt;? Prove the statement or give a counterexample.<br /> <br /> <br /> :[[8.9]]. Devise and analyze an algorithm that takes a weighted graph &lt;math&gt;G&lt;/math&gt; and finds the smallest change in the cost to a non-minimum spanning tree edge that would cause a change in the minimum spanning tree of &lt;math&gt;G&lt;/math&gt;. Your algorithm must be correct and run in polynomial time.<br /> [[8.9|Solution]]<br /> <br /> <br /> :8.10. Consider the problem of finding a minimum-weight connected subset &lt;math&gt;T&lt;/math&gt; of edges from a weighted connected graph &lt;math&gt;G&lt;/math&gt;. The weight of &lt;math&gt;T&lt;/math&gt; is the sum of all the edge weights in &lt;math&gt;T&lt;/math&gt;.<br /> :(a) Why is this problem not just the minimum spanning tree problem? (Hint: think negative weight edges.)<br /> :(b) Give an efficient algorithm to compute the minimum-weight connected subset &lt;math&gt;T&lt;/math&gt;.<br /> <br /> <br /> :[[8.11]]. Let &lt;math&gt;T = (V, E')&lt;/math&gt; be a minimum spanning tree of a given graph &lt;math&gt;G = (V, E)&lt;/math&gt; with positive edge weights. Now suppose the weight of a particular edge &lt;math&gt;e \in E&lt;/math&gt; is modified from &lt;math&gt;w(e)&lt;/math&gt; to a new value &lt;math&gt;\hat{w}(e)&lt;/math&gt;. We seek to update the minimum spanning tree &lt;math&gt;T&lt;/math&gt; to reflect this change without recomputing the entire tree from scratch. For each of the following four cases, give a linear-time algorithm to update the tree:<br /> :(a)&lt;math&gt;e \notin E'&lt;/math&gt; and &lt;math&gt; \hat{w}(e) &gt; w(e)&lt;/math&gt;<br /> :(b) &lt;math&gt;e \notin E'&lt;/math&gt; and &lt;math&gt;\hat{w}(e) &lt; w(e)&lt;/math&gt;<br /> :(c) &lt;math&gt;e \in E'&lt;/math&gt; and &lt;math&gt;\hat{w}(e) &lt; w(e)&lt;/math&gt;<br /> :(d) &lt;math&gt;e \in E' &lt;/math&gt; and &lt;math&gt;\hat{w}(e) &gt; w(e)&lt;/math&gt;<br /> [[8.11|Solution]]<br /> <br /> <br /> :8.12. Let &lt;math&gt;G=(V,E)&lt;/math&gt; be an undirected graph. A set &lt;math&gt;F \subseteq E&lt;/math&gt; of edges is called a ''feedback-edge set'' if every cycle of &lt;math&gt;G&lt;/math&gt; has at least one edge in &lt;math&gt;F&lt;/math&gt;.<br /> :(a)Suppose that &lt;math&gt;G&lt;/math&gt; is unweighted. Design an efficient algorithm to find a minimum-size feedback-edge set.<br /> :(b)Suppose that &lt;math&gt;G&lt;/math&gt; is a weighted undirected graph with positive edge weights. Design an efficient algorithm to find a minimum-weight feedback-edge set.<br /> <br /> ===Union Find===<br /> <br /> :[[8.13]]. Devise an efficient data structure to handle the following operations on a weighted directed graph:<br /> :(a)Merge two given components.<br /> :(b)Locate which component contains a given vertex &lt;math&gt;v&lt;/math&gt;.<br /> :(c)Retrieve a minimum edge from a given component.<br /> [[8.13|Solution]]<br /> <br /> <br /> :8.14. Design a data structure that can perform a sequence of, &lt;math&gt;m&lt;/math&gt; ''union'' and ''find'' operations on a universal set of &lt;math&gt;n&lt;/math&gt; elements, consisting of a sequence of all ''unions'' followed by a sequence of all ''finds'', in time &lt;math&gt;O(m+n)&lt;/math&gt;.<br /> <br /> ===Shortest Paths===<br /> <br /> :[[8.15]]. The ''single-destination shortest path'' problem for a directed graph seeks the shortest path ''from'' every vertex to a specified vertex &lt;math&gt;v&lt;/math&gt;. Give an efficient algorithm to solve the single-destination shortest paths problem.<br /> [[8.15|Solution]]<br /> <br /> <br /> :8.16. Let &lt;math&gt;G=(V,E)&lt;/math&gt; be an undirected weighted graph, and let &lt;math&gt;T&lt;/math&gt; be the shortest-path spanning tree rooted at a vertex &lt;math&gt;v&lt;/math&gt;. Suppose now that all the edge weights in &lt;math&gt;G&lt;/math&gt; are increased by a constant number &lt;math&gt;k&lt;/math&gt;. Is &lt;math&gt;T&lt;/math&gt; still the shortest-path spanning tree from &lt;math&gt;v&lt;/math&gt;?<br /> <br /> <br /> :[[8.17]]. (a)Give an example of a weighted connected graph &lt;math&gt;G=(V,E)&lt;/math&gt; and a vertex &lt;math&gt;v&lt;/math&gt;, such that the minimum spanning tree of &lt;math&gt;G&lt;/math&gt; is the same as the shortest-path spanning tree rooted at &lt;math&gt;v&lt;/math&gt;.<br /> :(b)Give an example of a weighted connected directed graph &lt;math&gt;G=(V,E)&lt;/math&gt; and a vertex &lt;math&gt;v&lt;/math&gt;, such that the minimum-cost spanning tree of &lt;math&gt;G&lt;/math&gt; is very different from the shortest-path spanning tree rooted at &lt;math&gt;v&lt;/math&gt;.<br /> :(c)Can the two trees be completely disjointed?<br /> [[8.17|Solution]]<br /> <br /> <br /> :8.18. Either prove the following or give a counterexample:<br /> :(a)Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path?<br /> :(b)Suppose that the minimum spanning tree of the graph is unique. Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path?<br /> <br /> <br /> :[[8.19]]. Give an efficient algorithm to find the shortest path from &lt;math&gt;x&lt;/math&gt; to &lt;math&gt;y&lt;/math&gt; in an undirected weighted graph &lt;math&gt;G = (V, E)&lt;/math&gt; with positive edge weights, subject to the constraint that this path must pass through a particular vertex &lt;math&gt;z&lt;/math&gt;.<br /> [[8.19|Solution]]<br /> <br /> <br /> :8.20. In certain graph problems, vertices have can have weights instead of or in addition to the weights of edges. Let &lt;math&gt;C_v&lt;/math&gt; be the cost of vertex &lt;math&gt;v&lt;/math&gt;, and &lt;math&gt;C_{(x,y)}&lt;/math&gt; the cost of the edge &lt;math&gt;(x,y)&lt;/math&gt;. This problem is concerned with finding the cheapest path between vertices &lt;math&gt;a&lt;/math&gt; and &lt;math&gt;b&lt;/math&gt; in a graph &lt;math&gt;G&lt;/math&gt;. The cost of a path is the sum of the costs of the edges and vertices encountered on the path.<br /> :(a)Suppose that each edge in the graph has a weight of zero (while non-edges have a cost of &lt;math&gt;\infty&lt;/math&gt;). Assume that &lt;math&gt;C_v = 1&lt;/math&gt; for all vertices &lt;math&gt;1 \leq v \leq n&lt;/math&gt; (i.e., all vertices have the same cost). Give an ''efficient'' algorithm to find the cheapest path from &lt;math&gt;a&lt;/math&gt; to &lt;math&gt;b&lt;/math&gt; and its time complexity.<br /> :(b)Now suppose that the vertex costs are not constant (but are all positive) and the edge costs remain as above. Give an ''efficient'' algorithm to find the cheapest path from &lt;math&gt;a&lt;/math&gt; to &lt;math&gt;b&lt;/math&gt; and its time complexity.<br /> :(c)Now suppose that both the edge and vertex costs are not constant (but are all positive). Give an ''efficient'' algorithm to find the cheapest path from &lt;math&gt;a&lt;/math&gt; to &lt;math&gt;b&lt;/math&gt; and its time complexity.<br /> <br /> <br /> :[[8.21]]. Give an &lt;math&gt;O(n^3)&lt;/math&gt; algorithm that takes an &lt;math&gt;n&lt;/math&gt;-vertex directed graph &lt;math&gt;G&lt;/math&gt; with positive edge lengths, and returns the length of the shortest cycle in the graph. This length is &lt;math&gt;\infty&lt;/math&gt; in the case of an acyclic graph.<br /> [[8.21|Solution]]<br /> <br /> <br /> :8.22. A highway network is represented by a weighted graph &lt;math&gt;G&lt;/math&gt;, with edges corresponding to roads and vertices corresponding to road intersections. Each road is labeled with the maximum possible height of vehicles that can pass through the road. Give an efficient algorithm to compute the maximum possible height of vehicles that can successfully travel from &lt;math&gt;s&lt;/math&gt; to &lt;math&gt;t&lt;/math&gt;. What is the runtime of your algorithm?<br /> <br /> <br /> :[[8.23]]. You are given a directed graph &lt;math&gt;G&lt;/math&gt; with possibly negative weighted edges, in which the shortest path between any two vertices is guaranteed to have at most &lt;math&gt;k&lt;/math&gt; edges. Give an algorithm that finds the shortest path between two vertices &lt;math&gt;u&lt;/math&gt; and &lt;math&gt;v&lt;/math&gt; in &lt;math&gt;O(k * (n + m))&lt;/math&gt; time.<br /> [[8.23|Solution]]<br /> <br /> <br /> :8.24. Can we solve the single-source ''longest''-path problem by changing ''minimum'' to ''maximum'' in Dijkstra’s algorithm? If so, then prove your algorithm correct. If not, then provide a counterexample.<br /> <br /> <br /> :[[8.25]]. Let &lt;math&gt;G = (V, E)&lt;/math&gt; be a weighted acyclic directed graph with possibly negative edge weights. Design a linear-time algorithm to solve the single-source shortest-path problem from a given source &lt;math&gt;v&lt;/math&gt;.<br /> [[8.25|Solution]]<br /> <br /> <br /> :8.26. Let &lt;math&gt;G=(V,E)&lt;/math&gt; be a directed weighted graph such that all the weights are positive. Let &lt;math&gt;v&lt;/math&gt; and &lt;math&gt;w&lt;/math&gt; be two vertices in &lt;math&gt;G&lt;/math&gt; and &lt;math&gt;k \leq |V|&lt;/math&gt; be an integer. Design an algorithm to find the shortest path from &lt;math&gt;v&lt;/math&gt; to &lt;math&gt;w&lt;/math&gt; that contains exactly &lt;math&gt;k&lt;/math&gt; edges. Note that the path need not be simple.<br /> <br /> <br /> :[[8.27]]. ''Arbitrage'' is the use of discrepancies in currency-exchange rates to make a profit. For example, there may be a small window of time during which 1 U.S. dollar buys 0.75 British pounds, 1 British pound buys 2 Australian dollars, and 1 Australian dollar buys 0.70 U.S. dollars. At such a time, a smart trader can trade one U.S. dollar and end up with &lt;math&gt;0.75 \times 2 \times 0.7 = 1.05&lt;/math&gt; U.S. dollars---a profit of 5%. Suppose that there are &lt;math&gt;n&lt;/math&gt; currencies &lt;math&gt;c_1,...,c_n&lt;/math&gt; and an &lt;math&gt;n \times n&lt;/math&gt; table &lt;math&gt;R&lt;/math&gt; of exchange rates, such that one unit of currency &lt;math&gt;c_i&lt;/math&gt; buys &lt;math&gt;R[i,j]&lt;/math&gt; units of currency &lt;math&gt;c_j&lt;/math&gt;. Devise and analyze an algorithm to determine the maximum value of &lt;math&gt; R[c_1,c_{i1}] \cdot R[c_{i1},c_{i2}] \cdots R[c_{i{k-1}},c_{ik}] \cdot R[c_{ik},c_1] &lt;/math&gt; Hint: think all-pairs shortest path.<br /> [[8.27|Solution]]<br /> <br /> ===Network Flow and Matching===<br /> <br /> :8.28. A matching in a graph is a set of disjoint edges—that is, edges that do not have common vertices. Give a linear-time algorithm to find a maximum matching in a tree.<br /> <br /> <br /> :[[8.29]]. An edge cover of an undirected graph &lt;math&gt;G = (V, E)&lt;/math&gt; is a set of edges such that each vertex in the graph is incident to at least one edge from the set. Give an efficient algorithm, based on matching, to find the minimum-size edge cover for &lt;math&gt;G&lt;/math&gt;.<br /> [[8.29|Solution]]<br /> <br /> <br /> Back to [[Chapter List]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=8.29&diff=432 8.29 2020-09-21T14:12:28Z <p>Algowikiadmin: Created page with &quot;1. Find maximum matching. Bipartite matching is described in the book. General matching would require Edmonds Blossom algorithm. 2. Include an arbitrary edge for every uncover...&quot;</p> <hr /> <div>1. Find maximum matching. Bipartite matching is described in the book. General matching would require Edmonds Blossom algorithm.<br /> 2. Include an arbitrary edge for every uncovered vertex. A greedy algorithm suffices.<br /> <br /> <br /> Back to [[Chapter 8]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=8.27&diff=431 8.27 2020-09-21T14:12:11Z <p>Algowikiadmin: Created page with &quot;The problem reduces to Floyd - Warshall algorithm if you take logs of all currency-exchange rates, as if a * b = c then ln(a) + ln(b) = ln(c). In computational finance people...&quot;</p> <hr /> <div>The problem reduces to Floyd - Warshall algorithm if you take logs of all currency-exchange rates, as if a * b = c then ln(a) + ln(b) = ln(c).<br /> <br /> In computational finance people often use logarithms of prices instead of prices themselves, as it gives faster calculations and makes it easy to apply log-normal distribution instead of normal distribution (log-normal distribution is more appropriate for asset prices).<br /> <br /> <br /> Back to [[Chapter 8]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=8.25&diff=430 8.25 2020-09-21T14:11:39Z <p>Algowikiadmin: Created page with &quot;Step 1: Perform topological sorting of the graph (we can do it as Graph is acyclic). This is O(n + m) Step 2: Go through vertices in topological order. Initially all vertic...&quot;</p> <hr /> <div>Step 1:<br /> <br /> Perform topological sorting of the graph (we can do it as Graph is acyclic). This is O(n + m)<br /> <br /> Step 2:<br /> <br /> Go through vertices in topological order. Initially all vertices marked as inaccessible and only starting vertex marked with 0 distance.<br /> For every vertex in cycle, if it is accessible, we update all its children with new distance if new distance is shorter then previous one.<br /> Topological sorting ensures that we don't ever need to go backtrack. This is O(n + m).<br /> <br /> Total running bound is O(n+m), which is linear as requested.<br /> <br /> <br /> Back to [[Chapter 8]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=8.23&diff=429 8.23 2020-09-21T14:10:45Z <p>Algowikiadmin: Created page with &quot; Back to Chapter 8&quot;</p> <hr /> <div><br /> <br /> Back to [[Chapter 8]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Chapter_8&diff=428 Chapter 8 2020-09-21T14:10:28Z <p>Algowikiadmin: /* Shortest Paths */</p> <hr /> <div>=Weighted Graph Algorithms=<br /> <br /> ===Simulating Graph Algorithms===<br /> <br /> :[[8.1]]. For the graphs in Problem 7-1:<br /> :(a) Draw the spanning forest after every iteration of the main loop in Kruskal’s algorithm.<br /> :(b) Draw the spanning forest after every iteration of the main loop in Prim’s algorithm.<br /> :(c) Find the shortest-path spanning tree rooted in &lt;math&gt;A&lt;/math&gt;.<br /> :(d) Compute the maximum flow from &lt;math&gt;A&lt;/math&gt; to &lt;math&gt;H&lt;/math&gt;.<br /> [[8.1|Solution]]<br /> <br /> ===Minimum Spanning Tree===<br /> <br /> :8.2. Is the path between two vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample.<br /> <br /> <br /> :[[8.3]]. Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the path between a pair of vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample.<br /> [[8.3|Solution]]<br /> <br /> <br /> :8.4. Can Prim’s and Kruskal’s algorithms yield different minimum spanning trees? Explain why or why not.<br /> <br /> <br /> :[[8.5]]. Does either Prim’s or Kruskal’s algorithm work if there are negative edge weights? Explain why or why not.<br /> [[8.5|Solution]]<br /> <br /> <br /> :8.6. (a) Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the ''minimum spanning tree'' of this graph unique? Give a proof or a counterexample.<br /> :(b) Again, assume that all edges in the graph have distinct edge weights (i.e. no pair of edges have the same weight). Is the ''shortest-path spanning tree'' of this graph unique? Give a proof or a counterexample.<br /> <br /> <br /> :[[8.7]]. Suppose we are given the minimum spanning tree &lt;math&gt;T&lt;/math&gt; of a given graph &lt;math&gt;G&lt;/math&gt; (with &lt;math&gt;n&lt;/math&gt; vertices and &lt;math&gt;m&lt;/math&gt; edges) and a new edge &lt;math&gt;e = (u, v)&lt;/math&gt; of weight &lt;math&gt;w&lt;/math&gt; that we will add to &lt;math&gt;G&lt;/math&gt;. Give an efficient algorithm to find the minimum spanning tree of the graph &lt;math&gt;G + e&lt;/math&gt;. Your algorithm should run in &lt;math&gt;O(n)&lt;/math&gt; time to receive full credit.<br /> [[8.7|Solution]]<br /> <br /> <br /> :8.8. (a) Let &lt;math&gt;T&lt;/math&gt; be a minimum spanning tree of a weighted graph &lt;math&gt;G&lt;/math&gt;. Construct a new graph &lt;math&gt;G'&lt;/math&gt; by adding a weight of &lt;math&gt;k&lt;/math&gt; to every edge of &lt;math&gt;G&lt;/math&gt;. Do the edges of &lt;math&gt;T&lt;/math&gt; form a minimum spanning tree of &lt;math&gt;G'&lt;/math&gt;? Prove the statement or give a counterexample.<br /> :(b) Let &lt;math&gt;P = {s, . . . , t}&lt;/math&gt; describe a shortest path between vertices &lt;math&gt;s&lt;/math&gt; and &lt;math&gt;t&lt;/math&gt; of a weighted graph &lt;math&gt;G&lt;/math&gt;. Construct a new graph &lt;math&gt;G'&lt;/math&gt; by adding a weight of &lt;math&gt;k&lt;/math&gt; to every edge of &lt;math&gt;G&lt;/math&gt;. Does &lt;math&gt;P&lt;/math&gt; describe a shortest path from &lt;math&gt;s&lt;/math&gt; to &lt;math&gt;t&lt;/math&gt; in &lt;math&gt;G'&lt;/math&gt;? Prove the statement or give a counterexample.<br /> <br /> <br /> :[[8.9]]. Devise and analyze an algorithm that takes a weighted graph &lt;math&gt;G&lt;/math&gt; and finds the smallest change in the cost to a non-minimum spanning tree edge that would cause a change in the minimum spanning tree of &lt;math&gt;G&lt;/math&gt;. Your algorithm must be correct and run in polynomial time.<br /> [[8.9|Solution]]<br /> <br /> <br /> :8.10. Consider the problem of finding a minimum-weight connected subset &lt;math&gt;T&lt;/math&gt; of edges from a weighted connected graph &lt;math&gt;G&lt;/math&gt;. The weight of &lt;math&gt;T&lt;/math&gt; is the sum of all the edge weights in &lt;math&gt;T&lt;/math&gt;.<br /> :(a) Why is this problem not just the minimum spanning tree problem? (Hint: think negative weight edges.)<br /> :(b) Give an efficient algorithm to compute the minimum-weight connected subset &lt;math&gt;T&lt;/math&gt;.<br /> <br /> <br /> :[[8.11]]. Let &lt;math&gt;T = (V, E')&lt;/math&gt; be a minimum spanning tree of a given graph &lt;math&gt;G = (V, E)&lt;/math&gt; with positive edge weights. Now suppose the weight of a particular edge &lt;math&gt;e \in E&lt;/math&gt; is modified from &lt;math&gt;w(e)&lt;/math&gt; to a new value &lt;math&gt;\hat{w}(e)&lt;/math&gt;. We seek to update the minimum spanning tree &lt;math&gt;T&lt;/math&gt; to reflect this change without recomputing the entire tree from scratch. For each of the following four cases, give a linear-time algorithm to update the tree:<br /> :(a)&lt;math&gt;e \notin E'&lt;/math&gt; and &lt;math&gt; \hat{w}(e) &gt; w(e)&lt;/math&gt;<br /> :(b) &lt;math&gt;e \notin E'&lt;/math&gt; and &lt;math&gt;\hat{w}(e) &lt; w(e)&lt;/math&gt;<br /> :(c) &lt;math&gt;e \in E'&lt;/math&gt; and &lt;math&gt;\hat{w}(e) &lt; w(e)&lt;/math&gt;<br /> :(d) &lt;math&gt;e \in E' &lt;/math&gt; and &lt;math&gt;\hat{w}(e) &gt; w(e)&lt;/math&gt;<br /> [[8.11|Solution]]<br /> <br /> <br /> :8.12. Let &lt;math&gt;G=(V,E)&lt;/math&gt; be an undirected graph. A set &lt;math&gt;F \subseteq E&lt;/math&gt; of edges is called a ''feedback-edge set'' if every cycle of &lt;math&gt;G&lt;/math&gt; has at least one edge in &lt;math&gt;F&lt;/math&gt;.<br /> :(a)Suppose that &lt;math&gt;G&lt;/math&gt; is unweighted. Design an efficient algorithm to find a minimum-size feedback-edge set.<br /> :(b)Suppose that &lt;math&gt;G&lt;/math&gt; is a weighted undirected graph with positive edge weights. Design an efficient algorithm to find a minimum-weight feedback-edge set.<br /> <br /> ===Union Find===<br /> <br /> :[[8.13]]. Devise an efficient data structure to handle the following operations on a weighted directed graph:<br /> :(a)Merge two given components.<br /> :(b)Locate which component contains a given vertex &lt;math&gt;v&lt;/math&gt;.<br /> :(c)Retrieve a minimum edge from a given component.<br /> [[8.13|Solution]]<br /> <br /> <br /> :8.14. Design a data structure that can perform a sequence of, &lt;math&gt;m&lt;/math&gt; ''union'' and ''find'' operations on a universal set of &lt;math&gt;n&lt;/math&gt; elements, consisting of a sequence of all ''unions'' followed by a sequence of all ''finds'', in time &lt;math&gt;O(m+n)&lt;/math&gt;.<br /> <br /> ===Shortest Paths===<br /> <br /> :[[8:15]]. The ''single-destination shortest path'' problem for a directed graph seeks the shortest path ''from'' every vertex to a specified vertex &lt;math&gt;v&lt;/math&gt;. Give an efficient algorithm to solve the single-destination shortest paths problem.<br /> [[8.15|Solution]]<br /> <br /> <br /> :8.16. Let &lt;math&gt;G=(V,E)&lt;/math&gt; be an undirected weighted graph, and let &lt;math&gt;T&lt;/math&gt; be the shortest-path spanning tree rooted at a vertex &lt;math&gt;v&lt;/math&gt;. Suppose now that all the edge weights in &lt;math&gt;G&lt;/math&gt; are increased by a constant number &lt;math&gt;k&lt;/math&gt;. Is &lt;math&gt;T&lt;/math&gt; still the shortest-path spanning tree from &lt;math&gt;v&lt;/math&gt;?<br /> <br /> <br /> :[[8.17]]. (a)Give an example of a weighted connected graph &lt;math&gt;G=(V,E)&lt;/math&gt; and a vertex &lt;math&gt;v&lt;/math&gt;, such that the minimum spanning tree of &lt;math&gt;G&lt;/math&gt; is the same as the shortest-path spanning tree rooted at &lt;math&gt;v&lt;/math&gt;.<br /> :(b)Give an example of a weighted connected directed graph &lt;math&gt;G=(V,E)&lt;/math&gt; and a vertex &lt;math&gt;v&lt;/math&gt;, such that the minimum-cost spanning tree of &lt;math&gt;G&lt;/math&gt; is very different from the shortest-path spanning tree rooted at &lt;math&gt;v&lt;/math&gt;.<br /> :(c)Can the two trees be completely disjointed?<br /> [[8.17|Solution]]<br /> <br /> <br /> :8.18. Either prove the following or give a counterexample:<br /> :(a)Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path?<br /> :(b)Suppose that the minimum spanning tree of the graph is unique. Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path?<br /> <br /> <br /> :[[8.19]]. Give an efficient algorithm to find the shortest path from &lt;math&gt;x&lt;/math&gt; to &lt;math&gt;y&lt;/math&gt; in an undirected weighted graph &lt;math&gt;G = (V, E)&lt;/math&gt; with positive edge weights, subject to the constraint that this path must pass through a particular vertex &lt;math&gt;z&lt;/math&gt;.<br /> [[8.19|Solution]]<br /> <br /> <br /> :8.20. In certain graph problems, vertices have can have weights instead of or in addition to the weights of edges. Let &lt;math&gt;C_v&lt;/math&gt; be the cost of vertex &lt;math&gt;v&lt;/math&gt;, and &lt;math&gt;C_{(x,y)}&lt;/math&gt; the cost of the edge &lt;math&gt;(x,y)&lt;/math&gt;. This problem is concerned with finding the cheapest path between vertices &lt;math&gt;a&lt;/math&gt; and &lt;math&gt;b&lt;/math&gt; in a graph &lt;math&gt;G&lt;/math&gt;. The cost of a path is the sum of the costs of the edges and vertices encountered on the path.<br /> :(a)Suppose that each edge in the graph has a weight of zero (while non-edges have a cost of &lt;math&gt;\infty&lt;/math&gt;). Assume that &lt;math&gt;C_v = 1&lt;/math&gt; for all vertices &lt;math&gt;1 \leq v \leq n&lt;/math&gt; (i.e., all vertices have the same cost). Give an ''efficient'' algorithm to find the cheapest path from &lt;math&gt;a&lt;/math&gt; to &lt;math&gt;b&lt;/math&gt; and its time complexity.<br /> :(b)Now suppose that the vertex costs are not constant (but are all positive) and the edge costs remain as above. Give an ''efficient'' algorithm to find the cheapest path from &lt;math&gt;a&lt;/math&gt; to &lt;math&gt;b&lt;/math&gt; and its time complexity.<br /> :(c)Now suppose that both the edge and vertex costs are not constant (but are all positive). Give an ''efficient'' algorithm to find the cheapest path from &lt;math&gt;a&lt;/math&gt; to &lt;math&gt;b&lt;/math&gt; and its time complexity.<br /> <br /> <br /> :[[8.21]]. Give an &lt;math&gt;O(n^3)&lt;/math&gt; algorithm that takes an &lt;math&gt;n&lt;/math&gt;-vertex directed graph &lt;math&gt;G&lt;/math&gt; with positive edge lengths, and returns the length of the shortest cycle in the graph. This length is &lt;math&gt;\infty&lt;/math&gt; in the case of an acyclic graph.<br /> [[8.21|Solution]]<br /> <br /> <br /> :8.22. A highway network is represented by a weighted graph &lt;math&gt;G&lt;/math&gt;, with edges corresponding to roads and vertices corresponding to road intersections. Each road is labeled with the maximum possible height of vehicles that can pass through the road. Give an efficient algorithm to compute the maximum possible height of vehicles that can successfully travel from &lt;math&gt;s&lt;/math&gt; to &lt;math&gt;t&lt;/math&gt;. What is the runtime of your algorithm?<br /> <br /> <br /> :[[8.23]]. You are given a directed graph &lt;math&gt;G&lt;/math&gt; with possibly negative weighted edges, in which the shortest path between any two vertices is guaranteed to have at most &lt;math&gt;k&lt;/math&gt; edges. Give an algorithm that finds the shortest path between two vertices &lt;math&gt;u&lt;/math&gt; and &lt;math&gt;v&lt;/math&gt; in &lt;math&gt;O(k * (n + m))&lt;/math&gt; time.<br /> [[8.23|Solution]]<br /> <br /> <br /> :8.24. Can we solve the single-source ''longest''-path problem by changing ''minimum'' to ''maximum'' in Dijkstra’s algorithm? If so, then prove your algorithm correct. If not, then provide a counterexample.<br /> <br /> <br /> :[[8.25]]. Let &lt;math&gt;G = (V, E)&lt;/math&gt; be a weighted acyclic directed graph with possibly negative edge weights. Design a linear-time algorithm to solve the single-source shortest-path problem from a given source &lt;math&gt;v&lt;/math&gt;.<br /> [[8.25|Solution]]<br /> <br /> <br /> :8.26. Let &lt;math&gt;G=(V,E)&lt;/math&gt; be a directed weighted graph such that all the weights are positive. Let &lt;math&gt;v&lt;/math&gt; and &lt;math&gt;w&lt;/math&gt; be two vertices in &lt;math&gt;G&lt;/math&gt; and &lt;math&gt;k \leq |V|&lt;/math&gt; be an integer. Design an algorithm to find the shortest path from &lt;math&gt;v&lt;/math&gt; to &lt;math&gt;w&lt;/math&gt; that contains exactly &lt;math&gt;k&lt;/math&gt; edges. Note that the path need not be simple.<br /> <br /> <br /> :[[8.27]]. ''Arbitrage'' is the use of discrepancies in currency-exchange rates to make a profit. For example, there may be a small window of time during which 1 U.S. dollar buys 0.75 British pounds, 1 British pound buys 2 Australian dollars, and 1 Australian dollar buys 0.70 U.S. dollars. At such a time, a smart trader can trade one U.S. dollar and end up with &lt;math&gt;0.75 \times 2 \times 0.7 = 1.05&lt;/math&gt; U.S. dollars---a profit of 5%. Suppose that there are &lt;math&gt;n&lt;/math&gt; currencies &lt;math&gt;c_1,...,c_n&lt;/math&gt; and an &lt;math&gt;n \times n&lt;/math&gt; table &lt;math&gt;R&lt;/math&gt; of exchange rates, such that one unit of currency &lt;math&gt;c_i&lt;/math&gt; buys &lt;math&gt;R[i,j]&lt;/math&gt; units of currency &lt;math&gt;c_j&lt;/math&gt;. Devise and analyze an algorithm to determine the maximum value of &lt;math&gt; R[c_1,c_{i1}] \cdot R[c_{i1},c_{i2}] \cdots R[c_{i{k-1}},c_{ik}] \cdot R[c_{ik},c_1] &lt;/math&gt; Hint: think all-pairs shortest path.<br /> [[8.27|Solution]]<br /> <br /> ===Network Flow and Matching===<br /> <br /> :8.28. A matching in a graph is a set of disjoint edges—that is, edges that do not have common vertices. Give a linear-time algorithm to find a maximum matching in a tree.<br /> <br /> <br /> :[[8.29]]. An edge cover of an undirected graph &lt;math&gt;G = (V, E)&lt;/math&gt; is a set of edges such that each vertex in the graph is incident to at least one edge from the set. Give an efficient algorithm, based on matching, to find the minimum-size edge cover for &lt;math&gt;G&lt;/math&gt;.<br /> [[8.29|Solution]]<br /> <br /> <br /> Back to [[Chapter List]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=8.21&diff=427 8.21 2020-09-21T14:09:46Z <p>Algowikiadmin: Created page with &quot; Back to Chapter 8&quot;</p> <hr /> <div><br /> <br /> Back to [[Chapter 8]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=8.19&diff=426 8.19 2020-09-21T14:09:39Z <p>Algowikiadmin: Created page with &quot; Back to Chapter 8&quot;</p> <hr /> <div><br /> <br /> Back to [[Chapter 8]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=8.17&diff=425 8.17 2020-09-21T14:09:33Z <p>Algowikiadmin: Created page with &quot; Back to Chapter 8&quot;</p> <hr /> <div><br /> <br /> Back to [[Chapter 8]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=8.15&diff=424 8.15 2020-09-21T14:09:09Z <p>Algowikiadmin: Created page with &quot; Back to Chapter 8&quot;</p> <hr /> <div><br /> <br /> Back to [[Chapter 8]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=Chapter_8&diff=423 Chapter 8 2020-09-21T14:08:42Z <p>Algowikiadmin: /* Shortest Paths */</p> <hr /> <div>=Weighted Graph Algorithms=<br /> <br /> ===Simulating Graph Algorithms===<br /> <br /> :[[8.1]]. For the graphs in Problem 7-1:<br /> :(a) Draw the spanning forest after every iteration of the main loop in Kruskal’s algorithm.<br /> :(b) Draw the spanning forest after every iteration of the main loop in Prim’s algorithm.<br /> :(c) Find the shortest-path spanning tree rooted in &lt;math&gt;A&lt;/math&gt;.<br /> :(d) Compute the maximum flow from &lt;math&gt;A&lt;/math&gt; to &lt;math&gt;H&lt;/math&gt;.<br /> [[8.1|Solution]]<br /> <br /> ===Minimum Spanning Tree===<br /> <br /> :8.2. Is the path between two vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample.<br /> <br /> <br /> :[[8.3]]. Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the path between a pair of vertices in a minimum spanning tree necessarily a shortest path between the two vertices in the full graph? Give a proof or a counterexample.<br /> [[8.3|Solution]]<br /> <br /> <br /> :8.4. Can Prim’s and Kruskal’s algorithms yield different minimum spanning trees? Explain why or why not.<br /> <br /> <br /> :[[8.5]]. Does either Prim’s or Kruskal’s algorithm work if there are negative edge weights? Explain why or why not.<br /> [[8.5|Solution]]<br /> <br /> <br /> :8.6. (a) Assume that all edges in the graph have distinct edge weights (i.e., no pair of edges have the same weight). Is the ''minimum spanning tree'' of this graph unique? Give a proof or a counterexample.<br /> :(b) Again, assume that all edges in the graph have distinct edge weights (i.e. no pair of edges have the same weight). Is the ''shortest-path spanning tree'' of this graph unique? Give a proof or a counterexample.<br /> <br /> <br /> :[[8.7]]. Suppose we are given the minimum spanning tree &lt;math&gt;T&lt;/math&gt; of a given graph &lt;math&gt;G&lt;/math&gt; (with &lt;math&gt;n&lt;/math&gt; vertices and &lt;math&gt;m&lt;/math&gt; edges) and a new edge &lt;math&gt;e = (u, v)&lt;/math&gt; of weight &lt;math&gt;w&lt;/math&gt; that we will add to &lt;math&gt;G&lt;/math&gt;. Give an efficient algorithm to find the minimum spanning tree of the graph &lt;math&gt;G + e&lt;/math&gt;. Your algorithm should run in &lt;math&gt;O(n)&lt;/math&gt; time to receive full credit.<br /> [[8.7|Solution]]<br /> <br /> <br /> :8.8. (a) Let &lt;math&gt;T&lt;/math&gt; be a minimum spanning tree of a weighted graph &lt;math&gt;G&lt;/math&gt;. Construct a new graph &lt;math&gt;G'&lt;/math&gt; by adding a weight of &lt;math&gt;k&lt;/math&gt; to every edge of &lt;math&gt;G&lt;/math&gt;. Do the edges of &lt;math&gt;T&lt;/math&gt; form a minimum spanning tree of &lt;math&gt;G'&lt;/math&gt;? Prove the statement or give a counterexample.<br /> :(b) Let &lt;math&gt;P = {s, . . . , t}&lt;/math&gt; describe a shortest path between vertices &lt;math&gt;s&lt;/math&gt; and &lt;math&gt;t&lt;/math&gt; of a weighted graph &lt;math&gt;G&lt;/math&gt;. Construct a new graph &lt;math&gt;G'&lt;/math&gt; by adding a weight of &lt;math&gt;k&lt;/math&gt; to every edge of &lt;math&gt;G&lt;/math&gt;. Does &lt;math&gt;P&lt;/math&gt; describe a shortest path from &lt;math&gt;s&lt;/math&gt; to &lt;math&gt;t&lt;/math&gt; in &lt;math&gt;G'&lt;/math&gt;? Prove the statement or give a counterexample.<br /> <br /> <br /> :[[8.9]]. Devise and analyze an algorithm that takes a weighted graph &lt;math&gt;G&lt;/math&gt; and finds the smallest change in the cost to a non-minimum spanning tree edge that would cause a change in the minimum spanning tree of &lt;math&gt;G&lt;/math&gt;. Your algorithm must be correct and run in polynomial time.<br /> [[8.9|Solution]]<br /> <br /> <br /> :8.10. Consider the problem of finding a minimum-weight connected subset &lt;math&gt;T&lt;/math&gt; of edges from a weighted connected graph &lt;math&gt;G&lt;/math&gt;. The weight of &lt;math&gt;T&lt;/math&gt; is the sum of all the edge weights in &lt;math&gt;T&lt;/math&gt;.<br /> :(a) Why is this problem not just the minimum spanning tree problem? (Hint: think negative weight edges.)<br /> :(b) Give an efficient algorithm to compute the minimum-weight connected subset &lt;math&gt;T&lt;/math&gt;.<br /> <br /> <br /> :[[8.11]]. Let &lt;math&gt;T = (V, E')&lt;/math&gt; be a minimum spanning tree of a given graph &lt;math&gt;G = (V, E)&lt;/math&gt; with positive edge weights. Now suppose the weight of a particular edge &lt;math&gt;e \in E&lt;/math&gt; is modified from &lt;math&gt;w(e)&lt;/math&gt; to a new value &lt;math&gt;\hat{w}(e)&lt;/math&gt;. We seek to update the minimum spanning tree &lt;math&gt;T&lt;/math&gt; to reflect this change without recomputing the entire tree from scratch. For each of the following four cases, give a linear-time algorithm to update the tree:<br /> :(a)&lt;math&gt;e \notin E'&lt;/math&gt; and &lt;math&gt; \hat{w}(e) &gt; w(e)&lt;/math&gt;<br /> :(b) &lt;math&gt;e \notin E'&lt;/math&gt; and &lt;math&gt;\hat{w}(e) &lt; w(e)&lt;/math&gt;<br /> :(c) &lt;math&gt;e \in E'&lt;/math&gt; and &lt;math&gt;\hat{w}(e) &lt; w(e)&lt;/math&gt;<br /> :(d) &lt;math&gt;e \in E' &lt;/math&gt; and &lt;math&gt;\hat{w}(e) &gt; w(e)&lt;/math&gt;<br /> [[8.11|Solution]]<br /> <br /> <br /> :8.12. Let &lt;math&gt;G=(V,E)&lt;/math&gt; be an undirected graph. A set &lt;math&gt;F \subseteq E&lt;/math&gt; of edges is called a ''feedback-edge set'' if every cycle of &lt;math&gt;G&lt;/math&gt; has at least one edge in &lt;math&gt;F&lt;/math&gt;.<br /> :(a)Suppose that &lt;math&gt;G&lt;/math&gt; is unweighted. Design an efficient algorithm to find a minimum-size feedback-edge set.<br /> :(b)Suppose that &lt;math&gt;G&lt;/math&gt; is a weighted undirected graph with positive edge weights. Design an efficient algorithm to find a minimum-weight feedback-edge set.<br /> <br /> ===Union Find===<br /> <br /> :[[8.13]]. Devise an efficient data structure to handle the following operations on a weighted directed graph:<br /> :(a)Merge two given components.<br /> :(b)Locate which component contains a given vertex &lt;math&gt;v&lt;/math&gt;.<br /> :(c)Retrieve a minimum edge from a given component.<br /> [[8.13|Solution]]<br /> <br /> <br /> :8.14. Design a data structure that can perform a sequence of, &lt;math&gt;m&lt;/math&gt; ''union'' and ''find'' operations on a universal set of &lt;math&gt;n&lt;/math&gt; elements, consisting of a sequence of all ''unions'' followed by a sequence of all ''finds'', in time &lt;math&gt;O(m+n)&lt;/math&gt;.<br /> <br /> ===Shortest Paths===<br /> <br /> :[[8:15]]. The ''single-destination shortest path'' problem for a directed graph seeks the shortest path ''from'' every vertex to a specified vertex &lt;math&gt;v&lt;/math&gt;. Give an efficient algorithm to solve the single-destination shortest paths problem.<br /> [[8.15|Solution]]<br /> <br /> <br /> :8.16. Let &lt;math&gt;G=(V,E)&lt;/math&gt; be an undirected weighted graph, and let &lt;math&gt;T&lt;/math&gt; be the shortest-path spanning tree rooted at a vertex &lt;math&gt;v&lt;/math&gt;. Suppose now that all the edge weights in &lt;math&gt;G&lt;/math&gt; are increased by a constant number &lt;math&gt;k&lt;/math&gt;. Is &lt;math&gt;T&lt;/math&gt; still the shortest-path spanning tree from &lt;math&gt;v&lt;/math&gt;?<br /> <br /> <br /> :[[8.17]]. (a)Give an example of a weighted connected graph &lt;math&gt;G=(V,E)&lt;/math&gt; and a vertex &lt;math&gt;v&lt;/math&gt;, such that the minimum spanning tree of &lt;math&gt;G&lt;/math&gt; is the same as the shortest-path spanning tree rooted at &lt;math&gt;v&lt;/math&gt;.<br /> :(b)Give an example of a weighted connected directed graph &lt;math&gt;G=(V,E)&lt;/math&gt; and a vertex &lt;math&gt;v&lt;/math&gt;, such that the minimum-cost spanning tree of &lt;math&gt;G&lt;/math&gt; is very different from the shortest-path spanning tree rooted at &lt;math&gt;v&lt;/math&gt;.<br /> :(c)Can the two trees be completely disjointed?<br /> [[8.17|Solution]]<br /> <br /> <br /> :8.18. Either prove the following or give a counterexample:<br /> :(a)Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path?<br /> :(b)Suppose that the minimum spanning tree of the graph is unique. Is the path between a pair of vertices in a minimum spanning tree of an undirected graph necessarily the shortest (minimum weight) path?<br /> <br /> <br /> :[[8.19]]. Give an efficient algorithm to find the shortest path from &lt;math&gt;x&lt;/math&gt; to &lt;math&gt;y&lt;/math&gt; in an undirected weighted graph &lt;math&gt;G = (V, E)&lt;/math&gt; with positive edge weights, subject to the constraint that this path must pass through a particular vertex &lt;math&gt;z&lt;/math&gt;.<br /> [[8.19|Solution]]<br /> <br /> <br /> :8.20. In certain graph problems, vertices have can have weights instead of or in addition to the weights of edges. Let &lt;math&gt;C_v&lt;/math&gt; be the cost of vertex &lt;math&gt;v&lt;/math&gt;, and &lt;math&gt;C_{(x,y)}&lt;/math&gt; the cost of the edge &lt;math&gt;(x,y)&lt;/math&gt;. This problem is concerned with finding the cheapest path between vertices &lt;math&gt;a&lt;/math&gt; and &lt;math&gt;b&lt;/math&gt; in a graph &lt;math&gt;G&lt;/math&gt;. The cost of a path is the sum of the costs of the edges and vertices encountered on the path.<br /> :(a)Suppose that each edge in the graph has a weight of zero (while non-edges have a cost of &lt;math&gt;\infty&lt;/math&gt;). Assume that &lt;math&gt;C_v = 1&lt;/math&gt; for all vertices &lt;math&gt;1 \leq v \leq n&lt;/math&gt; (i.e., all vertices have the same cost). Give an ''efficient'' algorithm to find the cheapest path from &lt;math&gt;a&lt;/math&gt; to &lt;math&gt;b&lt;/math&gt; and its time complexity.<br /> :(b)Now suppose that the vertex costs are not constant (but are all positive) and the edge costs remain as above. Give an ''efficient'' algorithm to find the cheapest path from &lt;math&gt;a&lt;/math&gt; to &lt;math&gt;b&lt;/math&gt; and its time complexity.<br /> :(c)Now suppose that both the edge and vertex costs are not constant (but are all positive). Give an ''efficient'' algorithm to find the cheapest path from &lt;math&gt;a&lt;/math&gt; to &lt;math&gt;b&lt;/math&gt; and its time complexity.<br /> <br /> <br /> :[[8.21]]. Give an &lt;math&gt;O(n^3)&lt;/math&gt; algorithm that takes an &lt;math&gt;n&lt;/math&gt;-vertex directed graph &lt;math&gt;G&lt;/math&gt; with positive edge lengths, and returns the length of the shortest cycle in the graph. This length is &lt;math&gt;\infty&lt;/math&gt; in the case of an acyclic graph.<br /> [[8.21|Solution]]<br /> <br /> <br /> :8.22. A highway network is represented by a weighted graph &lt;math&gt;G&lt;/math&gt;, with edges corresponding to roads and vertices corresponding to road intersections. Each road is labeled with the maximum possible height of vehicles that can pass through the road. Give an efficient algorithm to compute the maximum possible height of vehicles that can successfully travel from &lt;math&gt;s&lt;/math&gt; to &lt;math&gt;t&lt;/math&gt;. What is the runtime of your algorithm?<br /> <br /> <br /> :[[8.23]]. You are given a directed graph &lt;math&gt;G&lt;/math&gt; with possibly negative weighted edges, in which the shortest path between any two vertices is guaranteed to have at most &lt;math&gt;k&lt;/math&gt; edges. Give an algorithm that finds the shortest path between two vertices &lt;math&gt;u&lt;/math&gt; and &lt;math&gt;v&lt;/math&gt; in &lt;math&gt;O(k * (n + m))&lt;/math&gt; time.<br /> [[8.23|Solution]]<br /> <br /> <br /> :8.24. Can we solve the single-source ''longest''-path problem by changing ''minimum'' to ''maximum'' in Dijkstra’s algorithm? If so, then prove your algorithm correct. If not, then provide a counterexample.<br /> <br /> <br /> :[[8.25]]. Let &lt;math&gt;G = (V, E)&lt;/math&gt; be a weighted acyclic directed graph with possibly negative edge weights. Design a linear-time algorithm to solve the single-source shortest-path problem from a given source &lt;math&gt;v&lt;/math&gt;.<br /> [[8.25|Solution]]<br /> <br /> <br /> :8.26. Let &lt;math&gt;G=(V,E)&lt;/math&gt; be a directed weighted graph such that all the weights are positive. Let &lt;math&gt;v&lt;/math&gt; and &lt;math&gt;w&lt;/math&gt; be two vertices in &lt;math&gt;G&lt;/math&gt; and &lt;math&gt;k \leq |V|&lt;/math&gt; be an integer. Design an algorithm to find the shortest path from &lt;math&gt;v&lt;/math&gt; to &lt;math&gt;w&lt;/math&gt; that contains exactly &lt;math&gt;k&lt;/math&gt; edges. Note that the path need not be simple.<br /> <br /> <br /> :[[8.27]].''Arbitrage'' is the use of discrepancies in currency-exchange rates to make a profit. For example, there may be a small window of time during which 1 U.S. dollar buys 0.75 British pounds, 1 British pound buys 2 Australian dollars, and 1 Australian dollar buys 0.70 U.S. dollars. At such a time, a smart trader can trade one U.S. dollar and end up with &lt;math&gt;0.75 \times 2 \times 0.7 = 1.05&lt;/math&gt; U.S. dollars---a profit of 5%. Suppose that there are &lt;math&gt;n&lt;/math&gt; currencies &lt;math&gt;c_1,...,c_n&lt;/math&gt; and an &lt;math&gt;n \times n&lt;/math&gt; table &lt;math&gt;R&lt;/math&gt; of exchange rates, such that one unit of currency &lt;math&gt;c_i&lt;/math&gt; buys &lt;math&gt;R[i,j]&lt;/math&gt; units of currency &lt;math&gt;c_j&lt;/math&gt;. Devise and analyze an algorithm to determine the maximum value of &lt;math&gt; R[c_1,c_{i1}] \cdot R[c_{i1},c_{i2}] \cdots R[c_{i{k-1}},c_{ik}] \cdot R[c_{ik},c_1] &lt;/math&gt; Hint: think all-pairs shortest path.<br /> [[8.27|Solution]]<br /> <br /> ===Network Flow and Matching===<br /> <br /> :8.28. A matching in a graph is a set of disjoint edges—that is, edges that do not have common vertices. Give a linear-time algorithm to find a maximum matching in a tree.<br /> <br /> <br /> :[[8.29]]. An edge cover of an undirected graph &lt;math&gt;G = (V, E)&lt;/math&gt; is a set of edges such that each vertex in the graph is incident to at least one edge from the set. Give an efficient algorithm, based on matching, to find the minimum-size edge cover for &lt;math&gt;G&lt;/math&gt;.<br /> [[8.29|Solution]]<br /> <br /> <br /> Back to [[Chapter List]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=8.13&diff=422 8.13 2020-09-21T14:07:54Z <p>Algowikiadmin: Created page with &quot; Back to Chapter 8&quot;</p> <hr /> <div><br /> <br /> Back to [[Chapter 8]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=8.11&diff=421 8.11 2020-09-21T14:07:33Z <p>Algowikiadmin: Created page with &quot; Back to Chapter 8&quot;</p> <hr /> <div><br /> <br /> Back to [[Chapter 8]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=8.9&diff=420 8.9 2020-09-21T14:07:25Z <p>Algowikiadmin: Created page with &quot; Back to Chapter 8&quot;</p> <hr /> <div><br /> <br /> Back to [[Chapter 8]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=8.7&diff=419 8.7 2020-09-21T14:07:10Z <p>Algowikiadmin: Created page with &quot; Back to Chapter 8&quot;</p> <hr /> <div><br /> <br /> Back to [[Chapter 8]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=8.5&diff=418 8.5 2020-09-21T14:06:45Z <p>Algowikiadmin: Created page with &quot;In both algorithms, an edge can only ever be picked once, so they will both eventually terminate regardless of negative edge weights. I also suspect that they still generate...&quot;</p> <hr /> <div>In both algorithms, an edge can only ever be picked once, so they will both eventually terminate regardless of negative edge weights. I also suspect that they still generate minimum spanning trees, but don't have a proof of it.<br /> <br /> <br /> Back to [[Chapter 8]]</div> Algowikiadmin https://algorist.com//algowiki/index.php?title=8.3&diff=417 8.3 2020-09-21T14:06:03Z <p>Algowikiadmin: Created page with &quot;No. Counter example provided below: G(V,E,W) = ((A,B,C,D),({A,B},{B,C},{C,D},{D,A}),(1,2,3,4)) Minimum spanning tree has a weight of 6 with edges {A,B},{B,C},{C,D}. In the...&quot;</p> <hr /> <div>No. Counter example provided below:<br /> <br /> G(V,E,W) = ((A,B,C,D),({A,B},{B,C},{C,D},{D,A}),(1,2,3,4))<br /> <br /> Minimum spanning tree has a weight of 6 with edges {A,B},{B,C},{C,D}.<br /> <br /> In the full graph the minimum distance between A and D is 4.<br /> <br /> <br /> Back to [[Chapter 8]]</div> Algowikiadmin