]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/ext/pb_ds/example/priority_queue_dijkstra.cc
3 // Copyright (C) 2005-2023 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 3, or (at your option) any later
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
21 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
23 // Permission to use, copy, modify, sell, and distribute this software
24 // is hereby granted without fee, provided that the above copyright
25 // notice appears in all copies, and that both that copyright notice
26 // and this permission notice appear in supporting documentation. None
27 // of the above authors, nor IBM Haifa Research Laboratories, make any
28 // representation about the suitability of this software for any
29 // purpose. It is provided "as is" without express or implied
33 * @file priority_queue_dijkstra_example.cpp
34 * A basic example showing how to cross reference a vector and a
35 * priority-queue for modify.
39 * This example shows how to cross-reference priority queues
40 * and a vector. I.e., using a vector to
41 * map keys to entries in a priority queue, and using the priority
42 * queue to map entries to the vector. The combination
43 * can be used for fast modification of keys.
45 * As an example, a very simple form of Diskstra's algorithm is used. The graph
46 * is represented by an adjacency matrix. Nodes and vertices are size_ts, and
47 * it is assumed that the minimal path between any two nodes is less than 1000.
54 #include <ext/pb_ds/priority_queue.hpp>
57 using namespace __gnu_pbds
;
59 // The value type of the priority queue.
60 // The first entry is the node's id, and the second is the distance.
61 typedef std::pair
<size_t, size_t> pq_value
;
63 // Comparison functor used to compare priority-queue value types.
67 operator()(const pq_value
& r_lhs
, const pq_value
& r_rhs
) const
69 // Note that a value is considered smaller than a different value
70 // if its distance is* larger*. This is because by STL
71 // conventions, "larger" entries are nearer the top of the
73 return r_rhs
.second
< r_lhs
.second
;
81 // Number of vertices is hard-coded in this example.
87 // The edge-distance matrix.
88 // For example, the distance from node 0 to node 1 is 5, and the
89 // distance from node 1 to node 0 is 2.
90 const size_t a_a_edge_legnth
[num_vertices
][num_vertices
] =
99 // The priority queue type.
100 typedef __gnu_pbds::priority_queue
< pq_value
, pq_value_cmp
> pq_t
;
102 // The priority queue object.
105 // This vector contains for each node, a find-iterator into the
107 vector
<pq_t::point_iterator
> a_it
;
109 // First we initialize the data structures.
111 // For each node, we push into the priority queue a value
112 // identifying it with a distance of infinity.
113 for (size_t i
= 0; i
< num_vertices
; ++i
)
114 a_it
.push_back(p
.push(pq_value(i
, graph_inf
)));
116 // Now we take the initial node, in this case 0, and modify its
118 p
.modify(a_it
[0], pq_value(0, 0));
120 // The priority queue contains all vertices whose final distance has
121 // not been determined, so to finish the algorithm, we must loop
122 // until it is empty.
125 // First we find the node whose distance is smallest.
126 const pq_value
& r_v
= p
.top();
127 const size_t node_id
= r_v
.first
;
128 const size_t dist
= r_v
.second
;
130 // This is the node's final distance, so we can print it out.
131 cout
<< "The distance from 0 to " << node_id
132 << " is " << dist
<< endl
;
134 // Now we go over the node's neighbors and "relax" the
135 // distances, if applicable.
136 for (size_t neighbor_i
= 0; neighbor_i
< num_vertices
; ++neighbor_i
)
138 // Potentially, the distance to the neighbor is the distance
139 // to the currently-considered node + the distance from this
140 // node to the neighbor.
141 const size_t pot_dist
= dist
+ a_a_edge_legnth
[node_id
][neighbor_i
];
143 if (a_it
[neighbor_i
] == a_it
[0])
146 // "Relax" the distance (if appropriate) through modify.
147 if (pot_dist
< a_it
[neighbor_i
]->second
)
148 p
.modify(a_it
[neighbor_i
], pq_value(neighbor_i
, pot_dist
));
151 // Done with the node, so we pop it.
152 a_it
[node_id
] = a_it
[0];