在FlowChart图中查找所有方法?

时间:2010-03-16 22:09:24

标签: java algorithm flowchart depth-first-search

我在Java上创建了一个FlowChart图编辑器。它淹没流程图并将它们相互连接并创建了两个数组。其中一个显示连接节点和线,另一个显示相互连接的元素。我必须找到从Begin Two And开始的所有方法。 例如,如果我有一些钻石作为决定,我有两个独立的方式..我想得到所有这些方式..我必须使用哪种算法?

编辑3:已解决 嗨再次,我解决了我的问题我的自己..这是我的代码..))

 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();
}

编辑1:当我解决了我的问题时,我解决了一部分没有也有错误...这里我的问题更深入的描述: 我有一个描述元素之间连接的数组

          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     

这是我的连接数组..我正试图找到从启动A到E的所有方法

有两种方式

A-> B-> C-> E

A-> B-> D-> E

我可以找到从左到右搜索数组的第一种方式。如果我看到1我使用J的walu并转到i中的第j个元素行,请将该元素设为2并从[i,j + 1]开始搜索,如果到达E,则发送结果。

但是在这里我的问题是在第一行的第二次搜索中它不会看到1并且将进入第二行并且存在第一个元素1但它引用第一行并且它将是循环。

此外,我尝试使用DFS并使用回溯,但它并不是指显示所有路径,只有一条路径。

我已经尝试将下面的所有列都设为0如果我从1开始并且开始搜索[i,j]但是在第二次搜索时它看不到任何东西而且我的arry表来了一张空白表)。

我知道我错过了一件事,但我无法想象......

编辑2:

现在我关闭了解决方案,但问题仍然存在。我使用此代码计算矩阵

中的路径
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("");
    }

}

}

结束它的输出是

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

成立    成立    成立

[0 0,0 1 1,2 2 2,4 3 1,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意味着访问和更改..问题是你在访问列表中添加

00,01,12,24这是第一条路径,但后来只有13,34。这是因为我将数组的其余部分更改为0而不进行搜索。我怎么解决这个问题?它必须00,01,12,24和00,01或10,13,34 ..任何想法? 我不认为这是DFS还是BFS?还是别的什么?

2 个答案:

答案 0 :(得分:0)

您正在考虑的是与编译器优化器使用的分析非常接近。这些优化器不是流程图标,而是处理汇编语言指令的“基本块”。 “基本块”,就像流程图标一样,由流控制边界定义,描绘了基本块和流程图标的边界。

出于这个原因,我建议您查看编译器文献,以了解如何操作流程图。特别是,您需要阅读“数据流分析”,例如“def-use”和“达到定义”问题。

在回答您的问题时,您可以实现有向图遍历算法:

/* 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)

您可以实施calculations_on_node来执行您想要的每节点工作。

Steven Muchnik撰写的一本关于编译器优化的优秀教科书,我建议你研究一下,Advanced Compiler Design and ImplementationImage of the Muchnik Book http://ecx.images-amazon.com/images/I/51UmGvP2XDL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA240_SH20_OU01_.jpg

答案 1 :(得分:0)

Floyd-Warshall算法将计算所有顶点之间的最短路径。如果您正在寻找最短路径,只有所有路径,您可以通过在两个节点之间进行详尽的深度优先搜索来实现此目的。

我强烈建议您查看Wikipedia's page图算法。