From d1e05fa9998539ee508648fea11949b9edb3c9ad Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Sun, 25 Aug 2024 23:17:10 +0530 Subject: [PATCH 1/3] Refactor DynamicGraph print method --- include/clad/Differentiator/DynamicGraph.h | 53 ++++++++++++++++------ 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/include/clad/Differentiator/DynamicGraph.h b/include/clad/Differentiator/DynamicGraph.h index f7b5f61b0..e58a249d9 100644 --- a/include/clad/Differentiator/DynamicGraph.h +++ b/include/clad/Differentiator/DynamicGraph.h @@ -110,21 +110,46 @@ template class DynamicGraph { /// Print the nodes and edges in the graph. void print() { - // First print the nodes with their insertion order. - for (const T& node : m_nodes) { - std::pair nodeInfo = m_nodeMap[node]; - std::cout << (std::string)node << ": #" << nodeInfo.second; - if (m_sources.find(nodeInfo.second) != m_sources.end()) - std::cout << " (source)"; - if (nodeInfo.first) - std::cout << ", (done)\n"; - else - std::cout << ", (unprocessed)\n"; + std::unordered_set visited; + + // Recursive function to print nodes with indentation based on depth. + std::function printNode = [&](size_t nodeId, int depth) { + // Check if the node has been visited already to avoid cycles. + if (visited.find(nodeId) != visited.end()) + return; + visited.insert(nodeId); + + // Print the node with the appropriate indentation. + const T& node = m_nodes[nodeId]; + std::pair nodeInfo = m_nodeMap[node]; + std::string indent(depth * 4, ' '); + std::cout << indent << "`" << (std::string)node << ": #" << nodeInfo.second; + + // Indicate if the node is a source and/or done. + if (m_sources.find(nodeInfo.second) != m_sources.end()) + std::cout << " (source)"; + if (nodeInfo.first) + std::cout << ", (done)\n"; + else + std::cout << ", (unprocessed)\n"; + + // Recursively print all children (nodes connected by edges). + for (size_t destId : m_adjList[nodeId]) { + printNode(destId, depth + 1); + } + }; + + // Start printing from the source nodes. + for (size_t sourceId : m_sources) { + printNode(sourceId, 0); + } + + // If there are unvisited nodes, they are disconnected from sources, print them separately. + for (size_t i = 0; i < m_nodes.size(); ++i) { + if (visited.find(i) == visited.end()) { + printNode(i, 0); + } } - // Then print the edges. - for (int i = 0; i < m_nodes.size(); i++) - for (size_t dest : m_adjList[i]) - std::cout << i << " -> " << dest << "\n"; } /// Get the next node to be processed from the queue of nodes to be From 0b31c39b89554b939cede331a17dae76e13babbe Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Mon, 26 Aug 2024 20:14:00 +0530 Subject: [PATCH 2/3] Update include/clad/Differentiator/DynamicGraph.h Co-authored-by: Vassil Vassilev --- include/clad/Differentiator/DynamicGraph.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/clad/Differentiator/DynamicGraph.h b/include/clad/Differentiator/DynamicGraph.h index e58a249d9..fc7c1ac83 100644 --- a/include/clad/Differentiator/DynamicGraph.h +++ b/include/clad/Differentiator/DynamicGraph.h @@ -113,7 +113,7 @@ template class DynamicGraph { std::unordered_set visited; // Recursive function to print nodes with indentation based on depth. - std::function printNode = [&](size_t nodeId, int depth) { + auto printNode = [&](size_t nodeId, int depth) { // Check if the node has been visited already to avoid cycles. if (visited.find(nodeId) != visited.end()) return; From 3114f8caaea15d3eaaffa3fdc4bef4158b4cfcd3 Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Wed, 28 Aug 2024 10:06:29 +0530 Subject: [PATCH 3/3] chores: auto fix --- include/clad/Differentiator/DynamicGraph.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/clad/Differentiator/DynamicGraph.h b/include/clad/Differentiator/DynamicGraph.h index fc7c1ac83..e58a249d9 100644 --- a/include/clad/Differentiator/DynamicGraph.h +++ b/include/clad/Differentiator/DynamicGraph.h @@ -113,7 +113,7 @@ template class DynamicGraph { std::unordered_set visited; // Recursive function to print nodes with indentation based on depth. - auto printNode = [&](size_t nodeId, int depth) { + std::function printNode = [&](size_t nodeId, int depth) { // Check if the node has been visited already to avoid cycles. if (visited.find(nodeId) != visited.end()) return;