Question

In: Computer Science

Assume the input graph is directed but may or may not have cycle in it, that...

Assume the input graph is directed but may or may not have cycle in it, that is, may or may not be a directed acyclic graph. Consider the recursive topological sorting algorithm shown below. TopologicalSort(G) { If G has only one node v then return v. Find a node v with no incoming edge and remove v from G. Return v followed by the order returned by TopologicalSort(G). }

(a) Extend the recursive algorithm shown below so it can detect and output a cycle if the input graph G is not a directed acyclic graph. Clearly mark the extended part of the algorithm and state your reasoning behind the extension made.

Solutions

Expert Solution

(a) We notice that at any instant , if there does not exist any node v such that there is no incoming edge to v then we have a cycle in the graph. Thus, a topological sorting of the graph doesn't exist in such a case.

We maintain an adjacency list for the Directed graph, InAdj

In  InAdj , for each vertex u,we maintain all vertices v such that there is an incoming edge from v to u

We maintain a global list topoList which contains the nodes sorted in topological order .

We write the pseudo Code for recursive Topo sort below

Main(G) : // G is the input directed graph

1. Initialize InAdj

2. topoList = { }

3. val = RecurTopoSort // if val = 0 then there is cycle in the graph else graph's a DAG

4. if (val == 0)

5. print "There is cycle in the directed graph"

6. else

7. for each vertex v   topoList // print vertex v in order which they appear in topoList

8. print v  

RecurTopoSort:

1. isNodeExists = false

2. if (InAdj is empty) // if Adjacency list is empty

3. return 1 // 1 is returned when no cycle is encountered in graph G     

4. for each vertex u  InAdj

5.  if ( InAdj[u] is empty) // there exists a node u with no incoming edge

6.   isNodeExists = true

7.   Remove the node u from the all the entries in InAdj

8.   break

9. if (isNodeExists == true)

10.   topoList.push(u)

11.   return RecurTopoSort

12. else

13.   return 0


Related Solutions

Given a directed graph, prove that there exists an Eulerian cycle that is also a hamiltonian...
Given a directed graph, prove that there exists an Eulerian cycle that is also a hamiltonian cycle if and only if the graph is a single cycle.
A DAG is a directed graph that contains no directed cycles. Define G = (V, E)...
A DAG is a directed graph that contains no directed cycles. Define G = (V, E) in which V is the set of all nodes as {v1, v2, ..., vi , ...vn} and E is the set of edges E = {ei,j = (vi , vj ) | vi , vj ∈ V} . A topological order of a directed graph G = (V, E) is an ordering of its nodes as {v1, v2, ..., vi , ...vn} so that...
Someone says "All roads lead to Rome"! Given a directed graph Q (you can assume that...
Someone says "All roads lead to Rome"! Given a directed graph Q (you can assume that Q has no self-loops), define a Rome node RN to be a node m in Q such that: there is an edge from x to m for every node x != m in Q and m has no outgoing edges. n is the number of nodes in our graph Q, assume that the graph structure is stored in an adjacency matrix. what is a...
A chorded cycle in a graph is a cycle in the graph with one additional edge...
A chorded cycle in a graph is a cycle in the graph with one additional edge connecting two of the cycle vertices. Prove that every graph with minimum degree 3 contains a chorded cycle as a subgraph. (Hint: Consider a longest path in the graph. What does it tell you when a vertex is the end of a longest path? )
Write an algorithm to determine if the directed graph is strongly connected or not
Write an algorithm to determine if the directed graph is strongly connected or not
Show that if there is an Euler cycle in the graph, there is an Euler cycle...
Show that if there is an Euler cycle in the graph, there is an Euler cycle in any BCC of the graph
Prove that \strongly connected" is an equivalence relation on the vertex set of a directed graph
Prove that \strongly connected" is an equivalence relation on the vertex set of a directed graph
In this program you will read a file specifying a directed graph G as a list...
In this program you will read a file specifying a directed graph G as a list of edges. Each edge u →v is listed on one line as u v. The input file simply lists the edges in arbitrary order as pairs of vertices, with each edge on a separate line. The vertices are numbered in order from 1 to the total number of vertices. The program outputs the out-degree sequence for GSCC in increasing order. For example for the...
In this program you will read a file specifying a directed graph G as a list...
In this program you will read a file specifying a directed graph G as a list of edges. Each edge u →v is listed on one line as u v. The input file simply lists the edges in arbitrary order as pairs of vertices, with each edge on a separate line. The vertices are numbered in order from 1 to the total number of vertices. The program outputs the out-degree sequence for GSCC in increasing order. For example for the...
Use a dictionary to represent a directed graph in which each key is a pair (tuple)...
Use a dictionary to represent a directed graph in which each key is a pair (tuple) of two nodes, with the corresponding value set to the edge weight. For example, W[u,v] =42. Where in u → v. Write a function to calculate the in and out degree of a given node. What would be the advantages and disadvantages of this representation? in python
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT