0% found this document useful (0 votes)
10 views24 pages

1 8

Topological sorting is a linear ordering of vertices in a directed acyclic graph (DAG) that respects the direction of edges, ensuring that for every directed edge U -> V, U appears before V. It is used in applications like scheduling tasks with dependencies and can be implemented using algorithms such as Depth-First Search (DFS) or Kahn's algorithm. The process is only applicable to DAGs, as cycles would prevent a clear linear ordering.

Uploaded by

Rajat krishnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views24 pages

1 8

Topological sorting is a linear ordering of vertices in a directed acyclic graph (DAG) that respects the direction of edges, ensuring that for every directed edge U -> V, U appears before V. It is used in applications like scheduling tasks with dependencies and can be implemented using algorithms such as Depth-First Search (DFS) or Kahn's algorithm. The process is only applicable to DAGs, as cycles would prevent a clear linear ordering.

Uploaded by

Rajat krishnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

TOPOLOGICAL SORT

TOPOLOGICAL SORT
DIRECTED GRAPH

A directed graph is characterized by edges that possess specific


directions, often referred to as arcs. Consider two vertices, v and
w, within such a directed graph.
TOPOLOGICAL SORT
DIRECTED GRAPH

⮚ Inedge: An edge pointing toward or entering a node.


⮚ Outedge: An edge pointing away or departing from a node. The
nodes at the receiving end of the out edges of v are termed v's
neighbors.
⮚ Indegree: The count of edges directed into a node.
⮚ Outdegree: The count of outages departing from a node.
⮚ Dependency: Stating "v depends on w" or "v is dependent on w"
signifies a directed path from w to v, where v and w are
vertices in a directed graph.
TOPOLOGICAL SORT
TOPOLOGICAL SORT

Topological sorting is a linear ordering of the vertices of a


directed acyclic graph (DAG) in such a way that for every directed
edge U 🡪 V, Then vertex U comes before V in the ordering. In
simpler terms, it's an arrangement of the nodes in a directed graph
such that every directed edge goes from left to right.
TOPOLOGICAL SORT
TOPOLOGICAL SORT

1. DAG Requirement: Topological sorting is only applicable to


Directed Acyclic Graphs (DAGs). The absence of cycles is crucial
because cycles introduce ambiguity in defining a linear ordering.

2. Ordering: The output of a topological sort is a linear ordering


of the vertices. This ordering respects the partial order defined
by the directed edges.
TOPOLOGICAL SORT
TOPOLOGICAL SORT

3. Dependencies: Topological sorting has significant applications


in scheduling tasks that have dependencies. If task B depends on
task A, then A comes before B in the topological order.
4. Algorithm: One common algorithm for topological sorting is the
Depth-First Search (DFS) algorithm. The idea is to visit a node and
mark it as visited before exploring its neighbors. The linear order
of visiting nodes gives the topological order.
TOPOLOGICAL SORT
TOPOLOGICAL SORT

5. Multiple Solutions: It's important to note that a DAG may have


more than one valid topological ordering. The crucial aspect is the
relative order of vertices connected by directed edges.

topological sorting provides a systematic way to order the


vertices of a directed acyclic graph, reflecting the dependencies
between nodes in a natural and meaningful manner.
TOPOLOGICAL SORT
EXAMPLE
TOPOLOGICAL SORT
EXAMPLE
TOPOLOGICAL SORT
EXAMPLE
TOPOLOGICAL SORT
EXAMPLE
TOPOLOGICAL SORT
KHAN’S ALGORITHM

The steps below are involved in Kahn’s algorithm:

Auxiliary variables:

⮚ An array (“temp”) to hold the result of the preprocessing stage.

⮚ A variable (“visited”) to store the number of vertices that have


been visited.

⮚ A string (“result”) to hold the topological sort order.

⮚ A queue.
TOPOLOGICAL SORT
KHAN’S ALGORITHM

