This document discusses topological sorting of graphs. It defines a graph as consisting of vertices and edges. A topological sort orders the vertices of a directed acyclic graph (DAG) such that if there is an edge from vertex u to vertex v, u comes before v in the ordering. The algorithm computes indegrees of vertices, selects vertices with indegree 0, removes them and updates indegrees until all vertices are ordered. Topological sorting has applications in job scheduling, spreadsheet formula evaluation, logic synthesis, data serialization, and build systems.
2. Graph
A graph is an abstract data type that is meant to
implement the undirected graph and directed
graph concepts from mathematics.
A graph data structure consists of a finite set of
vertices or nodes or points, together with a set
of unordered pairs of these vertices for an
undirected graph or a set of ordered pairs for a
directed graph.
These pairs are known as edges, arcs, or
lines for an undirected graph and as arrows,
directed edges, directed arcs, or directed
lines for a directed graph.
5. Application of Graph
1) Topological Sorting.
2) Neural networks.
Vertices represent neurons and edges the synapses between them. Neural
networks are used to understand how our brain works and how connections
change when we learn. The human brain has about 1011 neurons and close
to 1015 synapses.
3) Robot planning.
Vertices represent states the robot can be in and the edges the possible
transitions between the states. This requires approximating continuous
motion as a sequence of discrete steps. Such graph plans are used, for
example, in planning paths for autonomous vehicles
6. Topological Sorting
A topological sort or topological ordering of a directed graph
is a linear ordering of its vertices such that for every
directed edge uv from vertex u to vertex v, u comes before
v in the ordering.
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.
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.
7. Algorithm
Compute the indegrees of all vertices
Find a vertex U with indegrees 0 and print it (store it in
the ordering)
If there is no such vertex then there is a cycle
and the vertices cannot be ordered. Stop.
Remove U and all its edges (U,V) from the graph.
Update the indegrees of the remaining vertices.
Repeat steps 2 through 4 while there are vertices to be
processed.
9. Application of topological
sort
Scheduling jobs from the given dependencies among
jobs.
Ordering of formula cell evaluation when re-computing
formula values in spreadsheets.
Logic synthesis.
Data serialization.
Build Systems