user338027
user338027

Reputation: 51

algorithm diagram

This is the max searching algorithm diagram: alt text http://img-photo.apps.zing.vn/upload/original/2010/05/17/17/1274093752543903925_574_574.jpg

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 !!!

Upvotes: 1

Views: 1627

Answers (2)

Edmund
Edmund

Reputation: 10819

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.

Upvotes: 2

Konrad Rudolph
Konrad Rudolph

Reputation: 545588

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.

Upvotes: 1

Related Questions