Java 在流程图中查找所有方法?
我用Java做了一个流程图编辑器。它驱动flowscharts并将它们相互连接,并创建两个数组。其中一个显示连接节点和线,另一个显示相互连接的元素。我必须从第二步开始,找到所有的方法。 例如,如果我有一些钻石做决定,我有两种不同的方法。我想得到所有这些方法。我必须使用哪些算法 编辑3:已解决 你好,我自己解决了我的问题……这是我的代码……) 这是我的连接阵列。我正在尝试找到从A到E的所有方法 有两条路 A->B->C->E A->B->D->E 我能找到从左到右搜索数组的第一种方法。如果我看到1,我取了J的值e,然后转到I中的第J个元素行,生成该元素2,并从[I,J+1]开始搜索,如果到达e,则发送结果 但这里我的问题是在第一行的第二次搜索中,它看不到1,将转到第二行,有第一个元素1,但它指的是第一行,它将是循环 此外,我还尝试将DFS与使用回溯一起使用,但它并不是指显示所有路径,而是仅显示一条路径 如果我找到1并开始搜索[I,j],我试图将下面的所有列都设置为0,但在第二次搜索中,它什么也看不到,我的arry表是空白表) 我知道我错过了一件事,但我想不起来 编辑2: 现在我接近解决方案,但问题又出现了。我使用这段代码从矩阵计算路径Java 在流程图中查找所有方法?,java,algorithm,flowchart,depth-first-search,Java,Algorithm,Flowchart,Depth First Search,我用Java做了一个流程图编辑器。它驱动flowscharts并将它们相互连接,并创建两个数组。其中一个显示连接节点和线,另一个显示相互连接的元素。我必须从第二步开始,找到所有的方法。 例如,如果我有一些钻石做决定,我有两种不同的方法。我想得到所有这些方法。我必须使用哪些算法 编辑3:已解决 你好,我自己解决了我的问题……这是我的代码……) 这是我的连接阵列。我正在尝试找到从A到E的所有方法 有两条路 A->B->C->E A->B->D->E 我能找到从左到右搜索数组的第一种方法。如果我看到1
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("");
}
}
创立
成立
创立
[0,01,12,24,13,34]
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?或者其他一些东西???您所想的与编译器优化器使用的分析非常接近。这些优化器处理汇编语言指令的“基本块”,而不是流程图图标。与流程图图标一样,“基本块”由流程控制边缘定义,该边缘描绘了基本块和流程图图标的边界 因此,我建议您查看编译器文献,了解如何操作流程图。特别是,您需要了解“数据流分析”,例如“def使用”和“达到定义”问题 在回答您的问题时,您可以实现有向图遍历算法,如下所示:
/* 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)
/*将所有流程图图标标记为“未访问”*/
对于(int i=0;i
您可以在节点上执行计算\u
以执行您想要的每个节点的工作
史蒂文·穆奇尼克(Steven Muchnik)是一本关于编译器优化的优秀教科书,我建议您仔细阅读。该算法将计算所有顶点之间的最短路径。如果您不是在寻找最短路径,而是所有路径,那么您可以通过在两个节点之间进行彻底的深度优先搜索来实现这一点
我强烈建议大家看一看图形算法。谢谢你们的回答。但我有个问题。你们能用java写你们的例子吗?或者用for循环代替foreach循环?这可能是一个愚蠢的问题,但在使用foreach循环时我什么都不懂..:((kk,我更改了foreach循环,但我坚持使用类似Python的伪代码,因为我已经多年没有在Java中涉猎过了。不过,您可能能够将上述内容转换为Java。Meko:在继续之前先学习如何使用foreach,这通常是更好的风格。我尝试使用您的想法,但我无法拒绝
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 2 0 0 0
0 0 2 2 0
0 0 0 0 2
0 0 0 0 2
0 0 0 0 0
/* 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)