-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUndirectedWeightedGraph.cpp
198 lines (160 loc) · 3.97 KB
/
UndirectedWeightedGraph.cpp
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//Copyright (C) Suresh Kumar Srivastava - All Rights Reserved
//DSA Masterclass courses are available on CourseGalaxy.com
//UndirectedWeightedGraph.cpp : Program for undirected graph with weight on edge using adjacency matrix.
#include<iostream>
#include<string>
using namespace std;
static const int maxSize = 30;
class Vertex
{
public:
string name;
public:
Vertex(string name)
{
this->name = name;
}
};//End of class Vertex
class UndirectedWeightedGraph
{
private:
int nVertices;
int nEdges;
int adj[maxSize][maxSize];
Vertex *vertexList[maxSize];
private:
int getIndex(string vertexName);
bool isAdjacent(int u, int v);
public:
UndirectedWeightedGraph();
~UndirectedWeightedGraph();
void insertVertex(string vertexName);
void insertEdge(string source, string destination, int weight);
void deleteEdge(string source, string destination);
void display();
bool edgeExists(string source, string destination);
int getDegree(string vertex);
};//End of class UndirectedWeightedGraph
UndirectedWeightedGraph::UndirectedWeightedGraph()
{
nVertices = 0;
nEdges = 0;
for(int i=0; i<maxSize; i++)
{
for(int j=0; j<maxSize; j++)
{
adj[i][j] = 0;
}
}
}//End of UndirectedWeightedGraph()
UndirectedWeightedGraph::~UndirectedWeightedGraph()
{
for(int i=0; i<nVertices; i++)
{
delete vertexList[i];
}
}//End of ~UndirectedWeightedGraph()
void UndirectedWeightedGraph::insertVertex(string vertexName)
{
vertexList[nVertices++] = new Vertex(vertexName);
}//End of insertVertex()
int UndirectedWeightedGraph::getIndex(string vertexName)
{
for(int i=0; i<nVertices; i++ )
{
if(vertexName == vertexList[i]->name)
return i;
}
throw exception("Invalid Vertex");
}//End of getIndex()
void UndirectedWeightedGraph::insertEdge(string source, string destination, int weight)
{
int u = getIndex(source);
int v = getIndex(destination);
if(u == v)
cout << "Not a valid edge\n";
else if(adj[u][v] != 0)
cout << "Edge already present\n";
else
{
adj[u][v] = weight;
adj[v][u] = weight;
nEdges++;
}
}//End of insertEdge()
void UndirectedWeightedGraph::deleteEdge(string source, string destination)
{
int u = getIndex(source);
int v = getIndex(destination);
if(adj[u][v] != 0)
{
adj[u][v] = 0;
adj[v][u] = 0;
nEdges--;
}
else
cout << "Edge does not exist\n";
}//End of deleteEdge()
void UndirectedWeightedGraph::display()
{
for(int i=0; i<nVertices; i++)
{
for(int j=0; j<nVertices; j++)
cout << adj[i][j] << " ";
cout <<"\n";
}
}//End of display()
bool UndirectedWeightedGraph::isAdjacent(int u, int v)
{
return (adj[u][v] != 0);
}//End of isAdjacent()
//Returns true if edge exists
bool UndirectedWeightedGraph::edgeExists(string source, string destination)
{
return isAdjacent(getIndex(source), getIndex(destination));
}//End of edgeExists()
//Returns number of edges from a vertex
int UndirectedWeightedGraph::getDegree(string vertex)
{
int u = getIndex(vertex);
int degree = 0;
for(int v=0; v<nVertices; v++)
{
if(adj[u][v] != 0)
degree++;
}
return degree;
}//End of getDegree()
int main()
{
UndirectedWeightedGraph uwGraph;
try
{
//Creating the graph, inserting the vertices and edges
uwGraph.insertVertex("0");
uwGraph.insertVertex("1");
uwGraph.insertVertex("2");
uwGraph.insertVertex("3");
uwGraph.insertEdge("0","3",1);
uwGraph.insertEdge("1","2",2);
uwGraph.insertEdge("2","3",3);
uwGraph.insertEdge("3","1",4);
uwGraph.insertEdge("0","2",5);
//Display the graph
uwGraph.display();
cout << "\n";
//Deleting an edge
uwGraph.deleteEdge("0","2");
//Display the graph
uwGraph.display();
//Check if there is an edge between two vertices
cout << "Edge exist : " << (uwGraph.edgeExists("2","3") ? "True" : "False") << "\n";
//Display Indegree and degree of a vertex\n";
cout << "Degree : " << uwGraph.getDegree("3") << "\n";
}//End of try
catch(exception e)
{
cout << e.what() << "\n";
}
return 0;
}//End of main()