Description
Great work! You're almost done. In this stage, you will complete the topological sorting algorithm.
Let's compare the current graph with the previous one:
- Finally, no more
2. Introduction to OOPtopic at the start of the graph. It moved to105. Introduction to OOPright before the topic106. Declaring classes in Kotlin. It's actually the best place for it, the various abstract topics need to be learned right before their "in-practice" counterpart topics, andDeclaring classes in Kotlinseems to be the first topic that usesOOP. Another example is the970. Filesabstract topic and the971. Reading files in Kotlintopic that uses files in practice: they are placed one after another. - There are 1242 topics that were chosen without alphabetic comparison, and 396 that were chosen alphabetically. Compare this result to the previous algorithm in Stage 5, where there were 842 and 796 of such topics, respectively. And in Stage 4, there were 190 and 1448 of such topics, respectively, so the improvements are very much visible!
Actually, the algorithm sorts topics quite well now, but you can fine-tune it a bit more. Let's prioritize topics that have higher connectivity of dependencies and higher connectivity of topics that depend on them. Here's an example:
Higher connectivity means that topics that are more interconnected should be more important than topics that are less interconnected. In the example above, Topic 5 is visibly more interconnected than Topic 6.
The final rule is, the higher the interconnection of a set of topics is (meaning a topic's dependencies and topics that depend on it), the more important the topic is.
Calculating such importance value is a bit different from calculating the previous importance value. You just need to replace the max function with the sum function. Resulting value will grow rapidly if the topics are more interconnected, and grow slower (but also quite fast) if topics are less interconnected. It's easy for such importance value to go above million, since it grows exponentially. That's why you need to normalize it. After calculating 2 importance values (one for the topic's dependencies and the other for topics that depend on it), you should apply the following formula to every value: , where is a natural logarithm function. After that, you can sum up these 2 importance values.
An example of calculating this value (before applying the logarithm) is presented in the following picture. The first value (before |) is the importance value calculated among the topic's dependencies, and the second value (after |) is the one calculated among the topics that depend on it. Remember that the former value is calculated from roots to leafs (from top to bottom) and the latter is calculated from leafs to roots (from bottom to top).
Examples for the first importance value (before the | symbol):
- For Topic
Fthe values for its dependenciesBandCare 13 and 11, respectively. Thus, the value for TopicFwould besum(13, 11) + 1 = 25. - For Topics
0and1such value is equal to 1, since they have no dependencies and therefore the sum is 0, but you should still add 1 according to the formula.
Examples for the second importance value (after the | symbol):
- For Topic
3the value of topics that directly depend on it – Topics5and9– are 17 and 4, respectively. Thus, the value for Topic3would besum(17, 4) + 1 = 22. - For topics
D,G,F, andHsuch value is equal to 1, since they don't have topics that depend on them, and therefore the maximum is 0, but you should still add 1 according to the formula.
In the end, the importance value for Topic 5 would be , and for Topic 6 the value would be Indeed, Topic 5 is more important than Topic 6, according to this rule.
The algorithm of prioritizing topics is the following:
- In case there are multiple candidates to be enumerated in the topological sorting algorithm, you should find the ones with the highest "mainline/sideline" importance value. It's the same as you did in the previous stage.
- In case there are still multiple candidates, compare them according to the following formula:
, where
1. is the connectivity importance value that you get to know in this stage;
2. is the maximum connectivity importance value among all topics in the graph;
3. is the "sum of dependencies and dependents" importance value;
4. is the maximum "sum of dependencies and dependents" importance value among all topics in the graph. - In case there are still multiple candidates, use alphabetic order.
Objectives
In this stage, you need to apply a topological sorting algorithm using three importance values of topic as sorting factor, as described in the section above. Here are the steps you should perform:
- Download the dataset of topics and their dependencies. It's the same dataset as in the previous stages. The dataset contains dependencies only, and they are presented in the format
Topic 1 -> Topic 2, which meansTopic 2depends onTopic 1. In every line, two topics are separated by the->sign, please ignore all spaces before and after this separator. Assume that topics with the same name are essentially the same topic. Assume that there are no topics that have no dependencies at all. - Apply a topological sorting algorithm.
- Paste the resulting list of topics, each on a separate line, into the solution window. Please put a topic's number, followed by a dot and a space, before the topic's name, for example,
15. Topic 9, which means this topic should be 15th in the learning order. Enumerate topics starting from 1 and print them in the ascending order.
Examples
Notice that the fine-grained improvements described in this stage are visible only on large-scale graphs (like the dataset you are processing), not small ones. For this reason, the examples below have the same output as in the previous stage.
Example 1
For the following file:
Topic 1 -> Topic 3
Topic 1 -> Topic 4
Topic 1 -> Topic 6
Topic 2 -> Topic 4
Topic 2 -> Topic 5
Topic 4 -> Topic 3
Topic 4 -> Topic 7
Which corresponds to the following graph:
You should get the following result:
1. Topic 1
2. Topic 2
3. Topic 4
4. Topic 3
5. Topic 7
6. Topic 5
7. Topic 6
Example 2
For the following file:
Topic 0 -> Topic 1
Topic 0 -> Topic 5
Topic 1 -> Topic 2
Topic 1 -> Topic 7
Topic 2 -> Topic 3
Topic 3 -> Topic 4
Topic 5 -> Topic 2
Topic 5 -> Topic 6
Topic 6 -> Topic 7
Topic 7 -> Topic 4
Topic 8 -> Topic 2
Topic 9 -> Topic 7
Which corresponds to the following graph:
You should get the following result:
1. Topic 0
2. Topic 5
3. Topic 1
4. Topic 6
5. Topic 8
6. Topic 2
7. Topic 3
8. Topic 9
9. Topic 7
10. Topic 4
Example 3
For the following file:
Topic 0 -> Topic 2
Topic 0 -> Topic 3
Topic 0 -> Topic 4
Topic 1 -> Topic 4
Topic 1 -> Topic 6
Topic 2 -> Topic 5
Topic 3 -> Topic 5
Topic 3 -> Topic 9
Topic 4 -> Topic 9
Topic 5 -> Topic 7
Topic 5 -> Topic 8
Topic 6 -> Topic 9
Topic 6 -> Topic C
Topic 7 -> Topic A
Topic 7 -> Topic B
Topic 8 -> Topic A
Topic 8 -> Topic B
Topic 9 -> Topic C
Topic A -> Topic D
Topic A -> Topic G
Topic B -> Topic E
Topic B -> Topic F
Topic C -> Topic F
Topic C -> Topic H
Topic E -> Topic G
Which corresponds to the graph in the description of this stage, you should get the following result:
1. Topic 0
2. Topic 3
3. Topic 2
4. Topic 5
5. Topic 7
6. Topic 8
7. Topic B
8. Topic E
9. Topic A
10. Topic G
11. Topic D
12. Topic 1
13. Topic 4
14. Topic 6
15. Topic 9
16. Topic C
17. Topic F
18. Topic H