<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Algorithms Q&amp;A - Recent questions and answers in Greedy Algorithms</title>
<link>https://notexponential.com/qa/greedy</link>
<description>Powered by Question2Answer</description>
<item>
<title>Answered: Is Greed the best choice?</title>
<link>https://notexponential.com/508/is-greed-the-best-choice?show=952#a952</link>
<description>&lt;p&gt;a.) Chess -&amp;nbsp;The current best step may or may not lead to the global best solution.&amp;nbsp;Because there are many possibilities where opponent can tackle our best step. We need to check all the possibilities. So, greedy may not be optimal/ applicable.&lt;/p&gt;&lt;p&gt;b.) Sorting - Yes, Greedy is applicable here but not necessarily encouraged. For ex-&amp;nbsp;Selection Sort. Where the local solution leads to the global solution. But the time complexity is high(O(n&lt;sup&gt;2&lt;/sup&gt;)). Using other approaches may give optimal ways to sort.&lt;/p&gt;&lt;p&gt;c.) Shortest path computation - Some problems like MST, Dijkstra&#039;s algorithm which are quite similar to Shortest path computation applies Greedy approach.&lt;/p&gt;&lt;p&gt;d.) Knapsack - Incase of fractional Knapsack we consider Max {Value/Weight} at each step which gives an optimal solution but for 0/1 Knapsack, dynamic programming gives optimal solution. So, for Fractional Knapsack, Greedy method is applicable.&amp;nbsp;&lt;/p&gt;</description>
<category>Greedy Algorithms</category>
<guid isPermaLink="true">https://notexponential.com/508/is-greed-the-best-choice?show=952#a952</guid>
<pubDate>Sat, 16 Dec 2023 18:29:53 +0000</pubDate>
</item>
<item>
<title>Answered: Maximum Product Spanning Tree</title>
<link>https://notexponential.com/505/maximum-product-spanning-tree?show=951#a951</link>
<description>​If the product &amp;nbsp;w1*w2*w3 is at its maximum, then the sum log(w1) + log(w2) + log(w3) is also at its maximum due to the logarithmic property. Therefore, substituting each w1 with its logarithmic value and determining the maximum weight spanning tree is similar to discovering the maximum product spanning tree in the original tree.</description>
<category>Greedy Algorithms</category>
<guid isPermaLink="true">https://notexponential.com/505/maximum-product-spanning-tree?show=951#a951</guid>
<pubDate>Sat, 16 Dec 2023 18:26:17 +0000</pubDate>
</item>
<item>
<title>Answered: Interval Scheduling</title>
<link>https://notexponential.com/503/interval-scheduling?show=944#a944</link>
<description>&lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in; font-size: 12pt; font-family: Calibri, sans-serif; color: rgb(0, 0, 0);&quot;&gt;Sort the given intervals based on their end times. This is crucial to ensure that you are considering intervals that end earliest first.&amp;nbsp;Start with the first interval and consider it as part of the solution.&amp;nbsp;Iterate through the sorted intervals.&lt;/p&gt;&lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in; font-size: 12pt; font-family: Calibri, sans-serif; color: rgb(0, 0, 0);&quot;&gt;For each interval, check if its start time is greater than or equal to the end time of the last selected interval. If it is, add it to the solution.&amp;nbsp;Maintain a count of the selected intervals.&amp;nbsp;The count represents the maximum number of non-overlapping intervals.&lt;/p&gt;</description>
<category>Greedy Algorithms</category>
<guid isPermaLink="true">https://notexponential.com/503/interval-scheduling?show=944#a944</guid>
<pubDate>Thu, 14 Dec 2023 04:29:36 +0000</pubDate>
</item>
<item>
<title>Answered: Maximum Spanning Tree</title>
<link>https://notexponential.com/504/maximum-spanning-tree?show=943#a943</link>
<description>&lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in; font-size: 12pt; font-family: Calibri, sans-serif; color: rgb(0, 0, 0);&quot;&gt;Sort all the edges in descending order based on their weights.&lt;/p&gt;&lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in; font-size: 12pt; font-family: Calibri, sans-serif; color: rgb(0, 0, 0);&quot;&gt;Start with an empty graph, which will become the Maximum Spanning Tree.&lt;/p&gt;&lt;p class=&quot;MsoNormal&quot; style=&quot;margin: 0in; font-size: 12pt; font-family: Calibri, sans-serif; color: rgb(0, 0, 0);&quot;&gt;Iterate through the sorted edges.&amp;nbsp;For each edge, if adding it to the current Maximum Spanning Tree doesn&#039;t create a cycle, add it to the tree. Continue this process until the Maximum Spanning Tree has (n-1) edges, where n is the number of vertices.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;</description>
<category>Greedy Algorithms</category>
<guid isPermaLink="true">https://notexponential.com/504/maximum-spanning-tree?show=943#a943</guid>
<pubDate>Thu, 14 Dec 2023 04:16:19 +0000</pubDate>
</item>
<item>
<title>Answered: Symbol Frequency</title>
<link>https://notexponential.com/507/symbol-frequency?show=942#a942</link>
<description>Huffman tree is what you describe.</description>
<category>Greedy Algorithms</category>
<guid isPermaLink="true">https://notexponential.com/507/symbol-frequency?show=942#a942</guid>
<pubDate>Thu, 14 Dec 2023 04:08:34 +0000</pubDate>
</item>
<item>
<title>Answered: Chromatic Number, Relation to Max Degree</title>
<link>https://notexponential.com/488/chromatic-number-relation-to-max-degree?show=894#a894</link>
<description>Let the vertices of a graph G be v 1 , v 2 , ..., v n and the maximum degree of G be ∆ .&lt;br /&gt;
For i = 1, 2, ..., n we color v i with the smallest number that is available, i.e., the number that has not been used on the vertices in {v 1 , v 2 , ..., v i−1 } that are adjacent to v i . &lt;br /&gt;
Since there are at most ∆ vertices that are adjacent to v i , this algorithm will not use more than ∆ + 1 colors. Therefore, we have χ(G) ≤ ∆(G)+1 .</description>
<category>Greedy Algorithms</category>
<guid isPermaLink="true">https://notexponential.com/488/chromatic-number-relation-to-max-degree?show=894#a894</guid>
<pubDate>Sun, 10 Dec 2023 22:21:33 +0000</pubDate>
</item>
<item>
<title>Answered: Maximize Party Planning Reward</title>
<link>https://notexponential.com/506/maximize-party-planning-reward?show=723#a723</link>
<description>&lt;p&gt;&lt;span style=&quot;font-family:Arial,Helvetica,sans-serif&quot;&gt;Suppose parties&amp;nbsp;are given by their start and end times as: [[s1, e1], [s2, e2], .... [sn, en]]&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family:Arial,Helvetica,sans-serif&quot;&gt;We can see there is an optimal schedule with non-overlapping party&amp;nbsp;time.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family:Arial,Helvetica,sans-serif&quot;&gt;Proof :&amp;nbsp; Assume that there is no optimal schedule with non-overlapping that includes the first ending party.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family:Arial,Helvetica,sans-serif&quot;&gt;&amp;nbsp;suppose an optimal schedule is given as S. We construct a schedule S’ as follows:&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family:Arial,Helvetica,sans-serif&quot;&gt;&amp;nbsp;remove the very first ending party&amp;nbsp;in S and add the first ending party&amp;nbsp;overall, which consists of interval [s1, e1].&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family:Arial,Helvetica,sans-serif&quot;&gt;We observe that S’ is a feasible schedule. Further S’ schedules as many parties as the given optimal schedule S. Further S’ includes the first ending one. This contradicts the assumption that there is no optimal schedule with non-overlapping parties that includes the first ending party.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family:Arial,Helvetica,sans-serif&quot;&gt;&amp;nbsp;we can simply sort them by their end time and iterate the parties&amp;nbsp;as following steps:&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family:Arial,Helvetica,sans-serif&quot;&gt;step1. Sort the parties by increasing end times using&amp;nbsp;O(n log n) time&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family:Arial,Helvetica,sans-serif&quot;&gt;step2. Initialize ending time of selected party in&amp;nbsp;O(1)&amp;nbsp;time&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family:Arial,Helvetica,sans-serif&quot;&gt;step3. For j = 1 to n&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family:Arial,Helvetica,sans-serif&quot;&gt;If j-th &amp;nbsp;starts after the ending time,&amp;nbsp;then: Select j​-th &amp;nbsp;and update ending time, else&amp;nbsp;skip which&amp;nbsp;takes O(n)&amp;nbsp;time&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family:Arial,Helvetica,sans-serif&quot;&gt;Thus, the overall algorithm runs in O(n log n) time.&lt;/span&gt;&lt;/p&gt;</description>
<category>Greedy Algorithms</category>
<guid isPermaLink="true">https://notexponential.com/506/maximize-party-planning-reward?show=723#a723</guid>
<pubDate>Tue, 23 Jun 2020 21:17:03 +0000</pubDate>
</item>
<item>
<title>Answered: Show that Greedy Algorithm is suboptimal for Gold Rod Question</title>
<link>https://notexponential.com/681/show-that-greedy-algorithm-suboptimal-for-gold-rod-question?show=682#a682</link>
<description>&lt;p style=&quot;margin-top: 0px; font-family: Helvetica, Arial, sans-serif; font-size: 14px;&quot;&gt;When using the greedy method, you will get an approximate answer, which is not always right. For example, due to the length of the wire are given, it is very possible you can&#039;t use all wire and has some wire left. In that case, what&#039;s has to be left probably be larger than those pieces you choose before.&lt;/p&gt;&lt;p style=&quot;margin-top: 0px; font-family: Helvetica, Arial, sans-serif; font-size: 14px;&quot;&gt;Here is a simple example: if the wire length is 50.&lt;/p&gt;&lt;p style=&quot;margin-top: 0px; font-family: Helvetica, Arial, sans-serif; font-size: 14px;&quot;&gt;p1.length = 10, value =60;&lt;/p&gt;&lt;p style=&quot;margin-top: 0px; font-family: Helvetica, Arial, sans-serif; font-size: 14px;&quot;&gt;p2.length = 20, value =100;&lt;/p&gt;&lt;p style=&quot;margin-top: 0px; font-family: Helvetica, Arial, sans-serif; font-size: 14px;&quot;&gt;p3.length = 30, value =120;&lt;/p&gt;&lt;p style=&quot;margin-top: 0px; font-family: Helvetica, Arial, sans-serif; font-size: 14px;&quot;&gt;Using greedy method, you will get p1+p2, value = 160.&lt;/p&gt;&lt;p style=&quot;margin-top: 0px; font-family: Helvetica, Arial, sans-serif; font-size: 14px;&quot;&gt;However, the right solution is p2+p3, value = 220.&lt;/p&gt;</description>
<category>Greedy Algorithms</category>
<guid isPermaLink="true">https://notexponential.com/681/show-that-greedy-algorithm-suboptimal-for-gold-rod-question?show=682#a682</guid>
<pubDate>Sat, 04 May 2019 17:36:56 +0000</pubDate>
</item>
<item>
<title>Answered: &quot;Fast Response&quot; Locating a Proxy Server for the network</title>
<link>https://notexponential.com/468/fast-response-locating-a-proxy-server-for-the-network?show=469#a469</link>
<description>&lt;p&gt;Greedy algorithm works fine.&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Iterate the weight array from left to right and find out the TOTAL weight.&amp;nbsp; // This step takes O(n) time&lt;/li&gt;&lt;li&gt;Iterate the weight array again from right to left and find the point at which the accumulated weight meets or exceeds half of total weight.&amp;nbsp; Put the proxy server on that location.&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Examples:&lt;/p&gt;&lt;p&gt;Given [1, 1, 1, 1], put the proxy server on the 2nd server&lt;/p&gt;&lt;p&gt;Given [1,2,3,4], put the proxy server on the 3rd server.&lt;/p&gt;&lt;p&gt;Given [1,2,3,10], put the proxy server on the 4th server.&lt;/p&gt;&lt;p&gt;Given [10,1,1,1,1,1,1], put the proxy server on the 1st server.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;</description>
<category>Greedy Algorithms</category>
<guid isPermaLink="true">https://notexponential.com/468/fast-response-locating-a-proxy-server-for-the-network?show=469#a469</guid>
<pubDate>Wed, 12 Jul 2017 20:56:27 +0000</pubDate>
</item>
<item>
<title>Answered: Adjustment to Greedy Knapsack Algorithm to Guarantee Atleast 50% value of Optimal</title>
<link>https://notexponential.com/169/adjustment-knapsack-algorithm-guarantee-atleast-optimal?show=175#a175</link>
<description>&lt;p&gt;&lt;strong style=&quot;line-height:1.6&quot;&gt;Solution:&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Add a 3rd step to the original algorithm states: &lt;strong&gt;Return MAX {the Greedy Algorithms&#039;s solution, the most valuable item}.&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Claim:&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;The new 3-steps algorithm guarantees at least 50% value&amp;nbsp;of the optimal solution.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Recall items have been sorted by its profit(value/weight).&amp;nbsp;Assume the 2nd step of this solution returns a result includes the first k&amp;nbsp;items (a&lt;sub&gt;1&lt;/sub&gt;,a&lt;sub&gt;2&lt;/sub&gt;...a&lt;sub&gt;k&lt;/sub&gt;), since items cannot be divided, a&lt;sub&gt;k &lt;/sub&gt;is the last item included, and a&lt;sub&gt;k+1&lt;/sub&gt;&amp;nbsp;is the first item excluded. Therefore, the result of this step will be at least equal to the total value of the first k items. That&#039;s RESULT1 =&amp;nbsp;a&lt;sub&gt;1 +&amp;nbsp;&lt;/sub&gt;a&lt;sub&gt;2 +&amp;nbsp;&lt;/sub&gt;...+ a&lt;sub&gt;k.&lt;/sub&gt;&lt;/li&gt;&lt;li&gt;Consider an alternative - a&lt;sub&gt;max&lt;/sub&gt;, where a&lt;sub&gt;max&lt;/sub&gt; is the most valuable item. &amp;nbsp;Therefore,&amp;nbsp;RESULT2 =&amp;nbsp;a&lt;sub&gt;max&lt;/sub&gt; &amp;gt;= a&lt;sub&gt;k+1&lt;/sub&gt;.&lt;/li&gt;&lt;li&gt;We choose RESULT = max{RESULT1, RESULT2}&lt;/li&gt;&lt;li&gt;Here comes the cool part.&amp;nbsp;Plus the two in-equations, We have (RESULT1 + RESULT2) =&amp;nbsp;a&lt;sub&gt;1 +&amp;nbsp;&lt;/sub&gt;a&lt;sub&gt;2 +&amp;nbsp;&lt;/sub&gt;...+ a&lt;sub&gt;k &lt;/sub&gt;+&lt;sub&gt;&amp;nbsp;&lt;/sub&gt;a&lt;sub&gt;k+1&lt;/sub&gt;, which is the total value of the first k+1 items, which is also &amp;gt;= the fractional solution we talked about earlier (because the fractional solution takes at most the (k+1)-th item), and also the fractional solution will be at least equal to&amp;nbsp;the optimal solution of this problem because the fractional solution consume all knapsack&#039;s capacity and choose items by its profit in decending order.&lt;/li&gt;&lt;li&gt;So we have: RESULT &amp;gt;= RESULT1, and RESULT &amp;gt;= RESULT2. &amp;nbsp;That is, we have,&amp;nbsp;2 * RESULT&amp;nbsp;&amp;nbsp;&amp;gt;= &amp;nbsp;RESULT1 + RESULT2 = Value(1,k+1) &amp;gt;= the fractional solution &amp;gt;= the optimal solution. That&#039;s 2 * RESULT &amp;gt;= Optimal solution. &amp;nbsp;That is, RESULT &amp;gt;= 0.5 * Optimal Solution.&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;I wrote an article contains more details about this problem:&amp;nbsp;&lt;a target=&quot;_blank&quot; rel=&quot;nofollow&quot; href=&quot;http://blog.scriptofzion.com/post/category/common-algorithms/-greedy-knapsack-problem#top&quot;&gt;Knapsack Problem&lt;/a&gt;&lt;/p&gt;</description>
<category>Greedy Algorithms</category>
<guid isPermaLink="true">https://notexponential.com/169/adjustment-knapsack-algorithm-guarantee-atleast-optimal?show=175#a175</guid>
<pubDate>Sun, 06 Mar 2016 03:22:51 +0000</pubDate>
</item>
</channel>
</rss>