Data Structures Algorithm Theory WS 2012/13 Fabian Kuhn Examples - - PowerPoint PPT Presentation

data structures
SMART_READER_LITE
LIVE PREVIEW

Data Structures Algorithm Theory WS 2012/13 Fabian Kuhn Examples - - PowerPoint PPT Presentation

Chapter 4 Data Structures Algorithm Theory WS 2012/13 Fabian Kuhn Examples Dictionary: Operations: insert( key,value ), delete( key ), find( key ) Implementations: Linked list: all operations take time ( : size of data


slide-1
SLIDE 1

Chapter 4

Data Structures

Algorithm Theory WS 2012/13 Fabian Kuhn

slide-2
SLIDE 2

Algorithm Theory, WS 2012/13 Fabian Kuhn 2

Examples

Dictionary:

  • Operations: insert(key,value), delete(key), find(key)
  • Implementations:

– Linked list: all operations take time (: size of data structure) – Balanced binary tree: all operations take log time – Hash table: all operations take 1 times (with some assumptions)

Stack (LIFO Queue):

  • Operations: push, pull
  • Linked list: 1 for both operations

(FIFO) Queue:

  • Operations: enqueue, dequeue
  • Linked list: 1 time for both operations

Here: Priority Queues (heaps), Union‐Find data structure

slide-3
SLIDE 3

Algorithm Theory, WS 2012/13 Fabian Kuhn 3

Dijkstra’s Algorithm

Single‐Source Shortest Path Problem:

  • Given: graph , with edge weights 0 for ∈

source node ∈

  • Goal: compute shortest paths from to all ∈

Dijkstra’s Algorithm:

  • 1. Initialize , 0 and , ∞ for all
  • 2. All nodes are unmarked
  • 3. Get unmarked node which minimizes , :

4. For all , ∈ , , min , , , 5. mark node

  • 6. Until all nodes are marked
slide-4
SLIDE 4

Algorithm Theory, WS 2012/13 Fabian Kuhn 4

Example

∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 13 4 9 1 10 32 23 3 3 8 2 20 1 18 17 1 19 9 1 6 2 2

slide-5
SLIDE 5

Algorithm Theory, WS 2012/13 Fabian Kuhn 5

Example

∞ ∞ ∞ ∞

  • 3

13 4 9 1 10 32 23 3 3 8 2 20 1 18 17 1 19 9 1 6 2 2

slide-6
SLIDE 6

Algorithm Theory, WS 2012/13 Fabian Kuhn 6

Example

  • 3

13 4 9 1 10 32 23 3 3 8 2 20 1 18 17 1 19 9 1 6 2 2

slide-7
SLIDE 7

Algorithm Theory, WS 2012/13 Fabian Kuhn 7

Example

  • 3

13 4 9 1 10 32 23 3 3 8 2 20 1 18 17 1 19 9 1 6 2 2

slide-8
SLIDE 8

Algorithm Theory, WS 2012/13 Fabian Kuhn 8

Example

  • 3

13 4 9 1 10 32 23 3 3 8 2 20 1 18 17 1 19 9 1 6 2 2

slide-9
SLIDE 9

Algorithm Theory, WS 2012/13 Fabian Kuhn 9

Example

  • 3

13 4 9 1 10 32 23 3 3 8 2 20 1 18 17 1 19 9 1 6 2 2

slide-10
SLIDE 10

Algorithm Theory, WS 2012/13 Fabian Kuhn 10

Example

  • 3

13 4 9 1 10 32 23 3 3 8 2 20 1 18 17 1 19 9 1 6 2 2

slide-11
SLIDE 11

Algorithm Theory, WS 2012/13 Fabian Kuhn 11

Example

  • 3

13 4 9 1 10 32 23 3 3 8 2 20 1 18 17 1 19 9 1 6 2 2

slide-12
SLIDE 12

Algorithm Theory, WS 2012/13 Fabian Kuhn 12

Implementation of Dijkstra’s Algorithm

Dijkstra’s Algorithm:

  • 1. Initialize , 0 and , ∞ for all
  • 2. All nodes are unmarked
  • 3. Get unmarked node which minimizes , :

4. For all , ∈ , , min , , , 5. mark node

  • 6. Until all nodes are marked
slide-13
SLIDE 13

Algorithm Theory, WS 2012/13 Fabian Kuhn 13

Priority Queue / Heap

  • Stores (key,data) pairs (like dictionary)
  • But, different set of operations:
  • Initialize‐Heap: creates new empty heap
  • Is‐Empty: returns true if heap is empty
  • Insert(key,data): inserts (key,data)‐pair, returns pointer to entry
  • Get‐Min: returns (key,data)‐pair with minimum key
  • Delete‐Min: deletes minimum (key,data)‐pair
  • Decrease‐Key(entry,newkey): decreases key of entry to newkey
  • Merge: merges two heaps into one
slide-14
SLIDE 14

Algorithm Theory, WS 2012/13 Fabian Kuhn 14

Implementation of Dijkstra’s Algorithm

Store nodes in a priority queue, use , as keys:

  • 1. Initialize , 0 and , ∞ for all
  • 2. All nodes are unmarked
  • 3. Get unmarked node which minimizes , :

4. mark node 5. For all , ∈ , , min , , ,

  • 6. Until all nodes are marked
slide-15
SLIDE 15

Algorithm Theory, WS 2012/13 Fabian Kuhn 15

Analysis

Number of priority queue operations for Dijkstra:

  • Initialize‐Heap:
  • Is‐Empty:
  • Insert:
  • Get‐Min:
  • Delete‐Min:
  • Decrease‐Key:
  • Merge:
  • ||

|| || || ||

slide-16
SLIDE 16

Algorithm Theory, WS 2012/13 Fabian Kuhn 16

Priority Queue Implementation

Implementation as min‐heap:  complete binary tree, e.g., stored in an array

  • Initialize‐Heap:
  • Is‐Empty:
  • Insert:
  • Get‐Min:
  • Delete‐Min:
  • Decrease‐Key:
  • Merge (heaps of size and , ):
slide-17
SLIDE 17

Algorithm Theory, WS 2012/13 Fabian Kuhn 17

Better Implementation

  • Can we do better?
  • Cost of Dijkstra with complete binary min‐heap implementation:

log

  • Can be improved if we can make decrease‐key cheaper…
  • Cost of merging two heaps is expensive
  • We will get there in two steps:

Binomial heap  Fibonacci heap

slide-18
SLIDE 18

Algorithm Theory, WS 2012/13 Fabian Kuhn 18

Definition: Binomial Tree

Binomial tree of order 0 :

slide-19
SLIDE 19

Algorithm Theory, WS 2012/13 Fabian Kuhn 19

Binomial Trees

slide-20
SLIDE 20

Algorithm Theory, WS 2012/13 Fabian Kuhn 20

Properties

  • 1. Tree has 2 nodes
  • 2. Height of tree is
  • 3. Root degree of is
  • 4. In , there are exactly
  • nodes at depth
slide-21
SLIDE 21

Algorithm Theory, WS 2012/13 Fabian Kuhn 21

Binomial Coefficients

  • Binomial coefficient:
  • : # of element subsets of a set of size
  • Property:
  • Pascal triangle:
slide-22
SLIDE 22

Algorithm Theory, WS 2012/13 Fabian Kuhn 22

Number of Nodes at Depth in

Claim: In , there are exactly

  • nodes at depth
slide-23
SLIDE 23

Algorithm Theory, WS 2012/13 Fabian Kuhn 23

Binomial Heap

  • Keys are stored in nodes of binomial trees of different order

nodes: there is a binomial tree or order iff bit of base‐2 representation of is 1.

  • Min‐Heap Property:

Key of node keys of all nodes in sub‐tree of

slide-24
SLIDE 24

Algorithm Theory, WS 2012/13 Fabian Kuhn 24

Example

  • 10 keys: 2, 5, 8, 9, 12, 14, 17, 18, 20, 22, 25
  • Binary representation of : 11 1011

 trees , , and present

14 25 5 20 12 22 9 18 2 8 17

slide-25
SLIDE 25

Algorithm Theory, WS 2012/13 Fabian Kuhn 25

Child‐Sibling Representation

Structure of a node:

  • parent

key degree child sibling

slide-26
SLIDE 26

Algorithm Theory, WS 2012/13 Fabian Kuhn 26

Link Operation

  • Unite two binomial trees of the same order to one tree:

⨁ ⇒

  • Time:

 12  18   20  15 22   40   25 30  

slide-27
SLIDE 27

Algorithm Theory, WS 2012/13 Fabian Kuhn 27

Merge Operation

Merging two binomial heaps:

  • For , , … , :

If there are 2 or 3 binomial trees : apply link operation to merge 2 trees into one binomial tree

Time:

slide-28
SLIDE 28

Algorithm Theory, WS 2012/13 Fabian Kuhn 28

Example

14 25 5 20 12 22 9 18 2 8 17 13

slide-29
SLIDE 29

Algorithm Theory, WS 2012/13 Fabian Kuhn 29

Operations

Initialize: create empty list of trees Get minimum of queue: time 1 (if we maintain a pointer) Decrease‐key at node :

  • Set key of node to new key
  • Swap with parent until min‐heap property is restored
  • Time: log

Insert key into queue :

  • 1. Create queue of size 1 containing only
  • 2. Merge and
  • Time for insert: log
slide-30
SLIDE 30

Algorithm Theory, WS 2012/13 Fabian Kuhn 30

Operations

Delete‐Min Operation:

  • 1. Find tree with minimum root
  • 2. Remove from queue  queue ′
  • 3. Children of form new queue ′′
  • 4. Merge queues ′ and ′′
  • Overall time:
slide-31
SLIDE 31

Algorithm Theory, WS 2012/13 Fabian Kuhn 31

Delete‐Min Example

14 25 2 20 12 22 9 18 5 8 17

slide-32
SLIDE 32

Algorithm Theory, WS 2012/13 Fabian Kuhn 32

Complexities Binomial Heap

  • Initialize‐Heap:
  • Is‐Empty:
  • Insert:
  • Get‐Min:
  • Delete‐Min:
  • Decrease‐Key:
  • Merge (heaps of size and , ):