Back to 课程

Computer-science_A-level_Cie

0% Complete
0/0 Steps
  1. computers-and-components
    6 主题
  2. logic-gates-and-logic-circuits
    2 主题
  3. central-processing-unit-cpu-architecture
    6 主题
  4. assembly-language-
    4 主题
  5. bit-manipulation
    1 主题
  6. operating-systems
    3 主题
  7. language-translators
    2 主题
  8. data-security
    3 主题
  9. data-integrity
    1 主题
  10. ethics-and-ownership
    3 主题
  11. database-concepts
    3 主题
  12. database-management-systems-dbms-
    1 主题
  13. data-definition-language-ddl-and-data-manipulation-language-dml
    1 主题
  14. computational-thinking-skills
    1 主题
  15. algorithms
    14 主题
  16. data-types-and-records
    2 主题
  17. arrays
    2 主题
  18. files
    1 主题
  19. introduction-to-abstract-data-types-adt
    1 主题
  20. programming-basics
    1 主题
  21. constructs
    2 主题
  22. structured-programming
    1 主题
  23. program-development-life-cycle
    2 主题
  24. program-design-
    2 主题
  25. program-testing-and-maintenance
    3 主题
  26. user-defined-data-types
    1 主题
  27. file-organisation-and-access-
    3 主题
  28. floating-point-numbers-representation-and-manipulation
    3 主题
  29. protocols
    2 主题
  30. circuit-switching-packet-switching
    1 主题
  31. processors-parallel-processing-and-virtual-machines
    5 主题
  32. boolean-algebra-and-logic-circuits
    4 主题
  33. purposes-of-an-operating-system-os
    3 主题
  34. translation-software
    3 主题
  35. encryption-encryption-protocols-and-digital-certificates
    3 主题
  36. artificial-intelligence-ai
    4 主题
  37. recursion
    1 主题
  38. programming-paradigms
    4 主题
  39. object-oriented-programming
    7 主题
  40. file-processing-and-exception-handling
    2 主题
  41. data-representation
    5 主题
  42. multimedia
    3 主题
  43. compression
    2 主题
  44. networks-and-the-internet
    11 主题
课 Progress
0% Complete

A* algorithm

What is the A* algorithm?

  • The A* (A-star) algorithm is a pathfinding algorithm that builds upon Dijkstra’s algorithm

  • It introduces a heuristic function to improve efficiency

  • It is used to find the shortest path from a starting node to a goal node in a graph

  • While Dijkstra’s algorithm considers only the actual cost from the start node:

    • A* enhances this by estimating the remaining distance to the goal

    • This makes it more goal-oriented and often more efficient

How A* improves on Dijkstra’s

  • To avoid inefficient detours, A* introduces a heuristic function, called h(x)

  • It estimates the straight-line distance (Euclidean distance) from the current node to the goal

  • A* combines both cost and estimate using the formula:

    • f(x) = g(x) + h(x)

      • g(x) = the actual cost from the start node to the current node

      • h(x) = the heuristic estimate to the goal node

      • f(x) = the total estimated cost of the cheapest solution through node x

  • By choosing nodes with the lowest f(x) value, A* aims to explore paths that are both cheap and move closer to the goal

Heuristics in A*

  • The heuristic function h(x) should never overestimate the true cost to the goal

  • This ensures A* remains optimally efficient

  • The closer h(x) is to the true cost, the fewer nodes A* needs to explore

  • If h(x) increases, it may indicate the algorithm is moving away from the goal, helping it backtrack or choose a better path

  • Below is an illustration of the A* search:

dijkstras-algorithm-a-search-1
dijkstras-algorithm-a-search-2
dijkstras-algorithm-a-search-3
dijkstras-algorithm-a-search-4
dijkstras-algorithm-a-search-5
dijkstras-algorithm-a-search-6

Figure 2: Performing the A* Search

A* vs Dijkstra’s

Feature

Dijkstra’s

A* Search

Goal-aware?

No

Yes

Cost function

g(x)

g(x) + h(x)

Speed

Slower (explores more)

Faster (more direct toward goal)

Use of heuristic

None

Yes, estimates distance to goal

Best for…

Full shortest path map

Fastest route to a specific destination

Programming A* algorithm

How do you write an A* algorithm?

Pseudocode

FUNCTION AStarSearch(graph, start, goal) // Initialise distances and f-scores FOR EACH node FROM graph SET g[node] TO infinity SET f[node] TO infinity NEXT node SET g[start] TO 0 SET f[start] TO h[start] DECLARE openSet AS LIST ADD start TO openSet WHILE openSet IS NOT EMPTY // Find node in openSet with the lowest f value SET min TO null FOR EACH node FROM openSet IF min = null THEN SET min TO node ELSEIF f[node] < f[min] THEN SET min TO node ENDIF NEXT node IF min = goal THEN EXIT WHILE ENDIF REMOVE min FROM openSet // Check all neighbours of current node FOR EACH neighbour FROM graph[min] SET tentativeG TO g[min] + cost(min, neighbour) IF tentativeG < g[neighbour] THEN SET g[neighbour] TO tentativeG SET f[neighbour] TO g[neighbour] + h[neighbour] SET previousNode[neighbour] TO min IF neighbour NOT IN openSet THEN ADD neighbour TO
  • graph[min] assumes you’re storing adjacency lists

  • h[node] is your heuristic table

  • g[] and f[] are maps (dictionaries) for real cost and estimated total cost

  • cost(min, neighbour) should be defined or assumed available

Python

def a_star_search(graph, start, goal, h): open_set = [start] g = {node: float('inf') for node in graph} f = {node: float('inf') for node in graph} came_from = {} g[start] = 0 f[start] = h[start] while open_set: # Get node with lowest f value current = min(open_set, key=lambda node: f[node]) if current == goal: break open_set.remove(current) for neighbour, cost in graph[current]: tentative_g = g[current] + cost if tentative_g < g[neighbour]: g[neighbour] = tentative_g f[neighbour] = g[neighbour] + h[neighbour] came_from[neighbour] = current if neighbour not in open_set: open_set.append(neighbour) # Reconstruct path path = [] node = goal while node != start: path.append(node) node = came_from[node] path.append(start) path.reverse() return path

Example usage:

graph = { 'A': [('B', 2), ('C', 5)], 'B': [('D', 4)], 'C': [('D', 1)], 'D': [('E', 3)], 'E': []
} h = { 'A': 7, 'B': 6, 'C': 4, 'D': 2, 'E': 0
} print(a_star_search(graph, 'A', 'E', h))

Output:

  • A possible shortest path from 'A' to 'E', guided by the heuristic values

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注