blog/assets/js/493c0536.6ae62548.js

1 line
17 KiB
JavaScript
Raw Normal View History

"use strict";(self.webpackChunkfi=self.webpackChunkfi||[]).push([[7292],{45594:(e,s,a)=>{a.r(s),a.d(s,{assets:()=>o,contentTitle:()=>r,default:()=>m,frontMatter:()=>i,metadata:()=>l,toc:()=>h});var t=a(85893),n=a(11151);const i={id:"astar",slug:"/paths/bf-to-astar/astar",title:"A* algorithm",description:"Moving from Dijkstra's algorithm into the A* algorithm.\n",tags:["cpp","dynamic programming","astar"],last_update:{date:new Date("2024-01-03T00:00:00.000Z")}},r=void 0,l={id:"paths/2024-01-01-bf-to-astar/astar",title:"A* algorithm",description:"Moving from Dijkstra's algorithm into the A* algorithm.\n",source:"@site/algorithms/11-paths/2024-01-01-bf-to-astar/03-astar.md",sourceDirName:"11-paths/2024-01-01-bf-to-astar",slug:"/paths/bf-to-astar/astar",permalink:"/algorithms/paths/bf-to-astar/astar",draft:!1,unlisted:!1,editUrl:"https://github.com/mfocko/blog/tree/main/algorithms/11-paths/2024-01-01-bf-to-astar/03-astar.md",tags:[{label:"cpp",permalink:"/algorithms/tags/cpp"},{label:"dynamic programming",permalink:"/algorithms/tags/dynamic-programming"},{label:"astar",permalink:"/algorithms/tags/astar"}],version:"current",lastUpdatedAt:170424e4,formattedLastUpdatedAt:"Jan 3, 2024",sidebarPosition:3,frontMatter:{id:"astar",slug:"/paths/bf-to-astar/astar",title:"A* algorithm",description:"Moving from Dijkstra's algorithm into the A* algorithm.\n",tags:["cpp","dynamic programming","astar"],last_update:{date:"2024-01-03T00:00:00.000Z"}},sidebar:"autogeneratedBar",previous:{title:"Dijkstra's algorithm",permalink:"/algorithms/paths/bf-to-astar/dijkstra"},next:{title:"Hash Tables",permalink:"/algorithms/category/hash-tables"}},o={},h=[{value:"Intro",id:"intro",level:2},{value:"A* description",id:"a-description",level:2},{value:"Roadmap heuristic",id:"roadmap-heuristic",level:2},{value:"Heuristic for our map",id:"heuristic-for-our-map",level:2},{value:"Passing the heuristic",id:"passing-the-heuristic",level:2},{value:"Implementation",id:"implementation",level:2},{value:"Running on our map",id:"running-on-our-map",level:2},{value:"Comparison",id:"comparison",level:2},{value:"Summary",id:"summary",level:2}];function c(e){const s={admonition:"admonition",annotation:"annotation",code:"code",em:"em",h2:"h2",li:"li",math:"math",mi:"mi",mo:"mo",mrow:"mrow",msub:"msub",ol:"ol",p:"p",pre:"pre",semantics:"semantics",span:"span",...(0,n.a)(),...e.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(s.h2,{id:"intro",children:"Intro"}),"\n",(0,t.jsx)(s.p,{children:"Let's start by the recap of what we've achieved so far:"}),"\n",(0,t.jsxs)(s.ol,{children:["\n",(0,t.jsx)(s.li,{children:"We have implemented a na\xefve brute-force algorithm that tries to relax paths\nas long as there are any paths to be relaxed."}),"\n",(0,t.jsxs)(s.li,{children:["Then we have fixed an issue caused by negative loops that can result in\na non-terminating run of our brute-force method. At this moment we have made\nsome small arguments why are bounding is enough and doesn't prevent any\nshortest path to ",(0,t.jsx)(s.em,{children:"not be"})," discovered."]}),"\n",(0,t.jsx)(s.li,{children:"Finally we have converted our bounded brute-force algorithm into the\nBellman-Ford algorithm."}),"\n",(0,t.jsx)(s.li,{children:"We have mentioned the worst-case time complexity of our bounded na\xefve\napproach and also the Bellman-Ford algorithm. Our worst-case depended on the\nfact that we assumed the worst possible ordering of the relaxations. However\nwe could also try to relax in the most ideal ordering which could result in a\nfaster algorithm and that's how we got to the Dijkstra's algorithm."}),"\n"]}),"\n",(0,t.jsxs)(s.p,{children:["Now the question is, could we improve the Dijkstra's algorithm to get even\nbetter results? And the answer is ",(0,t.jsx)(s.em,{children:"maybe"}),"!"]}),"\n",(0,t.jsx)(s.p,{children:"Dijkstra's algorithm chooses the next cheapest vertex for relaxing. This is good\nas long as there is no additional information. However, imagine a roadmap of\nsome country. If you're in the middle of the map and you want to go south, it\ndoesn't make much