-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.h
More file actions
162 lines (147 loc) · 4.11 KB
/
Graph.h
File metadata and controls
162 lines (147 loc) · 4.11 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
#include <iostream>
#include "COOArray.h"
struct GraphNode {
int value;
GraphNode *next = NULL;
GraphNode *previous = NULL;
};
struct GraphList {
GraphNode *header = NULL;
GraphNode *footer = NULL;
int size = 0;
void append(int val) {
GraphNode *temp = new GraphNode;
temp->value = val;
if(!header) {
header = temp;
footer = temp;
}
else {
footer->next = temp;
footer = temp;
}
size++;
}
};
struct Stack {
int count = 0;
GraphNode *header = NULL;
GraphNode *footer = NULL;
void push(int val) {
GraphNode *temp = new GraphNode;
temp->value = val;
if(footer == NULL) {
std::cout << "Pushing onto empty\n";
header = temp;
footer = temp;
std::cout << "setting footer pointers\n";
footer->next = NULL;
footer->previous = NULL;
}
else {
std::cout << "Pushing onto non empty\n";
footer->next = temp;
temp->previous = footer;
temp->next = NULL;
footer = temp;
}
count++;
temp = NULL;
}
void pop() {
GraphNode *temp = footer;
if(count == 1) {
delete temp;
header = NULL;
footer = NULL;
count--;
return;
}
footer = footer->previous;
footer->next = NULL;
delete temp;
count--;
}
int first() {
if(count > 0)
return footer->value;
return 0;
}
bool empty() {
std::cout << "count: " << count << "\n";
return count == 0;
}
};
class Graph {
private:
GraphList *graphNodes;
int k = 0;
int size = 0;
int source;
std::vector<int> vertices;
public:
Graph(COOArray &coo, int source) {
size = coo.getRows();
this->source = source;
GraphList *newAlloc = new GraphList[coo.getRows()];
graphNodes = newAlloc;
for(int i = 0; i < size; i++) {
graphNodes[i].header = NULL;
graphNodes[i].footer = NULL;
}
for(int i = 0; i < coo.length(); i++) {
graphNodes[coo.at(i).row-1].append(coo.at(i).col);
}
}
void print() {
for(int i = 0; i < size; i++) {
GraphNode *current = graphNodes[i].header;
while(current) {
std::cout << "GraphNode value: " << current->value << "\n";
current = current->next;
}
}
}
std::vector<bool> dfs(std::vector<bool> s0, int vertex) {
std::vector<bool> s = s0;
int v = vertex-1;
//std::cout << "Setting s to true at = " << vertex << "\n";
s.at(v) = true;
GraphNode *current = graphNodes[v].header;
while(current) {
if(!s.at(current->value - 1) || (current->value == source && vertices.size() > 0)) {
//printf("current->value: %d | source: %d\n", current->value, source);
vertices.push_back(current->value);
s = dfs(s, current->value);
}
current = current->next;
}
return s;
}
std::vector<bool> dfs_iterative(std::vector<bool> s0, int vertex) {
std::vector<bool> s = s0;
Stack stack;
std::cout << "size of s: " << s.size() << "\n";
stack.push(vertex);
std::cout << "finished pushing source\n";
while(!stack.empty()) {
std::cout << "Stack non empty\n";
vertex = stack.first();
stack.pop();
if(!s.at(vertex-1)) {
s.at(vertex-1) = true;
}
GraphNode *current = graphNodes[vertex-1].header;
while(current) {
std::cout << "current value: " << current->value << "\n";
if(!s.at(current->value - 1))
stack.push(current->value);
current = current->next;
}
}
return s;
}
std::vector<int> getVertices() {
return vertices;
}
};