PGROUTING  3.2
kruskal_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: kruskal_driver.cpp
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2018 Aditya Pratap Singh
11 ------
12 
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17 
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22 
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 
27  ********************************************************************PGR-GNU*/
28 
30 
31 #include <sstream>
32 #include <deque>
33 #include <vector>
34 #include <string>
35 
36 #include "cpp_common/pgr_alloc.hpp"
37 #include "cpp_common/pgr_assert.h"
38 
40 #include "spanningTree/details.hpp"
41 
42 
43 void
45  pgr_edge_t *data_edges,
46  size_t total_edges,
47 
48  int64_t *rootsArr,
49  size_t size_rootsArr,
50 
51  char* fn_suffix,
52 
53  int64_t max_depth,
54  double distance,
55 
56  pgr_mst_rt **return_tuples,
57  size_t *return_count,
58 
59  char ** log_msg,
60  char ** notice_msg,
61  char ** err_msg) {
62  std::ostringstream log;
63  std::ostringstream err;
64  std::ostringstream notice;
65  try {
66  pgassert(!(*log_msg));
67  pgassert(!(*notice_msg));
68  pgassert(!(*err_msg));
69  pgassert(!(*return_tuples));
70  pgassert(*return_count == 0);
71 
72  std::vector<int64_t> roots(rootsArr, rootsArr + size_rootsArr);
73  std::string suffix(fn_suffix);
74 
75  std::vector<pgr_mst_rt> results;
76 
77  if (total_edges == 0) {
79  } else {
81  undigraph.insert_edges(data_edges, total_edges);
82 
85 
86  if (suffix == "") {
87  results = kruskal.kruskal(undigraph);
88  } else if (suffix == "BFS") {
89  results = kruskal.kruskalBFS(undigraph, roots, max_depth);
90  } else if (suffix == "DFS") {
91  results = kruskal.kruskalDFS(undigraph, roots, max_depth);
92  } else if (suffix == "DD") {
93  results = kruskal.kruskalDD(undigraph, roots, distance);
94  } else {
95  err << "Unknown Kruskal function";
96  *err_msg = pgr_msg(err.str().c_str());
97  return;
98  }
99  }
100 
101  auto count = results.size();
102 
103  if (count == 0) {
104  (*return_tuples) = NULL;
105  (*return_count) = 0;
106  notice << "No spanning tree found";
107  return;
108  }
109 
110  (*return_tuples) = pgr_alloc(count, (*return_tuples));
111  for (size_t i = 0; i < count; i++) {
112  *((*return_tuples) + i) = results[i];
113  }
114  (*return_count) = count;
115 
116  pgassert(*err_msg == NULL);
117  *log_msg = log.str().empty()?
118  *log_msg :
119  pgr_msg(log.str().c_str());
120  *notice_msg = notice.str().empty()?
121  *notice_msg :
122  pgr_msg(notice.str().c_str());
123  } catch (AssertFailedException &except) {
124  (*return_tuples) = pgr_free(*return_tuples);
125  (*return_count) = 0;
126  err << except.what();
127  *err_msg = pgr_msg(err.str().c_str());
128  *log_msg = pgr_msg(log.str().c_str());
129  } catch (std::exception &except) {
130  (*return_tuples) = pgr_free(*return_tuples);
131  (*return_count) = 0;
132  err << except.what();
133  *err_msg = pgr_msg(err.str().c_str());
134  *log_msg = pgr_msg(log.str().c_str());
135  } catch(...) {
136  (*return_tuples) = pgr_free(*return_tuples);
137  (*return_count) = 0;
138  err << "Caught unknown exception!";
139  *err_msg = pgr_msg(err.str().c_str());
140  *log_msg = pgr_msg(log.str().c_str());
141  }
142 }
pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
pgr_mst_rt
Definition: pgr_mst_rt.h:36
pgr_edge_t
Definition: pgr_edge_t.h:37
pgr_msg
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:30
AssertFailedException::what
virtual const char * what() const
Definition: pgr_assert.cpp:67
pgr_kruskal.hpp
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
UNDIRECTED
@ UNDIRECTED
Definition: graph_enum.h:30
pgrouting::graph::Pgr_base_graph::insert_edges
void insert_edges(const T *edges, size_t count)
Inserts count edges of type T into the graph.
Definition: pgr_base_graph.hpp:357
pgr_alloc.hpp
pgr_assert.h
An assert functionality that uses C++ throw().
pgr_free
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:77
pgrouting::details::get_no_edge_graph_result
std::vector< pgr_mst_rt > get_no_edge_graph_result(std::vector< int64_t > vids)
Definition: details.cpp:45
pgrouting::graph::Pgr_base_graph
Definition: pgr_base_graph.hpp:168
details.hpp
kruskal_driver.h
pgrouting::functions::Pgr_kruskal
Definition: pgr_kruskal.hpp:41
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139
do_pgr_kruskal
void do_pgr_kruskal(pgr_edge_t *data_edges, size_t total_edges, int64_t *rootsArr, size_t size_rootsArr, char *fn_suffix, int64_t max_depth, double distance, pgr_mst_rt **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: kruskal_driver.cpp:44