Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

from collections import defaultdict import networkx as nx # Library for displaying graphs. import matplotlib.pyplot as plt import random class DependencyScheduler ( object ) :

from collections import defaultdict
import networkx as nx # Library for displaying graphs.
import matplotlib.pyplot as plt
import random
class DependencyScheduler(object):
def __init__(self):
self.tasks = set()
# The successors of a task are the tasks that depend on it, and can
# only be done once the task is completed.
self.successors = defaultdict(set)
# The predecessors of a task have to be done before the task.
self.predecessors = defaultdict(set)
self.completed_tasks = set() # completed tasks
def add_task(self, t, dependencies):
"""Adds a task t with given dependencies."""
# Makes sure we know about all tasks mentioned.
assert t not in self.tasks or len(self.predecessors[t])==0, "The task was already present."
self.tasks.add(t)
self.tasks.update(dependencies)
# The predecessors are the tasks that need to be done before.
self.predecessors[t]= set(dependencies)
# The new task is a successor of its dependencies.
for u in dependencies:
self.successors[u].add(t)
def reset(self):
self.completed_tasks = set()
@property
def done(self):
return self.completed_tasks == self.tasks
def show(self):
"""We use the nx graph to display the graph."""
g = nx.DiGraph()
g.add_nodes_from(self.tasks)
g.add_edges_from([(u, v) for u in self.tasks for v in self.successors[u]])
node_colors =[('green' if v in self.completed_tasks
else 'red' if v in self.available_tasks
else 'gray')
for v in self.tasks]
nx.draw(g, with_labels=True, node_color=node_colors)
plt.show()
@property
def uncompleted(self):
"""Returns the tasks that have not been completed.
This is a property, so you can say scheduler.uncompleted rather than
scheduler.uncompleted()"""
return self.tasks - self.completed_tasks
@property
def available_tasks(self):
"""Returns the available tasks.
This is just a placeholder; we will let you implement this."""
return set()
def mark_completed(self, t):
"""Marks that the task t is completed, and returns the set of tasks
that become available as a consequence.
This is just a placeholder, we will let you implement this."""
return set()
def _check(self):
"""We check that if t is a successor of u, then u is a predecessor
of t."""
for u in self.tasks:
for t in self.successors[u]:
assert u in self.predecessors[t]
#@title Implementation of `available_tasks` and `mark_completed`.
def scheduler_available_tasks(self):
"""Returns the set of tasks that can be done in parallel.
A task can be done if all its predecessors have been completed.
And of course, we don't return any task that has already been
completed."""
### YOUR SOLUTION HERE
def scheduler_mark_completed(self, t):
"""Marks the task t as completed, and returns the additional
set of tasks that can be done (and that could not be
previously done) once t is completed."""
### YOUR SOLUTION HERE
DependencyScheduler.available_tasks = property(scheduler_available_tasks)
DependencyScheduler.mark_completed = scheduler_mark_completed
s = DependencyScheduler()
s.add_task('a',['b','c'])
s.add_task('b',['c','e'])
s._check()
s.show()
# Tests 5 points: Simple tests.
s = DependencyScheduler()
s.add_task('a',[])
assert s.available_tasks =={'a'}
# Tests 5 points: Slightly more complicated.
s = DependencyScheduler()
s.add_task('a',['b','c'])
s.add_task('b',['c','e'])
assert s.available_tasks =={'e','c'}
s = DependencyScheduler()
s.add_task('a',['b'])
s.add_task('b',['a'])
assert s.available_tasks == set()
# Tests 5 points: Now, let's test `mark_completed`. Simple tests first.
s = DependencyScheduler()
s.add_task('a',[])
assert s.available_tasks =={'a'}
r = s.mark_completed('a')
assert r == set()
s = DependencyScheduler()
s.add_task('a',['b'])
assert s.available_tasks =={'b'}
r = s.mark_completed('b')
assert r =={'a'}
# Tests 5 points: Slightly more complicated.
s = DependencyScheduler()
s.add_task('a',['b','c'])
assert s.available_tasks =={'b','c'}
r = s.mark_completed('b')
assert r == set()
assert s.available_tasks =={'c'}
r = s.mark_completed('c')
assert r =={'a'}
s = DependencyScheduler()
s.add_task('a',['b','c'])
s.add_task('b',['c','e'])
s.add_task('c',[])
assert s.available_tasks =={'c','e'}
r = s.mark_completed('e')
assert r == set()
r = s.mark_completed('c')
assert r =={'b'}
r = s.mark_completed('b')
assert r =={'a'}
r = s.mark_completed('a')
assert r == set()
assert s.available_tasks == set()

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

Students also viewed these Databases questions