Pre-processing:
Calculate the in-degree of each vertex of the graph and store them
in the array “temp”.

INDEGREE A
TOPOLOGICAL SORT
KHAN’S ALGORITHM
Actual steps:
Enqueue the vertices with the in-degree of 0.
While the queue is not empty:
1. Dequeue a vertex.
2. Add this vertex to the result.
3. Increment the “visited” variable by 1.
4. Decrement the in-degree of all its neighboring vertices by 1 in
the array “temp”.
5 Enqueue the neighboring vertices with the in-degree of 0.
If the value of the “visited” variable is equal to the number of
vertices in the graph, then the graph is indeed directed and
acyclic ​
and the result will contain the topological sort for the
graph.
KHAN’S ALGORITHM TOPOLOGICAL SORT
import java.util.*; Queue<Integer> queue = new LinkedList<>();
class Graph { for (int i = 0; i < vertices; i++) {
private Map<Integer, List<Integer>> if (totalIndegree[i] == 0) {
adjacencyList; queue.add(i);
private int vertices; }
public Graph(int vertices) { }
this.vertices = vertices; int visitedNodes = 0;
this.adjacencyList = new List<Integer> order = new
HashMap<>(); ArrayList<>();
for (int i = 0; i < vertices; i++) {
this.adjacencyList.put(i, new while (!queue.isEmpty()) {
ArrayList<>()); int u = queue.poll();
} order.add(u);
}
public void createEdge(int u, int v) { for (int i :
this.adjacencyList.get(u).add(v); adjacencyList.get(u)) {
} totalIndegree[i]--;
public void topologicalSort() {
int[] totalIndegree = new if (totalIndegree[i] == 0) {
int[vertices]; queue.add(i);
for (int i = 0; i < vertices; i++) { }
for (int j : }
adjacencyList.get(i)) { visitedNodes++;
totalIndegree[j]++; }
KHAN’S ALGORITHM TOPOLOGICAL SORT
if (visitedNodes != vertices) {
System.out.println("There's a cycle present in the Graph.\nGiven graph is not
DAG");
} else {
System.out.println(order);
}
}

public static void main(String[] args) {


Graph graph = new Graph(6);
graph.createEdge(0, 1);
graph.createEdge(0, 2);
graph.createEdge(1, 3);
graph.createEdge(1, 5);
graph.createEdge(2, 3);
graph.createEdge(2, 5);
graph.createEdge(3, 4);
graph.createEdge(5, 4);

graph.topologicalSort();
}
}
INTERVIEW QUESTIONS

1. What is Topological Sorting?

Answer: Topological Sorting is a linear ordering of the


vertices of a directed acyclic graph (DAG) in such a way
that for every directed edge u -> v, vertex u comes before
v in the ordering.
INTERVIEW QUESTIONS

3. How is Topological Sorting useful in project scheduling?

Answer: Topological Sorting helps schedule tasks that have


dependencies. If task B depends on task A, then A comes before B
in the topological order, providing a natural way to schedule
dependent tasks.
INTERVIEW QUESTIONS

4. Explain the algorithm for Topological Sorting using Depth-First


Search (DFS).

Answer: The DFS-based algorithm involves visiting a node,


marking it as visited, and then recursively exploring its
neighbors. The linear order of visiting nodes gives the
topological order.
INTERVIEW QUESTIONS

4. Explain the algorithm for Topological Sorting using Depth-First Search (

Answer: The DFS-based algorithm involves visiting a node,


marking it as visited, and then recursively exploring its
neighbors. The linear order of visiting nodes gives the
topological order.
INTERVIEW QUESTIONS

5. Can Topological Sorting be applied to graphs with cycles?

Answer: No, Topological Sorting cannot be applied to graphs


with cycles. It is specifically designed for acyclic
graphs. Cycles introduce ambiguity in defining a linear
order.
https://learn.codemithra.com
THANK
YOU

+91 78150 [email protected] www.codemithra.com


95095

You might also like