Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are given a set of data that describes a directed graph. The goal is to either output 'order' and print the nodes of a

You are given a set of data that describes a directed graph. The goal is to either output 'order' and print the nodes of a given graph in topological order, or if there is a cycle in the graph, output 'cycle' and print the nodes in the cycle. Write this with python. In Python in order to write to the standard input and output, write import sys at the beginning of your program. Then the standard input and output are existing file streams with names sys.stdin, sys.stdout.

The sample input will be in a file, in the following format: in the first line, the first number is the number of nodes and the second number is the number of edges. Every line after that will specify an edge in the following format: the first number is the index of starting vertex, space, the second number is the index of ending vertex.

for example:

8 9 0 3 7 2 1 2 4 3 5 2 6 5 6 7 1 5 2 1

and the output could be:

cycle

5

2

1

or another correct output:

cycle

1

2

if there is no cycle in the given data, output the order of the data like this:

Order

1

2

3

4

5

(just an example)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions