├── paper ├── asdf └── FINDER-Overview.jpg ├── code ├── FINDER_CN │ ├── src │ │ └── lib │ │ │ ├── config.cpp │ │ │ ├── msg_pass.h │ │ │ ├── disjoint_set.h │ │ │ ├── i_env.h │ │ │ ├── graph_utils.h │ │ │ ├── graph.h │ │ │ ├── utils.h │ │ │ ├── mvc_env.h │ │ │ ├── config.h │ │ │ ├── nstep_replay_mem.h │ │ │ ├── graph_utils.cpp │ │ │ ├── nstep_replay_mem_prioritized.h │ │ │ ├── graph_struct.cpp │ │ │ ├── disjoint_set.cpp │ │ │ ├── PrepareBatchGraph.h │ │ │ ├── graph.cpp │ │ │ ├── msg_pass.cpp │ │ │ ├── decrease_strategy.cpp │ │ │ └── graph_struct.h │ ├── requirements.txt │ ├── models │ │ ├── nrange_30_50_iter_93300.ckpt.index │ │ ├── nrange_30_50_iter_93300.ckpt.meta │ │ └── nrange_30_50_iter_93300.ckpt.data-00000-of-00001 │ ├── train.py │ ├── utils.pxd │ ├── graph_struct.pxd │ ├── graph.pxd │ ├── mvc_env.pxd │ ├── PrepareBatchGraph.pxd │ ├── nstep_replay_mem.pxd │ ├── testSynthetic.py │ ├── setup.py │ ├── graph_struct.pyx │ ├── nstep_replay_mem_prioritized.pxd │ └── utils.pyx ├── FINDER_ND │ ├── src │ │ └── lib │ │ │ ├── config.cpp │ │ │ ├── msg_pass.h │ │ │ ├── disjoint_set.h │ │ │ ├── i_env.h │ │ │ ├── graph_utils.h │ │ │ ├── graph.h │ │ │ ├── utils.h │ │ │ ├── mvc_env.h │ │ │ ├── config.h │ │ │ ├── nstep_replay_mem.h │ │ │ ├── graph_utils.cpp │ │ │ ├── nstep_replay_mem_prioritized.h │ │ │ ├── disjoint_set.cpp │ │ │ ├── graph_struct.cpp │ │ │ ├── PrepareBatchGraph.h │ │ │ ├── graph.cpp │ │ │ ├── msg_pass.cpp │ │ │ ├── decrease_strategy.cpp │ │ │ └── graph_struct.h │ ├── requirements.txt │ ├── models │ │ ├── nrange_30_50_iter_78000.ckpt.index │ │ ├── nrange_30_50_iter_78000.ckpt.meta │ │ └── nrange_30_50_iter_78000.ckpt.data-00000-of-00001 │ ├── train.py │ ├── utils.pxd │ ├── graph_struct.pxd │ ├── graph.pxd │ ├── mvc_env.pxd │ ├── PrepareBatchGraph.pxd │ ├── nstep_replay_mem.pxd │ ├── testSynthetic.py │ ├── setup.py │ ├── graph_struct.pyx │ ├── nstep_replay_mem_prioritized.pxd │ └── utils.pyx ├── FINDER_CN_cost │ ├── src │ │ └── lib │ │ │ ├── config.cpp │ │ │ ├── disjoint_set.h │ │ │ ├── msg_pass.h │ │ │ ├── i_env.h │ │ │ ├── graph_utils.h │ │ │ ├── graph.h │ │ │ ├── utils.h │ │ │ ├── mvc_env.h │ │ │ ├── config.h │ │ │ ├── nstep_replay_mem.h │ │ │ ├── graph_utils.cpp │ │ │ ├── nstep_replay_mem_prioritized.h │ │ │ ├── graph_struct.cpp │ │ │ ├── disjoint_set.cpp │ │ │ ├── PrepareBatchGraph.h │ │ │ ├── graph.cpp │ │ │ ├── msg_pass.cpp │ │ │ ├── decrease_strategy.cpp │ │ │ └── graph_struct.h │ ├── requirements.txt │ ├── models │ │ ├── nrange_30_50_iter_122100.ckpt.meta │ │ ├── nrange_30_50_iter_122100.ckpt.index │ │ └── nrange_30_50_iter_122100.ckpt.data-00000-of-00001 │ ├── train.py │ ├── utils.pxd │ ├── graph_struct.pxd │ ├── mvc_env.pxd │ ├── graph.pxd │ ├── real2weight.py │ ├── PrepareBatchGraph.pxd │ ├── nstep_replay_mem.pxd │ ├── testSynthetic.py │ ├── setup.py │ ├── graph_struct.pyx │ ├── pkl2gml.py │ ├── nstep_replay_mem_prioritized.pxd │ └── utils.pyx └── FINDER_ND_cost │ ├── src │ └── lib │ │ ├── config.cpp │ │ ├── utils_py27.h │ │ ├── msg_pass.h │ │ ├── disjoint_set.h │ │ ├── i_env.h │ │ ├── graph_utils.h │ │ ├── graph.h │ │ ├── utils.h │ │ ├── mvc_env.h │ │ ├── config.h │ │ ├── nstep_replay_mem.h │ │ ├── graph_utils.cpp │ │ ├── utils_py27.cpp │ │ ├── nstep_replay_mem_prioritized.h │ │ ├── disjoint_set.cpp │ │ ├── graph_struct.cpp │ │ ├── PrepareBatchGraph.h │ │ ├── graph.cpp │ │ ├── msg_pass.cpp │ │ ├── decrease_strategy.cpp │ │ └── graph_struct.h │ ├── utils_py27.pyd │ ├── models │ ├── nrange_30_50_iter_134100.ckpt.meta │ ├── nrange_30_50_iter_134100.ckpt.index │ └── nrange_30_50_iter_134100.ckpt.data-00000-of-00001 │ ├── train.py │ ├── utils_py27.pxd │ ├── utils_py27.pyx │ ├── utils.pxd │ ├── graph_struct.pxd │ ├── mvc_env.pxd │ ├── graph.pxd │ ├── PrepareBatchGraph.pxd │ ├── nstep_replay_mem.pxd │ ├── testSynthetic.py │ ├── setup.py │ ├── graph_struct.pyx │ ├── real2weight.py │ ├── nstep_replay_mem_prioritized.pxd │ └── utils.pyx ├── requirements.txt ├── environment └── Dockerfile ├── LICENSE └── .gitignore /paper/asdf: -------------------------------------------------------------------------------- 1 | asdf 2 | -------------------------------------------------------------------------------- /code/FINDER_CN/src/lib/config.cpp: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | 3 | bool cfg::msg_average = false; -------------------------------------------------------------------------------- /code/FINDER_ND/src/lib/config.cpp: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | 3 | bool cfg::msg_average = false; -------------------------------------------------------------------------------- /code/FINDER_CN/requirements.txt: -------------------------------------------------------------------------------- 1 | tqdm 2 | cython 3 | networkx 4 | scipy 5 | numpy 6 | pandas -------------------------------------------------------------------------------- /code/FINDER_CN_cost/src/lib/config.cpp: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | 3 | bool cfg::msg_average = false; -------------------------------------------------------------------------------- /code/FINDER_ND/requirements.txt: -------------------------------------------------------------------------------- 1 | tqdm 2 | cython 3 | networkx 4 | scipy 5 | numpy 6 | pandas -------------------------------------------------------------------------------- /code/FINDER_ND_cost/src/lib/config.cpp: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | 3 | bool cfg::msg_average = false; -------------------------------------------------------------------------------- /code/FINDER_CN_cost/requirements.txt: -------------------------------------------------------------------------------- 1 | tqdm 2 | cython 3 | networkx 4 | scipy 5 | numpy 6 | pandas -------------------------------------------------------------------------------- /paper/FINDER-Overview.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FFrankyy/FINDER/HEAD/paper/FINDER-Overview.jpg -------------------------------------------------------------------------------- /code/FINDER_ND_cost/utils_py27.pyd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FFrankyy/FINDER/HEAD/code/FINDER_ND_cost/utils_py27.pyd -------------------------------------------------------------------------------- /code/FINDER_CN_cost/src/lib/disjoint_set.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FFrankyy/FINDER/HEAD/code/FINDER_CN_cost/src/lib/disjoint_set.h -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | cython==0.29.13 2 | networkx==2.3 3 | numpy==1.17.3 4 | pandas==0.25.2 5 | scipy==1.3.1 6 | tensorflow-gpu==1.14.0 7 | tqdm==4.36.1 8 | -------------------------------------------------------------------------------- /code/FINDER_CN/models/nrange_30_50_iter_93300.ckpt.index: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FFrankyy/FINDER/HEAD/code/FINDER_CN/models/nrange_30_50_iter_93300.ckpt.index -------------------------------------------------------------------------------- /code/FINDER_CN/models/nrange_30_50_iter_93300.ckpt.meta: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FFrankyy/FINDER/HEAD/code/FINDER_CN/models/nrange_30_50_iter_93300.ckpt.meta -------------------------------------------------------------------------------- /code/FINDER_ND/models/nrange_30_50_iter_78000.ckpt.index: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FFrankyy/FINDER/HEAD/code/FINDER_ND/models/nrange_30_50_iter_78000.ckpt.index -------------------------------------------------------------------------------- /code/FINDER_ND/models/nrange_30_50_iter_78000.ckpt.meta: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FFrankyy/FINDER/HEAD/code/FINDER_ND/models/nrange_30_50_iter_78000.ckpt.meta -------------------------------------------------------------------------------- /code/FINDER_CN_cost/models/nrange_30_50_iter_122100.ckpt.meta: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FFrankyy/FINDER/HEAD/code/FINDER_CN_cost/models/nrange_30_50_iter_122100.ckpt.meta -------------------------------------------------------------------------------- /code/FINDER_ND_cost/models/nrange_30_50_iter_134100.ckpt.meta: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FFrankyy/FINDER/HEAD/code/FINDER_ND_cost/models/nrange_30_50_iter_134100.ckpt.meta -------------------------------------------------------------------------------- /code/FINDER_CN_cost/models/nrange_30_50_iter_122100.ckpt.index: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FFrankyy/FINDER/HEAD/code/FINDER_CN_cost/models/nrange_30_50_iter_122100.ckpt.index -------------------------------------------------------------------------------- /code/FINDER_ND_cost/models/nrange_30_50_iter_134100.ckpt.index: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FFrankyy/FINDER/HEAD/code/FINDER_ND_cost/models/nrange_30_50_iter_134100.ckpt.index -------------------------------------------------------------------------------- /code/FINDER_CN/models/nrange_30_50_iter_93300.ckpt.data-00000-of-00001: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FFrankyy/FINDER/HEAD/code/FINDER_CN/models/nrange_30_50_iter_93300.ckpt.data-00000-of-00001 -------------------------------------------------------------------------------- /code/FINDER_ND/models/nrange_30_50_iter_78000.ckpt.data-00000-of-00001: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FFrankyy/FINDER/HEAD/code/FINDER_ND/models/nrange_30_50_iter_78000.ckpt.data-00000-of-00001 -------------------------------------------------------------------------------- /code/FINDER_CN_cost/models/nrange_30_50_iter_122100.ckpt.data-00000-of-00001: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FFrankyy/FINDER/HEAD/code/FINDER_CN_cost/models/nrange_30_50_iter_122100.ckpt.data-00000-of-00001 -------------------------------------------------------------------------------- /code/FINDER_ND_cost/models/nrange_30_50_iter_134100.ckpt.data-00000-of-00001: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FFrankyy/FINDER/HEAD/code/FINDER_ND_cost/models/nrange_30_50_iter_134100.ckpt.data-00000-of-00001 -------------------------------------------------------------------------------- /code/FINDER_CN/train.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from FINDER import FINDER 4 | 5 | def main(): 6 | dqn = FINDER() 7 | dqn.Train() 8 | 9 | 10 | if __name__=="__main__": 11 | main() 12 | -------------------------------------------------------------------------------- /code/FINDER_ND/train.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | # -*- coding: utf-8 -*- 3 | 4 | from FINDER import FINDER 5 | 6 | def main(): 7 | dqn = FINDER() 8 | dqn.Train() 9 | 10 | 11 | if __name__=="__main__": 12 | main() 13 | -------------------------------------------------------------------------------- /code/FINDER_CN_cost/train.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | # -*- coding: utf-8 -*- 3 | 4 | from FINDER import FINDER 5 | 6 | def main(): 7 | dqn = FINDER() 8 | dqn.Train() 9 | 10 | 11 | if __name__=="__main__": 12 | main() 13 | -------------------------------------------------------------------------------- /code/FINDER_ND_cost/train.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | # -*- coding: utf-8 -*- 3 | 4 | from FINDER import FINDER 5 | 6 | def main(): 7 | dqn = FINDER() 8 | dqn.Train() 9 | 10 | 11 | if __name__=="__main__": 12 | main() 13 | -------------------------------------------------------------------------------- /code/FINDER_ND_cost/utils_py27.pxd: -------------------------------------------------------------------------------- 1 | 2 | from libcpp.vector cimport vector 3 | from libcpp.set cimport set 4 | from libcpp.memory cimport shared_ptr 5 | from libcpp cimport bool 6 | 7 | cdef extern from "./src/lib/utils_py27.h": 8 | cdef cppclass Utils: 9 | Utils() 10 | double getRobustness(vector[int] edges_from,vector[int] edges_to,int num_nodes,vector[double] nodes_weight,vector[int] solution)except+ 11 | vector[double] MaxWccSzList 12 | 13 | -------------------------------------------------------------------------------- /code/FINDER_ND_cost/src/lib/utils_py27.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "disjoint_set.h" 8 | #include "graph_utils.h" 9 | 10 | class Utils 11 | { 12 | public: 13 | Utils(); 14 | double getRobustness(std::vector edges_from, std::vector edges_to,int num_nodes,std::vector nodes_weight, std::vector solution); 15 | std::vector MaxWccSzList; 16 | 17 | 18 | }; 19 | 20 | #endif -------------------------------------------------------------------------------- /code/FINDER_CN/src/lib/msg_pass.h: -------------------------------------------------------------------------------- 1 | #ifndef MSG_PASS_H 2 | #define MSG_PASS_H 3 | 4 | #include "graph_struct.h" 5 | #include 6 | #include 7 | 8 | std::shared_ptr n2n_construct(GraphStruct* graph); 9 | 10 | std::shared_ptr e2n_construct(GraphStruct* graph); 11 | 12 | std::shared_ptr n2e_construct(GraphStruct* graph); 13 | 14 | std::shared_ptr e2e_construct(GraphStruct* graph); 15 | 16 | std::shared_ptr subg_construct(GraphStruct* graph); 17 | 18 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND/src/lib/msg_pass.h: -------------------------------------------------------------------------------- 1 | #ifndef MSG_PASS_H 2 | #define MSG_PASS_H 3 | 4 | #include "graph_struct.h" 5 | #include 6 | #include 7 | 8 | std::shared_ptr n2n_construct(GraphStruct* graph); 9 | 10 | std::shared_ptr e2n_construct(GraphStruct* graph); 11 | 12 | std::shared_ptr n2e_construct(GraphStruct* graph); 13 | 14 | std::shared_ptr e2e_construct(GraphStruct* graph); 15 | 16 | std::shared_ptr subg_construct(GraphStruct* graph); 17 | 18 | #endif -------------------------------------------------------------------------------- /code/FINDER_CN_cost/src/lib/msg_pass.h: -------------------------------------------------------------------------------- 1 | #ifndef MSG_PASS_H 2 | #define MSG_PASS_H 3 | 4 | #include "graph_struct.h" 5 | #include 6 | #include 7 | 8 | std::shared_ptr n2n_construct(GraphStruct* graph); 9 | 10 | std::shared_ptr e2n_construct(GraphStruct* graph); 11 | 12 | std::shared_ptr n2e_construct(GraphStruct* graph); 13 | 14 | std::shared_ptr e2e_construct(GraphStruct* graph); 15 | 16 | std::shared_ptr subg_construct(GraphStruct* graph); 17 | 18 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND_cost/src/lib/msg_pass.h: -------------------------------------------------------------------------------- 1 | #ifndef MSG_PASS_H 2 | #define MSG_PASS_H 3 | 4 | #include "graph_struct.h" 5 | #include 6 | #include 7 | 8 | std::shared_ptr n2n_construct(GraphStruct* graph); 9 | 10 | std::shared_ptr e2n_construct(GraphStruct* graph); 11 | 12 | std::shared_ptr n2e_construct(GraphStruct* graph); 13 | 14 | std::shared_ptr e2e_construct(GraphStruct* graph); 15 | 16 | std::shared_ptr subg_construct(GraphStruct* graph); 17 | 18 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND_cost/utils_py27.pyx: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from libc.stdlib cimport free 3 | 4 | cdef class py_Utils: 5 | cdef Utils inner_Utils 6 | def __cinit__(self): 7 | self.inner_Utils = Utils() 8 | 9 | 10 | def getRobustness(self,edges_from,edges_to,num_nodes,nodes_weight,solution): 11 | return self.inner_Utils.getRobustness(edges_from,edges_to,num_nodes,nodes_weight,solution) 12 | 13 | 14 | @property 15 | def MaxWccSzList(self): 16 | return self.inner_Utils.MaxWccSzList 17 | -------------------------------------------------------------------------------- /code/FINDER_ND/src/lib/disjoint_set.h: -------------------------------------------------------------------------------- 1 | #ifndef DISJOINT_SET_H 2 | #define DISJOINT_SET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | class Disjoint_Set 10 | { 11 | public: 12 | Disjoint_Set(); 13 | Disjoint_Set(int graphSize); 14 | ~Disjoint_Set(); 15 | int findRoot(int node); 16 | void merge(int node1, int node2); 17 | double getBiggestComponentCurrentRatio() const; 18 | int getRank(int rootNode) const; 19 | std::vector unionSet; 20 | std::vector rankCount; 21 | int maxRankCount; 22 | }; 23 | 24 | 25 | 26 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND_cost/src/lib/disjoint_set.h: -------------------------------------------------------------------------------- 1 | #ifndef DISJOINT_SET_H 2 | #define DISJOINT_SET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | class Disjoint_Set 10 | { 11 | public: 12 | Disjoint_Set(); 13 | Disjoint_Set(int graphSize); 14 | ~Disjoint_Set(); 15 | int findRoot(int node); 16 | void merge(int node1, int node2); 17 | double getBiggestComponentCurrentRatio() const; 18 | int getRank(int rootNode) const; 19 | std::vector unionSet; 20 | std::vector rankCount; 21 | int maxRankCount; 22 | }; 23 | 24 | 25 | 26 | #endif -------------------------------------------------------------------------------- /code/FINDER_CN/src/lib/disjoint_set.h: -------------------------------------------------------------------------------- 1 | #ifndef DISJOINT_SET_H 2 | #define DISJOINT_SET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | class Disjoint_Set 10 | { 11 | public: 12 | Disjoint_Set(); 13 | Disjoint_Set(int graphSize); 14 | ~Disjoint_Set(); 15 | int findRoot(int node); 16 | void merge(int node1, int node2); 17 | double getBiggestComponentCurrentRatio() const; 18 | int getRank(int rootNode) const; 19 | std::vector unionSet; 20 | std::vector rankCount; 21 | int maxRankCount; 22 | double CCDScore; 23 | }; 24 | 25 | 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /code/FINDER_CN/src/lib/i_env.h: -------------------------------------------------------------------------------- 1 | #ifndef I_ENV_H 2 | #define I_ENV_H 3 | 4 | #include 5 | #include 6 | 7 | #include "graph.h" 8 | 9 | class IEnv 10 | { 11 | public: 12 | 13 | IEnv(double _norm) : norm(_norm), graph(nullptr) {} 14 | 15 | virtual void s0(std::shared_ptr _g) = 0; 16 | virtual double step(int a) = 0; 17 | virtual int randomAction() = 0; 18 | virtual bool isTerminal() = 0; 19 | virtual double getReward() = 0; 20 | double norm; 21 | std::shared_ptr graph; 22 | std::vector< std::vector > state_seq; 23 | std::vector act_seq, action_list; 24 | std::vector reward_seq, sum_rewards; 25 | }; 26 | 27 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND/src/lib/i_env.h: -------------------------------------------------------------------------------- 1 | #ifndef I_ENV_H 2 | #define I_ENV_H 3 | 4 | #include 5 | #include 6 | 7 | #include "graph.h" 8 | 9 | class IEnv 10 | { 11 | public: 12 | 13 | IEnv(double _norm) : norm(_norm), graph(nullptr) {} 14 | 15 | virtual void s0(std::shared_ptr _g) = 0; 16 | virtual double step(int a) = 0; 17 | virtual int randomAction() = 0; 18 | virtual bool isTerminal() = 0; 19 | virtual double getReward() = 0; 20 | double norm; 21 | std::shared_ptr graph; 22 | std::vector< std::vector > state_seq; 23 | std::vector act_seq, action_list; 24 | std::vector reward_seq, sum_rewards; 25 | }; 26 | 27 | #endif -------------------------------------------------------------------------------- /code/FINDER_CN/utils.pxd: -------------------------------------------------------------------------------- 1 | 2 | from libcpp.vector cimport vector 3 | from libcpp.set cimport set 4 | from libcpp.memory cimport shared_ptr 5 | from libcpp cimport bool 6 | from graph cimport Graph 7 | 8 | cdef extern from "./src/lib/utils.h": 9 | cdef cppclass Utils: 10 | Utils() 11 | double getRobustness(shared_ptr[Graph] graph,vector[int] solution)except+ 12 | vector[int] reInsert(shared_ptr[Graph] graph,vector[int] solution,vector[int] allVex,int decreaseStrategyID,int reinsertEachStep)except+ 13 | int getMxWccSz(shared_ptr[Graph] graph) 14 | vector[double] Betweenness(shared_ptr[Graph] graph) 15 | vector[double] MaxWccSzList 16 | 17 | -------------------------------------------------------------------------------- /code/FINDER_CN_cost/src/lib/i_env.h: -------------------------------------------------------------------------------- 1 | #ifndef I_ENV_H 2 | #define I_ENV_H 3 | 4 | #include 5 | #include 6 | 7 | #include "graph.h" 8 | 9 | class IEnv 10 | { 11 | public: 12 | 13 | IEnv(double _norm) : norm(_norm), graph(nullptr) {} 14 | 15 | virtual void s0(std::shared_ptr _g) = 0; 16 | virtual double step(int a) = 0; 17 | virtual int randomAction() = 0; 18 | virtual bool isTerminal() = 0; 19 | virtual double getReward() = 0; 20 | double norm; 21 | std::shared_ptr graph; 22 | std::vector< std::vector > state_seq; 23 | std::vector act_seq, action_list; 24 | std::vector reward_seq, sum_rewards; 25 | }; 26 | 27 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND/utils.pxd: -------------------------------------------------------------------------------- 1 | 2 | from libcpp.vector cimport vector 3 | from libcpp.set cimport set 4 | from libcpp.memory cimport shared_ptr 5 | from libcpp cimport bool 6 | from graph cimport Graph 7 | 8 | cdef extern from "./src/lib/utils.h": 9 | cdef cppclass Utils: 10 | Utils() 11 | double getRobustness(shared_ptr[Graph] graph,vector[int] solution)except+ 12 | vector[int] reInsert(shared_ptr[Graph] graph,vector[int] solution,vector[int] allVex,int decreaseStrategyID,int reinsertEachStep)except+ 13 | int getMxWccSz(shared_ptr[Graph] graph) 14 | vector[double] Betweenness(shared_ptr[Graph] graph) 15 | vector[double] MaxWccSzList 16 | 17 | -------------------------------------------------------------------------------- /code/FINDER_ND_cost/src/lib/i_env.h: -------------------------------------------------------------------------------- 1 | #ifndef I_ENV_H 2 | #define I_ENV_H 3 | 4 | #include 5 | #include 6 | 7 | #include "graph.h" 8 | 9 | class IEnv 10 | { 11 | public: 12 | 13 | IEnv(double _norm) : norm(_norm), graph(nullptr) {} 14 | 15 | virtual void s0(std::shared_ptr _g) = 0; 16 | virtual double step(int a) = 0; 17 | virtual int randomAction() = 0; 18 | virtual bool isTerminal() = 0; 19 | virtual double getReward() = 0; 20 | double norm; 21 | std::shared_ptr graph; 22 | std::vector< std::vector > state_seq; 23 | std::vector act_seq, action_list; 24 | std::vector reward_seq, sum_rewards; 25 | }; 26 | 27 | #endif -------------------------------------------------------------------------------- /code/FINDER_CN_cost/utils.pxd: -------------------------------------------------------------------------------- 1 | 2 | from libcpp.vector cimport vector 3 | from libcpp.set cimport set 4 | from libcpp.memory cimport shared_ptr 5 | from libcpp cimport bool 6 | from graph cimport Graph 7 | 8 | cdef extern from "./src/lib/utils.h": 9 | cdef cppclass Utils: 10 | Utils() 11 | double getRobustness(shared_ptr[Graph] graph,vector[int] solution)except+ 12 | vector[int] reInsert(shared_ptr[Graph] graph,vector[int] solution,vector[int] allVex,int decreaseStrategyID,int reinsertEachStep)except+ 13 | int getMxWccSz(shared_ptr[Graph] graph) 14 | vector[double] Betweenness(shared_ptr[Graph] graph) 15 | vector[double] MaxWccSzList 16 | 17 | -------------------------------------------------------------------------------- /code/FINDER_ND_cost/utils.pxd: -------------------------------------------------------------------------------- 1 | 2 | from libcpp.vector cimport vector 3 | from libcpp.set cimport set 4 | from libcpp.memory cimport shared_ptr 5 | from libcpp cimport bool 6 | from graph cimport Graph 7 | 8 | cdef extern from "./src/lib/utils.h": 9 | cdef cppclass Utils: 10 | Utils() 11 | double getRobustness(shared_ptr[Graph] graph,vector[int] solution)except+ 12 | vector[int] reInsert(shared_ptr[Graph] graph,vector[int] solution,vector[int] allVex,int decreaseStrategyID,int reinsertEachStep)except+ 13 | int getMxWccSz(shared_ptr[Graph] graph) 14 | vector[double] Betweenness(shared_ptr[Graph] graph) 15 | vector[double] MaxWccSzList 16 | 17 | -------------------------------------------------------------------------------- /environment/Dockerfile: -------------------------------------------------------------------------------- 1 | # hash:sha256:cdeb275e7f16e6ee9324818a5174fa0c4bc4016c7e465bc9911d4ad258dca810 2 | FROM registry.codeocean.com/codeocean/miniconda3:4.7.10-cuda10.1-cudnn7-ubuntu18.04 3 | 4 | ARG DEBIAN_FRONTEND=noninteractive 5 | 6 | RUN apt-get update \ 7 | && apt-get install -y --no-install-recommends \ 8 | gcc=4:7.4.0-1ubuntu2.3 \ 9 | python-pip=9.0.1-2.3~ubuntu1.18.04.1 \ 10 | python-setuptools=39.0.1-2 \ 11 | python-wheel=0.30.0-0.2 \ 12 | && rm -rf /var/lib/apt/lists/* 13 | 14 | RUN pip install -U --no-cache-dir \ 15 | cython==0.29.13 \ 16 | networkx==2.3 \ 17 | numpy==1.17.3 \ 18 | pandas==0.25.2 \ 19 | scipy==1.3.1 \ 20 | tensorflow-gpu==1.14.0 \ 21 | tqdm==4.36.1 22 | -------------------------------------------------------------------------------- /code/FINDER_CN/src/lib/graph_utils.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPH_UTILS_H 2 | #define GRAPH_UTILS_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "disjoint_set.h" 10 | 11 | 12 | class GraphUtil 13 | { 14 | public: 15 | GraphUtil(); 16 | 17 | ~GraphUtil(); 18 | 19 | void deleteNode(std::vector > &adjListGraph, int node); 20 | 21 | void recoverAddNode(const std::vector > &backupCompletedAdjListGraph, std::vector& backupAllVex, std::vector > &adjListGraph, int node, Disjoint_Set &unionSet); 22 | 23 | void addEdge(std::vector > &adjListGraph, int node0, int node1); 24 | 25 | 26 | }; 27 | 28 | 29 | 30 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND/src/lib/graph_utils.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPH_UTILS_H 2 | #define GRAPH_UTILS_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "disjoint_set.h" 10 | 11 | 12 | class GraphUtil 13 | { 14 | public: 15 | GraphUtil(); 16 | 17 | ~GraphUtil(); 18 | 19 | void deleteNode(std::vector > &adjListGraph, int node); 20 | 21 | void recoverAddNode(const std::vector > &backupCompletedAdjListGraph, std::vector& backupAllVex, std::vector > &adjListGraph, int node, Disjoint_Set &unionSet); 22 | 23 | void addEdge(std::vector > &adjListGraph, int node0, int node1); 24 | 25 | 26 | }; 27 | 28 | 29 | 30 | #endif -------------------------------------------------------------------------------- /code/FINDER_CN_cost/src/lib/graph_utils.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPH_UTILS_H 2 | #define GRAPH_UTILS_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "disjoint_set.h" 10 | 11 | 12 | class GraphUtil 13 | { 14 | public: 15 | GraphUtil(); 16 | 17 | ~GraphUtil(); 18 | 19 | void deleteNode(std::vector > &adjListGraph, int node); 20 | 21 | void recoverAddNode(const std::vector > &backupCompletedAdjListGraph, std::vector& backupAllVex, std::vector > &adjListGraph, int node, Disjoint_Set &unionSet); 22 | 23 | void addEdge(std::vector > &adjListGraph, int node0, int node1); 24 | 25 | 26 | }; 27 | 28 | 29 | 30 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND_cost/src/lib/graph_utils.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPH_UTILS_H 2 | #define GRAPH_UTILS_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "disjoint_set.h" 10 | 11 | 12 | class GraphUtil 13 | { 14 | public: 15 | GraphUtil(); 16 | 17 | ~GraphUtil(); 18 | 19 | void deleteNode(std::vector > &adjListGraph, int node); 20 | 21 | void recoverAddNode(const std::vector > &backupCompletedAdjListGraph, std::vector& backupAllVex, std::vector > &adjListGraph, int node, Disjoint_Set &unionSet); 22 | 23 | void addEdge(std::vector > &adjListGraph, int node0, int node1); 24 | 25 | 26 | }; 27 | 28 | 29 | 30 | #endif -------------------------------------------------------------------------------- /code/FINDER_CN/graph_struct.pxd: -------------------------------------------------------------------------------- 1 | from libcpp.vector cimport vector 2 | from libcpp.map cimport map 3 | from libcpp.pair cimport pair 4 | cdef extern from "./src/lib/graph_struct.h": 5 | cdef cppclass LinkedTable[T]: 6 | LinkedTable()except+ 7 | void AddEntry(int head_id, T content)except+ 8 | void Resize(int new_n)except+ 9 | int n 10 | vector[vector[T]] head 11 | cdef cppclass GraphStruct: 12 | GraphStruct()except+ 13 | void AddEdge(int idx, int x, int y)except+ 14 | void AddNode(int subg_id, int n_idx)except+ 15 | void Resize(unsigned _num_subgraph, unsigned _num_nodes)except+ 16 | LinkedTable[pair[int, int]] *out_edges 17 | LinkedTable[pair[int, int]] *in_edges 18 | LinkedTable[int]* subgraph 19 | vector[pair[int, int]] edge_list 20 | unsigned num_nodes 21 | unsigned num_edges 22 | unsigned num_subgraph 23 | -------------------------------------------------------------------------------- /code/FINDER_ND/graph_struct.pxd: -------------------------------------------------------------------------------- 1 | from libcpp.vector cimport vector 2 | from libcpp.map cimport map 3 | from libcpp.pair cimport pair 4 | cdef extern from "./src/lib/graph_struct.h": 5 | cdef cppclass LinkedTable[T]: 6 | LinkedTable()except+ 7 | void AddEntry(int head_id, T content)except+ 8 | void Resize(int new_n)except+ 9 | int n 10 | vector[vector[T]] head 11 | cdef cppclass GraphStruct: 12 | GraphStruct()except+ 13 | void AddEdge(int idx, int x, int y)except+ 14 | void AddNode(int subg_id, int n_idx)except+ 15 | void Resize(unsigned _num_subgraph, unsigned _num_nodes)except+ 16 | LinkedTable[pair[int, int]] *out_edges 17 | LinkedTable[pair[int, int]] *in_edges 18 | LinkedTable[int]* subgraph 19 | vector[pair[int, int]] edge_list 20 | unsigned num_nodes 21 | unsigned num_edges 22 | unsigned num_subgraph 23 | -------------------------------------------------------------------------------- /code/FINDER_CN_cost/graph_struct.pxd: -------------------------------------------------------------------------------- 1 | from libcpp.vector cimport vector 2 | from libcpp.map cimport map 3 | from libcpp.pair cimport pair 4 | cdef extern from "./src/lib/graph_struct.h": 5 | cdef cppclass LinkedTable[T]: 6 | LinkedTable()except+ 7 | void AddEntry(int head_id, T content)except+ 8 | void Resize(int new_n)except+ 9 | int n 10 | vector[vector[T]] head 11 | cdef cppclass GraphStruct: 12 | GraphStruct()except+ 13 | void AddEdge(int idx, int x, int y)except+ 14 | void AddNode(int subg_id, int n_idx)except+ 15 | void Resize(unsigned _num_subgraph, unsigned _num_nodes)except+ 16 | LinkedTable[pair[int, int]] *out_edges 17 | LinkedTable[pair[int, int]] *in_edges 18 | LinkedTable[int]* subgraph 19 | vector[pair[int, int]] edge_list 20 | unsigned num_nodes 21 | unsigned num_edges 22 | unsigned num_subgraph 23 | -------------------------------------------------------------------------------- /code/FINDER_ND_cost/graph_struct.pxd: -------------------------------------------------------------------------------- 1 | from libcpp.vector cimport vector 2 | from libcpp.map cimport map 3 | from libcpp.pair cimport pair 4 | cdef extern from "./src/lib/graph_struct.h": 5 | cdef cppclass LinkedTable[T]: 6 | LinkedTable()except+ 7 | void AddEntry(int head_id, T content)except+ 8 | void Resize(int new_n)except+ 9 | int n 10 | vector[vector[T]] head 11 | cdef cppclass GraphStruct: 12 | GraphStruct()except+ 13 | void AddEdge(int idx, int x, int y)except+ 14 | void AddNode(int subg_id, int n_idx)except+ 15 | void Resize(unsigned _num_subgraph, unsigned _num_nodes)except+ 16 | LinkedTable[pair[int, int]] *out_edges 17 | LinkedTable[pair[int, int]] *in_edges 18 | LinkedTable[int]* subgraph 19 | vector[pair[int, int]] edge_list 20 | unsigned num_nodes 21 | unsigned num_edges 22 | unsigned num_subgraph 23 | -------------------------------------------------------------------------------- /code/FINDER_CN/src/lib/graph.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPH_H 2 | #define GRAPH_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | class Graph 10 | { 11 | public: 12 | Graph(); 13 | Graph(const int _num_nodes, const int _num_edges, const int* edges_from, const int* edges_to); 14 | ~Graph(); 15 | int num_nodes; 16 | int num_edges; 17 | std::vector< std::vector< int > > adj_list; 18 | std::vector< std::pair > edge_list; 19 | double getTwoRankNeighborsRatio(std::vector covered); 20 | 21 | }; 22 | 23 | class GSet 24 | { 25 | public: 26 | GSet(); 27 | ~GSet(); 28 | void InsertGraph(int gid, std::shared_ptr graph); 29 | std::shared_ptr Sample(); 30 | std::shared_ptr Get(int gid); 31 | void Clear(); 32 | std::map > graph_pool; 33 | }; 34 | 35 | extern GSet GSetTrain; 36 | extern GSet GSetTest; 37 | 38 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND/src/lib/graph.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPH_H 2 | #define GRAPH_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | class Graph 10 | { 11 | public: 12 | Graph(); 13 | Graph(const int _num_nodes, const int _num_edges, const int* edges_from, const int* edges_to); 14 | ~Graph(); 15 | int num_nodes; 16 | int num_edges; 17 | std::vector< std::vector< int > > adj_list; 18 | std::vector< std::pair > edge_list; 19 | double getTwoRankNeighborsRatio(std::vector covered); 20 | 21 | }; 22 | 23 | class GSet 24 | { 25 | public: 26 | GSet(); 27 | ~GSet(); 28 | void InsertGraph(int gid, std::shared_ptr graph); 29 | std::shared_ptr Sample(); 30 | std::shared_ptr Get(int gid); 31 | void Clear(); 32 | std::map > graph_pool; 33 | }; 34 | 35 | extern GSet GSetTrain; 36 | extern GSet GSetTest; 37 | 38 | #endif -------------------------------------------------------------------------------- /code/FINDER_CN/graph.pxd: -------------------------------------------------------------------------------- 1 | ''' 2 | file:graph.pxd 类graph的定义文件对应graph.h 3 | ''' 4 | #Cython已经编译了C++的std模板库,位置在~/Cython/Includes/lincpp/ 5 | from libcpp.vector cimport vector 6 | from libcpp.memory cimport shared_ptr 7 | from libcpp.map cimport map 8 | from libcpp.pair cimport pair 9 | cdef extern from "./src/lib/graph.h": 10 | cdef cppclass Graph: 11 | Graph()except+ 12 | Graph(const int _num_nodes, const int _num_edges, const int* edges_from, const int* edges_to) except+ 13 | int num_nodes 14 | int num_edges 15 | vector[vector[int]] adj_list 16 | vector[pair[int,int]] edge_list 17 | 18 | cdef extern from "./src/lib/graph.h": 19 | cdef cppclass GSet: 20 | GSet()except+ 21 | void InsertGraph(int gid, shared_ptr[Graph] graph)except+ 22 | shared_ptr[Graph] Sample()except+ 23 | shared_ptr[Graph] Get(int gid)except+ 24 | void Clear()except+ 25 | map[int, shared_ptr[Graph]] graph_pool 26 | 27 | -------------------------------------------------------------------------------- /code/FINDER_ND/graph.pxd: -------------------------------------------------------------------------------- 1 | ''' 2 | file:graph.pxd 类graph的定义文件对应graph.h 3 | ''' 4 | #Cython已经编译了C++的std模板库,位置在~/Cython/Includes/lincpp/ 5 | from libcpp.vector cimport vector 6 | from libcpp.memory cimport shared_ptr 7 | from libcpp.map cimport map 8 | from libcpp.pair cimport pair 9 | cdef extern from "./src/lib/graph.h": 10 | cdef cppclass Graph: 11 | Graph()except+ 12 | Graph(const int _num_nodes, const int _num_edges, const int* edges_from, const int* edges_to) except+ 13 | int num_nodes 14 | int num_edges 15 | vector[vector[int]] adj_list 16 | vector[pair[int,int]] edge_list 17 | 18 | cdef extern from "./src/lib/graph.h": 19 | cdef cppclass GSet: 20 | GSet()except+ 21 | void InsertGraph(int gid, shared_ptr[Graph] graph)except+ 22 | shared_ptr[Graph] Sample()except+ 23 | shared_ptr[Graph] Get(int gid)except+ 24 | void Clear()except+ 25 | map[int, shared_ptr[Graph]] graph_pool 26 | 27 | -------------------------------------------------------------------------------- /code/FINDER_CN_cost/src/lib/graph.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPH_H 2 | #define GRAPH_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | class Graph 10 | { 11 | public: 12 | Graph(); 13 | Graph(const int _num_nodes, const int _num_edges, const int* edges_from, const int* edges_to,const double* _nodes_weight); 14 | int num_nodes; 15 | int num_edges; 16 | std::vector< std::vector< int > > adj_list; 17 | std::vector< std::pair > edge_list; 18 | std::vector nodes_weight; 19 | double total_nodes_weight; 20 | double getTwoRankNeighborsRatio(std::vector covered); 21 | 22 | }; 23 | 24 | class GSet 25 | { 26 | public: 27 | GSet(); 28 | void InsertGraph(int gid, std::shared_ptr graph); 29 | std::shared_ptr Sample(); 30 | std::shared_ptr Get(int gid); 31 | void Clear(); 32 | std::map > graph_pool; 33 | }; 34 | 35 | extern GSet GSetTrain; 36 | extern GSet GSetTest; 37 | 38 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND_cost/src/lib/graph.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPH_H 2 | #define GRAPH_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | class Graph 10 | { 11 | public: 12 | Graph(); 13 | Graph(const int _num_nodes, const int _num_edges, const int* edges_from, const int* edges_to,const double* _nodes_weight); 14 | int num_nodes; 15 | int num_edges; 16 | std::vector< std::vector< int > > adj_list; 17 | std::vector< std::pair > edge_list; 18 | std::vector nodes_weight; 19 | double total_nodes_weight; 20 | double getTwoRankNeighborsRatio(std::vector covered); 21 | 22 | }; 23 | 24 | class GSet 25 | { 26 | public: 27 | GSet(); 28 | void InsertGraph(int gid, std::shared_ptr graph); 29 | std::shared_ptr Sample(); 30 | std::shared_ptr Get(int gid); 31 | void Clear(); 32 | std::map > graph_pool; 33 | }; 34 | 35 | extern GSet GSetTrain; 36 | extern GSet GSetTest; 37 | 38 | #endif -------------------------------------------------------------------------------- /code/FINDER_CN_cost/src/lib/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "graph.h" 8 | #include "disjoint_set.h" 9 | #include "graph_utils.h" 10 | #include "decrease_strategy.cpp" 11 | 12 | class Utils 13 | { 14 | public: 15 | Utils(); 16 | 17 | double getRobustness(std::shared_ptr graph,std::vector); 18 | 19 | std::vector reInsert(std::shared_ptr graph,std::vector solution,const std::vector allVex,int decreaseStrategyID,int reinsertEachStep); 20 | 21 | std::vector reInsert_inner(const std::vector &beforeOutput, std::shared_ptr &graph, const std::vector &allVex, std::shared_ptr &decreaseStrategy,int reinsertEachStep); 22 | 23 | int getMxWccSz(std::shared_ptr graph); 24 | 25 | std::vector Betweenness(std::shared_ptr _g); 26 | 27 | std::vector Betweenness(std::vector< std::vector > adj_list); 28 | 29 | std::vector MaxWccSzList; 30 | 31 | 32 | }; 33 | 34 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND_cost/src/lib/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "graph.h" 8 | #include "disjoint_set.h" 9 | #include "graph_utils.h" 10 | #include "decrease_strategy.cpp" 11 | 12 | class Utils 13 | { 14 | public: 15 | Utils(); 16 | 17 | double getRobustness(std::shared_ptr graph,std::vector); 18 | 19 | std::vector reInsert(std::shared_ptr graph,std::vector solution,const std::vector allVex,int decreaseStrategyID,int reinsertEachStep); 20 | 21 | std::vector reInsert_inner(const std::vector &beforeOutput, std::shared_ptr &graph, const std::vector &allVex, std::shared_ptr &decreaseStrategy,int reinsertEachStep); 22 | 23 | int getMxWccSz(std::shared_ptr graph); 24 | 25 | std::vector Betweenness(std::shared_ptr _g); 26 | 27 | std::vector Betweenness(std::vector< std::vector > adj_list); 28 | 29 | std::vector MaxWccSzList; 30 | 31 | 32 | }; 33 | 34 | #endif -------------------------------------------------------------------------------- /code/FINDER_CN/src/lib/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "graph.h" 8 | #include "disjoint_set.h" 9 | #include "graph_utils.h" 10 | #include "decrease_strategy.cpp" 11 | 12 | class Utils 13 | { 14 | public: 15 | Utils(); 16 | 17 | double getRobustness(std::shared_ptr graph, std::vector solution); 18 | 19 | std::vector reInsert(std::shared_ptr graph,std::vector solution,const std::vector allVex,int decreaseStrategyID,int reinsertEachStep); 20 | 21 | std::vector reInsert_inner(const std::vector &beforeOutput, std::shared_ptr &graph, const std::vector &allVex, std::shared_ptr &decreaseStrategy,int reinsertEachStep); 22 | 23 | int getMxWccSz(std::shared_ptr graph); 24 | 25 | std::vector Betweenness(std::shared_ptr _g); 26 | 27 | std::vector Betweenness(std::vector< std::vector > adj_list); 28 | 29 | std::vector MaxWccSzList; 30 | 31 | 32 | }; 33 | 34 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND/src/lib/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "graph.h" 8 | #include "disjoint_set.h" 9 | #include "graph_utils.h" 10 | #include "decrease_strategy.cpp" 11 | 12 | class Utils 13 | { 14 | public: 15 | Utils(); 16 | 17 | double getRobustness(std::shared_ptr graph, std::vector solution); 18 | 19 | std::vector reInsert(std::shared_ptr graph,std::vector solution,const std::vector allVex,int decreaseStrategyID,int reinsertEachStep); 20 | 21 | std::vector reInsert_inner(const std::vector &beforeOutput, std::shared_ptr &graph, const std::vector &allVex, std::shared_ptr &decreaseStrategy,int reinsertEachStep); 22 | 23 | int getMxWccSz(std::shared_ptr graph); 24 | 25 | std::vector Betweenness(std::shared_ptr _g); 26 | 27 | std::vector Betweenness(std::vector< std::vector > adj_list); 28 | 29 | std::vector MaxWccSzList; 30 | 31 | 32 | }; 33 | 34 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND/mvc_env.pxd: -------------------------------------------------------------------------------- 1 | 2 | from libcpp.vector cimport vector 3 | from libcpp.set cimport set 4 | from libcpp.memory cimport shared_ptr 5 | from libcpp cimport bool 6 | from graph cimport Graph 7 | 8 | cdef extern from "./src/lib/mvc_env.h": 9 | cdef cppclass MvcEnv: 10 | MvcEnv(double _norm) 11 | void s0(shared_ptr[Graph] _g)except+ 12 | double step(int a)except+ 13 | void stepWithoutReward(int a)except+ 14 | int randomAction()except+ 15 | int betweenAction()except+ 16 | bool isTerminal()except+ 17 | # double getReward(double oldCcNum)except+ 18 | double getReward()except+ 19 | double getMaxConnectedNodesNum()except+ 20 | double norm 21 | double CcNum 22 | shared_ptr[Graph] graph 23 | vector[vector[int]] state_seq 24 | vector[int] act_seq 25 | vector[int] action_list 26 | vector[double] reward_seq 27 | vector[double] sum_rewards 28 | int numCoveredEdges 29 | set[int] covered_set 30 | vector[int] avail_list 31 | -------------------------------------------------------------------------------- /code/FINDER_CN_cost/mvc_env.pxd: -------------------------------------------------------------------------------- 1 | 2 | from libcpp.vector cimport vector 3 | from libcpp.set cimport set 4 | from libcpp.memory cimport shared_ptr 5 | from libcpp cimport bool 6 | from graph cimport Graph 7 | 8 | cdef extern from "./src/lib/mvc_env.h": 9 | cdef cppclass MvcEnv: 10 | MvcEnv(double _norm) 11 | void s0(shared_ptr[Graph] _g)except+ 12 | double step(int a)except+ 13 | void stepWithoutReward(int a)except+ 14 | int randomAction()except+ 15 | int betweenAction()except+ 16 | bool isTerminal()except+ 17 | # double getReward(double oldCcNum)except+ 18 | double getReward(int a)except+ 19 | double getMaxConnectedNodesNum()except+ 20 | double norm 21 | double CcNum 22 | shared_ptr[Graph] graph 23 | vector[vector[int]] state_seq 24 | vector[int] act_seq 25 | vector[int] action_list 26 | vector[double] reward_seq 27 | vector[double] sum_rewards 28 | int numCoveredEdges 29 | set[int] covered_set 30 | vector[int] avail_list 31 | -------------------------------------------------------------------------------- /code/FINDER_ND_cost/mvc_env.pxd: -------------------------------------------------------------------------------- 1 | 2 | from libcpp.vector cimport vector 3 | from libcpp.set cimport set 4 | from libcpp.memory cimport shared_ptr 5 | from libcpp cimport bool 6 | from graph cimport Graph 7 | 8 | cdef extern from "./src/lib/mvc_env.h": 9 | cdef cppclass MvcEnv: 10 | MvcEnv(double _norm) 11 | void s0(shared_ptr[Graph] _g)except+ 12 | double step(int a)except+ 13 | void stepWithoutReward(int a)except+ 14 | int randomAction()except+ 15 | int betweenAction()except+ 16 | bool isTerminal()except+ 17 | # double getReward(double oldCcNum)except+ 18 | double getReward(int a)except+ 19 | double getMaxConnectedNodesNum()except+ 20 | double norm 21 | double CcNum 22 | shared_ptr[Graph] graph 23 | vector[vector[int]] state_seq 24 | vector[int] act_seq 25 | vector[int] action_list 26 | vector[double] reward_seq 27 | vector[double] sum_rewards 28 | int numCoveredEdges 29 | set[int] covered_set 30 | vector[int] avail_list 31 | -------------------------------------------------------------------------------- /code/FINDER_CN_cost/graph.pxd: -------------------------------------------------------------------------------- 1 | ''' 2 | file:graph.pxd 类graph的定义文件对应graph.h 3 | ''' 4 | #Cython已经编译了C++的std模板库,位置在~/Cython/Includes/lincpp/ 5 | from libcpp.vector cimport vector 6 | from libcpp.memory cimport shared_ptr 7 | from libcpp.map cimport map 8 | from libcpp.pair cimport pair 9 | cdef extern from "./src/lib/graph.h": 10 | cdef cppclass Graph: 11 | Graph()except+ 12 | Graph(const int _num_nodes, const int _num_edges, const int* edges_from, const int* edges_to,const double* _nodes_weight) except+ 13 | int num_nodes 14 | int num_edges 15 | vector[vector[int]] adj_list 16 | vector[pair[int,int]] edge_list 17 | vector[double] nodes_weight 18 | double total_nodes_weight 19 | 20 | cdef extern from "./src/lib/graph.h": 21 | cdef cppclass GSet: 22 | GSet()except+ 23 | void InsertGraph(int gid, shared_ptr[Graph] graph)except+ 24 | shared_ptr[Graph] Sample()except+ 25 | shared_ptr[Graph] Get(int gid)except+ 26 | void Clear()except+ 27 | map[int, shared_ptr[Graph]] graph_pool 28 | 29 | -------------------------------------------------------------------------------- /code/FINDER_ND_cost/graph.pxd: -------------------------------------------------------------------------------- 1 | ''' 2 | file:graph.pxd 类graph的定义文件对应graph.h 3 | ''' 4 | #Cython已经编译了C++的std模板库,位置在~/Cython/Includes/lincpp/ 5 | from libcpp.vector cimport vector 6 | from libcpp.memory cimport shared_ptr 7 | from libcpp.map cimport map 8 | from libcpp.pair cimport pair 9 | cdef extern from "./src/lib/graph.h": 10 | cdef cppclass Graph: 11 | Graph()except+ 12 | Graph(const int _num_nodes, const int _num_edges, const int* edges_from, const int* edges_to,const double* _nodes_weight) except+ 13 | int num_nodes 14 | int num_edges 15 | vector[vector[int]] adj_list 16 | vector[pair[int,int]] edge_list 17 | vector[double] nodes_weight 18 | double total_nodes_weight 19 | 20 | cdef extern from "./src/lib/graph.h": 21 | cdef cppclass GSet: 22 | GSet()except+ 23 | void InsertGraph(int gid, shared_ptr[Graph] graph)except+ 24 | shared_ptr[Graph] Sample()except+ 25 | shared_ptr[Graph] Get(int gid)except+ 26 | void Clear()except+ 27 | map[int, shared_ptr[Graph]] graph_pool 28 | 29 | -------------------------------------------------------------------------------- /code/FINDER_CN_cost/real2weight.py: -------------------------------------------------------------------------------- 1 | import networkx as nx 2 | import numpy as np 3 | import random 4 | 5 | 6 | 7 | data_path = './Data/InternetP2P/p2p-Gnutella' 8 | data_name = ['04','05','06','08','09','24','25','30','31'] 9 | save_dir = './Data/InternetP2P_cost/' 10 | costType = {0: 'degree', 1:'random'} 11 | 12 | for k in range(2): 13 | for i in range(len(data_name)): 14 | data = data_path + data_name[i] + '.txt' 15 | g = nx.read_weighted_edgelist(data) 16 | 17 | if k == 0: ### degree weight 18 | degree = nx.degree(g) 19 | maxDegree = max(degree.values()) 20 | weights = {} 21 | for node in g.nodes(): 22 | weights[node] = degree[node] / maxDegree 23 | 24 | else: ### random weight 25 | weights = {} 26 | for node in g.nodes(): 27 | weights[node] = random.uniform(0, 1) 28 | 29 | nx.set_node_attributes(g, 'weight', weights) 30 | save_dir_g = '%s/p2p-Gnutella%s_%s.gml' % (save_dir, data_name[i], costType[k]) 31 | nx.write_gml(g, save_dir_g) 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Frank 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /code/FINDER_CN/mvc_env.pxd: -------------------------------------------------------------------------------- 1 | 2 | from libcpp.vector cimport vector 3 | from libcpp.set cimport set 4 | from libcpp.memory cimport shared_ptr 5 | from libcpp cimport bool 6 | from graph cimport Graph 7 | 8 | cdef extern from "./src/lib/mvc_env.h": 9 | cdef cppclass MvcEnv: 10 | MvcEnv(double _norm) 11 | void s0(shared_ptr[Graph] _g)except+ 12 | double step(int a)except+ 13 | void stepWithoutReward(int a)except+ 14 | int randomAction()except+ 15 | int betweenAction()except+ 16 | bool isTerminal()except+ 17 | # double getReward(double oldCcNum)except+ 18 | double getReward()except+ 19 | double getMaxConnectedNodesNum()except+ 20 | double getRemainingCNDScore()except+ 21 | double norm 22 | double CcNum 23 | shared_ptr[Graph] graph 24 | vector[vector[int]] state_seq 25 | vector[int] act_seq 26 | vector[int] action_list 27 | vector[double] reward_seq 28 | vector[double] sum_rewards 29 | int numCoveredEdges 30 | set[int] covered_set 31 | vector[int] avail_list 32 | -------------------------------------------------------------------------------- /code/FINDER_CN/PrepareBatchGraph.pxd: -------------------------------------------------------------------------------- 1 | from libcpp.vector cimport vector 2 | from libcpp.memory cimport shared_ptr 3 | from libcpp.pair cimport pair 4 | from graph cimport Graph 5 | cdef extern from "./src/lib/PrepareBatchGraph.h": 6 | cdef cppclass sparseMatrix: 7 | sparseMatrix()except+ 8 | vector[int] rowIndex 9 | vector[int] colIndex 10 | vector[double] value 11 | int rowNum 12 | int colNum 13 | 14 | cdef cppclass PrepareBatchGraph: 15 | PrepareBatchGraph(int aggregatorID)except+ 16 | void SetupTrain(vector[int] idxes,vector[shared_ptr[Graph] ] g_list,vector[vector[int]] covered,const int* actions)except+ 17 | void SetupPredAll(vector[int] idxes,vector[shared_ptr[Graph] ] g_list,vector[vector[int]] covered)except+ 18 | shared_ptr[sparseMatrix] act_select 19 | shared_ptr[sparseMatrix] rep_global 20 | shared_ptr[sparseMatrix] n2nsum_param 21 | shared_ptr[sparseMatrix] laplacian_param 22 | shared_ptr[sparseMatrix] subgsum_param 23 | vector[vector[int]] idx_map_list 24 | vector[pair[int,int]] subgraph_id_span 25 | vector[vector[double]] aux_feat 26 | vector[int] avail_act_cnt 27 | int aggregatorID -------------------------------------------------------------------------------- /code/FINDER_CN_cost/src/lib/mvc_env.h: -------------------------------------------------------------------------------- 1 | #ifndef MVC_ENV_H 2 | #define MVC_ENV_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "graph.h" 8 | #include "disjoint_set.h" 9 | 10 | class MvcEnv 11 | { 12 | public: 13 | MvcEnv(double _norm); 14 | 15 | ~MvcEnv(); 16 | 17 | void s0(std::shared_ptr _g); 18 | 19 | double step(int a); 20 | 21 | void stepWithoutReward(int a); 22 | 23 | std::vector Betweenness(std::vector< std::vector > adj_list); 24 | 25 | int randomAction(); 26 | 27 | int betweenAction(); 28 | 29 | bool isTerminal(); 30 | 31 | double getReward(int a); 32 | 33 | double getMaxConnectedNodesNum(); 34 | 35 | double getNumofConnectedComponents(); 36 | 37 | double CcNum; 38 | 39 | void printGraph(); 40 | 41 | double norm; 42 | 43 | std::shared_ptr graph; 44 | 45 | std::vector< std::vector > state_seq; 46 | 47 | std::vector act_seq, action_list; 48 | 49 | std::vector reward_seq, sum_rewards; 50 | 51 | int numCoveredEdges; 52 | 53 | std::set covered_set; 54 | 55 | std::vector avail_list; 56 | 57 | std::vector node_degrees; 58 | 59 | int total_degrees; 60 | }; 61 | 62 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND/PrepareBatchGraph.pxd: -------------------------------------------------------------------------------- 1 | from libcpp.vector cimport vector 2 | from libcpp.memory cimport shared_ptr 3 | from libcpp.pair cimport pair 4 | from graph cimport Graph 5 | cdef extern from "./src/lib/PrepareBatchGraph.h": 6 | cdef cppclass sparseMatrix: 7 | sparseMatrix()except+ 8 | vector[int] rowIndex 9 | vector[int] colIndex 10 | vector[double] value 11 | int rowNum 12 | int colNum 13 | 14 | cdef cppclass PrepareBatchGraph: 15 | PrepareBatchGraph(int aggregatorID)except+ 16 | void SetupTrain(vector[int] idxes,vector[shared_ptr[Graph] ] g_list,vector[vector[int]] covered,const int* actions)except+ 17 | void SetupPredAll(vector[int] idxes,vector[shared_ptr[Graph] ] g_list,vector[vector[int]] covered)except+ 18 | shared_ptr[sparseMatrix] act_select 19 | shared_ptr[sparseMatrix] rep_global 20 | shared_ptr[sparseMatrix] n2nsum_param 21 | shared_ptr[sparseMatrix] laplacian_param 22 | shared_ptr[sparseMatrix] subgsum_param 23 | vector[vector[int]] idx_map_list 24 | vector[pair[int,int]] subgraph_id_span 25 | vector[vector[double]] aux_feat 26 | vector[int] avail_act_cnt 27 | int aggregatorID -------------------------------------------------------------------------------- /code/FINDER_ND_cost/src/lib/mvc_env.h: -------------------------------------------------------------------------------- 1 | #ifndef MVC_ENV_H 2 | #define MVC_ENV_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "graph.h" 8 | #include "disjoint_set.h" 9 | 10 | class MvcEnv 11 | { 12 | public: 13 | MvcEnv(double _norm); 14 | 15 | ~MvcEnv(); 16 | 17 | void s0(std::shared_ptr _g); 18 | 19 | double step(int a); 20 | 21 | void stepWithoutReward(int a); 22 | 23 | std::vector Betweenness(std::vector< std::vector > adj_list); 24 | 25 | int randomAction(); 26 | 27 | int betweenAction(); 28 | 29 | bool isTerminal(); 30 | 31 | double getReward(int a); 32 | 33 | double getMaxConnectedNodesNum(); 34 | 35 | double getNumofConnectedComponents(); 36 | 37 | double CcNum; 38 | 39 | void printGraph(); 40 | 41 | double norm; 42 | 43 | std::shared_ptr graph; 44 | 45 | std::vector< std::vector > state_seq; 46 | 47 | std::vector act_seq, action_list; 48 | 49 | std::vector reward_seq, sum_rewards; 50 | 51 | int numCoveredEdges; 52 | 53 | std::set covered_set; 54 | 55 | std::vector avail_list; 56 | 57 | std::vector node_degrees; 58 | 59 | int total_degrees; 60 | }; 61 | 62 | #endif -------------------------------------------------------------------------------- /code/FINDER_CN/src/lib/mvc_env.h: -------------------------------------------------------------------------------- 1 | #ifndef MVC_ENV_H 2 | #define MVC_ENV_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "graph.h" 8 | #include "disjoint_set.h" 9 | 10 | class MvcEnv 11 | { 12 | public: 13 | MvcEnv(double _norm); 14 | 15 | ~MvcEnv(); 16 | 17 | void s0(std::shared_ptr _g); 18 | 19 | double step(int a); 20 | 21 | void stepWithoutReward(int a); 22 | 23 | std::vector Betweenness(std::vector< std::vector > adj_list); 24 | 25 | int randomAction(); 26 | 27 | int betweenAction(); 28 | 29 | bool isTerminal(); 30 | 31 | // double getReward(double oldCcNum); 32 | double getReward(); 33 | 34 | double getMaxConnectedNodesNum(); 35 | 36 | double getRemainingCNDScore(); 37 | 38 | double CcNum; 39 | 40 | void printGraph(); 41 | 42 | double norm; 43 | 44 | std::shared_ptr graph; 45 | 46 | std::vector< std::vector > state_seq; 47 | 48 | std::vector act_seq, action_list; 49 | 50 | std::vector reward_seq, sum_rewards; 51 | 52 | int numCoveredEdges; 53 | 54 | std::set covered_set; 55 | 56 | std::vector avail_list; 57 | 58 | std::vector node_degrees; 59 | 60 | int total_degrees; 61 | }; 62 | 63 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND/src/lib/mvc_env.h: -------------------------------------------------------------------------------- 1 | #ifndef MVC_ENV_H 2 | #define MVC_ENV_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "graph.h" 8 | #include "disjoint_set.h" 9 | 10 | class MvcEnv 11 | { 12 | public: 13 | MvcEnv(double _norm); 14 | 15 | ~MvcEnv(); 16 | 17 | void s0(std::shared_ptr _g); 18 | 19 | double step(int a); 20 | 21 | void stepWithoutReward(int a); 22 | 23 | std::vector Betweenness(std::vector< std::vector > adj_list); 24 | 25 | int randomAction(); 26 | 27 | int betweenAction(); 28 | 29 | bool isTerminal(); 30 | 31 | // double getReward(double oldCcNum); 32 | double getReward(); 33 | 34 | double getMaxConnectedNodesNum(); 35 | 36 | double getNumofConnectedComponents(); 37 | 38 | double CcNum; 39 | 40 | void printGraph(); 41 | 42 | double norm; 43 | 44 | std::shared_ptr graph; 45 | 46 | std::vector< std::vector > state_seq; 47 | 48 | std::vector act_seq, action_list; 49 | 50 | std::vector reward_seq, sum_rewards; 51 | 52 | int numCoveredEdges; 53 | 54 | std::set covered_set; 55 | 56 | std::vector avail_list; 57 | 58 | std::vector node_degrees; 59 | 60 | int total_degrees; 61 | }; 62 | 63 | #endif -------------------------------------------------------------------------------- /code/FINDER_CN_cost/PrepareBatchGraph.pxd: -------------------------------------------------------------------------------- 1 | from libcpp.vector cimport vector 2 | from libcpp.memory cimport shared_ptr 3 | from libcpp.pair cimport pair 4 | from graph cimport Graph 5 | cdef extern from "./src/lib/PrepareBatchGraph.h": 6 | cdef cppclass sparseMatrix: 7 | sparseMatrix()except+ 8 | vector[int] rowIndex 9 | vector[int] colIndex 10 | vector[double] value 11 | int rowNum 12 | int colNum 13 | 14 | cdef cppclass PrepareBatchGraph: 15 | PrepareBatchGraph(int aggregatorID)except+ 16 | void SetupTrain(vector[int] idxes,vector[shared_ptr[Graph] ] g_list,vector[vector[int]] covered,const int* actions)except+ 17 | void SetupPredAll(vector[int] idxes,vector[shared_ptr[Graph] ] g_list,vector[vector[int]] covered)except+ 18 | shared_ptr[sparseMatrix] act_select 19 | shared_ptr[sparseMatrix] rep_global 20 | shared_ptr[sparseMatrix] n2nsum_param 21 | shared_ptr[sparseMatrix] laplacian_param 22 | shared_ptr[sparseMatrix] subgsum_param 23 | vector[vector[int]] idx_map_list 24 | vector[pair[int,int]] subgraph_id_span 25 | vector[vector[double]] aux_feat 26 | vector[vector[double]] node_feat 27 | vector[int] avail_act_cnt 28 | int aggregatorID -------------------------------------------------------------------------------- /code/FINDER_ND_cost/PrepareBatchGraph.pxd: -------------------------------------------------------------------------------- 1 | from libcpp.vector cimport vector 2 | from libcpp.memory cimport shared_ptr 3 | from libcpp.pair cimport pair 4 | from graph cimport Graph 5 | cdef extern from "./src/lib/PrepareBatchGraph.h": 6 | cdef cppclass sparseMatrix: 7 | sparseMatrix()except+ 8 | vector[int] rowIndex 9 | vector[int] colIndex 10 | vector[double] value 11 | int rowNum 12 | int colNum 13 | 14 | cdef cppclass PrepareBatchGraph: 15 | PrepareBatchGraph(int aggregatorID)except+ 16 | void SetupTrain(vector[int] idxes,vector[shared_ptr[Graph] ] g_list,vector[vector[int]] covered,const int* actions)except+ 17 | void SetupPredAll(vector[int] idxes,vector[shared_ptr[Graph] ] g_list,vector[vector[int]] covered)except+ 18 | shared_ptr[sparseMatrix] act_select 19 | shared_ptr[sparseMatrix] rep_global 20 | shared_ptr[sparseMatrix] n2nsum_param 21 | shared_ptr[sparseMatrix] laplacian_param 22 | shared_ptr[sparseMatrix] subgsum_param 23 | vector[vector[int]] idx_map_list 24 | vector[pair[int,int]] subgraph_id_span 25 | vector[vector[double]] aux_feat 26 | vector[vector[double]] node_feat 27 | vector[int] avail_act_cnt 28 | int aggregatorID -------------------------------------------------------------------------------- /code/FINDER_CN/nstep_replay_mem.pxd: -------------------------------------------------------------------------------- 1 | from libcpp.vector cimport vector 2 | from libcpp.memory cimport shared_ptr 3 | from libcpp cimport bool 4 | from graph cimport Graph 5 | from mvc_env cimport MvcEnv 6 | 7 | 8 | cdef extern from "./src/lib/nstep_replay_mem.h": 9 | cdef cppclass ReplaySample: 10 | ReplaySample(int batch_size)except+ 11 | vector[shared_ptr[Graph]] g_list 12 | vector[vector[int]] list_st 13 | vector[vector[int]] list_s_primes 14 | vector[int] list_at 15 | vector[double] list_rt 16 | vector[bool] list_term 17 | 18 | 19 | cdef extern from "./src/lib/nstep_replay_mem.h": 20 | cdef cppclass NStepReplayMem: 21 | NStepReplayMem(int memory_size)except+ 22 | # void Add(shared_ptr[Graph] g,vector[int]& s_t,int a_t,double r_t,vector[int]& s_prime,bool terminal) 23 | void Add(shared_ptr[MvcEnv] env,int n_step)except+ 24 | shared_ptr[ReplaySample] Sampling(int batch_size)except+ 25 | void Clear()except+ 26 | vector[shared_ptr[Graph]] graphs 27 | vector[int] actions 28 | vector[double] rewards 29 | vector[vector[int]] states 30 | vector[vector[int]] s_primes 31 | vector[bool] terminals 32 | int current 33 | int count 34 | int memory_size 35 | 36 | -------------------------------------------------------------------------------- /code/FINDER_ND/nstep_replay_mem.pxd: -------------------------------------------------------------------------------- 1 | from libcpp.vector cimport vector 2 | from libcpp.memory cimport shared_ptr 3 | from libcpp cimport bool 4 | from graph cimport Graph 5 | from mvc_env cimport MvcEnv 6 | 7 | 8 | cdef extern from "./src/lib/nstep_replay_mem.h": 9 | cdef cppclass ReplaySample: 10 | ReplaySample(int batch_size)except+ 11 | vector[shared_ptr[Graph]] g_list 12 | vector[vector[int]] list_st 13 | vector[vector[int]] list_s_primes 14 | vector[int] list_at 15 | vector[double] list_rt 16 | vector[bool] list_term 17 | 18 | 19 | cdef extern from "./src/lib/nstep_replay_mem.h": 20 | cdef cppclass NStepReplayMem: 21 | NStepReplayMem(int memory_size)except+ 22 | # void Add(shared_ptr[Graph] g,vector[int]& s_t,int a_t,double r_t,vector[int]& s_prime,bool terminal) 23 | void Add(shared_ptr[MvcEnv] env,int n_step)except+ 24 | shared_ptr[ReplaySample] Sampling(int batch_size)except+ 25 | void Clear()except+ 26 | vector[shared_ptr[Graph]] graphs 27 | vector[int] actions 28 | vector[double] rewards 29 | vector[vector[int]] states 30 | vector[vector[int]] s_primes 31 | vector[bool] terminals 32 | int current 33 | int count 34 | int memory_size 35 | 36 | -------------------------------------------------------------------------------- /code/FINDER_ND/testSynthetic.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | import sys,os 4 | sys.path.append(os.path.dirname(__file__) + os.sep + '../') 5 | from FINDER import FINDER 6 | from tqdm import tqdm 7 | 8 | 9 | def main(): 10 | dqn = FINDER() 11 | data_test_path = '../data/synthetic/uniform_cost/' 12 | # data_test_name = ['30-50', '50-100', '100-200', '200-300', '300-400', '400-500'] 13 | data_test_name = ['30-50', '50-100'] 14 | model_file = './FINDER_ND/models/nrange_30_50_iter_78000.ckpt' 15 | 16 | file_path = '../results/FINDER_ND/synthetic' 17 | 18 | if not os.path.exists('../results/FINDER_ND'): 19 | os.mkdir('../results/FINDER_ND') 20 | if not os.path.exists('../results/FINDER_ND/synthetic'): 21 | os.mkdir('../results/FINDER_ND/synthetic') 22 | 23 | with open('%s/result.txt'%file_path, 'w') as fout: 24 | for i in tqdm(range(len(data_test_name))): 25 | data_test = data_test_path + data_test_name[i] 26 | score_mean, score_std, time_mean, time_std = dqn.Evaluate(data_test, model_file) 27 | fout.write('%.2f±%.2f,' % (score_mean * 100, score_std * 100)) 28 | fout.flush() 29 | print('\ndata_test_%s has been tested!' % data_test_name[i]) 30 | 31 | 32 | if __name__=="__main__": 33 | main() 34 | -------------------------------------------------------------------------------- /code/FINDER_CN_cost/nstep_replay_mem.pxd: -------------------------------------------------------------------------------- 1 | from libcpp.vector cimport vector 2 | from libcpp.memory cimport shared_ptr 3 | from libcpp cimport bool 4 | from graph cimport Graph 5 | from mvc_env cimport MvcEnv 6 | 7 | 8 | cdef extern from "./src/lib/nstep_replay_mem.h": 9 | cdef cppclass ReplaySample: 10 | ReplaySample(int batch_size)except+ 11 | vector[shared_ptr[Graph]] g_list 12 | vector[vector[int]] list_st 13 | vector[vector[int]] list_s_primes 14 | vector[int] list_at 15 | vector[double] list_rt 16 | vector[bool] list_term 17 | 18 | 19 | cdef extern from "./src/lib/nstep_replay_mem.h": 20 | cdef cppclass NStepReplayMem: 21 | NStepReplayMem(int memory_size)except+ 22 | # void Add(shared_ptr[Graph] g,vector[int]& s_t,int a_t,double r_t,vector[int]& s_prime,bool terminal) 23 | void Add(shared_ptr[MvcEnv] env,int n_step)except+ 24 | shared_ptr[ReplaySample] Sampling(int batch_size)except+ 25 | void Clear()except+ 26 | vector[shared_ptr[Graph]] graphs 27 | vector[int] actions 28 | vector[double] rewards 29 | vector[vector[int]] states 30 | vector[vector[int]] s_primes 31 | vector[bool] terminals 32 | int current 33 | int count 34 | int memory_size 35 | 36 | -------------------------------------------------------------------------------- /code/FINDER_ND_cost/nstep_replay_mem.pxd: -------------------------------------------------------------------------------- 1 | from libcpp.vector cimport vector 2 | from libcpp.memory cimport shared_ptr 3 | from libcpp cimport bool 4 | from graph cimport Graph 5 | from mvc_env cimport MvcEnv 6 | 7 | 8 | cdef extern from "./src/lib/nstep_replay_mem.h": 9 | cdef cppclass ReplaySample: 10 | ReplaySample(int batch_size)except+ 11 | vector[shared_ptr[Graph]] g_list 12 | vector[vector[int]] list_st 13 | vector[vector[int]] list_s_primes 14 | vector[int] list_at 15 | vector[double] list_rt 16 | vector[bool] list_term 17 | 18 | 19 | cdef extern from "./src/lib/nstep_replay_mem.h": 20 | cdef cppclass NStepReplayMem: 21 | NStepReplayMem(int memory_size)except+ 22 | # void Add(shared_ptr[Graph] g,vector[int]& s_t,int a_t,double r_t,vector[int]& s_prime,bool terminal) 23 | void Add(shared_ptr[MvcEnv] env,int n_step)except+ 24 | shared_ptr[ReplaySample] Sampling(int batch_size)except+ 25 | void Clear()except+ 26 | vector[shared_ptr[Graph]] graphs 27 | vector[int] actions 28 | vector[double] rewards 29 | vector[vector[int]] states 30 | vector[vector[int]] s_primes 31 | vector[bool] terminals 32 | int current 33 | int count 34 | int memory_size 35 | 36 | -------------------------------------------------------------------------------- /code/FINDER_CN/src/lib/config.h: -------------------------------------------------------------------------------- 1 | #ifndef cfg_H 2 | #define cfg_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | typedef float Dtype; 11 | 12 | struct cfg 13 | { 14 | static int embed_dim; 15 | static int batch_size; 16 | static int max_n, min_n; 17 | static int mem_size; 18 | static int node_dim; 19 | static int aux_dim; 20 | static bool msg_average; 21 | 22 | static void LoadParams(const int argc, const char** argv) 23 | { 24 | for (int i = 1; i < argc; i += 2) 25 | { 26 | if (strcmp(argv[i], "-embed_dim") == 0) 27 | embed_dim = atoi(argv[i + 1]); 28 | if (strcmp(argv[i], "-max_n") == 0) 29 | max_n = atoi(argv[i + 1]); 30 | if (strcmp(argv[i], "-min_n") == 0) 31 | min_n = atoi(argv[i + 1]); 32 | if (strcmp(argv[i], "-mem_size") == 0) 33 | mem_size = atoi(argv[i + 1]); 34 | if (strcmp(argv[i], "-batch_size") == 0) 35 | batch_size = atoi(argv[i + 1]); 36 | if (strcmp(argv[i], "-msg_average") == 0) 37 | msg_average = atoi(argv[i + 1]); 38 | 39 | } 40 | 41 | if (n_step <= 0) 42 | n_step = max_n; 43 | 44 | } 45 | }; 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /code/FINDER_ND/src/lib/config.h: -------------------------------------------------------------------------------- 1 | #ifndef cfg_H 2 | #define cfg_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | typedef float Dtype; 11 | 12 | struct cfg 13 | { 14 | static int embed_dim; 15 | static int batch_size; 16 | static int max_n, min_n; 17 | static int mem_size; 18 | static int node_dim; 19 | static int aux_dim; 20 | static bool msg_average; 21 | 22 | static void LoadParams(const int argc, const char** argv) 23 | { 24 | for (int i = 1; i < argc; i += 2) 25 | { 26 | if (strcmp(argv[i], "-embed_dim") == 0) 27 | embed_dim = atoi(argv[i + 1]); 28 | if (strcmp(argv[i], "-max_n") == 0) 29 | max_n = atoi(argv[i + 1]); 30 | if (strcmp(argv[i], "-min_n") == 0) 31 | min_n = atoi(argv[i + 1]); 32 | if (strcmp(argv[i], "-mem_size") == 0) 33 | mem_size = atoi(argv[i + 1]); 34 | if (strcmp(argv[i], "-batch_size") == 0) 35 | batch_size = atoi(argv[i + 1]); 36 | if (strcmp(argv[i], "-msg_average") == 0) 37 | msg_average = atoi(argv[i + 1]); 38 | 39 | } 40 | 41 | if (n_step <= 0) 42 | n_step = max_n; 43 | 44 | } 45 | }; 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /code/FINDER_CN/testSynthetic.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | # -*- coding: utf-8 -*- 3 | import sys,os 4 | sys.path.append(os.path.dirname(__file__) + os.sep + '../') 5 | from FINDER import FINDER 6 | from tqdm import tqdm 7 | 8 | def main(): 9 | dqn = FINDER() 10 | data_test_path = '../data/synthetic/uniform_cost/' 11 | # data_test_name = ['30-50', '50-100', '100-200', '200-300', '300-400', '400-500'] 12 | data_test_name =['30-50', '50-100'] 13 | model_file = './FINDER_CN/models/nrange_30_50_iter_93300.ckpt' 14 | 15 | file_path = '../results/FINDER_CN/synthetic' 16 | # if not os.path.exists(file_path): 17 | if not os.path.exists('../results/FINDER_CN'): 18 | os.mkdir('../results/FINDER_CN') 19 | if not os.path.exists('../results/FINDER_CN/synthetic'): 20 | os.mkdir('../results/FINDER_CN/synthetic') 21 | 22 | with open('%s/result.txt'%file_path, 'w') as fout: 23 | for i in tqdm(range(len(data_test_name))): 24 | data_test = data_test_path + data_test_name[i] 25 | score_mean, score_std, time_mean, time_std = dqn.Evaluate(data_test, model_file) 26 | fout.write('%.2f±%.2f,' % (score_mean * 100, score_std * 100)) 27 | fout.flush() 28 | print('data_test_%s has been tested!' % data_test_name[i]) 29 | 30 | 31 | if __name__=="__main__": 32 | main() 33 | -------------------------------------------------------------------------------- /code/FINDER_CN_cost/src/lib/config.h: -------------------------------------------------------------------------------- 1 | #ifndef cfg_H 2 | #define cfg_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | typedef float Dtype; 11 | 12 | struct cfg 13 | { 14 | static int embed_dim; 15 | static int batch_size; 16 | static int max_n, min_n; 17 | static int mem_size; 18 | static int node_dim; 19 | static int aux_dim; 20 | static bool msg_average; 21 | 22 | static void LoadParams(const int argc, const char** argv) 23 | { 24 | for (int i = 1; i < argc; i += 2) 25 | { 26 | if (strcmp(argv[i], "-embed_dim") == 0) 27 | embed_dim = atoi(argv[i + 1]); 28 | if (strcmp(argv[i], "-max_n") == 0) 29 | max_n = atoi(argv[i + 1]); 30 | if (strcmp(argv[i], "-min_n") == 0) 31 | min_n = atoi(argv[i + 1]); 32 | if (strcmp(argv[i], "-mem_size") == 0) 33 | mem_size = atoi(argv[i + 1]); 34 | if (strcmp(argv[i], "-batch_size") == 0) 35 | batch_size = atoi(argv[i + 1]); 36 | if (strcmp(argv[i], "-msg_average") == 0) 37 | msg_average = atoi(argv[i + 1]); 38 | 39 | } 40 | 41 | if (n_step <= 0) 42 | n_step = max_n; 43 | 44 | } 45 | }; 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /code/FINDER_ND_cost/src/lib/config.h: -------------------------------------------------------------------------------- 1 | #ifndef cfg_H 2 | #define cfg_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | typedef float Dtype; 11 | 12 | struct cfg 13 | { 14 | static int embed_dim; 15 | static int batch_size; 16 | static int max_n, min_n; 17 | static int mem_size; 18 | static int node_dim; 19 | static int aux_dim; 20 | static bool msg_average; 21 | 22 | static void LoadParams(const int argc, const char** argv) 23 | { 24 | for (int i = 1; i < argc; i += 2) 25 | { 26 | if (strcmp(argv[i], "-embed_dim") == 0) 27 | embed_dim = atoi(argv[i + 1]); 28 | if (strcmp(argv[i], "-max_n") == 0) 29 | max_n = atoi(argv[i + 1]); 30 | if (strcmp(argv[i], "-min_n") == 0) 31 | min_n = atoi(argv[i + 1]); 32 | if (strcmp(argv[i], "-mem_size") == 0) 33 | mem_size = atoi(argv[i + 1]); 34 | if (strcmp(argv[i], "-batch_size") == 0) 35 | batch_size = atoi(argv[i + 1]); 36 | if (strcmp(argv[i], "-msg_average") == 0) 37 | msg_average = atoi(argv[i + 1]); 38 | 39 | } 40 | 41 | if (n_step <= 0) 42 | n_step = max_n; 43 | 44 | } 45 | }; 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /code/FINDER_CN/src/lib/nstep_replay_mem.h: -------------------------------------------------------------------------------- 1 | #ifndef NSTEP_REPLAY_MEM_H 2 | #define NSTEP_REPLAY_MEM_H 3 | 4 | #include 5 | #include 6 | #include "graph.h" 7 | #include "mvc_env.h" 8 | 9 | 10 | class ReplaySample 11 | { 12 | public: 13 | ReplaySample(int batch_size); 14 | std::vector< std::shared_ptr > g_list; 15 | std::vector< std::vector> list_st, list_s_primes; 16 | std::vector list_at; 17 | std::vector list_rt; 18 | std::vector list_term; 19 | }; 20 | 21 | class NStepReplayMem 22 | { 23 | public: 24 | NStepReplayMem(int memory_size); 25 | 26 | void Add(std::shared_ptr g, 27 | std::vector s_t, 28 | int a_t, 29 | double r_t, 30 | std::vector s_prime, 31 | bool terminal); 32 | 33 | void Add(std::shared_ptr env,int n_step); 34 | 35 | std::shared_ptr Sampling(int batch_size); 36 | std::vector< std::shared_ptr > graphs; 37 | std::vector actions; 38 | std::vector rewards; 39 | std::vector< std::vector > states, s_primes; 40 | std::vector terminals; 41 | 42 | int current, count, memory_size; 43 | std::default_random_engine generator; 44 | std::uniform_int_distribution* distribution; 45 | }; 46 | 47 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND/src/lib/nstep_replay_mem.h: -------------------------------------------------------------------------------- 1 | #ifndef NSTEP_REPLAY_MEM_H 2 | #define NSTEP_REPLAY_MEM_H 3 | 4 | #include 5 | #include 6 | #include "graph.h" 7 | #include "mvc_env.h" 8 | 9 | 10 | class ReplaySample 11 | { 12 | public: 13 | ReplaySample(int batch_size); 14 | std::vector< std::shared_ptr > g_list; 15 | std::vector< std::vector> list_st, list_s_primes; 16 | std::vector list_at; 17 | std::vector list_rt; 18 | std::vector list_term; 19 | }; 20 | 21 | class NStepReplayMem 22 | { 23 | public: 24 | NStepReplayMem(int memory_size); 25 | 26 | void Add(std::shared_ptr g, 27 | std::vector s_t, 28 | int a_t, 29 | double r_t, 30 | std::vector s_prime, 31 | bool terminal); 32 | 33 | void Add(std::shared_ptr env,int n_step); 34 | 35 | std::shared_ptr Sampling(int batch_size); 36 | std::vector< std::shared_ptr > graphs; 37 | std::vector actions; 38 | std::vector rewards; 39 | std::vector< std::vector > states, s_primes; 40 | std::vector terminals; 41 | 42 | int current, count, memory_size; 43 | std::default_random_engine generator; 44 | std::uniform_int_distribution* distribution; 45 | }; 46 | 47 | #endif -------------------------------------------------------------------------------- /code/FINDER_CN_cost/src/lib/nstep_replay_mem.h: -------------------------------------------------------------------------------- 1 | #ifndef NSTEP_REPLAY_MEM_H 2 | #define NSTEP_REPLAY_MEM_H 3 | 4 | #include 5 | #include 6 | #include "graph.h" 7 | #include "mvc_env.h" 8 | 9 | 10 | class ReplaySample 11 | { 12 | public: 13 | ReplaySample(int batch_size); 14 | std::vector< std::shared_ptr > g_list; 15 | std::vector< std::vector> list_st, list_s_primes; 16 | std::vector list_at; 17 | std::vector list_rt; 18 | std::vector list_term; 19 | }; 20 | 21 | class NStepReplayMem 22 | { 23 | public: 24 | NStepReplayMem(int memory_size); 25 | 26 | void Add(std::shared_ptr g, 27 | std::vector s_t, 28 | int a_t, 29 | double r_t, 30 | std::vector s_prime, 31 | bool terminal); 32 | 33 | void Add(std::shared_ptr env,int n_step); 34 | 35 | std::shared_ptr Sampling(int batch_size); 36 | std::vector< std::shared_ptr > graphs; 37 | std::vector actions; 38 | std::vector rewards; 39 | std::vector< std::vector > states, s_primes; 40 | std::vector terminals; 41 | 42 | int current, count, memory_size; 43 | std::default_random_engine generator; 44 | std::uniform_int_distribution* distribution; 45 | }; 46 | 47 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND_cost/src/lib/nstep_replay_mem.h: -------------------------------------------------------------------------------- 1 | #ifndef NSTEP_REPLAY_MEM_H 2 | #define NSTEP_REPLAY_MEM_H 3 | 4 | #include 5 | #include 6 | #include "graph.h" 7 | #include "mvc_env.h" 8 | 9 | 10 | class ReplaySample 11 | { 12 | public: 13 | ReplaySample(int batch_size); 14 | std::vector< std::shared_ptr > g_list; 15 | std::vector< std::vector> list_st, list_s_primes; 16 | std::vector list_at; 17 | std::vector list_rt; 18 | std::vector list_term; 19 | }; 20 | 21 | class NStepReplayMem 22 | { 23 | public: 24 | NStepReplayMem(int memory_size); 25 | 26 | void Add(std::shared_ptr g, 27 | std::vector s_t, 28 | int a_t, 29 | double r_t, 30 | std::vector s_prime, 31 | bool terminal); 32 | 33 | void Add(std::shared_ptr env,int n_step); 34 | 35 | std::shared_ptr Sampling(int batch_size); 36 | std::vector< std::shared_ptr > graphs; 37 | std::vector actions; 38 | std::vector rewards; 39 | std::vector< std::vector > states, s_primes; 40 | std::vector terminals; 41 | 42 | int current, count, memory_size; 43 | std::default_random_engine generator; 44 | std::uniform_int_distribution* distribution; 45 | }; 46 | 47 | #endif -------------------------------------------------------------------------------- /code/FINDER_CN_cost/testSynthetic.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | # -*- coding: utf-8 -*- 3 | import sys,os 4 | sys.path.append(os.path.dirname(__file__) + os.sep + '../') 5 | from FINDER import FINDER 6 | from tqdm import tqdm 7 | 8 | 9 | 10 | def main(): 11 | dqn = FINDER() 12 | cost_types = ['degree_cost', 'random_cost'] 13 | for cost in cost_types: 14 | data_test_path = '../data/synthetic/%s/'%cost 15 | data_test_name = ['30-50', '50-100', '100-200', '200-300', '300-400', '400-500'] 16 | model_file = './models/nrange_30_50_iter_122100.ckpt' 17 | 18 | file_path = '../results/FINDER_CN_cost/synthetic' 19 | if not os.path.exists('../results/FINDER_CN_cost'): 20 | os.mkdir('../results/FINDER_CN_cost') 21 | if not os.path.exists('../results/FINDER_CN_cost/synthetic'): 22 | os.mkdir('../results/FINDER_CN_cost/synthetic') 23 | 24 | with open('%s/%s_score.txt'%(file_path, cost), 'w') as fout: 25 | for i in tqdm(range(len(data_test_name))): 26 | data_test = data_test_path + data_test_name[i] 27 | score_mean, score_std, time_mean, time_std = dqn.Evaluate(data_test, model_file) 28 | fout.write('%.2f±%.2f,' % (score_mean * 100, score_std * 100)) 29 | fout.flush() 30 | print('data_test_%s has been tested!' % data_test_name[i]) 31 | 32 | 33 | if __name__=="__main__": 34 | main() 35 | -------------------------------------------------------------------------------- /code/FINDER_ND_cost/testSynthetic.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | # -*- coding: utf-8 -*- 3 | import sys,os 4 | sys.path.append(os.path.dirname(__file__) + os.sep + '../') 5 | from FINDER import FINDER 6 | from tqdm import tqdm 7 | 8 | 9 | def main(): 10 | dqn = FINDER() 11 | cost_types = ['degree_cost', 'random_cost'] 12 | for cost in cost_types: 13 | data_test_path = '../data/synthetic/%s/'%cost 14 | # data_test_name = ['30-50', '50-100', '100-200', '200-300', '300-400', '400-500'] 15 | data_test_name = ['30-50', '50-100'] 16 | model_file = './FINDER_ND_cost/models/nrange_30_50_iter_134100.ckpt' 17 | 18 | file_path = '../results/FINDER_ND_cost/synthetic' 19 | 20 | if not os.path.exists('../results/FINDER_ND_cost'): 21 | os.mkdir('../results/FINDER_ND_cost') 22 | if not os.path.exists('../results/FINDER_ND_cost/synthetic'): 23 | os.mkdir('../results/FINDER_ND_cost/synthetic') 24 | 25 | with open('%s/%s_score.txt'%(file_path, cost), 'w') as fout: 26 | for i in tqdm(range(len(data_test_name))): 27 | data_test = data_test_path + data_test_name[i] 28 | score_mean, score_std, time_mean, time_std = dqn.Evaluate(data_test, model_file) 29 | fout.write('%.2f±%.2f,' % (score_mean * 100, score_std * 100)) 30 | fout.flush() 31 | print('data_test_%s has been tested!' % data_test_name[i]) 32 | 33 | if __name__=="__main__": 34 | main() 35 | -------------------------------------------------------------------------------- /code/FINDER_CN_cost/src/lib/graph_utils.cpp: -------------------------------------------------------------------------------- 1 | #include "graph_utils.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | //#include "stdio.h" 7 | 8 | GraphUtil::GraphUtil() 9 | { 10 | 11 | } 12 | 13 | GraphUtil::~GraphUtil() 14 | { 15 | 16 | } 17 | 18 | void GraphUtil::deleteNode(std::vector > &adjListGraph, int node) 19 | { 20 | // for (auto neighbour : adjListGraph[node]) 21 | // { 22 | for (int i = 0;i<(int)adjListGraph[node].size();++i) 23 | { 24 | int neighbour = adjListGraph[node][i]; 25 | adjListGraph[neighbour].erase(remove(adjListGraph[neighbour].begin(), adjListGraph[neighbour].end(), node), adjListGraph[neighbour].end()); 26 | } 27 | 28 | adjListGraph[node].clear(); 29 | } 30 | 31 | void GraphUtil::recoverAddNode(const std::vector > &backupCompletedAdjListGraph, std::vector& backupAllVex, std::vector > &adjListGraph, int node, Disjoint_Set &unionSet) 32 | { 33 | 34 | for (int i = 0; i < (int)backupCompletedAdjListGraph[node].size(); i++) 35 | { 36 | int neighbourNode = backupCompletedAdjListGraph[node][i]; 37 | 38 | if (backupAllVex[neighbourNode]) 39 | { 40 | addEdge(adjListGraph, node, neighbourNode); 41 | unionSet.merge(node, neighbourNode); 42 | } 43 | } 44 | 45 | backupAllVex[node] = true; 46 | } 47 | 48 | void GraphUtil::addEdge(std::vector > &adjListGraph, int node0, int node1) 49 | { 50 | if (((int)adjListGraph.size() - 1) < std::max(node0, node1)) 51 | { 52 | adjListGraph.resize(std::max(node0, node1) + 1); 53 | } 54 | 55 | adjListGraph[node0].push_back(node1); 56 | adjListGraph[node1].push_back(node0); 57 | } 58 | 59 | -------------------------------------------------------------------------------- /code/FINDER_ND/setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | from distutils.extension import Extension 3 | from Cython.Distutils import build_ext 4 | 5 | setup( 6 | cmdclass = {'build_ext':build_ext}, 7 | ext_modules = [ 8 | Extension('PrepareBatchGraph', sources = ['PrepareBatchGraph.pyx','src/lib/PrepareBatchGraph.cpp','src/lib/graph.cpp','src/lib/graph_struct.cpp', 'src/lib/disjoint_set.cpp'],language='c++',extra_compile_args=['-std=c++11']), 9 | Extension('graph', sources=['graph.pyx', 'src/lib/graph.cpp'], language='c++',extra_compile_args=['-std=c++11']), 10 | Extension('mvc_env', sources=['mvc_env.pyx', 'src/lib/mvc_env.cpp', 'src/lib/graph.cpp','src/lib/disjoint_set.cpp'], language='c++',extra_compile_args=['-std=c++11']), 11 | Extension('utils', sources=['utils.pyx', 'src/lib/utils.cpp', 'src/lib/graph.cpp', 'src/lib/graph_utils.cpp', 'src/lib/disjoint_set.cpp', 'src/lib/decrease_strategy.cpp'], language='c++',extra_compile_args=['-std=c++11']), 12 | Extension('nstep_replay_mem', sources=['nstep_replay_mem.pyx', 'src/lib/nstep_replay_mem.cpp', 'src/lib/graph.cpp', 'src/lib/mvc_env.cpp', 'src/lib/disjoint_set.cpp'], language='c++',extra_compile_args=['-std=c++11']), 13 | Extension('nstep_replay_mem_prioritized',sources=['nstep_replay_mem_prioritized.pyx', 'src/lib/nstep_replay_mem_prioritized.cpp','src/lib/graph.cpp', 'src/lib/mvc_env.cpp', 'src/lib/disjoint_set.cpp'], language='c++',extra_compile_args=['-std=c++11']), 14 | Extension('graph_struct', sources=['graph_struct.pyx', 'src/lib/graph_struct.cpp'], language='c++',extra_compile_args=['-std=c++11']), 15 | Extension('FINDER', sources = ['FINDER.pyx']) 16 | ]) -------------------------------------------------------------------------------- /code/FINDER_ND_cost/src/lib/graph_utils.cpp: -------------------------------------------------------------------------------- 1 | #include "graph_utils.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | //#include "stdio.h" 7 | 8 | GraphUtil::GraphUtil() 9 | { 10 | 11 | } 12 | 13 | GraphUtil::~GraphUtil() 14 | { 15 | 16 | } 17 | 18 | void GraphUtil::deleteNode(std::vector > &adjListGraph, int node) 19 | { 20 | // for (auto neighbour : adjListGraph[node]) 21 | // { 22 | for (int i = 0;i<(int)adjListGraph[node].size();++i) 23 | { 24 | int neighbour = adjListGraph[node][i]; 25 | adjListGraph[neighbour].erase(remove(adjListGraph[neighbour].begin(), adjListGraph[neighbour].end(), node), adjListGraph[neighbour].end()); 26 | } 27 | 28 | adjListGraph[node].clear(); 29 | } 30 | 31 | void GraphUtil::recoverAddNode(const std::vector > &backupCompletedAdjListGraph, std::vector& backupAllVex, std::vector > &adjListGraph, int node, Disjoint_Set &unionSet) 32 | { 33 | 34 | for (int i = 0; i < (int)backupCompletedAdjListGraph[node].size(); i++) 35 | { 36 | int neighbourNode = backupCompletedAdjListGraph[node][i]; 37 | 38 | if (backupAllVex[neighbourNode]) 39 | { 40 | addEdge(adjListGraph, node, neighbourNode); 41 | unionSet.merge(node, neighbourNode); 42 | } 43 | } 44 | 45 | backupAllVex[node] = true; 46 | } 47 | 48 | void GraphUtil::addEdge(std::vector > &adjListGraph, int node0, int node1) 49 | { 50 | if (((int)adjListGraph.size() - 1) < std::max(node0, node1)) 51 | { 52 | adjListGraph.resize(std::max(node0, node1) + 1); 53 | } 54 | 55 | adjListGraph[node0].push_back(node1); 56 | adjListGraph[node1].push_back(node0); 57 | } 58 | 59 | -------------------------------------------------------------------------------- /code/FINDER_CN_cost/setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | from distutils.extension import Extension 3 | # from Cython.Build import cythonize 4 | from Cython.Distutils import build_ext 5 | 6 | setup( 7 | cmdclass = {'build_ext':build_ext}, 8 | ext_modules = [ 9 | Extension('PrepareBatchGraph', sources = ['PrepareBatchGraph.pyx','src/lib/PrepareBatchGraph.cpp','src/lib/graph.cpp','src/lib/graph_struct.cpp', 'src/lib/disjoint_set.cpp'],language='c++',extra_compile_args=['-std=c++11']), 10 | Extension('graph', sources=['graph.pyx', 'src/lib/graph.cpp'], language='c++',extra_compile_args=['-std=c++11']), 11 | Extension('mvc_env', sources=['mvc_env.pyx', 'src/lib/mvc_env.cpp', 'src/lib/graph.cpp','src/lib/disjoint_set.cpp'], language='c++',extra_compile_args=['-std=c++11']), 12 | Extension('utils', sources=['utils.pyx', 'src/lib/utils.cpp', 'src/lib/graph.cpp', 'src/lib/graph_utils.cpp', 'src/lib/disjoint_set.cpp', 'src/lib/decrease_strategy.cpp'], language='c++',extra_compile_args=['-std=c++11']), 13 | Extension('nstep_replay_mem', sources=['nstep_replay_mem.pyx', 'src/lib/nstep_replay_mem.cpp', 'src/lib/graph.cpp', 'src/lib/mvc_env.cpp', 'src/lib/disjoint_set.cpp'], language='c++',extra_compile_args=['-std=c++11']), 14 | Extension('nstep_replay_mem_prioritized',sources=['nstep_replay_mem_prioritized.pyx', 'src/lib/nstep_replay_mem_prioritized.cpp','src/lib/graph.cpp', 'src/lib/mvc_env.cpp', 'src/lib/disjoint_set.cpp'], language='c++',extra_compile_args=['-std=c++11']), 15 | Extension('graph_struct', sources=['graph_struct.pyx', 'src/lib/graph_struct.cpp'], language='c++',extra_compile_args=['-std=c++11']), 16 | Extension('FINDER', sources = ['FINDER.pyx']) 17 | ]) -------------------------------------------------------------------------------- /code/FINDER_CN/setup.py: -------------------------------------------------------------------------------- 1 | 2 | from distutils.core import setup 3 | from distutils.extension import Extension 4 | # from Cython.Build import cythonize 5 | from Cython.Distutils import build_ext 6 | 7 | setup( 8 | cmdclass = {'build_ext':build_ext}, 9 | 10 | #################for ubuntu compile 11 | ext_modules = [ 12 | Extension('PrepareBatchGraph', sources = ['PrepareBatchGraph.pyx','src/lib/PrepareBatchGraph.cpp','src/lib/graph.cpp','src/lib/graph_struct.cpp', 'src/lib/disjoint_set.cpp'],language='c++',extra_compile_args=['-std=c++11']), 13 | Extension('graph', sources=['graph.pyx', 'src/lib/graph.cpp'], language='c++',extra_compile_args=['-std=c++11']), 14 | Extension('mvc_env', sources=['mvc_env.pyx', 'src/lib/mvc_env.cpp', 'src/lib/graph.cpp','src/lib/disjoint_set.cpp'], language='c++',extra_compile_args=['-std=c++11']), 15 | Extension('utils', sources=['utils.pyx', 'src/lib/utils.cpp', 'src/lib/graph.cpp', 'src/lib/graph_utils.cpp', 'src/lib/disjoint_set.cpp', 'src/lib/decrease_strategy.cpp'], language='c++',extra_compile_args=['-std=c++11']), 16 | Extension('nstep_replay_mem', sources=['nstep_replay_mem.pyx', 'src/lib/nstep_replay_mem.cpp', 'src/lib/graph.cpp', 'src/lib/mvc_env.cpp', 'src/lib/disjoint_set.cpp'], language='c++',extra_compile_args=['-std=c++11']), 17 | Extension('nstep_replay_mem_prioritized',sources=['nstep_replay_mem_prioritized.pyx', 'src/lib/nstep_replay_mem_prioritized.cpp','src/lib/graph.cpp', 'src/lib/mvc_env.cpp', 'src/lib/disjoint_set.cpp'], language='c++',extra_compile_args=['-std=c++11']), 18 | Extension('graph_struct', sources=['graph_struct.pyx', 'src/lib/graph_struct.cpp'], language='c++',extra_compile_args=['-std=c++11']), 19 | Extension('FINDER', sources = ['FINDER.pyx']) 20 | ]) -------------------------------------------------------------------------------- /code/FINDER_ND_cost/setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | from distutils.extension import Extension 3 | # from Cython.Build import cythonize 4 | from Cython.Distutils import build_ext 5 | 6 | setup( 7 | cmdclass = {'build_ext':build_ext}, 8 | 9 | #################for ubuntu compile 10 | ext_modules = [ 11 | Extension('PrepareBatchGraph', sources = ['PrepareBatchGraph.pyx','src/lib/PrepareBatchGraph.cpp','src/lib/graph.cpp','src/lib/graph_struct.cpp', 'src/lib/disjoint_set.cpp'],language='c++',extra_compile_args=['-std=c++11']), 12 | Extension('graph', sources=['graph.pyx', 'src/lib/graph.cpp'], language='c++',extra_compile_args=['-std=c++11']), 13 | Extension('mvc_env', sources=['mvc_env.pyx', 'src/lib/mvc_env.cpp', 'src/lib/graph.cpp','src/lib/disjoint_set.cpp'], language='c++',extra_compile_args=['-std=c++11']), 14 | Extension('utils', sources=['utils.pyx', 'src/lib/utils.cpp', 'src/lib/graph.cpp', 'src/lib/graph_utils.cpp', 'src/lib/disjoint_set.cpp', 'src/lib/decrease_strategy.cpp'], language='c++',extra_compile_args=['-std=c++11']), 15 | Extension('nstep_replay_mem', sources=['nstep_replay_mem.pyx', 'src/lib/nstep_replay_mem.cpp', 'src/lib/graph.cpp', 'src/lib/mvc_env.cpp', 'src/lib/disjoint_set.cpp'], language='c++',extra_compile_args=['-std=c++11']), 16 | Extension('nstep_replay_mem_prioritized',sources=['nstep_replay_mem_prioritized.pyx', 'src/lib/nstep_replay_mem_prioritized.cpp','src/lib/graph.cpp', 'src/lib/mvc_env.cpp', 'src/lib/disjoint_set.cpp'], language='c++',extra_compile_args=['-std=c++11']), 17 | Extension('graph_struct', sources=['graph_struct.pyx', 'src/lib/graph_struct.cpp'], language='c++',extra_compile_args=['-std=c++11']), 18 | Extension('FINDER', sources = ['FINDER.pyx']) 19 | ]) 20 | 21 | -------------------------------------------------------------------------------- /code/FINDER_CN/graph_struct.pyx: -------------------------------------------------------------------------------- 1 | from cython.operator import dereference as deref 2 | from libcpp.memory cimport shared_ptr 3 | # import gc 4 | from libc.stdlib cimport free 5 | 6 | cdef class py_GraphStruct: 7 | cdef shared_ptr[GraphStruct] inner_GraphStruct 8 | def __cinit__(self): 9 | self.inner_GraphStruct = shared_ptr[GraphStruct](new GraphStruct()) 10 | # def __dealloc__(self): 11 | # if self.inner_GraphStruct != NULL: 12 | # self.inner_GraphStruct.reset() 13 | # gc.collect() 14 | def AddEdge(self,int idx, int x, int y): 15 | deref(self.inner_GraphStruct).AddEdge(idx,x,y) 16 | 17 | def AddNode(self,int subg_id, int n_idx): 18 | deref(self.inner_GraphStruct).AddNode(subg_id,n_idx) 19 | 20 | def Resize(self,unsigned _num_subgraph, unsigned _num_nodes): 21 | deref(self.inner_GraphStruct).Resize(_num_subgraph,_num_nodes) 22 | 23 | @property 24 | def num_nodes(self): 25 | return deref(self.inner_GraphStruct).num_nodes 26 | 27 | @property 28 | def num_edges(self): 29 | return deref(self.inner_GraphStruct).num_edges 30 | 31 | @property 32 | def num_subgraph(self): 33 | return deref(self.inner_GraphStruct).num_subgraph 34 | 35 | @property 36 | def edge_list(self): 37 | return deref(self.inner_GraphStruct).edge_list 38 | 39 | @property 40 | def out_edges(self): 41 | return deref(deref(self.inner_GraphStruct).out_edges).head 42 | @property 43 | def in_edges(self): 44 | return deref(deref(self.inner_GraphStruct).in_edges).head 45 | # cint_edges_from = np.zeros([num_edges],dtype=np.int) 46 | # cint_edges_to = np.zeros([num_edges],dtype=np.int) 47 | # for i in range(num_edges): 48 | # cint_edges_from[i]=edge_list[i].first 49 | # cint_edges_to[i] =edge_list[i].second -------------------------------------------------------------------------------- /code/FINDER_ND/graph_struct.pyx: -------------------------------------------------------------------------------- 1 | from cython.operator import dereference as deref 2 | from libcpp.memory cimport shared_ptr 3 | # import gc 4 | from libc.stdlib cimport free 5 | 6 | cdef class py_GraphStruct: 7 | cdef shared_ptr[GraphStruct] inner_GraphStruct 8 | def __cinit__(self): 9 | self.inner_GraphStruct = shared_ptr[GraphStruct](new GraphStruct()) 10 | # def __dealloc__(self): 11 | # if self.inner_GraphStruct != NULL: 12 | # self.inner_GraphStruct.reset() 13 | # gc.collect() 14 | def AddEdge(self,int idx, int x, int y): 15 | deref(self.inner_GraphStruct).AddEdge(idx,x,y) 16 | 17 | def AddNode(self,int subg_id, int n_idx): 18 | deref(self.inner_GraphStruct).AddNode(subg_id,n_idx) 19 | 20 | def Resize(self,unsigned _num_subgraph, unsigned _num_nodes): 21 | deref(self.inner_GraphStruct).Resize(_num_subgraph,_num_nodes) 22 | 23 | @property 24 | def num_nodes(self): 25 | return deref(self.inner_GraphStruct).num_nodes 26 | 27 | @property 28 | def num_edges(self): 29 | return deref(self.inner_GraphStruct).num_edges 30 | 31 | @property 32 | def num_subgraph(self): 33 | return deref(self.inner_GraphStruct).num_subgraph 34 | 35 | @property 36 | def edge_list(self): 37 | return deref(self.inner_GraphStruct).edge_list 38 | 39 | @property 40 | def out_edges(self): 41 | return deref(deref(self.inner_GraphStruct).out_edges).head 42 | @property 43 | def in_edges(self): 44 | return deref(deref(self.inner_GraphStruct).in_edges).head 45 | # cint_edges_from = np.zeros([num_edges],dtype=np.int) 46 | # cint_edges_to = np.zeros([num_edges],dtype=np.int) 47 | # for i in range(num_edges): 48 | # cint_edges_from[i]=edge_list[i].first 49 | # cint_edges_to[i] =edge_list[i].second -------------------------------------------------------------------------------- /code/FINDER_CN_cost/graph_struct.pyx: -------------------------------------------------------------------------------- 1 | from cython.operator import dereference as deref 2 | from libcpp.memory cimport shared_ptr 3 | # import gc 4 | from libc.stdlib cimport free 5 | 6 | cdef class py_GraphStruct: 7 | cdef shared_ptr[GraphStruct] inner_GraphStruct 8 | def __cinit__(self): 9 | self.inner_GraphStruct = shared_ptr[GraphStruct](new GraphStruct()) 10 | # def __dealloc__(self): 11 | # if self.inner_GraphStruct != NULL: 12 | # self.inner_GraphStruct.reset() 13 | # gc.collect() 14 | def AddEdge(self,int idx, int x, int y): 15 | deref(self.inner_GraphStruct).AddEdge(idx,x,y) 16 | 17 | def AddNode(self,int subg_id, int n_idx): 18 | deref(self.inner_GraphStruct).AddNode(subg_id,n_idx) 19 | 20 | def Resize(self,unsigned _num_subgraph, unsigned _num_nodes): 21 | deref(self.inner_GraphStruct).Resize(_num_subgraph,_num_nodes) 22 | 23 | @property 24 | def num_nodes(self): 25 | return deref(self.inner_GraphStruct).num_nodes 26 | 27 | @property 28 | def num_edges(self): 29 | return deref(self.inner_GraphStruct).num_edges 30 | 31 | @property 32 | def num_subgraph(self): 33 | return deref(self.inner_GraphStruct).num_subgraph 34 | 35 | @property 36 | def edge_list(self): 37 | return deref(self.inner_GraphStruct).edge_list 38 | 39 | @property 40 | def out_edges(self): 41 | return deref(deref(self.inner_GraphStruct).out_edges).head 42 | @property 43 | def in_edges(self): 44 | return deref(deref(self.inner_GraphStruct).in_edges).head 45 | # cint_edges_from = np.zeros([num_edges],dtype=np.int) 46 | # cint_edges_to = np.zeros([num_edges],dtype=np.int) 47 | # for i in range(num_edges): 48 | # cint_edges_from[i]=edge_list[i].first 49 | # cint_edges_to[i] =edge_list[i].second -------------------------------------------------------------------------------- /code/FINDER_ND_cost/graph_struct.pyx: -------------------------------------------------------------------------------- 1 | from cython.operator import dereference as deref 2 | from libcpp.memory cimport shared_ptr 3 | # import gc 4 | from libc.stdlib cimport free 5 | 6 | cdef class py_GraphStruct: 7 | cdef shared_ptr[GraphStruct] inner_GraphStruct 8 | def __cinit__(self): 9 | self.inner_GraphStruct = shared_ptr[GraphStruct](new GraphStruct()) 10 | # def __dealloc__(self): 11 | # if self.inner_GraphStruct != NULL: 12 | # self.inner_GraphStruct.reset() 13 | # gc.collect() 14 | def AddEdge(self,int idx, int x, int y): 15 | deref(self.inner_GraphStruct).AddEdge(idx,x,y) 16 | 17 | def AddNode(self,int subg_id, int n_idx): 18 | deref(self.inner_GraphStruct).AddNode(subg_id,n_idx) 19 | 20 | def Resize(self,unsigned _num_subgraph, unsigned _num_nodes): 21 | deref(self.inner_GraphStruct).Resize(_num_subgraph,_num_nodes) 22 | 23 | @property 24 | def num_nodes(self): 25 | return deref(self.inner_GraphStruct).num_nodes 26 | 27 | @property 28 | def num_edges(self): 29 | return deref(self.inner_GraphStruct).num_edges 30 | 31 | @property 32 | def num_subgraph(self): 33 | return deref(self.inner_GraphStruct).num_subgraph 34 | 35 | @property 36 | def edge_list(self): 37 | return deref(self.inner_GraphStruct).edge_list 38 | 39 | @property 40 | def out_edges(self): 41 | return deref(deref(self.inner_GraphStruct).out_edges).head 42 | @property 43 | def in_edges(self): 44 | return deref(deref(self.inner_GraphStruct).in_edges).head 45 | # cint_edges_from = np.zeros([num_edges],dtype=np.int) 46 | # cint_edges_to = np.zeros([num_edges],dtype=np.int) 47 | # for i in range(num_edges): 48 | # cint_edges_from[i]=edge_list[i].first 49 | # cint_edges_to[i] =edge_list[i].second -------------------------------------------------------------------------------- /code/FINDER_CN/src/lib/graph_utils.cpp: -------------------------------------------------------------------------------- 1 | #include "graph_utils.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | //#include "stdio.h" 7 | 8 | GraphUtil::GraphUtil() 9 | { 10 | 11 | } 12 | 13 | GraphUtil::~GraphUtil() 14 | { 15 | 16 | } 17 | 18 | 19 | void GraphUtil::deleteNode(std::vector > &adjListGraph, int node) 20 | { 21 | // for (auto neighbour : adjListGraph[node]) 22 | // { 23 | // adjListGraph[neighbour].erase(remove(adjListGraph[neighbour].begin(), adjListGraph[neighbour].end(), node), adjListGraph[neighbour].end()); 24 | // } 25 | for (int i=0; i<(int)adjListGraph[node].size(); ++i) 26 | { 27 | int neighbour = adjListGraph[node][i]; 28 | adjListGraph[neighbour].erase(remove(adjListGraph[neighbour].begin(), adjListGraph[neighbour].end(), node), adjListGraph[neighbour].end()); 29 | } 30 | adjListGraph[node].clear(); 31 | } 32 | 33 | void GraphUtil::recoverAddNode(const std::vector > &backupCompletedAdjListGraph, std::vector& backupAllVex, std::vector > &adjListGraph, int node, Disjoint_Set &unionSet) 34 | { 35 | 36 | for (int i = 0; i < (int)backupCompletedAdjListGraph[node].size(); i++) 37 | { 38 | int neighbourNode = backupCompletedAdjListGraph[node][i]; 39 | 40 | if (backupAllVex[neighbourNode]) 41 | { 42 | addEdge(adjListGraph, node, neighbourNode); 43 | unionSet.merge(node, neighbourNode); 44 | } 45 | } 46 | 47 | backupAllVex[node] = true; 48 | } 49 | 50 | void GraphUtil::addEdge(std::vector > &adjListGraph, int node0, int node1) 51 | { 52 | if (((int)adjListGraph.size() - 1) < std::max(node0, node1)) 53 | { 54 | adjListGraph.resize(std::max(node0, node1) + 1); 55 | } 56 | 57 | adjListGraph[node0].push_back(node1); 58 | adjListGraph[node1].push_back(node0); 59 | } 60 | 61 | -------------------------------------------------------------------------------- /code/FINDER_ND/src/lib/graph_utils.cpp: -------------------------------------------------------------------------------- 1 | #include "graph_utils.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | //#include "stdio.h" 7 | 8 | GraphUtil::GraphUtil() 9 | { 10 | 11 | } 12 | 13 | GraphUtil::~GraphUtil() 14 | { 15 | 16 | } 17 | 18 | 19 | void GraphUtil::deleteNode(std::vector > &adjListGraph, int node) 20 | { 21 | // for (auto neighbour : adjListGraph[node]) 22 | // { 23 | // adjListGraph[neighbour].erase(remove(adjListGraph[neighbour].begin(), adjListGraph[neighbour].end(), node), adjListGraph[neighbour].end()); 24 | // } 25 | for (int i=0; i<(int)adjListGraph[node].size(); ++i) 26 | { 27 | int neighbour = adjListGraph[node][i]; 28 | adjListGraph[neighbour].erase(remove(adjListGraph[neighbour].begin(), adjListGraph[neighbour].end(), node), adjListGraph[neighbour].end()); 29 | } 30 | adjListGraph[node].clear(); 31 | } 32 | 33 | void GraphUtil::recoverAddNode(const std::vector > &backupCompletedAdjListGraph, std::vector& backupAllVex, std::vector > &adjListGraph, int node, Disjoint_Set &unionSet) 34 | { 35 | 36 | for (int i = 0; i < (int)backupCompletedAdjListGraph[node].size(); i++) 37 | { 38 | int neighbourNode = backupCompletedAdjListGraph[node][i]; 39 | 40 | if (backupAllVex[neighbourNode]) 41 | { 42 | addEdge(adjListGraph, node, neighbourNode); 43 | unionSet.merge(node, neighbourNode); 44 | } 45 | } 46 | 47 | backupAllVex[node] = true; 48 | } 49 | 50 | void GraphUtil::addEdge(std::vector > &adjListGraph, int node0, int node1) 51 | { 52 | if (((int)adjListGraph.size() - 1) < std::max(node0, node1)) 53 | { 54 | adjListGraph.resize(std::max(node0, node1) + 1); 55 | } 56 | 57 | adjListGraph[node0].push_back(node1); 58 | adjListGraph[node1].push_back(node0); 59 | } 60 | 61 | -------------------------------------------------------------------------------- /code/FINDER_ND_cost/src/lib/utils_py27.cpp: -------------------------------------------------------------------------------- 1 | #include "utils_py27.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "stdio.h" 7 | #include 8 | #include 9 | 10 | Utils::Utils() 11 | { 12 | MaxWccSzList.clear(); 13 | } 14 | 15 | double Utils::getRobustness(std::vector edges_from, std::vector edges_to,int num_nodes,std::vector nodes_weight, std::vector solution) 16 | { 17 | MaxWccSzList.clear(); 18 | std::vector> backupCompletedAdjListGraph; 19 | backupCompletedAdjListGraph.resize(num_nodes); 20 | for (int i = 0; i < (int)edges_from.size(); ++i) 21 | { 22 | int x = edges_from[i], y = edges_to[i]; 23 | backupCompletedAdjListGraph[x].push_back(y); 24 | backupCompletedAdjListGraph[y].push_back(x); 25 | } 26 | std::vector> current_adj_list; 27 | GraphUtil graphutil =GraphUtil(); 28 | Disjoint_Set disjoint_Set = Disjoint_Set(num_nodes); 29 | std::vector backupAllVex(num_nodes, false); 30 | int totalMaxNum = 0; 31 | double solution_weights = 0.0; 32 | double total_nodes_weight =0.0; 33 | for(int i =0;i<(int)nodes_weight.size();++i){ 34 | total_nodes_weight+=nodes_weight[i]; 35 | } 36 | 37 | 38 | for (int i = (int)solution.size()-1;i>=0;i=i-1) 39 | { 40 | int Node = solution[i]; 41 | if (i == 0) 42 | { 43 | solution_weights = 0; 44 | } 45 | else 46 | { 47 | solution_weights = nodes_weight[solution[i-1]]; 48 | } 49 | graphutil.recoverAddNode(backupCompletedAdjListGraph, backupAllVex, current_adj_list, Node, disjoint_Set); 50 | totalMaxNum += (double)disjoint_Set.maxRankCount * solution_weights; 51 | MaxWccSzList.push_back((double)disjoint_Set.maxRankCount /num_nodes); 52 | } 53 | 54 | std::reverse(MaxWccSzList.begin(), MaxWccSzList.end()); 55 | 56 | return (double)totalMaxNum/(num_nodes*total_nodes_weight); 57 | } 58 | 59 | -------------------------------------------------------------------------------- /code/FINDER_CN/src/lib/nstep_replay_mem_prioritized.h: -------------------------------------------------------------------------------- 1 | #ifndef NSTEP_REPLAY_MEM_PRIORITIZED_H 2 | #define NSTEP_REPLAY_MEM_PRIORITIZED_H 3 | 4 | #include 5 | #include 6 | #include "graph.h" 7 | #include "mvc_env.h" 8 | 9 | 10 | class Data{ 11 | public: 12 | Data(); 13 | std::shared_ptr g; 14 | std::vector s_t; 15 | std::vector s_prime; 16 | int a_t; 17 | double r_t; 18 | bool term_t; 19 | }; 20 | 21 | class LeafResult{ 22 | public: 23 | LeafResult(); 24 | int leaf_idx; 25 | double p; 26 | std::shared_ptr data; 27 | }; 28 | 29 | class SumTree 30 | { 31 | public: 32 | SumTree(int capacity); 33 | int capacity; 34 | int data_pointer; 35 | //bool isOverWrite; 36 | double minElement; 37 | double maxElement; 38 | std::vector tree; 39 | std::vector> data; 40 | void Add(double p, std::shared_ptr data); 41 | void Update(int tree_idx,double p); 42 | std::shared_ptr Get_leaf(double v); 43 | }; 44 | 45 | 46 | class ReplaySample{ 47 | public: 48 | ReplaySample(int batch_size); 49 | std::vector b_idx; 50 | std::vector ISWeights; 51 | std::vector< std::shared_ptr > g_list; 52 | std::vector< std::vector> list_st, list_s_primes; 53 | std::vector list_at; 54 | std::vector list_rt; 55 | std::vector list_term; 56 | }; 57 | 58 | 59 | class Memory{ 60 | public: 61 | Memory(double epsilon,double alpha,double beta,double beta_increment_per_sampling,double abs_err_upper,int capacity); 62 | std::shared_ptr tree; 63 | double epsilon; 64 | double alpha; 65 | double beta; 66 | double beta_increment_per_sampling; 67 | double abs_err_upper; 68 | void Store(std::shared_ptr transition); 69 | void Add(std::shared_ptr env,int n_step); 70 | std::shared_ptr Sampling(int n); 71 | void batch_update(std::vector tree_idx, std::vector abs_errors); 72 | }; 73 | 74 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND/src/lib/nstep_replay_mem_prioritized.h: -------------------------------------------------------------------------------- 1 | #ifndef NSTEP_REPLAY_MEM_PRIORITIZED_H 2 | #define NSTEP_REPLAY_MEM_PRIORITIZED_H 3 | 4 | #include 5 | #include 6 | #include "graph.h" 7 | #include "mvc_env.h" 8 | 9 | 10 | class Data{ 11 | public: 12 | Data(); 13 | std::shared_ptr g; 14 | std::vector s_t; 15 | std::vector s_prime; 16 | int a_t; 17 | double r_t; 18 | bool term_t; 19 | }; 20 | 21 | class LeafResult{ 22 | public: 23 | LeafResult(); 24 | int leaf_idx; 25 | double p; 26 | std::shared_ptr data; 27 | }; 28 | 29 | class SumTree 30 | { 31 | public: 32 | SumTree(int capacity); 33 | int capacity; 34 | int data_pointer; 35 | //bool isOverWrite; 36 | double minElement; 37 | double maxElement; 38 | std::vector tree; 39 | std::vector> data; 40 | void Add(double p, std::shared_ptr data); 41 | void Update(int tree_idx,double p); 42 | std::shared_ptr Get_leaf(double v); 43 | }; 44 | 45 | 46 | class ReplaySample{ 47 | public: 48 | ReplaySample(int batch_size); 49 | std::vector b_idx; 50 | std::vector ISWeights; 51 | std::vector< std::shared_ptr > g_list; 52 | std::vector< std::vector> list_st, list_s_primes; 53 | std::vector list_at; 54 | std::vector list_rt; 55 | std::vector list_term; 56 | }; 57 | 58 | 59 | class Memory{ 60 | public: 61 | Memory(double epsilon,double alpha,double beta,double beta_increment_per_sampling,double abs_err_upper,int capacity); 62 | std::shared_ptr tree; 63 | double epsilon; 64 | double alpha; 65 | double beta; 66 | double beta_increment_per_sampling; 67 | double abs_err_upper; 68 | void Store(std::shared_ptr transition); 69 | void Add(std::shared_ptr env,int n_step); 70 | std::shared_ptr Sampling(int n); 71 | void batch_update(std::vector tree_idx, std::vector abs_errors); 72 | }; 73 | 74 | #endif -------------------------------------------------------------------------------- /code/FINDER_CN_cost/src/lib/nstep_replay_mem_prioritized.h: -------------------------------------------------------------------------------- 1 | #ifndef NSTEP_REPLAY_MEM_PRIORITIZED_H 2 | #define NSTEP_REPLAY_MEM_PRIORITIZED_H 3 | 4 | #include 5 | #include 6 | #include "graph.h" 7 | #include "mvc_env.h" 8 | 9 | 10 | class Data{ 11 | public: 12 | Data(); 13 | std::shared_ptr g; 14 | std::vector s_t; 15 | std::vector s_prime; 16 | int a_t; 17 | double r_t; 18 | bool term_t; 19 | }; 20 | 21 | class LeafResult{ 22 | public: 23 | LeafResult(); 24 | int leaf_idx; 25 | double p; 26 | std::shared_ptr data; 27 | }; 28 | 29 | class SumTree 30 | { 31 | public: 32 | SumTree(int capacity); 33 | int capacity; 34 | int data_pointer; 35 | //bool isOverWrite; 36 | double minElement; 37 | double maxElement; 38 | std::vector tree; 39 | std::vector> data; 40 | void Add(double p, std::shared_ptr data); 41 | void Update(int tree_idx,double p); 42 | std::shared_ptr Get_leaf(double v); 43 | }; 44 | 45 | 46 | class ReplaySample{ 47 | public: 48 | ReplaySample(int batch_size); 49 | std::vector b_idx; 50 | std::vector ISWeights; 51 | std::vector< std::shared_ptr > g_list; 52 | std::vector< std::vector> list_st, list_s_primes; 53 | std::vector list_at; 54 | std::vector list_rt; 55 | std::vector list_term; 56 | }; 57 | 58 | 59 | class Memory{ 60 | public: 61 | Memory(double epsilon,double alpha,double beta,double beta_increment_per_sampling,double abs_err_upper,int capacity); 62 | std::shared_ptr tree; 63 | double epsilon; 64 | double alpha; 65 | double beta; 66 | double beta_increment_per_sampling; 67 | double abs_err_upper; 68 | void Store(std::shared_ptr transition); 69 | void Add(std::shared_ptr env,int n_step); 70 | std::shared_ptr Sampling(int n); 71 | void batch_update(std::vector tree_idx, std::vector abs_errors); 72 | }; 73 | 74 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND_cost/src/lib/nstep_replay_mem_prioritized.h: -------------------------------------------------------------------------------- 1 | #ifndef NSTEP_REPLAY_MEM_PRIORITIZED_H 2 | #define NSTEP_REPLAY_MEM_PRIORITIZED_H 3 | 4 | #include 5 | #include 6 | #include "graph.h" 7 | #include "mvc_env.h" 8 | 9 | 10 | class Data{ 11 | public: 12 | Data(); 13 | std::shared_ptr g; 14 | std::vector s_t; 15 | std::vector s_prime; 16 | int a_t; 17 | double r_t; 18 | bool term_t; 19 | }; 20 | 21 | class LeafResult{ 22 | public: 23 | LeafResult(); 24 | int leaf_idx; 25 | double p; 26 | std::shared_ptr data; 27 | }; 28 | 29 | class SumTree 30 | { 31 | public: 32 | SumTree(int capacity); 33 | int capacity; 34 | int data_pointer; 35 | //bool isOverWrite; 36 | double minElement; 37 | double maxElement; 38 | std::vector tree; 39 | std::vector> data; 40 | void Add(double p, std::shared_ptr data); 41 | void Update(int tree_idx,double p); 42 | std::shared_ptr Get_leaf(double v); 43 | }; 44 | 45 | 46 | class ReplaySample{ 47 | public: 48 | ReplaySample(int batch_size); 49 | std::vector b_idx; 50 | std::vector ISWeights; 51 | std::vector< std::shared_ptr > g_list; 52 | std::vector< std::vector> list_st, list_s_primes; 53 | std::vector list_at; 54 | std::vector list_rt; 55 | std::vector list_term; 56 | }; 57 | 58 | 59 | class Memory{ 60 | public: 61 | Memory(double epsilon,double alpha,double beta,double beta_increment_per_sampling,double abs_err_upper,int capacity); 62 | std::shared_ptr tree; 63 | double epsilon; 64 | double alpha; 65 | double beta; 66 | double beta_increment_per_sampling; 67 | double abs_err_upper; 68 | void Store(std::shared_ptr transition); 69 | void Add(std::shared_ptr env,int n_step); 70 | std::shared_ptr Sampling(int n); 71 | void batch_update(std::vector tree_idx, std::vector abs_errors); 72 | }; 73 | 74 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND/src/lib/disjoint_set.cpp: -------------------------------------------------------------------------------- 1 | #include "graph.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "disjoint_set.h" 7 | //#include "stdio.h" 8 | 9 | Disjoint_Set::Disjoint_Set(int graphSize) 10 | { 11 | unionSet.resize(graphSize); 12 | rankCount.resize(graphSize); 13 | for (int i = 0; i < unionSet.size(); i++) 14 | { 15 | unionSet[i] = i; 16 | rankCount[i] = 1; 17 | } 18 | maxRankCount = 1; 19 | } 20 | 21 | Disjoint_Set::~Disjoint_Set() 22 | { 23 | unionSet.clear(); 24 | rankCount.clear(); 25 | maxRankCount = 1; 26 | } 27 | 28 | int Disjoint_Set::findRoot(int node) 29 | { 30 | if (node != unionSet[node]) 31 | { 32 | int rootNode = findRoot(unionSet[node]); 33 | unionSet[node] = rootNode; 34 | return rootNode; 35 | } 36 | else 37 | { 38 | return node; 39 | } 40 | } 41 | 42 | void Disjoint_Set::merge(int node1, int node2) 43 | { 44 | int node1Root = findRoot(node1); 45 | int node2Root = findRoot(node2); 46 | if (node1Root != node2Root) 47 | { 48 | if (rankCount[node2Root] > rankCount[node1Root]) 49 | { 50 | unionSet[node1Root] = node2Root; 51 | rankCount[node2Root] += rankCount[node1Root]; 52 | 53 | if (rankCount[node2Root] > maxRankCount) 54 | { 55 | maxRankCount = rankCount[node2Root]; 56 | } 57 | } 58 | else 59 | { 60 | unionSet[node2Root] = node1Root; 61 | rankCount[node1Root] += rankCount[node2Root]; 62 | 63 | if (rankCount[node1Root] > maxRankCount) 64 | { 65 | maxRankCount = rankCount[node1Root]; 66 | } 67 | 68 | } 69 | } 70 | } 71 | 72 | double Disjoint_Set::getBiggestComponentCurrentRatio() const 73 | { 74 | return double(maxRankCount) / double(rankCount.size()); 75 | } 76 | 77 | int Disjoint_Set::getRank(int rootNode) const 78 | { 79 | return rankCount[rootNode]; 80 | } 81 | 82 | -------------------------------------------------------------------------------- /code/FINDER_ND_cost/src/lib/disjoint_set.cpp: -------------------------------------------------------------------------------- 1 | #include "graph.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "disjoint_set.h" 7 | //#include "stdio.h" 8 | 9 | Disjoint_Set::Disjoint_Set(int graphSize) 10 | { 11 | unionSet.resize(graphSize); 12 | rankCount.resize(graphSize); 13 | for (int i = 0; i < (int)unionSet.size(); i++) 14 | { 15 | unionSet[i] = i; 16 | rankCount[i] = 1; 17 | } 18 | maxRankCount = 1; 19 | } 20 | 21 | Disjoint_Set::~Disjoint_Set() 22 | { 23 | unionSet.clear(); 24 | rankCount.clear(); 25 | maxRankCount = 1; 26 | } 27 | 28 | int Disjoint_Set::findRoot(int node) 29 | { 30 | if (node != unionSet[node]) 31 | { 32 | int rootNode = findRoot(unionSet[node]); 33 | unionSet[node] = rootNode; 34 | return rootNode; 35 | } 36 | else 37 | { 38 | return node; 39 | } 40 | } 41 | 42 | void Disjoint_Set::merge(int node1, int node2) 43 | { 44 | int node1Root = findRoot(node1); 45 | int node2Root = findRoot(node2); 46 | if (node1Root != node2Root) 47 | { 48 | if (rankCount[node2Root] > rankCount[node1Root]) 49 | { 50 | unionSet[node1Root] = node2Root; 51 | rankCount[node2Root] += rankCount[node1Root]; 52 | 53 | if (rankCount[node2Root] > maxRankCount) 54 | { 55 | maxRankCount = rankCount[node2Root]; 56 | } 57 | } 58 | else 59 | { 60 | unionSet[node2Root] = node1Root; 61 | rankCount[node1Root] += rankCount[node2Root]; 62 | 63 | if (rankCount[node1Root] > maxRankCount) 64 | { 65 | maxRankCount = rankCount[node1Root]; 66 | } 67 | 68 | } 69 | } 70 | } 71 | 72 | double Disjoint_Set::getBiggestComponentCurrentRatio() const 73 | { 74 | return double(maxRankCount) / double(rankCount.size()); 75 | } 76 | 77 | int Disjoint_Set::getRank(int rootNode) const 78 | { 79 | return rankCount[rootNode]; 80 | } 81 | 82 | -------------------------------------------------------------------------------- /code/FINDER_ND_cost/real2weight.py: -------------------------------------------------------------------------------- 1 | import networkx as nx 2 | import numpy as np 3 | import random 4 | 5 | 6 | 7 | data_path = 'E:/Ubuntu_home/Code/My Code/GRAPHDQN/Server version/Data&Solution/Real_Data/Data/' 8 | data_name = ['maayan-figeys','maayan-Stelzl','maayan-vidal','maayan-yeast'] 9 | save_dir = 'E:/Ubuntu_home/Code/My Code/GRAPHDQN/Server version/Data&Solution/Real_Data/Data_Cost' 10 | 11 | cost_type = 'degree_noise' 12 | 13 | for i in range(len(data_name)): 14 | data = data_path + data_name[i] + '.txt' 15 | g = nx.read_edgelist(data) 16 | 17 | ### degree weight 18 | if cost_type == 'degree': 19 | degree = nx.degree(g) 20 | maxDegree = max(dict(degree).values()) 21 | weights = {} 22 | for node in g.nodes(): 23 | weights[node] = degree[node] / maxDegree 24 | 25 | elif cost_type == 'degree_noise': 26 | degree = nx.degree(g) 27 | median_val = np.median(list(dict(degree).values())) 28 | weights = {} 29 | for node in g.nodes(): 30 | episilon = 0.5 * median_val * np.random.normal(0, 1) 31 | weights[node] = 0.5 * degree[node] + episilon 32 | if weights[node] < 0.0: 33 | weights[node] = - weights[node] 34 | max_weight = np.max(list(weights.values())) 35 | for node in g.nodes(): 36 | weights[node] = weights[node] / max_weight 37 | 38 | elif cost_type == 'random': 39 | # ### random weight 40 | weights = {} 41 | for node in g.nodes(): 42 | weights[node] = random.uniform(0, 1) 43 | 44 | nx.set_node_attributes(g, weights, 'weight') 45 | save_dir_g = '%s/%s_%s.gml' % (save_dir, data_name[i], cost_type) 46 | nx.write_gml(g, save_dir_g) 47 | 48 | 49 | f_weight = open('%s/%s_%s_weight.txt'%(save_dir, data_name[i], cost_type), 'w') 50 | for j in range(nx.number_of_nodes(g)): 51 | f_weight.write('%.8f\n'%weights[str(j)]) 52 | f_weight.flush() 53 | f_weight.close() 54 | 55 | print ('Data %s is finished!'%data_name[i]) 56 | -------------------------------------------------------------------------------- /code/FINDER_CN_cost/pkl2gml.py: -------------------------------------------------------------------------------- 1 | import pickle as cp 2 | import networkx as nx 3 | import os 4 | from tqdm import tqdm 5 | import random 6 | 7 | data_test_path = 'E:/Ubuntu_home/Code/GRAPHDQN/Server version/Data&Solution/Synthesized_Data/gtype-barabasi_albert-nrange-' 8 | data_test_name = ['15-20','30-50','50-100','100-200','200-300','300-400','400-500','500-600','1000-1200'] 9 | data_test_suffix = '-n_graph-1000-p-0.00-m-4.pkl' 10 | 11 | save_dir_1 = 'E:/Ubuntu_home/Code/GRAPHDQN/Server version/Data&Solution/Synthesized_Data/gml_randomCost/' 12 | n_test = 100 13 | for i in range(len(data_test_name)): 14 | print ('\ndata_test%d'%i) 15 | data_test = data_test_path + data_test_name[i] + data_test_suffix 16 | save_dir_2 = data_test_name[i] 17 | save_dir = save_dir_1 + save_dir_2 18 | if not os.path.exists(save_dir): #make dir 19 | os.mkdir(save_dir) 20 | f = open(data_test, 'rb') 21 | for j in tqdm(range(n_test)): 22 | g = cp.load(f) 23 | 24 | ### random weight 25 | weights = {} 26 | for node in g.nodes(): 27 | weights[node] = random.uniform(0, 1) 28 | 29 | # ### degree weight 30 | # degree = nx.degree(g) 31 | # maxDegree = max(degree.values()) 32 | # weights = {} 33 | # for node in g.nodes(): 34 | # weights[node] = degree[node] / maxDegree 35 | 36 | nx.set_node_attributes(g, 'weight', weights) 37 | save_dir_g = '%s/g_%d'%(save_dir,j) 38 | nx.write_gml(g, save_dir_g) 39 | 40 | 41 | # save_dir_11 = 'E:/Ubuntu_home/Code/GRAPHDQN/Server version/Data&Solution/Synthesized_Data/gml_randomCost/' 42 | # n_test = 100 43 | # for i in range(len(data_test_name)): 44 | # print ('data_test%d'%i) 45 | # data_test = data_test_path + data_test_name[i] + data_test_suffix 46 | # save_dir_22 = data_test_name[i] 47 | # save_dir = save_dir_11 + save_dir_22 48 | # if not os.path.exists(save_dir): #make dir 49 | # os.mkdir(save_dir) 50 | # f = open(data_test, 'rb') 51 | # for j in tqdm(range(n_test)): 52 | # g = cp.load(f) 53 | # save_dir_g = '%s/g_%d'%(save_dir,j) 54 | # nx.write_gml(g, save_dir_g) -------------------------------------------------------------------------------- /code/FINDER_ND/src/lib/graph_struct.cpp: -------------------------------------------------------------------------------- 1 | #include "graph_struct.h" 2 | 3 | template 4 | LinkedTable::LinkedTable() 5 | { 6 | n = ncap = 0; 7 | head.clear(); 8 | } 9 | 10 | template 11 | LinkedTable::~LinkedTable() 12 | { 13 | n = ncap = 0; 14 | head.clear(); 15 | } 16 | 17 | template 18 | void LinkedTable::AddEntry(int head_id, T content) 19 | { 20 | if (head_id >= n) 21 | { 22 | if (head_id + 1 > ncap) 23 | { 24 | ncap = std::max(ncap * 2, head_id + 1); 25 | head.resize(ncap); 26 | for (int i = n; i < head_id + 1; ++i) 27 | head[i].clear(); 28 | } 29 | n = head_id + 1; 30 | } 31 | 32 | head[head_id].push_back(content); 33 | } 34 | 35 | template 36 | void LinkedTable::Resize(int new_n) 37 | { 38 | if (new_n > ncap) 39 | { 40 | ncap = std::max(ncap * 2, new_n); 41 | head.resize(ncap); 42 | } 43 | n = new_n; 44 | for (int i = 0; i < (int)head.size(); ++i) 45 | head[i].clear(); 46 | } 47 | 48 | template class LinkedTable; 49 | template class LinkedTable< std::pair >; 50 | 51 | GraphStruct::GraphStruct() 52 | { 53 | out_edges = new LinkedTable< std::pair >(); 54 | in_edges = new LinkedTable< std::pair >(); 55 | subgraph = new LinkedTable< int >(); 56 | edge_list.clear(); 57 | } 58 | 59 | GraphStruct::~GraphStruct() 60 | { 61 | delete out_edges; 62 | delete in_edges; 63 | delete subgraph; 64 | } 65 | 66 | void GraphStruct::AddEdge(int idx, int x, int y) 67 | { 68 | out_edges->AddEntry(x, std::pair(idx, y)); 69 | in_edges->AddEntry(y, std::pair(idx, x)); 70 | num_edges++; 71 | edge_list.push_back(std::make_pair(x, y)); 72 | assert(num_edges == edge_list.size()); 73 | assert(num_edges - 1 == (unsigned)idx); 74 | } 75 | 76 | void GraphStruct::AddNode(int subg_id, int n_idx) 77 | { 78 | subgraph->AddEntry(subg_id, n_idx); 79 | } 80 | 81 | void GraphStruct::Resize(unsigned _num_subgraph, unsigned _num_nodes) 82 | { 83 | num_nodes = _num_nodes; 84 | num_edges = 0; 85 | edge_list.clear(); 86 | num_subgraph = _num_subgraph; 87 | 88 | in_edges->Resize(num_nodes); 89 | out_edges->Resize(num_nodes); 90 | subgraph->Resize(num_subgraph); 91 | } 92 | -------------------------------------------------------------------------------- /code/FINDER_CN/src/lib/graph_struct.cpp: -------------------------------------------------------------------------------- 1 | #include "graph_struct.h" 2 | 3 | template 4 | LinkedTable::LinkedTable() 5 | { 6 | n = ncap = 0; 7 | head.clear(); 8 | } 9 | 10 | template 11 | LinkedTable::~LinkedTable() 12 | { 13 | n = ncap = 0; 14 | head.clear(); 15 | } 16 | 17 | template 18 | void LinkedTable::AddEntry(int head_id, T content) 19 | { 20 | if (head_id >= n) 21 | { 22 | if (head_id + 1 > ncap) 23 | { 24 | ncap = std::max(ncap * 2, head_id + 1); 25 | head.resize(ncap); 26 | for (int i = n; i < head_id + 1; ++i) 27 | head[i].clear(); 28 | } 29 | n = head_id + 1; 30 | } 31 | 32 | head[head_id].push_back(content); 33 | } 34 | 35 | template 36 | void LinkedTable::Resize(int new_n) 37 | { 38 | if (new_n > ncap) 39 | { 40 | ncap = std::max(ncap * 2, new_n); 41 | head.resize(ncap); 42 | } 43 | n = new_n; 44 | for (size_t i = 0; i < (int)head.size(); ++i) 45 | head[i].clear(); 46 | } 47 | 48 | template class LinkedTable; 49 | template class LinkedTable< std::pair >; 50 | 51 | GraphStruct::GraphStruct() 52 | { 53 | out_edges = new LinkedTable< std::pair >(); 54 | in_edges = new LinkedTable< std::pair >(); 55 | subgraph = new LinkedTable< int >(); 56 | edge_list.clear(); 57 | } 58 | 59 | GraphStruct::~GraphStruct() 60 | { 61 | delete out_edges; 62 | delete in_edges; 63 | delete subgraph; 64 | } 65 | 66 | void GraphStruct::AddEdge(int idx, int x, int y) 67 | { 68 | out_edges->AddEntry(x, std::pair(idx, y)); 69 | in_edges->AddEntry(y, std::pair(idx, x)); 70 | num_edges++; 71 | edge_list.push_back(std::make_pair(x, y)); 72 | assert(num_edges == edge_list.size()); 73 | assert(num_edges - 1 == (unsigned)idx); 74 | } 75 | 76 | void GraphStruct::AddNode(int subg_id, int n_idx) 77 | { 78 | subgraph->AddEntry(subg_id, n_idx); 79 | } 80 | 81 | void GraphStruct::Resize(unsigned _num_subgraph, unsigned _num_nodes) 82 | { 83 | num_nodes = _num_nodes; 84 | num_edges = 0; 85 | edge_list.clear(); 86 | num_subgraph = _num_subgraph; 87 | 88 | in_edges->Resize(num_nodes); 89 | out_edges->Resize(num_nodes); 90 | subgraph->Resize(num_subgraph); 91 | } 92 | -------------------------------------------------------------------------------- /code/FINDER_CN_cost/src/lib/graph_struct.cpp: -------------------------------------------------------------------------------- 1 | #include "graph_struct.h" 2 | 3 | template 4 | LinkedTable::LinkedTable() 5 | { 6 | n = ncap = 0; 7 | head.clear(); 8 | } 9 | 10 | template 11 | LinkedTable::~LinkedTable() 12 | { 13 | n = ncap = 0; 14 | head.clear(); 15 | } 16 | 17 | template 18 | void LinkedTable::AddEntry(int head_id, T content) 19 | { 20 | if (head_id >= n) 21 | { 22 | if (head_id + 1 > ncap) 23 | { 24 | ncap = std::max(ncap * 2, head_id + 1); 25 | head.resize(ncap); 26 | for (int i = n; i < head_id + 1; ++i) 27 | head[i].clear(); 28 | } 29 | n = head_id + 1; 30 | } 31 | 32 | head[head_id].push_back(content); 33 | } 34 | 35 | template 36 | void LinkedTable::Resize(int new_n) 37 | { 38 | if (new_n > ncap) 39 | { 40 | ncap = std::max(ncap * 2, new_n); 41 | head.resize(ncap); 42 | } 43 | n = new_n; 44 | for (size_t i = 0; i < (int)head.size(); ++i) 45 | head[i].clear(); 46 | } 47 | 48 | template class LinkedTable; 49 | template class LinkedTable< std::pair >; 50 | 51 | GraphStruct::GraphStruct() 52 | { 53 | out_edges = new LinkedTable< std::pair >(); 54 | in_edges = new LinkedTable< std::pair >(); 55 | subgraph = new LinkedTable< int >(); 56 | edge_list.clear(); 57 | } 58 | 59 | GraphStruct::~GraphStruct() 60 | { 61 | delete out_edges; 62 | delete in_edges; 63 | delete subgraph; 64 | } 65 | 66 | void GraphStruct::AddEdge(int idx, int x, int y) 67 | { 68 | out_edges->AddEntry(x, std::pair(idx, y)); 69 | in_edges->AddEntry(y, std::pair(idx, x)); 70 | num_edges++; 71 | edge_list.push_back(std::make_pair(x, y)); 72 | assert(num_edges == edge_list.size()); 73 | assert(num_edges - 1 == (unsigned)idx); 74 | } 75 | 76 | void GraphStruct::AddNode(int subg_id, int n_idx) 77 | { 78 | subgraph->AddEntry(subg_id, n_idx); 79 | } 80 | 81 | void GraphStruct::Resize(unsigned _num_subgraph, unsigned _num_nodes) 82 | { 83 | num_nodes = _num_nodes; 84 | num_edges = 0; 85 | edge_list.clear(); 86 | num_subgraph = _num_subgraph; 87 | 88 | in_edges->Resize(num_nodes); 89 | out_edges->Resize(num_nodes); 90 | subgraph->Resize(num_subgraph); 91 | } 92 | -------------------------------------------------------------------------------- /code/FINDER_ND_cost/src/lib/graph_struct.cpp: -------------------------------------------------------------------------------- 1 | #include "graph_struct.h" 2 | 3 | template 4 | LinkedTable::LinkedTable() 5 | { 6 | n = ncap = 0; 7 | head.clear(); 8 | } 9 | 10 | template 11 | LinkedTable::~LinkedTable() 12 | { 13 | n = ncap = 0; 14 | head.clear(); 15 | } 16 | 17 | template 18 | void LinkedTable::AddEntry(int head_id, T content) 19 | { 20 | if (head_id >= n) 21 | { 22 | if (head_id + 1 > ncap) 23 | { 24 | ncap = std::max(ncap * 2, head_id + 1); 25 | head.resize(ncap); 26 | for (int i = n; i < head_id + 1; ++i) 27 | head[i].clear(); 28 | } 29 | n = head_id + 1; 30 | } 31 | 32 | head[head_id].push_back(content); 33 | } 34 | 35 | template 36 | void LinkedTable::Resize(int new_n) 37 | { 38 | if (new_n > ncap) 39 | { 40 | ncap = std::max(ncap * 2, new_n); 41 | head.resize(ncap); 42 | } 43 | n = new_n; 44 | for (size_t i = 0; i < (int)head.size(); ++i) 45 | head[i].clear(); 46 | } 47 | 48 | template class LinkedTable; 49 | template class LinkedTable< std::pair >; 50 | 51 | GraphStruct::GraphStruct() 52 | { 53 | out_edges = new LinkedTable< std::pair >(); 54 | in_edges = new LinkedTable< std::pair >(); 55 | subgraph = new LinkedTable< int >(); 56 | edge_list.clear(); 57 | } 58 | 59 | GraphStruct::~GraphStruct() 60 | { 61 | delete out_edges; 62 | delete in_edges; 63 | delete subgraph; 64 | } 65 | 66 | void GraphStruct::AddEdge(int idx, int x, int y) 67 | { 68 | out_edges->AddEntry(x, std::pair(idx, y)); 69 | in_edges->AddEntry(y, std::pair(idx, x)); 70 | num_edges++; 71 | edge_list.push_back(std::make_pair(x, y)); 72 | assert(num_edges == edge_list.size()); 73 | assert(num_edges - 1 == (unsigned)idx); 74 | } 75 | 76 | void GraphStruct::AddNode(int subg_id, int n_idx) 77 | { 78 | subgraph->AddEntry(subg_id, n_idx); 79 | } 80 | 81 | void GraphStruct::Resize(unsigned _num_subgraph, unsigned _num_nodes) 82 | { 83 | num_nodes = _num_nodes; 84 | num_edges = 0; 85 | edge_list.clear(); 86 | num_subgraph = _num_subgraph; 87 | 88 | in_edges->Resize(num_nodes); 89 | out_edges->Resize(num_nodes); 90 | subgraph->Resize(num_subgraph); 91 | } 92 | -------------------------------------------------------------------------------- /code/FINDER_CN/nstep_replay_mem_prioritized.pxd: -------------------------------------------------------------------------------- 1 | from libcpp.vector cimport vector 2 | from libcpp.memory cimport shared_ptr 3 | from libcpp cimport bool 4 | from graph cimport Graph 5 | from mvc_env cimport MvcEnv 6 | 7 | 8 | cdef extern from "./src/lib/nstep_replay_mem_prioritized.h": 9 | cdef cppclass Data: 10 | Data()except+ 11 | shared_ptr[Graph] g 12 | vector[int] s_t 13 | vector[int] s_prime 14 | int a_t 15 | double r_t 16 | bool term_t 17 | 18 | cdef extern from "./src/lib/nstep_replay_mem_prioritized.h": 19 | cdef cppclass LeafResult: 20 | LeafResult()except+ 21 | int leaf_idx 22 | double p 23 | shared_ptr[Data] data 24 | 25 | cdef extern from "./src/lib/nstep_replay_mem_prioritized.h": 26 | cdef cppclass SumTree: 27 | SumTree(int capacity)except+ 28 | int data_pointer 29 | int capacity 30 | vector[double] tree 31 | vector[shared_ptr[Data]] data 32 | void Add(double p, shared_ptr[Data]) 33 | void Update(int tree_idx,double p) 34 | shared_ptr[LeafResult] Get_leaf(double v) 35 | 36 | 37 | cdef extern from "./src/lib/nstep_replay_mem_prioritized.h": 38 | cdef cppclass ReplaySample: 39 | ReplaySample(int batch_size)except+ 40 | vector[int] b_idx 41 | vector[double] ISWeights 42 | vector[shared_ptr[Graph]] g_list 43 | vector[vector[int]] list_st 44 | vector[vector[int]] list_s_primes 45 | vector[int] list_at 46 | vector[double] list_rt 47 | vector[bool] list_term 48 | 49 | 50 | cdef extern from "./src/lib/nstep_replay_mem_prioritized.h": 51 | cdef cppclass Memory: 52 | Memory(double epsilon,double alpha,double beta,double beta_increment_per_sampling,double abs_err_upper,int capacity)except+ 53 | shared_ptr[SumTree] tree 54 | double epsilon 55 | double alpha 56 | double beta 57 | double beta_increment_per_sampling 58 | double abs_err_upper 59 | void Store(shared_ptr[Data] transition)except+ 60 | void Add(shared_ptr[MvcEnv] env,int n_step)except+ 61 | shared_ptr[ReplaySample] Sampling(int n)except+ 62 | void batch_update(vector[int] tree_idx, vector[double] abs_errors)except+ 63 | -------------------------------------------------------------------------------- /code/FINDER_ND/nstep_replay_mem_prioritized.pxd: -------------------------------------------------------------------------------- 1 | from libcpp.vector cimport vector 2 | from libcpp.memory cimport shared_ptr 3 | from libcpp cimport bool 4 | from graph cimport Graph 5 | from mvc_env cimport MvcEnv 6 | 7 | 8 | cdef extern from "./src/lib/nstep_replay_mem_prioritized.h": 9 | cdef cppclass Data: 10 | Data()except+ 11 | shared_ptr[Graph] g 12 | vector[int] s_t 13 | vector[int] s_prime 14 | int a_t 15 | double r_t 16 | bool term_t 17 | 18 | cdef extern from "./src/lib/nstep_replay_mem_prioritized.h": 19 | cdef cppclass LeafResult: 20 | LeafResult()except+ 21 | int leaf_idx 22 | double p 23 | shared_ptr[Data] data 24 | 25 | cdef extern from "./src/lib/nstep_replay_mem_prioritized.h": 26 | cdef cppclass SumTree: 27 | SumTree(int capacity)except+ 28 | int data_pointer 29 | int capacity 30 | vector[double] tree 31 | vector[shared_ptr[Data]] data 32 | void Add(double p, shared_ptr[Data]) 33 | void Update(int tree_idx,double p) 34 | shared_ptr[LeafResult] Get_leaf(double v) 35 | 36 | 37 | cdef extern from "./src/lib/nstep_replay_mem_prioritized.h": 38 | cdef cppclass ReplaySample: 39 | ReplaySample(int batch_size)except+ 40 | vector[int] b_idx 41 | vector[double] ISWeights 42 | vector[shared_ptr[Graph]] g_list 43 | vector[vector[int]] list_st 44 | vector[vector[int]] list_s_primes 45 | vector[int] list_at 46 | vector[double] list_rt 47 | vector[bool] list_term 48 | 49 | 50 | cdef extern from "./src/lib/nstep_replay_mem_prioritized.h": 51 | cdef cppclass Memory: 52 | Memory(double epsilon,double alpha,double beta,double beta_increment_per_sampling,double abs_err_upper,int capacity)except+ 53 | shared_ptr[SumTree] tree 54 | double epsilon 55 | double alpha 56 | double beta 57 | double beta_increment_per_sampling 58 | double abs_err_upper 59 | void Store(shared_ptr[Data] transition)except+ 60 | void Add(shared_ptr[MvcEnv] env,int n_step)except+ 61 | shared_ptr[ReplaySample] Sampling(int n)except+ 62 | void batch_update(vector[int] tree_idx, vector[double] abs_errors)except+ 63 | -------------------------------------------------------------------------------- /code/FINDER_CN_cost/nstep_replay_mem_prioritized.pxd: -------------------------------------------------------------------------------- 1 | from libcpp.vector cimport vector 2 | from libcpp.memory cimport shared_ptr 3 | from libcpp cimport bool 4 | from graph cimport Graph 5 | from mvc_env cimport MvcEnv 6 | 7 | 8 | cdef extern from "./src/lib/nstep_replay_mem_prioritized.h": 9 | cdef cppclass Data: 10 | Data()except+ 11 | shared_ptr[Graph] g 12 | vector[int] s_t 13 | vector[int] s_prime 14 | int a_t 15 | double r_t 16 | bool term_t 17 | 18 | cdef extern from "./src/lib/nstep_replay_mem_prioritized.h": 19 | cdef cppclass LeafResult: 20 | LeafResult()except+ 21 | int leaf_idx 22 | double p 23 | shared_ptr[Data] data 24 | 25 | cdef extern from "./src/lib/nstep_replay_mem_prioritized.h": 26 | cdef cppclass SumTree: 27 | SumTree(int capacity)except+ 28 | int data_pointer 29 | int capacity 30 | vector[double] tree 31 | vector[shared_ptr[Data]] data 32 | void Add(double p, shared_ptr[Data]) 33 | void Update(int tree_idx,double p) 34 | shared_ptr[LeafResult] Get_leaf(double v) 35 | 36 | 37 | cdef extern from "./src/lib/nstep_replay_mem_prioritized.h": 38 | cdef cppclass ReplaySample: 39 | ReplaySample(int batch_size)except+ 40 | vector[int] b_idx 41 | vector[double] ISWeights 42 | vector[shared_ptr[Graph]] g_list 43 | vector[vector[int]] list_st 44 | vector[vector[int]] list_s_primes 45 | vector[int] list_at 46 | vector[double] list_rt 47 | vector[bool] list_term 48 | 49 | 50 | cdef extern from "./src/lib/nstep_replay_mem_prioritized.h": 51 | cdef cppclass Memory: 52 | Memory(double epsilon,double alpha,double beta,double beta_increment_per_sampling,double abs_err_upper,int capacity)except+ 53 | shared_ptr[SumTree] tree 54 | double epsilon 55 | double alpha 56 | double beta 57 | double beta_increment_per_sampling 58 | double abs_err_upper 59 | void Store(shared_ptr[Data] transition)except+ 60 | void Add(shared_ptr[MvcEnv] env,int n_step)except+ 61 | shared_ptr[ReplaySample] Sampling(int n)except+ 62 | void batch_update(vector[int] tree_idx, vector[double] abs_errors)except+ 63 | -------------------------------------------------------------------------------- /code/FINDER_ND_cost/nstep_replay_mem_prioritized.pxd: -------------------------------------------------------------------------------- 1 | from libcpp.vector cimport vector 2 | from libcpp.memory cimport shared_ptr 3 | from libcpp cimport bool 4 | from graph cimport Graph 5 | from mvc_env cimport MvcEnv 6 | 7 | 8 | cdef extern from "./src/lib/nstep_replay_mem_prioritized.h": 9 | cdef cppclass Data: 10 | Data()except+ 11 | shared_ptr[Graph] g 12 | vector[int] s_t 13 | vector[int] s_prime 14 | int a_t 15 | double r_t 16 | bool term_t 17 | 18 | cdef extern from "./src/lib/nstep_replay_mem_prioritized.h": 19 | cdef cppclass LeafResult: 20 | LeafResult()except+ 21 | int leaf_idx 22 | double p 23 | shared_ptr[Data] data 24 | 25 | cdef extern from "./src/lib/nstep_replay_mem_prioritized.h": 26 | cdef cppclass SumTree: 27 | SumTree(int capacity)except+ 28 | int data_pointer 29 | int capacity 30 | vector[double] tree 31 | vector[shared_ptr[Data]] data 32 | void Add(double p, shared_ptr[Data]) 33 | void Update(int tree_idx,double p) 34 | shared_ptr[LeafResult] Get_leaf(double v) 35 | 36 | 37 | cdef extern from "./src/lib/nstep_replay_mem_prioritized.h": 38 | cdef cppclass ReplaySample: 39 | ReplaySample(int batch_size)except+ 40 | vector[int] b_idx 41 | vector[double] ISWeights 42 | vector[shared_ptr[Graph]] g_list 43 | vector[vector[int]] list_st 44 | vector[vector[int]] list_s_primes 45 | vector[int] list_at 46 | vector[double] list_rt 47 | vector[bool] list_term 48 | 49 | 50 | cdef extern from "./src/lib/nstep_replay_mem_prioritized.h": 51 | cdef cppclass Memory: 52 | Memory(double epsilon,double alpha,double beta,double beta_increment_per_sampling,double abs_err_upper,int capacity)except+ 53 | shared_ptr[SumTree] tree 54 | double epsilon 55 | double alpha 56 | double beta 57 | double beta_increment_per_sampling 58 | double abs_err_upper 59 | void Store(shared_ptr[Data] transition)except+ 60 | void Add(shared_ptr[MvcEnv] env,int n_step)except+ 61 | shared_ptr[ReplaySample] Sampling(int n)except+ 62 | void batch_update(vector[int] tree_idx, vector[double] abs_errors)except+ 63 | -------------------------------------------------------------------------------- /code/FINDER_CN/src/lib/disjoint_set.cpp: -------------------------------------------------------------------------------- 1 | #include "graph.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "disjoint_set.h" 7 | //#include "stdio.h" 8 | 9 | Disjoint_Set::Disjoint_Set(int graphSize) 10 | { 11 | unionSet.resize(graphSize); 12 | rankCount.resize(graphSize); 13 | for (int i = 0; i < (int)unionSet.size(); i++) 14 | { 15 | unionSet[i] = i; 16 | rankCount[i] = 1; 17 | } 18 | maxRankCount = 1; 19 | CCDScore = 0.0; 20 | } 21 | 22 | Disjoint_Set::~Disjoint_Set() 23 | { 24 | unionSet.clear(); 25 | rankCount.clear(); 26 | maxRankCount = 1; 27 | CCDScore = 0.0; 28 | } 29 | 30 | int Disjoint_Set::findRoot(int node) 31 | { 32 | if (node != unionSet[node]) 33 | { 34 | int rootNode = findRoot(unionSet[node]); 35 | unionSet[node] = rootNode; 36 | return rootNode; 37 | } 38 | else 39 | { 40 | return node; 41 | } 42 | } 43 | 44 | void Disjoint_Set::merge(int node1, int node2) 45 | { 46 | int node1Root = findRoot(node1); 47 | int node2Root = findRoot(node2); 48 | if (node1Root != node2Root) 49 | { 50 | double node1Rank = (double)rankCount[node1Root]; 51 | double node2Rank = (double)rankCount[node2Root]; 52 | CCDScore = CCDScore - node1Rank*(node1Rank-1)/2.0 - node2Rank*(node2Rank-1)/2.0; 53 | CCDScore = CCDScore + (node1Rank+node2Rank)*(node1Rank+node2Rank-1)/2.0; 54 | 55 | if (rankCount[node2Root] > rankCount[node1Root]) 56 | { 57 | unionSet[node1Root] = node2Root; 58 | rankCount[node2Root] += rankCount[node1Root]; 59 | 60 | if (rankCount[node2Root] > maxRankCount) 61 | { 62 | maxRankCount = rankCount[node2Root]; 63 | } 64 | } 65 | else 66 | { 67 | unionSet[node2Root] = node1Root; 68 | rankCount[node1Root] += rankCount[node2Root]; 69 | 70 | if (rankCount[node1Root] > maxRankCount) 71 | { 72 | maxRankCount = rankCount[node1Root]; 73 | } 74 | 75 | } 76 | } 77 | } 78 | 79 | 80 | double Disjoint_Set::getBiggestComponentCurrentRatio() const 81 | { 82 | return double(maxRankCount) / double(rankCount.size()); 83 | } 84 | 85 | 86 | int Disjoint_Set::getRank(int rootNode) const 87 | { 88 | return rankCount[rootNode]; 89 | } 90 | 91 | -------------------------------------------------------------------------------- /code/FINDER_CN_cost/src/lib/disjoint_set.cpp: -------------------------------------------------------------------------------- 1 | #include "graph.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "disjoint_set.h" 7 | //#include "stdio.h" 8 | 9 | Disjoint_Set::Disjoint_Set(int graphSize) 10 | { 11 | unionSet.resize(graphSize); 12 | rankCount.resize(graphSize); 13 | for (int i = 0; i < (int)unionSet.size(); i++) 14 | { 15 | unionSet[i] = i; 16 | rankCount[i] = 1; 17 | } 18 | maxRankCount = 1; 19 | CCDScore = 0.0; 20 | } 21 | 22 | Disjoint_Set::~Disjoint_Set() 23 | { 24 | unionSet.clear(); 25 | rankCount.clear(); 26 | maxRankCount = 1; 27 | CCDScore = 0.0; 28 | } 29 | 30 | int Disjoint_Set::findRoot(int node) 31 | { 32 | if (node != unionSet[node]) 33 | { 34 | int rootNode = findRoot(unionSet[node]); 35 | unionSet[node] = rootNode; 36 | return rootNode; 37 | } 38 | else 39 | { 40 | return node; 41 | } 42 | } 43 | 44 | void Disjoint_Set::merge(int node1, int node2) 45 | { 46 | int node1Root = findRoot(node1); 47 | int node2Root = findRoot(node2); 48 | if (node1Root != node2Root) 49 | { 50 | double node1Rank = (double)rankCount[node1Root]; 51 | double node2Rank = (double)rankCount[node2Root]; 52 | CCDScore = CCDScore - node1Rank*(node1Rank-1)/2.0 - node2Rank*(node2Rank-1)/2.0; 53 | CCDScore = CCDScore + (node1Rank+node2Rank)*(node1Rank+node2Rank-1)/2.0; 54 | 55 | if (rankCount[node2Root] > rankCount[node1Root]) 56 | { 57 | unionSet[node1Root] = node2Root; 58 | rankCount[node2Root] += rankCount[node1Root]; 59 | 60 | if (rankCount[node2Root] > maxRankCount) 61 | { 62 | maxRankCount = rankCount[node2Root]; 63 | } 64 | } 65 | else 66 | { 67 | unionSet[node2Root] = node1Root; 68 | rankCount[node1Root] += rankCount[node2Root]; 69 | 70 | if (rankCount[node1Root] > maxRankCount) 71 | { 72 | maxRankCount = rankCount[node1Root]; 73 | } 74 | 75 | } 76 | } 77 | } 78 | 79 | 80 | double Disjoint_Set::getBiggestComponentCurrentRatio() const 81 | { 82 | return double(maxRankCount) / double(rankCount.size()); 83 | } 84 | 85 | 86 | int Disjoint_Set::getRank(int rootNode) const 87 | { 88 | return rankCount[rootNode]; 89 | } 90 | 91 | -------------------------------------------------------------------------------- /code/FINDER_CN/src/lib/PrepareBatchGraph.h: -------------------------------------------------------------------------------- 1 | #ifndef PREPAREBATCHGRAPH_H_ 2 | #define PREPAREBATCHGRAPH_H_ 3 | 4 | #include "graph.h" 5 | #include "graph_struct.h" 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | class sparseMatrix 14 | { 15 | public: 16 | sparseMatrix(); 17 | ~sparseMatrix(); 18 | std::vector rowIndex; 19 | std::vector colIndex; 20 | std::vector value; 21 | int rowNum; 22 | int colNum; 23 | }; 24 | 25 | class PrepareBatchGraph{ 26 | public: 27 | PrepareBatchGraph(int aggregatorID); 28 | ~PrepareBatchGraph(); 29 | void SetupGraphInput(std::vector idxes, 30 | std::vector< std::shared_ptr > g_list, 31 | std::vector< std::vector > covered, 32 | const int* actions); 33 | void SetupTrain(std::vector idxes, 34 | std::vector< std::shared_ptr > g_list, 35 | std::vector< std::vector > covered, 36 | const int* actions); 37 | void SetupPredAll(std::vector idxes, 38 | std::vector< std::shared_ptr > g_list, 39 | std::vector< std::vector > covered); 40 | int GetStatusInfo(std::shared_ptr g, int num, const int* covered, int& counter,int& twohop_number,int& threehop_number, std::vector& idx_map); 41 | std::shared_ptr act_select; 42 | std::shared_ptr rep_global; 43 | std::shared_ptr n2nsum_param; 44 | std::shared_ptr laplacian_param; 45 | std::shared_ptr subgsum_param; 46 | std::vector< std::vector > idx_map_list; 47 | std::vector> subgraph_id_span; 48 | std::vector< std::vector > aux_feat; 49 | GraphStruct graph; 50 | std::vector avail_act_cnt; 51 | int aggregatorID; 52 | }; 53 | 54 | 55 | 56 | std::vector> n2n_construct(GraphStruct* graph, int aggregatorID); 57 | 58 | std::shared_ptr e2n_construct(GraphStruct* graph); 59 | 60 | std::shared_ptr n2e_construct(GraphStruct* graph); 61 | 62 | std::shared_ptr e2e_construct(GraphStruct* graph); 63 | 64 | std::shared_ptr subg_construct(GraphStruct* graph, std::vector>& subgraph_id_span); 65 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND/src/lib/PrepareBatchGraph.h: -------------------------------------------------------------------------------- 1 | #ifndef PREPAREBATCHGRAPH_H_ 2 | #define PREPAREBATCHGRAPH_H_ 3 | 4 | #include "graph.h" 5 | #include "graph_struct.h" 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | class sparseMatrix 14 | { 15 | public: 16 | sparseMatrix(); 17 | ~sparseMatrix(); 18 | std::vector rowIndex; 19 | std::vector colIndex; 20 | std::vector value; 21 | int rowNum; 22 | int colNum; 23 | }; 24 | 25 | class PrepareBatchGraph{ 26 | public: 27 | PrepareBatchGraph(int aggregatorID); 28 | ~PrepareBatchGraph(); 29 | void SetupGraphInput(std::vector idxes, 30 | std::vector< std::shared_ptr > g_list, 31 | std::vector< std::vector > covered, 32 | const int* actions); 33 | void SetupTrain(std::vector idxes, 34 | std::vector< std::shared_ptr > g_list, 35 | std::vector< std::vector > covered, 36 | const int* actions); 37 | void SetupPredAll(std::vector idxes, 38 | std::vector< std::shared_ptr > g_list, 39 | std::vector< std::vector > covered); 40 | int GetStatusInfo(std::shared_ptr g, int num, const int* covered, int& counter,int& twohop_number,int& threehop_number, std::vector& idx_map); 41 | std::shared_ptr act_select; 42 | std::shared_ptr rep_global; 43 | std::shared_ptr n2nsum_param; 44 | std::shared_ptr laplacian_param; 45 | std::shared_ptr subgsum_param; 46 | std::vector< std::vector > idx_map_list; 47 | std::vector> subgraph_id_span; 48 | std::vector< std::vector > aux_feat; 49 | GraphStruct graph; 50 | std::vector avail_act_cnt; 51 | int aggregatorID; 52 | }; 53 | 54 | 55 | 56 | std::vector> n2n_construct(GraphStruct* graph, int aggregatorID); 57 | 58 | std::shared_ptr e2n_construct(GraphStruct* graph); 59 | 60 | std::shared_ptr n2e_construct(GraphStruct* graph); 61 | 62 | std::shared_ptr e2e_construct(GraphStruct* graph); 63 | 64 | std::shared_ptr subg_construct(GraphStruct* graph, std::vector>& subgraph_id_span); 65 | #endif -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /code/FINDER_CN/utils.pyx: -------------------------------------------------------------------------------- 1 | from cython.operator import dereference as deref 2 | from libcpp.memory cimport shared_ptr 3 | import numpy as np 4 | import graph 5 | from graph cimport Graph 6 | import gc 7 | from libc.stdlib cimport free 8 | 9 | cdef class py_Utils: 10 | cdef shared_ptr[Utils] inner_Utils 11 | cdef shared_ptr[Graph] inner_Graph 12 | def __cinit__(self): 13 | self.inner_Utils = shared_ptr[Utils](new Utils()) 14 | # def __dealloc__(self): 15 | # if self.inner_Utils != NULL: 16 | # self.inner_Utils.reset() 17 | # gc.collect() 18 | 19 | def getRobustness(self,_g,solution): 20 | self.inner_Graph =shared_ptr[Graph](new Graph()) 21 | deref(self.inner_Graph).num_nodes = _g.num_nodes 22 | deref(self.inner_Graph).num_edges = _g.num_edges 23 | deref(self.inner_Graph).edge_list = _g.edge_list 24 | deref(self.inner_Graph).adj_list = _g.adj_list 25 | return deref(self.inner_Utils).getRobustness(self.inner_Graph,solution) 26 | 27 | 28 | def reInsert(self,_g,solution,allVex,int decreaseStrategyID,int reinsertEachStep): 29 | self.inner_Graph =shared_ptr[Graph](new Graph()) 30 | deref(self.inner_Graph).num_nodes = _g.num_nodes 31 | deref(self.inner_Graph).num_edges = _g.num_edges 32 | deref(self.inner_Graph).edge_list = _g.edge_list 33 | deref(self.inner_Graph).adj_list = _g.adj_list 34 | return deref(self.inner_Utils).reInsert(self.inner_Graph,solution,allVex,decreaseStrategyID,reinsertEachStep) 35 | 36 | def getMxWccSz(self, _g): 37 | self.inner_Graph =shared_ptr[Graph](new Graph()) 38 | deref(self.inner_Graph).num_nodes = _g.num_nodes 39 | deref(self.inner_Graph).num_edges = _g.num_edges 40 | deref(self.inner_Graph).edge_list = _g.edge_list 41 | deref(self.inner_Graph).adj_list = _g.adj_list 42 | return deref(self.inner_Utils).getMxWccSz(self.inner_Graph) 43 | 44 | def Betweenness(self,_g): 45 | self.inner_Graph =shared_ptr[Graph](new Graph()) 46 | deref(self.inner_Graph).num_nodes = _g.num_nodes 47 | deref(self.inner_Graph).num_edges = _g.num_edges 48 | deref(self.inner_Graph).edge_list = _g.edge_list 49 | deref(self.inner_Graph).adj_list = _g.adj_list 50 | return deref(self.inner_Utils).Betweenness(self.inner_Graph) 51 | 52 | @property 53 | def MaxWccSzList(self): 54 | return deref(self.inner_Utils).MaxWccSzList 55 | -------------------------------------------------------------------------------- /code/FINDER_ND/utils.pyx: -------------------------------------------------------------------------------- 1 | from cython.operator import dereference as deref 2 | from libcpp.memory cimport shared_ptr 3 | import numpy as np 4 | import graph 5 | from graph cimport Graph 6 | import gc 7 | from libc.stdlib cimport free 8 | 9 | cdef class py_Utils: 10 | cdef shared_ptr[Utils] inner_Utils 11 | cdef shared_ptr[Graph] inner_Graph 12 | def __cinit__(self): 13 | self.inner_Utils = shared_ptr[Utils](new Utils()) 14 | # def __dealloc__(self): 15 | # if self.inner_Utils != NULL: 16 | # self.inner_Utils.reset() 17 | # gc.collect() 18 | 19 | def getRobustness(self,_g,solution): 20 | self.inner_Graph =shared_ptr[Graph](new Graph()) 21 | deref(self.inner_Graph).num_nodes = _g.num_nodes 22 | deref(self.inner_Graph).num_edges = _g.num_edges 23 | deref(self.inner_Graph).edge_list = _g.edge_list 24 | deref(self.inner_Graph).adj_list = _g.adj_list 25 | return deref(self.inner_Utils).getRobustness(self.inner_Graph,solution) 26 | 27 | 28 | def reInsert(self,_g,solution,allVex,int decreaseStrategyID,int reinsertEachStep): 29 | self.inner_Graph =shared_ptr[Graph](new Graph()) 30 | deref(self.inner_Graph).num_nodes = _g.num_nodes 31 | deref(self.inner_Graph).num_edges = _g.num_edges 32 | deref(self.inner_Graph).edge_list = _g.edge_list 33 | deref(self.inner_Graph).adj_list = _g.adj_list 34 | return deref(self.inner_Utils).reInsert(self.inner_Graph,solution,allVex,decreaseStrategyID,reinsertEachStep) 35 | 36 | def getMxWccSz(self, _g): 37 | self.inner_Graph =shared_ptr[Graph](new Graph()) 38 | deref(self.inner_Graph).num_nodes = _g.num_nodes 39 | deref(self.inner_Graph).num_edges = _g.num_edges 40 | deref(self.inner_Graph).edge_list = _g.edge_list 41 | deref(self.inner_Graph).adj_list = _g.adj_list 42 | return deref(self.inner_Utils).getMxWccSz(self.inner_Graph) 43 | 44 | def Betweenness(self,_g): 45 | self.inner_Graph =shared_ptr[Graph](new Graph()) 46 | deref(self.inner_Graph).num_nodes = _g.num_nodes 47 | deref(self.inner_Graph).num_edges = _g.num_edges 48 | deref(self.inner_Graph).edge_list = _g.edge_list 49 | deref(self.inner_Graph).adj_list = _g.adj_list 50 | return deref(self.inner_Utils).Betweenness(self.inner_Graph) 51 | 52 | @property 53 | def MaxWccSzList(self): 54 | return deref(self.inner_Utils).MaxWccSzList 55 | -------------------------------------------------------------------------------- /code/FINDER_CN_cost/src/lib/PrepareBatchGraph.h: -------------------------------------------------------------------------------- 1 | #ifndef PREPAREBATCHGRAPH_H_ 2 | #define PREPAREBATCHGRAPH_H_ 3 | 4 | #include "graph.h" 5 | #include "graph_struct.h" 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | class sparseMatrix 14 | { 15 | public: 16 | sparseMatrix(); 17 | ~sparseMatrix(); 18 | std::vector rowIndex; 19 | std::vector colIndex; 20 | std::vector value; 21 | int rowNum; 22 | int colNum; 23 | }; 24 | 25 | class PrepareBatchGraph{ 26 | public: 27 | PrepareBatchGraph(int aggregatorID); 28 | ~PrepareBatchGraph(); 29 | void SetupGraphInput(std::vector idxes, 30 | std::vector< std::shared_ptr > g_list, 31 | std::vector< std::vector > covered, 32 | const int* actions); 33 | void SetupTrain(std::vector idxes, 34 | std::vector< std::shared_ptr > g_list, 35 | std::vector< std::vector > covered, 36 | const int* actions); 37 | void SetupPredAll(std::vector idxes, 38 | std::vector< std::shared_ptr > g_list, 39 | std::vector< std::vector > covered); 40 | int GetStatusInfo(std::shared_ptr g, int num, const int* covered, int& counter,int& twohop_number,int& threehop_number, std::vector& idx_map); 41 | std::shared_ptr act_select; 42 | std::shared_ptr rep_global; 43 | std::shared_ptr n2nsum_param; 44 | std::shared_ptr laplacian_param; 45 | std::shared_ptr subgsum_param; 46 | std::vector< std::vector > idx_map_list; 47 | std::vector> subgraph_id_span; 48 | std::vector< std::vector > aux_feat; 49 | std::vector< std::vector > node_feat; 50 | GraphStruct graph; 51 | std::vector avail_act_cnt; 52 | int aggregatorID; 53 | }; 54 | 55 | 56 | 57 | std::vector> n2n_construct(GraphStruct* graph, int aggregatorID); 58 | 59 | std::shared_ptr e2n_construct(GraphStruct* graph); 60 | 61 | std::shared_ptr n2e_construct(GraphStruct* graph); 62 | 63 | std::shared_ptr e2e_construct(GraphStruct* graph); 64 | 65 | std::shared_ptr subg_construct(GraphStruct* graph, std::vector>& subgraph_id_span); 66 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND_cost/src/lib/PrepareBatchGraph.h: -------------------------------------------------------------------------------- 1 | #ifndef PREPAREBATCHGRAPH_H_ 2 | #define PREPAREBATCHGRAPH_H_ 3 | 4 | #include "graph.h" 5 | #include "graph_struct.h" 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | class sparseMatrix 14 | { 15 | public: 16 | sparseMatrix(); 17 | ~sparseMatrix(); 18 | std::vector rowIndex; 19 | std::vector colIndex; 20 | std::vector value; 21 | int rowNum; 22 | int colNum; 23 | }; 24 | 25 | class PrepareBatchGraph{ 26 | public: 27 | PrepareBatchGraph(int aggregatorID); 28 | ~PrepareBatchGraph(); 29 | void SetupGraphInput(std::vector idxes, 30 | std::vector< std::shared_ptr > g_list, 31 | std::vector< std::vector > covered, 32 | const int* actions); 33 | void SetupTrain(std::vector idxes, 34 | std::vector< std::shared_ptr > g_list, 35 | std::vector< std::vector > covered, 36 | const int* actions); 37 | void SetupPredAll(std::vector idxes, 38 | std::vector< std::shared_ptr > g_list, 39 | std::vector< std::vector > covered); 40 | int GetStatusInfo(std::shared_ptr g, int num, const int* covered, int& counter,int& twohop_number,int& threehop_number, std::vector& idx_map); 41 | std::shared_ptr act_select; 42 | std::shared_ptr rep_global; 43 | std::shared_ptr n2nsum_param; 44 | std::shared_ptr laplacian_param; 45 | std::shared_ptr subgsum_param; 46 | std::vector< std::vector > idx_map_list; 47 | std::vector> subgraph_id_span; 48 | std::vector< std::vector > aux_feat; 49 | std::vector< std::vector > node_feat; 50 | GraphStruct graph; 51 | std::vector avail_act_cnt; 52 | int aggregatorID; 53 | }; 54 | 55 | 56 | 57 | std::vector> n2n_construct(GraphStruct* graph, int aggregatorID); 58 | 59 | std::shared_ptr e2n_construct(GraphStruct* graph); 60 | 61 | std::shared_ptr n2e_construct(GraphStruct* graph); 62 | 63 | std::shared_ptr e2e_construct(GraphStruct* graph); 64 | 65 | std::shared_ptr subg_construct(GraphStruct* graph, std::vector>& subgraph_id_span); 66 | #endif -------------------------------------------------------------------------------- /code/FINDER_CN/src/lib/graph.cpp: -------------------------------------------------------------------------------- 1 | #include "graph.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | //#include "stdio.h" 7 | 8 | Graph::Graph() : num_nodes(0), num_edges(0) 9 | { 10 | edge_list.clear(); 11 | adj_list.clear(); 12 | } 13 | 14 | Graph::Graph(const int _num_nodes, const int _num_edges, const int* edges_from, const int* edges_to) 15 | : num_nodes(_num_nodes), num_edges(_num_edges) 16 | { 17 | edge_list.resize(num_edges); 18 | adj_list.resize(num_nodes); 19 | for (int i = 0; i < num_nodes; ++i) 20 | adj_list[i].clear(); 21 | 22 | for (int i = 0; i < num_edges; ++i) 23 | { 24 | int x = edges_from[i], y = edges_to[i]; 25 | adj_list[x].push_back(y); 26 | adj_list[y].push_back(x); 27 | edge_list[i] = std::make_pair(edges_from[i], edges_to[i]); 28 | } 29 | } 30 | 31 | Graph::~Graph() 32 | { 33 | edge_list.clear(); 34 | adj_list.clear(); 35 | num_nodes = 0; 36 | num_edges = 0; 37 | } 38 | 39 | double Graph::getTwoRankNeighborsRatio(std::vector covered) 40 | { 41 | std::set tempSet; 42 | for(int i =0;i<(int)covered.size();++i){ 43 | tempSet.insert(covered[i]); 44 | } 45 | double sum = 0; 46 | for(int i =0;i v3; 51 | std::set_intersection(adj_list[i].begin(),adj_list[i].end(),adj_list[j].begin(),adj_list[j].end(),std::inserter(v3,v3.begin())); 52 | if(v3.size()>0){ 53 | sum += 1.0; 54 | } 55 | } 56 | } 57 | } 58 | } 59 | return sum; 60 | } 61 | 62 | GSet::GSet() 63 | { 64 | graph_pool.clear(); 65 | } 66 | 67 | GSet::~GSet() 68 | { 69 | graph_pool.clear(); 70 | } 71 | 72 | void GSet::Clear() 73 | { 74 | graph_pool.clear(); 75 | } 76 | 77 | void GSet::InsertGraph(int gid, std::shared_ptr graph) 78 | { 79 | assert(graph_pool.count(gid) == 0); 80 | 81 | graph_pool[gid] = graph; 82 | } 83 | 84 | std::shared_ptr GSet::Get(int gid) 85 | { 86 | assert(graph_pool.count(gid)); 87 | return graph_pool[gid]; 88 | } 89 | 90 | std::shared_ptr GSet::Sample() 91 | { 92 | // printf("graph_pool_size:%d",graph_pool.size()); 93 | assert(graph_pool.size()); 94 | // printf("graph_pool_size:%d",graph_pool.size()); 95 | int gid = rand() % graph_pool.size(); 96 | assert(graph_pool[gid]); 97 | return graph_pool[gid]; 98 | } 99 | 100 | GSet GSetTrain, GSetTest; -------------------------------------------------------------------------------- /code/FINDER_ND/src/lib/graph.cpp: -------------------------------------------------------------------------------- 1 | #include "graph.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | //#include "stdio.h" 7 | 8 | Graph::Graph() : num_nodes(0), num_edges(0) 9 | { 10 | edge_list.clear(); 11 | adj_list.clear(); 12 | } 13 | 14 | Graph::Graph(const int _num_nodes, const int _num_edges, const int* edges_from, const int* edges_to) 15 | : num_nodes(_num_nodes), num_edges(_num_edges) 16 | { 17 | edge_list.resize(num_edges); 18 | adj_list.resize(num_nodes); 19 | for (int i = 0; i < num_nodes; ++i) 20 | adj_list[i].clear(); 21 | 22 | for (int i = 0; i < num_edges; ++i) 23 | { 24 | int x = edges_from[i], y = edges_to[i]; 25 | adj_list[x].push_back(y); 26 | adj_list[y].push_back(x); 27 | edge_list[i] = std::make_pair(edges_from[i], edges_to[i]); 28 | } 29 | } 30 | 31 | Graph::~Graph() 32 | { 33 | edge_list.clear(); 34 | adj_list.clear(); 35 | num_nodes = 0; 36 | num_edges = 0; 37 | } 38 | 39 | double Graph::getTwoRankNeighborsRatio(std::vector covered) 40 | { 41 | std::set tempSet; 42 | for(int i =0;i<(int)covered.size();++i){ 43 | tempSet.insert(covered[i]); 44 | } 45 | double sum = 0; 46 | for(int i =0;i v3; 51 | std::set_intersection(adj_list[i].begin(),adj_list[i].end(),adj_list[j].begin(),adj_list[j].end(),std::inserter(v3,v3.begin())); 52 | if(v3.size()>0){ 53 | sum += 1.0; 54 | } 55 | } 56 | } 57 | } 58 | } 59 | return sum; 60 | } 61 | 62 | GSet::GSet() 63 | { 64 | graph_pool.clear(); 65 | } 66 | 67 | GSet::~GSet() 68 | { 69 | graph_pool.clear(); 70 | } 71 | 72 | void GSet::Clear() 73 | { 74 | graph_pool.clear(); 75 | } 76 | 77 | void GSet::InsertGraph(int gid, std::shared_ptr graph) 78 | { 79 | assert(graph_pool.count(gid) == 0); 80 | 81 | graph_pool[gid] = graph; 82 | } 83 | 84 | std::shared_ptr GSet::Get(int gid) 85 | { 86 | assert(graph_pool.count(gid)); 87 | return graph_pool[gid]; 88 | } 89 | 90 | std::shared_ptr GSet::Sample() 91 | { 92 | // printf("graph_pool_size:%d",graph_pool.size()); 93 | assert(graph_pool.size()); 94 | // printf("graph_pool_size:%d",graph_pool.size()); 95 | int gid = rand() % graph_pool.size(); 96 | assert(graph_pool[gid]); 97 | return graph_pool[gid]; 98 | } 99 | 100 | GSet GSetTrain, GSetTest; -------------------------------------------------------------------------------- /code/FINDER_CN_cost/src/lib/graph.cpp: -------------------------------------------------------------------------------- 1 | #include "graph.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | //#include "stdio.h" 7 | 8 | Graph::Graph() : num_nodes(0), num_edges(0) 9 | { 10 | edge_list.clear(); 11 | adj_list.clear(); 12 | nodes_weight.clear(); 13 | total_nodes_weight= 0.0; 14 | } 15 | 16 | Graph::Graph(const int _num_nodes, const int _num_edges, const int* edges_from, const int* edges_to,const double* _nodes_weight) 17 | : num_nodes(_num_nodes), num_edges(_num_edges) 18 | { 19 | edge_list.resize(num_edges); 20 | adj_list.resize(num_nodes); 21 | nodes_weight.clear(); 22 | total_nodes_weight = 0.0; 23 | for (int i = 0; i < num_nodes; ++i) 24 | { 25 | adj_list[i].clear(); 26 | nodes_weight.push_back(_nodes_weight[i]); 27 | total_nodes_weight+=_nodes_weight[i]; 28 | } 29 | 30 | for (int i = 0; i < num_edges; ++i) 31 | { 32 | int x = edges_from[i], y = edges_to[i]; 33 | adj_list[x].push_back(y); 34 | adj_list[y].push_back(x); 35 | edge_list[i] = std::make_pair(edges_from[i], edges_to[i]); 36 | } 37 | // for (int i = 0; i < num_nodes; ++i) 38 | // sort(adj_list[i].begin(),adj_list[i].end()); 39 | } 40 | 41 | double Graph::getTwoRankNeighborsRatio(std::vector covered) 42 | { 43 | std::set tempSet; 44 | for(int i =0;i< (int)covered.size();++i){ 45 | tempSet.insert(covered[i]); 46 | } 47 | double sum = 0; 48 | for(int i =0;i v3; 53 | std::set_intersection(adj_list[i].begin(),adj_list[i].end(),adj_list[j].begin(),adj_list[j].end(),std::inserter(v3,v3.begin())); 54 | if(v3.size()>0){ 55 | sum += 1.0; 56 | } 57 | } 58 | } 59 | } 60 | } 61 | return sum; 62 | } 63 | 64 | GSet::GSet() 65 | { 66 | graph_pool.clear(); 67 | } 68 | void GSet::Clear() 69 | { 70 | graph_pool.clear(); 71 | } 72 | 73 | void GSet::InsertGraph(int gid, std::shared_ptr graph) 74 | { 75 | assert(graph_pool.count(gid) == 0); 76 | 77 | graph_pool[gid] = graph; 78 | } 79 | 80 | std::shared_ptr GSet::Get(int gid) 81 | { 82 | assert(graph_pool.count(gid)); 83 | return graph_pool[gid]; 84 | } 85 | 86 | std::shared_ptr GSet::Sample() 87 | { 88 | // printf("graph_pool_size:%d",graph_pool.size()); 89 | assert(graph_pool.size()); 90 | // printf("graph_pool_size:%d",graph_pool.size()); 91 | int gid = rand() % graph_pool.size(); 92 | assert(graph_pool[gid]); 93 | return graph_pool[gid]; 94 | } 95 | 96 | GSet GSetTrain, GSetTest; -------------------------------------------------------------------------------- /code/FINDER_ND_cost/src/lib/graph.cpp: -------------------------------------------------------------------------------- 1 | #include "graph.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | //#include "stdio.h" 7 | 8 | Graph::Graph() : num_nodes(0), num_edges(0) 9 | { 10 | edge_list.clear(); 11 | adj_list.clear(); 12 | nodes_weight.clear(); 13 | total_nodes_weight= 0.0; 14 | } 15 | 16 | Graph::Graph(const int _num_nodes, const int _num_edges, const int* edges_from, const int* edges_to,const double* _nodes_weight) 17 | : num_nodes(_num_nodes), num_edges(_num_edges) 18 | { 19 | edge_list.resize(num_edges); 20 | adj_list.resize(num_nodes); 21 | nodes_weight.clear(); 22 | total_nodes_weight = 0.0; 23 | for (int i = 0; i < num_nodes; ++i) 24 | { 25 | adj_list[i].clear(); 26 | nodes_weight.push_back(_nodes_weight[i]); 27 | total_nodes_weight+=_nodes_weight[i]; 28 | } 29 | 30 | for (int i = 0; i < num_edges; ++i) 31 | { 32 | int x = edges_from[i], y = edges_to[i]; 33 | adj_list[x].push_back(y); 34 | adj_list[y].push_back(x); 35 | edge_list[i] = std::make_pair(edges_from[i], edges_to[i]); 36 | } 37 | // for (int i = 0; i < num_nodes; ++i) 38 | // sort(adj_list[i].begin(),adj_list[i].end()); 39 | } 40 | 41 | double Graph::getTwoRankNeighborsRatio(std::vector covered) 42 | { 43 | std::set tempSet; 44 | for(int i =0;i<(int)covered.size();++i){ 45 | tempSet.insert(covered[i]); 46 | } 47 | double sum = 0; 48 | for(int i =0;i v3; 53 | std::set_intersection(adj_list[i].begin(),adj_list[i].end(),adj_list[j].begin(),adj_list[j].end(),std::inserter(v3,v3.begin())); 54 | if(v3.size()>0){ 55 | sum += 1.0; 56 | } 57 | } 58 | } 59 | } 60 | } 61 | return sum; 62 | } 63 | 64 | GSet::GSet() 65 | { 66 | graph_pool.clear(); 67 | } 68 | void GSet::Clear() 69 | { 70 | graph_pool.clear(); 71 | } 72 | 73 | void GSet::InsertGraph(int gid, std::shared_ptr graph) 74 | { 75 | assert(graph_pool.count(gid) == 0); 76 | 77 | graph_pool[gid] = graph; 78 | } 79 | 80 | std::shared_ptr GSet::Get(int gid) 81 | { 82 | assert(graph_pool.count(gid)); 83 | return graph_pool[gid]; 84 | } 85 | 86 | std::shared_ptr GSet::Sample() 87 | { 88 | // printf("graph_pool_size:%d",graph_pool.size()); 89 | assert(graph_pool.size()); 90 | // printf("graph_pool_size:%d",graph_pool.size()); 91 | int gid = rand() % graph_pool.size(); 92 | assert(graph_pool[gid]); 93 | return graph_pool[gid]; 94 | } 95 | 96 | GSet GSetTrain, GSetTest; -------------------------------------------------------------------------------- /code/FINDER_CN_cost/utils.pyx: -------------------------------------------------------------------------------- 1 | from cython.operator import dereference as deref 2 | from libcpp.memory cimport shared_ptr 3 | import numpy as np 4 | import graph 5 | from graph cimport Graph 6 | import gc 7 | from libc.stdlib cimport free 8 | 9 | cdef class py_Utils: 10 | cdef shared_ptr[Utils] inner_Utils 11 | cdef shared_ptr[Graph] inner_Graph 12 | def __cinit__(self): 13 | self.inner_Utils = shared_ptr[Utils](new Utils()) 14 | # def __dealloc__(self): 15 | # if self.inner_Utils != NULL: 16 | # self.inner_Utils.reset() 17 | # gc.collect() 18 | 19 | def getRobustness(self,_g,solution): 20 | self.inner_Graph =shared_ptr[Graph](new Graph()) 21 | deref(self.inner_Graph).num_nodes = _g.num_nodes 22 | deref(self.inner_Graph).num_edges = _g.num_edges 23 | deref(self.inner_Graph).edge_list = _g.edge_list 24 | deref(self.inner_Graph).adj_list = _g.adj_list 25 | deref(self.inner_Graph).nodes_weight=_g.nodes_weight 26 | deref(self.inner_Graph).total_nodes_weight=_g.total_nodes_weight 27 | return deref(self.inner_Utils).getRobustness(self.inner_Graph,solution) 28 | 29 | 30 | def reInsert(self,_g,solution,allVex,int decreaseStrategyID,int reinsertEachStep): 31 | self.inner_Graph =shared_ptr[Graph](new Graph()) 32 | deref(self.inner_Graph).num_nodes = _g.num_nodes 33 | deref(self.inner_Graph).num_edges = _g.num_edges 34 | deref(self.inner_Graph).edge_list = _g.edge_list 35 | deref(self.inner_Graph).adj_list = _g.adj_list 36 | deref(self.inner_Graph).nodes_weight=_g.nodes_weight 37 | deref(self.inner_Graph).total_nodes_weight=_g.total_nodes_weight 38 | return deref(self.inner_Utils).reInsert(self.inner_Graph,solution,allVex,decreaseStrategyID,reinsertEachStep) 39 | 40 | def getMxWccSz(self, _g): 41 | self.inner_Graph =shared_ptr[Graph](new Graph()) 42 | deref(self.inner_Graph).num_nodes = _g.num_nodes 43 | deref(self.inner_Graph).num_edges = _g.num_edges 44 | deref(self.inner_Graph).edge_list = _g.edge_list 45 | deref(self.inner_Graph).adj_list = _g.adj_list 46 | deref(self.inner_Graph).nodes_weight=_g.nodes_weight 47 | deref(self.inner_Graph).total_nodes_weight=_g.total_nodes_weight 48 | return deref(self.inner_Utils).getMxWccSz(self.inner_Graph) 49 | 50 | def Betweenness(self,_g): 51 | self.inner_Graph =shared_ptr[Graph](new Graph()) 52 | deref(self.inner_Graph).num_nodes = _g.num_nodes 53 | deref(self.inner_Graph).num_edges = _g.num_edges 54 | deref(self.inner_Graph).edge_list = _g.edge_list 55 | deref(self.inner_Graph).adj_list = _g.adj_list 56 | return deref(self.inner_Utils).Betweenness(self.inner_Graph) 57 | 58 | @property 59 | def MaxWccSzList(self): 60 | return deref(self.inner_Utils).MaxWccSzList 61 | -------------------------------------------------------------------------------- /code/FINDER_ND_cost/utils.pyx: -------------------------------------------------------------------------------- 1 | from cython.operator import dereference as deref 2 | from libcpp.memory cimport shared_ptr 3 | import numpy as np 4 | import graph 5 | from graph cimport Graph 6 | import gc 7 | from libc.stdlib cimport free 8 | 9 | cdef class py_Utils: 10 | cdef shared_ptr[Utils] inner_Utils 11 | cdef shared_ptr[Graph] inner_Graph 12 | def __cinit__(self): 13 | self.inner_Utils = shared_ptr[Utils](new Utils()) 14 | # def __dealloc__(self): 15 | # if self.inner_Utils != NULL: 16 | # self.inner_Utils.reset() 17 | # gc.collect() 18 | 19 | def getRobustness(self,_g,solution): 20 | self.inner_Graph =shared_ptr[Graph](new Graph()) 21 | deref(self.inner_Graph).num_nodes = _g.num_nodes 22 | deref(self.inner_Graph).num_edges = _g.num_edges 23 | deref(self.inner_Graph).edge_list = _g.edge_list 24 | deref(self.inner_Graph).adj_list = _g.adj_list 25 | deref(self.inner_Graph).nodes_weight=_g.nodes_weight 26 | deref(self.inner_Graph).total_nodes_weight=_g.total_nodes_weight 27 | return deref(self.inner_Utils).getRobustness(self.inner_Graph,solution) 28 | 29 | 30 | def reInsert(self,_g,solution,allVex,int decreaseStrategyID,int reinsertEachStep): 31 | self.inner_Graph =shared_ptr[Graph](new Graph()) 32 | deref(self.inner_Graph).num_nodes = _g.num_nodes 33 | deref(self.inner_Graph).num_edges = _g.num_edges 34 | deref(self.inner_Graph).edge_list = _g.edge_list 35 | deref(self.inner_Graph).adj_list = _g.adj_list 36 | deref(self.inner_Graph).nodes_weight=_g.nodes_weight 37 | deref(self.inner_Graph).total_nodes_weight=_g.total_nodes_weight 38 | return deref(self.inner_Utils).reInsert(self.inner_Graph,solution,allVex,decreaseStrategyID,reinsertEachStep) 39 | 40 | def getMxWccSz(self, _g): 41 | self.inner_Graph =shared_ptr[Graph](new Graph()) 42 | deref(self.inner_Graph).num_nodes = _g.num_nodes 43 | deref(self.inner_Graph).num_edges = _g.num_edges 44 | deref(self.inner_Graph).edge_list = _g.edge_list 45 | deref(self.inner_Graph).adj_list = _g.adj_list 46 | deref(self.inner_Graph).nodes_weight=_g.nodes_weight 47 | deref(self.inner_Graph).total_nodes_weight=_g.total_nodes_weight 48 | return deref(self.inner_Utils).getMxWccSz(self.inner_Graph) 49 | 50 | def Betweenness(self,_g): 51 | self.inner_Graph =shared_ptr[Graph](new Graph()) 52 | deref(self.inner_Graph).num_nodes = _g.num_nodes 53 | deref(self.inner_Graph).num_edges = _g.num_edges 54 | deref(self.inner_Graph).edge_list = _g.edge_list 55 | deref(self.inner_Graph).adj_list = _g.adj_list 56 | return deref(self.inner_Utils).Betweenness(self.inner_Graph) 57 | 58 | @property 59 | def MaxWccSzList(self): 60 | return deref(self.inner_Utils).MaxWccSzList 61 | -------------------------------------------------------------------------------- /code/FINDER_CN_cost/src/lib/msg_pass.cpp: -------------------------------------------------------------------------------- 1 | #include "msg_pass.h" 2 | 3 | std::shared_ptr n2n_construct(GraphStruct* graph) 4 | { 5 | std::shared_ptr result =std::shared_ptr(new sparseMatrix()); 6 | result.rowNum = graph->num_nodes; 7 | result.colNum = graph->num_nodes; 8 | for (uint i = 0; i < graph->num_nodes; ++i) 9 | { 10 | 11 | auto& list = graph->in_edges->head[i]; 12 | 13 | for (size_t j = 0; j < list.size(); ++j) 14 | { 15 | 16 | result.value.pushback(1.0); 17 | result.rowIndex.push_back(i); 18 | result.colIndex.push_back(list[j].second); 19 | } 20 | } 21 | return result; 22 | } 23 | 24 | std::shared_ptr e2n_construct(GraphStruct* graph) 25 | { 26 | std::shared_ptr result =std::shared_ptr(new sparseMatrix()); 27 | result.rowNum = graph->num_nodes; 28 | result.colNum = graph->num_edges; 29 | for (uint i = 0; i < graph->num_nodes; ++i) 30 | { 31 | auto& list = graph->in_edges->head[i]; 32 | for (size_t j = 0; j < list.size(); ++j) 33 | { 34 | result.value.pushback(1.0); 35 | result.rowIndex.push_back(i); 36 | result.colIndex.push_back(list[j].first); 37 | } 38 | } 39 | return result; 40 | } 41 | 42 | std::shared_ptr n2e_construct(GraphStruct* graph) 43 | { 44 | std::shared_ptr result =std::shared_ptr(new sparseMatrix()); 45 | result.rowNum = graph->num_edges; 46 | result.colNum = graph->num_nodes; 47 | 48 | for (uint i = 0; i < graph->num_edges; ++i) 49 | { 50 | result.value.pushback(1.0); 51 | result.rowIndex.push_back(i); 52 | result.colIndex.push_back(graph->edge_list[i].first); 53 | } 54 | return result; 55 | } 56 | 57 | std::shared_ptr e2e_construct(GraphStruct* graph) 58 | { 59 | std::shared_ptr result =std::shared_ptr(new sparseMatrix()); 60 | result.rowNum = graph->num_edges; 61 | result.colNum = graph->num_edges; 62 | for (uint i = 0; i < graph->num_edges; ++i) 63 | { 64 | int node_from = graph->edge_list[i].first, node_to = graph->edge_list[i].second; 65 | auto& list = graph->in_edges->head[node_from]; 66 | for (size_t j = 0; j < list.size(); ++j) 67 | { 68 | if (list[j].second == node_to) 69 | continue; 70 | result.value.pushback(1.0); 71 | result.rowIndex.push_back(i); 72 | result.colIndex.push_back(list[j].first); 73 | } 74 | } 75 | return result; 76 | } 77 | 78 | std::shared_ptr subg_construct(GraphStruct* graph) 79 | { 80 | std::shared_ptr result =std::shared_ptr(new sparseMatrix()); 81 | result.rowNum = graph->num_subgraph; 82 | result.colNum = graph->num_nodes; 83 | for (uint i = 0; i < graph->num_subgraph; ++i) 84 | { 85 | auto& list = graph->subgraph->head[i]; 86 | 87 | for (size_t j = 0; j < list.size(); ++j) 88 | { 89 | result.value.push_back(1.0); 90 | result.rowIndex.push_back(i); 91 | result.colIndex.push_back(list[j]); 92 | } 93 | } 94 | return result; 95 | } 96 | -------------------------------------------------------------------------------- /code/FINDER_CN/src/lib/msg_pass.cpp: -------------------------------------------------------------------------------- 1 | #include "msg_pass.h" 2 | 3 | std::shared_ptr n2n_construct(GraphStruct* graph) 4 | { 5 | std::shared_ptr result =std::shared_ptr(new sparseMatrix()); 6 | result.rowNum = graph->num_nodes; 7 | result.colNum = graph->num_nodes; 8 | for (uint i = 0; i < graph->num_nodes; ++i) 9 | { 10 | 11 | auto& list = graph->in_edges->head[i]; 12 | 13 | for (size_t j = 0; j < (int)list.size(); ++j) 14 | { 15 | 16 | result.value.pushback(1.0); 17 | result.rowIndex.push_back(i); 18 | result.colIndex.push_back(list[j].second); 19 | } 20 | } 21 | return result; 22 | } 23 | 24 | std::shared_ptr e2n_construct(GraphStruct* graph) 25 | { 26 | std::shared_ptr result =std::shared_ptr(new sparseMatrix()); 27 | result.rowNum = graph->num_nodes; 28 | result.colNum = graph->num_edges; 29 | for (uint i = 0; i < graph->num_nodes; ++i) 30 | { 31 | auto& list = graph->in_edges->head[i]; 32 | for (size_t j = 0; j < (int)list.size(); ++j) 33 | { 34 | result.value.pushback(1.0); 35 | result.rowIndex.push_back(i); 36 | result.colIndex.push_back(list[j].first); 37 | } 38 | } 39 | return result; 40 | } 41 | 42 | std::shared_ptr n2e_construct(GraphStruct* graph) 43 | { 44 | std::shared_ptr result =std::shared_ptr(new sparseMatrix()); 45 | result.rowNum = graph->num_edges; 46 | result.colNum = graph->num_nodes; 47 | 48 | for (uint i = 0; i < graph->num_edges; ++i) 49 | { 50 | result.value.pushback(1.0); 51 | result.rowIndex.push_back(i); 52 | result.colIndex.push_back(graph->edge_list[i].first); 53 | } 54 | return result; 55 | } 56 | 57 | std::shared_ptr e2e_construct(GraphStruct* graph) 58 | { 59 | std::shared_ptr result =std::shared_ptr(new sparseMatrix()); 60 | result.rowNum = graph->num_edges; 61 | result.colNum = graph->num_edges; 62 | for (uint i = 0; i < graph->num_edges; ++i) 63 | { 64 | int node_from = graph->edge_list[i].first, node_to = graph->edge_list[i].second; 65 | auto& list = graph->in_edges->head[node_from]; 66 | for (size_t j = 0; j < (int)list.size(); ++j) 67 | { 68 | if (list[j].second == node_to) 69 | continue; 70 | result.value.pushback(1.0); 71 | result.rowIndex.push_back(i); 72 | result.colIndex.push_back(list[j].first); 73 | } 74 | } 75 | return result; 76 | } 77 | 78 | std::shared_ptr subg_construct(GraphStruct* graph) 79 | { 80 | std::shared_ptr result =std::shared_ptr(new sparseMatrix()); 81 | result.rowNum = graph->num_subgraph; 82 | result.colNum = graph->num_nodes; 83 | for (uint i = 0; i < graph->num_subgraph; ++i) 84 | { 85 | auto& list = graph->subgraph->head[i]; 86 | 87 | for (size_t j = 0; j < (int)list.size(); ++j) 88 | { 89 | result.value.push_back(1.0); 90 | result.rowIndex.push_back(i); 91 | result.colIndex.push_back(list[j]); 92 | } 93 | } 94 | return result; 95 | } 96 | -------------------------------------------------------------------------------- /code/FINDER_ND/src/lib/msg_pass.cpp: -------------------------------------------------------------------------------- 1 | #include "msg_pass.h" 2 | 3 | std::shared_ptr n2n_construct(GraphStruct* graph) 4 | { 5 | std::shared_ptr result =std::shared_ptr(new sparseMatrix()); 6 | result.rowNum = graph->num_nodes; 7 | result.colNum = graph->num_nodes; 8 | for (uint i = 0; i < graph->num_nodes; ++i) 9 | { 10 | 11 | auto& list = graph->in_edges->head[i]; 12 | 13 | for (size_t j = 0; j < (int)list.size(); ++j) 14 | { 15 | 16 | result.value.pushback(1.0); 17 | result.rowIndex.push_back(i); 18 | result.colIndex.push_back(list[j].second); 19 | } 20 | } 21 | return result; 22 | } 23 | 24 | std::shared_ptr e2n_construct(GraphStruct* graph) 25 | { 26 | std::shared_ptr result =std::shared_ptr(new sparseMatrix()); 27 | result.rowNum = graph->num_nodes; 28 | result.colNum = graph->num_edges; 29 | for (uint i = 0; i < graph->num_nodes; ++i) 30 | { 31 | auto& list = graph->in_edges->head[i]; 32 | for (size_t j = 0; j < (int)list.size(); ++j) 33 | { 34 | result.value.pushback(1.0); 35 | result.rowIndex.push_back(i); 36 | result.colIndex.push_back(list[j].first); 37 | } 38 | } 39 | return result; 40 | } 41 | 42 | std::shared_ptr n2e_construct(GraphStruct* graph) 43 | { 44 | std::shared_ptr result =std::shared_ptr(new sparseMatrix()); 45 | result.rowNum = graph->num_edges; 46 | result.colNum = graph->num_nodes; 47 | 48 | for (uint i = 0; i < graph->num_edges; ++i) 49 | { 50 | result.value.pushback(1.0); 51 | result.rowIndex.push_back(i); 52 | result.colIndex.push_back(graph->edge_list[i].first); 53 | } 54 | return result; 55 | } 56 | 57 | std::shared_ptr e2e_construct(GraphStruct* graph) 58 | { 59 | std::shared_ptr result =std::shared_ptr(new sparseMatrix()); 60 | result.rowNum = graph->num_edges; 61 | result.colNum = graph->num_edges; 62 | for (uint i = 0; i < graph->num_edges; ++i) 63 | { 64 | int node_from = graph->edge_list[i].first, node_to = graph->edge_list[i].second; 65 | auto& list = graph->in_edges->head[node_from]; 66 | for (size_t j = 0; j < (int)list.size(); ++j) 67 | { 68 | if (list[j].second == node_to) 69 | continue; 70 | result.value.pushback(1.0); 71 | result.rowIndex.push_back(i); 72 | result.colIndex.push_back(list[j].first); 73 | } 74 | } 75 | return result; 76 | } 77 | 78 | std::shared_ptr subg_construct(GraphStruct* graph) 79 | { 80 | std::shared_ptr result =std::shared_ptr(new sparseMatrix()); 81 | result.rowNum = graph->num_subgraph; 82 | result.colNum = graph->num_nodes; 83 | for (uint i = 0; i < graph->num_subgraph; ++i) 84 | { 85 | auto& list = graph->subgraph->head[i]; 86 | 87 | for (size_t j = 0; j < (int)list.size(); ++j) 88 | { 89 | result.value.push_back(1.0); 90 | result.rowIndex.push_back(i); 91 | result.colIndex.push_back(list[j]); 92 | } 93 | } 94 | return result; 95 | } 96 | -------------------------------------------------------------------------------- /code/FINDER_ND_cost/src/lib/msg_pass.cpp: -------------------------------------------------------------------------------- 1 | #include "msg_pass.h" 2 | 3 | std::shared_ptr n2n_construct(GraphStruct* graph) 4 | { 5 | std::shared_ptr result =std::shared_ptr(new sparseMatrix()); 6 | result.rowNum = graph->num_nodes; 7 | result.colNum = graph->num_nodes; 8 | for (uint i = 0; i < graph->num_nodes; ++i) 9 | { 10 | 11 | auto& list = graph->in_edges->head[i]; 12 | 13 | for (size_t j = 0; j < (int)list.size(); ++j) 14 | { 15 | 16 | result.value.pushback(1.0); 17 | result.rowIndex.push_back(i); 18 | result.colIndex.push_back(list[j].second); 19 | } 20 | } 21 | return result; 22 | } 23 | 24 | std::shared_ptr e2n_construct(GraphStruct* graph) 25 | { 26 | std::shared_ptr result =std::shared_ptr(new sparseMatrix()); 27 | result.rowNum = graph->num_nodes; 28 | result.colNum = graph->num_edges; 29 | for (uint i = 0; i < graph->num_nodes; ++i) 30 | { 31 | auto& list = graph->in_edges->head[i]; 32 | for (size_t j = 0; j < (int)list.size(); ++j) 33 | { 34 | result.value.pushback(1.0); 35 | result.rowIndex.push_back(i); 36 | result.colIndex.push_back(list[j].first); 37 | } 38 | } 39 | return result; 40 | } 41 | 42 | std::shared_ptr n2e_construct(GraphStruct* graph) 43 | { 44 | std::shared_ptr result =std::shared_ptr(new sparseMatrix()); 45 | result.rowNum = graph->num_edges; 46 | result.colNum = graph->num_nodes; 47 | 48 | for (uint i = 0; i < graph->num_edges; ++i) 49 | { 50 | result.value.pushback(1.0); 51 | result.rowIndex.push_back(i); 52 | result.colIndex.push_back(graph->edge_list[i].first); 53 | } 54 | return result; 55 | } 56 | 57 | std::shared_ptr e2e_construct(GraphStruct* graph) 58 | { 59 | std::shared_ptr result =std::shared_ptr(new sparseMatrix()); 60 | result.rowNum = graph->num_edges; 61 | result.colNum = graph->num_edges; 62 | for (uint i = 0; i < graph->num_edges; ++i) 63 | { 64 | int node_from = graph->edge_list[i].first, node_to = graph->edge_list[i].second; 65 | auto& list = graph->in_edges->head[node_from]; 66 | for (size_t j = 0; j < (int)list.size(); ++j) 67 | { 68 | if (list[j].second == node_to) 69 | continue; 70 | result.value.pushback(1.0); 71 | result.rowIndex.push_back(i); 72 | result.colIndex.push_back(list[j].first); 73 | } 74 | } 75 | return result; 76 | } 77 | 78 | std::shared_ptr subg_construct(GraphStruct* graph) 79 | { 80 | std::shared_ptr result =std::shared_ptr(new sparseMatrix()); 81 | result.rowNum = graph->num_subgraph; 82 | result.colNum = graph->num_nodes; 83 | for (uint i = 0; i < graph->num_subgraph; ++i) 84 | { 85 | auto& list = graph->subgraph->head[i]; 86 | 87 | for (size_t j = 0; j < (int)list.size(); ++j) 88 | { 89 | result.value.push_back(1.0); 90 | result.rowIndex.push_back(i); 91 | result.colIndex.push_back(list[j]); 92 | } 93 | } 94 | return result; 95 | } 96 | -------------------------------------------------------------------------------- /code/FINDER_CN/src/lib/decrease_strategy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "stdio.h" 7 | #include "disjoint_set.h" 8 | #include "math.h" 9 | using namespace std; 10 | 11 | class decreaseComponentStrategy 12 | { 13 | public: 14 | 15 | decreaseComponentStrategy() 16 | { 17 | // printf("decreaseComponentStrategy:%s\n",description); 18 | } 19 | 20 | virtual long long decreaseComponentNumIfAddNode(const vector >& backupCompletedAdjListGraph, const vector& currentAllVex, Disjoint_Set &unionSet, int node) = 0; 21 | 22 | }; 23 | 24 | 25 | class decreaseComponentRank :public decreaseComponentStrategy 26 | { 27 | 28 | public: 29 | decreaseComponentRank() :decreaseComponentStrategy(){} 30 | 31 | virtual long long decreaseComponentNumIfAddNode(const vector >& backupCompletedAdjListGraph, const vector& currentAllVex, Disjoint_Set &unionSet, int node) 32 | { 33 | unordered_set componentSet; 34 | 35 | for (int i = 0; i < (int)backupCompletedAdjListGraph[node].size(); i++) 36 | { 37 | int neighbourNode = backupCompletedAdjListGraph[node][i]; 38 | 39 | if (currentAllVex[neighbourNode]) 40 | { 41 | componentSet.insert(unionSet.findRoot(neighbourNode)); 42 | } 43 | } 44 | 45 | long long sum = 1; 46 | 47 | for (int eachNode : componentSet) 48 | { 49 | sum += unionSet.getRank(eachNode); 50 | } 51 | return sum; 52 | } 53 | }; 54 | 55 | class decreaseComponentCount : public decreaseComponentStrategy 56 | { 57 | public: 58 | 59 | decreaseComponentCount() :decreaseComponentStrategy(){} 60 | 61 | virtual long long decreaseComponentNumIfAddNode(const vector >& backupCompletedAdjListGraph, const vector& currentAllVex, Disjoint_Set &unionSet, int node) override 62 | { 63 | unordered_set componentSet; 64 | 65 | for (int i = 0; i < (int)backupCompletedAdjListGraph[node].size(); i++) 66 | { 67 | int neighbourNode = backupCompletedAdjListGraph[node][i]; 68 | 69 | if (currentAllVex[neighbourNode]) 70 | { 71 | componentSet.insert(unionSet.findRoot(neighbourNode)); 72 | } 73 | } 74 | 75 | return (long long)componentSet.size(); 76 | } 77 | }; 78 | 79 | class decreaseComponentMultiple : public decreaseComponentStrategy 80 | { 81 | public: 82 | 83 | decreaseComponentMultiple() :decreaseComponentStrategy(){} 84 | 85 | virtual long long decreaseComponentNumIfAddNode(const vector >& backupCompletedAdjListGraph, const vector& currentAllVex, Disjoint_Set &unionSet, int node) override 86 | { 87 | unordered_set componentSet; 88 | 89 | for (int i = 0; i < (int)backupCompletedAdjListGraph[node].size(); i++) 90 | { 91 | int neighbourNode = backupCompletedAdjListGraph[node][i]; 92 | 93 | if (currentAllVex[neighbourNode]) 94 | { 95 | componentSet.insert(unionSet.findRoot(neighbourNode)); 96 | } 97 | } 98 | 99 | long long sum = 1; 100 | 101 | for (int eachNode : componentSet) 102 | { 103 | sum += unionSet.getRank(eachNode); 104 | } 105 | 106 | sum *= componentSet.size(); 107 | 108 | return sum; 109 | } 110 | }; 111 | 112 | -------------------------------------------------------------------------------- /code/FINDER_ND/src/lib/decrease_strategy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "stdio.h" 7 | #include "disjoint_set.h" 8 | #include "math.h" 9 | using namespace std; 10 | 11 | class decreaseComponentStrategy 12 | { 13 | public: 14 | 15 | decreaseComponentStrategy() 16 | { 17 | // printf("decreaseComponentStrategy:%s\n",description); 18 | } 19 | 20 | virtual long long decreaseComponentNumIfAddNode(const vector >& backupCompletedAdjListGraph, const vector& currentAllVex, Disjoint_Set &unionSet, int node) = 0; 21 | 22 | }; 23 | 24 | 25 | class decreaseComponentRank :public decreaseComponentStrategy 26 | { 27 | 28 | public: 29 | decreaseComponentRank() :decreaseComponentStrategy(){} 30 | 31 | virtual long long decreaseComponentNumIfAddNode(const vector >& backupCompletedAdjListGraph, const vector& currentAllVex, Disjoint_Set &unionSet, int node) 32 | { 33 | unordered_set componentSet; 34 | 35 | for (int i = 0; i < (int)backupCompletedAdjListGraph[node].size(); i++) 36 | { 37 | int neighbourNode = backupCompletedAdjListGraph[node][i]; 38 | 39 | if (currentAllVex[neighbourNode]) 40 | { 41 | componentSet.insert(unionSet.findRoot(neighbourNode)); 42 | } 43 | } 44 | 45 | long long sum = 1; 46 | 47 | for (int eachNode : componentSet) 48 | { 49 | sum += unionSet.getRank(eachNode); 50 | } 51 | return sum; 52 | } 53 | }; 54 | 55 | class decreaseComponentCount : public decreaseComponentStrategy 56 | { 57 | public: 58 | 59 | decreaseComponentCount() :decreaseComponentStrategy(){} 60 | 61 | virtual long long decreaseComponentNumIfAddNode(const vector >& backupCompletedAdjListGraph, const vector& currentAllVex, Disjoint_Set &unionSet, int node) override 62 | { 63 | unordered_set componentSet; 64 | 65 | for (int i = 0; i < (int)backupCompletedAdjListGraph[node].size(); i++) 66 | { 67 | int neighbourNode = backupCompletedAdjListGraph[node][i]; 68 | 69 | if (currentAllVex[neighbourNode]) 70 | { 71 | componentSet.insert(unionSet.findRoot(neighbourNode)); 72 | } 73 | } 74 | 75 | return (long long)componentSet.size(); 76 | } 77 | }; 78 | 79 | class decreaseComponentMultiple : public decreaseComponentStrategy 80 | { 81 | public: 82 | 83 | decreaseComponentMultiple() :decreaseComponentStrategy(){} 84 | 85 | virtual long long decreaseComponentNumIfAddNode(const vector >& backupCompletedAdjListGraph, const vector& currentAllVex, Disjoint_Set &unionSet, int node) override 86 | { 87 | unordered_set componentSet; 88 | 89 | for (int i = 0; i < (int)backupCompletedAdjListGraph[node].size(); i++) 90 | { 91 | int neighbourNode = backupCompletedAdjListGraph[node][i]; 92 | 93 | if (currentAllVex[neighbourNode]) 94 | { 95 | componentSet.insert(unionSet.findRoot(neighbourNode)); 96 | } 97 | } 98 | 99 | long long sum = 1; 100 | 101 | for (int eachNode : componentSet) 102 | { 103 | sum += unionSet.getRank(eachNode); 104 | } 105 | 106 | sum *= componentSet.size(); 107 | 108 | return sum; 109 | } 110 | }; 111 | 112 | -------------------------------------------------------------------------------- /code/FINDER_CN_cost/src/lib/decrease_strategy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "stdio.h" 7 | #include "disjoint_set.h" 8 | #include "math.h" 9 | using namespace std; 10 | 11 | class decreaseComponentStrategy 12 | { 13 | public: 14 | 15 | decreaseComponentStrategy() 16 | { 17 | // printf("decreaseComponentStrategy:%s\n",description); 18 | } 19 | 20 | virtual long long decreaseComponentNumIfAddNode(const vector >& backupCompletedAdjListGraph, const vector& currentAllVex, Disjoint_Set &unionSet, int node) = 0; 21 | 22 | }; 23 | 24 | 25 | class decreaseComponentRank :public decreaseComponentStrategy 26 | { 27 | 28 | public: 29 | decreaseComponentRank() :decreaseComponentStrategy(){} 30 | 31 | virtual long long decreaseComponentNumIfAddNode(const vector >& backupCompletedAdjListGraph, const vector& currentAllVex, Disjoint_Set &unionSet, int node) 32 | { 33 | unordered_set componentSet; 34 | 35 | for (int i = 0; i < (int)backupCompletedAdjListGraph[node].size(); i++) 36 | { 37 | int neighbourNode = backupCompletedAdjListGraph[node][i]; 38 | 39 | if (currentAllVex[neighbourNode]) 40 | { 41 | componentSet.insert(unionSet.findRoot(neighbourNode)); 42 | } 43 | } 44 | 45 | long long sum = 1; 46 | 47 | for (int eachNode : componentSet) 48 | { 49 | sum += unionSet.getRank(eachNode); 50 | } 51 | 52 | return sum; 53 | } 54 | }; 55 | 56 | class decreaseComponentCount : public decreaseComponentStrategy 57 | { 58 | public: 59 | 60 | decreaseComponentCount() :decreaseComponentStrategy(){} 61 | 62 | virtual long long decreaseComponentNumIfAddNode(const vector >& backupCompletedAdjListGraph, const vector& currentAllVex, Disjoint_Set &unionSet, int node) override 63 | { 64 | unordered_set componentSet; 65 | 66 | for (int i = 0; i < (int)backupCompletedAdjListGraph[node].size(); i++) 67 | { 68 | int neighbourNode = backupCompletedAdjListGraph[node][i]; 69 | 70 | if (currentAllVex[neighbourNode]) 71 | { 72 | componentSet.insert(unionSet.findRoot(neighbourNode)); 73 | } 74 | } 75 | 76 | return (long long)componentSet.size(); 77 | } 78 | }; 79 | 80 | class decreaseComponentMultiple : public decreaseComponentStrategy 81 | { 82 | public: 83 | 84 | decreaseComponentMultiple() :decreaseComponentStrategy(){} 85 | 86 | virtual long long decreaseComponentNumIfAddNode(const vector >& backupCompletedAdjListGraph, const vector& currentAllVex, Disjoint_Set &unionSet, int node) override 87 | { 88 | unordered_set componentSet; 89 | 90 | for (int i = 0; i < (int)backupCompletedAdjListGraph[node].size(); i++) 91 | { 92 | int neighbourNode = backupCompletedAdjListGraph[node][i]; 93 | 94 | if (currentAllVex[neighbourNode]) 95 | { 96 | componentSet.insert(unionSet.findRoot(neighbourNode)); 97 | } 98 | } 99 | 100 | long long sum = 1; 101 | 102 | for (int eachNode : componentSet) 103 | { 104 | sum += unionSet.getRank(eachNode); 105 | } 106 | 107 | sum *= componentSet.size(); 108 | 109 | return sum; 110 | } 111 | }; 112 | 113 | -------------------------------------------------------------------------------- /code/FINDER_ND_cost/src/lib/decrease_strategy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "stdio.h" 7 | #include "disjoint_set.h" 8 | #include "math.h" 9 | using namespace std; 10 | 11 | class decreaseComponentStrategy 12 | { 13 | public: 14 | 15 | decreaseComponentStrategy() 16 | { 17 | // printf("decreaseComponentStrategy:%s\n",description); 18 | } 19 | 20 | virtual long long decreaseComponentNumIfAddNode(const vector >& backupCompletedAdjListGraph, const vector& currentAllVex, Disjoint_Set &unionSet, int node) = 0; 21 | 22 | }; 23 | 24 | 25 | class decreaseComponentRank :public decreaseComponentStrategy 26 | { 27 | 28 | public: 29 | decreaseComponentRank() :decreaseComponentStrategy(){} 30 | 31 | virtual long long decreaseComponentNumIfAddNode(const vector >& backupCompletedAdjListGraph, const vector& currentAllVex, Disjoint_Set &unionSet, int node) 32 | { 33 | unordered_set componentSet; 34 | 35 | for (int i = 0; i < (int)backupCompletedAdjListGraph[node].size(); i++) 36 | { 37 | int neighbourNode = backupCompletedAdjListGraph[node][i]; 38 | 39 | if (currentAllVex[neighbourNode]) 40 | { 41 | componentSet.insert(unionSet.findRoot(neighbourNode)); 42 | } 43 | } 44 | 45 | long long sum = 1; 46 | 47 | for (int eachNode : componentSet) 48 | { 49 | sum += unionSet.getRank(eachNode); 50 | } 51 | 52 | return sum; 53 | } 54 | }; 55 | 56 | class decreaseComponentCount : public decreaseComponentStrategy 57 | { 58 | public: 59 | 60 | decreaseComponentCount() :decreaseComponentStrategy(){} 61 | 62 | virtual long long decreaseComponentNumIfAddNode(const vector >& backupCompletedAdjListGraph, const vector& currentAllVex, Disjoint_Set &unionSet, int node) override 63 | { 64 | unordered_set componentSet; 65 | 66 | for (int i = 0; i < (int)backupCompletedAdjListGraph[node].size(); i++) 67 | { 68 | int neighbourNode = backupCompletedAdjListGraph[node][i]; 69 | 70 | if (currentAllVex[neighbourNode]) 71 | { 72 | componentSet.insert(unionSet.findRoot(neighbourNode)); 73 | } 74 | } 75 | 76 | return (long long)componentSet.size(); 77 | } 78 | }; 79 | 80 | class decreaseComponentMultiple : public decreaseComponentStrategy 81 | { 82 | public: 83 | 84 | decreaseComponentMultiple() :decreaseComponentStrategy(){} 85 | 86 | virtual long long decreaseComponentNumIfAddNode(const vector >& backupCompletedAdjListGraph, const vector& currentAllVex, Disjoint_Set &unionSet, int node) override 87 | { 88 | unordered_set componentSet; 89 | 90 | for (int i = 0; i < (int)backupCompletedAdjListGraph[node].size(); i++) 91 | { 92 | int neighbourNode = backupCompletedAdjListGraph[node][i]; 93 | 94 | if (currentAllVex[neighbourNode]) 95 | { 96 | componentSet.insert(unionSet.findRoot(neighbourNode)); 97 | } 98 | } 99 | 100 | long long sum = 1; 101 | 102 | for (int eachNode : componentSet) 103 | { 104 | sum += unionSet.getRank(eachNode); 105 | } 106 | 107 | sum *= componentSet.size(); 108 | 109 | return sum; 110 | } 111 | }; 112 | 113 | -------------------------------------------------------------------------------- /code/FINDER_CN/src/lib/graph_struct.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPH_STRUCT_H 2 | #define GRAPH_STRUCT_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | /** 11 | * @brief Class for linked table (an array of linked list) 12 | * 13 | * @tparam T element type in the linked list 14 | */ 15 | template 16 | class LinkedTable 17 | { 18 | public: 19 | /** 20 | * @brief constructor; create an empty linked table 21 | */ 22 | LinkedTable(); 23 | ~LinkedTable(); 24 | /** 25 | * @brief add an entry to the head_id-th linked list with content 26 | * 27 | * @param[in] head_id The linked list identifier 28 | * @param[in] content The content to be append 29 | */ 30 | void AddEntry(int head_id, T content); 31 | 32 | /** 33 | * @brief preserve the space for header array 34 | * 35 | * @param[in] new_n new array size 36 | */ 37 | void Resize(int new_n); 38 | 39 | /** 40 | * header array size 41 | */ 42 | int n; 43 | 44 | /** 45 | * array of linked list (vector of vector) 46 | */ 47 | std::vector< std::vector > head; 48 | 49 | private: 50 | /** 51 | * capacity of the head vector; when doing resize, we won't clean up the head, 52 | * rather, we will only increat the size when ncap is not enough; 53 | */ 54 | int ncap; 55 | }; 56 | 57 | /** 58 | * @brief represents a (directed) graph 59 | */ 60 | class GraphStruct 61 | { 62 | public: 63 | /** 64 | * @brief construct an empty graph 65 | */ 66 | GraphStruct(); 67 | 68 | /** 69 | * @brief delete the pointers 70 | */ 71 | ~GraphStruct(); 72 | 73 | /** 74 | * @brief Adds an edge. 75 | * 76 | * @param[in] idx The edge index, must add in the increasing order of idx (start from 0) 77 | * @param[in] x edge start 78 | * @param[in] y edge end 79 | */ 80 | void AddEdge(int idx, int x, int y); 81 | 82 | /** 83 | * @brief Add a node to the graph 84 | * 85 | * @param[in] subg_id The subgraph identifier (used for mini-batch) 86 | * @param[in] n_idx The node index 87 | */ 88 | void AddNode(int subg_id, int n_idx); 89 | 90 | /** 91 | * @brief preserve the space 92 | * 93 | * @param[in] _num_subgraph The number of subgraph 94 | * @param[in] _num_nodes The number of nodes 95 | */ 96 | void Resize(unsigned _num_subgraph, unsigned _num_nodes = 0); 97 | 98 | /** 99 | * adjacent list representation; stores out going edges of a node 100 | */ 101 | LinkedTable< std::pair > *out_edges; 102 | /** 103 | * adjacent list representation; stores incoming edges of a node 104 | */ 105 | LinkedTable< std::pair > *in_edges; 106 | 107 | /** 108 | * track the node lists of each subgraph 109 | */ 110 | LinkedTable< int >* subgraph; 111 | 112 | /** 113 | * edge list ({x->y}), where the position in the edge_list corresponds to the edge index 114 | */ 115 | std::vector< std::pair > edge_list; 116 | 117 | /** 118 | * total # nodes 119 | */ 120 | unsigned num_nodes; 121 | /** 122 | * total # edges 123 | */ 124 | unsigned num_edges; 125 | /** 126 | * # subgraphs 127 | */ 128 | unsigned num_subgraph; 129 | }; 130 | 131 | extern GraphStruct batch_graph; // global variable 132 | 133 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND/src/lib/graph_struct.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPH_STRUCT_H 2 | #define GRAPH_STRUCT_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | /** 11 | * @brief Class for linked table (an array of linked list) 12 | * 13 | * @tparam T element type in the linked list 14 | */ 15 | template 16 | class LinkedTable 17 | { 18 | public: 19 | /** 20 | * @brief constructor; create an empty linked table 21 | */ 22 | LinkedTable(); 23 | ~LinkedTable(); 24 | /** 25 | * @brief add an entry to the head_id-th linked list with content 26 | * 27 | * @param[in] head_id The linked list identifier 28 | * @param[in] content The content to be append 29 | */ 30 | void AddEntry(int head_id, T content); 31 | 32 | /** 33 | * @brief preserve the space for header array 34 | * 35 | * @param[in] new_n new array size 36 | */ 37 | void Resize(int new_n); 38 | 39 | /** 40 | * header array size 41 | */ 42 | int n; 43 | 44 | /** 45 | * array of linked list (vector of vector) 46 | */ 47 | std::vector< std::vector > head; 48 | 49 | private: 50 | /** 51 | * capacity of the head vector; when doing resize, we won't clean up the head, 52 | * rather, we will only increat the size when ncap is not enough; 53 | */ 54 | int ncap; 55 | }; 56 | 57 | /** 58 | * @brief represents a (directed) graph 59 | */ 60 | class GraphStruct 61 | { 62 | public: 63 | /** 64 | * @brief construct an empty graph 65 | */ 66 | GraphStruct(); 67 | 68 | /** 69 | * @brief delete the pointers 70 | */ 71 | ~GraphStruct(); 72 | 73 | /** 74 | * @brief Adds an edge. 75 | * 76 | * @param[in] idx The edge index, must add in the increasing order of idx (start from 0) 77 | * @param[in] x edge start 78 | * @param[in] y edge end 79 | */ 80 | void AddEdge(int idx, int x, int y); 81 | 82 | /** 83 | * @brief Add a node to the graph 84 | * 85 | * @param[in] subg_id The subgraph identifier (used for mini-batch) 86 | * @param[in] n_idx The node index 87 | */ 88 | void AddNode(int subg_id, int n_idx); 89 | 90 | /** 91 | * @brief preserve the space 92 | * 93 | * @param[in] _num_subgraph The number of subgraph 94 | * @param[in] _num_nodes The number of nodes 95 | */ 96 | void Resize(unsigned _num_subgraph, unsigned _num_nodes = 0); 97 | 98 | /** 99 | * adjacent list representation; stores out going edges of a node 100 | */ 101 | LinkedTable< std::pair > *out_edges; 102 | /** 103 | * adjacent list representation; stores incoming edges of a node 104 | */ 105 | LinkedTable< std::pair > *in_edges; 106 | 107 | /** 108 | * track the node lists of each subgraph 109 | */ 110 | LinkedTable< int >* subgraph; 111 | 112 | /** 113 | * edge list ({x->y}), where the position in the edge_list corresponds to the edge index 114 | */ 115 | std::vector< std::pair > edge_list; 116 | 117 | /** 118 | * total # nodes 119 | */ 120 | unsigned num_nodes; 121 | /** 122 | * total # edges 123 | */ 124 | unsigned num_edges; 125 | /** 126 | * # subgraphs 127 | */ 128 | unsigned num_subgraph; 129 | }; 130 | 131 | extern GraphStruct batch_graph; // global variable 132 | 133 | #endif -------------------------------------------------------------------------------- /code/FINDER_CN_cost/src/lib/graph_struct.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPH_STRUCT_H 2 | #define GRAPH_STRUCT_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | /** 11 | * @brief Class for linked table (an array of linked list) 12 | * 13 | * @tparam T element type in the linked list 14 | */ 15 | template 16 | class LinkedTable 17 | { 18 | public: 19 | /** 20 | * @brief constructor; create an empty linked table 21 | */ 22 | LinkedTable(); 23 | ~LinkedTable(); 24 | /** 25 | * @brief add an entry to the head_id-th linked list with content 26 | * 27 | * @param[in] head_id The linked list identifier 28 | * @param[in] content The content to be append 29 | */ 30 | void AddEntry(int head_id, T content); 31 | 32 | /** 33 | * @brief preserve the space for header array 34 | * 35 | * @param[in] new_n new array size 36 | */ 37 | void Resize(int new_n); 38 | 39 | /** 40 | * header array size 41 | */ 42 | int n; 43 | 44 | /** 45 | * array of linked list (vector of vector) 46 | */ 47 | std::vector< std::vector > head; 48 | 49 | private: 50 | /** 51 | * capacity of the head vector; when doing resize, we won't clean up the head, 52 | * rather, we will only increat the size when ncap is not enough; 53 | */ 54 | int ncap; 55 | }; 56 | 57 | /** 58 | * @brief represents a (directed) graph 59 | */ 60 | class GraphStruct 61 | { 62 | public: 63 | /** 64 | * @brief construct an empty graph 65 | */ 66 | GraphStruct(); 67 | 68 | /** 69 | * @brief delete the pointers 70 | */ 71 | ~GraphStruct(); 72 | 73 | /** 74 | * @brief Adds an edge. 75 | * 76 | * @param[in] idx The edge index, must add in the increasing order of idx (start from 0) 77 | * @param[in] x edge start 78 | * @param[in] y edge end 79 | */ 80 | void AddEdge(int idx, int x, int y); 81 | 82 | /** 83 | * @brief Add a node to the graph 84 | * 85 | * @param[in] subg_id The subgraph identifier (used for mini-batch) 86 | * @param[in] n_idx The node index 87 | */ 88 | void AddNode(int subg_id, int n_idx); 89 | 90 | /** 91 | * @brief preserve the space 92 | * 93 | * @param[in] _num_subgraph The number of subgraph 94 | * @param[in] _num_nodes The number of nodes 95 | */ 96 | void Resize(unsigned _num_subgraph, unsigned _num_nodes = 0); 97 | 98 | /** 99 | * adjacent list representation; stores out going edges of a node 100 | */ 101 | LinkedTable< std::pair > *out_edges; 102 | /** 103 | * adjacent list representation; stores incoming edges of a node 104 | */ 105 | LinkedTable< std::pair > *in_edges; 106 | 107 | /** 108 | * track the node lists of each subgraph 109 | */ 110 | LinkedTable< int >* subgraph; 111 | 112 | /** 113 | * edge list ({x->y}), where the position in the edge_list corresponds to the edge index 114 | */ 115 | std::vector< std::pair > edge_list; 116 | 117 | /** 118 | * total # nodes 119 | */ 120 | unsigned num_nodes; 121 | /** 122 | * total # edges 123 | */ 124 | unsigned num_edges; 125 | /** 126 | * # subgraphs 127 | */ 128 | unsigned num_subgraph; 129 | }; 130 | 131 | extern GraphStruct batch_graph; // global variable 132 | 133 | #endif -------------------------------------------------------------------------------- /code/FINDER_ND_cost/src/lib/graph_struct.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPH_STRUCT_H 2 | #define GRAPH_STRUCT_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | /** 11 | * @brief Class for linked table (an array of linked list) 12 | * 13 | * @tparam T element type in the linked list 14 | */ 15 | template 16 | class LinkedTable 17 | { 18 | public: 19 | /** 20 | * @brief constructor; create an empty linked table 21 | */ 22 | LinkedTable(); 23 | ~LinkedTable(); 24 | /** 25 | * @brief add an entry to the head_id-th linked list with content 26 | * 27 | * @param[in] head_id The linked list identifier 28 | * @param[in] content The content to be append 29 | */ 30 | void AddEntry(int head_id, T content); 31 | 32 | /** 33 | * @brief preserve the space for header array 34 | * 35 | * @param[in] new_n new array size 36 | */ 37 | void Resize(int new_n); 38 | 39 | /** 40 | * header array size 41 | */ 42 | int n; 43 | 44 | /** 45 | * array of linked list (vector of vector) 46 | */ 47 | std::vector< std::vector > head; 48 | 49 | private: 50 | /** 51 | * capacity of the head vector; when doing resize, we won't clean up the head, 52 | * rather, we will only increat the size when ncap is not enough; 53 | */ 54 | int ncap; 55 | }; 56 | 57 | /** 58 | * @brief represents a (directed) graph 59 | */ 60 | class GraphStruct 61 | { 62 | public: 63 | /** 64 | * @brief construct an empty graph 65 | */ 66 | GraphStruct(); 67 | 68 | /** 69 | * @brief delete the pointers 70 | */ 71 | ~GraphStruct(); 72 | 73 | /** 74 | * @brief Adds an edge. 75 | * 76 | * @param[in] idx The edge index, must add in the increasing order of idx (start from 0) 77 | * @param[in] x edge start 78 | * @param[in] y edge end 79 | */ 80 | void AddEdge(int idx, int x, int y); 81 | 82 | /** 83 | * @brief Add a node to the graph 84 | * 85 | * @param[in] subg_id The subgraph identifier (used for mini-batch) 86 | * @param[in] n_idx The node index 87 | */ 88 | void AddNode(int subg_id, int n_idx); 89 | 90 | /** 91 | * @brief preserve the space 92 | * 93 | * @param[in] _num_subgraph The number of subgraph 94 | * @param[in] _num_nodes The number of nodes 95 | */ 96 | void Resize(unsigned _num_subgraph, unsigned _num_nodes = 0); 97 | 98 | /** 99 | * adjacent list representation; stores out going edges of a node 100 | */ 101 | LinkedTable< std::pair > *out_edges; 102 | /** 103 | * adjacent list representation; stores incoming edges of a node 104 | */ 105 | LinkedTable< std::pair > *in_edges; 106 | 107 | /** 108 | * track the node lists of each subgraph 109 | */ 110 | LinkedTable< int >* subgraph; 111 | 112 | /** 113 | * edge list ({x->y}), where the position in the edge_list corresponds to the edge index 114 | */ 115 | std::vector< std::pair > edge_list; 116 | 117 | /** 118 | * total # nodes 119 | */ 120 | unsigned num_nodes; 121 | /** 122 | * total # edges 123 | */ 124 | unsigned num_edges; 125 | /** 126 | * # subgraphs 127 | */ 128 | unsigned num_subgraph; 129 | }; 130 | 131 | extern GraphStruct batch_graph; // global variable 132 | 133 | #endif --------------------------------------------------------------------------------