blog/assets/js/3adcbc3a.c19cae1b.js

1 line
48 KiB
JavaScript
Raw Normal View History

"use strict";(self.webpackChunkfi=self.webpackChunkfi||[]).push([[5701],{62535:(e,s,n)=>{n.r(s),n.d(s,{assets:()=>h,contentTitle:()=>r,default:()=>d,frontMatter:()=>i,metadata:()=>l,toc:()=>c});var t=n(85893),a=n(11151);const i={id:"bf",slug:"/paths/bf-to-astar/bf",title:"BF",description:"Solving the shortest path problem with a na\xefve approach that turns into\nsomething.\n",tags:["cpp","brute force","bellman ford","dynamic programming"],last_update:{date:new Date("2024-01-01T00:00:00.000Z")}},r=void 0,l={id:"paths/2024-01-01-bf-to-astar/bf",title:"BF",description:"Solving the shortest path problem with a na\xefve approach that turns into\nsomething.\n",source:"@site/algorithms/11-paths/2024-01-01-bf-to-astar/01-bf.md",sourceDirName:"11-paths/2024-01-01-bf-to-astar",slug:"/paths/bf-to-astar/bf",permalink:"/algorithms/paths/bf-to-astar/bf",draft:!1,unlisted:!1,editUrl:"https://github.com/mfocko/blog/tree/main/algorithms/11-paths/2024-01-01-bf-to-astar/01-bf.md",tags:[{label:"cpp",permalink:"/algorithms/tags/cpp"},{label:"brute force",permalink:"/algorithms/tags/brute-force"},{label:"bellman ford",permalink:"/algorithms/tags/bellman-ford"},{label:"dynamic programming",permalink:"/algorithms/tags/dynamic-programming"}],version:"current",lastUpdatedAt:1704067200,formattedLastUpdatedAt:"Jan 1, 2024",sidebarPosition:1,frontMatter:{id:"bf",slug:"/paths/bf-to-astar/bf",title:"BF",description:"Solving the shortest path problem with a na\xefve approach that turns into\nsomething.\n",tags:["cpp","brute force","bellman ford","dynamic programming"],last_update:{date:"2024-01-01T00:00:00.000Z"}},sidebar:"autogeneratedBar",previous:{title:"From BF to A*",permalink:"/algorithms/paths/bf-to-astar"},next:{title:"Dijkstra's algorithm",permalink:"/algorithms/paths/bf-to-astar/dijkstra"}},h={},c=[{value:"Basic idea",id:"basic-idea",level:2},{value:"Na\xefve approach",id:"na\xefve-approach",level:2},{value:"Correctness",id:"correctness",level:3},{value:"Fixing the infinite loop",id:"fixing-the-infinite-loop",level:3},{value:"Bellman-Ford",id:"bellman-ford",level:2},{value:"On the Bellman-Ford",id:"on-the-bellman-ford",level:3},{value:"Time complexity",id:"time-complexity",level:2},{value:"Small refactor",id:"small-refactor",level:2}];function o(e){const s={a:"a",admonition:"admonition",annotation:"annotation",blockquote:"blockquote",code:"code",em:"em",h2:"h2",h3:"h3",hr:"hr",li:"li",math:"math",mi:"mi",mn:"mn",mo:"mo",mrow:"mrow",msub:"msub",ol:"ol",p:"p",pre:"pre",section:"section",semantics:"semantics",span:"span",strong:"strong",sup:"sup",...(0,a.a)(),...e.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(s.h2,{id:"basic-idea",children:"Basic idea"}),"\n",(0,t.jsxs)(s.p,{children:["We will ",(0,t.jsx)(s.em,{children:"ease in"})," with our own algorithm to find the shortest path. We will\nstart by thinking about the ways we can achieve that. If we didn't have the ",(0,t.jsx)(s.code,{children:"*"}),"\ncells, we could've easily run a BFS",(0,t.jsx)(s.sup,{children:(0,t.jsx)(s.a,{href:"#user-content-fn-1",id:"user-content-fnref-1","data-footnote-ref":!0,"aria-describedby":"footnote-label",children:"1"})})," and be done with it. Maybe it is a good\nplace to start, or isn't, there is only one way to find out though."]}),"\n",(0,t.jsxs)(s.p,{children:[(0,t.jsx)(s.em,{children:"How does the BFS work?"})," We know the vertex where we start and we know the\nvertex we want to find the shortest path to. Given this knowledge we\nincrementally visit all of our neighbours and we do that over and over until the\ndestination is found",(0,t.jsx)(s.sup,{children:(0,t.jsx)(s.a,{href:"#user-content-fn-2",id:"user-content-fnref-2","data-footnote-ref":!0,"aria-describedby":"footnote-label",children:"2"})}),". Could we leverage this somehow?"]}),"\n",(0,t.jsx)(s.h2,{id:"na\xefve-approach",children:"Na\xefve approach"}),"\n",(0,t.jsxs)(s.p,{children:["Well, we could probably start with all vertices being ",(0,t.jsx)(s.em,{children:"unreachable"})," (having the\nhighest possible price) and try to improve what we've gotten so far until there\nare no i