views:

701

answers:

2

Hi All... I made an FlowChart diagram editor on Java. It It drows flowscharts and connect them each other and creates me two array. One of it shows connection nodes and lines , other shows connnecting elements eachother. I have to find all ways from starting Begin Two And . For example if I have some diamond for decision I have two seperate way ..I want to get all this ways..Which algorithms I must use?

Edit 3:SOLVED Hi again and i solved my problem my self..Here my codes .. ))

 public void search(){
  //  System.out.print(map.length);

for(i=0;i<map.length;i++)

    visit[i]=0;

    visit[0]=1;

    find(0,map.length-1,1);
}



  public void  find(int i,int d,int step){

for(int j=0;j<map.length;j++){
 System.out.println(">>"+i+"->"+j);
    if(visit[j]!=0 || map[i][j]==0)

        continue;

    if(j==d){
        visit[j]=step;
        OutputCycle();
      visit[j]=0;
        return;

    }
System.out.println(""+i+" to "+j);
    visit[j]=step;
    find(j,d,step+1);
    visit[j]=0;




}


  }
public void OutputCycle(){
    System.out.println("OUTPUT");

    for(k=0;k<visit.length;k++){

         for(int i=0;i<visit.length;i++){
             if(visit[i]==k+1){
                 System.out.print(i);
             }
         }
    }
    System.out.println();
}

Edit 1: As I woreked on my problem I solved one part no there is also mistakes... Here my problem deeper description : I have an array that describes connection between elements

          j

       A  B  C  D  E 

    A  0  1  0  0  0 

    B  1  0  1  1  0 

i   C  0  1  0  0  1

    D  0  1  0  0  1

    E  0  0  1  1  0     

This is my connection array ..I am trying to find all ways from starting A to E

There is 2 way

A->B->C->E

A->B->D->E

I canfind first way which searchin array from left to rigt. If I see 1 I took walu e of J and go to J`th element line in i,make that element 2 and start searchign from [i,j+1] and if reached E then send result.

But here my problem is in econd search in first line it wont see 1 and will go second line and there is first element 1 but it refers to first line and it will be loop.

Also I tried to use DFS with using backtrack but it doesnt refer to show all paths ,only one path.

And I have tried to making all below column to 0 if i foun 1 and start seaching [i,j] but in second seach it wont see anything and my arry table comes a blank table )).

I know I am missing one thing but I cant figure it ..

Edit 2:

Now I closed to solution but there is problem againg. I used this code for calculating paths from matrix

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
*
* @author Meko
*/
public class Main {

List visited = new ArrayList();
List allObjects = new ArrayList();
int map[][] = {{3, 1, 0, 0, 0},
    {1, 0, 1, 1, 0},
    {0, 1, 0, 0, 3},
    {0, 1, 0, 0, 3},
    {0, 0, 1, 1, 0}};
int i, j, k;

public Main() {

    ShowArray();
    System.out.println();
    find(0, 0);
    System.out.println();
    result();
    System.out.println();
    afterFind();
    System.out.println();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here

    new Main();

}

public void ShowArray() {

    for (int i = 0; i < map.length; i++) {
        for (int j = 0; j < map.length; j++) {

            System.out.print(" " + map[i][j]);
        }
        System.out.println("");
    }


}

public void find(int sRow, int sCol) {


    for (i = sRow; i < map.length; i++) {
        for (j = sCol; j < map.length; j++) {


            if (map[i][j] == 1) {
                map[i][j] = 2;
                visited.add(" " + i + " " + j);
                for (k = i; k < map.length; k++) {
                    map[k][i] = 0;

                }
                find(j, i);
            } else if (map[i][j] == 3) {
                visited.add(" " + i + " " + j);
              for (k = i; k < map.length; k++) {
                    map[k][i] = 0;

                }
                System.out.println("Founded");
                map[i][j] = 2;
                find(0, 0);
            }
        }

    }
}

public void result() {
    System.out.println(visited);
}

public void afterFind() {

    for (int i = 0; i < map.length; i++) {
        for (int j = 0; j < map.length; j++) {

            System.out.print(" " + map[i][j]);
        }
        System.out.println("");
    }

}

}

End it`s output is

3 1 0 0 0
1 0 1 1 0
0 1 0 0 3
0 1 0 0 3
0 0 1 1 0

Founded Founded Founded

[ 0 0, 0 1, 1 2, 2 4, 1 3, 3 4]

0 2 0 0 0
0 0 2 2 0
0 0 0 0 2
0 0 0 0 2
0 0 0 0 0

2 means visited and changed.. Problem is as you se in visited list it adds

00 , 01 , 12, 24 this is first path but then only 13,34 .this is because I change rest of array to 0 to not search. How can I solve this? it must 00,01,12,24 and 00,01 or 10,13,34.. Any Idea?? And I dont figure this is DFS or BFS ? or some thing else??

A: 

What you are thinking about is very close to the kind of analysis that compiler optimizers use. Instead of flowchart icons, these optimizers work on "basic blocks" of assembly language instructions. The "basic blocks", just like flowchart icons are defined by flow-control edges which delineate the boundaries of both basic blocks and flowchart icons.

For this reason, I recommend you look at the compiler literature to get ideas for how you can operate on your flowchart graph. In particular, you will want to read about "dataflow analysis" such as "def-use" and "reaching definitions" problems.

In answer to your question, you can implement a directed-graph traversal algorithm as so:

/* Marks all flowchart icons as "unvisited." */
for (int i = 0; i < nodes.Count(); i++):
   node[i].visited = false

/* Schedule the Start node for processing. */
node_queue.push(nodes.start_node())

/* Traverse the graph. */
while (node_queue.not_empty()):
    current_node = node_queue.pop_front()

    calculations_on_node(current_node)

    current_node.visited = true

    for (int i = 0; i < current_node.outgoing_edges.Count(); i++):
        edge  = current_node.outgoing_edges[i]
        if (!edge.destination_node.visited):
            node_queue.push_back(edge.destination_node)

You can implement calculations_on_node to perform the per-node work you are intending.

An excellent text book on compiler optimizations, which I suggest you look into, is Advanced Compiler Design and Implementation, by Steven Muchnik.
Image of the Muchnik Book

Heath Hunnicutt
Thanks for answer.But I have question.Can you write your example in java ? or instead of foreach loop with for loop?May be this is stupid question but I dont understand nothing when using foreach loop.. :((
Meko
kk, I changed the foreach loop, but I am sticking with the Python-like pseudocode because I haven't waded around in Java for too many years. You will probably be able to translate the above to Java, though.
Heath Hunnicutt
Meko: learn how to use foreach before you continue, it's usually better style.
reinierpost
I tryed to use your idea but i couldent impemen it
Meko
A: 

The Floyd-Warshall algorithm will compute shortest paths between all vertices. If you're not looking for shortest paths, just all paths, you can achieve this with an exhaustive depth-first search between your two nodes.

I'd highly recommend looking at Wikipedia's page of graph algorithms.

Pestilence