Skip to content

Commit 3c83c4b

Browse files
committed
D3128: Update Prim's algorithm to match current implementation
- Consolidate two prim() overloads into a single signature with defaulted WF and CompareOp template parameters (seed moved to second parameter) - Remove init_dist parameter; add explicit requires clause for basic_edge_weight_function with plus<> combine - Update mst.hpp and prim.hpp signatures accordingly - algorithms.tex: resolve \phil note by documenting delegation to dijkstra_shortest_paths; fix lstinputlisting line range (24-39); correct Mandates type (vertex_property_map_value_t<Weight>); add init_shortest_paths precondition; update Effects; add Throws section; add Remarks for disconnected-graph usage"
1 parent 23b86b8 commit 3c83c4b

3 files changed

Lines changed: 47 additions & 43 deletions

File tree

‎D3128_Algorithms/src/mst.hpp‎

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,19 @@ auto inplace_kruskal(IELR&& e, OELR&& t, CompareOp compare);
2121
/*
2222
* Prim's Algorithm
2323
*/
24-
template <adjacency_list G, class Predecessor, class Weight>
25-
auto prim(G&& g, Predecessor& predecessor, Weight& weight, const vertex_id_t<G>& seed = 0);
26-
2724
template <adjacency_list G,
2825
class Predecessor,
2926
class Weight,
30-
class CompareOp,
31-
class WF>
32-
requires basic_edge_weight_function<G,
33-
WF,
34-
vertex_property_map_value_t<Weight>,
35-
CompareOp,
27+
class WF = function<vertex_property_map_value_t<Weight>(const remove_reference_t<G>&,
28+
const edge_t<G>&)>,
29+
class CompareOp = less<vertex_property_map_value_t<Weight>>>
30+
requires vertex_property_map_for<Predecessor, G> &&
31+
vertex_property_map_for<Weight, G> &&
32+
basic_edge_weight_function<G, WF, vertex_property_map_value_t<Weight>, CompareOp,
3633
plus<vertex_property_map_value_t<Weight>>>
37-
auto prim(G&& g,
38-
Predecessor& predecessor,
39-
Weight& weight,
40-
CompareOp compare,
41-
vertex_property_map_value_t<Weight> init_dist,
42-
WF&& weight_fn,
43-
vertex_id_t<G> seed = 0);
34+
auto prim(G&& g,
35+
const vertex_id_t<G>& seed,
36+
Predecessor& predecessor,
37+
Weight& weight,
38+
WF&& weight_fn = [](const auto&, const edge_t<G>& uv) { return edge_value(g, uv); },
39+
CompareOp compare = less<vertex_property_map_value_t<Weight>>());

‎D3128_Algorithms/src/prim.hpp‎

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1-
template <adjacency_list G,
2-
class Predecessor,
3-
class Weight>
4-
requires vertex_property_map_for<Predecessor, G> &&
5-
vertex_property_map_for<Weight, G>
6-
void prim(G&& g, Predecessor& predecessor, Weight& weight, const vertex_id_t<G>& source = 0);
7-
81
template <adjacency_list G,
92
class Predecessor,
103
class Weight,
11-
class CompareOp,
12-
class WF>
4+
class WF = function<vertex_property_map_value_t<Weight>(const remove_reference_t<G>&,
5+
const edge_t<G>&)>,
6+
class CompareOp = less<vertex_property_map_value_t<Weight>>>
137
requires vertex_property_map_for<Predecessor, G> &&
148
vertex_property_map_for<Weight, G> &&
15-
basic_edge_weight_function<G, WF, vertex_property_map_value_t<Weight>, CompareOp, plus<vertex_property_map_value_t<Weight>>>
16-
void prim(G&& g,
17-
Predecessor& predecessor,
18-
Weight& weight,
19-
CompareOp compare,
20-
vertex_property_map_value_t<Weight> init_dist,
21-
WF&& weight_fn,
22-
vertex_id_t<G> source = 0);
9+
basic_edge_weight_function<G, WF, vertex_property_map_value_t<Weight>, CompareOp,
10+
plus<vertex_property_map_value_t<Weight>>>
11+
auto prim(G&& g,
12+
const vertex_id_t<G>& seed,
13+
Predecessor& predecessor,
14+
Weight& weight,
15+
WF&& weight_fn = [](const auto&, const edge_t<G>& uv) { return edge_value(g, uv); },
16+
CompareOp compare = less<vertex_property_map_value_t<Weight>>());

‎D3128_Algorithms/tex/algorithms.tex‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,8 +1691,7 @@ \subsection{Inplace Kruskal Minimum Spanning Tree}
16911691

