6.10 Topological Sorting (with Examples) | How to find all Topological Orderings of a Graph

Jenny's Lectures CS IT
3 Feb 201914:18

Summary

TLDRThis video script introduces topological sorting, a linear ordering of vertices in a directed acyclic graph (DAG), where for every directed edge UV, vertex U comes before V. The script explains that a graph must be directed and acyclic to have a topological sort, and demonstrates the process of finding such an ordering by calculating the in-degree of vertices, removing those with in-degree zero, and updating the graph iteratively. It clarifies that a DAG can have multiple valid topological sorts and illustrates the method with examples, highlighting the impossibility of sorting graphs with cycles.

Takeaways

  • 📚 Topological sorting is a linear ordering of vertices in a directed graph such that for every directed edge UV, vertex U comes before V in the ordering.
  • ⛔ The graph must be directed and acyclic (DAG) for topological sorting to be possible; undirected graphs or graphs with cycles cannot have topological ordering.
  • 🔍 To perform topological sorting, start by identifying the in-degree of each vertex, which is the number of incoming edges to the vertex.
  • 🔄 Begin the sorting process by selecting vertices with an in-degree of 0 and deleting them along with their outgoing edges from the graph.
  • 🔄 After deletion, update the in-degrees of the affected vertices and repeat the process until all vertices are ordered or no vertices with in-degree 0 are left.
  • 🤔 If at any point no vertex with in-degree 0 is found and there are still vertices left, it indicates the presence of a cycle, making topological sorting impossible.
  • 🔢 In-degree is recalculated after each deletion of a vertex and its edges to reflect changes in the graph structure.
  • 🌐 A directed acyclic graph (DAG) can have more than one valid topological ordering, showcasing the flexibility in the sorting process.
  • 📉 The process involves iteratively reducing the in-degree of vertices by removing those with an in-degree of 0 and their associated edges.
  • 📈 The topological sort order is constructed incrementally, with vertices being added in the order they satisfy the in-degree of 0 condition.
  • 🔗 The presence of a cycle in a graph is a critical factor that prevents the successful completion of a topological sort, as it leads to an impasse in the sorting process.

Q & A

  • What is topological sorting?

    -Topological sorting is a linear ordering of the vertices in a directed acyclic graph (DAG) such that for every directed edge UV, vertex U comes before vertex V in the ordering.

  • What is the prerequisite for a graph to have a topological ordering?

    -A graph must be directed and acyclic to have a topological ordering. If the graph is undirected or contains a cycle, topological sorting is not possible.

  • What is the significance of in-degree in topological sorting?

    -In-degree is the number of incoming edges to a vertex. It is used to determine the starting point for topological sorting, as the vertices with in-degree of 0 are the first to be placed in the ordering.

  • How does the presence of a cycle in a directed graph affect topological sorting?

    -If a directed graph contains a cycle, it is not possible to find a topological ordering for that graph, as the presence of a cycle violates the linear ordering condition required for topological sorting.

  • Can a directed acyclic graph have more than one topological ordering?

    -Yes, a directed acyclic graph can have more than one topological ordering. The script provides examples where different selections of vertices with in-degree 0 lead to different valid orderings.

  • What is the first step in the process of finding a topological ordering of a graph?

    -The first step is to find the in-degree of each vertex in the directed graph, which will help identify the vertices that can be placed first in the topological ordering.

  • What happens when you delete a vertex from the graph during topological sorting?

    -When a vertex is deleted from the graph, all its outgoing edges are also deleted. This may change the in-degree of other vertices, which could lead to new vertices having an in-degree of 0 and being candidates for the next position in the ordering.

  • How does the script illustrate the process of topological sorting?

    -The script uses examples of directed acyclic graphs and walks through the process of identifying vertices with in-degree 0, deleting them along with their outgoing edges, and updating the in-degrees of other vertices until a topological ordering is formed.

  • Why is it necessary to update the in-degrees of other vertices after deleting a vertex and its edges?

    -Updating the in-degrees is necessary because the deletion of a vertex and its edges changes the connectivity of the graph. This may result in other vertices having their in-degree reduced to 0, making them candidates for the next step in the topological sorting.

  • What is the final result of topological sorting for a directed acyclic graph?

    -The final result is a linear ordering of all vertices such that every directed edge from vertex U to vertex V appears in the ordering as U before V. This ordering is not unique and can vary depending on the selection process.

Outlines

00:00

📚 Introduction to Topological Sorting

