Hi @ashwanimalviya, welcome to our community!
I recommend you look at our list of good practices. I think you used the blockquote instead of triple backticks to post your code and it lost its identation, making it harder to read. If you use triple backticks instead the spacing is preserved (and a monospace font is used).
If you break your code in smaller functions the error message will probably be more precise. For now it is hard to understand which exact line of your problem that is throwing the error.
Giving your code a look, I found strange that you have M, N = size[DAG]
, did you mean M, N = size(DAG)
? Do you have some data to test you function with that throws the same error?