Answered step by step
Verified Expert Solution
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 DependencySchedulerobject:
def initself:
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 defaultdictset
# The predecessors of a task have to be done before the task.
self.predecessors defaultdictset
self.completedtasks set # completed tasks
def addtaskself t dependencies:
Adds a task t with given dependencies."""
# Makes sure we know about all tasks mentioned.
assert t not in self.tasks or lenselfpredecessorst "The task was already present."
self.tasks.addt
self.tasks.updatedependencies
# The predecessors are the tasks that need to be done before.
self.predecessorst setdependencies
# The new task is a successor of its dependencies.
for u in dependencies:
self.successorsuaddt
def resetself:
self.completedtasks set
@property
def doneself:
return self.completedtasks self.tasks
def showself:
We use the nx graph to display the graph."""
g nxDiGraph
gaddnodesfromselftasks
gaddedgesfromu v for u in self.tasks for v in self.successorsu
nodecolors green if v in self.completedtasks
else 'red' if v in self.availabletasks
else 'gray'
for v in self.tasks
nxdrawg withlabelsTrue, nodecolornodecolors
pltshow
@property
def uncompletedself:
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.completedtasks
@property
def availabletasksself:
Returns the available tasks.
This is just a placeholder; we will let you implement this."""
return set
def markcompletedself 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 checkself:
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.successorsu:
assert u in self.predecessorst
#@title Implementation of availabletasks and markcompleted
def scheduleravailabletasksself:
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 schedulermarkcompletedself 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.availabletasks propertyscheduleravailabletasks
DependencyScheduler.markcompleted schedulermarkcompleted
s DependencyScheduler
saddtaskabc
saddtaskbce
scheck
sshow
# Tests points: Simple tests.
s DependencyScheduler
saddtaska
assert savailabletasks a
# Tests points: Slightly more complicated.
s DependencyScheduler
saddtaskabc
saddtaskbce
assert savailabletasks ec
s DependencyScheduler
saddtaskab
saddtaskba
assert savailabletasks set
# Tests points: Now, let's test markcompleted Simple tests first.
s DependencyScheduler
saddtaska
assert savailabletasks a
r smarkcompleteda
assert r set
s DependencyScheduler
saddtaskab
assert savailabletasks b
r smarkcompletedb
assert r a
# Tests points: Slightly more complicated.
s DependencyScheduler
saddtaskabc
assert savailabletasks bc
r smarkcompletedb
assert r set
assert savailabletasks c
r smarkcompletedc
assert r a
s DependencyScheduler
saddtaskabc
saddtaskbce
saddtaskc
assert savailabletasks ce
r smarkcompletede
assert r set
r smarkcompletedc
assert r b
r smarkcompletedb
assert r a
r smarkcompleteda
assert r set
assert savailabletasks set
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