Académique Documents
Professionnel Documents
Culture Documents
1.
Problema
2.
Desarrollo
Algorithm Bipartite
Input : Grafo G = {V, E}.
Output: Retorna
true si el grafo es bipartito y f alse en caso
contrario.
1
2
3
4
5
6
7
8
Algorithm DFS(int i)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
status[i] REACHED;
if set[i] = W HIT E then
set[i] BLU E;
end
for j 0 to n 1 do
if status[j] = IN IT IAL then
if set[i] = BLU E then
set[j] RED;
end
else
set[j] BLU E;
end
DF S(j);
end
if status[j] = REACHED set[j] = set[i] then
returnf alse;
end
end
Algorithm Bipartite
Input : Grafo G = {V, E}.
Output: Retorna
true si el grafo es bipartito y f alse en caso
contrario.
1
2
3
4
5
6
7
8
Algorithm BFS(i)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
status[i] REACHED;
if set[i] = W HIT E then
set[i] BLU E;
end
enqueue(i);
while queue is not empty do
for j 0 to n 1 do
if status[j] = IN IT IAL then
status[j] REACHED;
if set[i] = BLU E then
set[j] RED;
end
else
set[j] BLU E;
end
enqueue(j);
end
if status[j] = REACHED set[j] = set[i] then
returnf alse;
end
end
dequeue();
end
y este no ha sido visitado anteriormente este elemento se encola (lnea 16), luego
se verifica que los nodos adyacentes tienen el mismo color y retorna f alse en
tal caso (lneas 18 a 20), finalmente se saca de la cola el ltimo elemento, si no
quedan elementos en la cola termina el algoritmo (lneas 6 y 22).
Tama
no del problema: n2 + 2n. en el caso que usemos una matriz de adyacencia, a esta se suma los arreglos status y set.
Operaci
on b
asica: Comparacin (status[j] = IN IT IAL).
Pn1 Pn
Pk
C(n) = i=1 j=i+1 s=1 1
Pn1 Pn
C(n) = i=1 j=i+1 k
Pn
C(n) = k i=1 (n i)
2
] = k n 2n O(kn2 )
C(n) = k[n(n 1) n(n1)
2
Es importante notar que si k = na ; (a 1 t.q. a N) O(nb ), donde b = a+2,
osea el algoritmo tendra orden exponencial.
Espacio requerido: kn, donde k es la dimension del espacio.
2.1.
An
alisis
Figura 1: Gr
afico comparativo entre ambos metodos