/
CreateG.java
169 lines (139 loc) · 5.44 KB
/
CreateG.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class CreateG{
public static void main(String[] args){
Graph newGraph = new Graph();
//read the Nodes.txt files to create nodes a add them to the Graph
try {
FileReader arq = new FileReader("Nodes.txt");
BufferedReader lerArq = new BufferedReader(arq);
String nodeInfo = lerArq.readLine();
int aux = 0;
while (nodeInfo != null) {
String splittedNodeInfo[] = nodeInfo.split("-");
//splittedNodeInfo[0] = name
//splittedNodeInfo[1] = generation
Node newNode = new Node (aux, 0);
newNode.setName(splittedNodeInfo[0]);
newNode.setGeneration(Integer.parseInt(splittedNodeInfo[1]));
newGraph.addNode(newNode);
nodeInfo = lerArq.readLine();
aux++;
}
arq.close();
} catch (IOException e) {
System.err.printf("Erro na abertura do arquivo: %s.\n",
e.getMessage());
}
//read AdjacencyList to find each neighbor
//each line of the adjancency list has the format: targetid.relation-targetid.relation ...
try {
FileReader arq = new FileReader("AdjacencyList.txt");
BufferedReader lerArq = new BufferedReader(arq);
String line = lerArq.readLine();
int aux = 0;
while (line != null) {
if (!line.trim().equals("")){ //making sure that the line isn't empty
String [] neightborIdAndRelation = line.split("-");
for(int aux2=0; aux2<neightborIdAndRelation.length; aux2++){
String [] neighbor = new String[2];
neighbor = neightborIdAndRelation[aux2].split("/");
//neighbor[0] = neighbor id,
//neighbor[1] = neighbor relation
Edge newEdge = new Edge(newGraph.findNodebyId(aux),
newGraph.findNodebyId(Integer.parseInt(neighbor[0])),
neighbor[1]);
//adding the new edge to the list of neighbor of the node
newGraph.findNodebyId(aux).addNeighbor(newEdge);
//adding the new edge to the list of edges of the graph
newGraph.addEdge(newEdge);
}
}
line = lerArq.readLine();
aux++;
}
arq.close();
} catch (IOException e) {
System.err.printf("Erro na abertura do arquivo: %s.\n",
e.getMessage());
}
// Simple Menu
JPanel panel = new JPanel();
JButton plotGraphButton = new JButton("Plotar Grafo");
JButton minimumTreeButton = new JButton("Árvore Mínima");
JButton searchDistanceButton = new JButton("Distância entre nós");
JButton bFSButton = new JButton ("Busca em Largura");
JButton dFSButton = new JButton("Busca em Profundidade");
plotGraphButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JGraphXFrame.createFrame(newGraph);
}
});
minimumTreeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String primPath = newGraph.prim();
JOptionPane.showMessageDialog(null,primPath);
newGraph.resetGraph();
}
});
searchDistanceButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String originNodeName = JOptionPane.showInputDialog(null,"Nó origem:","Torrhen Stark");
String seekNodeName = JOptionPane.showInputDialog(null,"Nó destino:","Benjen Stark");
if(originNodeName != null && seekNodeName != null){
int value = newGraph.distanceBetweenNodes(newGraph.findNodebyName(originNodeName),newGraph.findNodebyName(seekNodeName));
String msg;
if(value == -1)
msg = "There are no conection between this two nodes";
else
msg = String.format("Origin node: %s\nEnd node: %s\nDistance: %d\n",originNodeName,seekNodeName,value);
JOptionPane.showMessageDialog(null,msg);
}
newGraph.resetGraph();
}
});
bFSButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String originNodeName = JOptionPane.showInputDialog(null, "Nó de origem", "Torrhen Stark");
if(originNodeName != null){
String msg = newGraph.findNodebyName(originNodeName).bFS();
JOptionPane.showMessageDialog(null,msg);
newGraph.resetGraph();
}else{
JOptionPane.showMessageDialog(null,"É necessário inserir o nome de um nó");
}
}
});
dFSButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String targetNode = JOptionPane.showInputDialog(null,"Nó:", "Torrhen Stark");
if(targetNode != null){
Node node = newGraph.findNodebyName(targetNode);
String dfs = node.buscaProfundidade();
JOptionPane.showMessageDialog(null,dfs);
newGraph.resetGraph();
}else{
JOptionPane.showMessageDialog(null,"É necessário inserir o nome de um nó");
}
}
});
panel.add(plotGraphButton);
panel.add(minimumTreeButton);
panel.add(searchDistanceButton);
panel.add(bFSButton);
panel.add(dFSButton);
JFrame mainWindow = new JFrame("TF EDA 2");
mainWindow.add(panel);
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.pack();
mainWindow.setVisible(true);
}
}