找到流程图中的所有方法?

发布于 2024-08-25 08:56:49 字数 4200 浏览 11 评论 0原文

我用 Java 做了一个流程图编辑器。它绘制流程图并将它们相互连接并创建两个数组。其中之一显示连接节点和线,另一张显示相互连接的元素。我必须找到从 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的所有方法

有2种方法

A->B->C->E

A->B ->D->E

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

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

我还尝试将 DFS 与回溯一起使用,但它并不是指显示所有路径,而是仅显示一条路径。

如果我发现 1 并开始搜索 [i,j],我尝试将下面的所有列都设置为 0,但在第二次搜索中它不会看到任何内容,并且我的 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 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 ?或者其他什么?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方的巷 2024-09-01 08:56:49

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

我强烈建议您查看图形算法的维基百科页面

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.

赠我空喜 2024-09-01 08:56:49

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

因此,我建议您查看编译器文献,以了解如何在流程图上进行操作。特别是,您将需要阅读有关“数据流分析”的内容,例如“定义使用”和“达到定义”问题。

为了回答您的问题,您可以实现有向图遍历算法,如下所示:

/* 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来执行您想要的每个节点工作。

我建议您阅读一本关于编译器优化的优秀教科书,它是 Advanced Compiler Design and实施,作者:Steven Muchnik。
Muchnik 书的图片

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文