-
Notifications
You must be signed in to change notification settings - Fork 196
/
graph.rs
460 lines (382 loc) · 13 KB
/
graph.rs
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under both the MIT license found in the
* LICENSE-MIT file in the root directory of this source tree and the Apache
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
* of this source tree.
*/
use crate::types::OptionalVertexId;
use crate::types::VertexData;
use crate::types::VertexId;
#[derive(Copy, Clone)]
pub struct GraphVertex {
pub edges_idx: u32,
pub edges_count: u32,
}
#[derive(Clone)]
pub struct Graph {
pub(crate) vertices: VertexData<GraphVertex>,
pub(crate) edges: Vec<VertexId>,
}
impl Graph {
#[inline]
pub fn iter_vertices(&self) -> impl DoubleEndedIterator<Item = VertexId> {
self.vertices.keys()
}
#[inline]
pub fn iter_edges(&self, idx: VertexId) -> impl Iterator<Item = VertexId> + '_ {
let vertex = self.vertices[idx];
let range_from = vertex.edges_idx as usize;
let range_to = range_from + vertex.edges_count as usize;
self.edges[range_from..range_to].iter().copied()
}
pub fn iter_all_edges(&self) -> impl Iterator<Item = (VertexId, VertexId)> + '_ {
self.iter_vertices()
.flat_map(|i| self.iter_edges(i).map(move |j| (i, j)))
}
/// Allocate a VertexData with space for each of the vertices in this graph.
pub fn allocate_vertex_data<T>(&self, default: T) -> VertexData<T>
where
T: Clone,
{
VertexData::new(vec![default; self.vertices.len()])
}
/// Reverse this Graph. The VertexIds are unchanged (they still represent the same vertices so
/// they can be used to index into VertexData).
pub fn reversed(&self) -> Self {
let mut reverse_vertices = self.allocate_vertex_data(GraphVertex {
edges_idx: 0,
edges_count: 0,
});
// We could compute that count ahead of time while reading, probably not worth it.
for edge in &self.edges {
reverse_vertices[*edge].edges_count += 1;
}
let mut idx = 0;
for vertex in reverse_vertices.values_mut() {
vertex.edges_idx = idx;
idx += vertex.edges_count;
}
assert_eq!(idx as usize, self.edges.len());
let mut vertex_edge_offset = self.allocate_vertex_data(0usize);
let mut reverse_edges = vec![VertexId::default(); self.edges.len()];
for from in self.iter_vertices() {
for to in self.iter_edges(from) {
let offset = &mut vertex_edge_offset[to];
reverse_edges[reverse_vertices[to].edges_idx as usize + *offset] = from;
*offset += 1;
}
}
Self {
vertices: reverse_vertices,
edges: reverse_edges,
}
}
/// Obtain a topological ordering of this graph. The graph must be a DAG (but that's the only
/// thing that GraphBuilder can construct).
pub fn topo_sort(&self) -> Result<Vec<VertexId>, TopoSortError> {
enum Work {
Push,
Pop,
}
let mut topo_order = vec![VertexId::new(0); self.vertices.len()];
// A pair of (marked, visited), where marked means this node is on the stack and visited
// means it was popped off.
let mut state = self.allocate_vertex_data((false, false));
let mut current_topo_order_index = self.vertices.len().saturating_sub(1);
let mut queue = Vec::new();
for i in self.iter_vertices() {
queue.push((i, Work::Push));
while let Some((j, work)) = queue.pop() {
let (marked, visited) = &mut state[j];
match work {
Work::Push => {
if *visited {
continue;
}
if *marked {
return Err(TopoSortError::Cycle);
}
*marked = true;
queue.push((j, Work::Pop));
queue.extend(self.iter_edges(j).map(|k| (k, Work::Push)));
}
Work::Pop => {
*visited = true;
topo_order[current_topo_order_index] = j;
current_topo_order_index = current_topo_order_index.saturating_sub(1);
}
}
}
}
Ok(topo_order)
}
/// Given a DAG and a *reverse topological* ordering thereof, return the predecessor for each
/// node after aggregating by runtime.
pub fn find_longest_paths(
&self,
reverse_topo_order: impl IntoIterator<Item = VertexId>,
weights: &VertexData<u64>,
) -> (VertexData<PathCost>, VertexData<OptionalVertexId>) {
let mut predecessor = self.allocate_vertex_data(OptionalVertexId::none());
// The runtime for this vertex
let mut costs = self.allocate_vertex_data(PathCost::default());
for idx in reverse_topo_order {
let mut max: Option<(PathCost, VertexId)> = None;
// Visit my dependencies.
for from in self.iter_edges(idx) {
let from_cost = &costs[from];
let replace = match max {
Some((max_cost, _)) => max_cost < *from_cost,
_ => true,
};
if replace {
max = Some((*from_cost, from));
}
}
let me = PathCost {
runtime: weights[idx],
len: 1,
};
match max {
Some((cost, vertex)) => {
costs[idx] = cost + me;
predecessor[idx] = vertex.into();
}
None => {
costs[idx] = me;
predecessor[idx] = OptionalVertexId::none();
}
}
}
(costs, predecessor)
}
pub fn vertices_count(&self) -> usize {
self.vertices.len()
}
pub fn edges_count(&self) -> usize {
self.edges.len()
}
/// Produce a new graph after adding edges. This may produce a multigraph and the graph may
/// stop being a DAG.
pub fn add_edges(
&self,
add: &impl AddEdges,
size_hint: impl Into<Option<usize>>,
) -> Result<Graph, AddEdgesError> {
let mut vertices = self.allocate_vertex_data(GraphVertex {
edges_idx: 0,
edges_count: 0,
});
let mut edges = Vec::with_capacity(self.edges.len() + size_hint.into().unwrap_or_default());
let mut all_edges_idx = 0;
for from in self.iter_vertices() {
let edges_idx = all_edges_idx;
let mut edges_count = 0;
for e in self.iter_edges(from) {
edges.push(e);
edges_count += 1;
all_edges_idx += 1;
}
for e in add.edges_for_vertex(from) {
edges.push(e);
edges_count += 1;
all_edges_idx += 1;
}
vertices[from] = GraphVertex {
edges_idx,
edges_count,
};
}
Ok(Graph { vertices, edges })
}
}
#[derive(buck2_error::Error, Debug)]
pub enum TopoSortError {
#[error("cycle")]
Cycle,
}
pub trait AddEdges {
type EdgeIterator: Iterator<Item = VertexId>;
fn edges_for_vertex(&self, idx: VertexId) -> Self::EdgeIterator;
}
impl AddEdges for VertexData<OptionalVertexId> {
type EdgeIterator = std::option::IntoIter<VertexId>;
#[inline]
fn edges_for_vertex(&self, idx: VertexId) -> Self::EdgeIterator {
self[idx].into_option().into_iter()
}
}
#[derive(buck2_error::Error, Debug)]
pub enum AddEdgesError {
#[error("overflow")]
Overflow,
}
#[derive(
Default,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
derive_more::Add,
Debug
)]
pub struct PathCost {
pub runtime: u64,
pub len: u32,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::builder::GraphBuilder;
use crate::test_utils::make_dag;
use crate::test_utils::seeded_rng;
use crate::types::VertexKeys;
const K0: &str = "key0";
const K1: &str = "key1";
const K2: &str = "key2";
const K3: &str = "key3";
fn test_graph() -> (Graph, VertexKeys<&'static str>, VertexData<&'static str>) {
let mut builder = GraphBuilder::new();
builder.push(K3, std::iter::empty(), K3).unwrap();
builder.push(K2, vec![K3].into_iter(), K2).unwrap();
builder.push(K1, std::iter::empty(), K1).unwrap();
builder.push(K0, vec![K1, K2].into_iter(), K0).unwrap();
builder.finish()
}
#[test]
fn test_iter() {
let (graph, _keys, data) = test_graph();
let edges = graph
.iter_all_edges()
.map(|(l, r)| (data[l], data[r]))
.collect::<Vec<_>>();
assert_eq!(vec![(K2, K3), (K0, K1), (K0, K2)], edges);
}
#[test]
fn test_reverse() {
let (graph, _keys, data) = test_graph();
let rev_graph = graph.reversed();
let rev_edges = rev_graph
.iter_all_edges()
.map(|(l, r)| (data[l], data[r]))
.collect::<Vec<_>>();
assert_eq!(vec![(K3, K2), (K2, K0), (K1, K0)], rev_edges);
}
#[test]
fn test_topo_sort() {
let (graph, _keys, data) = test_graph();
let topo = graph
.topo_sort()
.unwrap()
.into_iter()
.map(|k| data[k])
.collect::<Vec<_>>();
assert_eq!(topo, vec![K0, K1, K2, K3]);
}
#[test]
fn test_topo_sort_empty() {
let (graph, _, _) = GraphBuilder::<&'static str, ()>::new().finish();
assert_eq!(graph.topo_sort().unwrap(), vec![]);
}
#[test]
fn test_topo_sort_large() {
let mut rng = seeded_rng();
let graph = make_dag(10000, &mut rng).shuffled(&mut rng).0.graph;
let topo_order = graph.topo_sort().unwrap();
let ranks = {
let mut ranks = VertexData::new(vec![0; graph.vertices.len()]);
for (rank, idx) in topo_order.iter().enumerate() {
ranks[*idx] = rank;
}
ranks
};
for (i, j) in graph.iter_all_edges() {
assert!(ranks[i] < ranks[j]);
}
}
#[test]
fn test_topo_sort_cycle() {
let mut builder = GraphBuilder::new();
builder.push(K1, std::iter::empty(), ()).unwrap();
builder.push(K0, std::iter::empty(), ()).unwrap();
let (graph, keys, _data) = builder.finish();
let v0 = keys.get(&K0).unwrap();
let v1 = keys.get(&K1).unwrap();
let mut new_edges = graph.allocate_vertex_data(OptionalVertexId::none());
new_edges[v0] = v1.into();
new_edges[v1] = v0.into();
let graph = graph.add_edges(&new_edges, None).unwrap();
assert!(graph.topo_sort().is_err());
}
#[test]
fn test_longest_paths() {
let (graph, keys, _data) = test_graph();
// Get the vertex ids
let v0 = keys.get(&K0).unwrap();
let v1 = keys.get(&K1).unwrap();
let v2 = keys.get(&K2).unwrap();
let v3 = keys.get(&K3).unwrap();
let mut weights = graph.allocate_vertex_data(0);
weights[v0] = 5;
weights[v1] = 15;
weights[v2] = 10;
weights[v3] = 20;
let topo = graph.topo_sort().unwrap();
let (costs, predecessor) = graph.find_longest_paths(topo.iter().rev().copied(), &weights);
assert_eq!(
costs[v3],
PathCost {
len: 1,
runtime: 20
}
);
assert_eq!(
costs[v2],
PathCost {
len: 2,
runtime: 30
}
);
assert_eq!(
costs[v1],
PathCost {
len: 1,
runtime: 15
}
);
assert_eq!(
costs[v0],
PathCost {
len: 3,
runtime: 35
}
);
assert_eq!(predecessor[v0].into_option(), Some(v2));
assert_eq!(predecessor[v1].into_option(), None);
assert_eq!(predecessor[v2].into_option(), Some(v3));
assert_eq!(predecessor[v3].into_option(), None);
}
#[test]
fn test_add_edges() {
let (graph, keys, data) = test_graph();
let v0 = keys.get(&K0).unwrap();
let v2 = keys.get(&K2).unwrap();
let v3 = keys.get(&K3).unwrap();
let mut new_edges = graph.allocate_vertex_data(OptionalVertexId::none());
new_edges[v0] = v3.into(); // new edge
new_edges[v2] = v3.into(); // already exists
let graph = graph.add_edges(&new_edges, None).unwrap();
let edges = graph
.iter_all_edges()
.map(|(l, r)| (data[l], data[r]))
.collect::<Vec<_>>();
assert_eq!(
vec![(K2, K3), (K2, K3), (K0, K1), (K0, K2), (K0, K3)],
edges
);
}
}