This paragraph introduces the concept of topological sorting, which is a linear ordering of vertices in a directed acyclic graph (DAG) such that for every directed edge UV, vertex U comes before V. The importance of the graph being acyclic is emphasized, as a cycle would make topological sorting impossible. The paragraph also mentions that a DAG can have multiple valid topological orderings. The method begins by identifying the in-degree of each vertex, starting the sorting with vertices that have an in-degree of zero, and iteratively removing them and their outgoing edges, updating the in-degrees of the affected vertices.

05:00

🔍 Step-by-Step Topological Sorting Process

The second paragraph delves into the step-by-step process of topological sorting. It explains how to update the in-degrees of vertices after removing a vertex with an in-degree of zero and its outgoing edges. The paragraph illustrates the process with examples, showing how the in-degree changes as vertices are removed and how the process continues until all vertices are placed in a topological order or until it's determined that a cycle prevents a valid ordering.

10:02

🚫 Cycle Detection in Topological Sorting

The final paragraph discusses the importance of cycle detection in determining whether a topological sort is possible. It provides examples of graphs with cycles and explains why these cannot be topologically sorted. The paragraph also demonstrates how the process of topological sorting can reveal the presence of cycles by reaching a point where no vertex has an in-degree of zero, indicating a cycle and the impossibility of a valid topological ordering.

Mindmap

Keywords

💡Topological Sorting

Topological Sorting is a linear ordering of vertices in a directed graph such that for every directed edge UV, vertex U comes before V in the ordering. It is a key concept in the video, as it is the main topic being discussed. The script explains that topological sorting is only possible for directed acyclic graphs (DAGs), and it cannot be performed on graphs with cycles. The video provides examples of how to perform topological sorting by starting with vertices that have an in-degree of zero and iteratively removing them and their associated edges until all vertices are ordered.

💡Directed Acyclic Graph (DAG)

A Directed Acyclic Graph, or DAG, is a directed graph with no cycles. It is essential in the context of topological sorting because only DAGs can be topologically ordered. The script emphasizes that the presence of a cycle in a graph makes topological sorting impossible, as demonstrated with examples where cycles prevent the completion of the sorting process.

💡In-Degree

In-Degree refers to the number of incoming edges to a vertex in a directed graph. The script explains that finding the in-degree of each vertex is the first step in the topological sorting process. Vertices with an in-degree of zero are the starting points for creating the topological order because they have no incoming edges, indicating that they can be placed at the beginning of the ordering without violating the ordering rules.

💡Out-Degree

Out-Degree is the number of outgoing edges from a vertex in a directed graph. While the script does not explicitly define out-degree, it is implied when discussing the deletion of edges after selecting a vertex with an in-degree of zero. The out-degree affects the topological sorting process as it determines how many edges need to be removed when a vertex is placed in the order.

💡Cycles

Cycles in a graph refer to a sequence of edges that starts and ends at the same vertex, forming a loop. The video script makes it clear that cycles are a critical factor in determining whether a graph can be topologically sorted. If a graph contains a cycle, as shown in the examples, topological sorting is not possible because it would violate the linear ordering rule.

💡Linear Ordering

Linear Ordering is a sequence in which elements are arranged in a line following a specific order. In the context of the video, linear ordering is synonymous with topological ordering, where vertices are placed in a sequence that respects the directed edges of the graph. The script illustrates how topological sorting creates a linear ordering that satisfies the condition that for every directed edge UV, U appears before V in the sequence.

💡Vertex

A vertex, also known as a node, is a fundamental unit of a graph, representing an entity with which edges connect to other vertices. The script frequently refers to vertices when explaining the process of topological sorting, emphasizing their role in forming the order and how their in-degrees determine the sequence of the sorting.

💡Edge

An edge in a graph is a connection between two vertices, representing a relationship or link between them. The script discusses edges in the context of directed graphs, where edges have a direction, and their orientation is crucial for topological sorting. The process involves considering the edges that are incoming to a vertex (in-edges) and outgoing from a vertex (out-edges).

💡Algorithm

Although the term 'algorithm' is not explicitly mentioned in the script, the process described for topological sorting is an algorithmic procedure. The steps of finding in-degrees, selecting vertices with zero in-degree, deleting vertices and their edges, and updating the graph are all part of an algorithm to determine the topological order of a DAG.

💡Multiple Orderings

The concept of multiple orderings refers to the possibility that a directed acyclic graph can have more than one valid topological sort. The script illustrates this by showing different sequences that satisfy the topological sorting conditions for the same graph, emphasizing that there is not a single correct answer but multiple valid solutions.

Highlights

