2010-03-16 59 views
1

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

编辑3:再解决 嗨,我解决我的问题我self..Here我的代码..))

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:正如我woreked对我的问题我解决了一个部分不也有失误......在这里我的问题更深入的描述: 我已经描述元件之间的连接的阵列

  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  

这是我的C onnection阵列..我想找到起始于A到E

有2路

A-> B-> C->电子

A-> B-> D-所有的方法> E

我可以第一种方式从左到右搜索数组。如果我看到1,我拿走了J的walu e,然后转到i的第J条元素行,使该元素2从[i,j + 1]开始搜索,如果达到E,则发送结果。

但是,在这里我的问题是在第一行中搜索它不会看到1,并会去第二行,并有第一个元素1,但它是指第一行,它将循环。

另外我试图使用DFS与使用回溯,但它并没有指代显示所有路径,只有一个路径。

而且我试图让所有下面的专栏为0,如果我创建1并开始seaching [i,j],但在第二个seach它将不会看到任何东西,我的arry table来一张空白表))。

我知道我缺少一个东西,但我不能算出它..

编辑2:

现在我关闭的解决方案,但有问题againg。我用这个代码从矩阵

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 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表示已访问并更改..问题就像你在访问列表中添加的那样

00,01,12,24这是第一个路径,但后来只有13,34。这是因为我将其余数组更改为0而不搜索。我该如何解决这个问题?它必须00,01,12,24和00,01或10,13,34 ..任何想法? 而我不认为这是DFS或BFS?或者是其他东西??

回答

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 Implementation
Image 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

+0

谢谢你的回答。但我有问题。你能用java编写你的例子吗?或者而不是foreach循环与for循环?可能是这个愚蠢的问题,但我不明白什么时使用foreach循环.. :(( – Ercan 2010-03-16 22:36:28

+0

kk,我改变了foreach循环,但我坚持Python的伪代码,因为我在Java中没有多少年没有涉及到,你可能会把上面的代码翻译成Java,但是我可以把它转换成Java。 – 2010-03-16 23:16:56

+0

Meko:在继续之前学习如何使用foreach,它通常是更好的风格。 – reinierpost 2010-03-16 23:31:09

0

Floyd-Warshall算法将计算所有顶点之间的最短路径。如果你是而不是寻找最短路径,只需要所有的路径,你可以通过在你的两个节点之间进行彻底的深度优先搜索来实现这一点。

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