Bahria University CSC-470: Artificial Intelligence Department of Computer Science Semester 07 (Spring 2015) Lab06: Greedy and A* Algorithms Objectives Introducing the search technique and write programs to implement the following techniques: 1. Greedy Searching Algorithm 2. A* Searching Algorithm 3. Exercise 1. Greedy Searching Algorithm The greedy best-first search algorithm is incomplete without cycle checking. If a closed list is used to not visit nodes which have already been visited, then the search is complete. The algorithm is also not optimal. Since the algorithm is only based on the heuristic value, if the heuristic is not exactly equal to the actual cost, it might go down a less optimal path than the ideal one. The greedy best-first search algorithm is O(bm) in terms of space and time complexity. (Where b is the average branching factor (the average number of successors from a state), and m is the maximum depth of the search tree.) A good heuristic can bring this down substantially for the common case, but all nodes are visited in the worst case. Example of the algorithm: BU, CS Department CSC-470: AI 2/4 Semester 7 (Spring 2015) Lab 06: Greedy and A* Algorithms Now there is a choice on which node to visit next. Because we are using greedy best-first search, the node with the lowest heuristic is used. If this solution was implemented, a priority queue would be used for the fringe, so it would always return the node with the lowest heuristic. Since node D has the lowest heuristic value, we visit at that node and add its neighbors to the fringe. BU, CS Department CSC-470: AI 3/4 Semester 7 (Spring 2015) Lab 06: Greedy and A* Algorithms The path found from Start to Goal is: Start -> A -> D -> E -> Goal. In this case, it was the optimal path, but only because the heuristic values were fairly accurate. Ref: http://www.cs.utah.edu/~hal/courses/2009S_AI/Walkthrough/GreedyBFS/ 2. A* Searching Algorithm A * is a best-firstgraph search algorithm that finds the least-cost path from a given initial node to one goal node (out of one or more possible goals).It uses a distance-plus-cost heuristic function (usually denoted f(x)) to determine the order in which the search visits nodes in the tree. The distance-pluscost heuristic is a sum of two functions: the path-cost function, which is the cost from the starting node to the current node (usually denoted g(x)) BU, CS Department CSC-470: AI 4/4 Semester 7 (Spring 2015) Lab 06: Greedy and A* Algorithms and an admissible "heuristic estimate" of the distance to the goal (usually denoted h(x)). The h(x) part of the f(x) function must be an admissible heuristic; that is, it must not overestimate the distance to the goal. Thus for an application like routing, h(x) might represent the straight-line distance to the goal, since that is physically the smallest possible distance between any two points (or nodes for that matter).The algorithm traverses various paths from start to goal. For each node x traversed, it maintains 3 values: • g(x): the actual shortest distance traveled from initial node to current node •h(x): the estimated (or "heuristic") distance from current node to goal •f(x): the sum of g(x) and h(x) Exercises Exercise 1 & 2 (Greedy and Astar) Write C# or Java program to implement Greedy Best First and A* Search Algorithms for following rout finding problem from Arad to Bucharest. Arad Bucharest Craiova Dobreta Eforie Fagaras Giurgiu Hirsova Iasi Lugoj Mehadia Neamt Oradea Pitesti RimnicuVilcea Sibiu Timisoara Urziceni Vaslui Zerind 366 0 160 242 161 178 77 151 226 244 241 234 380 98 193 253 329 80 199 374