Topological sorting is a linear ordering of vertices in a directed acyclic graph (DAG) such that for every directed edge UV, vertex U comes before V.

A graph must be directed and acyclic to perform topological sorting; undirected or cyclic graphs are not suitable.

The presence of a cycle in a directed graph makes topological ordering impossible.

Topological sorting can have multiple valid orderings for a single directed acyclic graph.

The method to find topological sorting involves identifying the in-degree of each vertex in the graph.

In-degree is the number of incoming edges to a vertex, and out-degree is the number of outgoing edges.

The topological sorting process begins by selecting vertices with an in-degree of zero.

After selecting a vertex with in-degree zero, all its outgoing edges and the vertex itself are removed from the graph.

The in-degrees of the vertices affected by the removal of edges are updated in the next step.

The process repeats, selecting the next vertex with an in-degree of zero and removing it along with its edges.

If at any point no vertex with an in-degree of zero is found, and there are still vertices with in-degree greater than zero, the graph contains a cycle.

The video provides examples to demonstrate the method of finding topological sorting for different graphs.

In the examples, it's shown how the graph's structure changes with each step of the topological sorting process.

The importance of updating in-degrees after each removal is emphasized for the correct progression of the sorting.

The video concludes by reiterating that for every directed acyclic graph, more than one topological ordering is possible.

A directed graph with a cycle cannot have a topological ordering, as demonstrated with examples.

The video explains the theoretical and practical aspects of topological sorting with clear examples.

The method is applicable in various fields such as project scheduling, task sequencing, and more.

Transcripts

play00:00

hi guys welcome back in this video I'm

play00:02

going to discuss with you what is

play00:03

topological sorting or you can also say

play00:06

topological ordering fine what is the

play00:08

topological ordering and how to find out

play00:10

topological altering of a given graph

play00:12

fine so first of all we'll just discuss

play00:15

some key points of topological sorting

play00:17

what is topological sorting a

play00:19

topological ordering okay first point is

play00:22

to find out topological ordering that

play00:25

graph should be directed and acyclic

play00:28

that is the must condition graph should

play00:32

be dug that means directed and acyclic

play00:41

graph if graph is undirected you cannot

play00:45

find out topological sorting if graph is

play00:47

directed but it contains any cycle like

play00:51

this suppose we have 1 2 & 3 3 vertices

play00:55

this 1 2 2 3 & 3 to 1 1 to 2 2 to 3 and

play01:00

3 to 1 the cycle is wrong now so such

play01:03

type of cycle contains if in graph then

play01:06

it is not possible to find out

play01:07

topological ordering of that graph so

play01:09

that graph should not contain any cycle

play01:12

not a single one fine that is must

play01:14

condition now what is topological

play01:16

sorting see topological sorting is

play01:20

basically a linear ordering linear

play01:25

ordering of the vertices in the graph

play01:28

such that for every directed edge UV so

play01:34

I see the edges from you to suppose u to

play01:37

V vertex u to V then in the or brain you

play01:42

must come before V fine I have written

play01:48

the definition here for vertex u 2 V u

play01:51

comes before vertex V in that ordering

play01:55

we will discuss it with the example then

play01:57

you will get it right and see one more

play02:00

point is for every dag if graph is

play02:02

directed in Si Si clique then at least

play02:05

one topological sorting would be there

play02:07

at least one but it is not compulsory

play02:10

that that graph will have only one

play02:11

topological sorting as

play02:13

Telegraph can have more than one

play02:15

topological sorting two three four five

play02:17

six fine any number of topological

play02:19

sorting now we'll discuss the method how

play02:22

to find out topological sorting okay

play02:26

discuss it with the help of one example

play02:30

now suppose this one is our graph see

play02:34

this is directed graph sorry this was

play02:37

directed acyclic graph or not first of

play02:39

all check it out this is directed yes

play02:41

every edge is having some direction is

play02:43

there any cycle directed cycle this this

play02:47

no this is not coming like that this

play02:49

no there is no cycle fine so topological

play02:52

sorting or topological ordering on this

play02:54

graph is possible now how to find out

play02:57

topological sorting the first step is

play03:00

very first step find out the in degree

play03:05

of each vertex in the directed graph we

play03:08

have the concept of in-degree and

play03:09

out-degree fine in degree means the

play03:12

number of edges coming to that word X

play03:14

and how degree means the number of edges

play03:16

outgoing from that vertex now find out

play03:19

in degree of every vertex indicative of

play03:22

this one is is there any edge which is

play03:24

coming to one no there are two edges

play03:27

which are outgoing from this one no I

play03:29

