<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bhavika Dhanwani on Medium]]></title>
        <description><![CDATA[Stories by Bhavika Dhanwani on Medium]]></description>
        <link>https://medium.com/@dhanwanibhavika12?source=rss-280f7e340328------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*-f4K5k3ktwMlNu46BmW9zQ.jpeg</url>
            <title>Stories by Bhavika Dhanwani on Medium</title>
            <link>https://medium.com/@dhanwanibhavika12?source=rss-280f7e340328------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 11:54:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dhanwanibhavika12/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Backtracking Approach]]></title>
            <link>https://medium.com/@dhanwanibhavika12/backtracking-approach-a4abbbfef398?source=rss-280f7e340328------2</link>
            <guid isPermaLink="false">https://medium.com/p/a4abbbfef398</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[backtracking]]></category>
            <dc:creator><![CDATA[Bhavika Dhanwani]]></dc:creator>
            <pubDate>Fri, 23 Aug 2019 04:27:31 GMT</pubDate>
            <atom:updated>2019-08-23T04:30:46.608Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*ol2JbaG5yMI8nKyVh3yX8g.jpeg" /></figure><p>Can you hear this picture ??</p><p>Yes,you heard it right this image conveys the meaning of ‘Backtrack’.</p><h4>“What is backtracking in terms of algorithm?”</h4><p>Backtracking is a standard problem solving technique based on <a href="https://medium.freecodecamp.org/recursion-recursion-recursion-4db8890a674d">recursion</a>.</p><p>A backtracking algorithm tries to build a solution to a computational problem incrementally. Whenever the algorithm needs to decide between multiple alternatives to the next component of the solution, it simply tries all possible options recursively.</p><p>There are three types of problems in backtracking –</p><ol><li>Decision Problem — In this, we search for a feasible solution.</li><li>Optimization Problem — In this, we search for the best solution.</li><li>Enumeration Problem — In this, we find all feasible solutions.</li></ol><p><strong>How to determine if a problem can be solved using Backtracking?</strong></p><p>Generally, every <a href="https://en.wikipedia.org/wiki/Constraint_satisfaction_problem">constraint satisfaction problem</a> which has clear and well-defined constraints on any objective solution, that incrementally builds candidate to the solution and abandons a candidate (“backtracks”) as soon as it determines that the candidate cannot possibly be completed to a valid solution, can be solved by Backtracking.</p><h4>For example:-</h4><p>Consider a situation that you have three boxes in front of you and only one of them has a gold coin in it but you do not know which one. So, in order to get the coin, you will have to open all of the boxes one by one. You will first check the first box, if it does not contain the coin, you will have to close it and check the second box and so on until you find the coin. This is what backtracking is, that is solving all sub-problems one by one in order to reach the best possible solution.</p><h3>The Famous N-Queens Problem</h3><p>Placing queen on a chess board is a classical problem in mathematics and computer science.</p><p>The <a href="https://en.wikipedia.org/wiki/Eight_queens_puzzle">Queen’s Puzzle</a> (aka the eight queens puzzle), was originally published in 1848. It involves placing eight queens on an 8x8 chess board, in such a manner that no two queens can attack each other.The queen happens to be the most powerful piece on the chess board, primarily because of the freedom of movement that it has.</p><p>The queen can move in 8 different directions, as illustrated in the image below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/417/1*iWOU6yBR7zcBFpay-pqO-Q.jpeg" /></figure><p><strong>Backtracking Concept</strong>: The idea is to place queens one by one in different columns, starting from the leftmost column.</p><p>When we place a queen in a column, we check for clashes with already placed queens. In the current column, if we find a row for which there is no clash, we mark this row and column as part of the solution.</p><p>If we do not find such a row due to clashes then we backtrack and return false.</p><pre>1)Start in the leftmost column<br>2) If all queens are placed<br>    return true<br>3) Try all rows in the current column. <br>   Do following for every tried row.<br>    a) If the queen can be placed safely in this row <br>       then mark this [row, column] as part of the <br>       solution and recursively check if placing<br>       queen here leads to a solution.<br>    b) If placing the queen in [row, column] leads to<br>       a solution then return true.<br>    c) If placing queen doesn&#39;t lead to a solution then<br>       unmark this [row, column] (Backtrack) and go to <br>       step (a) to try other rows.<br>3) If all rows have been tried and nothing worked,<br>   return false to trigger backtracking.</pre><p>Other examples where backtracking can be used to solve puzzles or problems include:</p><ul><li>Puzzles such as <a href="https://en.wikipedia.org/wiki/Crosswords">crosswords</a>, <a href="https://en.wikipedia.org/wiki/Verbal_arithmetic">verbal arithmetic</a>, <a href="https://en.wikipedia.org/wiki/Algorithmics_of_sudoku">Sudoku</a><a href="https://en.wikipedia.org/wiki/Backtracking#cite_note-5">[nb 1]</a>, and <a href="https://en.wikipedia.org/wiki/Peg_solitaire">Peg Solitaire</a>.</li><li><a href="https://en.wikipedia.org/wiki/Combinatorial_optimization">Combinatorial optimization</a> problems such as <a href="https://en.wikipedia.org/wiki/Parsing">parsing</a> and the <a href="https://en.wikipedia.org/wiki/Knapsack_problem">knapsack problem</a>.</li><li><a href="https://en.wikipedia.org/wiki/Logic_programming">Logic programming</a> languages such as <a href="https://en.wikipedia.org/wiki/Icon_programming_language">Icon</a>, <a href="https://en.wikipedia.org/wiki/Planner_programming_language">Planner</a> and <a href="https://en.wikipedia.org/wiki/Prolog">Prolog</a>, which use backtracking internally to generate answers.</li></ul><p>As certain there are two sides of coin so this algorithm has limitations beyond advantages.</p><h4>Advantages:</h4><p><em>1) Simple to implement.<br>2) State changes are stored in stack, meaning we do not need to concern ourselves about them.<br>3) Intuitive approach of trial and error.<br>4) Code size is usually small.</em></p><h4>Disadvantages:</h4><ol><li><em>Multiple function calls are expensive.<br>2) Inefficient when there is lots of branching from one state.<br>3) Requires large amount of space as the each function state needs to be stored on system stack.</em></li></ol><p>Reference: <a href="https://www.freecodecamp.org/news/lets-backtrack-and-save-some-queens-1f9ef6af5415/">https://www.freecodecamp.org/news/lets-backtrack-and-save-some-queens-1f9ef6af5415/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a4abbbfef398" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[I chose to write on greedy approach because]]></title>
            <link>https://medium.com/@dhanwanibhavika12/i-chose-to-write-on-greedy-approach-because-485690654596?source=rss-280f7e340328------2</link>
            <guid isPermaLink="false">https://medium.com/p/485690654596</guid>
            <category><![CDATA[greedy-algorithms]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Bhavika Dhanwani]]></dc:creator>
            <pubDate>Thu, 22 Aug 2019 10:02:28 GMT</pubDate>
            <atom:updated>2019-08-22T11:31:06.702Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*UTqn8pt1jqJtpykRbJ5BMQ.jpeg" /></figure><h3>Greedy Approach</h3><p>As the name implies,this algorithm selects the best possile solution among all the solutions available.The best of all possible solutions is called the optimal solution.</p><p>The Greedy approach constructs the solution through a sequence of steps. Each step is chosen such that it is the best alternative among all feasible choices that are available. The choice of a step once made cannot be changed in subsequent steps.</p><p>The choice of each step in a greedy approach is done based in the following:</p><p>· It must be feasible</p><p>· It must be locally optimal</p><p>· It must be unalterable</p><h4><em>Where do we use ‘greedy approach’?</em></h4><p>-Knapsack problem</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/486/1*TR0o3RwtUpjXo4fT6gMsZA.png" /></figure><p>Let there are n number of objects and each object is having a weight and contribution to profit. The knapsack of capacity M is given. The objective is to fill the knapsack in such a way that profit shall be maximum.</p><p><em>Consider the following example on how to use greedy approach for knapsack problem</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wpy5-pdB-ghGUOFM5xQU3g.jpeg" /></figure><p>Greedy strategy I: In this case, the items are arranged by their profit values. Here the item with maximum profit is selected first. If the weight of the object is less than the remaining capacity of the knapsack then the object is selected full and the profit associated with the object is added to the total profit. Otherwise, a fraction of the object is selected so that the knapsack can be filled exactly. This process continues from selecting the highest profitable object to the lowest profitable object till the knapsack is exactly full.</p><p>Greedy strategy II: In this case, the items are arranged by fair weights. Here the item with minimum weight in selected first and the process continues like greedy strategy-I till the knapsack is exactly full.</p><p>Greedy strategy III: In this case, the items are arranged by profit/weight ratio and the item with maximum profit/weight ratio is selected first and the process continues like greedy strategy-I till theknapsack is exactly full.</p><p>The third greedy strategy always provides an optimal solution.So a simple code is as follows</p><p>/*here w[i] is for weight of different items and v[i] for value of different items .W stands for knapsack capacity*/</p><p>for(i=0;i&lt;n;i++){ <br> r[i]=(v[i]/w[i]);}//calculating value/weight ratio(r=w/v)<br> sort1(w,v,r,n);//sort according to r in descending order<br> for(i=0;i&lt;n;i++){<br> if(w[i]&lt;W)<br> {<br> W=W-w[i]; <br> p=p+v[i]; //adding profit or value of item<br> }}</p><h4>Applications:</h4><p>The greedy method is quite powerful and works well for a wide range of problems. Many algorithms can be viewed as applications of the Greedy algorithms, such as (includes but is not limited to):</p><ol><li>Minimum Spanning Tree</li><li>Dijkstra’s algorithm for shortest path</li><li>Huffman codes (data-compression codes)</li></ol><p><em>Next post coming up for other problems solved using Greedy Approach.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=485690654596" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>