From ca090daabe2f4c082c9129b0b6a9723f5e9c7183 Mon Sep 17 00:00:00 2001 From: josch Date: Thu, 11 Apr 2013 10:23:18 +0200 Subject: [PATCH] read edges from stdin instead of argv --- README.md | 26 ++++++++++++++++---------- cycles.py | 8 ++++---- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 3cc7f05..1525b3a 100644 --- a/README.md +++ b/README.md @@ -11,25 +11,26 @@ Algorithm by R. Tarjan Usage ----- - python cycles.py 4 0,1 0,2 1,0 1,3 2,0 3,0 3,1 3,2 + echo "0 1\n0 2\n1 0\n1 3\n2 0\n3 0\n3 1\n3 2" | python cycles.py -First argument is the number of vertices. Subsequent arguments are ordered -pairs of comma separated vertices that make up the directed edges of the +First argument is the number of vertices. Ordered pairs of space separated +vertices are given via standard input and make up the directed edges of the graph. DOT file input -------------- For simplicity, there is no DOT file parser included but the following allows -to create a suitable argument string for simple DOT graphs. +to create a suitable argument string and standard input for simple DOT graphs. Given a DOT file of a simple (no labels, colors, styles, only pairs of -vertices...) directed graph, the following line produces commandline -arguments in the above format for that graph. +vertices...) directed graph, the following lines generate the number of +vertices as well as the edge list expected on standard input. - echo `sed -n -e '/^\s*[0-9]\+;$/p' graph.dot | wc -l` `sed -n -e 's/^\s*\([0-9]\) -> \([0-9]\);$/\1,\2/p' graph.dot` + sed -n -e '/^\s*[0-9]\+;$/p' graph.dot | wc -l + sed -n -e 's/^\s*\([0-9]\) -> \([0-9]\);$/\1 \2/p' graph.dot -The above line works on DOT files like the following: +The above lines works on DOT files like the following: digraph G { 0; @@ -42,6 +43,11 @@ The above line works on DOT files like the following: 2 -> 1; } -It would produce the following output: +They would produce the following output: - 3 0,1 0,2 1,0 2,0 2,1 + 3 + 0 1 + 0 2 + 1 0 + 2 0 + 2 1 diff --git a/cycles.py b/cycles.py index 4a3c71a..bcf4bb9 100644 --- a/cycles.py +++ b/cycles.py @@ -61,13 +61,13 @@ procedure circuit_enumeration; import sys -if len(sys.argv) < 3: - print "usage: %s num_vertices [v1,v2...]"%(sys.argv[0]) +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.argv[2:]: - v1,v2 = edge.split(',', 1) +for edge in sys.stdin.readlines(): + v1,v2 = edge.split(' ', 1) A[int(v1)].append(int(v2)); def print_point_stack():