-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortestPathTest.java
More file actions
107 lines (87 loc) · 3.13 KB
/
ShortestPathTest.java
File metadata and controls
107 lines (87 loc) · 3.13 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
/* ************************************************
* Classe per testare gli algoritmi di ricerca dei single-source shortest paths
*
* Esempio di esecuzione:
*
* java ShortestPathTest fileIn
*
* dove "fileIn" è il nome di un file che contiene la descrizione di un grafo orientato pesato:
* ad ogni riga del file si riportano gli indici di due nodi collegati da un arco e del relativo peso
* separati da un TAB.
*
* Dopo aver letto il file, crea il grafo relativo, ne calcola i cammini minimi da singole sorgente
* considerando il prigmo vertice, poi stampa le distanze dei vertici dalla sorgente
*
* *************************************************/
import java.io.*;
import java.util.*;
import datastructure.graph.*;
import algorithm.graph.SSSP.*;
public class ShortestPathTest {
/*
* Main per leggere un file che reppresenta un grafo e calcolare i cammini minimi
* a partire dal primo vertice
*/
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));
}
// Calcola i camminimi minimi dal primo vertice
SSSP<Integer> cammini;
Map<Vertex<Integer>,Edge<Integer>> parent;
cammini = new BellmanFord<Integer>();
parent = cammini.SingleSourceShortestPaths(g, nodi.get(0));
// Stampa le distanze dei vertici dalla sorgente
for (int i=0; i<nodi.size(); i++) {
System.out.println( "Distanza nodo " + nodi.get(i).getData() + " : " +
computeDist(parent,nodi.get(i),nodi.get(0)) );
}
} catch (IOException e) {
e.printStackTrace();
}
}
// Calcola la distanza di un vertice v da s considerando l'albero dei cammini minimi da s rappresentato da parent
private static double computeDist(Map<Vertex<Integer>,Edge<Integer>> parent, Vertex<Integer> v, Vertex<Integer> s) {
if (v!=s && parent.get(v) == null) return Double.POSITIVE_INFINITY;
int d = 0;
while (v != s) {
d += parent.get(v).getWeight();
v = parent.get(v).getSource();
}
return d;
}
}