-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
66 lines (58 loc) · 1.47 KB
/
Copy pathGraph.cpp
File metadata and controls
66 lines (58 loc) · 1.47 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
#include "Graph.h"
using namespace std;
string getCityName(int index) {
string cityNames[10] = {
"Islamabad", "Rawalpindi", "Lahore", "Karachi", "Peshawar",
"Quetta", "Multan", "Faisalabad", "Sialkot", "Gujranwala"
};
if (index < 10) return cityNames[index];
return "City_" + to_string(index);
}
// DynamicGraph Implementation
DynamicGraph::DynamicGraph(int v) {
numVertices = v;
edgeCount = 0;
for (int i = 0; i < v; i++) {
adjList[i] = NULL;
}
}
DynamicGraph::~DynamicGraph() {
for (int i = 0; i < numVertices; i++) {
NODEPTR ptr = adjList[i];
while (ptr != NULL) {
NODEPTR temp = ptr;
ptr = ptr->next;
delete temp;
}
}
}
void DynamicGraph::addEdge(int u, int v, int w) {
if (w > 0) {
NODEPTR p = new Node;
p->adj = v;
p->weight = w;
p->next = adjList[u];
adjList[u] = p;
edgeCount++;
}
}
int DynamicGraph::getWeight(int u, int v) {
NODEPTR ptr = adjList[u];
while (ptr != NULL) {
if (ptr->adj == v) return ptr->weight;
ptr = ptr->next;
}
return 0;
}
// StaticGraph Implementation
StaticGraph::StaticGraph(int v) {
numVertices = v;
for (int i = 0; i < v; i++) {
for (int j = 0; j < v; j++) {
g[i][j] = 0;
}
}
}
void StaticGraph::addEdge(int u, int v, int w) {
g[u][v] = w;
}