just coming so in degree of this one is

play03:32

zero in degree of this 2 is there is

play03:35

only one edge that is coming one in

play03:39

degree of this four is one and two edges

play03:41

are coming to four to this edges like

play03:47

this right in degree of this five is one

play03:51

in degree of three is two edges are

play03:53

coming to three one two now we have

play03:57

finally found out the in degree of every

play04:00

vertex by first step is this second step

play04:04

is you will start by writing down the

play04:08

topological ordering or you can say the

play04:10

linear or drain from the vertex having

play04:13

in degree 0 find you and choose that

play04:17

vertex first that is having in degree 0

play04:20

now find out which vertex is having in

play04:23

degree 0 1 so we will

play04:26

one okay

play04:29

now second step is we have selected this

play04:34

one fine

play04:35

so from this graph we will delete this

play04:39

one and all the edges which are outgoing

play04:43

from this one every edge so from this

play04:47

one how many edges are outgoing one into

play04:51

fine so we will delete this one and we

play04:55

will delete this this one also this one

play05:00

and this one now when we delete these

play05:03

edges then obviously the in degree of

play05:06

these vertices would be changed

play05:09

yes or no so the graph would be

play05:12

something like that we have only two we

play05:16

have four we have three we have fine

play05:19

fine two to four still we have four to

play05:23

three we have four to five we have this

play05:27

edge no we are not having one this edge

play05:30

we are not having this edge we have only

play05:32

two to three now in degree of this two

play05:35

will become what now you have to update

play05:37

the in degree of these vertices in

play05:40

degree of this two is now zero because

play05:42

first it was one this edge was deleted

play05:45

then it would be minus 1 1 minus 1 that

play05:47

is 0 it would be 2 - 1 - 1 edges had has

play05:52

been deleted now in degree of this 4s

play05:54

only this edge 1 5 is as it is 3 is in

play05:59

degrees - now we have this graph now

play06:03

again select the vertex having in degree

play06:06

0 which one is having in degree 0 2 will

play06:10

write down to here next the same step we

play06:14

will delete this too.we will delete and

play06:17

will delete the every vertex out going

play06:19

from this to this also and this also

play06:23

fine and update the degree of these

play06:26

vertices you are supposed to update that

play06:28

what degree of those in degree of those

play06:30

vertices which are being affected

play06:33

because of because of deletion of this -

play06:35

and these edges now the graph would be

play06:39

is 2

play06:39

has been deleted now we have only three

play06:41

we have four we have five yes four to

play06:45

five we have four to five million four

play06:47

to three we have do we have this edge no

play06:50

two has been deleted do we have this no

play06:52

this has also been deleted we have only

play06:54

two edges now now in degree of three

play06:56

would become first of all it was 2 minus

play06:59

1 that is 1 only for e 1 3 1 minus 1 is

play07:04

because this one has been deleted so in

play07:06

degree C you can see this there is no

play07:08

issue in coming to that vertex that is 0

play07:11

and here we have only one now again

play07:15

select the vertex having in degree 0 we

play07:18

have 4 write down this for the same step

play07:23

4 would be deleted and also the outgoing

play07:25

edges would be deleted now the graph

play07:28

would be we have only vertex 3 and 5 now

play07:32

these two edges has been deleted is

play07:34

there any edge between these 3 and 5 no

play07:37

in degree of this 3 is now 0 in degree

play07:40

of this 5 is now zero now select again a

play07:44

vertex having in degree 0

play07:46

now see here we have 2 what this is

play07:48

having in degree 0 3 & 5 so you can take

play07:52

this 3 also and you can take 5

play07:55

suppose we are taking this three now

play08:01

three has been deleted now you can take

play08:03

this 5 or the second ordering is

play08:06

possible that is 1 2 4

play08:09

yata cassity's and after that first of

play08:12

all we will take this 5 and then we'll

play08:14

take this 3 so 2 topological ordering of

play08:19

topological ordering is possible off for

play08:22

this graph alright we'll take one more

play08:26

example suppose we have this kind of

play08:32

graph 1 2 3

play08:42

and 5c now find out the topological

play08:48

ordering of this graph see the

play08:53

topological ordering of this graphs

play08:54

you'll not be able to find out why so

play08:57

because this graph is having one

play08:59

directed cycle see this then this then

play09:02

this this is a cycle and we have already

play09:06

discussed in these properties that if a

play09:08

graph is having any cycle then you would

play09:10

not be able to find topological ordering

play09:12

for that graph so let us see is it

play09:15

possible is it true or not same step

play09:18

find out in degree of every vertex in

