Heap Binomial

Download Report

Transcript Heap Binomial

Heaps Binomiais
Rômulo de Almeida Bruno
Mestrando em Ciência da Computação
Professora Liliane Salgado
Disciplina - Algoritmos







Histórico
Visão Geral
Árvores Binomiais
Heap Binomial
Operações em Heaps Binomiais
Aplicações
Referências
Agenda

“A Data Structure for Manipulating Priority
Queues”, Jean Vuillemin, 1978 – Université
de Paris-sud, Orsay, France.
◦ ABSTRACT: A data structure is described which can
be used for representing a collection of priority
queues. The primitive operations are insertion,
deletion, union, update, and search for an item of
earliest priority.

“Implementation and analysis of binomial
queue algorithms” , Brown, M.R.
Histórico

Estrutura de dados que faz parte das
mergeable heaps (Fibonacci heaps e Soft
heaps) e suporta as seguintes operações:
◦
◦
◦
◦
◦
◦
◦
MAKE-HEAP()
INSERT(H,x)
MINIMUM(H)
EXTRACT-MIN(H)
UNION(H1,H2)
DECREASE-KEY(H,x,k)
DELETE(H,x)
Visão Geral
Procedimento
MAKE-HEAP
Heap Binária
(pior caso)
Heap Binomial Heap de
(pior caso)
Fibonacci
(amortizado)
Θ(1)
Θ(1)
Θ(1)
Θ(lg n)
O(lg n)
Θ(1)
Θ(1)*
O(lg n)*
Θ(1)
Θ(lg n)
Θ(lg n)
O(lg n)
Θ(n)
O(lg n)
Θ(1)
DECREASE-KEY
Θ(lg n)
Θ(lg n)
Θ(1)
DELETE
Θ(lg n)
Θ(lg n)
O(lg n)
INSERT
MINIMUM
EXTRACT-MIN
UNION
Estrutura de Prioridades => Ruim para Busca
Visão Geral (Análise Assintótica)

Uma Árvore Binomial Bk
é uma árvore ordenada
definida recursivamente:
◦ k = 0, um único nó
◦ Senão, duas árvores
Bk-1 ligadas: a raiz de
uma é a filha mais a
esquerda da outra.
Árvores Binomiais

Propriedades de Bk:
◦ possui 2k nós,
◦ a altura da árvore é k,
◦ há exatamente  k  nós na profundidade i, onde
i 
i = 0,...,k
◦ a raiz tem grau k (maior grau); se os nós filhos da
raiz fossem numerados da esquerda para a direita
por k-1, k-2, ..., 0, um dado nó i é a raiz de uma
sub-árvore Bi.

Corolário: O grau máximo de qualquer nó de
uma árvore binomial de n nós é lg n.
Árvores Binomiais

Um Heap Binomial H é um conjunto de
árvores binomiais com as seguintes
propriedades:
◦ cada árvore é ordenada como um heap mínino (ou
máximo)
◦ Há no máximo uma árvore binomial em H com uma
raíz de um determinado grau.
 Se H tem n nós, então ela contém no máximo lg n +
1 árvores binomiais. Prova: observe que, em binário, n
tem lg n + 1 bits. Como cada árvore binomial de
ordem k tem 2k nós, teríamos uma árvore para cada
bit de n que fosse igual a 1.
Heap Binomial
•
•
Heap Binomial
Heaps são representadas como
listas ordenadas (por grau/altura)
de árvores binomiais
Exemplo: Uma heap binomial com
13 (=1011B) nós







Criar novo Heap Binomial
Encontrar chave mínima
Unir dois Heaps Binomiais
Inserir nó
Extrair nó com chave mínima
Decrementar chave
Apagar chave
Operações em Heaps Binomiais

MAKE-BINOMIAL-HEAP() - O(1)
◦ Cria um heap binomial vazio onde o nó inicial é
igual a null
H.inicio = NIL
return H

BINOMIAL-HEAP-MINIMUM(H)- O(lgn)
◦ Retorna o menor valor
y := NIL
x := H.inicio
min := infinity
while x <> NIL
do if x.chave < min
then min := x.chave
y := x
x := x.irmao
return y
Operações em Heaps Binomiais

BINOMIAL-HEAP-UNION(H1,H2) - O(lgn)
◦ 2 partes:
 Criar um heap resultante com o merge (H1, H2).
 Executar um laço até que esse novo heap tenha
todas as sub árvores em ordem crescente por
grau e que nenhuma sub árvore tenha mesmo
grau que outra.
Operações em Heaps Binomiais

BINOMIAL-HEAP-UNION(H1,H2)
◦ 4 casos (x = nó inicial do heap):
 1 (x.degree != next-x.degree): os ponteiros se
