<?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 Bhavek Mahyavanshi on Medium]]></title>
        <description><![CDATA[Stories by Bhavek Mahyavanshi on Medium]]></description>
        <link>https://medium.com/@bhavek.mahyavanshi50?source=rss-8a189b3e01d4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dLvQ4qyhLGvBHLRlYbGL3g.jpeg</url>
            <title>Stories by Bhavek Mahyavanshi on Medium</title>
            <link>https://medium.com/@bhavek.mahyavanshi50?source=rss-8a189b3e01d4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 11:21:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bhavek.mahyavanshi50/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[Johnson’s algorithm for All-pairs shortest paths]]></title>
            <link>https://medium.com/@bhavek.mahyavanshi50/johnsons-algorithm-for-all-pairs-shortest-paths-78337a76ef78?source=rss-8a189b3e01d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/78337a76ef78</guid>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Bhavek Mahyavanshi]]></dc:creator>
            <pubDate>Sun, 08 Sep 2019 12:52:46 GMT</pubDate>
            <atom:updated>2019-09-08T12:52:46.620Z</atom:updated>
            <content:encoded><![CDATA[<p>The problem is to find shortest paths between every pair of vertices in a given weighted directed Graph and weights may be negative. Time complexity of Floyd Warshall Algorithm is Θ(V3). <em>Using Johnson’s algorithm, we can find all pair shortest paths in O(V2log V + VE) time.</em> Johnson’s algorithm uses both <a href="https://www.geeksforgeeks.org/greedy-algorithms-set-6-dijkstras-shortest-path-algorithm/">Dijkstra</a> and <a href="https://www.geeksforgeeks.org/dynamic-programming-set-23-bellman-ford-algorithm/">Bellman-Ford</a> as subroutines.</p><p>If we apply <a href="https://www.geeksforgeeks.org/greedy-algorithms-set-6-dijkstras-shortest-path-algorithm/">Dijkstra’s Single Source shortest path algorithm</a> for every vertex, considering every vertex as source, we can find all pair shortest paths in O(V*VLogV) time. So using Dijkstra’s single source shortest path seems to be a better option than <a href="https://www.geeksforgeeks.org/dynamic-programming-set-16-floyd-warshall-algorithm/">Floyd Warshell</a>, but the problem with Dijkstra’s algorithm is, it doesn’t work for negative weight edge.<br> The idea of Johnson’s algorithm is to re-weight all edges and make them all positive, then apply Dijkstra’s algorithm for every vertex.</p><p><strong>How to transform a given graph to a graph with all non-negative weight edges?</strong></p><p>One may think of a simple approach of finding the minimum weight edge and adding this weight to all edges. Unfortunately, this doesn’t work as there may be different number of edges in different paths (See <a href="http://geeksquiz.com/data-structures-graph-question-31/">this</a> for an example). If there are multiple paths from a vertex u to v, then all paths must be increased by same amount, so that the shortest path remains the shortest in the transformed graph.<br> The idea of Johnson’s algorithm is to assign a weight to every vertex. Let the weight assigned to vertex u be h[u]. We reweight edges using vertex weights. For example, for an edge (u, v) of weight w(u, v), the new weight becomes w(u, v) + h[u] — h[v]. The great thing about this reweighting is, all set of paths between any two vertices are increased by same amount and all negative weights become non-negative. Consider any path between two vertices s and t, weight of every path is increased by h[s] — h[t], all h[] values of vertices on path from s to t cancel each other.</p><p>How do we calculate h[] values? <a href="https://www.geeksforgeeks.org/dynamic-programming-set-23-bellman-ford-algorithm/">Bellman-Ford algorithm</a> is used for this purpose. Following is the complete algorithm. A new vertex is added to the graph and connected to all existing vertices. The shortest distance values from new vertex to all existing vertices are h[] values.</p><p><strong>Algorithm:</strong><br> <strong>1)</strong> Let the given graph be G. Add a new vertex s to the graph, add edges from new vertex to all vertices of G. Let the modified graph be G’</p><p><strong>2)</strong> Run <a href="https://www.geeksforgeeks.org/dynamic-programming-set-23-bellman-ford-algorithm/">Bellman-Ford algorithm</a> on G’ with s as source. Let the distances calculated by Bellman-Ford be h[0], h[1], .. h[V-1]. If we find a negative weight cycle, then return. Note that the negative weight cycle cannot be created by new vertex s as there is no edge to s. All edges are from s.</p><p><strong>3)</strong> Reweight the edges of original graph. For each edge (u, v), assign the new weight as “original weight + h[u] — h[v]”.</p><p><strong>4)</strong> Remove the added vertex s and run <a href="https://www.geeksforgeeks.org/greedy-algorithms-set-6-dijkstras-shortest-path-algorithm/">Dijkstra’s algorithm</a> for every vertex.</p><p><strong>How does the transformation ensure nonnegative weight edges:</strong></p><p>The following property is always true about h[] values as they are shortest distances.</p><p>h[v] &lt;= h[u] + w(u, v)</p><p>The property simply means, shortest distance from s to v must be smaller than or equal to shortest distance from s to u plus weight of edge (u, v). The new weights are w(u, v) + h[u] — h[v]. The value of the new weights must be greater than or equal to zero because of the inequality “h[v] &lt;= h[u] + w(u, v)”. <strong>Example:</strong><br> Let us consider the following graph.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*UlGZTf9Ocngkfk188hLf-g.png" /></figure><p>We add a source s and add edges from s to all vertices of the original graph. In the following diagram s is 4.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*j50WNwaVuSWoiq2UW1WV3g.png" /></figure><p>We calculate the shortest distances from 4 to all other vertices using Bellman-Ford algorithm. The shortest distances from 4 to 0, 1, 2 and 3 are 0, -5, -1 and 0 respectively, i.e., h[] = {0, -5, -1, 0}. Once we get these distances, we remove the source vertex 4 and reweight the edges using following formula. w(u, v) = w(u, v) + h[u] — h[v].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*KqVcPlAEuhF0MpHiSyiA9g.png" /></figure><p>Since all weights are positive now, we can run Dijkstra’s shortest path algorithm for every vertex as source.</p><p><strong>Time Complexity: </strong>The main steps in algorithm are Bellman Ford Algorithm called once and Dijkstra called V times. Time complexity of Bellman Ford is O(VE) and time complexity of Dijkstra is O(VLogV). So overall time complexity is O(V2log V + VE).<br> The time complexity of Johnson’s algorithm becomes same as <a href="https://www.geeksforgeeks.org/dynamic-programming-set-16-floyd-warshall-algorithm/">Floyd Warshell </a>when the graphs is complete (For a complete graph E = O(V2). But for sparse graphs, the algorithm performs much better than <a href="https://www.geeksforgeeks.org/dynamic-programming-set-16-floyd-warshall-algorithm/">Floyd Warshell</a>.</p><p><strong>References:</strong></p><p><a href="https://www.geeksforgeeks.org/johnsons-algorithm/">https://www.geeksforgeeks.org/johnsons-algorithm/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=78337a76ef78" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Hill Climbing | Artificial Intelligence]]></title>
            <link>https://medium.com/@bhavek.mahyavanshi50/introduction-to-hill-climbing-artificial-intelligence-a3714ed2d8d8?source=rss-8a189b3e01d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/a3714ed2d8d8</guid>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Bhavek Mahyavanshi]]></dc:creator>
            <pubDate>Sun, 08 Sep 2019 11:57:06 GMT</pubDate>
            <atom:updated>2019-09-08T11:57:06.595Z</atom:updated>
            <content:encoded><![CDATA[<p>Hill Climbing is a heuristic search used for mathematical optimization problems in the field of Artificial Intelligence.<br> Given a large set of inputs and a good heuristic function, it tries to find a sufficiently good solution to the problem. This solution may not be the global optimal maximum.</p><p>· In the above definition, <strong>mathematical optimization problems</strong> implies that hill-climbing solves the problems where we need to maximize or minimize a given real function by choosing values from the given inputs. Example-<a href="https://www.geeksforgeeks.org/travelling-salesman-problem-set-1/">Travelling salesman problem</a> where we need to minimize the distance traveled by the salesman.</p><p>· ‘Heuristic search’ means that this search algorithm may not find the optimal solution to the problem. However, it will give a good solution in <strong>reasonable time.</strong></p><p>· A <strong>heuristic function</strong> is a function that will rank all the possible alternatives at any branching step in search algorithm based on the available information. It helps the algorithm to select the best route out of possible routes.</p><p><strong>Features of Hill Climbing</strong></p><p>1. <strong>Variant of generate and test algorithm :</strong> It is a variant of generate and test algorithm. The generate and test algorithm is as follows :</p><p>1. Generate possible solutions.<br> 2. Test to see if this is the expected solution.<br> 3. If the solution has been found quit else go to step 1<em>.</em></p><p>Hence we call Hill climbing as a variant of generate and test algorithm as it takes the feedback from the test procedure. Then this feedback is utilized by the generator in deciding the next move in search space.</p><p>2. <strong>Uses the </strong><a href="https://www.geeksforgeeks.org/greedy-algorithms-set-1-activity-selection-problem/"><strong>Greedy approach</strong></a><strong> :</strong> At any point in state space, the search moves in that direction only which optimizes the cost of function with the hope of finding the optimal solution at the end.</p><p><strong>Types of Hill Climbing</strong></p><p><strong>1.</strong> <strong>Simple Hill Climbing:</strong></p><p>Simple hill climbing is the simplest way to implement a hill climbing algorithm. It only evaluates the neighbor node state at a time and selects the first one which optimizes current cost and set it as a current state. It only checks it’s one successor state, and if it finds better than the current state, then move else be in the same state. This algorithm has the following features:</p><ul><li>Less time consuming</li><li>Less optimal solution and the solution is not guaranteed</li></ul><p>Algorithm for Simple Hill Climbing:</p><ul><li><strong>Step 1:</strong> Evaluate the initial state, if it is goal state then return success and Stop.</li><li><strong>Step 2:</strong> Loop Until a solution is found or there is no new operator left to apply.</li><li><strong>Step 3:</strong> Select and apply an operator to the current state.</li><li><strong>Step 4:</strong> Check new state:</li></ul><p>a. If it is goal state, then return success and quit.</p><p>b. Else if it is better than the current state then assign new state as a current state.</p><p>c. Else if not better than the current state, then return to step2.</p><ul><li><strong>Step 5:</strong> Exit.</li></ul><p><strong>2. Steepest-Ascent hill climbing:</strong></p><p>The steepest-Ascent algorithm is a variation of simple hill climbing algorithm. This algorithm examines all the neighboring nodes of the current state and selects one neighbor node which is closest to the goal state. This algorithm consumes more time as it searches for multiple neighbors</p><p>Algorithm for Steepest-Ascent hill climbing:</p><ul><li><strong>Step 1:</strong> Evaluate the initial state, if it is goal state then return success and stop, else make current state as initial state.</li><li><strong>Step 2:</strong> Loop until a solution is found or the current state does not change.</li></ul><p>a. Let SUCC be a state such that any successor of the current state will be better than it.</p><p>b. For each operator that applies to the current state:</p><p>a. Apply the new operator and generate a new state.</p><p>b. Evaluate the new state.</p><p>c. If it is goal state, then return it and quit, else compare it to the SUCC.</p><p>d. If it is better than SUCC, then set new state as SUCC.</p><p>e. If the SUCC is better than the current state, then set current state to SUCC.</p><ul><li><strong>Step 5:</strong> Exit.</li></ul><p><strong>3. Stochastic hill climbing:</strong></p><p>Stochastic hill climbing does not examine for all its neighbor before moving. Rather, this search algorithm selects one neighbor node at random and decides whether to choose it as a current state or examine another state.</p><p><strong>State Space diagram for Hill Climbing</strong></p><p>State space diagram is a graphical representation of the set of states our search algorithm can reach vs the value of our objective function(the function which we wish to maximize).<br> <strong>X-axis :</strong> denotes the state space ie states or configuration our algorithm may reach.<br> <strong>Y-axis :</strong> denotes the values of objective function corresponding to a particular state.<br> The best solution will be that state space where objective function has maximum value(global maximum).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/672/1*ymMUv1hzqBYPL-fErH7vIQ.png" /></figure><p><strong>Different regions in the State Space Diagram</strong></p><p>1. <strong>Local maximum: </strong>It is a state which is better than its neighboring state however there exists a state which is better than it(global maximum). This state is better because here the value of the objective function is higher than its neighbors.</p><p>2. <strong>Global maximum : </strong>It is the best possible state in the state space diagram. This because at this state, objective function has highest value.</p><p>3. <strong>Plateau/flat local maximum : </strong>It is a flat region of state space where neighboring states have the same value.</p><p>4. <strong>Ridge : </strong>It is region which is higher than its neighbor’s but itself has a slope. It is a special kind of local maximum.</p><p>5. <strong>Current state : </strong>The region of state space diagram where we are currently present during the search.</p><p>6. <strong>Shoulder : </strong>It is a plateau that has an uphill edge.</p><p><strong>Problems in different regions in Hill climbing</strong></p><p>Hill climbing cannot reach the optimal/best state(global maximum) if it enters any of the following regions :</p><p>1. <strong>Local maximum : </strong>At a local maximum all neighboring states have a values which is worse than the current state. Since hill-climbing uses a greedy approach, it will not move to the worse state and terminate itself. The process will end even though a better solution may exist.<br> <strong>To overcome local maximum problem : </strong>Utilize <a href="https://www.geeksforgeeks.org/backtracking-set-1-the-knights-tour-problem/">backtracking technique</a>. Maintain a list of visited states. If the search reaches an undesirable state, it can backtrack to the previous configuration and explore a new path.</p><p>2. <strong>Plateau : </strong>On plateau all neighbors have same value . Hence, it is not possible to select the best direction.</p><p><strong>To overcome plateaus :</strong> Make a big jump. Randomly select a state far away from the current state. Chances are that we will land at a non-plateau region</p><p>3. <strong>Ridge : </strong>Any point on a ridge can look like peak because movement in all possible directions is downward. Hence the algorithm stops when it reaches this state.<br> <strong>To overcome Ridge :</strong> In this kind of obstacle, use two or more rules before testing. It implies moving in several directions at once.</p><h3>Simulated Annealing:</h3><p>A hill-climbing algorithm which never makes a move towards a lower value guaranteed to be incomplete because it can get stuck on a local maximum. And if algorithm applies a random walk, by moving a successor, then it may complete but not efficient. <strong>Simulated Annealing</strong> is an algorithm which yields both efficiency and completeness.</p><p>In mechanical term <strong>Annealing</strong> is a process of hardening a metal or glass to a high temperature then cooling gradually, so this allows the metal to reach a low-energy crystalline state. The same process is used in simulated annealing in which the algorithm picks a random move, instead of picking the best move. If the random move improves the state, then it follows the same path. Otherwise, the algorithm follows the path which has a probability of less than 1 or it moves downhill and chooses another path.</p><p><strong>References:</strong></p><p><a href="https://www.javatpoint.com/hill-climbing-algorithm-in-ai">https://www.javatpoint.com/hill-climbing-algorithm-in-ai</a></p><p><a href="https://www.geeksforgeeks.org/introduction-hill-climbing-artificial-intelligence/&#39;/">https://www.geeksforgeeks.org/introduction-hill-climbing-artificial-intelligence/&#39;/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a3714ed2d8d8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>