Good Graph Questions
Good Graph Questions
This algorithm can be easily checked to run in O(n + m). If the input graph
G is a DAG the queue O, once algorithm terminates, will contain a topological
order: this can be verified by observing that Q is first initialised by all sources
of G and then decreasing i(w) corresponds to “removing” vertex form G and
updating the in-degrees.
Exercise 3: Describe algorithm to find a shortest path in a edge-labelled DAG
which makes use of the topological ordering of vertices. Edge-labelled DAG an
acyclic oriented graph G = (V, E) where every edge e ∈ E has associated length
`(e). The length of a given path is then sum of lengths of all edges in it. On next
class we will study an algorithm to answer this question on graphs in general.
Solution: This is similar to the problem of counting number of paths discussed
on the class. Given graph G = (V, E) and vertex v0 first put h(v0 ) = 0. Then
process vertices in the topological order. All vertices before v0 are not reachable
from v0 and thus we can put h(v0 ) = ∞ for other vertices we can put
1
At the end of the algorithm h(v) will contain distances form v0 . As usual we
can also compute predecessors array which will identify a shortest path.
Exercise 4: Do the same to find the longest path in an edge-labelled DAG (this
is a problem we do now know how to solve effectively on general graphs).
Solution: This is the same algorithm replacing min with max
Exercise 5: We say that connected graph G = (V, E) is semi-connected if for
every pair of vertices u, v ∈ E there exists an oriented path from u to v or from
v to u (possibly both). Design linear-time algorithm to decide if given graph G
is semi-connected.
Solution: Observe that if G is DAG then it is semi-connected if and only if it
has unique topological order. We know how to solve this problem. To solve the
problem for general graphs, first construct component graph using the algorithm
from the class and then apply Exercise 1.