Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include "vList.h"
- #include "grRepres.h"
- using namespace std;
- void GrA::setisDir(bool d) { isDir = d; }
- GrMA::~GrMA() {
- if (E != nullptr) {
- for (size_t l = 0; l < n; ++l) {
- delete[] E[l];
- E[l] = nullptr;
- }
- delete[] E;
- E = nullptr;
- }
- }
- bool GrMA::create(size_t _n) {
- // deletes all the information in previous 2D array
- if (E != nullptr) {
- for (size_t l = 0; l < n; ++l) {
- delete[] E[l];
- E[l] = nullptr;
- }
- delete[] E;
- E = nullptr;
- }
- // creates new 2D matrix
- n = _n;
- E = new(nothrow) bool* [n] {};
- if (E != nullptr) {
- for (size_t i = 0; i < n; ++i) {
- E[i] = new(nothrow) bool [n] {};
- if (E[i] == nullptr)
- break;
- }
- }
- return (E != nullptr);
- }
- bool GrMA::addArc(size_t v, size_t w) {
- E[v][w] = true;
- if (!isDir)
- E[w][v] = true;
- ++m;
- return true;
- }
- VList GrMA::neighbors(size_t i) {
- VList L(n);
- for (int j = 0; j < n; ++j) // check entire matrix
- if (E[i][j] != false)
- L.add(j);
- return L;
- }
- VList GrA::dfs() {
- VList L(n);
- bool* visited = new(nothrow) bool[n];
- if (visited != nullptr) {
- size_t v;
- for (v = 0; v < n; ++v)
- visited[v] = false;
- for (v = 0; v < n; ++v)
- if (!visited[v])
- dfsr(v, L, visited);
- }
- delete[]visited;
- return L;
- }
- void GrMA::dfsr(size_t v, VList& L, bool*& visited) {
- L.add(v); visited[v] = 1;
- for (size_t w = 0; w < n; ++w)
- if (E[v][w]) // w adjacent with v
- if (!visited[w]) // і isn't visited
- dfsr(w, L, visited);
- }
Advertisement
Add Comment
Please, Sign In to add comment