Breadth-first search will find you the shortest path (path with the fewest segments).
Dijkstra’s algorithm will find you the fastest path (path with the smallest total weight).
# Find the lowest cost node that you haven't processed yet
node = find_lowest_cost_node(costs)
# If you've processed all the nodes, the while loop is done
while node is not None:
# Get the cost and neighbors of current node
cost = costs[node]
neighbors = graph[node]
# Loop through the neighbors
for n in neighbors.keys():
new_cost = cost + neighbors[n]
# If it's cheaper to get to the neighbor by going through current node,
# update the cost for the neighbor
if costs[n] > new_cost:
costs[n] = new_cost
parents[n] = node
# Mark the node as processed
processed.append(node)
# Find the next node to process
node = find_lowest_cost_node(costs)
Javascript implementation
const graph = {
start: {
a: 6,
b: 2
},
a: {
finish: 1
},
b: {
a: 3,
finish: 5
},
finish: {}
};
const costs = {
a: 6,
b: 2,
finish: Infinity
};
const parents = {
a: "start",
b: "start",
finish: null
};
function findShortestPath(graph) {
let processed = [];
// Find the lowest cost node that you haven't processed yet
let node = findLowestCostNode(costs, processed);
// If you've processed all the nodes, the while loop is done
while (node) {
// Get the cost and neighbors of current node
const cost = costs[node];
const neighbors = graph[node];
// Loop through the neighbors
for (const neighbor in neighbors) {
const oldCost = costs[neighbor];
const newCost = cost + neighbors[neighbor];
// If it's cheaper to get to the neighbor by going through current node,
// update the cost for the neighbor
if (newCost < oldCost) {
costs[neighbor] = newCost;
parents[neighbor] = node;
}
}
// Mark the node as processed
processed.push(node);
// Find the next node to process
node = findLowestCostNode(costs, processed);
}
return costs.finish;
}
function findLowestCostNode(costs, processed) {
let lowestCost = Infinity;
let lowestCostNode = null;
for (const node in costs) {
const cost = costs[node];
if (!processed.includes(node) && cost < lowestCost) {
lowestCost = cost;
lowestCostNode = node;
}
}
return lowestCostNode;
}
Cycle in a graph happens when you start at a node, travel around, and end up at the same node.
Dijkstra only works on graphs with no cycles, or on graphs with a positive weight cycle.
Negative-weight edges break the algorithm when the same node has to be processed twice.
Bellman-ford algorithm
if you want to find the shortest path in a graph that has negative-weight edges.