|
| 1 | +#include <iostream> |
| 2 | +#include <unordered_map> |
| 3 | +#include <string> |
| 4 | +#include <vector> |
| 5 | +#include <limits> |
| 6 | + |
| 7 | +int main(void) |
| 8 | +{ |
| 9 | + using node_t = std::string; |
| 10 | + using node_cost_t = std::unordered_map<node_t, unsigned>; |
| 11 | + using graph_t = std::unordered_map<node_t, node_cost_t>; |
| 12 | + using parent_graph_t = std::unordered_map<node_t, node_t>; |
| 13 | + |
| 14 | + // Setup graph |
| 15 | + graph_t graph; |
| 16 | + graph.reserve(4U); |
| 17 | + graph.emplace("start", node_cost_t{{"a", 6}, {"b", 2}}); |
| 18 | + graph.emplace("a", node_cost_t{{"finish", 1}}); |
| 19 | + graph.emplace("b", node_cost_t{{"a", 3},{"finish", 5}}); |
| 20 | + graph.emplace("finish", node_cost_t{}); |
| 21 | + // Setup costs table |
| 22 | + node_cost_t costs{{"a", 6},{"b", 2},{"finish", std::numeric_limits<unsigned>::max()}}; |
| 23 | + // Setup parents table |
| 24 | + parent_graph_t parents{{"a", "start"}, {"b", "start"}}; |
| 25 | + |
| 26 | + // A vector of processed nodes |
| 27 | + std::vector<node_t> processed; |
| 28 | + processed.reserve(3U); |
| 29 | + // A lambda function to find the lowest cost node |
| 30 | + const auto find_lowest_cost_node = [&processed](const node_cost_t& costs) |
| 31 | + { |
| 32 | + auto lowest_cost = std::numeric_limits<unsigned>::max(); |
| 33 | + node_t lowest_cost_node{}; |
| 34 | + // Go through each node in the costs graph |
| 35 | + for (const auto& nodeCost : costs) |
| 36 | + { |
| 37 | + const auto& cost = nodeCost.second; |
| 38 | + const auto& node = nodeCost.first; |
| 39 | + // Check if this node is processed or not; |
| 40 | + bool isNotProcessed = std::find(processed.cbegin(), processed.cend(), node) == processed.cend(); |
| 41 | + // Find the lowest cost node |
| 42 | + if (cost < lowest_cost && isNotProcessed) |
| 43 | + { |
| 44 | + lowest_cost = cost; |
| 45 | + lowest_cost_node = node; |
| 46 | + } |
| 47 | + } |
| 48 | + return lowest_cost_node; |
| 49 | + }; |
| 50 | + |
| 51 | + // node is "b" at this time |
| 52 | + auto node = find_lowest_cost_node(costs); |
| 53 | + while (!node.empty()) |
| 54 | + { |
| 55 | + const auto costSoFar = costs[node]; |
| 56 | + const auto& neighbours = graph[node]; |
| 57 | + // Loop through all the nodes |
| 58 | + for (const auto& neighbour : neighbours) |
| 59 | + { |
| 60 | + const auto newCost = costSoFar + neighbour.second; |
| 61 | + const auto& currentNeighbourName = neighbour.first; |
| 62 | + // If it is cheaper than the cost registered in the costs graph, update the costs graph |
| 63 | + if (newCost < costs[currentNeighbourName]) |
| 64 | + { |
| 65 | + costs[currentNeighbourName] = newCost; |
| 66 | + parents[currentNeighbourName] = node; |
| 67 | + } |
| 68 | + } |
| 69 | + // Mark the current node as processed |
| 70 | + processed.push_back(node); |
| 71 | + // Find the next node to process. If they are all processed, this will return an empty string. |
| 72 | + node = find_lowest_cost_node(costs); |
| 73 | + } |
| 74 | + std::cout << "Cost from the start to each node:" << std::endl; |
| 75 | + // prints finish 6 b 2 a 5 |
| 76 | + for (const auto& cost : costs) |
| 77 | + { |
| 78 | + std::cout << cost.first << " " << cost.second << std::endl; |
| 79 | + } |
| 80 | + return 0; |
| 81 | +} |
0 commit comments