Skip to content

Commit

Permalink
Merge pull request #9 from nike0good/toposort
Browse files Browse the repository at this point in the history
[toposort] PR of week 2
  • Loading branch information
nike0good committed Jun 9, 2019
2 parents 2da1592 + 75c2cb3 commit c9642b8
Show file tree
Hide file tree
Showing 18 changed files with 344 additions and 110 deletions.
2 changes: 1 addition & 1 deletion configuration.conf
Expand Up @@ -38,7 +38,7 @@ costFlow | Y | Y | Y
ChPP | Y | Y | Y
spanningTree | Y | Y | Y
mincut | Y | Y | Y
topological_sort | Y | Y | N
topologicalSort | Y | Y | N

#----------------------
# SQL only directories
Expand Down
44 changes: 44 additions & 0 deletions doc/queries/doc-topologicalSort.queries
@@ -0,0 +1,44 @@
BEGIN;
BEGIN
SET client_min_messages TO NOTICE;
SET
SELECT * FROM pgr_topologicalSort(
'SELECT id, source, target, cost, reverse_cost FROM edge_table'
);
ERROR: function pgr_topologicalsort(unknown) does not exist
LINE 1: SELECT * FROM pgr_topologicalSort(
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
-- q1
SELECT * FROM pgr_topologicalSort(
'SELECT id, source, target, cost, reverse_cost
FROM edge_table
WHERE id < 17'
);
ERROR: current transaction is aborted, commands ignored until end of transaction block
-- q2
SELECT * FROM pgr_topologicalSort(
'SELECT id, source, target, cost, reverse_cost
FROM edge_table
WHERE id = 18'
);
ERROR: current transaction is aborted, commands ignored until end of transaction block
-- q3
SELECT * FROM pgr_topologicalSort(
$$
SELECT id, source, target, cost, reverse_cost FROM edge_table
where source = any (ARRAY(SELECT node FROM pgr_connectedComponents(
'SELECT id, source, target, cost, reverse_cost FROM edge_table ')
WHERE component = 14)
)
OR
target = any (ARRAY(SELECT node FROM pgr_connectedComponents(
'SELECT id, source, target, cost, reverse_cost FROM edge_table ')
WHERE component = 14)
)
$$
);
ERROR: current transaction is aborted, commands ignored until end of transaction block
-- q4
ROLLBACK;
ROLLBACK
@@ -1,5 +1,8 @@
/*PGR-GNU*****************************************************************
File: pgr_topological_sort_t.h
File: pgr_topologicalSortt.h
Copyright (c) 2015 pgRouting developers
Mail: project@pgrouting.org
Function's developer:
Copyright (c) 2019 Hang Wu
Expand All @@ -20,8 +23,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
********************************************************************PGR-GNU*/
/*! @file */

#ifndef INCLUDE_C_TYPES_PGR_TOPOLOGICAL_SORT_T_H_
#define INCLUDE_C_TYPES_PGR_TOPOLOGICAL_SORT_T_H_
#ifndef INCLUDE_C_TYPES_PGR_TOPOLOGICALSORT_T_H_
#define INCLUDE_C_TYPES_PGR_TOPOLOGICALSORT_T_H_
#pragma once

/* for int64_t */
Expand All @@ -34,6 +37,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
typedef struct {
int seq;
int sorted_v;
} pgr_topological_sort_t;
} pgr_topologicalSort_t;

#endif // INCLUDE_C_TYPES_PGR_TOPOLOGICAL_SORT_T_H_
#endif // INCLUDE_C_TYPES_PGR_TOPOLOGICALSORT_T_H_
42 changes: 0 additions & 42 deletions include/cpp_common/pgr_topological_sort_t.h

This file was deleted.

@@ -1,6 +1,6 @@
/*PGR-GNU*****************************************************************
File: topological_sort_driver.h
File: topologicalSort_driver.h
Generated with Template by:
Copyright (c) 2015 pgRouting developers
Expand Down Expand Up @@ -28,8 +28,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
********************************************************************PGR-GNU*/

#ifndef INCLUDE_DRIVERS_TOPOLOGICAL_SORT_TOPOLOGICAL_SORT_DRIVER_H_
#define INCLUDE_DRIVERS_TOPOLOGICAL_SORT_TOPOLOGICAL_SORT_DRIVER_H_
#ifndef INCLUDE_DRIVERS_TOPOLOGICALSORT_TOPOLOGICALSORT_DRIVER_H_
#define INCLUDE_DRIVERS_TOPOLOGICALSORT_TOPOLOGICALSORT_DRIVER_H_

/* for size-t */
#ifdef __cplusplus
Expand All @@ -39,19 +39,19 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#endif

#include "c_types/pgr_edge_t.h"
#include "c_types/pgr_topological_sort_t.h"
#include "c_types/pgr_topologicalSort_t.h"

