Skip to content

Commit e306d86

Browse files
committed
bipartite matching finished TODO display in visualizer
1 parent 6af424a commit e306d86

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

algorithms/apareamientos.hpp

+23-7
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
std::vector<int> apareamientos_iniciales(){
1212
// lo mas facil de hacer, pues es agarrar un nodo y juntarlo con su vecino, revisando que no esten apareados anteriormente
1313

14-
std::vector<int> matches(vertices.size(), 0);
14+
std::vector<int> matches(vertices.size(), -1);
1515
for (auto &n : vertices){
1616
for (auto &h : get_vecinos(n)){
17-
if (matches[h] == 0 && matches[n.id] == 0){
17+
if (matches[h] == -1 && matches[n.id] == -1){
1818
matches[h] = n.id;
1919
matches[n.id] = h;
2020
break;
@@ -38,19 +38,29 @@ std::vector<int> bfs(const node &root, std::vector<int> &matches){
3838
caminito_bfs.push_back(s);
3939
queue.pop_back();
4040
for (auto &v : get_vecinos(vertices[s])){
41-
if (!visited[v] && matches[v] == 0)
41+
if (!visited[v] && matches[v] == -1)
4242
queue.push_back(v);
4343
}
4444
}
45-
return caminito_bfs;
45+
// std::vector<int> matches(vertices.size(), 0);
46+
47+
// ahora necesito ver quien esta matcheado con quien
48+
// para eso, tengo dos opciones, generar un vector de aristas y decir que tienen match, mas facil, o
49+
// generar un vector de nodos que digan que esos dos estan mathcheados
50+
for (int i = 1; i < caminito_bfs.size(); i += 2){
51+
matches[caminito_bfs[i - 1]] = matches[caminito_bfs[i]];
52+
matches[caminito_bfs[i]] = matches[caminito_bfs[i - 1]];
53+
}
54+
55+
return matches;
4656
}
4757

4858

4959
node get_first_not_matched_node(std::vector<int> &matches){
5060
node not_matched;
51-
61+
not_matched.id = -2; // if the id is -2 there no more matchings
5262
for (int i = 0; i < matches.size(); ++i){
53-
if (matches[i] == 0){
63+
if (matches[i] == -1){
5464
not_matched = vertices[i];
5565
break;
5666
}
@@ -63,11 +73,17 @@ std::vector<int> apareamientos(){
6373
// primero, tenemos que hacer los apareamientos iniciales
6474
std::vector<int> inital_matches = apareamientos_iniciales();
6575
std::vector<bool> visited(vertices.size(), false);
66-
// std::vector<int> caminito_bfs = bfs(vertices[inital_matches[0]]);
76+
6777
//ahora tenemos que sacar el nuevo camino de aumento con bfs
6878
// lo que le tenemos que pasar es un nodo que no este en initial_matches
79+
6980
node first_not_matches = get_first_not_matched_node(inital_matches);
81+
82+
if (first_not_matches.id == -2)
83+
return inital_matches;
84+
7085
std::vector<int> matches = bfs(first_not_matches, inital_matches);
86+
7187
while (matches != inital_matches){
7288
inital_matches = matches;
7389
node first_not_matches = get_first_not_matched_node(inital_matches);

includes/event_handler_class.hpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,18 @@ void event_handler::select_algorithms(const sf::Event &event, sf::RenderWindow &
115115
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num8) || sf::Keyboard::isKeyPressed(sf::Keyboard::Numpad8)){ //TODO rewirite function for diffent types of LCA
116116
std::vector<int> parents = call_get_parents();
117117
if (!parents.empty()){
118-
node lca = call_lca_lite(parents);
118+
node lca = call_lca(parents);
119119
std::cout << "El LCA es " << lca.id << std::endl;
120120
make_sprite_red(sprites[lca.id]);
121121
}
122122
else
123123
std::cout << "Hay, un ciclo, no es arbol" << std::endl;
124124
}
125+
126+
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Num9) || sf::Keyboard::isKeyPressed(sf::Keyboard::Numpad9)){
127+
std::vector<edge> matches = call_bipartite_matching();
128+
129+
}
125130
}
126131
/**
127132
* @brief handles the interactivity for the visualizer

includes/functions.hpp

+19-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "../algorithms/prim3.hpp"
1212
#include "../algorithms/componentes_conexas.hpp"
1313
#include "../algorithms/LCA.hpp"
14+
#include "../algorithms/apareamientos.hpp"
1415

1516
#define MAX_WEIGHT 25;
1617
//fuciones de ayuda para dibujar y ser interactivos
@@ -283,7 +284,7 @@ std::vector<int> call_get_parents(){
283284
return get_parents(vertices[root_index]);
284285
}
285286

286-
node call_lca_lite(const std::vector<int> &padres){
287+
node call_lca(const std::vector<int> &padres){
287288
int v_id,u_id;
288289
std::cout << "Id del primer nodo: ";
289290
std::cin >> v_id;
@@ -295,6 +296,23 @@ node call_lca_lite(const std::vector<int> &padres){
295296
return factor_deconposition(padres, u, v);
296297
}
297298

299+
std::vector<edge> call_bipartite_matching(){
300+
std::vector<int> matches = apareamientos();
301+
std::vector<edge> bipartite_edges;
302+
303+
int size = matches.size()/2;
304+
for (int i = 0; i < size; ++i){
305+
edge a;
306+
std::vector<int> nodes;
307+
nodes.push_back(i);
308+
nodes.push_back(matches[i]);
309+
a.nodes = nodes;
310+
a.colored = true;
311+
bipartite_edges.push_back(a);
312+
}
313+
return bipartite_edges;
314+
}
315+
298316
//dar parametros para el visualizador para dibujar cosas en lugares random
299317
/**
300318
* @brief displays the components in random positions on the canvas

0 commit comments

Comments
 (0)