-3

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]]
CryptoFool
  • 21,719
  • 5
  • 26
  • 44
Recommence
  • 75
  • 1
  • 8
  • Please provide the expected [MRE](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results deviate from the ones you expect. We should be able to paste a single block of your code into file, run it, and reproduce your problem. Your given code is incomplete, not properly indented, and has no attempt to trace the offending value. – Prune Oct 08 '20 at 06:00

1 Answers1

0

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):
CryptoFool
  • 21,719
  • 5
  • 26
  • 44