Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
22 changes: 13 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
cmake_minimum_required(VERSION 3.20)
cmake_minimum_required(VERSION 3.15)
Copy link
Member

Choose a reason for hiding this comment

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

Does it need to be lowered?


project(cpp_template)
set(ProjectName "itlab")

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

include_directories(include)

enable_testing()

add_subdirectory(3rdparty)
add_subdirectory(app)
add_subdirectory(include)
add_subdirectory(src)
add_subdirectory(3rdparty/googletest/gtest)
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.
47 changes: 47 additions & 0 deletions include/graph/graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef GRAPH_H
#define GRAPH_H

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

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;
bool empty() const;
void printGraph() const;
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)
146 changes: 146 additions & 0 deletions src/graph/graph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#include "./graph/graph.h"

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 << std::endl;
}

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

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

Graph::Graph() {}

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

void Graph::getVertex() const {
if (!this->empty()) {
for (auto it = vertices.begin(); it != vertices.end(); it++)
std::cout << it->first << " ";
std::cout << std::endl;
}
}

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 {
int count = 0;
for (auto it = vertices.begin(); it != vertices.end(); it++)
count++;
return count;
}

int Graph::edgeCount() const {
int count = 0;
for (auto it = vertices.begin(); it != vertices.end(); it++) {
count += (it->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::hasPath(int u, int v) {
if (vertices.find(u) != vertices.end() && vertices.find(v) != vertices.end()) {
std::unordered_map <int, bool> visited;
std::queue<int> queue;
queue.push(u);

while (!queue.empty()) {
int current = queue.front();
queue.pop();
if (current == v) { return true; }
visited[current] = true;

for (const int& neighbor : vertices[current]->getNeighbors())
if (!visited[neighbor])
queue.push(neighbor);

}
return false;
Copy link
Member

Choose a reason for hiding this comment

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

Can we reuse the code in hasPath and BFS?

}
}

std::vector<int> Graph::BFS(int start) {
std::vector<int> traversal_order;
std::unordered_map<int, bool> visited;
std::queue<int> queue;

visited[start] = true;
queue.push(start);

while (!queue.empty()) {
int current = queue.front();
queue.pop();
traversal_order.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);
}
}
}
}
//for (int i : traversal_order)
// std::cout << i << " ";
Copy link
Member

Choose a reason for hiding this comment

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

Please, remove all commented out code in the final version

return traversal_order;
}

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

add_executable(run_tests ${TEST_SRC_FILES})
target_link_libraries(run_tests PUBLIC
gtest_main
)
set(TestsName "Tests")

add_executable(${TestsName} ${TEST_FILES})

target_link_libraries(${TestsName} PRIVATE ${ProjectName} gtest)

enable_testing()
add_test(NAME ${TestsName} COMMAND ${TestsName})
4 changes: 2 additions & 2 deletions test/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
#include <gtest.h>

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