我试图通过将起始节点和目标节点作为输入来查找节点的所有可能路径。
我正在从文本文件中读取所有节点的所有可能路径,然后过滤可能的路径。
我在使用System.out.println函数时获取输出。但是在将输出设置为JTextArea
时遇到了问题。
我在下面添加了我的代码。
Graph.java
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
public class Graph {
private Map<String, LinkedHashSet<String>> map = new HashMap();
public void addEdge(String node1, String node2) {
LinkedHashSet<String> adjacent = map.get(node1);
if(adjacent==null) {
adjacent = new LinkedHashSet();
map.put(node1, adjacent);
}
adjacent.add(node2);
}
public void addTwoWayVertex(String node1, String node2) {
addEdge(node1, node2);
addEdge(node2, node1);
}
public boolean isConnected(String node1, String node2) {
Set adjacent = map.get(node1);
if(adjacent==null) {
return false;
}
return adjacent.contains(node2);
}
public LinkedList<String> adjacentNodes(String last) {
LinkedHashSet<String> adjacent = map.get(last);
if(adjacent==null) {
return new LinkedList();
}
return new LinkedList<String>(adjacent);
}
}
Search.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Search extends JFrame{
public static Graph graph;
public JFrame jPanel;
public JLabel headingLabel,startLabel,endLabel,possiblePathLabel;
public JTextField startField,endField;
public JTextArea possiblePathArea;
public JButton findButton;
JScrollPane jsp;
private static String START = "";
private static String END = "";
List<String> stringList=new ArrayList<String>();
public Search(){
jPanel=new JFrame("Optimal Path Computation");
jPanel.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
headingLabel=new JLabel("Optimal Path Computation");
startLabel=new JLabel("Source Node");
endLabel=new JLabel("Destination Node");
possiblePathLabel=new JLabel("Possible Paths");
startField=new JTextField();
endField=new JTextField();
possiblePathArea=new JTextArea(16,58);
findButton=new JButton("Find possible path");
}
public void launchFrame(){
jPanel.setLayout(null);
jPanel.setVisible(true);
jPanel.setExtendedState(JFrame.MAXIMIZED_BOTH);
jPanel.add(headingLabel);
headingLabel.setSize(270,50);
headingLabel.setLocation(550, 100);
headingLabel.setHorizontalAlignment(JLabel.CENTER);
jPanel.add(startLabel);
startLabel.setSize(270,50);
startLabel.setLocation(375, 150);
startLabel.setHorizontalAlignment(JLabel.CENTER);
jPanel.add(endLabel);
endLabel.setSize(270,50);
endLabel.setLocation(375, 225);
endLabel.setHorizontalAlignment(JLabel.CENTER);
jPanel.add(startField);
startField.setSize(270,30);
startField.setLocation(775, 150);
startField.setHorizontalAlignment(JLabel.CENTER);
jPanel.add(endField);
endField.setSize(270,30);
endField.setLocation(775, 225);
endField.setHorizontalAlignment(JLabel.CENTER);
jPanel.add(findButton);
findButton.setSize(170,30);
findButton.setLocation(600, 325);
findButton.setHorizontalAlignment(JLabel.CENTER);
findButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
START=startField.getText();
END=endField.getText();
shortFunc();
}
});
jPanel.add(possiblePathLabel);
possiblePathLabel.setSize(170,30);
possiblePathLabel.setLocation(600, 375);
possiblePathLabel.setHorizontalAlignment(JLabel.CENTER);
jPanel.add(possiblePathArea);
possiblePathArea.setSize(300, 200);
possiblePathArea.setLocation(600, 425);
}
public static void main(String[] args) {
// this graph is directional
Search ss=new Search();
ss.launchFrame();
BufferedReader br=null;
String line="";
String eachLine="";
List<String> list = new ArrayList<String>();
graph = new Graph();
try{
br=new BufferedReader(new FileReader("D://Dataset.txt"));
while(line!=null){
list.add(line);
line=br.readLine();
}
String[] stringArr = list.toArray(new String[0]);
for(int i=1;i<=stringArr.length-1;i++){
String eachLinesArray[]=stringArr[i].split(",");
graph.addEdge(eachLinesArray[0], eachLinesArray[1]);
}
}
catch(Exception e){
e.printStackTrace();
}
}
public static void shortFunc(){
LinkedList<String> visited = new LinkedList();
visited.add(START);
new Search().breadthFirst(graph, visited);
}
private void breadthFirst(Graph graph, LinkedList<String> visited) {
LinkedList<String> nodes = graph.adjacentNodes(visited.getLast());
// examine adjacent nodes
for (String node : nodes) {
if (visited.contains(node)) {
continue;
}
if (node.equals(END)) {
visited.add(node);
printPath(visited);
visited.removeLast();
break;
}
}
// in breadth-first, recursion needs to come after visiting adjacent nodes
for (String node : nodes) {
if (visited.contains(node) || node.equals(END)) {
continue;
}
visited.addLast(node);
breadthFirst(graph, visited);
visited.removeLast();
}
}
private void printPath(LinkedList<String> visited) {
for (String node : visited) {
possiblePathArea.append(node+"\n");
System.out.print(node);
System.out.print(" ");
}
System.out.println();
}
}
从开始和结束节点作为文本字段输入时使用System.out.println()输出:
B E
B A C E
B A C F E
B F E
B F C E
答案 0 :(得分:2)
问题与
有关public static Graph graph;
并在此处显示
public static void shortFunc(){
LinkedList<String> visited = new LinkedList();
visited.add(START);
new Search().breadthFirst(graph, visited);
}
基本上,您正在创建Search
的全新实例,该实例与屏幕上可见的窗口完全无关。
static
不是你的朋友。
首先删除针对static
的{{1}}延迟,并要求Graph
提供Search
个实例,作为其初始化的一部分......
Graph
接下来,更改您的public class Search extends JFrame {
private Graph graph;
//...
public Search(Graph graph) {
this.graph = graph;
方法,先加载main
,然后再加载窗口......
Graph
接下来将public static void main(String[] args) {
// this graph is directional
BufferedReader br = null;
String line = "";
String eachLine = "";
List<String> list = new ArrayList<String>();
Graph graph = new Graph();
try {
br = new BufferedReader(new FileReader("Data.txt"));
while (line != null) {
list.add(line);
line = br.readLine();
}
String[] stringArr = list.toArray(new String[0]);
for (int i = 1; i <= stringArr.length - 1; i++) {
String eachLinesArray[] = stringArr[i].split(",");
graph.addEdge(eachLinesArray[0], eachLinesArray[1]);
}
} catch (Exception e) {
e.printStackTrace();
}
Search ss = new Search(graph);
ss.launchFrame();
}
更改为实例方法...
shortFunc