#ifdef __cplusplus
extern "C" {
#endif
// CREATE OR REPLACE FUNCTION pgr_topological_sort(
// CREATE OR REPLACE FUNCTION pgr_topologicalSort(
// sql text,
void do_pgr_topological_sort(
void do_pgr_topologicalSort(
pgr_edge_t *data_edges,
size_t total_tuples,

pgr_topological_sort_t **return_tuples,
pgr_topologicalSort_t **return_tuples,
size_t *return_count,

char** log_msg,
Expand All @@ -62,4 +62,4 @@ extern "C" {
}
#endif

#endif // INCLUDE_DRIVERS_TOPOLOGICAL_SORT_DRIVER_H_
#endif // INCLUDE_DRIVERS_TOPOLOGICALSORT_TOPOLOGICALSORT_DRIVER_H_
121 changes: 121 additions & 0 deletions include/topologicalSort/pgr_topologicalSort.hpp
@@ -0,0 +1,121 @@
/*PGR-GNU*****************************************************************
File: pgr_topologicalSort.hpp
Copyright (c) 2015 pgRouting developers
Mail: project@pgrouting.org
Function's developer:
Copyright (c) 2019 Hang Wu
mail: nike0good@gmail.com
------
This program 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 of the License, or
(at your option) any later version.
This program 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 program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
********************************************************************PGR-GNU*/

#ifndef INCLUDE_TOPOLOGICALSORT_PGR_TOPOLOGICALSORT_HPP_
#define INCLUDE_TOPOLOGICALSORT_PGR_TOPOLOGICALSORT_HPP_
#pragma once

#include <boost/config.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/one_bit_color_map.hpp>
#include <boost/graph/stoer_wagner_min_cut.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/typeof/typeof.hpp>
#include <boost/graph/topological_sort.hpp>

#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <functional>
#include <limits>

#include "cpp_common/basePath_SSEC.hpp"
#include "cpp_common/pgr_base_graph.hpp"

template < class G > class Pgr_topologicalSort;
// user's functions
// for development

//******************************************

template < class G >
class Pgr_topologicalSort {
public:
typedef typename G::V V;
typedef typename G::E E;
typedef typename G::E_i E_i;

std::vector<pgr_topologicalSort_t> topologicalSort(
G &graph);

private:
std::vector< pgr_topologicalSort_t >
generatetopologicalSort(
const G &graph ) {
std::vector< pgr_topologicalSort_t > results;

// auto parities = boost::make_one_bit_color_map(
// num_vertices(graph.graph),
// get(boost::vertex_index, graph.graph));

// double w = stoer_wagner_min_cut(
// graph.graph,
// get(&G::G_T_E::cost, graph.graph),
// boost::parity_map(parities));

// double totalcost = 0;
// E_i ei, ei_end;
// for (boost::tie(ei, ei_end) = edges(graph.graph); ei != ei_end; ei++) {
// auto s = source(*ei, graph.graph);
// auto t = target(*ei, graph.graph);

// if (get(parities, s) != get(parities, t)) {
// pgr_topologicalSort_t tmp;

// tmp.cost = graph[*ei].cost;

// auto edge_id =
// graph.get_edge_id(
// source(*ei, graph.graph),
// target(*ei, graph.graph),
// tmp.cost);

// tmp.edge = edge_id;
// totalcost += tmp.cost;
// tmp.mincut = totalcost;
// results.push_back(tmp);
// }
// }

// pgassert(w == totalcost);
pgr_topologicalSort_t tmp;
tmp.seq=tmp.sorted_v=1;
results.push_back(tmp);
return results;
}
};

template < class G >
std::vector<pgr_topologicalSort_t>
Pgr_topologicalSort< G >::topologicalSort(
G &graph) {
// pgassert(num_vertices(graph.graph) > 1);
return generatetopologicalSort(
graph);
}


#endif // INCLUDE_TOPOLOGICALSORT_PGR_TOPOLOGICALSORT_HPP_
14 changes: 14 additions & 0 deletions pgtap/topologicalSort/topologicalSort-innerQuery.sql
@@ -0,0 +1,14 @@
\i setup.sql

SELECT plan(2);

SELECT has_function('pgr_topologicalSort',
ARRAY['text']);

SELECT function_returns('pgr_topologicalSort',
ARRAY['text'],
'setof record');


SELECT * FROM finish();
ROLLBACK;
4 changes: 2 additions & 2 deletions sql/sigs/pgrouting--3.0.0.sig
Expand Up @@ -178,8 +178,8 @@ _pgr_stoerwagner(text)
pgr_stoerwagner(text)
_pgr_strongcomponents(text)
pgr_strongcomponents(text)
_pgr_topological_sort(text)
pgr_topological_sort(text)
_pgr_topologicalsort(text)
pgr_topologicalsort(text)
_pgr_trsp(text,integer,double precision,integer,double precision,boolean,boolean,text)
pgr_trsp(text,integer,double precision,integer,double precision,boolean,boolean,text)
pgr_trsp(text,integer,integer,boolean,boolean,text)
Expand Down
@@ -1,7 +1,7 @@

SET(LOCAL_FILES
_topological_sort.sql
topological_sort.sql
_topologicalSort.sql
topologicalSort.sql
)

foreach (f ${LOCAL_FILES})
Expand Down
Expand Up @@ -3,6 +3,7 @@
Copyright (c) 2015 pgRouting developers
Mail: project@pgrouting.org
Function's developer:
Copyright (c) 2019 Hang Wu
mail: nike0good@gmail.com
Expand All @@ -26,20 +27,20 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

---------------
---------------
-- topological_sort
-- topologicalSort
---------------
---------------

CREATE OR REPLACE FUNCTION _pgr_topological_sort(
CREATE OR REPLACE FUNCTION _pgr_topologicalSort(
edges_sql TEXT,

OUT seq INTEGER,
OUT sorted_v INTEGER)
RETURNS SETOF RECORD AS
'MODULE_PATHNAME', 'topological_sort'
'MODULE_PATHNAME', 'topologicalSort'
LANGUAGE C VOLATILE STRICT;

-- COMMENTS

COMMENT ON FUNCTION _pgr_topological_sort(TEXT)
COMMENT ON FUNCTION _pgr_topologicalSort(TEXT)
IS 'pgRouting internal function';

0 comments on commit c9642b8

Please sign in to comment.