Skip to content

Commit fc1401d

Browse files
committed
Notes:
- Coloring algorithm fixed. - AddEdge function from graph class fixed. - Function to write details of execution in a file. - More code refacturing: divided the parts of run() Application's class function into different functions.
1 parent 8d7e896 commit fc1401d

File tree

4 files changed

+232
-22559
lines changed

4 files changed

+232
-22559
lines changed

NP-Hard/src/Classes/Application.java

+106-32
Original file line numberDiff line numberDiff line change
@@ -2,90 +2,166 @@
22

33
import java.io.File;
44
import java.io.FileNotFoundException;
5+
import java.io.FileWriter;
6+
import java.io.IOException;
7+
import java.time.LocalTime;
58
import java.util.Scanner;
69

710
public class Application {
811

12+
public long algorithmDuration;
13+
public String instanceFileName;
14+
915
public Application() {
10-
16+
this.algorithmDuration = 0;
17+
this.instanceFileName = "";
1118
}
1219

13-
public static void main(String[] args) throws FileNotFoundException {
20+
public static void main(String[] args) {
1421

1522
Application app = new Application();
1623
app.run();
1724

1825
}
1926

2027

21-
public void run() throws FileNotFoundException {
22-
String instanceFileName = this.selectInstance();
28+
public void run() {
2329

24-
Graph graph = this.setUpGraph(instanceFileName);
30+
this.instanceFileName = this.selectInstance();
31+
32+
Graph graph = this.setUpGraph();
2533

2634
if(graph == null) {
27-
System.out.println("Error during the creation of graph");
35+
System.out.println("Error during the graph creation");
2836
return;
2937
}
3038

3139

32-
long start = System.currentTimeMillis();
40+
long start = System.nanoTime();
3341
int chromaticNumber = graph.RLF();
34-
long timeElapsed = System.currentTimeMillis() - start;
42+
this.algorithmDuration = System.nanoTime() - start;
43+
3544

36-
this.printExecutionInfo(timeElapsed, chromaticNumber);
45+
this.printExecutionInfo(graph, chromaticNumber);
46+
47+
this.saveExecutionInformation(graph, chromaticNumber);
3748
}
3849

50+
public String getCurrentTime() {
51+
LocalTime time = LocalTime.now();
52+
53+
Integer hour = time.getHour();
54+
Integer minute = time.getMinute();
55+
56+
return hour.toString() + "h" + minute.toString();
57+
}
3958

40-
private void printExecutionInfo(long timeElapsed, int chromaticNumber) {
41-
System.out.format("Chromatic number: %d\n", chromaticNumber);
42-
System.out.format("Duration: %d ms", timeElapsed);
59+
private void saveExecutionInformation(Graph graph, int chromaticNumber) {
60+
try {
61+
62+
String fileName = "src/reports/" + this.getCurrentTime() + "_execution-report.txt";
63+
64+
FileWriter fileWriter = new FileWriter(fileName);
65+
66+
fileWriter.write("Instance file: " + this.instanceFileName + "\n");
67+
fileWriter.write("Number of Vertices: " + graph.getVerticesNumber() + "\n");
68+
fileWriter.write("Number of Edges: " + graph.getEdgesNumber() + "\n");
69+
70+
fileWriter.write("Number of colors used: " + chromaticNumber + "\n");
71+
fileWriter.write("Duration: " + this.algorithmDuration + " ns\n");
72+
73+
System.out.println("Execution information saved on " + fileName);
74+
75+
fileWriter.close();
76+
}
77+
catch(IOException e) {
78+
System.out.println("Error: it was not possible to write on file");
79+
System.out.println(e.getMessage());
80+
}
4381
}
4482

4583

46-
private String selectInstance() throws FileNotFoundException {
84+
private void printExecutionInfo(Graph graph, int chromaticNumber) {
85+
System.out.println("");
4786

48-
this.printAvailableInstances();
87+
System.out.println("Execution information:");
4988

50-
Scanner sc = new Scanner(System.in);
51-
System.out.print("Digite o nome do arquivo de teste (nome_arquivo.txt) para execucao: ");
52-
String instanceFileName = sc.nextLine();
53-
sc.close();
89+
System.out.println("\tInstance file: " + this.instanceFileName);
90+
System.out.println("");
91+
System.out.format("\tNumber of Vertices: %d\n", graph.getVerticesNumber());
92+
System.out.format("\tNumber of Edges: %d\n", graph.getEdgesNumber());
5493

55-
instanceFileName = "src/instances/" + instanceFileName;
94+
System.out.println("");
5695

57-
return instanceFileName;
96+
if(graph.verifyColoring())
97+
System.out.println("\tColoring is correct.");
98+
else
99+
System.out.println("\tColoring is incorrect.");
100+
101+
System.out.println("");
102+
103+
System.out.format("\tChromatic number: %d\n", chromaticNumber);
104+
System.out.println("");
105+
System.out.format("\tDuration: %d ns", this.algorithmDuration);
106+
System.out.println("");
107+
graph.printVertexColor();
58108
}
59109

60110

111+
61112
private void printAvailableInstances() {
62-
File folder = new File("src/instances/");
63-
System.out.println("Instancias disponiveis: ");
64113

114+
115+
File folder = new File("src/instances/");
116+
System.out.println("Available instance files: ");
117+
65118
for(File file : folder.listFiles()) {
66119
if(file.isFile() && file.canRead())
67120
System.out.println("\t" + file.getName());
68121
}
122+
69123
}
70124

71125

72126

73-
public Graph setUpGraph(String instanceFileName) throws FileNotFoundException {
74-
return this.setUpGraph(new Graph(), instanceFileName);
127+
128+
private String selectInstance() {
129+
130+
this.printAvailableInstances();
131+
132+
Scanner sc = new Scanner(System.in);
133+
System.out.print("Digite o nome do arquivo de teste (nome_arquivo.txt): ");
134+
String instanceFileName = sc.nextLine();
135+
sc.close();
136+
137+
instanceFileName = "src/instances/" + instanceFileName;
138+
139+
return instanceFileName;
75140
}
76141

142+
143+
77144

78-
private Graph setUpGraph (Graph graph, String instanceFileName) throws FileNotFoundException {
145+
146+
public Graph setUpGraph() {
147+
return this.setUpGraph(new Graph(), this.instanceFileName);
148+
}
149+
150+
151+
private Graph setUpGraph (Graph graph, String instanceFileName) {
79152

80153
try {
81154
File instanceFile = new File (instanceFileName);
155+
82156
Scanner fileReader = new Scanner(instanceFile);
83157
this.readInstances(fileReader, graph);
158+
84159
fileReader.close();
160+
85161
return graph;
86162
}
87163
catch(FileNotFoundException e) {
88-
System.out.println("File not found");
164+
System.out.println(e.getMessage());
89165
e.printStackTrace();
90166
}
91167

@@ -97,13 +173,9 @@ private void readInstances(Scanner fileReader, Graph graph) {
97173
String line = fileReader.nextLine();
98174
String[] information = line.split(" ");
99175

176+
177+
graph.setNumberEdges(Integer.valueOf(information[3]).intValue());
100178

101-
System.out.println("Number of vertices: " + information[2]+"\n"
102-
+ "Number of edges: " + information[3]);
103-
104-
105-
//Considering the implementation of string with an array,
106-
// we cannot use an iterator over it
107179

108180
while(fileReader.hasNextLine()) {
109181
line = fileReader.nextLine();
@@ -114,6 +186,8 @@ private void readInstances(Scanner fileReader, Graph graph) {
114186
int u = Integer.valueOf(values[1]).intValue();
115187
int v = Integer.valueOf(values[2]).intValue();
116188

189+
//System.out.format("Edge (%d,%d)\n", u,v);
190+
117191
graph.addEdge(u, v);
118192

119193
}

0 commit comments

Comments
 (0)