Skip to main content

minimum_vertex_cover

Function minimum_vertex_cover 

Source
pub fn minimum_vertex_cover(graph: &Graph) -> IgraphResult<Vec<VertexId>>
Expand description

Compute a minimum vertex cover using a greedy 2-approximation.

Repeatedly picks an uncovered edge and adds both endpoints to the cover. The result is guaranteed to be at most twice the size of the optimal minimum vertex cover.

For undirected graphs, direction is ignored. For directed graphs, each arc counts as an edge that must be covered.

ยงExamples

use rust_igraph::{Graph, minimum_vertex_cover, is_vertex_cover};

// Path 0-1-2-3: optimal cover is {1, 2}, greedy gives at most 4
let mut g = Graph::with_vertices(4);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 3).unwrap();
let cover = minimum_vertex_cover(&g).unwrap();
assert!(is_vertex_cover(&g, &cover));
assert!(cover.len() <= 4); // 2-approx of opt=2

// Empty graph: no edges, empty cover
let g = Graph::with_vertices(5);
let cover = minimum_vertex_cover(&g).unwrap();
assert!(cover.is_empty());