tags:

views:

79

answers:

2
+1  Q: 

algorithm diagram

This is the max searching algorithm diagram: alt text

So, I wonder how can draw diagram for HaNoi Tower program:

package tunl; 

public class TowersApp {
    static int n = 3;

    public static void main(String[] args) {
        TowersApp.doTowers(3, 'A', 'B', 'C');
    }

    public static void doTowers(int n, char from, char inter, char to) {
        if (n == 1) {
            System.out.println("disk 1 from  "+ from + "  to  " + to);
        } else {
            doTowers(n-1, from, to, inter);
            System.out.println("disk  " + n + "  from  " + from + "  to  " + to);
            doTowers(n-1, inter, from, to);
        }
    }
}

I can't draw it. Anyone can help me !!!

+1  A: 

Recursive implementations cannot be modelled directly by this type of flow diagram.1) One solution would be to rewrite the algorithm to be non-recursive and then draw the diagram.

I admit that this isn’t very satisfactory.


1) Well, technically they can but they’re no longer useful because they no longer represent the control flow and data stack implied by the recursive call.

Konrad Rudolph
+2  A: 

Your conundrum is that the algorithm is recursive. One option is to translate it into an iterative algorithm with an explicit stack. But this loses some of the high level structure of the algorithm. It would be nice to show the recursive structure.

I am unaware of a standard flow-chart idiom for recursion, but what I'd draw is the basic algorithm as flow chart, and put a box around it with the same name as is used in the calls. Since the calls have slightly different parameters from the parent call, you could assign these to new variables which are then passed as parameters. The call itself can be labelled "CALL doTowers" or similar.

Edmund