Update object-graph example to add path finding between nodes using BFS

+ Clean code, add overloaded functions and helper functions for common tasks
This commit is contained in:
2021-07-01 17:17:47 -04:00
parent 348586ec38
commit 4a8b607ff6
3 changed files with 199 additions and 40 deletions

View File

@@ -14,7 +14,7 @@
int main (const int argc, const char * argv[])
{
// We could initialize the graph with some localNodes...
std::map<int, std::vector<int>> localNodes{
std::vector<Node> localNodes{
{1, {2, 5}}, // Node 1
{2, {1, 6}}, // Node 2
{3, {4, 6, 7}},
@@ -24,34 +24,50 @@ int main (const int argc, const char * argv[])
{7, {3, 4, 6, 8}},
{8, {4, 6}},
};
// Graph bfsGraph(localNodes);
Graph bfsGraphInit(localNodes);
std::cout << "\n\n##### Breadth First Search #####\n";
// Or we could use an initializer list...
// Initialize a example graph for Breadth First Search
Graph bfsGraph (
Graph bfsGraph(
{
{Node(1, {2, 5})}, // Node 1
{Node(2, {1, 6})}, // Node 2...
{Node(3, {4, 6, 7})},
{Node(4, {3, 7, 8})},
{Node(5, {1})},
{Node(6, {2, 3, 7})},
{Node(7, {3, 4, 6, 8})},
{Node(8, {4, 6})},
{1, {2, 5}}, // Node 1
{2, {1, 6}}, // Node 2...
{3, {4, 6, 7}},
{4, {3, 7, 8}},
{5, {1}},
{6, {2, 3, 7}},
{7, {3, 4, 6, 8}},
{8, {4, 6}},
}
);
// The graph traversed in this example is seen in MIT Intro to Algorithms
// + Chapter 22, Figure 22.3 on BFS
auto iter = bfsGraph.nodes_.begin();
std::advance(iter, 1);
bfsGraph.BFS(*iter);
bfsGraph.BFS(bfsGraph.GetNodeCopy(2));
Node test = bfsGraph.GetNodeCopy(3);
std::cout << "\nTesting finding a path between two nodes using BFS...\n";
// Test finding a path between two nodes using BFS
auto path = bfsGraph.PathBFS(
bfsGraph.GetNodeCopy(1), bfsGraph.GetNodeCopy(7)
);
// If we were returned an empty path, it doesn't exist
if (path.empty()) std::cout << "No valid path found!\n";
else {
// If we were returned a path, print it
std::cout << "\nValid path from " << path.front()->number
<< " to " << path.back()->number << ": ";
for (const auto &node : path) {
std::cout << node->number << " ";
}
std::cout << std::endl;
}
std::cout << "\n\n##### Depth First Search #####\n";
// Initialize an example graph for Depth First Search
Graph dfsGraph (
Graph dfsGraph(
{
{1, {2, 4}},
{2, {5}},
@@ -68,7 +84,41 @@ int main (const int argc, const char * argv[])
std::cout << "\n\n##### Topological Sort #####\n";
// Initialize an example graph for Depth First Search
// + The order of initialization is important
// + To produce the same result as seen in the book
// ++ If the order is changed, other valid topological orders will be found
// The book starts on the 'shirt' node (with the number 6, in this example)
Graph topologicalGraph (
{
{1, {4, 5}}, // undershorts
{2, {5}}, // socks
{3, {}}, // watch
{4, {5, 7}}, // pants
{5, {}}, // shoes
{6, {8, 7}}, // shirt
{7, {9}}, // belt
{8, {9}}, // tie
{9, {}}, // jacket
}
);
// The graph traversed in this example is seen in MIT Intro to Algorithms
// + Chapter 22, Figure 22.4 on DFS
// Unlike the simple-graph example, this final result matches MIT Algorithms
// + Aside from the placement of the watch node, which is not connected
// + This is because the node is visited after all other nodes are finished
std::vector<Node> order =
topologicalGraph.TopologicalSort(topologicalGraph.GetNodeCopy(6));
std::cout << "\n\nTopological order: ";
while (!order.empty()) {
std::cout << order.back().number << " ";
order.pop_back();
}
std::cout << std::endl;
// If we want the topological order to match what is seen in the book
// + We have to initialize the graph carefully to get this result -
Graph topologicalGraph2 (
{
{6, {8, 7}}, // shirt
{8, {9}}, // tie
@@ -81,15 +131,11 @@ int main (const int argc, const char * argv[])
{2, {5}}, // socks
}
);
// The graph traversed in this example is seen in MIT Intro to Algorithms
// + Chapter 22, Figure 22.4 on DFS
// Unlike the simple-graph example, this final result matches MIT Algorithms
std::vector<Node> order = topologicalGraph.TopologicalSort();
auto order2 = topologicalGraph2.TopologicalSort(*topologicalGraph2.NodeBegin());
std::cout << "\n\nTopological order: ";
while (!order.empty()) {
std::cout << order.back().number << " ";
order.pop_back();
while (!order2.empty()) {
std::cout << order2.back().number << " ";
order2.pop_back();
}
std::cout << std::endl;
}
}