CSES Solutions - Acyclic Graph Edges Last Updated : 25 Jun, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an undirected graph, the task is to choose a direction for each edge so that the resulting directed graph is acyclic. You can print any valid solution. Example:Input: n = 3, m = 3, edge = {{1, 2}, {2, 3}, {3, 1}}Output: 1 22 31 3Explanation: Connecting the graph in this manner will result in directed acyclic graph. Input: n = 4, m = 4, edge = {{1, 2}, {2, 3}, {3, 4}, {4, 1}} Output: 1 22 33 41 4 Explanation: Connecting the graph in this manner will result in directed acyclic graph. Approach: To solve the problem, follow the idea below: The idea is to direct the edges from smaller vertex to largest one, to make the graph acyclic. It ensures that all edges are directed in a way that prevents cycles. Since each edge is directed from a smaller node to a larger node, it’s impossible to go back to a smaller node from a larger node, which prevents any cycles from forming. Step-by-step algorithm: Loop through each edge.For each edge, check if the first vertex is greater than the second vertex.If so, swap the vertices to ensure the edge is directed from the smaller vertex to the larger vertex.Below is the implementation of the algorithm: C++ // C++ code #include <bits/stdc++.h> using namespace std; vector<pair<int, int> > diracycgraph(vector<pair<int, int> >& edge, int m) { // Direct all the edges from smaller vertex to larger // one for (int i = 0; i < m; i++) if (edge[i].first > edge[i].second) swap(edge[i].first, edge[i].second); return edge; } // Driver code int main() { int n = 3, m = 3; vector<pair<int, int> > edge{ { 1, 2 }, { 2, 3 }, { 3, 1 } }; vector<pair<int, int> > res = diracycgraph(edge, m); for (int i = 0; i < m; i++) cout << res[i].first << " " << res[i].second << endl; } Output1 2 2 3 1 3 Time Complexity: O(n), where n is the number of nodes.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Directed Acyclic Graph in Compiler Design (with examples) A abhishek9202 Follow Improve Article Tags : Competitive Programming CSES Problems Similar Reads CSES Solutions â Reachable Nodes A directed acyclic graph consists of n nodes and m edges. The nodes are numbered 1,2,â¦,n.Calculate for each node the number of nodes you can reach from that node (including the node itself).Examples:Input: n = 5, m = 6, edges = {{1, 2}, {1, 3}, {1, 4}, {2, 3}, {3, 5}, {4, 5}}Output: 5 3 2 2 1Explana 8 min read Directed Acyclic Graph in Compiler Design (with examples) In compiler design, a Directed Acyclic Graph (DAG) plays a crucial role in representing expressions and optimizing code. A DAG is a graph containing directed edges but no cycles, ensuring no path leads back to the starting node. DAGs are particularly useful in eliminating redundant computations and 4 min read What is Cyclic Graph? A cyclic graph is defined as a graph that contains at least one cycle which is a path that begins and ends at the same node, without passing through any other node twice. Formally, a cyclic graph is defined as a graph G = (V, E) that contains at least one cycle, where V is the set of vertices (nodes 3 min read Dynamic Programming (DP) and Directed Acyclic Graphs (DAG) Pre-Requisite:What is Directed Graph? | Directed Graph meaningDynamic Programming (DP) Tutorial with Problems Every Dynamic Programming problem can be represented as a Directed Acyclic Graph(DAG). The nodes of the DAG represent the subproblems and the edges represents the transitions between the sub 15+ min read POTD Solutions | 19 Octâ 23 | Level of Nodes View all POTD Solutions Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Graphs but will also help you build up problem-solving 5 min read Check if adding an edge makes the Undirected Graph cyclic or not Given an undirected graph, the task is to if adding an edge makes the graph cyclic or not. In an Undirected graph, a cycle is a path of edges that connects a sequence of vertices back to itself. In other words, a cycle is a closed loop of edges that allows you to traverse the graph and return to the 15+ min read Like