Data Structures
Getting paths in unweighted graph between two points
what we need:
- parameters - start vertex, end vertex, graph(adjacency list)
checklistarrayqueuearray-
maxMovesvariable to stop loop (Object.keys(graph.length) -
create a
startObj{current: neighbour.data, prev: null }, push tochecklistarr - add source neighbours to
queue. node object forEach{current: neighbour.data, prev: startObj } - while
queueis NOT empty &&checklist.length < maxMoves- let current vertex =
shift()first item offqueue - if the current vertex is NOT in
checklist, push current vertex tochecklist - for each neighbour of the vertex,
- if it is NOT the end vertex, push to
queue - if it is the end vertex, push to
checklist
- if it is NOT the end vertex, push to
- let current vertex =
return checklist
Getting shortest path from above
what we need:
- parameters - start vertex, end vertex, pathArr(from above)
shortestPatharray-
currentvariable (pointer) -
set
currentto the end vertex in thepathArr, pushcurrenttoshortestPath - while
currentis NOT the start vertex,- set
currenttocurrent.prev - push
current
- set
- reverse
shortestPath
return shortestPath