A * (A_star) Search Summary

Source: Internet
Author: User

\ (a^* (A star) \)Search Summary

Tags: algorithms--search
Reading experience: https://zybuluo.com/Junlier/note/1299772

The definition duplicates a definition first

\ (a^*\) algorithm is a typical heuristic search algorithm in artificial intelligence
The estimate in the heuristic is expressed in the valuation function:
H (n) =f (n) +g (n)
where f (n) is the value function of node n
G (n) represents the actual cost of the actual state space from the initial node to the N node
H (N) is the estimated cost of the best path from N to the target node.
Also define the actual value of H ' (n) as n to the best path of the target node.
If H ' (n) ≥h (n) has the lowest cost of moving from the initial state to the target State, the solution
Then the algorithm used to search for the valuation function is called the \ (a^*\) algorithm.

It's a little cumbersome, but I can see it.

In layman's terms

The core of \ (a^*\) is the valuation function mentioned above
What does he do for a while?
Is that we are in the process of search, to ensure a better first-search
It's still a little cumbersome right, well, I don't know much about it ( I'll cheer up)

Hey, look underneath, I'm serious ...

If a topic asks us to ask for the least expensive solution of the first K (just a typical, not all topics)
Suppose we now have a state in \ (now\)
The price that has to be recorded in the answer is \ (d\)(I like to use this)
We found out that if we do, it's going to be messy, right, and it's definitely going to search too much.
And if the state is sorted directly by \ (d\) , there is no guarantee that the answer will be correct (or go greedy, of course).
So we introduce a valuation function \ (g[state]\)
Of course, it is generally possible to preprocess the optimal solution of a state to an answer state.
Go back to the current state \ (now\) mentioned earlier
What if we sort all the states that are tied to \ (now\) by \ (d+g[now]\) ?
Does not affect the correctness of the answer, but also reduce the transfer of bad state
(because the title request is the K-optimal state, and so that the decision-making state will be ordered and the completion of K can be completed, so it will become faster)

Well, it's a little bit wrong, so let's see an example.

Examples

Rokua P2901 [Usaco08mar] bull jog cow jogging
As if many other \ (oj\) have, but \ (bzoj\) is permission ...

Topic description

Ask us to find the length of the shortest K-path from start N to end 1
(Can only go from the numbered points to the numbered dots & side with Benquan)

Pretty naked, right?
  1. Preprocessing valuation functions

First run the reverse side of \ (spfa\) preprocessing each point to 1 of the shortest path as a valuation function

  1. Direct run \ (a^*\)(implemented here with \ (bfs\) )

Start with n (bfs\)and use the heap instead of the queue (to achieve the sort mentioned above).
At this point, the positive answer to the 1-node is better (i.e., the path is shorter)
The reason is simple: the valuation function guarantees that the answer is legitimate, and the answer is ordered after sorting .
Search for K to reach the 1 node of the path can be ended, fast fly ...

Put a code?

A very hard time to write a comment

#include <bits/stdc++.h> #define LST long long#define ldb double#define N 1050#define M 10050#define qw ljl[i].tousin    G namespace Std;const lst Inf=1e15;int read () {int S=0,m=0;char ch=getchar ();    while (!isdigit (CH)) {if (ch== '-') M=1;ch=getchar ();}    while (IsDigit (CH)) s= (s<<3) + (s<<1) + (ch^48), Ch=getchar (); return m?-s:s;} int N,m,k,done;bool in[n];lst dis[n];queue<int> q;int hd[n],cnt;struct edge{int to,nxt,v;} Ljl[m<<1];void Add (int p,int q,int o) {ljl[++cnt]= (EDGE) {q,hd[p],o},hd[p]=cnt;}    void Spfa () {for (int i=2;i<=n;++i) Dis[i]=inf; while (!    Q.empty ()) Q.pop ();    Q.push (1), dis[1]=0,in[1]=true; while (! Q.empty ()) {int Now=q.front ();        Q.pop (), In[now]=false;                for (int i=hd[now];i;i=ljl[i].nxt) if (QW&GT;NOW&AMP;&AMP;DIS[QW]&GT;DIS[NOW]+LJL[I].V) {                DIS[QW]=DIS[NOW]+LJL[I].V;            if (!in[qw]) In[qw]=true,q.push (QW); }}}//h[i]=g[i]+f[i]---->ans[i]=d+dis[i]struct node{LSTD;int ID;        BOOL operator< (const NODE &x) Const {return d+dis[id]>x.d+dis[x.id]; }};p riority_queue<node> h;void A_star_bfs () {while (!    H.empty ()) H.pop ();    H.push (NODE) {0,n}); while (!        H.empty ()) {NODE temp=h.top (); int now=temp.id;        H.pop (); if (now==1) {printf ("%lld\n", temp.            D);        if (++done==k) return;continue; } for (int i=hd[now];i;i=ljl[i].nxt) if (Qw<now) H.push ((NODE) {temp.    D+LJL[I].V,QW}); }while (done<k) ++done,puts ("1");}    int main () {N=read (), M=read (), K=read ();        for (int i=1;i<=m;++i) {int p=read (), Q=read (), O=read ();    Add (P,q,o), add (Q,p,o);    } SPFA (), A_star_bfs (); return 0;} /************1.a* algorithm is a typical heuristic search algorithm in artificial intelligence The estimation is expressed by the valuation function: H (n) =f (n) +g (n) where F (n) is the valued function g (n) of node n The actual cost of representing the actual state space from the initial node to the N node, h (n), is the estimated cost of the best path from N to the target node. Also define the actual value of H ' (n) as n to the best path of the target node. If H ' (n) ≥h (n) is present, if there is a solution to the minimum cost of moving from the initial state to the target State then the algorithm searched with the valuation function is called a * algorithm. 2. The K-Shortest algorithm we set the source point to S, the end point is T, we set the state F (i) of G (i) to go from S toThe actual distance of the node I, h (i) is the shortest distance from the node I to T, thus satisfying the requirement of a * algorithm, when the K-time goes to F (n-1), the G (n-1) at this time is the least-shorted length of K. 3. This is Kuai's xzy ... Don't blame me.  *************/
Summarize

So much for the time being.
The main is to see the internet is not written so popular \ (a^*\) Search
Just want to sum up (in fact, it is not popular ...). )
Take a walk, slip away, _______.

A * (A_star) Search Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.