-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.cpp.test
260 lines (220 loc) · 6.62 KB
/
graph.cpp.test
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/** Arquitetura de Computadores III - Lesandro Ponciano
* Trabalho Pratico 1 - Flicker Database
* Integrantes: Igor Machado Seixas
* Joao Castro
* Linguagem utilizada: C++
* Recursos: <string> substr (int inicio, int quantidade) - Substring
* <string> find (String padrao) - "contains" retorna posicao do padrao se achar, se nao achar returna -1
* <string> at (int posicao) - "charAt" retorna o char da posicao passada por parametro
* <string> stod (string str) - "parseDouble" retorna o valor double da string em parametro
*/
// Imports
#include <iostream>
#include <list>
#include <algorithm>
//#include <string> // Not Used
using namespace std;
#define ARRAY_SIZE 200 // Define the length of the study group
class graph {
public:
// Atributes
list<double>* usergraph; // Vertex wich will cotain user id
list<double>::iterator it;
int n;
/**
* Graph Construtctor
* @param double V - Number of Vertex of the graph
*/
graph ( ) {
usergraph = new list<double> [ARRAY_SIZE];
n = 0;
}
/**
* Add Edge to Graph
* @param u1 - Vertex User
* @param u1 - User to be added
*/
void addEdge ( double u1, double u2 ) {
int pos1, pos2;
// Verificatins if there is User u1
pos1 = this->binaryFind(u1);
if ( pos1 == -1 ) { // Find and check position of first user
pos1 = n; // Update pos1 situation
this->addVertex(u1);
}
// Verificatins if there is User u2
pos2 = this->binaryFind(u2);
if ( pos2 == -1 ) { // Find and check position of second user
pos2 = n; // Update pos2 situation
this->addVertex(u2);
}
// Create Edge between User u1 and User u2
usergraph[pos1].push_back(u2);
usergraph[pos2].push_back(u1);
//it = find(usergraph[0].begin( ), usergraph[0].end( ), 2 );
}
/**
* Add Vertex to Graph
* @param v - Vertex to be added
*/
void addVertex (double v) {
usergraph[n].push_front(v);
n++;
}
/**
* Binary Find
* @param u - User wich will be search
*/
int binaryFind ( double u ) {
int left = 0, right = n -1, middle;
int ans = -1;
while (left <= right) {
middle = (left+right) /2;
if ( u == *usergraph[middle].begin( ) ) {
ans = middle;
left = ARRAY_SIZE;
} else {
if ( u > *usergraph[middle].begin( ) ) {
left = middle+1;
} else {
right = middle-1;
}
}
} // end while
return ans;
}
/**
* Print Graph
*/
void printGraph ( ) {
for ( int i = 0; i < n; i++) {
for ( list<double>::iterator j = usergraph[i].begin( ); j != usergraph[i].end( ); j++ ) {
cout << *j << ' ';
}
cout << '\n';
} // end for
}
/**
* Sequencial Search
* @param u - user which will be search
*/
int sequencialSearch(double u){
int answer = -1;
for(int i = 0; i < n; i++){
if( u == *usergraph[i].begin()){
answer = i;
i = n;
}
}
return answer;
}
/*
void BFS(double u, double userfriend)
{
// Mark all the vertices as not visited
int j = 0;
int s = sequencialSearch(u);
bool *visited = new bool[n];
int *degree = new int[n];
for(int i = 0; i < n; i++) {
visited[i] = false;
degree[i] = 99999;
} // end for
// Create a queue for BFS
list<int> queue;
// Mark the current node as visited and enqueue it
visited[s] = true;
degree[s] = -1;
queue.push_back(s);
// 'i' will be used to get all adjacent vertices of a vertex
list<double>::iterator i;
while(!queue.empty( ))
{
// Dequeue a vertex from queue and print it
s = queue.front();
cout << "Grau do o Vertice: " << u << " para o vertice: " << *usergraph[s].begin( ) << " e' de: " << degree[s] << '\n';
queue.pop_front( );
// Get all adjacent vertices of the dequeued vertex s
// If a adjacent has not been visited, then mark it visited
// and enqueue it
for(i = usergraph[s].begin( ); i != usergraph[s].end( ); ++i)
{
j = sequencialSearch(*i);
if(!visited[j])
{
visited[j] = true;
degree[j] = degree[s]+1;;
queue.push_back(j);
}
}
}
}
*/
int BFS(double u, double userfriend)
{
// Mark all the vertices as not visited
int j = 0;
int s = sequencialSearch(u);
bool *visited = new bool[n];
int *degree = new int[n];
int foundDegree = 999999;
for(int i = 0; i < n; i++) {
visited[i] = false;
degree[i] = 99999;
} // end for
// Create a queue for BFS
list<int> queue;
// Mark the current node as visited and enqueue it
visited[s] = true;
degree[s] = -1;
queue.push_back(s);
// 'i' will be used to get all adjacent vertices of a vertex
list<double>::iterator i;
while(!queue.empty( ))
{
// Dequeue a vertex from queue and print it
s = queue.front( );
//cout << "Grau do o Vertice: " << u << " para o vertice: " << *usergraph[s].begin( ) << " e' de: " << degree[s] << '\n';
if ( *usergraph[s].begin( ) == userfriend ) {
//cout << "Grau do o Vertice: " << u << " para o vertice: " << *usergraph[s].begin( ) << " e' de: " << degree[s] << '\n';
foundDegree = degree[s];
queue.clear( );
} else {
queue.pop_front( );
// Get all adjacent vertices of the dequeued vertex s
// If a adjacent has not been visited, then mark it visited
// and enqueue it
i = usergraph[s].begin( );
advance(i, 1);
for(; i != usergraph[s].end( ); ++i)
{
j = sequencialSearch(*i);
if(!visited[j])
{
visited[j] = true;
degree[j] = degree[s]+1;;
queue.push_back(j);
}
} // end for
} // end else
}
return foundDegree;
} // end BFS
}; // end class graph
int main ()
{
//////////////////////// TESTING AREA \\\\\\\\\\\\\\\\\\\\\\\
graph g;
g.addEdge(1,2);
g.addEdge(2,3);
g.addEdge(3,4);
g.printGraph( );
cout << "Distancia: " << g.BFS(1,4);
cout << '\n';
//g.BFS(3);
cout << '\n';
//int tmp = g.sequencialSearch(1233);
//cout << g.n << '\n';
return 0;
}
// github.com/castrob/ac3_filcker/TODO.txt to task list