A graph that does not contain any cycles or loops is called an acyclic graph. A tree is an acyclic graph in which every node is connected to another unique node. Acyclic graphs are also called acyclic graphs.
The difference between cyclic graphs and acyclic graphs -
Cycle Graph | is: Cycle Graph |
Acyclic graph |
---|---|---|
The graph forms a closed loop. |
The chart does not form a closed loop. |
|
Deep loops are not included in the chart |
Chart contains every depth. |
Example 1
Let’s take an example of a cyclic graph −
When a closed loop exists, a cyclic graph is formed.

Figure I represents a cycle graph and does not contain depth nodes.
Example 2
is translated as:Example 2
Let us illustrate with an example of an acyclic graph:

The root node of the tree is called the zero-depth node. In Figure II, there is only one root at zero depth, which is 2. Therefore it is considered a node with a minimum depth of zero.
In the first depth node, we have 3 node elements like 4, 9 and 1, but the smallest element is 4.
In the second depth node we again have 3 node elements like 6, 3 and 1 but the smallest element is 1.
We will know how the total depth node is derived,
Total depth node = Minimum value of Zero_Depth node Minimum value of First_Depth node Minimum value of Zero_Depth node
Total depth nodes = 2 4 3 = 9. So, 9 is the total minimum sum of the acyclic graph.
grammar
The following syntax used in the program: struct name_of_structure{ data_type var_name; // data member or field of the structure. }
struct − This keyword is used to represent the structure data type.
name_of_struct - We provide any name for the structure.
A structure is a collection of various related variables in one place.
Queue < pair < datatype, datatype> > queue_of_pair
make_pair()
parameter
Pair queue in C -
This is a generic STL template for combining queue pairs of two different data types, the queue pairs are located under the utility header file.
Queue_of_pair - We give the pair any name.
make_pair() - Used to construct a pair object with two elements.
name_of_queue.push()
parameter
name_of_queue - We are naming the queue name.
push() − This is a predefined method that is part of the head of the queue. The push method is used to insert elements or values.
name_of_queue.pop()
parameter
-
name_of_queue − We are giving the queue a name.
pop() − This is a predefined method that belongs to the queue header file, and the pop method is used to delete the entire element or value.
algorithm
-
We will start the program header files, namely 'iostream', 'climits', 'utility', and 'queue'.
We are creating a structure "tree_node" with an integer value "val" to get the node value. We then create tree_node pointers with the given data to initialize the left child node and right child node to store the values. Next, we create a tree_node function passing int x as argument and verify that it is equal to the 'val' integer and assign the left and right child nodes as null .
Now we will define a function minimum_sum_at_each_depth() which accepts an integer value as a parameter to find the minimum sum at each depth. Using an if- statement, it checks if the root value of the tree is empty and returns 0 if it is empty.
We are creating a queue pair of STL (Standard Template Library) to combine two values.
We create a queue variable named q that performs two methods as a pair, namely push() and make_pair(). Using these two methods, we insert values and construct two pairs of an object.
We are initializing three variables namely 'present_depth', 'present_sum' and 'totalSum' which will be used further to find the current sum as well as to find the total minimum sum.
After initializing the variables, we create a while loop to check the condition, if the queue pair is not empty, the count of the nodes will start from the beginning. Next, we use the 'pop()' method to remove an existing node as it will be moved to the next depth of the tree to calculate the minimum sum.
Now we will create three if statements to return the minimum sum of the sums.
After this, we will start the main function and build the tree structure of the input mode with the help of the root pointer, left and right child nodes, and pass the node value through the new 'tree_node' .
Finally, we call the 'minimum_sum_at_each_depth(root)' function and pass the parameter root to calculate the minimum sum at each depth. Next, print the statement "sum of each depth of the acyclic graph" and get the result.
Remember that a pair queue is a container containing pairs of queue elements.
The Chinese translation ofExample
is:Example
In this program we will calculate the sum of all minimum nodes for each depth.

In Figure 2, the minimum sum of the total depth is 15 8 4 1 = 13.
现在我们将把这个数字作为该程序的输入。
#include <iostream> #include <queue> // required for FIFO operation #include <utility> // required for queue pair #include <climits> using namespace std; // create the structure definition for a binary tree node of non-cycle graph struct tree_node { int val; tree_node *left; tree_node *right; tree_node(int x) { val = x; left = NULL; right = NULL; } }; // This function is used to find the minimum sum at each depth int minimum_sum_at_each_depth(tree_node* root) { if (root == NULL) { return 0; } queue<pair<tree_node*, int>> q; // create a queue to store node and depth and include pair to combine two together values. q.push(make_pair(root, 0)); // construct a pair object with two element int present_depth = -1; // present depth int present_sum = 0; // present sum for present depth int totalSum = 0; // Total sum for all depths while (!q.empty()) { pair<tree_node*, int> present = q.front(); // assign queue pair - present q.pop(); // delete an existing element from the beginning if (present.second != present_depth) { // We are moving to a new depth, so update the total sum and reset the present sum present_depth = present.second; totalSum += present_sum; present_sum = INT_MAX; } // Update the present sum with the value of the present node present_sum = min(present_sum, present.first->val); //We are adding left and right children to the queue for updating the new depth. if (present.first->left) { q.push(make_pair(present.first->left, present.second + 1)); } if (present.first->right) { q.push(make_pair(present.first->right, present.second + 1)); } } // We are adding the present sum of last depth to the total sum totalSum += present_sum; return totalSum; } // start the main function int main() { tree_node *root = new tree_node(15); root->left = new tree_node(14); root->left->left = new tree_node(11); root->left->right = new tree_node(4); root->right = new tree_node(8); root->right->left = new tree_node(13); root->right->right = new tree_node(16); root->left->left->left = new tree_node(1); root->left->right->left = new tree_node(6); root->right->right->right = new tree_node(2); root->right->left->right = new tree_node(7); cout << "Total sum at each depth of non cycle graph: " << minimum_sum_at_each_depth(root) << endl; return 0; }
输出
Total sum at each depth of non cycle graph: 28
结论
我们探讨了给定非循环图中每个深度的元素最小和的概念。我们看到箭头运算符连接节点并构建树形结构,利用它计算每个深度的最小和。该应用程序使用非循环图,例如城市规划、网络拓扑、谷歌地图等。
The above is the detailed content of Given an acyclic graph, compute the minimum sum of elements at each depth. For more information, please follow other related articles on the PHP Chinese website!

This article details C function return types, encompassing basic (int, float, char, etc.), derived (arrays, pointers, structs), and void types. The compiler determines the return type via the function declaration and the return statement, enforcing

Gulc is a high-performance C library prioritizing minimal overhead, aggressive inlining, and compiler optimization. Ideal for performance-critical applications like high-frequency trading and embedded systems, its design emphasizes simplicity, modul

This article explains C function declaration vs. definition, argument passing (by value and by pointer), return values, and common pitfalls like memory leaks and type mismatches. It emphasizes the importance of declarations for modularity and provi

This article details C functions for string case conversion. It explains using toupper() and tolower() from ctype.h, iterating through strings, and handling null terminators. Common pitfalls like forgetting ctype.h and modifying string literals are

This article examines C function return value storage. Small return values are typically stored in registers for speed; larger values may use pointers to memory (stack or heap), impacting lifetime and requiring manual memory management. Directly acc

This article analyzes the multifaceted uses of the adjective "distinct," exploring its grammatical functions, common phrases (e.g., "distinct from," "distinctly different"), and nuanced application in formal vs. informal

This article explains the C Standard Template Library (STL), focusing on its core components: containers, iterators, algorithms, and functors. It details how these interact to enable generic programming, improving code efficiency and readability t

This article details efficient STL algorithm usage in C . It emphasizes data structure choice (vectors vs. lists), algorithm complexity analysis (e.g., std::sort vs. std::partial_sort), iterator usage, and parallel execution. Common pitfalls like


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
