Here I'm using DAG to solve Job scheduling problem.
from collections import defaultdict
class JobGraph:
def __init__(self):
self.graph = defaultdict(list)
self.indegree = defaultdict(int)
self.visited = set()
def addEdge(u, v):
self.graph(u).append(v)
try:
self.indegree[v] += 1
except:
self.indegree[v] = 1
def topologicalSort(jobs, deps, queue = [], order = [], flag = 0):
# Write your code here.
if flag == 0:
g = JobGraph()
for dep in deps:
g.addEgde(dep[0], dep[1])
for job in jobs:
if g.indegree[job] == 0:
queue.append(job)
order.append(queue[0])
root = queue[0]
del queue[0]
for neighbour in self.graph[root]:
g.indegree[neighbour] -= 1
if g.indegree[neighbour] == 0 and neighbour not in g.visited:
queue.append(neighbour)
if len(queue) == 0:
return order
else:
topologicalSort(jobs, deps, queue, order, 1)
But the error I'm getting is
'JobGraph' object has no attribute 'addEgde'
Traceback (most recent call last):
File "/tester/json_wrapper.py", line 8, in run
actual = program.topologicalSort(inputs["jobs"][:], aelib.deepCopy(inputs["deps"]))
File "/tester/program.py", line 20, in topologicalSort
g.addEgde(dep[0], dep[1])
AttributeError: 'JobGraph' object has no attribute 'addEgde'
I know it's a syntactic issue I just don't know how to solve it
Input example
jobs = [1,2,3,4]
deps = [[1,2], [1,3], [3,2], [4,2], [4,3]]
Your code seems to have other problems, but the one that's causing this error is simple and obvious. You define a method named addEdge
like this:
def addEdge(u, v):
but then you are calling a method named addEgde
, like this:
g.addEgde(dep[0], dep[1])
You simply have a typo. addEdge != addEgde
. You've reversed the g
and the d
.
BTW, the signature of that method should probably be:
def addEdge(self, u, v):
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.