Question: class Main { public static void main(String args[]) { // A graph Graph g1=new Graph(6); g1.addEdge(0, 1); g1.addEdge(0, 5); g1.addEdge(2, 1); g1.addEdge(5, 4); g1.addEdge(4, 2);
![class Main { public static void main(String args[]) { // A](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f38b6f6aff7_97466f38b6edf465.jpg)
class Main { public static void main(String args[]) { // A graph Graph g1=new Graph(6); g1.addEdge(0, 1); g1.addEdge(0, 5); g1.addEdge(2, 1); g1.addEdge(5, 4); g1.addEdge(4, 2); g1.addEdge(4, 3); // A graph that contains a cycle Graph g2=new Graph(6); g2.addEdge(0, 1); g2.addEdge(1, 5); g2.addEdge(2, 1); g2.addEdge(4, 2); g2.addEdge(4, 3); g2.addEdge(5, 4); // Question 4: Comment here } } // A Java program to print topological sorting of a graph using indegrees // src: https://www.geeksforgeeks.org/topological-sorting-indegree-based-solution/ import java.util.*; //Class to represent a graph class Graph { int V;// No. of vertices //An Array of List which contains //references to the Adjacency List of //each vertex List
Lab 4 - Topological Sort Download Graph.java and Main.java from Blackboard. Create a Java Project; put all downloaded Java files in the same package. In this assignment you are already given an algorithm for topological sorting. 1. Run the topological sorting algorithm on Graph gi defined in main. Note down the result somewhere. [10 pts] 2. For a topological sort to exist, the graph must not contain any cycles. Modify topologicalSort () method any way you like to handle such cases. The method now must print "A cycle detected!" in such a case. Test your code on Graph g2. (30 pts) 3. Current implementation of topologicalSort () uses queues. Implement a new topologicalSortStack() method so that the underlying data structure is now a stack. [30 pts) 4. Test this modified version with Graph g1. Is the result different from before? Does it mean your code is wrong? Explain in detail as a comment in your source code. [30 pts)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
