Merge -r 127928:132243 from trunk
[official-gcc.git] / libstdc++-v3 / testsuite / ext / pb_ds / example / priority_queue_dijkstra.cc
blobaf582b37b341e2737ccc15a6d9e6277a520f49f1
1 // -*- C++ -*-
3 // Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4 //
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 2, or (at your option) any later
9 // version.
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 COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
29 // Public License.
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
40 // warranty.
42 /**
43 * @file priority_queue_dijkstra_example.cpp
44 * A basic example showing how to cross reference a vector and a
45 * priority-queue for modify.
48 /**
49 * This example shows how to cross-reference priority queues
50 * and a vector. I.e., using a vector to
51 * map keys to entries in a priority queue, and using the priority
52 * queue to map entries to the vector. The combination
53 * can be used for fast modification of keys.
55 * As an example, a very simple form of Diskstra's algorithm is used. The graph
56 * is represented by an adjacency matrix. Nodes and vertices are size_ts, and
57 * it is assumed that the minimal path between any two nodes is less than 1000.
62 #include <vector>
63 #include <iostream>
64 #include <ext/pb_ds/priority_queue.hpp>
66 using namespace std;
67 using namespace __gnu_pbds;
69 // The value type of the priority queue.
70 // The first entry is the node's id, and the second is the distance.
71 typedef std::pair<size_t, size_t> pq_value;
73 // Comparison functor used to compare priority-queue value types.
74 struct pq_value_cmp : public binary_function<pq_value, pq_value, bool>
76 inline bool
77 operator()(const pq_value& r_lhs, const pq_value& r_rhs) const
79 // Note that a value is considered smaller than a different value
80 // if its distance is* larger*. This is because by STL
81 // conventions, "larger" entries are nearer the top of the
82 // priority queue.
83 return r_rhs.second < r_lhs.second;
87 int main()
89 enum
91 // Number of vertices is hard-coded in this example.
92 num_vertices = 5,
93 // "Infinity".
94 graph_inf = 1000
97 // The edge-distance matrix.
98 // For example, the distance from node 0 to node 1 is 5, and the
99 // distance from node 1 to node 0 is 2.
100 const size_t a_a_edge_legnth[num_vertices][num_vertices] =
102 {0, 5, 3, 7, 6},
103 {2, 0, 2, 8, 9},
104 {2, 1, 0, 8, 0},
105 {1, 8, 3, 0, 2},
106 {2, 3, 4, 2, 0}
109 // The priority queue type.
110 typedef __gnu_pbds::priority_queue< pq_value, pq_value_cmp> pq_t;
112 // The priority queue object.
113 pq_t p;
115 // This vector contains for each node, a find-iterator into the
116 // priority queue.
117 vector<pq_t::point_iterator> a_it;
119 // First we initialize the data structures.
121 // For each node, we push into the priority queue a value
122 // identifying it with a distance of infinity.
123 for (size_t i = 0; i < num_vertices; ++i)
124 a_it.push_back(p.push(pq_value(i, graph_inf)));
126 // Now we take the initial node, in this case 0, and modify its
127 // distance to 0.
128 p.modify(a_it[0], pq_value(0, 0));
130 // The priority queue contains all vertices whose final distance has
131 // not been determined, so to finish the algorithm, we must loop
132 // until it is empty.
133 while (!p.empty())
135 // First we find the node whose distance is smallest.
136 const pq_value& r_v = p.top();
137 const size_t node_id = r_v.first;
138 const size_t dist = r_v.second;
140 // This is the node's final distance, so we can print it out.
141 cout << "The distance from 0 to " << node_id
142 << " is " << dist << endl;
144 // Now we go over the node's neighbors and "relax" the
145 // distances, if applicable.
146 for (size_t neighbor_i = 0; neighbor_i < num_vertices; ++neighbor_i)
148 // Potentially, the distance to the neighbor is the distance
149 // to the currently-considered node + the distance from this
150 // node to the neighbor.
151 const size_t pot_dist = dist + a_a_edge_legnth[node_id][neighbor_i];
153 if (a_it[neighbor_i] == a_it[0])
154 continue;
156 // "Relax" the distance (if appropriate) through modify.
157 if (pot_dist < a_it[neighbor_i]->second)
158 p.modify(a_it[neighbor_i], pq_value(neighbor_i, pot_dist));
161 // Done with the node, so we pop it.
162 a_it[node_id] = a_it[0];
163 p.pop();
166 return 0;