Print nodes in a directed graph that do not belong to any cycle
In coordination diagrams, identifying hubs that do not belong to any cycle is crucial for different applications. These centers form the basis of acyclic subgraphs and play an important role in understanding the general graph structure. By using efficient graph intersection calculations, such as Profundity First Hunt (DFS) or Tarjan's calculation of closely related parts, we can effortlessly decide and print hubs that do not participate in any loops. These methods ensure the characterization of centers without circular collaboration, provide important knowledge for the non-circular parts of diagrams, and support different critical thinking situations related to diagrams.
usage instructions
Depth-first search (DFS) with loop detection
Tarjan’s strongly connected component algorithm
Depth-first search (DFS) with loop detection
In this approach, we use depth-first tracking (DFS) to navigate the coordination chart and distinguish cycles on the way. We mark visited centers and keep a list so that centers can be tracked in an ongoing DFS manner. If we hit a trailing edge (reaching the edge of the hub in a sustained DFS manner), we differentiate a cycle. At the end of DFS, the center in the ongoing DFS way will be important for a cycle. Hubs that do not use persistent DFS are not part of any loop and can be printed.
algorithm
Perform a Deep First Hunt (DFS) from each unvisited center on the chart.
During DFS, visited hubs are marked and added to the ongoing DFS path list.
If we encounter a trailing edge (an edge to a hub in the current DFS mode), we distinguish a cycle and mark all hubs in the current DFS mode as part of the cycle.
When DFS for a hub is complete, remove it from the list of in-progress DFS paths.
After completing the DFS of all hubs, the hubs that do not belong to any cycle will remain unchanged and we can print them.
Example
#include <iostream> #include <vector> class Graph { public: Graph(int numVertices); void addEdge(int src, int dest); void DFS(); private: void DFSUtil(int v, std::vector<bool>& visited, std::vector<int>& dfsPath); int numVertices; std::vector<std::vector<int>> adjList; }; Graph::Graph(int numVertices) : numVertices(numVertices) { adjList.resize(numVertices); } void Graph::addEdge(int src, int dest) { adjList[src].push_back(dest); } void Graph::DFSUtil(int v, std::vector<bool>& visited, std::vector<int>& dfsPath) { visited[v] = true; dfsPath.push_back(v); for (int neighbor : adjList[v]) { if (!visited[neighbor]) { DFSUtil(neighbor, visited, dfsPath); } else { std::cout << "Cycle found: "; for (size_t i = 0; i < dfsPath.size(); ++i) { if (dfsPath[i] == neighbor) { while (i < dfsPath.size()) { std::cout << dfsPath[i] << " "; ++i; } break; } } std::cout << std::endl; } } dfsPath.pop_back(); } void Graph::DFS() { std::vector<bool> visited(numVertices, false); std::vector<int> dfsPath; for (int i = 0; i < numVertices; ++i) { if (!visited[i]) { DFSUtil(i, visited, dfsPath); } } } int main() { Graph graph(6); graph.addEdge(0, 1); graph.addEdge(1, 2); graph.addEdge(2, 3); graph.addEdge(3, 4); graph.addEdge(4, 1); graph.addEdge(4, 5); std::cout << "DFS traversal with cycle detection:\n"; graph.DFS(); return 0; }
Output
DFS traversal with cycle detection: Cycle found: 1 2 3 4
Tarjan’s strongly connected component algorithm
Tarjan's calculation is a powerful calculation used to track all key related parts of the coordination diagram. Explicitly related parts are subsets of hubs for which coordination exists between any two hubs in the subset. A hub that is not part of any closely related component is not part of any cycle. By finding key associated parts we can identify hubs that do not belong to any cycle and print them\
algorithm
Apply Tarjan's calculations to the bootstrapping diagram to track all key relevant parts.
After tracing all the important related parts, distinguish the centers that are crucial for the closely related parts.
Hubs that do not belong to any explicitly associated parts do not belong to any loop and can be printed.
Both methods do differentiate and print centers that do not belong to any cycle in the coordination chart. The DFS method provides a simpler and more straightforward implementation, while Tarjan's calculations are more complex but provide additional data on focused correlation parts, which can be helpful for specific chart-related tasks. The decision on approach depends on the specific needs and the context of the main pressing issues.
Example
#include <iostream> #include <vector> #include <stack> #include <algorithm> using namespace std; class Graph { int V; vector<vector<int>> adj; vector<bool> visited; vector<int> disc, low; stack<int> st; vector<vector<int>> SCCs; vector<bool> essentialNodes; public: Graph(int V) : V(V) { adj.resize(V); visited.resize(V, false); disc.resize(V, -1); low.resize(V, -1); essentialNodes.resize(V, true); } void addEdge(int u, int v) { adj[u].push_back(v); } void tarjanDFS(int u) { static int time = 0; disc[u] = low[u] = ++time; st.push(u); visited[u] = true; for (int v : adj[u]) { if (disc[v] == -1) { tarjanDFS(v); low[u] = min(low[u], low[v]); } else if (visited[v]) { low[u] = min(low[u], disc[v]); } } if (low[u] == disc[u]) { vector<int> SCC; int v; do { v = st.top(); st.pop(); SCC.push_back(v); visited[v] = false; } while (v != u); SCCs.push_back(SCC); } } void tarjan() { for (int i = 0; i < V; ++i) { if (disc[i] == -1) { tarjanDFS(i); } } } void identifyEssentialNodes() { for (const vector<int>& SCC : SCCs) { for (int v : SCC) { for (int u : adj[v]) { if (find(SCC.begin(), SCC.end(), u) == SCC.end()) { essentialNodes[u] = false; } } } } } void printEssentialNodes() { cout << "Essential Nodes for Each SCC:\n"; for (int i = 0; i < V; ++i) { if (essentialNodes[i]) { cout << i << " "; } } cout << endl; } }; int main() { Graph g(6); g.addEdge(0, 1); g.addEdge(1, 2); g.addEdge(2, 0); g.addEdge(1, 3); g.addEdge(3, 4); g.addEdge(4, 5); g.addEdge(5, 3); g.tarjan(); g.identifyEssentialNodes(); g.printEssentialNodes(); return 0; }
Output
Essential Nodes for Each SCC: 0 1 2 4 5
in conclusion
These two methods do solve the problem of identifying centers that do not belong to any cycle in the coordination chart. The DFS method is easy to implement and does not require many additional information structures. Tarjan's calculations, on the other hand, provide additional data on key correlation components, which may be helpful in certain situations.
The decision between the two methods depends on the specific prerequisites of the problem and the requirements for additional data passing through period-independent differentiation centers. In general, if the only goal is to find hubs that do not belong to any cycle, the DFS approach may be favored for its simplicity. Nonetheless, Tarjan's calculations may be an important tool if further examination of key relevant parts is required. Both methods provide proficient arrangements and can be adapted to the properties of the coordination chart and the desired outcome of the exam
The above is the detailed content of Print nodes in a directed graph that do not belong to any cycle. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The history and evolution of C# and C are unique, and the future prospects are also different. 1.C was invented by BjarneStroustrup in 1983 to introduce object-oriented programming into the C language. Its evolution process includes multiple standardizations, such as C 11 introducing auto keywords and lambda expressions, C 20 introducing concepts and coroutines, and will focus on performance and system-level programming in the future. 2.C# was released by Microsoft in 2000. Combining the advantages of C and Java, its evolution focuses on simplicity and productivity. For example, C#2.0 introduced generics and C#5.0 introduced asynchronous programming, which will focus on developers' productivity and cloud computing in the future.

