openGPMP
Open Source Mathematics Package
ospf.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file.
25  *
26  *
27  *
28  * This software is distributed on an AS IS basis, WITHOUT
29  * WARRANTY OF ANY KIND, either express or implied.
30  *
31  ************************************************************************/
32 
33 /*
34  * definitions for openGPMP naive implementation of Open Shortest Path First
35  * (OSPF)
36  */
37 
38 #include <iostream>
39 #include <limits>
40 #include <queue>
41 #include <vector>
42 
43 // Constants
44 const int INF = std::numeric_limits<int>::max();
45 
46 // Data structure to represent edges
47 struct Edge {
48  int to;
49  int weight;
50 };
51 
52 // Dijkstra's algorithm implementation
53 void dijkstra(const std::vector<std::vector<Edge>> &graph,
54  int source,
55  std::vector<int> &distances,
56  std::vector<int> &parents) {
57  int n = graph.size();
58  distances.assign(n, INF);
59  parents.assign(n, -1);
60 
61  distances[source] = 0;
62  std::priority_queue<std::pair<int, int>,
63  std::vector<std::pair<int, int>>,
64  std::greater<std::pair<int, int>>>
65  pq;
66  pq.push(std::make_pair(0, source));
67 
68  while (!pq.empty()) {
69  int current = pq.top().second;
70  int distance = pq.top().first;
71  pq.pop();
72 
73  if (distance > distances[current])
74  continue;
75 
76  for (const Edge &edge : graph[current]) {
77  int neighbor = edge.to;
78  int new_distance = distance + edge.weight;
79 
80  if (new_distance < distances[neighbor]) {
81  distances[neighbor] = new_distance;
82  parents[neighbor] = current;
83  pq.push(std::make_pair(new_distance, neighbor));
84  }
85  }
86  }
87 }
88 
89 int main() {
90  int n = 4; // Number of nodes
91  std::vector<std::vector<Edge>> graph(n);
92 
93  graph[0].push_back({1, 2});
94  graph[0].push_back({2, 5});
95  graph[1].push_back({2, 1});
96  graph[1].push_back({3, 3});
97  graph[2].push_back({3, 1});
98 
99  int source = 0;
100  std::vector<int> distances, parents;
101  dijkstra(graph, source, distances, parents);
102 
103  int destination = 3;
104  std::cout << "Shortest distance from " << source << " to " << destination
105  << ": " << distances[destination] << std::endl;
106  std::cout << "Shortest path: ";
107  for (int node = destination; node != -1; node = parents[node])
108  std::cout << node << " ";
109  std::cout << std::endl;
110 
111  return 0;
112 }
const int INF
Definition: ospf.cpp:44
void dijkstra(const std::vector< std::vector< Edge >> &graph, int source, std::vector< int > &distances, std::vector< int > &parents)
Definition: ospf.cpp:53
int main()
Definition: ospf.cpp:89
Definition: ospf.cpp:47
int weight
Definition: ospf.cpp:49
int to
Definition: ospf.cpp:48