Fix same issues in Graph draft as well

This commit is contained in:
2026-04-23 18:23:54 +02:00
parent 02cd1a0dea
commit 67e16f50cd
+22 -18
View File
@@ -21,6 +21,7 @@ struct IncidenceEntry {
next: Option<Incidence>, next: Option<Incidence>,
neighbor: Vertex, neighbor: Vertex,
} }
struct VertexNeighborIterator<'a> { struct VertexNeighborIterator<'a> {
graph: &'a Graph, graph: &'a Graph,
incidence: Option<Incidence>, incidence: Option<Incidence>,
@@ -64,6 +65,12 @@ impl Graph {
} }
} }
impl Default for Graph {
fn default() -> Self {
Self::new()
}
}
impl GraphTopology for Graph { impl GraphTopology for Graph {
type Vertex = Vertex; type Vertex = Vertex;
@@ -82,7 +89,7 @@ impl GraphTopology for Graph {
} }
fn are_adjacent(&self, v1: Self::Vertex, v2: Self::Vertex) -> bool { fn are_adjacent(&self, v1: Self::Vertex, v2: Self::Vertex) -> bool {
self.neighbors(v1).find(|&x| x == v2).is_some() self.neighbors(v1).any(|x| x == v2)
} }
fn vertices(&self) -> impl Iterator<Item = Self::Vertex> { fn vertices(&self) -> impl Iterator<Item = Self::Vertex> {
@@ -180,9 +187,8 @@ mod tests {
fn are_adjacent_vertex_self() { fn are_adjacent_vertex_self() {
let mut graph = Graph::new(); let mut graph = Graph::new();
let v = graph.add_vertex(); let v = graph.add_vertex();
assert_eq!( assert!(
graph.are_adjacent(v, v), !graph.are_adjacent(v, v),
false,
"should not be adjacent to itself" "should not be adjacent to itself"
); );
} }
@@ -211,7 +217,7 @@ mod tests {
assert!( assert!(
graph.are_adjacent(vertices[9], vertices[5]), graph.are_adjacent(vertices[9], vertices[5]),
"expected {:?} and {:?} to be adjacent", "expected {:?} and {:?} to be adjacent",
vertices[0], vertices[9],
vertices[5] vertices[5]
); );
assert!( assert!(
@@ -287,11 +293,11 @@ mod tests {
assert_eq!( assert_eq!(
graph.neighbors(vertices[4]).count(), graph.neighbors(vertices[4]).count(),
4, 4,
"unexpected vertex count" "unexpected neighbor count"
); );
// Expects each neighbor to appear exactly once. This will not work if there are multiple edges. // Expects each neighbor to appear exactly once. This will not work if there are multiple edges.
let neighbors: Vec<Vertex> = vec![vertices[1], vertices[2], vertices[7], vertices[8]]; let neighbors = vec![vertices[1], vertices[2], vertices[7], vertices[8]];
for v in graph.neighbors(vertices[4]) { for v in graph.neighbors(vertices[4]) {
assert_eq!( assert_eq!(
neighbors.iter().filter(|&x| *x == v).count(), neighbors.iter().filter(|&x| *x == v).count(),
@@ -310,9 +316,8 @@ mod tests {
assert_eq!(graph.vertex_count(), 1, "unexpected vertex count"); assert_eq!(graph.vertex_count(), 1, "unexpected vertex count");
assert_eq!(graph.edge_count(), 1, "unexpected edge count"); assert_eq!(graph.edge_count(), 1, "unexpected edge count");
assert_eq!(graph.degree(v), 2, "unexpected degree"); assert_eq!(graph.degree(v), 2, "unexpected degree");
assert_eq!( assert!(
graph.are_adjacent(v, v), graph.are_adjacent(v, v),
true,
"vertex with loop edge should be adjacent to itself" "vertex with loop edge should be adjacent to itself"
); );
let mut neighbors = graph.neighbors(v); let mut neighbors = graph.neighbors(v);
@@ -326,15 +331,15 @@ mod tests {
Some(v), Some(v),
"vertex should be neighbor of itself twice" "vertex should be neighbor of itself twice"
); );
assert_eq!(neighbors.next(), None, "two many neighbors from iterator"); assert_eq!(neighbors.next(), None, "too many neighbors from iterator");
} }
#[test] #[test]
fn multiple_edges() { fn multiple_edges() {
let mult = 3; let k = 3;
let mut graph = Graph::new(); let mut graph = Graph::new();
let vertices = [graph.add_vertex(), graph.add_vertex()]; let vertices = [graph.add_vertex(), graph.add_vertex()];
for _ in 0..mult { for _ in 0..k {
graph.add_edge(vertices[0], vertices[1]); graph.add_edge(vertices[0], vertices[1]);
} }
assert_eq!( assert_eq!(
@@ -342,18 +347,17 @@ mod tests {
vertices.len(), vertices.len(),
"unexpected vertex count" "unexpected vertex count"
); );
assert_eq!(graph.edge_count(), mult, "unexpected edge count"); assert_eq!(graph.edge_count(), k, "unexpected edge count");
for v in vertices { for v in vertices {
assert_eq!(graph.degree(v), mult, "unexpected degree of {v:?}"); assert_eq!(graph.degree(v), k, "unexpected degree of {v:?}");
} }
assert_eq!( assert!(
graph.are_adjacent(vertices[0], vertices[1]), graph.are_adjacent(vertices[0], vertices[1]),
true,
"should be adjacent" "should be adjacent"
); );
for i in 0..2 { for i in 0..2 {
let mut neighbors = graph.neighbors(vertices[i]); let mut neighbors = graph.neighbors(vertices[i]);
for j in 0..mult { for j in 0..k {
assert_eq!( assert_eq!(
neighbors.next(), neighbors.next(),
Some(vertices[1 - i]), Some(vertices[1 - i]),
@@ -365,7 +369,7 @@ mod tests {
assert_eq!( assert_eq!(
neighbors.next(), neighbors.next(),
None, None,
"two many neighbors of {:?} from iterator", "too many neighbors of {:?} from iterator",
vertices[i] vertices[i]
); );
} }