AStar(start, goal): Open = üres prioritásos sor Closed = üres halmaz g = 0 f = h(start) Open.helyezd_el(start) amíg Open nem üres: current = Open.min_f() ha current == goal: térj vissza az útvonalra Open.távolítsd_el(current) Closed.helyezd_el(current) minden szomszéd in current.szomszédai: ha szomszéd in Closed: folytasd tentative_g = g + cost(current, szomszéd) ha szomszéd not in Open vagy tentative_g < g: g = tentative_g f = g + h(szomszéd) ha szomszéd not in Open: Open.helyezd_el(szomszéd) térj vissza "Nincs út"
import heapq
def a_star(graph, start, goal, h):
open_set =
heapq.heappush(open_set, (0, start))
came_from = {}
g_score = {node: float('inf') for node in graph}
g_score = 0
f_score = {node: float('inf') for node in graph}
f_score = h
while open_set:
_, current = heapq.heappop(open_set)
if current == goal:
path =
while current in came_from:
path.append(current)
current = came_from
return path
for neighbor, cost in graph.items():
tentative_g_score = g_score + cost
if tentative_g_score < g_score:
came_from = current
g_score = tentative_g_score
f_score = g_score + h
if not any(neighbor == item for item in open_set):
heapq.heappush(open_set, (f_score, neighbor))
return "Nincs elérhető út"
# Példa gráf
graph = {
'A': {'B': 1, 'C': 4},
'B': {'A': 1, 'C': 2, 'D': 5},
'C': {'A': 4, 'B': 2, 'D': 1},
'D': {'B': 5, 'C': 1}
}
# Heurisztikus függvény
h = {
'A': 7,
'B': 6,
'C': 2,
'D': 0
}
start = 'A'
goal = 'D'
path = a_star(graph, start, goal, h)
print("Útvonal:", path)
Kimenet:
Útvonal:
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <limits>
using namespace std;
struct Node {
string name;
double cost;
bool operator>(const Node& other) const {
return cost > other.cost;
}
};
vector<string> reconstruct_path(unordered_map<string, string>& came_from, string current) {
vector<string> path;
while (came_from.find(current) != came_from.end()) {
path.push_back(current);
current = came_from;
}
reverse(path.begin(), path.end());
return path;
}
vector<string> a_star(
unordered_map<string, unordered_map<string, double>>& graph,
unordered_map<string, double>& h,
string start,
string goal) {
priority_queue<Node, vector<Node>, greater<Node>> open_set;
unordered_map<string, double> g_score;
unordered_map<string, double> f_score;
unordered_map<string, string> came_from;
for (auto& node : graph) {
g_score = numeric_limits<double>::infinity();
f_score = numeric_limits<double>::infinity();
}
g_score = 0;
f_score = h;
open_set.push({start, f_score});
while (!open_set.empty()) {
string current = open_set.top().name;
open_set.pop();
if (current == goal) {
return reconstruct_path(came_from, current);
}
for (auto& neighbor : graph) {
double tentative_g_score = g_score + neighbor.second;
if (tentative_g_score < g_score) {
came_from = current;
g_score = tentative_g_score;
f_score = g_score + h;
open_set.push({neighbor.first, f_score});
}
}
}
return {"Nincs elérhető út"};
}
int main() {
unordered_map<string, unordered_map<string, double>> graph = {
{"A", {{"B", 1}, {"C", 4}}},
{"B", {{"A", 1}, {"C", 2}, {"D", 5}}},
{"C", {{"A", 4}, {"B", 2}, {"D", 1}}},
{"D", {{"B", 5}, {"C", 1}}}
};
unordered_map<string, double> h = {
{"A", 7},
{"B", 6},
{"C", 2},
{"D", 0}
};
string start = "A";
string goal = "D";
vector<string> path = a_star(graph, h, start, goal);
cout << "Útvonal: ";
for (auto& node : path) {
cout << node << " ";
}
cout << endl;
return 0;
}
Kimenet:
Útvonal: A B C D
Az A* algoritmus kiváló választás útvonaltervezési problémákra, például térképészeti alkalmazásokban, mesterséges intelligenciában és robotikában.