Added files
This commit is contained in:
parent
3b8e7b6eeb
commit
83caba483f
|
@ -0,0 +1,34 @@
|
|||
# VARIABLES
|
||||
CXX = g++
|
||||
SRC_DIR = src
|
||||
OBJ_DIR = .obj
|
||||
INC_DIR = inc
|
||||
WFLAGS = -Wall
|
||||
OFLAG = -o
|
||||
TARGET = main
|
||||
EXEC = ./$(TARGET)
|
||||
INCLUDE = -I $(INC_DIR)
|
||||
|
||||
|
||||
# FUNCTION:
|
||||
SRCS := $(shell find $(SRC_DIR) -name "*.cpp") # extract all file in src/
|
||||
FILES := $(notdir $(basename $(SRCS))) # extract the name of the file
|
||||
OBJS := $(addprefix $(OBJ_DIR)/,$(addsuffix .o,$(FILES))) # concat obj/ + FILES + .o
|
||||
|
||||
all: execute
|
||||
|
||||
execute: linking
|
||||
@ echo execute...
|
||||
@ $(EXEC)
|
||||
@ echo ...terminate
|
||||
|
||||
linking: $(OBJS)
|
||||
@ echo linking
|
||||
@ $(CXX) $(WFLAGS) $(OFLAG) $(TARGET) $(OBJS)
|
||||
|
||||
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
|
||||
@ echo compile $<
|
||||
@ $(CXX) $(WFLAGS) -c $< $(OFLAG) $@ $(INCLUDE)
|
||||
|
||||
clean:
|
||||
@ -rm -f $(OBJS)
|
|
@ -0,0 +1,8 @@
|
|||
0, 0, 0, 1, 0, 0, 0, 0,
|
||||
1, 0, 0, 0, 1, 0, 0, 0,
|
||||
0, 1, 0, 0, 1, 0, 0, 0,
|
||||
0, 1, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 1, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 1, 1,
|
||||
0, 0, 1, 0, 1, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
|
@ -0,0 +1,27 @@
|
|||
#ifndef MY_GRAPH
|
||||
#define MY_GRAPH
|
||||
|
||||
#include "my_structures.h"
|
||||
#include "my_util.h"
|
||||
#include "my_operator.h"
|
||||
|
||||
|
||||
// take an adjacency matrix and return a vector of verteces O(V ^^ 2)
|
||||
Graph mat2list(Matrix& adj_matrix);
|
||||
// from list of adjacency list to a adjecency matrix 0(V + E)
|
||||
Matrix list2mat(Graph& adj_list);
|
||||
|
||||
// generate an oriented adj matrix
|
||||
Matrix generate_mat(float prob, int dim, bool oriented = true, int max_weight = 1);
|
||||
|
||||
vector<bfs_vertex> BFS(Graph& list, size_t src);
|
||||
vector<bfs_vertex> BFS(Matrix& mat, int src);
|
||||
|
||||
|
||||
bool sametree(vector<dfs_vertex>& dfs_vec, int start);
|
||||
void DFS_visit(Graph& G, vector<dfs_vertex>& dfs_vec, vector<vector<eStatus>>& stat, int& time, int index);
|
||||
vector<dfs_vertex> DFS(Graph& G, int src = 0);
|
||||
vector<dfs_vertex> DFS(Matrix& mat, int src = 0);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
#ifndef MY_OPERATOR
|
||||
#define MY_OPERATOR
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include "my_structures.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
ostream& operator<<(ostream& out, Matrix& mat);
|
||||
istream& operator>>(istream& in, Matrix& mat);
|
||||
|
||||
ostream& operator<<(ostream& out, Graph& list);
|
||||
|
||||
ostream& operator<<(ostream& out, vector<vector<eStatus>>& mat);
|
||||
|
||||
Color operator++(Color& obj); // ++x
|
||||
Color operator++(Color& obj, int); // x++
|
||||
|
||||
#endif
|
|
@ -0,0 +1,62 @@
|
|||
#ifndef MY_STRUCTURES
|
||||
#define MY_STRUCTURES
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
|
||||
using namespace std;
|
||||
using Matrix = vector<vector<int>>;
|
||||
|
||||
|
||||
typedef struct {
|
||||
size_t to;
|
||||
int cost;
|
||||
} Edge;
|
||||
|
||||
|
||||
typedef struct {
|
||||
vector<Edge> neighbours;
|
||||
} Vertex;
|
||||
|
||||
|
||||
using Graph = vector<Vertex>;
|
||||
|
||||
|
||||
enum Color {
|
||||
white,
|
||||
gray,
|
||||
black
|
||||
};
|
||||
|
||||
|
||||
enum eStatus {
|
||||
absent,
|
||||
tree,
|
||||
backward,
|
||||
forward,
|
||||
cross
|
||||
};
|
||||
|
||||
|
||||
typedef struct {
|
||||
int depth;
|
||||
Color color;
|
||||
int previous;
|
||||
} bfs_vertex;
|
||||
|
||||
|
||||
typedef struct {
|
||||
int start;
|
||||
int end;
|
||||
} Time_t;
|
||||
|
||||
|
||||
typedef struct {
|
||||
Color color;
|
||||
int previous;
|
||||
Time_t time;
|
||||
} dfs_vertex;
|
||||
|
||||
#endif
|
|
@ -0,0 +1,16 @@
|
|||
#ifndef MY_UTIL
|
||||
#define MY_UTIL
|
||||
|
||||
#include "../inc/my_structures.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// util
|
||||
bool randomize(float prob);
|
||||
int rand_num(int mod);
|
||||
vector<string> split(string& s, const char delim[] = ",");
|
||||
Matrix read_file(const char path[] = "data/adj_matrix.in");
|
||||
|
||||
#endif
|
|
@ -0,0 +1,27 @@
|
|||
#include <fstream>
|
||||
#include <../inc/my_graph.h>
|
||||
|
||||
const int n = 10000;
|
||||
|
||||
int main() {
|
||||
/* Matrix mat;
|
||||
ofstream out_f("in_out/adj_matrix.out");
|
||||
out_f << mat << endl;
|
||||
Graph list = mat2list(mat);
|
||||
auto res = BFS(list, 1);
|
||||
int src, dest, cost;
|
||||
|
||||
while (true) {
|
||||
cout << "insert src and dest [1, " << list.size() << "]\n";
|
||||
cin >> src >> dest;
|
||||
res = BFS(list, src);
|
||||
cost = res[dest - 1].depth;
|
||||
cout << cost << endl;
|
||||
} */
|
||||
ofstream out("data/adj_list.out");
|
||||
Matrix mat = read_file();
|
||||
Graph list = mat2list(mat);
|
||||
mat.clear();
|
||||
mat = list2mat(list);
|
||||
out << mat;
|
||||
}
|
|
@ -0,0 +1,166 @@
|
|||
#include <../inc/my_graph.h>
|
||||
#include <queue>
|
||||
|
||||
|
||||
const int inf = INT32_MAX;
|
||||
|
||||
|
||||
Graph mat2list(Matrix& adj_matrix) {
|
||||
Graph result(adj_matrix.size());
|
||||
for (size_t i = 0; i < result.size(); i++) {
|
||||
for (size_t j = 0; j < result.size(); j++) {
|
||||
if (adj_matrix[i][j]) {
|
||||
result[i].neighbours.push_back((Edge) { .to = j, .cost = adj_matrix[i][j]});
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Matrix list2mat(Graph& adj_list) {
|
||||
Matrix result(adj_list.size(), vector<int>(adj_list.size()));
|
||||
int i = 0;
|
||||
for (auto& vertex: adj_list) {
|
||||
for (auto& edge: vertex.neighbours) {
|
||||
result[i][edge.to] = edge.cost;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Matrix generate_mat(float prob, int dim, bool oriented, int max_weight) {
|
||||
srand(time(0));
|
||||
Matrix result(dim, vector<int>(dim));
|
||||
if (oriented) {
|
||||
for (int i = 0; i < dim; i++) {
|
||||
for (int j = 0; j < dim; j++) {
|
||||
result[i][j] = randomize(prob) ? rand_num(max_weight) : 0;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < dim; i++) {
|
||||
result[i][i] = 0;
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < dim; i++) {
|
||||
for (int j = dim - 1; j > i; j--) {
|
||||
result[i][j] = randomize(prob) ? rand_num(max_weight) : 0;
|
||||
result[j][i] = result[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Matrix generate_nor(float prob, int dim, int max_weight) {
|
||||
srand(time(0));
|
||||
Matrix result(dim, vector<int>(dim));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
vector<bfs_vertex> BFS(Graph& list, size_t src) {
|
||||
if (src < 1 || src > list.size()) {
|
||||
cout << "ERROR: the index of src isn't in the domain\n";
|
||||
}
|
||||
size_t start_index = src - 1;
|
||||
vector<bfs_vertex> bfs_vec(list.size());
|
||||
for (auto& vertex: bfs_vec) {
|
||||
vertex.color = white;
|
||||
vertex.depth = inf;
|
||||
}
|
||||
bfs_vec[start_index].depth = 0;
|
||||
bfs_vec[start_index].color = gray;
|
||||
bfs_vec[start_index].previous = -1;
|
||||
queue<int> to_visit;
|
||||
to_visit.push(start_index);
|
||||
int current;
|
||||
while (!to_visit.empty()) {
|
||||
current = to_visit.front();
|
||||
for (auto& edge: list[current].neighbours) {
|
||||
if (bfs_vec[edge.to].color == white) {
|
||||
bfs_vec[edge.to].color = gray;
|
||||
bfs_vec[edge.to].depth = bfs_vec[current].depth + edge.cost;
|
||||
bfs_vec[edge.to].previous = current;
|
||||
to_visit.push(edge.to);
|
||||
}
|
||||
}
|
||||
bfs_vec[current].color = black;
|
||||
to_visit.pop();
|
||||
}
|
||||
return bfs_vec;
|
||||
}
|
||||
|
||||
|
||||
vector<bfs_vertex> BFS(Matrix& mat, int src) {
|
||||
Graph list = mat2list(mat);
|
||||
return BFS(list, src);
|
||||
}
|
||||
|
||||
|
||||
bool sametree(vector<dfs_vertex>& dfs_vec, int start) {
|
||||
for (int i = start; i != -1; i = dfs_vec[i].previous) {
|
||||
if (dfs_vec[i].color == gray) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void DFS_visit(Graph& G, vector<dfs_vertex>& dfs_vec, vector<vector<eStatus>>& stat, int& time, int index) {
|
||||
dfs_vec[index].color = gray;
|
||||
dfs_vec[index].time.start = ++time;
|
||||
// cout << "(" << index << " ";
|
||||
for (Edge& e: G[index].neighbours) {
|
||||
switch (dfs_vec[e.to].color) {
|
||||
case white:
|
||||
dfs_vec[e.to].previous = index;
|
||||
stat[index].push_back(eStatus::tree);
|
||||
DFS_visit(G, dfs_vec, stat, time, e.to);
|
||||
break;
|
||||
case gray:
|
||||
stat[index].push_back(eStatus::backward);
|
||||
break;
|
||||
default:
|
||||
if (sametree(dfs_vec, e.to)) {
|
||||
stat[index].push_back(eStatus::forward);
|
||||
} else {
|
||||
stat[index].push_back(eStatus::cross);
|
||||
}
|
||||
}
|
||||
}
|
||||
dfs_vec[index].color = black;
|
||||
dfs_vec[index].time.end = ++time;
|
||||
// cout << index << ") ";
|
||||
}
|
||||
|
||||
|
||||
vector<dfs_vertex> DFS(Graph& G, int src) {
|
||||
vector<dfs_vertex> dfs_vec(G.size());
|
||||
for (auto& vertex: dfs_vec) {
|
||||
vertex.color = white;
|
||||
vertex.previous = -1;
|
||||
}
|
||||
int time = 0;
|
||||
vector<vector<eStatus>> stat(G.size());
|
||||
DFS_visit(G, dfs_vec, stat, time, src);
|
||||
src = 0;
|
||||
for (auto& elem: dfs_vec) {
|
||||
if (elem.color == white) {
|
||||
DFS_visit(G, dfs_vec, stat, time, src);
|
||||
}
|
||||
src++;
|
||||
}
|
||||
return dfs_vec;
|
||||
}
|
||||
|
||||
|
||||
vector<dfs_vertex> DFS(Matrix& mat, int src) {
|
||||
Graph list = mat2list(mat);
|
||||
return DFS(list, src);
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
#include <../inc/my_structures.h>
|
||||
#include <../inc/my_util.h>
|
||||
|
||||
ostream& operator<<(ostream& out, Matrix& mat) {
|
||||
for (auto& row: mat) {
|
||||
for (auto& weight: row) {
|
||||
out << weight;
|
||||
out << ", ";
|
||||
}
|
||||
out << endl;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
ostream& operator<<(ostream& out, Graph& list) {
|
||||
int i = 0;
|
||||
for (auto& vertex: list) {
|
||||
out << "n: " << i + 1 << " => ";
|
||||
string s;
|
||||
for (auto& edge: vertex.neighbours) {
|
||||
out << "[" << edge.to + 1 << ", " << edge.cost << "], ";
|
||||
}
|
||||
out << endl;
|
||||
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
ostream& operator<<(ostream& out, vector<vector<eStatus>>& vec) {
|
||||
int i = 0;
|
||||
for (auto& vertex: vec) {
|
||||
out << ++i << ": ";
|
||||
for (auto& edge: vertex) {
|
||||
switch(edge) {
|
||||
case eStatus::absent:
|
||||
out << "A";
|
||||
break;
|
||||
case eStatus::backward:
|
||||
out << "B";
|
||||
break;
|
||||
case eStatus::cross:
|
||||
out << "C";
|
||||
break;
|
||||
case eStatus::forward:
|
||||
out << "F";
|
||||
break;
|
||||
default:
|
||||
out << "T";
|
||||
}
|
||||
out << ", ";
|
||||
}
|
||||
out << endl;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
istream& operator>>(istream& in, Matrix& mat) {
|
||||
string input_str;
|
||||
vector<string> weigth_list;
|
||||
int row = 0, dim = 0;
|
||||
do {
|
||||
getline(in, input_str);
|
||||
weigth_list = split(input_str);
|
||||
if (dim == 0) {
|
||||
dim = weigth_list.size();
|
||||
mat.resize(dim, vector<int>(dim));
|
||||
}
|
||||
for (int col = 0; col < dim; col++) {
|
||||
mat[row][col] = atoi(&weigth_list[col][0]);
|
||||
}
|
||||
row++;
|
||||
} while (row < dim);
|
||||
return in;
|
||||
}
|
||||
|
||||
|
||||
Color operator++(Color& obj) { // ++x
|
||||
obj = static_cast<Color>((obj + 1));
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
||||
Color operator++(Color& obj, int) { // x++
|
||||
Color result = obj;
|
||||
++obj;
|
||||
return result;
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
#include <fstream>
|
||||
#include <../inc/my_structures.h>
|
||||
#include <../inc/my_operator.h>
|
||||
|
||||
|
||||
bool randomize(float prob) {
|
||||
return prob >= (float)(rand() % 1000) / 1000;
|
||||
}
|
||||
|
||||
|
||||
int rand_num(int mod) {
|
||||
return rand() % (mod) + 1;
|
||||
}
|
||||
|
||||
|
||||
vector<string> split(string& s, const char delim[]) {
|
||||
vector<string> result;
|
||||
size_t pos;
|
||||
do {
|
||||
if ((pos = s.find(delim)) == string::npos) {
|
||||
result.push_back(s);
|
||||
break;
|
||||
}
|
||||
result.push_back(s.substr(0, pos));
|
||||
s = s.substr(pos + 1);
|
||||
} while (s.length() > 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Matrix read_file(const char path[]) {
|
||||
Matrix result;
|
||||
std::ifstream ifile("data/adj_matrix.in");
|
||||
ifile >> result;
|
||||
return result;
|
||||
}
|
Loading…
Reference in New Issue