deslocam uma posiçao mais baixo na lista de raízes.
Ou seja, x passa a apontar para seu irmao.
 2 (x.degree = next-x.degree = next-x.irmao.degree):
os ponteiros se movem uma posição mais abaixo na
lista, e a próxima iteração executa o caso 3 ou o caso
4.
 3 (x.degree = next-x.degree != next-x.irmao.degree &
x.key <= next-x.key): remove-se next-x da lista de
raízes e a liga-se a x, criando uma árvore B k + 1
 4 (x.degree = next-x.degree != next-x.irmao.degree &
x.key > next-x.key): remove-se x da lista de raízes e a
liga-se a next-x, criando uma árvore B k + 1
Operações em Heaps Binomiais

BINOMIAL-HEAP-UNION(H1,H2)
H:=make-binomial-Heap()
H.inicio := Binomial-Heap-Merge(H1,H2)
if H.inicio = NIL
then return H
x.ant := NIL
x.prox := x.irmao
while x.prox <> NIL
do if (x.grau <> x.prox.grau) or (x.prox.irmao <> NIL
and x.prox.irmao.grau = x.grau)
then x.ant := x
x := x.prox
else if x.chave <= x.prox.chave
then x.irmao := x.prox.irmao
Binomial-Link(x.prox,x)
else if x.ant = NIL
then H.inicio = x.prox
else x.ant.irmao := x.prox
Binomial-Link(x,x.prox)
x := x.prox
x.prox := x.irmao
return H
Operações em Heaps Binomiais

BINOMIAL-HEAP-MERGE(H1,H2)
a = H1.inicio
b = H2.inicio
H1.inicio = minimoGrau(a, b)
if H1.inicio = NIL
return
if H1.inicio = b
then b = a
a = H1.inicio
while b <> NIL
do if a.irmao = NIL
then a.irmao = b
return
else if a.irmao.grau < b.grau
then a = a.irmao
else c = b.irmao
b.irmao = a.irmao
a.irmao = b
a = a.irmao
b=c
BINOMIAL-LINK(Y,Z)
y.pai := z
y.irmao := z.filho
z.filho := y
z.grau := z.grau + 1
Operações em Heaps Binomiais

BINOMIAL-HEAP-UNION(H1,H2)
APPLET
IMG 1
IMG 2
Operações em Heaps Binomiais

BINOMIAL-HEAP-INSERT(H,x) - O(lgn)
H' := makeHeap()
x.pai := NIL
x.filho := NIL
x.irmao := NIL
x.grau := 0
H'.inicio := x
H := uniao(H,H‘)

EXEMPLO
BINOMIAL-HEAP-EXTRACT-MIN(H) EXEMPLO
O(lgn)
//encontrar a raiz x com a chave mínima em H e remover x da lista
H':= makeHeap()
//inverter a ordem da lista ligada de filhos de x, e definir H' apontando
//para o inicio da lista resultante
H:= Uniao(H,H')
return x
Operações em Heaps Binomiais

BINOMIAL-HEAP-DECREASEKEY(H,x,k) - O(lgn)
EXEMPLO
if k > x.chave
then error “new key is greater than current key"
x.chave := k
y := x
z := y.pai
while z <> NIL and y.chave < z.chave
do troca y.chave e z.chave
if y and z have satellite fields, exchange them, too.
y := z
z := y.pai

BINOMIAL-HEAP-DELETE(H,x) - O(lgn)
BINOMIAL-HEAP-DECREASE-KEY(H,x,-∞)
BINOMIAL-HEAP-EXTRACT-MIN(H)
Operações em Heaps Binomiais

Artigo de Vuillemin:
◦ Job scheduling
◦ Discrete simulation languages, where labels
represent the time at which events are to occur,
◦ Various sorting problems
◦ Optimal code constructions
◦ Chartre's prime number generator
◦ Brown's power series multiplication
◦ Numerical analysis algorithms and in graph
algorithms for such problems as finding shortest
paths and minimum cost spanning tree.
Aplicações



Thomas H. Cormen, Charles E. Leiserson, Ronald
L. Rivest, and Clifford Stein. Introduction to
Algorithms, Second Edition. MIT Press and
McGraw-Hill, 2001. ISBN 0-262-03293-7.
Chapter 19: Binomial Heaps, pp.455–475.
Vuillemin, J. (1978). A data structure for
manipulating priority queues. Communications of
the ACM 21, 309-314. Disponível:
http://portal.acm.org/citation.cfm?id=359478
http://www.cse.yorku.ca/~aaw/Sotirios/Binomial
Heap.html
Referências
Dúvidas
VOLTAR
VOLTAR
VOLTAR
VOLTAR
VOLTAR