-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.java
145 lines (124 loc) · 2.95 KB
/
Graph.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
import java.util.ArrayList;
import java.util.LinkedList;
import lejos.geom.*;
public class Graph {
private ArrayList<LinkedList<Edge>> adj;
private int V;
private int E;
private ArrayList<Point> Points;
public Graph (int V) {
this.V = V;
this.E = 0;
this.Points = new ArrayList<Point>();
this.adj = new ArrayList<LinkedList<Edge>>();
for (int i = 0; i < V; i++) {
this.Points.add(null);
this.adj.add(new LinkedList<Edge>());
}
}
public int V() {
return V;
}
public int E() {
return E;
}
public void addEdge (int v, int w, double weight) {
Edge e = new Edge (v, w, weight);
E++;
adj.get(v).add(e);
adj.get(w).add(e.reverse());
}
public void addNode () {
V++;
Points.add (null);
this.adj.add(new LinkedList<Edge>());
}
public void addNode (double x, double y) {
V++;
Points.add (new Point ((float)x,(float)y));
this.adj.add(new LinkedList<Edge>());
}
public void addNode (Point p) {
V++;
Points.add (p);
this.adj.add(new LinkedList<Edge>());
}
public Point getPoint (int v) {
return Points.get (v);
}
// Devolve a lista dos nós adjacentes a v
public LinkedList<Edge> adj(int v) {
return this.adj.get(v);
}
// Devolve a ShortestPathTree de um nó s representada em um vetor
public int[] spt (int s) {
int[] spt = new int[V];
boolean explored[] = new boolean[V];
double distTo[] = new double[V];
IndexMinPQ<Double> q;
q = new IndexMinPQ<Double>(V);
for (int v = 0; v < V; v++) {
explored[v] = false;
distTo[v] = Double.POSITIVE_INFINITY;
}
explored[s] = true;
distTo[s] = 0;
spt[s] = s;
q.insert (s, (double)0);
while (q.size() != 0) {
int v = q.delMin();
explored[v] = true;
for (Edge e : this.adj(v)) {
int w = e.w();
if (!explored[w]) {
if (distTo[w] > distTo[v] + e.weight()) {
distTo[w] = distTo[v] + e.weight();
spt[w] = v;
if (q.contains(w)) {
q.decreaseKey (w, distTo[w]);
}
else {
q.insert (w, distTo[w]);
}
}
}
}
}
return spt;
}
public String toString() {
StringBuilder s = new StringBuilder();
s.append(V + " vertices, " + E + " edges " + "\n");
for (int v = 0; v < V; v++) {
s.append(v + ": ");
for (Edge e : adj.get(v)) {
int w = e.w;
s.append(w + " ");
}
s.append("\n");
}
return s.toString();
}
public class Edge {
int v;
int w;
double weight;
public Edge (int v, int w, double weight) {
this.v = v;
this.w = w;
this.weight = weight;
}
public int v () {
return v;
}
public int w () {
return w;
}
public double weight () {
return weight;
}
public Edge reverse () {
return new Edge (w, v, weight);
}
}
}