-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.h
More file actions
197 lines (174 loc) · 4.21 KB
/
Copy pathGraph.h
File metadata and controls
197 lines (174 loc) · 4.21 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
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
#ifndef GRAPH_H
#define GRAPH_H
#include <stdexcept>
#include <iostream>
using namespace std;
struct Pair {
int first;
int second;
Pair(int f, int s) : first(f), second(s) {}
};
template<typename T>
struct ListNode {
T data;
ListNode* next;
ListNode(T d) : data(d), next(nullptr) {}
};
template<typename T>
class List {
public:
ListNode<T>* head;
List() : head(nullptr) {}
~List() {
clear();
}
void push_back(const T& value) {
if (!head) {
head = new ListNode<T>(value);
} else {
ListNode<T>* node = head;
while (node->next) {
node = node->next;
}
node->next = new ListNode<T>(value);
}
}
void push_front(const T& value) {
ListNode<T>* new_node = new ListNode<T>(value);
new_node->next = head;
head = new_node;
}
void erase(ListNode<T>* node) {
if (!node) return;
if (node == head) {
head = head->next;
delete node;
} else {
ListNode<T>* prev = head;
while (prev && prev->next != node) {
prev = prev->next;
}
if (prev) {
prev->next = node->next;
delete node;
}
}
}
void clear() {
ListNode<T>* node = head;
while (node) {
ListNode<T>* temp = node;
node = node->next;
delete temp;
}
head = nullptr;
}
void print() const {
ListNode<T>* node = head;
while (node) {
std::cout << node->data << " ";
node = node->next;
}
std::cout << std::endl;
}
T get(int index) const {
ListNode<T>* current = head;
int count = 0;
while (current != nullptr) {
if (count == index) {
return current->data;
}
current = current->next;
count++;
}
throw std::out_of_range("Index out of bounds"); // Index out of valid range
}
int size() const {
int count = 0;
ListNode<T>* current = head;
while (current != nullptr) {
count++;
current = current->next;
}
return count;
}
};
template<typename K, typename V>
struct MapNode {
K key;
V value;
MapNode* next;
MapNode(K k, V v) : key(k), value(v), next(nullptr) {}
};
template<typename K, typename V>
class Map {
public:
MapNode<K, V>* head;
int count;
Map() : head(nullptr) {}
~Map() {
clear();
}
V& operator[](const K& key) {
MapNode<K, V>* node = head;
while (node) {
if (node->key == key) {
return node->value;
}
node = node->next;
}
// If not found, add new node
V v{};
MapNode<K, V>* newNode = new MapNode<K, V>(key, v);
newNode->next = head;
head = newNode;
count++;
return head->value;
}
const V& at(const K& key) const {
const MapNode<K, V>* node = head;
while (node) {
if (node->key == key) {
return node->value;
}
node = node->next;
}
throw out_of_range("Key not found");
}
void clear() {
MapNode<K, V>* node = head;
while (node) {
MapNode<K, V>* temp = node;
node = node->next;
delete temp;
}
head = nullptr;
count = 0;
}
int size() const {
int size = 0;
MapNode<K, V>* current = head;
while (current) {
size++;
current = current->next;
}
return size;
}
};
class Graph {
private:
Map<int, List<Pair> > adjList;
int numEdges;
void DFS(int v, Map<int,bool>& visited);
int minDistance(const Map<int, int>& dist, const Map<int, bool>& sptSet);
public:
Graph();
~Graph();
Pair getSize();
bool insertEdge(int v1, int v2, int weight);
bool deleteEdge(int v1, int v2);
int findConnectedComponents();
List<int> computeShortestPath(int v1, int v2);
int computeSpanningTree();
};
#endif //UNTITLED4_GRAPH_H