I am trying to find all the possible paths of nodes by getting the start and destination node as input.
I am reading all possible paths of all nodes from a text file and then filter the possible paths.
I am getting the output when using System.out.println function. But I'm having trouble when setting the output to a JTextArea
.
I have added my code below.
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();
}
}
Output using System.out.println() when getting start and end node as input from textfields:
B E
B A C E
B A C F E
B F E
B F C E
The problem is related to
public static Graph graph;
and manifests here
public static void shortFunc(){
LinkedList<String> visited = new LinkedList();
visited.add(START);
new Search().breadthFirst(graph, visited);
}
Basically, you are creating a brand new instance of Search
which has absolutely nothing to do with the window that is visible on the screen.
static
is not your friend.
Start by removing the static
decleration against Graph
and require that Search
be provided with an instance of Graph
as part of it's initialisation...
public class Search extends JFrame {
private Graph graph;
//...
public Search(Graph graph) {
this.graph = graph;
Next, change your main
method to load the Graph
first and then the window...
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();
}
Next change shortFunc
to be a instance method...
public void shortFunc() {
LinkedList<String> visited = new LinkedList();
visited.add(START);
breadthFirst(graph, visited);
}