The future development trends of C and XML are: 1) C will introduce new features such as modules, concepts and coroutines through the C 20 and C 23 standards to improve programming efficiency and security; 2) XML will continue to occupy an important position in data exchange and configuration files, but will face the challenges of JSON and YAML, and will develop in a more concise and easy-to-parse direction, such as the improvements of XMLSchema1.1 and XPath3.1.

C Reasons for continuous use include its high performance, wide application and evolving characteristics. 1) High-efficiency performance: C performs excellently in system programming and high-performance computing by directly manipulating memory and hardware. 2) Widely used: shine in the fields of game development, embedded systems, etc. 3) Continuous evolution: Since its release in 1983, C has continued to add new features to maintain its competitiveness.

C The core concepts of multithreading and concurrent programming include thread creation and management, synchronization and mutual exclusion, conditional variables, thread pooling, asynchronous programming, common errors and debugging techniques, and performance optimization and best practices. 1) Create threads using the std::thread class. The example shows how to create and wait for the thread to complete. 2) Synchronize and mutual exclusion to use std::mutex and std::lock_guard to protect shared resources and avoid data competition. 3) Condition variables realize communication and synchronization between threads through std::condition_variable. 4) The thread pool example shows how to use the ThreadPool class to process tasks in parallel to improve efficiency. 5) Asynchronous programming uses std::as

C interacts with XML through third-party libraries (such as TinyXML, Pugixml, Xerces-C). 1) Use the library to parse XML files and convert them into C-processable data structures. 2) When generating XML, convert the C data structure to XML format. 3) In practical applications, XML is often used for configuration files and data exchange to improve development efficiency.

C's memory management, pointers and templates are core features. 1. Memory management manually allocates and releases memory through new and deletes, and pay attention to the difference between heap and stack. 2. Pointers allow direct operation of memory addresses, and use them with caution. Smart pointers can simplify management. 3. Template implements generic programming, improves code reusability and flexibility, and needs to understand type derivation and specialization.

The modern C design model uses new features of C 11 and beyond to help build more flexible and efficient software. 1) Use lambda expressions and std::function to simplify observer pattern. 2) Optimize performance through mobile semantics and perfect forwarding. 3) Intelligent pointers ensure type safety and resource management.

C Learners and developers can get resources and support from StackOverflow, Reddit's r/cpp community, Coursera and edX courses, open source projects on GitHub, professional consulting services, and CppCon. 1. StackOverflow provides answers to technical questions; 2. Reddit's r/cpp community shares the latest news; 3. Coursera and edX provide formal C courses; 4. Open source projects on GitHub such as LLVM and Boost improve skills; 5. Professional consulting services such as JetBrains and Perforce provide technical support; 6. CppCon and other conferences help careers
