From 4fd96f6987ae8d2d67ae3a79201bc2f9c28e29be Mon Sep 17 00:00:00 2001 From: fhl43211 <7145936+fhl43211@users.noreply.github.com> Date: Thu, 20 Feb 2020 17:23:54 -0800 Subject: [PATCH 1/2] Create cpp file --- .../c++11/01_dijkstras_algorithm.cpp | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 07_dijkstras_algorithm/c++11/01_dijkstras_algorithm.cpp diff --git a/07_dijkstras_algorithm/c++11/01_dijkstras_algorithm.cpp b/07_dijkstras_algorithm/c++11/01_dijkstras_algorithm.cpp new file mode 100644 index 0000000..3998393 --- /dev/null +++ b/07_dijkstras_algorithm/c++11/01_dijkstras_algorithm.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include +#include + +namespace +{ + using node_t = std::string; + using node_cost_t = std::unordered_map; + using graph_t = std::unordered_map; + using parent_graph_t = std::unordered_map; + + std::vector processed; + auto max = std::numeric_limits::max(); + const node_t find_lowest_cost_node(const node_cost_t& costs) + { + auto lowest_cost = max; + node_t lowest_cost_node{}; + // Go through each node in the costs graph + for (const auto& nodeCost : costs) + { + const auto& cost = nodeCost.second; + const auto& node = nodeCost.first; + // Check if this node is processed or not; + bool isNotProcessed = std::find(processed.cbegin(), processed.cend(), node) == processed.cend(); + // Find the lowest cost node + if (cost < lowest_cost && isNotProcessed) + { + lowest_cost = cost; + lowest_cost_node = node; + } + } + return lowest_cost_node; + } +} + +int main(void) +{ + // Setup graph + graph_t graph; + graph.emplace("start", node_cost_t{{"a", 6}, {"b", 2}}); + graph.emplace("a", node_cost_t{{"finish", 1}}); + graph.emplace("b", node_cost_t{{"a", 3},{"finish", 5}}); + graph.emplace("finish", node_cost_t{}); + // Setup costs table + node_cost_t costs{{"a", 6},{"b", 2},{"finish", max}}; + // Setup parents table + parent_graph_t parents{{"a", "start"}, {"b", "start"}}; + // node is "b" at this time + auto node = find_lowest_cost_node(costs); + while (!node.empty()) + { + const auto costSoFar = costs[node]; + const auto& neighbours = graph[node]; + // Loop through all the nodes + for (const auto& neighbour : neighbours) + { + const auto newCost = costSoFar + neighbour.second; + const auto& currentNeighbourName = neighbour.first; + // If it is cheaper than the cost registered in the costs graph, update the costs graph + if (newCost < costs[currentNeighbourName]) + { + costs[currentNeighbourName] = newCost; + parents[currentNeighbourName] = node; + } + } + // Mark the current node as processed + processed.push_back(node); + // Find the next node to process. If they are all processed, this will return an empty string. + node = find_lowest_cost_node(costs); + } + std::cout << "Cost from the start to each node:" << std::endl; + std::for_each(costs.cbegin(), + costs.cend(), + [](const std::pair& cost) { std::cout << cost.first << " " << cost.second << std::endl; }); + return 0; +} From e6558351a88270f2e3d949dd7b63728b553babfb Mon Sep 17 00:00:00 2001 From: fhl43211 Date: Fri, 21 Feb 2020 09:06:58 -0800 Subject: [PATCH 2/2] Use lambda function --- .../c++11/01_dijkstras_algorithm.cpp | 66 ++++++++++--------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/07_dijkstras_algorithm/c++11/01_dijkstras_algorithm.cpp b/07_dijkstras_algorithm/c++11/01_dijkstras_algorithm.cpp index 3998393..f0e069d 100644 --- a/07_dijkstras_algorithm/c++11/01_dijkstras_algorithm.cpp +++ b/07_dijkstras_algorithm/c++11/01_dijkstras_algorithm.cpp @@ -3,51 +3,51 @@ #include #include #include -#include -namespace +int main(void) { using node_t = std::string; using node_cost_t = std::unordered_map; using graph_t = std::unordered_map; using parent_graph_t = std::unordered_map; - std::vector processed; - auto max = std::numeric_limits::max(); - const node_t find_lowest_cost_node(const node_cost_t& costs) - { - auto lowest_cost = max; - node_t lowest_cost_node{}; - // Go through each node in the costs graph - for (const auto& nodeCost : costs) - { - const auto& cost = nodeCost.second; - const auto& node = nodeCost.first; - // Check if this node is processed or not; - bool isNotProcessed = std::find(processed.cbegin(), processed.cend(), node) == processed.cend(); - // Find the lowest cost node - if (cost < lowest_cost && isNotProcessed) - { - lowest_cost = cost; - lowest_cost_node = node; - } - } - return lowest_cost_node; - } -} - -int main(void) -{ // Setup graph graph_t graph; + graph.reserve(4U); graph.emplace("start", node_cost_t{{"a", 6}, {"b", 2}}); graph.emplace("a", node_cost_t{{"finish", 1}}); graph.emplace("b", node_cost_t{{"a", 3},{"finish", 5}}); graph.emplace("finish", node_cost_t{}); // Setup costs table - node_cost_t costs{{"a", 6},{"b", 2},{"finish", max}}; + node_cost_t costs{{"a", 6},{"b", 2},{"finish", std::numeric_limits::max()}}; // Setup parents table parent_graph_t parents{{"a", "start"}, {"b", "start"}}; + + // A vector of processed nodes + std::vector processed; + processed.reserve(3U); + // A lambda function to find the lowest cost node + const auto find_lowest_cost_node = [&processed](const node_cost_t& costs) + { + auto lowest_cost = std::numeric_limits::max(); + node_t lowest_cost_node{}; + // Go through each node in the costs graph + for (const auto& nodeCost : costs) + { + const auto& cost = nodeCost.second; + const auto& node = nodeCost.first; + // Check if this node is processed or not; + bool isNotProcessed = std::find(processed.cbegin(), processed.cend(), node) == processed.cend(); + // Find the lowest cost node + if (cost < lowest_cost && isNotProcessed) + { + lowest_cost = cost; + lowest_cost_node = node; + } + } + return lowest_cost_node; + }; + // node is "b" at this time auto node = find_lowest_cost_node(costs); while (!node.empty()) @@ -72,8 +72,10 @@ int main(void) node = find_lowest_cost_node(costs); } std::cout << "Cost from the start to each node:" << std::endl; - std::for_each(costs.cbegin(), - costs.cend(), - [](const std::pair& cost) { std::cout << cost.first << " " << cost.second << std::endl; }); + // prints finish 6 b 2 a 5 + for (const auto& cost : costs) + { + std::cout << cost.first << " " << cost.second << std::endl; + } return 0; }