0

I am using this Tarjan algorithm in python to find circuits in a directed graph. I have tried all types of inputs but it's not working.

Here is complete code and following is the code that takes input.

if len(sys.argv) != 2:
    print "usage: echo \"v1 v2\nv1 v3\n...\" | %s num_vertices"%(sys.argv[0])

A = [[] for a in range(int(sys.argv[1]))]

for edge in sys.stdin.readlines():
    v1,v2 = edge.split(' ', 1)
    A[int(v1)].append(int(v2));

I have given following inputs:

$ echo "3" "0 1 0 2 1 0 2 0 2 1" | python cycles.py
$ echo "3 0 1 0 2 1 0 2 0 2 1" | python cycles.py
$ echo "0 1\n0 2\n1 0\n1 3\n2 0\n3 0\n3 1\n3 2" | python cycles.py //as instructed.

but none of them is working. Can somebody help in figuring out the input format or is there some bug in the program?

1 Answer 1

1

Length of sys.argv

The length of sys.argv is always going to include the name of the script itself. So for your script:

>> print len(sys.argv)
3
>> print sys.argv
['cycles.py', '3', '0 1 0 2 1 0 2 0 2 1']

Error in your script

You make a check that sys.argv must not be any other length than 2. When you pass in 2 arguments, sys.argv will have a length of 3.

if len(sys.argv) != 2:
    print "usage: echo \"v1 v2\nv1 v3\n...\" | %s num_vertices"%(sys.argv[0])

Should be

if len(sys.argv) != 3:
    print "usage: echo \"v1 v2\nv1 v3\n...\" | %s num_vertices"%(sys.argv[0])

Invoking python scripts with arguments

A much more readable way to invoke scripts would be to use this syntax:

python -i cycles.py "3" "0 1 0 2 1 0 2 0 2 1"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.