16921692
\subsection{Prim Minimum Spanning Tree}
16931693
Find the minimum weight spanning tree of a graph using Prim's algorithm.
1694-
1695-
\phil{Use general form of dijkstra's shortest path?}
1694+
Delegates to \lstinline{dijkstra_shortest_paths} with a projecting combine function that uses the edge weight directly rather than accumulating path distance.
16961695

16971696
\begin{table}[ht]
16981697
\setcellgapes{3pt}
@@ -1712,14 +1711,14 @@ \subsection{Prim Minimum Spanning Tree}
17121711
\end{table}
17131712

17141713
{\small
1715-
\lstinputlisting[firstline=24,lastline=43]{D3128_Algorithms/src/mst.hpp}
1714+
\lstinputlisting[firstline=24,lastline=39]{D3128_Algorithms/src/mst.hpp}
17161715
}
17171716

17181717
\begin{itemdescr}
17191718
\pnum\mandates
17201719
\begin{itemize}
17211720
\item
1722-
\lstinline{compare} operator is a valid comparison operation on two edge values of type \lstinline{edge_value_t<G>} which returns a bool.
1721+
\lstinline{compare} is a valid strict weak ordering on values of type \lstinline{vertex_property_map_value_t<Weight>}.
17231722
\end{itemize}
17241723
\pnum\hardprecond
17251724
\begin{itemize}
@@ -1729,30 +1728,45 @@ \subsection{Prim Minimum Spanning Tree}
17291728
\lstinline{weight} contains an entry for each vertex of \lstinline{g}.
17301729
\item
17311730
\lstinline{predecessor} contains an entry for each vertex of \lstinline{g}.
1731+
\item
1732+
\lstinline{weight} and \lstinline{predecessor} have been initialized before calling
1733+
(e.g.\ via \lstinline{init_shortest_paths(g, weight, predecessor)}).
17321734
\end{itemize}
17331735
\pnum\preconditions
17341736
\begin{itemize}
17351737
\item
1736-
\lstinline{weight_fn}, when provided, must satisfy
1737-
\lstinline{basic_edge_weight_function<G, WF, vertex_property_map_value_t<Weight>, CompareOp>}.
1738+
\lstinline{weight_fn} must satisfy
1739+
\lstinline{basic_edge_weight_function<G, WF, vertex_property_map_value_t<Weight>, CompareOp,}
1740+
\lstinline{plus<vertex_property_map_value_t<Weight>>>}.
17381741
When omitted, the default is \lstinline{edge_value(g, uv)}.
17391742
\end{itemize}
17401743
\pnum\effects
17411744
\begin{itemize}
17421745
\item
1743-
\lstinline{predecessor[v]} is the parent vertex of \lstinline{v} in a tree rooted at \lstinline{seed} and \lstinline{weight[v]} is the value of the edge between \lstinline{v} and \lstinline{predecessor[v]} in the tree. When \lstinline{compare} is \lstinline{<} and \lstinline{init_dist==+inf}, \lstinline{predecessor} represents a minimum weight spanning tree.
1746+
\lstinline{predecessor[v]} is the parent vertex of \lstinline{v} in a tree rooted at \lstinline{seed}
1747+
and \lstinline{weight[v]} is the value of the edge between \lstinline{v} and \lstinline{predecessor[v]}
1748+
in the tree. \lstinline{predecessor[seed] == seed}.
1749+
When \lstinline{compare} is \lstinline{<}, \lstinline{predecessor} represents a minimum weight spanning tree.
17441750
\item
1745-
If \lstinline{predecessor} and \lstinline{weight} are not initialized by the user, and the graph is not fully connected, \lstinline{predecessor[v]} and \lstinline{weight[v]} will be undefined for vertices not in the same connected component as \lstinline{seed}.
1751+
For vertices not reachable from \lstinline{seed}, \lstinline{predecessor[v]} and \lstinline{weight[v]}
1752+
retain their initialized values (as set by \lstinline{init_shortest_paths}).
17461753
\end{itemize}
17471754
%\pnum\result
17481755
\pnum\returns The total weight of the minimum spanning tree rooted at \lstinline{seed}.
1749-
%\pnum\throws
1756+
\pnum\throws
1757+
\begin{itemize}
1758+
\item \lstinline{std::out_of_range} if \lstinline{seed} is not a valid vertex ID in \lstinline{g}.
1759+
\item \lstinline{std::out_of_range} if \lstinline{predecessor} or \lstinline{weight} are undersized for the graph.
1760+
\item \lstinline{std::out_of_range} if a negative edge weight is encountered (for signed weight types).
1761+
\item \lstinline{std::logic_error} if an internal invariant violation is detected.
1762+
\end{itemize}
17501763
\pnum\complexity
17511764
\begin{itemize}
17521765
\item \textbf{Time:} $\mathcal{O}(|E|\log{|V|})$.
17531766
\item \textbf{Space:} $\mathcal{O}(|V|)$ auxiliary --- priority queue and bookkeeping arrays.
17541767
\end{itemize}
1755-
%\pnum\remarks
1768+
\pnum\remarks Only produces a spanning tree for the connected component containing \lstinline{seed}.
1769+
For disconnected graphs, call \lstinline{prim} once per component with a seed vertex from each component.
17561770
%\pnum\errors
17571771
\end{itemdescr}
17581772

0 commit comments

Comments
 (0)