The Wayback Machine - https://web.archive.org/web/20240118183057/https://www.geeksforgeeks.org/assign-directions-to-edges-so-that-the-directed-graph-remains-acyclic/
Open In App
Related Articles

Assign directions to edges so that the directed graph remains acyclic

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given a graph with both directed and undirected edges. It is given that the directed edges don’t form cycle. How to assign directions to undirected edges so that the graph (with all directed edges) remains acyclic even after the assignment? 

For example, in the below graph, blue edges don’t have directions.

 first 

We strongly recommend to minimize your browser and try this yourself first. The idea is to use Topological Sorting. Following are two steps used in the algorithm. 1) Consider the subgraph with directed edges only and find topological sorting of the subgraph. In the above example, topological sorting is {0, 5, 1, 2, 3, 4}. 

Below diagram shows topological sorting for the above example graph. 

second 

2) Use above topological sorting to assign directions to undirected edges. For every undirected edge (u, v), assign it direction from u to v if u comes before v in topological sorting, else assign it direction from v to u. 

Below diagram shows assigned directions in the example graph.

 third

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 27 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials