Topological Sort Flashcards

1
Q

Front

A

Back

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a topological sort?

A

An ordering of the vertices in a DAG such that for every directed edge u → v, vertex u comes before v in the ordering.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What type of graph allows a topological sort?

A

A directed acyclic graph (DAG).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why can’t a graph with cycles be topologically sorted?

A

Because at least one task depends on itself, making a consistent ordering impossible.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What do the edges in a DAG represent in task scheduling?

A

Dependencies between tasks; task A → B means A must be completed before B.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What algorithm is commonly used to perform a topological sort?

A

Depth-First Search (DFS).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you determine the topological order using DFS?

A

Sort nodes in descending order of their finish times during DFS.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What action is taken when a node finishes in DFS during topological sort?

A

It is inserted at the head of a list to build the final topological order.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a back edge in DFS?

A

An edge that points to an ancestor in the DFS tree, indicating a cycle.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the presence of a back edge imply?

A

That the graph has a cycle and cannot be topologically sorted.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the time complexity of topological sorting using DFS?

A

Θ(V + E), where V is the number of vertices and E is the number of edges.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the purpose of tracking discovery and finish times in DFS?

A

To identify the order of node processing and detect cycles.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can you use DFS to both traverse and sort a DAG?

A

By inserting each node into a list upon finish, creating a reverse order of finish times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a real-life example of tasks with dependencies?

A

Making tea: boil water before pouring, place teabag before adding water, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What happens if you try to topologically sort a graph with a cycle?

A

It will fail; no valid topological order exists.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you detect a cycle while doing DFS for topological sort?

A

By checking for back edges.