Lecture 3 - Topological Ordering Flashcards
What is a DAG?
Directed Acyclic Graph
A directed graph with no cycles.
Where is topological ordering used?
- project modelling (dependency modelling more specifically)
- finding longest path or longest weighted path
- can determine which activies are critical (i.e. on longest path)
- scheduling and PERT networks
When does a graph have a topological ordering?
Only if the directed graph is a DAG, it is impossible to have a topological ordering if the graph has a cycle
What is a source?
A vertex of in-degree 0.
What is a sink?
A vertex of out-degree 0.
What is the basic fact about a DAG.
DAG has atleast one source and one sink.
- otherwise a cycle could be built
What does a topological ordering imply?
that we label vertices 1 … n such that (u,v) < E implies label(u) < label(v)
What attributes do we need to add to each vertex in order to perform topological ordering?
A label and a count
What is a label?
The label , the lower the earlier in the order.
What is a count?
Initially it equals the in-degree of a vertex., but it is updated as the algorithm runs. It always equals the amont of incoming edges that are not labelled.
How does the algorithm work?
- initiate the count of each vertex
- add source to queue
while there is element in the queue
give label to element
for each adjacent vertex decrease the count by 1, if it is 0 add it to queue
When is a vertex labelled?
When the number of incoming edges from unlabelled vertices is 0.
Predecessors of a vertex are labelled with…
a smaller number
What is the complexity of the topological ordering algorithm?
- O(m+n) to find the in-degree, as we need to scan each adjacency list
- mainloop is executed n times so O(n)
- OVERALL IS O(M+N)
How can we determine if a digraph has a cycle?
Method 1:
if the source queue becomes empty before all vertices are labelled we have a cycle
Method 2:
(adaptation of dfs)
when at a vertex check if it connects to any vertex on the path that reached this vertex