<?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 shruti6 on Medium]]></title>
        <description><![CDATA[Stories by shruti6 on Medium]]></description>
        <link>https://medium.com/@shrutitambe06?source=rss-60cac85ea35b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*a4eTmo09c4lsUdEVGCf19g.jpeg</url>
            <title>Stories by shruti6 on Medium</title>
            <link>https://medium.com/@shrutitambe06?source=rss-60cac85ea35b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 04:28:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shrutitambe06/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[Best First Search : step by step approach]]></title>
            <link>https://medium.com/@shrutitambe06/best-first-search-step-by-step-approach-5a3794650f46?source=rss-60cac85ea35b------2</link>
            <guid isPermaLink="false">https://medium.com/p/5a3794650f46</guid>
            <dc:creator><![CDATA[shruti6]]></dc:creator>
            <pubDate>Tue, 25 Apr 2023 03:48:59 GMT</pubDate>
            <atom:updated>2023-04-25T03:48:59.322Z</atom:updated>
            <content:encoded><![CDATA[<h3>Best First Search : step by step approach</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/1*iiCPkKohG-tnrNO5cC7n_A.jpeg" /><figcaption>Graph Traversal</figcaption></figure><p><strong><em>What is Best First Search algorithm?</em></strong></p><p>Best First Search is an informed searching method or a heuristic search algorithm, that is commonly used to solve complex problems with large search spaces. The algorithm is designed to find the optimal solution by exploring the most promising (or ‘BEST’) paths first while traversing in a graph, based on an evaluation function or heuristic function.</p><p><strong><em>How did it come into picture?</em></strong></p><p>Best-First Search Algorithm was developed in the field of Artificial Intelligence (AI). It was developed in response to the limitations of earlier search algorithms, such as Breadth-First Search (BFS) and Depth-First Search (DFS).</p><p>BFS and DFS are systematic uninformed search algorithms that are not designed to handle large search spaces efficiently. BFS blindly explores all nodes at a given depth level before moving on to the next level, while DFS explores a path to the deepest possible node before backtracking and exploring another path.</p><p>Informed search algorithms are more efficient than uninformed in terms of cost, space and performance.</p><p>Researchers developed heuristic search algorithms to address the short comings of uninformed search algorithms. Best-First Search Algorithm that could search more efficiently by using heuristic information to guide the search.</p><p><strong><em>Benefits and Drawbacks:</em></strong></p><p>Best-First Search Algorithm has several benefits and drawbacks. One of its main advantages is its ability to quickly find the optimal solution. It is also flexible in the choice of heuristic function used, which can be tailored to the problem domain to improve accuracy.</p><p>However, the algorithm has limitations such as the potential for getting stuck in local optima and being sensitive to the choice of heuristic function. It also requires a large amount of memory and computational resources, especially in large search spaces, and does not guarantee finding the globally optimal solution in all cases.</p><p>Thus, Best-First Search Algorithm can be effective in many problem domains, but it is important to consider its benefits and drawbacks before applying it to a specific problem.</p><p><strong><em>Applications:</em></strong></p><p>Today, this algorithm is widely used in many problem domains, including robotics, natural language processing, computer vision, game playing, and optimization. Its ability to find optimal solutions efficiently and its adaptability to a wide range of problem domains make Best-First Search Algorithm a powerful tool in these fields.</p><p><strong><em>Here is a Step-by-step explanation of the Algorithm:</em></strong></p><p>The best first search method for searching the graph space employs two lists for traversal tracking. An ‘Open’ list that keeps track of the current ‘immediate’ nodes that are available for traversal and a ‘CLOSED’ list that keeps track of the nodes that have already been traversed.</p><ol><li>Create 2 empty lists: OPEN and CLOSED</li><li>Start from the initial node (say N) and put it in the ‘ordered’ OPEN list</li><li>Repeat the next steps until the GOAL node is reached</li><li>If the OPEN list is empty, then EXIT the loop returning ‘False’</li><li>Select the first/top node (say N) in the OPEN list and move it to the CLOSED list. Also, capture the information of the parent node</li><li>If N is a GOAL node, then move the node to the Closed list and exit the loop returning ‘True’. The solution can be found by backtracking the path</li><li>If N is not the GOAL node, expand node N to generate the ‘immediate’ next nodes linked to node N and add all those to the OPEN list</li><li>Reorder the nodes in the OPEN list in ascending order according to an evaluation function f(n)</li></ol><p><em>Time Complexity: </em>This algorithm will traverse the shortest path first in the queue. The time complexity of the algorithm is given by <strong>O(n*log n).</strong></p><p><strong><em>Variants of Best First Search Algorithm</em></strong></p><p>One popular variant of best-first search is A* search, which balances the estimated cost of reaching the goal with the actual cost of reaching each node in the search space.</p><p>Weighted A* search is a modification of A* search that allows for adjusting the weight given to the heuristic function.</p><p>Greedy best-first search is another variant that only considers the estimated cost to the goal and does not take into account the cost of reaching each node.</p><p>Each of these algorithms has its strengths and weaknesses depending on the problem domain and the quality of the heuristic function.</p><p><strong><em>Pseudo Code:</em></strong></p><pre>function best_first_search(problem, heuristic):<br>    open_list = [problem.initial_state]<br>    while open_list is not empty:<br>        current_node = select_node_with_lowest_heuristic(open_list, heuristic)<br>        if problem.is_goal_state(current_node):<br>            return current_node<br>        successors = problem.get_successors(current_node)<br>        for successor in successors:<br>            open_list.append(successor)<br>        current_node.visited = True<br>        open_list.sort(key=heuristic)<br>    return failure<br><br>function select_node_with_lowest_heuristic(open_list, heuristic):<br>    min_heuristic_value = infinity<br>    min_heuristic_node = None<br>    for node in open_list:<br>        if not node.visited and heuristic(node) &lt; min_heuristic_value:<br>            min_heuristic_value = heuristic(node)<br>            min_heuristic_node = node<br>    return min_heuristic_node</pre><p><strong><em>Python Code:</em></strong></p><pre>from queue import PriorityQueue<br><br>def best_first_search(problem, heuristic):<br>    start_node = problem.initial_state<br>    if problem.is_goal_state(start_node):<br>        return start_node<br>    <br>    open_list = PriorityQueue()<br>    open_list.put((heuristic(start_node), start_node))<br>    visited = set()<br><br>    while not open_list.empty():<br>        current_node = open_list.get()[1]<br>        visited.add(current_node)<br><br>        for successor in problem.get_successors(current_node):<br>            if successor not in visited:<br>                if problem.is_goal_state(successor):<br>                    return successor<br>                open_list.put((heuristic(successor), successor))<br><br>    return None</pre><p>This code uses the PriorityQueue data structure from the queue module to keep track of the open list. The heuristic function is used to prioritize nodes in the open list. The visited set is used to avoid revisiting nodes. The problem object should have the methods initial_state, is_goal_state, get_successors, and any additional methods or attributes required by the heuristic function.</p><p><strong><em>Real world Examples:</em></strong></p><ol><li>Shortest Path Finding: Given a map with cities and their distances, the Best First Search algorithm can be used to find the shortest path between two cities. The heuristic function can be the straight line distance between two cities (i.e., Euclidean distance).</li></ol><pre>from queue import PriorityQueue<br><br>graph = {<br>    &#39;A&#39;: [(&#39;B&#39;, 5), (&#39;C&#39;, 10)],<br>    &#39;B&#39;: [(&#39;D&#39;, 4)],<br>    &#39;C&#39;: [(&#39;E&#39;, 3)],<br>    &#39;D&#39;: [(&#39;F&#39;, 6)],<br>    &#39;E&#39;: [(&#39;F&#39;, 8)],<br>    &#39;F&#39;: []<br>}<br><br>def best_first_search(start, goal, heuristic):<br>    frontier = PriorityQueue()<br>    frontier.put((0, start))<br>    came_from = {}<br>    cost_so_far = {start: 0}<br>    <br>    while not frontier.empty():<br>        current = frontier.get()[1]<br>        <br>        if current == goal:<br>            path = []<br>            while current != start:<br>                path.append(current)<br>                current = came_from[current]<br>            path.append(start)<br>            path.reverse()<br>            return path<br>        <br>        for neighbor, distance in graph[current]:<br>            new_cost = cost_so_far[current] + distance<br>            if neighbor not in cost_so_far or new_cost &lt; cost_so_far[neighbor]:<br>                cost_so_far[neighbor] = new_cost<br>                priority = new_cost + heuristic[neighbor]<br>                frontier.put((priority, neighbor))<br>                came_from[neighbor] = current<br>                <br>    return None<br><br>start = &#39;A&#39;<br>goal = &#39;F&#39;<br>heuristic = {<br>    &#39;A&#39;: 10,<br>    &#39;B&#39;: 8,<br>    &#39;C&#39;: 6,<br>    &#39;D&#39;: 4,<br>    &#39;E&#39;: 2,<br>    &#39;F&#39;: 0<br>}<br><br>path = best_first_search(start, goal, heuristic)<br><br>print(path)</pre><p><strong>Output:</strong></p><pre>Expected output: [&#39;A&#39;, &#39;B&#39;, &#39;D&#39;, &#39;F&#39;]</pre><p>In this example, we define a graph dictionary that represents the cities and distances between them. We also define a heuristic dictionary that provides an estimate of the distance between each city and the goal city, which in this case is the straight-line distance between the cities. Finally, we call the best_first_search function, passing in the start and goal cities and the heuristic dictionary. The output is the shortest path between the two cities [&#39;A&#39;, &#39;B&#39;, &#39;D&#39;, &#39;F&#39;].</p><p>2. Game Playing: The Best First Search algorithm can be used to develop game-playing agents that can make decisions based on heuristic evaluations of game states. For example, in chess, a heuristic function can evaluate a chess board state based on the number and type of pieces for each player and the board position of each piece.</p><pre>class EightPuzzleProblem:<br>    def __init__(self, initial_state):<br>        self.initial_state = initial_state<br><br>    def is_goal_state(self, state):<br>        return state == (1, 2, 3, 4, 5, 6, 7, 8, 0)<br><br>    def get_successors(self, state):<br>        successors = []<br>        empty_index = state.index(0)<br>        if empty_index not in [0, 1, 2]:<br>            new_state = list(state)<br>            new_state[empty_index], new_state[empty_index - 3] = new_state[empty_index - 3], new_state[empty_index]<br>            successors.append(tuple(new_state))<br>        if empty_index not in [6, 7, 8]:<br>            new_state = list(state)<br>            new_state[empty_index], new_state[empty_index + 3] = new_state[empty_index + 3], new_state[empty_index]<br>            successors.append(tuple(new_state))<br>        if empty_index not in [0, 3, 6]:<br>            new_state = list(state)<br>            new_state[empty_index], new_state[empty_index - 1] = new_state[empty_index - 1], new_state[empty_index]<br>            successors.append(tuple(new_state))<br>        if empty_index not in [2, 5, 8]:<br>            new_state = list(state)<br>            new_state[empty_index], new_state[empty_index + 1] = new_state[empty_index + 1], new_state[empty_index]<br>            successors.append(tuple(new_state))<br>        return successors<br><br>def heuristic(state):<br>    goal_state = (1, 2, 3, 4, 5, 6, 7, 8, 0)<br>    return sum([1 for i in range(len(state)) if state[i] != goal_state[i]])<br><br>initial_state = (1, 2, 0, 4, 5, 3, 7, 8, 6)<br>problem = EightPuzzleProblem(initial_state)<br><br>solution = best_first_search(problem, heuristic)<br><br>print(solution)</pre><p><strong>Output:</strong></p><pre>Expected output: (1, 2, 3, 4, 5, 6, 7, 8, 0)</pre><p>In this example, we define an EightPuzzleProblem class that represents the 8-puzzle problem. The is_goal_state method checks if the current state is the goal state. The get_successors method returns a list of the successor states of the current state. The heuristic function returns the number of misplaced tiles, which serves as an estimate of the number of moves required to reach the goal state. Finally, we create a problem object and pass it along with the heuristic function to the best_first_search function to solve the puzzle. The output is the goal state (1, 2, 3, 4, 5, 6, 7, 8, 0).</p><p><strong><em>Summary of Key Points:</em></strong></p><ul><li>Best First Search is a search algorithm that uses a heuristic function to guide its search towards the goal state.</li><li>It maintains a priority queue of nodes to be explored, with the node with the lowest heuristic value being the next to be explored.</li><li>Best First Search is not guaranteed to find the optimal solution, as it can get stuck in local optima.</li><li>Best First Search can be very efficient in some cases, especially when the heuristic function is well-designed and accurate.</li><li>Best First Search is widely used in artificial intelligence and robotics applications, including pathfinding and planning.</li></ul><p><strong><em>In Conclusion</em></strong></p><p>Best First Search is a powerful search algorithm that employs a heuristic function to direct its search toward the desired state. In some cases, it can be very efficient, especially if the heuristic function is well-designed and accurate.<br>Best First Search is widely used in AI and robotics applications such as pathfinding and planning. It has been used successfully in a variety of real-world scenarios, including route planning in GPS systems, game play, and robot navigation.</p><p>Anyone interested in artificial intelligence and robotics should be familiar with Best First Search and its variants. We can make smarter decisions and solve complex problems faster by using this algorithm.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5a3794650f46" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>