Question: Programming: Directed Graphs Consider the task of checking to see if a directed graph contains a cycle. This task can, of course, be accomplished with

 Programming: Directed Graphs Consider the task of checking to see if

Programming: Directed Graphs Consider the task of checking to see if a directed graph contains a cycle. This task can, of course, be accomplished with a recursive DFS algorithm. Unfortunately, for very large graphs, it is not appropriate to use recursion to explore, because it requires a vast amount of system memory (to track each recursive call). Implement a method for checking for a cycle (given a starting node) in a graph using the DFS algorithm WITHOUT using recursion. Assume that the Stack ADT is available and you are using the standard DiGraph ADT discussed in class (show below for reference). The Stack ADT will support the Iterable interface. (Huge Hint: start by thinking about BFS, and how it works without recursion.) public class Digraph Digraph(int V) create a V-vertex digraph with no edges Digraph(In in) read a digraph from input stream in int VO number of vertices int EO number of edges void addEdge(int v, int w) add edge v->w to this digraph Iterable adj(int v) vertices connected to v by edges pointing from v Digraph reverse() reverse of this digraph String toString() string representation //Also assume this global exists has been up to be the size of the graph //and with every index set to false. public static boolean[] marked; public static boolean hasCycleFromStart(DiGraph G, int start) { //TODO: implement this method

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!