filtered_graph: num_vertices is an index bound, not a count

Offen
#690 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Anfängerfreundlichkeit
15/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Aktiv
Tech-Stack
cpp
Bereich
api

Rechercherichtung

Start with the two num_vertices call sites in include/boost/graph/connected_components.hpp (lines 72-73 and 106-108) to see where the value is used as a count versus an index bound. Then check the filtered-graph docs in doc/modules/ROOT/pages/adaptors/filtered_graph.adoc and the open issues #231 and #627. Done means agreeing on the proposed API split with maintainers; the issue itself lists the design choice as open, so a newcomer should not start on the library-wide change.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

api data structure

num_vertices(g) is used for two different things in BGL:

  1. as a vertex count (emptiness checks, loop bounds, sampling)
    https://github.com/boostorg/graph/blob/ddce215b8b7adafd22927ea508b5991ade7c9064/include/boost/graph/connected_components.hpp#L72-L73
  2. and as an upper bound on vertex indices to size property arrays.
    https://github.com/boostorg/graph/blob/ddce215b8b7adafd22927ea508b5991ade7c9064/include/boost/graph/connected_components.hpp#L106-L108

[!NOTE]
This works today because filtering doesn't renumber vertices: a kept vertex keeps its index from the base graph, so indices still go up to the base graph's size. filtered_graph forwards num_vertices and num_edges to the base graph, so any array sized with num_vertices(fg) is large enough for every index still in use. The price is that num_vertices no longer means "how many vertices vertices(fg) yields".

// base graph has vertices 0 1 2 3 4, the filter keeps 0 2 4
std::vector<color> color_map(num_vertices(fg));

// num_vertices(fg) is 5, so color_map[4] is valid
// if it returned 3, color_map[4] would be out of bounds
color_map[get(vertex_index, fg, 4)] = ...;

Bug: Since num_vertices(fg) doesn't match what vertices(fg) yields, every algorithm that uses it as a count can go wrong once vertices are filtered out:

Users hit this too: Rosetta, Hyperscan and IfcOpenShell all carry comments and workarounds for it, and BGL has its own count_vertices in isomorphism.hpp.

Proposal
  1. Add vertex_index_bound(g) / edge_index_bound(g) (names TBD), defaulting to num_vertices(g) / num_edges(g). Nothing changes for existing graphs.
  2. Move algorithms to the bound when sizing property storage, and keep num_vertices for counting.
  3. Then make filtered_graph's num_vertices / num_edges return the real count (O(V) or cached, O(1) when the predicate is keep_all).

Step 3 silently breaks user code that does std::vector<T> v(num_vertices(fg)) on a vertex-filtered graph and indexes it by vertex index. It compiles and then writes out of bounds. A search of public code found only a handful of such cases (bark, two OpenCV books, a BGL test), but it should still go with a release that already breaks things, plus a loud release note.

For #231 itself, fixing the call sites is enough.

Vorherrschende Sprache
C++
Sterne
402
Forks
251
Ø Merge
1 T. 9 Std.
Gemergte PRs (30 T.)
38

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus boostorg/graph

Alle Issues in boostorg/graph

Ähnliche Issues

Weitere Issues zu C++

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.