-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweighted_graph.h
168 lines (161 loc) · 5.32 KB
/
weighted_graph.h
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#ifndef WEIGHTED_GRAPH_H
#define WEIGHTED_GRAPH_H
#ifndef nullptr
#define nullptr 0
#endif
#include <iostream>
#include <cmath>
#include <cstdlib>
#include<ctime>
#include <limits>
using namespace std;
//````````````````````````````````````````````` Class Weighted_Graph```````````````````````````````````````````````````
class Weighted_graph
{
private:
int num_ver;
double** adjacency_matrix;
static const double INF;
public:
Weighted_graph();
Weighted_graph(int n);
int degree(int n) const;
int edge_count() const;
double adjacent(int m, int n) const;
double distance(int m, int n) const;
void insert(int m, int n, double w);
~Weighted_graph();
friend ostream& operator<<(ostream&, Weighted_graph const&);
};
const double Weighted_graph::INF = std::numeric_limits<double>::infinity();
//`````````````````````````````````````````````` Default Constructor `````````````````````````````````````````````````````
Weighted_graph::Weighted_graph()
{
num_ver = 0;
adjacency_matrix = nullptr;
}
//````````````````````````````````````````````` Parameterized Constructor ``````````````````````````````````````````````````
Weighted_graph::Weighted_graph(int n = 50)
{
num_ver = (n <= 0) ? 1 : n;
adjacency_matrix = new double* [num_ver];
for (int i = 0; i < num_ver; ++i)
{
adjacency_matrix[i] = new double[num_ver];
for (int j = 0; j < num_ver; ++j)
adjacency_matrix[i][j] = -1.0;
}
}
//`````````````````````````````````````````````````````` Degree ```````````````````````````````````````````````````````````
int Weighted_graph::degree(int n) const
{
if (n < 0 || n >= num_ver)
throw "Vertex index out of bounds";
int degree_count = 0;
for (int i = 0; i < num_ver; ++i)
{
if (i != n && adjacency_matrix[n][i] >= 0)
++degree_count;
}
return degree_count;
}
//````````````````````````````````````````````````````` Edge Count````````````````````````````````````````````````````````````
int Weighted_graph::edge_count() const
{
int count = 0;
for (int i = 0; i < num_ver; ++i)
{
for (int j = i + 1; j < num_ver; ++j)
{
if (adjacency_matrix[i][j] >= 0)
++count;
}
}
return count;
}
//```````````````````````````````````````````````````````` Adjacent ````````````````````````````````````````````````````````````
double Weighted_graph::adjacent(int m, int n) const
{
if (m < 0 || m >= num_ver || n < 0 || n >= num_ver)
throw "Vertex index out of bounds";
if (m == n)
return 0.0;
if (adjacency_matrix[m][n] >= 0)
return adjacency_matrix[m][n];
return std::numeric_limits<double>::infinity();
}
//````````````````````````````````````````````````````````` Distance `````````````````````````````````````````````````````````````
double Weighted_graph::distance(int m, int n) const
{
//Applying Dijistra Algo
if (m < 0 || m >= num_ver || n < 0 || n >= num_ver)
throw "Vertex index out of bounds";
double* dist = new double[num_ver];
for (int i = 0; i < num_ver; ++i)
dist[i] = std::numeric_limits<double>::infinity();
dist[m] = 0.0;
bool* visited = new bool[num_ver] {false};
while (!visited[n])
{
int u = -1;
for (int i = 0; i < num_ver; ++i) {
if (!visited[i] && (u == -1 || dist[i] < dist[u]))
u = i;
}
if (u == -1)
break;
visited[u] = true;
for (int v = 0; v < num_ver; ++v) {
if (!visited[v] && adjacency_matrix[u][v] >= 0) {
dist[v] = std::min(dist[v], dist[u] + adjacency_matrix[u][v]);
}
}
}
double result = dist[n];
delete[] dist;
delete[] visited;
return result;
}
//``````````````````````````````````````````````````````````` Insert ``````````````````````````````````````````````````````````````
void Weighted_graph::insert(int m, int n, double w)
{
if (m < 0 || m >= num_ver || n < 0 || n >= num_ver || m == n)
throw "Invalid vertex indices";
if (w <= 0 || isinf(w))
throw "Invalid edge weight";
adjacency_matrix[m][n] = w;
adjacency_matrix[n][m] = w;
}
//`````````````````````````````````````````````````````````` Destructor ````````````````````````````````````````````````````````````
Weighted_graph::~Weighted_graph() {
if (adjacency_matrix) {
for (int i = 0; i < num_ver; ++i)
delete[] adjacency_matrix[i];
delete[] adjacency_matrix;
}
}
//`````````````````````````````````````````````````````````` Operator<< `````````````````````````````````````````````````````````````
ostream& operator<<(ostream& out, const Weighted_graph& graph)
{
int numVertices = graph.num_ver;
for (int i = 0; i < numVertices; ++i)
{
for (int j = 0; j < numVertices; ++j)
{
if (graph.adjacency_matrix[i][j] >= 0)
{
out << graph.adjacency_matrix[i][j] << "\t";
}
else
{
out << "INF\t";
}
}
out << endl;
}
return out;
}
#endif
//For more Projects
//Contact: +92 3495312852 || +92 3145520810
// Thank you :)