Question
1. How to Use Ocaml Language to write a topological sort Your program must take a list of dependent tasks and either output a valid
1. How to Use Ocaml Language to write a topological sort
Your program must take a list of dependent tasks and either output a valid order in which to perform them or the single word cycle.
Your program will accept a number of lines of textual input (via standard input). There are no command line arguments you must always read from standard input. Do not open a named file. Instead, always read from standard input.
The text input will contain a non-zero but even number of lines. Every two lines represent a pair of tasks. The first line gives the name of the task, the second line gives the name of the task that it depends on. This text input is also called the task list.
The task list will contain only standard ASCII characters (no UTF-8 Unicode or special accents). The goal is to rest programming and program language concepts, not your internationalization abilities.
Each task name starts at the beginning of the line and extends all the way up to (but not including) the end of that line. So, the newline or carriage return characters or are not part of the task list name.
Example task list:
learn OCaml read the OCaml tutorial do exercise 1 learn OCaml
The interpretation is that in order to learn OCaml one must first read the OCaml tutorial and that in order to do exercise 1 one must first learn OCaml. Desired output for this example:
read the OCaml tutorial learn OCaml do assignment 1
If the task list contains a cycle of any size, then your program should output exactly and only the word cycle. Example cyclic input:
get a job have experience have experience work on a job work on a job get a job
Even if the task list contains a few non-cyclic parts, any single cycle forces you to output only the word cycle.
Always output to standard output only. Do not write anything to standard error.
There is no fixed limit on the number of lines in the task list (although it is not zero and it is even).
Two tasks with the same name are really just the same task. Use standard string equality.
Duplicated pairs of tasks are not allowed. For example:
learn OCaml read the OCaml tutorial do assignment 1 learn OCaml learn OCaml read the OCaml tutorial
is not valid input because the pair learn OCaml/read the OCaml tutorial appears twice. Program behavior if the task contains duplicate pair is undefined. You will not be tested on it.
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