play09:20

degree of 1 is 1 in degree of this 2 is

play09:23

2 1 & 2 2 edges are coming to that

play09:26

vertex 3 is 1 & 2 2 edges are coming for

play09:31

5 in degree 0 for 4 in degree 0 select

play09:35

the vertex having in degree 0 you can

play09:37

select either 4 or 5 it's up to you

play09:39

suppose we are selecting for case 1 is

play09:44

this 4 or case to me you can select fine

play09:47

that's why I have told you for a graph

play09:51

directed acyclic graph many topological

play09:53

ordering are possible we are taking this

play09:56

case suppose we have choosen this for

play09:59

now after choosing this for delete this

play10:01

4 and also this vertex now the graph

play10:05

would be 1 2 3 this one this one and

play10:10

this one now this one has been deleted

play10:13

but we have this 5 now in degree of this

play10:17

5 is same in degree of 3 is still two in

play10:20

degree of 1 is still 1 but in degree of

play10:22

2 is now 1 la too--they this has been

play10:25

deleted then 2 minus 1 that is 1 C 1

play10:27

edge now again select a vertex I mean

play10:31

degree 0 we have 5 suppose we have taken

play10:33

this 5 now delete this 5 and this edge

play10:37

now the graph would be 1 2 & 3 this one

play10:44

this one and this one now 5 has been

play10:48

deleted

play10:48

now put 8 the in degree of every vertex

play10:50

1 1

play10:53

and still 2-1 deleted my one now is

play10:57

there any vertex having in degree zero

play11:00

no every vertex is having degree one one

play11:02

one so we cannot proceed further that is

play11:06

why I am saying if a graph contains a

play11:09

cycle then it would not be possible to

play11:11

find out topological ordering because if

play11:13

graph contains a cycle then at some

play11:16

point you will do it you will reach

play11:18

tonight some situations then at that

play11:22

time you will not have any vertex you

play11:25

will not find any vertex having in

play11:26

degree zero okay so that is the case you

play11:32

can take one more example suppose we are

play11:36

having this kind of graph now we are

play11:37

supposed to find out topological sorting

play11:40

of this graph find out first step in

play11:42

degree of every vertex in degree of is

play11:44

zero no I just coming in degree of D is

play11:47

one two three in degree of C is zero in

play11:50

degree of D is one to select a vertex

play11:53

having in degree zero you can select

play11:54

either C or you can select hey so case

play11:58

one is we have selected a and suppose

play12:01

case two we have selected C now

play12:06

case one we have selected a now after

play12:09

selection of a these would be deleted

play12:12

okay and the graph would be something

play12:15

like that we have B we have C we have D

play12:19

this one this one and this one and these

play12:24

edges has been related and update the in

play12:28

degree of every vertex sorry or you can

play12:31

see the update in degree of vertex which

play12:32

are being affected because of this

play12:34

deletion in degree of B now become 2 and

play12:37

B become 1 and C in degrees 0 now select

play12:42

a vertex having in degree 0 we have only

play12:45

C has been selected delayed this delayed

play12:48

this fine after deletion of Bees edges

play12:52

in degree of D would become 0 and in

play12:55

degree of B would become 1 then we would

play12:58

select in degree zero vertex having in

play13:01

degree 0 that is d after

play13:04

the selection of did this edge is also

play13:06

be deleted then we'll select right or

play13:11

the next case was suppose you have

play13:14

selected this C then delete this and

play13:17

this and the graph would be a b and b we

play13:23

are having this edge we are having this

play13:25

edge we are having this one update in

play13:28

degree this is having zero same this

play13:31

will be having to 3 minus 1 this will be

play13:33

having 1 now we will select a because in

play13:38

vertex a is having in degree 0

play13:41

now delete this this one and this one

play13:43

you have selected this a nor delete the

play13:45

outgoing edges of the say also and this

play13:47

vertex also now in degree of this would

play13:50

be updated to 0 after this deletion by

play13:53

an integral of this B would be updated

play13:55

to 1 now you can select after that you

play14:00

can select B and you can select now D so

play14:03

these are 2 topological ordering

play14:05

possible of this graph right so for

play14:09

every directed acyclic graph more than

play14:11

one topological ordering is possible

play14:13

okay I'll say in the next video

play14:15

till then bye bye take care

Rate This

5.0 / 5 (0 votes)

Related Tags
Topological SortingDirected GraphsAcyclic GraphsGraph TheoryAlgorithmsLinear OrderingIn-DegreeOut-DegreeCycles in GraphsData StructuresEducational Content