aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/ext/pb_ds/example/priority_queue_dijkstra.cc
blob: af582b37b341e2737ccc15a6d9e6277a520f49f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// -*- C++ -*-

// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later
// version.

// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.

// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.

/**
 * @file priority_queue_dijkstra_example.cpp
 * A basic example showing how to cross reference a vector and a
 * priority-queue for modify.
 */

/**
 * This example shows how to cross-reference priority queues
 * and a vector. I.e., using a vector to
 * map keys to entries in a priority queue, and using the priority
 * queue to map entries to the vector. The combination
 * can be used for fast modification of keys.
 *
 * As an example, a very simple form of Diskstra's algorithm is used. The graph
 * is represented by an adjacency matrix. Nodes and vertices are size_ts, and
 * it is assumed that the minimal path between any two nodes is less than 1000.
 */



#include <vector>
#include <iostream>
#include <ext/pb_ds/priority_queue.hpp>

using namespace std;
using namespace __gnu_pbds;

// The value type of the priority queue.
// The first entry is the node's id, and the second is the distance.
typedef std::pair<size_t, size_t> pq_value;

// Comparison functor used to compare priority-queue value types.
struct pq_value_cmp : public binary_function<pq_value, pq_value, bool>
{
  inline bool
  operator()(const pq_value& r_lhs, const pq_value& r_rhs) const
  {
    // Note that a value is considered smaller than a different value
    // if its distance is* larger*. This is because by STL
    // conventions, "larger" entries are nearer the top of the
    // priority queue.
    return r_rhs.second < r_lhs.second;
  }
};

int main()
{
  enum
    {
      // Number of vertices is hard-coded in this example.
      num_vertices = 5,
      // "Infinity".
      graph_inf = 1000
    };

  // The edge-distance matrix.
  // For example, the distance from node 0 to node 1 is 5, and the
  // distance from node 1 to node 0 is 2.
  const size_t a_a_edge_legnth[num_vertices][num_vertices] =
    {
      {0, 5, 3, 7, 6},
      {2, 0, 2, 8, 9},
      {2, 1, 0, 8, 0},
      {1, 8, 3, 0, 2},
      {2, 3, 4, 2, 0}
    };

  // The priority queue type.
  typedef __gnu_pbds::priority_queue< pq_value, pq_value_cmp> pq_t;

  // The priority queue object.
  pq_t p;

  // This vector contains for each node, a find-iterator into the
  // priority queue.
  vector<pq_t::point_iterator> a_it;

  // First we initialize the data structures.

  // For each node, we push into the priority queue a value
  // identifying it with a distance of infinity.
  for (size_t i = 0; i < num_vertices; ++i)
    a_it.push_back(p.push(pq_value(i, graph_inf)));

  // Now we take the initial node, in this case 0, and modify its
  // distance to 0.
  p.modify(a_it[0], pq_value(0, 0));

  // The priority queue contains all vertices whose final distance has
  // not been determined, so to finish the algorithm, we must loop
  // until it is empty.
  while (!p.empty())
    {
      // First we find the node whose distance is smallest.
      const pq_value& r_v = p.top();
      const size_t node_id = r_v.first;
      const size_t dist = r_v.second;

      // This is the node's final distance, so we can print it out.
      cout << "The distance from 0 to " << node_id 
	   << " is " << dist << endl;

      // Now we go over the node's neighbors and "relax" the
      // distances, if applicable.
      for (size_t neighbor_i = 0; neighbor_i < num_vertices; ++neighbor_i)
        {
	  // Potentially, the distance to the neighbor is the distance
	  // to the currently-considered node + the distance from this
	  // node to the neighbor.
	  const size_t pot_dist = dist + a_a_edge_legnth[node_id][neighbor_i];

	  if (a_it[neighbor_i] == a_it[0])
	    continue;

	  // "Relax" the distance (if appropriate) through modify.
	  if (pot_dist < a_it[neighbor_i]->second)
	    p.modify(a_it[neighbor_i], pq_value(neighbor_i, pot_dist));
        }

      // Done with the node, so we pop it.
      a_it[node_id] = a_it[0];
      p.pop();
    }

  return 0;
}