Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include "graph.h"
Graph::Graph(const int &numberOfNodes) {
for (int i = 0; i < numberOfNodes; i++) {
std::vector<bool> row;
for (int j = 0; j < numberOfNodes; j++) {
row.emplace_back(false);
}
connections.emplace_back(row);
}
}
void Graph::connectNodes(const int &node1, const int &node2) {
if (node2 < node1) {
const int temp = node1;
const int node1 = node2;
const int node2 = temp;
}
if ((node1 < 1) || (node2 > connections.size())) {
throw "Node number is bigger than number of nodes or smaller than 1.";
}
connections[node1 - 1][node2 - 1] = true;
connections[node2 - 1][node1 - 1] = true;
}
void Graph::disconnectNodes(const int &node1, const int &node2) {
if (node2 < node1) {
const int temp = node1;
const int node1 = node2;
const int node2 = temp;
}
if ((node1 < 1) || (node2 > connections.size())) {
throw "Node number is bigger than number of nodes or smaller than 1.";
}
connections[node1 - 1][node2 - 1] = false;
connections[node2 - 1][node1 - 1] = false;
}
void Graph::printAdjacencyMatrix() {
for (int i = 0; i < connections.size(); i++) {
for (int j = 0; j < connections.size(); j++) {
std::cout << connections[i][j] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
bool Graph::isPath(const int &node1, const int &node2) {
if ((node1 < 1) || (node2 > connections.size()) || (node2 < 1) || (node1 > connections.size())) {
throw "Node number is bigger than number of nodes or smaller than 1.";
}
std::vector<int> visited;
std::vector<int> path;
return searchPath(node2, node1, &visited);
}
bool Graph::searchPath(const int &node1, const int &node2, std::vector<int> *visitedNodes) {
if (std::find(visitedNodes->begin(), visitedNodes->end(), node1) == visitedNodes->end()) {
visitedNodes->emplace_back(node1);
if (connections[node1 - 1][node2 - 1]) {
Helper::writeNodeToFile(node2, "exercise3");
Helper::writeNodeToFile(node1, "exercise3");
return true;
}
for (int i = 0; i < connections[node1 - 1].size(); i++) {
if (connections[node1 - 1][i]) {
if (searchPath(i + 1, node2, visitedNodes)) {
Helper::writeNodeToFile(node1, "exercise3");
return true;
} else {
return false;
}
}
}
return false;
}
return false;
}
bool Graph::isConnected(){
//Implement for Task2
return false;
}