2
2
3
3
import java .io .File ;
4
4
import java .io .FileNotFoundException ;
5
+ import java .io .FileWriter ;
6
+ import java .io .IOException ;
7
+ import java .time .LocalTime ;
5
8
import java .util .Scanner ;
6
9
7
10
public class Application {
8
11
12
+ public long algorithmDuration ;
13
+ public String instanceFileName ;
14
+
9
15
public Application () {
10
-
16
+ this .algorithmDuration = 0 ;
17
+ this .instanceFileName = "" ;
11
18
}
12
19
13
- public static void main (String [] args ) throws FileNotFoundException {
20
+ public static void main (String [] args ) {
14
21
15
22
Application app = new Application ();
16
23
app .run ();
17
24
18
25
}
19
26
20
27
21
- public void run () throws FileNotFoundException {
22
- String instanceFileName = this .selectInstance ();
28
+ public void run () {
23
29
24
- Graph graph = this .setUpGraph (instanceFileName );
30
+ this .instanceFileName = this .selectInstance ();
31
+
32
+ Graph graph = this .setUpGraph ();
25
33
26
34
if (graph == null ) {
27
- System .out .println ("Error during the creation of graph" );
35
+ System .out .println ("Error during the graph creation " );
28
36
return ;
29
37
}
30
38
31
39
32
- long start = System .currentTimeMillis ();
40
+ long start = System .nanoTime ();
33
41
int chromaticNumber = graph .RLF ();
34
- long timeElapsed = System .currentTimeMillis () - start ;
42
+ this .algorithmDuration = System .nanoTime () - start ;
43
+
35
44
36
- this .printExecutionInfo (timeElapsed , chromaticNumber );
45
+ this .printExecutionInfo (graph , chromaticNumber );
46
+
47
+ this .saveExecutionInformation (graph , chromaticNumber );
37
48
}
38
49
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
+ }
39
58
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
+ }
43
81
}
44
82
45
83
46
- private String selectInstance () throws FileNotFoundException {
84
+ private void printExecutionInfo (Graph graph , int chromaticNumber ) {
85
+ System .out .println ("" );
47
86
48
- this . printAvailableInstances ( );
87
+ System . out . println ( "Execution information:" );
49
88
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 ( " \t Instance file: " + this . instanceFileName );
90
+ System .out .println ( " " );
91
+ System . out . format ( " \t Number of Vertices: %d \n " , graph . getVerticesNumber () );
92
+ System . out . format ( " \t Number of Edges: %d \n " , graph . getEdgesNumber () );
54
93
55
- instanceFileName = "src/instances/" + instanceFileName ;
94
+ System . out . println ( "" ) ;
56
95
57
- return instanceFileName ;
96
+ if (graph .verifyColoring ())
97
+ System .out .println ("\t Coloring is correct." );
98
+ else
99
+ System .out .println ("\t Coloring is incorrect." );
100
+
101
+ System .out .println ("" );
102
+
103
+ System .out .format ("\t Chromatic number: %d\n " , chromaticNumber );
104
+ System .out .println ("" );
105
+ System .out .format ("\t Duration: %d ns" , this .algorithmDuration );
106
+ System .out .println ("" );
107
+ graph .printVertexColor ();
58
108
}
59
109
60
110
111
+
61
112
private void printAvailableInstances () {
62
- File folder = new File ("src/instances/" );
63
- System .out .println ("Instancias disponiveis: " );
64
113
114
+
115
+ File folder = new File ("src/instances/" );
116
+ System .out .println ("Available instance files: " );
117
+
65
118
for (File file : folder .listFiles ()) {
66
119
if (file .isFile () && file .canRead ())
67
120
System .out .println ("\t " + file .getName ());
68
121
}
122
+
69
123
}
70
124
71
125
72
126
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 ;
75
140
}
76
141
142
+
143
+
77
144
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 ) {
79
152
80
153
try {
81
154
File instanceFile = new File (instanceFileName );
155
+
82
156
Scanner fileReader = new Scanner (instanceFile );
83
157
this .readInstances (fileReader , graph );
158
+
84
159
fileReader .close ();
160
+
85
161
return graph ;
86
162
}
87
163
catch (FileNotFoundException e ) {
88
- System .out .println ("File not found" );
164
+ System .out .println (e . getMessage () );
89
165
e .printStackTrace ();
90
166
}
91
167
@@ -97,13 +173,9 @@ private void readInstances(Scanner fileReader, Graph graph) {
97
173
String line = fileReader .nextLine ();
98
174
String [] information = line .split (" " );
99
175
176
+
177
+ graph .setNumberEdges (Integer .valueOf (information [3 ]).intValue ());
100
178
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
107
179
108
180
while (fileReader .hasNextLine ()) {
109
181
line = fileReader .nextLine ();
@@ -114,6 +186,8 @@ private void readInstances(Scanner fileReader, Graph graph) {
114
186
int u = Integer .valueOf (values [1 ]).intValue ();
115
187
int v = Integer .valueOf (values [2 ]).intValue ();
116
188
189
+ //System.out.format("Edge (%d,%d)\n", u,v);
190
+
117
191
graph .addEdge (u , v );
118
192
119
193
}
0 commit comments