I just started doing my bachelor thesis and what I am trying to do is to create an algorithm that will provide personalized suggestions regarding course selection based on past academic performance. I am learning and using python and till now I have created a list of courses and a list of prerequisites that looks like this:
[['CS101'], ['CS105'], ['CS106', 'CS105'], ['CS107'], ['CS130'], ['CS151', 'CS105', 'MATH101'], ['CS180'], ['CS201', 'CS151'], ['CS205', 'CS105'], ...]
So for example in order to take CS101
you do not need to have taken any other course, but for you to take CS106
you have to already have taken CS105
. Now, I have to do topological sort and even though I found some code from a similar problem that was provided here in Stack Overflow, it does not work. The solution was this (I modified only the part of my_lists
in order to work for my code):
import networkx as nx
my_lists = data.prerequisites
my_graph = nx.DiGraph()
for path in my_lists:
my_graph.add_nodes_from(path)
my_graph.add_path(path)
ts = nx.topological_sort(my_graph)
print(ts)
where data.prerequisites is the list that was created in another python file(data.py). The problem is that when I run this it gives me this error:
AttributeError: 'DiGraph' object has no attribute 'add_path'
I searched for documentation of networkx 2.4
and there is nothing indicating with what they replaced the add_path()
. There was one solution for this error but it does not work anymore since the solution was given in 2011 and the add_path()
was completely removed in 2019. I tried to use other examples from the internet but I couldn't find a proper one for lists. Should I try to find a completely different approach or there is a small easy solution like this one? Because a lot of them I cannot really understand.
See the documentation of add_path
the new way to call the method is
nx.add_path(my_graph, path)
and as additional hint, you don't need to call my_graph.add_nodes_from(path)
since the nodes are added together with the edges.