Skip to content
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ jobs:
cmake --build build --parallel
- name: Test
run: |
build/bin/run_tests
build/test/run_test
env:
CTEST_OUTPUT_ON_FAILURE: 1
- name: Generate lcov Coverage Data
Expand Down
20 changes: 13 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
cmake_minimum_required(VERSION 3.20)

project(cpp_template)

include(cmake/configure.cmake)
set(ProjectName "itlab")
project(${ProjectName})

include_directories(include)

enable_testing()

add_subdirectory(3rdparty)
add_subdirectory(app)
add_subdirectory(include)
add_subdirectory(src)

add_subdirectory(3rdparty/googletest)
add_subdirectory(src/graph)
add_subdirectory(test)

# REPORT
message( STATUS "")
message( STATUS "General configuration for ${PROJECT_NAME}")
message( STATUS "======================================")
message( STATUS "")
message( STATUS " Configuration: ${CMAKE_BUILD_TYPE}")
message( STATUS "")
Empty file removed include/CMakeLists.txt
Empty file.
43 changes: 43 additions & 0 deletions include/graph/graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef GRAPH_H
#define GRAPH_H

#include <list>
#include <unordered_map>
#include <vector>

class Vertex {
private:
int id_;
std::list<int> neighbors_;

public:
Vertex(int id_);
void addNeighbor(int neighbor);
void removeNeighbor(int neighbor);
void print() const;
int getId() const;
const std::list<int>& getNeighbors() const;
};

class Graph {
private:
std::unordered_map<int, Vertex*> vertices_;

public:
Graph();
void addVertex(int id_);
void getVertex() const;
void addEdge(int u, int v);
void removeEdge(int u, int v);
void removeVertex(int id_);
int vertexCount() const;
int edgeCount() const;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to name them as "get"

bool empty() const;
void printGraph() const;
bool bfs_helper(int start, int vert, bool flag, std::vector<int>* v_ord);
bool hasPath(int u, int v);
std::vector<int> BFS(int start);
~Graph();
};

#endif
Empty file removed src/CMakeLists.txt
Empty file.
7 changes: 7 additions & 0 deletions src/graph/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
file(GLOB_RECURSE HEADER_FILES "${CMAKE_SOURCE_DIR}/include/*.h")
file(GLOB_RECURSE SOURCE_FILES "${CMAKE_SOURCE_DIR}/src/*.cpp")

add_library(${ProjectName} STATIC ${SOURCE_FILES} ${HEADER_FILES})
target_sources(${ProjectName} PRIVATE ${HEADER_FILES})

target_include_directories(${ProjectName} PUBLIC ${CMAKE_SOURCE_DIR}/src)
141 changes: 141 additions & 0 deletions src/graph/graph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#include "./graph/graph.h"

#include <iostream>
#include <list>
#include <queue>
#include <unordered_map>
#include <vector>

Vertex::Vertex(int id_) : id_(id_) {}

void Vertex::addNeighbor(int neighbor) {
if (neighbor != id_) {
neighbors_.push_back(neighbor);
}
}

void Vertex::removeNeighbor(int neighbor) { neighbors_.remove(neighbor); }

void Vertex::print() const {
std::cout << id_ << ": ";
for (const int& neighbor : neighbors_) {
std::cout << neighbor << " ";
}
std::cout << '\n';
}

int Vertex::getId() const { return id_; }

const std::list<int>& Vertex::getNeighbors() const { return neighbors_; }

Graph::Graph() = default;

void Graph::addVertex(int id_) {
if (vertices_.find(id_) == vertices_.end()) {
vertices_[id_] = new Vertex(id_);
}
}

void Graph::getVertex() const {
if (!this->empty()) {
for (const auto& vertice : vertices_) {
std::cout << vertice.first << " ";
}
std::cout << '\n';
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this function does not give a vertex, it only prints some messages


void Graph::addEdge(int u, int v) {
if (vertices_.find(u) == vertices_.end()) {
addVertex(u);
}
if (vertices_.find(v) == vertices_.end()) {
addVertex(v);
}
vertices_[u]->addNeighbor(v);
}

void Graph::removeEdge(int u, int v) {
if (vertices_.find(u) != vertices_.end()) {
vertices_[u]->removeNeighbor(v);
}
}

void Graph::removeVertex(int id_) {
for (auto& pair : vertices_) {
pair.second->removeNeighbor(id_);
}
auto it = vertices_.find(id_);
if (it != vertices_.end()) {
delete it->second;
vertices_.erase(it);
}
}

int Graph::vertexCount() const { return static_cast<int>(vertices_.size()); }

int Graph::edgeCount() const {
int count = 0;
for (const auto& vertice : vertices_) {
count += (vertice.second->getNeighbors()).size();
}
return count;
}

bool Graph::empty() const { return vertices_.empty(); }

void Graph::printGraph() const {
for (const auto& pair : vertices_) {
pair.second->print();
}
}

bool Graph::bfs_helper(int start, int vert, bool flag,
std::vector<int>* v_ord) {
std::unordered_map<int, bool> visited;
std::queue<int> queue;
queue.push(start);
visited[start] = true;

while (!queue.empty()) {
int current = queue.front();
queue.pop();

if (flag && current == vert) {
return true;
}
if (v_ord != nullptr) {
v_ord->push_back(current);
}

if (vertices_.find(current) != vertices_.end()) {
for (const int& neighbor : vertices_[current]->getNeighbors()) {
if (!visited[neighbor]) {
visited[neighbor] = true;
queue.push(neighbor);
}
}
}
}
return false;
}

bool Graph::hasPath(int u, int v) {
if (vertices_.find(u) == vertices_.end() ||
vertices_.find(v) == vertices_.end()) {
return false;
}
return bfs_helper(u, v, true, nullptr);
}

std::vector<int> Graph::BFS(int start) {
std::vector<int> v_ord;
bfs_helper(start, -1, false, &v_ord);
return v_ord;
}

Graph::~Graph() {
for (auto& pair : vertices_) {
delete pair.second;
}
}
12 changes: 7 additions & 5 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
file(GLOB_RECURSE TEST_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)

add_executable(run_tests ${TEST_SRC_FILES})
target_link_libraries(run_tests PUBLIC
gtest_main
)
file(GLOB_RECURSE TEST_FILES ./*.cpp)

set(TestsName "run_test")

add_executable(${TestsName} ${TEST_FILES})

target_link_libraries(${TestsName} PRIVATE ${ProjectName} gtest)
6 changes: 3 additions & 3 deletions test/main.cpp → test/graph/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
#include "gtest/gtest.h"

int main(int argc, char **argv) {
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
}
Loading
Loading