Computer scienceAlgorithms and Data StructuresAlgorithmsGraph algorithmsTopological sorting algorithms

Topological sort

Abstract graph

Report a typo

Let's now turn your mind to an abstract way of thinking. Imagine you have a topological order of graph's vertices. Here is the sort:

4, 5, 0, 2, 3, 1

You are also given the number of edges that come out of each node:

Node value

"0"

"1"

"2"

"3"

"4"

"5"

Number of outcome edges

0

0

1

1

2

2


Can you recover graph representation if you also know that nodes are ordered with the rule that the smallest-numbered available vertex comes first?

Let's take a look at an example:

Abstract graph

With such a small graph, the table that contains the number of edges that come out of each node will be the following:

Node value

"0"

"1"

"2"

"3"

Number of outcome edges

3

0

1

0

So, as you can see, firstly we listed all nodes in ascending order and then put down connections between nodes starting from the first input node.

Also, it should be mentioned that an order number inside round brackets is important because it shows the direction of an arrow.

Select one option from the list
___

Create a free account to access the full topic