-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42ce09a
commit ee35c78
Showing
6 changed files
with
227 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
export function dijkstra(grid, startNode, finishNode) { | ||
startNode.distance = 0; | ||
const visistedNodesInOrder = []; | ||
const unvisitedNodes = getAllNodes(grid); | ||
console.log(startNode); | ||
while (unvisitedNodes.length != 0) { | ||
sortNodesByDistance(unvisitedNodes); | ||
const currNode = unvisitedNodes.shift(); | ||
if (currNode.distance === Infinity) { | ||
return visistedNodesInOrder; | ||
} | ||
currNode.isVisited = true; | ||
visistedNodesInOrder.push(currNode); | ||
if (currNode === finishNode) return visistedNodesInOrder; | ||
updateUnvisitedNeighbours(currNode, grid); | ||
} | ||
} | ||
|
||
function sortNodesByDistance(unvisitedNodes) { | ||
unvisitedNodes.sort((nodeA, nodeB) => nodeA.distance - nodeB.distance); | ||
} | ||
|
||
function updateUnvisitedNeighbours(node, grid) { | ||
const unvisitedNeighbours = getUnvisistedNeighbours(node, grid); | ||
for (let neighbour of unvisitedNeighbours) { | ||
neighbour.distance = node.distance + 1; | ||
neighbour.previousNode = node; | ||
} | ||
} | ||
|
||
function getUnvisistedNeighbours(node, grid) { | ||
const neighbours = []; | ||
const { col, row } = node; | ||
if (row > 0) neighbours.push(grid[row - 1][col]); | ||
if (col > 0) neighbours.push(grid[row][col - 1]); | ||
if (row < grid.length - 1) neighbours.push(grid[row + 1][col]); | ||
if (col < grid[0].length - 1) neighbours.push(grid[row][col + 1]); | ||
return neighbours.filter((node) => !node.isVisited); | ||
} | ||
|
||
function getAllNodes(grid) { | ||
const nodes = []; | ||
for (let row of grid) { | ||
for (let col of row) { | ||
nodes.push(col); | ||
} | ||
} | ||
return nodes; | ||
} | ||
|
||
export function findTheShortestPath(finishNode) { | ||
const nodesInShortesPathOrder = []; | ||
let currNode = finishNode; | ||
while (currNode !== null) { | ||
nodesInShortesPathOrder.unshift(currNode); | ||
currNode = currNode.previousNode; | ||
} | ||
return nodesInShortesPathOrder; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import Vue from 'vue' | ||
import App from './App.vue' | ||
import router from './router' | ||
import store from './store' | ||
import Vue from "vue"; | ||
import App from "./App.vue"; | ||
import router from "./router"; | ||
import store from "./store"; | ||
import "@fortawesome/fontawesome-free/css/all.css"; | ||
import "@fortawesome/fontawesome-free/js/all.js"; | ||
|
||
Vue.config.productionTip = false | ||
Vue.config.productionTip = false; | ||
|
||
new Vue({ | ||
router, | ||
store, | ||
render: h => h(App) | ||
}).$mount('#app') | ||
render: (h) => h(App), | ||
}).$mount("#app"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters