Question

In: Computer Science

Suppose we are given an arbitrary digraph G = (V, E) that may or may not...

Suppose we are given an arbitrary digraph G = (V, E) that may or may not be a DAG. Modify the topological ordering algorithm so that given an input G, it outputs one of the two things:

a. A topological ordering thus establishing that G is a DAG.

b. A cycle in G thus establishing that it is not a DAG.

The runtime of your algorithm should be O(m+n) where m = |E| and n = |V|

Solutions

Expert Solution

Answer:-

Given an arbitrary digraph G = (V,E)

a) A topological ordering thus it establishing that G :-

The algorithm that we know is a Topological - Sort (G)

  • First select v G.V with no incoming edges
  • Output v
  • G.V = G.V-v
  • Topological- Sort (G)

Modified algorithm:-

For every Iteration, we find a node with no incoming edges and then we will succeed in producing a topological ordering.

If in some iteration, it finds that every node has at least one incoming edge then the graph must contain a cycle.

b. A cycle in G thus establishing that it is not a DAG:-

If every node has at least one incoming edge then the graph must contain a cycle.

Now, follow the steps

Step 1:- Follow an edge into the node and do this repeatedly i.e., [lEl].

Step 2:- Since every node has an incoming edge, and do this repeatedly until we revisit a node 'v' for the first time i.e., [lVl].

Step 3:-Now, the set of nodes encountered between these 2 successive Visits(i.e., step 1 and step 2) is a cycle. Then,

  • First visit , it will take O(IEl) to loop through every edge.
  • Second visit, it will take O(lVl) to loop through every node.

Hence, The algorithm runs in O(lEl + IVl) = O(m+n). [ m = |E| and n = |V|]


Related Solutions

Use the weighted digraph for problems below: V = {a, b, c, d, e, f, g,...
Use the weighted digraph for problems below: V = {a, b, c, d, e, f, g, h} E = {(a,b,6), (a,d,3), (b,c,2), (b,e,5), (c,f,4), (d,e,9), (d,g,1), (e,f,7), (e,g,8), (e,h,2), (f,h,4), (g,h,4)} (3 pts.) What is the length of the longest path from a to h? Show the path! (2 pts.) Does the graph contain a cycle? Justify your answer! (3 pts.) Give the adjacency matrix for the graph. (4 pts.) Provide the order in which nodes would be visited in...
Given an undirected graph G=(V, E) with weights and a vertex , we ask for a...
Given an undirected graph G=(V, E) with weights and a vertex , we ask for a minimum weight spanning tree in G where is not a leaf (a leaf node has degree one). Can you solve this problem in polynomial time? Please write the proof.
Given two graphs G = [V ; E] and G0 = [V 0 ; E0 ],...
Given two graphs G = [V ; E] and G0 = [V 0 ; E0 ], and an isomorphism, f : V → V 0 , and making direct use of the formal definition for isomorphism: (a) Explain why G and G0 must have the same number of vertices. (b) Explain why G and G0 must have the same number of edges. (c) Explain why G and G0 must have the same degree sequences. (d) Given two vertices, u, v...
Determine, for a given graph G =V,E and a positive integer m ≤ |V |, whether...
Determine, for a given graph G =V,E and a positive integer m ≤ |V |, whether G contains a clique of size m or more. (A clique of size k in a graph is its complete subgraph of k vertices.) Determine, for a given graph G = V,E and a positive integer m ≤ |V |, whether there is a vertex cover of size m or less for G. (A vertex cover of size k for a graph G =...
If G = (V, E) is a graph and x ∈ V , let G \...
If G = (V, E) is a graph and x ∈ V , let G \ x be the graph whose vertex set is V \ {x} and whose edges are those edges of G that don’t contain x. Show that every connected finite graph G = (V, E) with at least two vertices has at least two vertices x1, x2 ∈ V such that G \ xi is connected.
You are given an undirected graph G = ( V, E ) in which the edge...
You are given an undirected graph G = ( V, E ) in which the edge weights are highly restricted. In particular, each edge has a positive integer weight from 1 to W, where W is a constant (independent of the number of edges or vertices). Show that it is possible to compute the single-source shortest paths in such a graph in O(E+V) time.
# Problem Description Given a directed graph G = (V,E) with edge length l(e) > 0...
# Problem Description Given a directed graph G = (V,E) with edge length l(e) > 0 for any e in E, and a source vertex s. Use Dijkstra’s algorithm to calculate distance(s,v) for all of the vertices v in V. (You can implement your own priority queue or use the build-in function for C++/Python) # Input The graph has `n` vertices and `m` edges. There are m + 1 lines, the first line gives three numbers `n`,`m` and `s`(1 <=...
Prove that the following problem is in NP: Given a digraph G over n vertices (n...
Prove that the following problem is in NP: Given a digraph G over n vertices (n is an even number), does G contain 2 vertex-disjoint simple cycles in which each cycle is of length n/2?
# Problem Description Given a directed graph G = (V, E), find the number of connected...
# Problem Description Given a directed graph G = (V, E), find the number of connected components in G. # Input The graph has `n` vertices and `m` edges. There are m + 1 lines, the first line gives two numbers `n` and `m`, describing the number of vertices and edges. Each of the following lines contains two numbers `a` and `b` meaning there is an edge (a,b) belong to E. All the numbers in a line are separated by...
ii. Let G = (V, E) be a tree. Prove G has |V | − 1...
ii. Let G = (V, E) be a tree. Prove G has |V | − 1 edges using strong induction. Hint: In the inductive step, choose an edge (u, v) and partition the set vertices into two subtrees, those that are reachable from u without traversing (u, v) and those that are reachable from v without traversing (u, v). You will have to reason why these subtrees are distinct subgraphs of G. iii. What is the total degree of a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT