0% found this document useful (0 votes)
245 views25 pages

Computer Science Directed Graph Vertices: Topological Sort or Topological Ordering of A

A topological sort is a linear ordering of the vertices in a directed graph where for every edge from vertex u to vertex v, u comes before v in the ordering. It is possible if and only if the graph has no directed cycles. Depth-first search can be used to perform a topological sort of a directed acyclic graph in linear time by visiting vertices in order of their finishing times from the DFS.

Uploaded by

Jaswinder
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
245 views25 pages

Computer Science Directed Graph Vertices: Topological Sort or Topological Ordering of A

A topological sort is a linear ordering of the vertices in a directed graph where for every edge from vertex u to vertex v, u comes before v in the ordering. It is possible if and only if the graph has no directed cycles. Depth-first search can be used to perform a topological sort of a directed acyclic graph in linear time by visiting vertices in order of their finishing times from the DFS.

Uploaded by

Jaswinder
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

TOPOLOGICAL SORT

In computer science, a topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that, for every edge uv, u comes before v in the ordering. For instance, the vertices of the graph may represent tasks to be performed, and the edges may represent constraints that one task must be performed before another; in this application, a topological ordering is just a valid sequence for the tasks. A topological ordering is possible if and only if the graph has no directed cycles, that is, if it is a directed acyclic graph (DAG). Any DAG has at least one topological ordering, and algorithms are known for constructing a topological ordering of any DAG in linear time.
Jasvinder Singh, [email protected]
1

Topological Sort: DFS


C

F H

Jasvinder Singh, [email protected]

Topological Sort: DFS


C

dfs(A)

F H

Jasvinder Singh, [email protected]

Topological Sort: DFS


C

dfs(A) dfs(D)

F H

Jasvinder Singh, [email protected]

Topological Sort: DFS


C

dfs(A) dfs(D) dfs(E)


G A B

F H

Jasvinder Singh, [email protected]

Topological Sort: DFS


C

dfs(A) dfs(D) dfs(E) dfs(F)


G A B

F H

Jasvinder Singh, [email protected]

Topological Sort: DFS


C

dfs(A) dfs(D) dfs(E) dfs(F) dfs(H)

F H

Jasvinder Singh, [email protected]

Topological Sort: DFS


C

dfs(A) dfs(D) dfs(E) dfs(F)


G A B

F H 7

Jasvinder Singh, [email protected]

Topological Sort: DFS


C

dfs(A) dfs(D) dfs(E)


G A B

H 7

Jasvinder Singh, [email protected]

Topological Sort: DFS


C

dfs(A) dfs(D)

H 7

Jasvinder Singh, [email protected]

10

Topological Sort: DFS


C

dfs(A) dfs(D)

H 7

Jasvinder Singh, [email protected]

11

Topological Sort: DFS


C

dfs(A)

D 4

H 7

Jasvinder Singh, [email protected]

12

Topological Sort: DFS


C

dfs(A)

D 4

H 7

Jasvinder Singh, [email protected]

13

Topological Sort: DFS


C

D 4

H 7

Jasvinder Singh, [email protected]

14

Topological Sort: DFS


C

dfs(B)

D 4

H 7

Jasvinder Singh, [email protected]

15

Topological Sort: DFS


C

dfs(B)

D 4

H 7

Jasvinder Singh, [email protected]

16

Topological Sort: DFS


C

D 4

H 7

Jasvinder Singh, [email protected]

17

Topological Sort: DFS


C

dfs(C)

D 4

H 7

Jasvinder Singh, [email protected]

18

Topological Sort: DFS


C

dfs(C)

D 4

H 7

Jasvinder Singh, [email protected]

19

Topological Sort: DFS


C

dfs(C)

D 4

H 7

Jasvinder Singh, [email protected]

20

Topological Sort: DFS


C

dfs(C)

D 4

H 7

Jasvinder Singh, [email protected]

21

Topological Sort: DFS


C

dfs(C) dfs(G)

D 4

H 7

Jasvinder Singh, [email protected]

22

Topological Sort: DFS


C

dfs(C)

D 4

H 7

Jasvinder Singh, [email protected]

23

Topological Sort: DFS


0 C

D 4

H 7

Jasvinder Singh, [email protected]

24

Topological Sort: DFS


0 C

D 4

H 7

Topological order: C G B A D E F H
Jasvinder Singh, [email protected]
25

You might also like