A apresentação está carregando. Por favor, espere

A apresentação está carregando. Por favor, espere

Heaps Binomiais Rômulo de Almeida Bruno Mestrando em Ciência da Computação Professora Liliane Salgado Disciplina - Algoritmos.

Apresentações semelhantes


Apresentação em tema: "Heaps Binomiais Rômulo de Almeida Bruno Mestrando em Ciência da Computação Professora Liliane Salgado Disciplina - Algoritmos."— Transcrição da apresentação:

1 Heaps Binomiais Rômulo de Almeida Bruno Mestrando em Ciência da Computação Professora Liliane Salgado Disciplina - Algoritmos

2 Agenda Histórico Visão Geral Árvores Binomiais Heap Binomial Operações em Heaps Binomiais Aplicações Referências

3 Histórico “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.

4 Visão Geral Estrutura de dados que faz parte das mergeable heaps (Fibonacci heaps e Soft heaps) e suporta as seguintes operações:Fibonacci heapsSoft heaps ◦MAKE-HEAP() ◦INSERT(H,x) ◦MINIMUM(H) ◦EXTRACT-MIN(H) ◦UNION(H 1,H 2 ) ◦DECREASE-KEY(H,x,k) ◦DELETE(H,x)

5 Visão Geral (Análise Assintótica) ProcedimentoHeap Binária (pior caso) Heap Binomial (pior caso) Heap de Fibonacci (amortizado) MAKE-HEAPΘ(1) INSERTΘ(lg n)O(lg n)Θ(1) MINIMUMΘ(1)*O(lg n)*Θ(1) EXTRACT-MINΘ(lg n) O(lg n) UNIONΘ(n)O(lg n)Θ(1) DECREASE-KEYΘ(lg n) Θ(1) DELETEΘ(lg n) O(lg n) Estrutura de Prioridades => Ruim para Busca

6 Árvores Binomiais Uma Árvore Binomial B k é uma árvore ordenada definida recursivamente: ◦k = 0, um único nó ◦Senão, duas árvores B k-1 ligadas: a raiz de uma é a filha mais a esquerda da outra.

7 Árvores Binomiais Propriedades de B k : ◦possui 2 k nós, ◦a altura da árvore é k, ◦há exatamente nós na profundidade i, onde 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 B i. Corolário: O grau máximo de qualquer nó de uma árvore binomial de n nós é lg n.

8 Heap Binomial 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 2 k nós, teríamos uma árvore para cada bit de n que fosse igual a 1.

9 Heaps são representadas como listas ordenadas (por grau/altura) de árvores binomiais Exemplo: Uma heap binomial com 13 (=1011B) nós Heap Binomial

10 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

11 MAKE-BINOMIAL-HEAP() - O(1) ◦Cria um heap binomial vazio onde o nó inicial é igual a null BINOMIAL-HEAP-MINIMUM(H)- O(lgn) ◦Retorna o menor valor Operações em Heaps Binomiais 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 H.inicio = NIL return H

12 BINOMIAL-HEAP-UNION(H 1,H 2 ) - 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

13 BINOMIAL-HEAP-UNION(H 1,H 2 ) ◦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

14 BINOMIAL-HEAP-UNION(H 1,H 2 ) Operações em Heaps Binomiais 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

15 BINOMIAL-HEAP-MERGE(H1,H2) BINOMIAL-LINK(Y,Z) Operações em Heaps Binomiais y.pai := z y.irmao := z.filho z.filho := y z.grau := z.grau + 1 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

16 BINOMIAL-HEAP-UNION(H 1,H 2 ) Operações em Heaps Binomiais APPLET IMG 1IMG 2

17 BINOMIAL-HEAP-INSERT(H,x) - O(lgn) BINOMIAL-HEAP-EXTRACT-MIN(H) - O(lgn) Operações em Heaps Binomiais H' := makeHeap() x.pai := NIL x.filho := NIL x.irmao := NIL x.grau := 0 H'.inicio := x H := uniao(H,H‘) //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 EXEMPLO

18 BINOMIAL-HEAP-DECREASE- KEY(H,x,k) - O(lgn) BINOMIAL-HEAP-DELETE(H,x) - O(lgn) Operações em Heaps Binomiais 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-DECREASE-KEY(H,x,-∞) BINOMIAL-HEAP-EXTRACT-MIN(H) EXEMPLO

19 Aplicações 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.

20 Referências 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.Introduction to Algorithms 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

21 Dúvidas ?

22 VOLTAR

23

24

25

26


Carregar ppt "Heaps Binomiais Rômulo de Almeida Bruno Mestrando em Ciência da Computação Professora Liliane Salgado Disciplina - Algoritmos."

Apresentações semelhantes


Anúncios Google