Update object graph implementation to track node discover and finish time

+ Allows traversal and topological sort algorithms to show examples from MIT Algorithms more accurately
This commit is contained in:
2021-06-28 12:46:04 -04:00
parent 5d37db1ce2
commit 348586ec38
4 changed files with 58 additions and 75 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::set<int>> localNodes{
std::map<int, std::vector<int>> localNodes{
{1, {2, 5}}, // Node 1
{2, {1, 6}}, // Node 2
{3, {4, 6, 7}},
@@ -26,13 +26,6 @@ int main (const int argc, const char * argv[])
};
// Graph bfsGraph(localNodes);
// Graph testGraph(
// {
// {Node(1, {2, 5})},
//// {Node(1, {2, 5})},
// }
// )
std::cout << "\n\n##### Breadth First Search #####\n";
// Or we could use an initializer list...
@@ -77,19 +70,20 @@ int main (const int argc, const char * argv[])
// Initialize an example graph for Depth First Search
Graph topologicalGraph (
{
{1, {4, 5}},
{2, {5}},
{3, {}},
{4, {5, 7}},
{5, {}},
{6, {7, 8}},
{7, {9}},
{8, {9}},
{9, {}},
{6, {8, 7}}, // shirt
{8, {9}}, // tie
{7, {9}}, // belt
{9, {}}, // jacket
{3, {}}, // watch
{1, {4, 5}}, // undershorts
{4, {5, 7}}, // pants
{5, {}}, // shoes
{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();
std::cout << "\n\nTopological order: ";
while (!order.empty()) {