-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
138 lines (123 loc) · 3.57 KB
/
Graph.cpp
File metadata and controls
138 lines (123 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// NAME: GAL BEN AMI
#include <iostream>
#include <vector>
#include "Algorithms.hpp"
#include <stdexcept>
#include "Graph.hpp"
#include <climits>
using namespace std;
namespace ariel
{
// Determines the weight type of the graph.
int whatWeightType(const vector<vector<int>> &matrix)
{
int type = 0;
size_t matSize = matrix.size();
for (size_t i = 0; i < matSize; i++)
{
for (size_t j = 0; j < matSize; j++)
{
if (matrix[i][j] < 0)
{
return -1;
}
if (matrix[i][j] > 1)
{
type = 1;
}
}
}
return type;
}
/*
Loads a graph from an adjacency matrix.
Throws an exception if the matrix is not square or if the diagonal is not zero.
If the graph is undirected and the matrix is not symmetric, we set the graph to be directed.
*/
void Graph::loadGraph(const vector<vector<int>> &matrix)
{
if (matrix.empty() || matrix[0].size() < 2)
{
throw invalid_argument("Input matrix is empty");
}
this->numVertices = matrix.size();
this->adjacencyMatrix = matrix;
size_t matSize = matrix.size();
for (size_t i = 0; i < matSize; i++)
{
if (matrix[i].size() != matSize)
{
throw invalid_argument("Input matrix is not a square matrix");
}
for (size_t j = 0; j < matSize; j++)
{
if (i == j && matrix[i][j] != 0)
{
throw invalid_argument("In adjacency matrix, the diagonal elements should be zeros");
}
if (matrix[i][j] != matrix[j][i] && !this->isDirected)
{
throw invalid_argument("Undirected graph should have symmetric adjacency matrix");
}
}
}
this->weightType = whatWeightType(matrix); // eventually sets the weight type of the graph
}
// Prints the graph, stating whether it's directed or undirected,
// and the number of vertices and edges.
void Graph::printGraph()
{
int edges = 0;
for (size_t i = 0; i < adjacencyMatrix.size(); ++i)
{
for (size_t j = 0; j < adjacencyMatrix[i].size(); ++j)
{
if (i != j && adjacencyMatrix[i][j] != 0)
{
++edges;
}
}
}
if (!this->isDirected)
{
edges /= 2;
cout << "Undirected graph with " << numVertices << " vertices and " << edges << " edges." << endl;
}
else
{
cout << "Directed graph with " << numVertices << " vertices and " << edges << " edges." << endl;
}
}
void Graph::setContainsNegativeCycle(bool flag)
{
this->containsNegativeCycle = flag;
}
bool Graph::getContainsNegativeCycle() const
{
return this->containsNegativeCycle;
}
void Graph::setIsDirected(bool type)
{
this->isDirected = type;
}
void Graph::setWeightsType(int type)
{
this->weightType = type;
}
size_t Graph::getNumVertices() const
{
return numVertices;
}
vector<vector<int>> Graph::getAdjacencyMatrix() const
{
return adjacencyMatrix;
}
bool Graph::getIsDirected() const
{
return this->isDirected;
}
int Graph::getWeightsType() const
{
return this->weightType;
}
}