Question
For Java: Goals: Work with graph data structures and worklists. Implement the in-degree based version of the Topological Sort algorithm. Take as input a list
For Java:
Goals: Work with graph data structures and worklists.
Implement the in-degree based version of the Topological Sort algorithm.
Take as input a list of vertices and a list of edges for a directed graph.
Create the adjacency list representation, storing the outlist and the indegree for each vertex. (Count the vertices as you do so.) Set the label for the next vertex to 0.
Traverse the vertex headers in the adjacency list, placing any vertex with indegree 0 on a worklist (use a queue).
While the worklist is not empty: remove the top vertex, give it the current label, and increment the next_label. Traverse its adjacency list, decrementing the indegree of each of its out-neighbors. If an indegree becomes zero, add that vertex to the worklist.
If next_label == the number of vertices, report the topological labeling in numerical order. If not, report that there is a cycle
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started