Topological Sort in Interviews

Topological Sort is used to find a linear ordering of elements that have dependencies on each other. For example, if event ‘B’ is dependent on event ‘A’, ‘A’ comes before ‘B’ in topological ordering.

This pattern defines an easy way to understand the …


This content originally appeared on DEV Community and was authored by SalahElhossiny

Topological Sort is used to find a linear ordering of elements that have dependencies on each other. For example, if event ‘B’ is dependent on event ‘A’, ‘A’ comes before ‘B’ in topological ordering.

This pattern defines an easy way to understand the technique for performing topological sorting of a set of elements.

The pattern works like this:

Initialization

a) Store the graph in adjacency lists by using a HashMap

b) To find all sources, use a HashMap to keep the count of in-degrees

Build the graph and find in-degrees of all vertices
a) Build the graph from the input and populate the in-degrees HashMap.

Image description

Find all sources
a) All vertices with ‘0’ in-degrees will be sources and are stored in a Queue.
Sort
a) For each source, do the following things:
—i) Add it to the sorted list.
 — ii)Get all of its children from the graph.
 — iii)Decrement the in-degree of each child by 1.  
— iv)If a child’s in-degree becomes ‘0’, add it to the sources Queue.

b) Repeat (a), until the source Queue is empty.
How to identify the Topological Sort pattern:

The problem will deal with graphs that have no directed cycles

If you’re asked to update all objects in a sorted order
If you have a class of objects that follow a particular order

Problems featuring the Topological Sort pattern:
Task scheduling
Minimum height of a tree


This content originally appeared on DEV Community and was authored by SalahElhossiny


Print Share Comment Cite Upload Translate Updates
APA

SalahElhossiny | Sciencx (2022-07-02T08:55:49+00:00) Topological Sort in Interviews. Retrieved from https://www.scien.cx/2022/07/02/topological-sort-in-interviews/

MLA
" » Topological Sort in Interviews." SalahElhossiny | Sciencx - Saturday July 2, 2022, https://www.scien.cx/2022/07/02/topological-sort-in-interviews/
HARVARD
SalahElhossiny | Sciencx Saturday July 2, 2022 » Topological Sort in Interviews., viewed ,<https://www.scien.cx/2022/07/02/topological-sort-in-interviews/>
VANCOUVER
SalahElhossiny | Sciencx - » Topological Sort in Interviews. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/02/topological-sort-in-interviews/
CHICAGO
" » Topological Sort in Interviews." SalahElhossiny | Sciencx - Accessed . https://www.scien.cx/2022/07/02/topological-sort-in-interviews/
IEEE
" » Topological Sort in Interviews." SalahElhossiny | Sciencx [Online]. Available: https://www.scien.cx/2022/07/02/topological-sort-in-interviews/. [Accessed: ]
rf:citation
» Topological Sort in Interviews | SalahElhossiny | Sciencx | https://www.scien.cx/2022/07/02/topological-sort-in-interviews/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.