-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectTest.java
More file actions
129 lines (108 loc) · 3.83 KB
/
ProjectTest.java
File metadata and controls
129 lines (108 loc) · 3.83 KB
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
/* ************************************************
* Algorithms and Data Structures Project
*
* ProjectTest.java
*
* Main class for the project.
*
* The program reads a file containing a list of edges with their weights
* and computes the Minimum Spanning Tree of the graph.
*
* The input file must contain one edge per line in the format:
* N1 N2 W
* where N1 and N2 are the nodes of the edge and W is the weight of the edge.
* The columns N1 and N2 must be composed of integer numbers ordered in ascending order.
* The column W must be composed of double numbers.
* Those three columns must be separated by a tab.
*
* The program writes the result of the computation in the file output.txt.
*
* To compile:
* javac ProjectTest.java
*
* To run:
* java ProjectTest grafoMST.txt
* *************************************************/
import java.io.*;
import java.util.*;
import algorithm.graph.MST.*;
import datastructure.graph.*;
public class ProjectTest {
/*
* Main per leggere un file che reppresenta un grafo, calcolare il suo
* Minimum Spanning Tree e stamparlo
*/
public static void main( String[] args ) {
try {
long start_t, end_t, elapsed, min;
double sec;
File file = new File(args[0]);
// Legge gli archi dal fileIn inserendo i vertici sorgente, i vertici
// destinazione e i pesi in tre ArrayList src, dest e pesi, rispettivamente
BufferedReader br = new BufferedReader(new FileReader(file));
ArrayList<Integer> src = new ArrayList<Integer>();
ArrayList<Integer> dst = new ArrayList<Integer>();
ArrayList<Double> pesi = new ArrayList<Double>();
String st,strest;
int max=0,s,d,v,v2;
double p;
while ((st = br.readLine()) != null) {
v = st.indexOf("\t");
s = Integer.valueOf(st.substring(0,v));
strest = st.substring(v+1);
v2 = strest.indexOf("\t");
d = Integer.valueOf(strest.substring(0,v2));
p = Double.valueOf(strest.substring(v2+1));
if (s>max) max=s;
if (d>max) max=d;
src.add(s);
dst.add(d);
pesi.add(p);
}
// Crea il relativo grafo
Graph<Integer> g =
new GraphAL<Integer>();
ArrayList<Vertex<Integer>> nodi =
new ArrayList<Vertex<Integer>>(max+1);
for (int i=0; i<=max; i++)
nodi.add(g.addVertex(i));
for (int j=0; j<src.size(); j++) {
g.addEdge(nodi.get(src.get(j)),
nodi.get(dst.get(j)),pesi.get(j));
g.addEdge(nodi.get(dst.get(j)),
nodi.get(src.get(j)),pesi.get(j));
}
// Calcola il Minimum Spanning Tree
MST<Integer> mst = new Boruvka<Integer>();
start_t = System.currentTimeMillis();
Graph<Integer> t = mst.MinimumSpanningTree(g);
end_t = System.currentTimeMillis();
elapsed = end_t - start_t;
min = elapsed / 60000;
sec = (double)(elapsed % 60000) / 1000.0;
File fout = new File("output.txt");
FileOutputStream fos = new FileOutputStream(fout);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
// Stampa il Minimum Spanning Tree ed il relativo costo
ArrayList<Vertex<Integer>> vert = t.vertexes();
for (int i=0; i<t.vertexNum(); i++) {
bw.write("Adiacenti a "+(vert.get(i)).getData()+":");
ArrayList<Edge<Integer>> archi = t.outEdges(vert.get(i));
for (int j=0; j<g.outDegree(vert.get(i)); j++) {
bw.write( " "+ (archi.get(j)).getDest().getData()+" "+
(archi.get(j)).getWeight() );
}
bw.newLine();
}
double totPesi = 0;
ArrayList<Edge<Integer>> e = t.edges();
for (int i=0; i < e.size(); i++) totPesi = totPesi + e.get(i).getWeight();
bw.write("Costo totale: "+totPesi/2);
bw.newLine();
bw.write("Elapsed time: " + min + " minuti e " + sec + " secondi");
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}