0% found this document useful (0 votes)
38 views4 pages

Quiz#03: Q1. What Is A and Its Concepts?

The document discusses the A* search algorithm. It explains that A* is an intelligent pathfinding algorithm that is commonly used in games and maps to efficiently find shortest paths. The document provides pseudocode to implement A* in Python. It defines a Graph class with methods for getting neighbors and heuristic values. The A* search method uses open and closed lists to iteratively explore nodes until the target is found.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views4 pages

Quiz#03: Q1. What Is A and Its Concepts?

The document discusses the A* search algorithm. It explains that A* is an intelligent pathfinding algorithm that is commonly used in games and maps to efficiently find shortest paths. The document provides pseudocode to implement A* in Python. It defines a Graph class with methods for getting neighbors and heuristic values. The A* search method uses open and closed lists to iteratively explore nodes until the target is found.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

AI Indus University Sir Hassan

Quiz#03
Q1. What is A* and its Concepts?

A* Search algorithm is one of the best and popular technique used in path-finding and graph traversals.

A* Search algorithms, unlike other traversal techniques, it has “brains”. What it means is that it is really
a smart algorithm which separates it from the other conventional algorithms. This fact is cleared in
detail in below sections.

And it is also worth mentioning that many games and web-based maps use this algorithm to find the
shortest path very efficiently (approximation).

Working

 A* Algorithm is one of the best and popular techniques used for path finding and graph
traversals.

 A lot of games and web-based maps use this algorithm for finding the shortest path efficiently.

 It is essentially a best first search algorithm.

 It maintains a tree of paths originating at the start node.

 It extends those paths one edge at a time.

 It continues until its termination criterion is satisfied.

Concept/Example:

Let’s try to understand Basic AI Concepts and to comprehend how does A* algorithm work. Imagine a
huge maze, one that is too big that it takes hours to reach the endpoint manually. Once you complete it
on foot, you need to go for another one. Which implies that you would end up investing a lot of time
and effort to find the possible paths in this maze. Now, you want to make it less time-consuming. To
make it easier, we will consider this maze as a search problem and will try to apply it to other possible
mazes we might encounter in the due course, provided they follow the same structure and rules.

Q2. Implementation of A* algorithm in Python.

1|Page
Akrima Huzaifa Akhtar
2719-2017
AI Indus University Sir Hassan

Code:

from collections import deque

class Graph:
    # example of adjacency list (or rather map)
    # adjacency_list = {
    # 'A': [('B', 1), ('C', 3), ('D', 7)],
    # 'B': [('D', 5)],
    # 'C': [('D', 12)]
    # }

    def __init__(self, adjacency_list):
        self.adjacency_list = adjacency_list
    def get_neighbors(self, v):
        return self.adjacency_list[v]

    def h(self, n):
        H = {
            'A': 1,
            'B': 1,
            'C': 1,
            'D': 1
        }
        return H[n]

    def a_star_algorithm(self, start_node, stop_node):
        open_list = set([start_node])
        closed_list = set([])

        g = {}

        g[start_node] = 0

        parents = {}
        parents[start_node] = start_node

        while len(open_list) > 0:
            n = None
            for v in open_list:
                if n == None or g[v] + self.h(v) < g[n] + self.h(n):
                    n = v;
            if n == None:
                print('Path does not exist!')

2|Page
Akrima Huzaifa Akhtar
2719-2017
AI Indus University Sir Hassan

                return None
            if n == stop_node:
                reconst_path = []

                while parents[n] != n:
                    reconst_path.append(n)
                    n = parents[n]

                reconst_path.append(start_node)

                reconst_path.reverse()

                print('Path found: {}'.format(reconst_path))
                return reconst_path

            for (m, weight) in self.get_neighbors(n):
                if m not in open_list and m not in closed_list:
                    open_list.add(m)
                    parents[m] = n
                    g[m] = g[n] + weight
                else:
                    if g[m] > g[n] + weight:
                        g[m] = g[n] + weight
                        parents[m] = n
                        if m in closed_list:
                            closed_list.remove(m)
                            open_list.add(m)
            open_list.remove(n)
            closed_list.add(n)

        print('Path does not exist!')
        return None

adjacency_list = {
    'A': [('B', 1), ('C', 3), ('D', 7)],
    'B': [('D', 5)],
    'C': [('D', 12)]
}
graph1 = Graph(adjacency_list)
graph1.a_star_algorithm('A', 'D')

Output:

3|Page
Akrima Huzaifa Akhtar
2719-2017
AI Indus University Sir Hassan

4|Page
Akrima Huzaifa Akhtar
2719-2017

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy