13
9/11/2017 1 Graphs, Trees, Heaps & Priority Queues Graphs and Trees Heaps and Heap Sort Priority Queues using Heaps COSC 320 - Fall 2017 Slide courtesy of Dr. Sang-Eon Park 1 Graphs Directed graph (or digraph) G = (V, E) V: Set of vertex (node) E: Set of edges (ordered vertex pair) Undirected graph G = (V, E) V: Set of vertex E: Set of edges (unordered vertex pair) COSC 320 - Fall 2017 Slide courtesy of Dr. Sang-Eon Park 2 Graphs COSC 320 - Fall 2017 Slide courtesy of Dr. Sang-Eon Park 3 A C B D Directed graph (aka, Digraph) G = (V, E) V = {A, B, C, D, E, F} E = {(A, B), (C, A), (B, D), (B, C), (C, D), (D, C), (B, B) (F, E)} E F Graphs COSC 320 - Fall 2017 Slide courtesy of Dr. Sang-Eon Park 4 A C B D E F Undirected Graph G = (V, E) V = {A, B, C, D, E, F} E = {(A, B), (A, C), (B, D), (B, C), (E, F)} Trees A undirected graph is connected if every pair of vertices is connected by a path. Free Tree is a connected acyclic, undirected graph. Forest a acyclic but possibly disconnected undirected graph Rooted tree a free tree where one of the vertices is distinguished from the other. The distinguished vertex is called the root. COSC 320 - Fall 2017 Slide courtesy of Dr. Sang-Eon Park 5 Trees 6 Free Tree Forest COSC 320 - Fall 2017 Slide courtesy of Dr. Sang-Eon Park

PowerPoint Presentationfaculty.salisbury.edu/~stlauterburg/COSC320/lectures/_fa17_cosc320... · Rooted tree –a free tree where one of ... its children for a min-heap). ... 3 if

  • Upload
    vubao

  • View
    214

  • Download
    1

Embed Size (px)

Citation preview

Page 1: PowerPoint Presentationfaculty.salisbury.edu/~stlauterburg/COSC320/lectures/_fa17_cosc320... · Rooted tree –a free tree where one of ... its children for a min-heap). ... 3 if

9/11/2017

1

Graphs, Trees, Heaps & Priority Queues

Graphs and Trees

Heaps and Heap Sort

Priority Queues using Heaps

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

1

Graphs

Directed graph (or digraph)

G = (V, E)

V: Set of vertex (node)

E: Set of edges (ordered vertex pair)

Undirected graph

G = (V, E)

V: Set of vertex

E: Set of edges (unordered vertex pair)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

2

Graphs

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

3

A

C

B

D

Directed graph (aka, Digraph) G = (V, E)

V = {A, B, C, D, E, F}

E = {(A, B), (C, A), (B, D), (B, C), (C, D), (D, C), (B, B) (F, E)}

E

F

Graphs

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

4

A

C

B

D

E

F

Undirected Graph G = (V, E)

V = {A, B, C, D, E, F}

E = {(A, B), (A, C), (B, D), (B, C), (E, F)}

Trees

A undirected graph is connected if every pair of vertices is connected by a path.

Free Tree – is a connected acyclic, undirected graph.

Forest – a acyclic but possibly disconnected undirected graph

Rooted tree – a free tree where one of the vertices is distinguished from the other. The distinguished vertex is called the root.

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

5

Trees

6

Free Tree Forest

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

Page 2: PowerPoint Presentationfaculty.salisbury.edu/~stlauterburg/COSC320/lectures/_fa17_cosc320... · Rooted tree –a free tree where one of ... its children for a min-heap). ... 3 if

9/11/2017

2

Trees If the last edge on the path from

the root R of a tree to a node is (y, x) then y is the parent of x and x is the child of y.

If two nodes have same parent, they are siblings.

A node that has no children is a leaf node.

A non-leaf node is an internallnode.

The length of the path from the root R to a node x is the depth of x in the tree.

The largest depth of any node in tree T is the height of tree T.

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

7

R

A C DB

GF

H

E

Binary Trees

A binary tree is a tree data structure in which each node has at most two children.

Typically the child nodes are called leftchild and right child.

Binary trees are commonly used to implement binary search trees and

heaps.

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

8

Binary Trees

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

9

Height-Balanced Binary Tree???

Binary Trees

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

10

Height-Balanced Binary Tree!

An empty tree is height-balanced.A non-empty binary tree T is balanced if:1. Left subtree of T is balanced2. Right subtree of T is balanced3. The difference between heights of left subtree

and right subtree is not more than 1.

Binary Tree

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

11

Unbalanced Binary Tree

Sorting with Recursion(Heap Sort: Heap)

The (binary) heap data structure is an arraythat can be viewed as a complete binary tree.

Complete binary tree: A tree that is completely filled on all levels except possibly the lowest, where all the nodes must be as far left as possible.

Heap property: the key of a parent is always greater than or equal to the keys of its children for a max-heap (less than or equal to the keys of its children for a min-heap).

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

12

Page 3: PowerPoint Presentationfaculty.salisbury.edu/~stlauterburg/COSC320/lectures/_fa17_cosc320... · Rooted tree –a free tree where one of ... its children for a min-heap). ... 3 if

9/11/2017

3

Sorting with Recursion(Heap Sort: Heap)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

13

16

14 10

8 7 9 3

2 4 1

16 14 10 8 7 9 3 2 4 1

0 1 2 3 4 5 6 7 8 9

Heap size = 10

Sorting with Recursion(Heap Sort: Heap)

The root of the tree is A[0], and given the index i of a node, the indices of its parent, left child and right child can be computed simply:

Parent (i)

If i is even

Return ((i - 1)/2)

Else

Return (i / 2)

LeftChild (i)

Return (2i + 1)

RightChild (i)

Return (2i + 2)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

14

16

14 10

8 7 9 3

2 4 1

Sorting with Recursion(Heap Sort: Heap Functions)

Since a heap of n elements is based on a complete binary tree, the height is (log2 n).

There are five basic algorithms regarding heap.

1. HEAPIFY: is the key to maintain the heap property.

2. BUILD-HEAP: produces a heap from an unordered input array.

3. HEAPSORT: sorts an array in place.

4. EXTRACT-MAX: extracts the maximum value from the heap

5. INSERT: allows the heap data structure to be used as a priority queue.

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

15

Sorting with Recursion(Heap Sort: Heap: HEAPIFY)

The HEAPIFY function is an important procedure for manipulating heaps.

Its inputs are an array A and an index i into the array.

When HEAPIFY is called, it is assumed that the binary trees rooted at LeftChild(i) and RightChild(i) are heaps, but that A[i] may be smaller than its children, thus violating the heap property.

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

16

Sorting with Recursion(Heap Sort: Heap: HEAPIFY)

HEAPIFY(A, i) {

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L < Heapsize(A) and A[L] > A[i]

4 Largest = L;

5 else

6 Largest = i;

7 if R < Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } // end if

} // end of HEAPIFY

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

17

Sorting with Recursion(Heap Sort: Heap: HEAPIFY)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

18

10

14 7 9 3

2 8 1

10 14 7 9 3 2 8 1

HEAPIFY (A, 0)

0 1 2 3 4 5 6 7 8 9

HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L < Heapsize(A) and A[L] > A[i]

4 Largest = L;

5 else

6 Largest = i;

7 if R < Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY

4

16

4

0

1 2

3 4 5 6

7 8 9

16

Page 4: PowerPoint Presentationfaculty.salisbury.edu/~stlauterburg/COSC320/lectures/_fa17_cosc320... · Rooted tree –a free tree where one of ... its children for a min-heap). ... 3 if

9/11/2017

4

Sorting with Recursion(Heap Sort: Heap: HEAPIFY)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

19

16

10

7 9 3

2 8 1

16 10 7 9 3 2 8 1

HEAPIFY (A, 1)

0 1 2 3 4 5 6 7 8 9

HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L < Heapsize(A) and A[L] > A[i]

4 Largest = L;

5 else

6 Largest = i;

7 if R < Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY14

4

14

0

1 2

3 4 5 6

7 8 9

4

Sorting with Recursion(Heap Sort: Heap: HEAPIFY)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

20

16

14 10

7 9 3

2 1

16 14 10 7 9 3 2 1

HEAPIFY (A, 3)

0 1 2 3 4 5 6 7 8 9

HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L < Heapsize(A) and A[L] > A[i]

4 Largest = L;

5 else

6 Largest = i;

7 if R < Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY

8

4

8

0

1 2

3 4 5 6

7 8 9

4

Sorting with Recursion(Heap Sort: Heap: HEAPIFY)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

21

16

14 10

8 7 9 3

2 4 1

16 14 10 8 7 9 3 2 4 1

0 1 2 3 4 5 6 7 8 9

HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L < Heapsize(A) and A[L] > A[i]

4 Largest = L;

5 else

6 Largest = i;

7 if R < Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY

0

1 2

3 4 5 6

7 8 9

Sorting with Recursion(Heap Sort: Heap: Run Time for HEAPIFY)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

22

HEAPIFY(A, i){

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L < Heapsize(A) and A[L] > A[i]

4 Largest = L;

5 else

6 Largest = i;

7 if R < Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } // end if

} // end of HEAPIFY

(1): constant time

Worst Case(log2n): Height of tree

Sorting with Recursion(Heap Sort: Heap: Build_Heap)

We can use the procedure HEAPIFY in a bottom-up manner to convert an array A[0..n-1] into a heap.

Since elements in the sub-array A[(n/2)..n-1] are all leaves of the tree, The procedure Build-Heap goes through the internal nodes (remaining nodes) of the tree ( A[0..(n/2)-1] ) and runs HEAPIFY on each node.

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

23

Sorting with Recursion(Heap Sort: Heap: Build_Heap)

Build_Heap (A)

{

1 heap_size = |A|

2 For i = ((|A|/2) - 1) downto 0

3 HEAPIFY (A, i)

}

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

24

Page 5: PowerPoint Presentationfaculty.salisbury.edu/~stlauterburg/COSC320/lectures/_fa17_cosc320... · Rooted tree –a free tree where one of ... its children for a min-heap). ... 3 if

9/11/2017

5

Sorting with Recursion(Heap Sort: Heap: Build_Heap)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

25

4

1 3

2 16 9 10

14 8 7

4 1 3 2 16 9 10 14 8 7

0 1 2 3 4 5 6 7 8 9

Build_Heap (A)

{

1 heap_size = |A|

2 For i = ((|A|/2) - 1) down to 0

3 HEAPIFY (A, i)

}

heap_size = 10i = 10/2 – 1 = 4

0

1 2

3 4 5 6

7 8 9

HEAPIFY(A, 4)

Sorting with Recursion(Heap Sort: Heap: Build_Heap)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

26

Build_Heap (A)

{

1 heap_size = |A|

2 For i = ((|A|/2) - 1) down to 0

3 HEAPIFY (A, i)

}

4

1 3

16 9 10

8 7

4 1 3 16 9 10 8 7

0 1 2 3 4 5 6 7 8 9

0

1 2

3 4 5 6

7 8 9

heap_size = 10i = i - 1 = 4 -1=3HEAPIFY(A, 3)

2

14

2 14

Sorting with Recursion(Heap Sort: Heap: Build_Heap)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

27

4

1

14 16 9

2 8 7

4 1 14 16 9 2 8 7

0 1 2 3 4 5 6 7 8 9

Build_Heap (A)

{

1 heap_size = |A|

2 For i = ((|A|/2) - 1) down to 0

3 HEAPIFY (A, i)

}

heap_size = 10i = 3 – 1 = 2

0

1 2

3 4 5 6

7 8 9

HEAPIFY(A, 2)

3

10

3 10

Sorting with Recursion(Heap Sort: Heap: Build_Heap)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

28

4

10

14 9 3

2 8 7

4 10 14 9 3 2 8 7

0 1 2 3 4 5 6 7 8 9

Build_Heap (A)

{

1 heap_size = |A|

2 For i = ((|A|/2) - 1) down to 0

3 HEAPIFY (A, i)

}

heap_size = 10i = i – 1 = 1

0

1 2

3 4 5 6

7 8 9

HEAPIFY(A, 1)

1

16

1 16

Sorting with Recursion(Heap Sort: Heap: Build_Heap)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

29

4

10

14 9 3

2 8

4 10 14 9 3 2 8

0 1 2 3 4 5 6 7 8 9

Build_Heap (A)

{

1 heap_size = |A|

2 For i = ((|A|/2) - 1) down to 0

3 HEAPIFY (A, i)

}

heap_size = 10i = i – 1 = 1

0

1 2

3 4 5 6

7 8 9

HEAPIFY(A, 4)

16

1

16 1

7

7

Sorting with Recursion(Heap Sort: Heap: Build_Heap)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

30

10

14 7 9 3

2 8 1

10 14 7 9 3 2 8 1

0 1 2 3 4 5 6 7 8 9

Build_Heap (A)

{

1 heap_size = |A|

2 For i = ((|A|/2) - 1) down to 0

3 HEAPIFY (A, i)

}

heap_size = 10i = i – 1 = 0

0

1 2

3 4 5 6

7 8 9

HEAPIFY(A, 0)4

16

4 16

Page 6: PowerPoint Presentationfaculty.salisbury.edu/~stlauterburg/COSC320/lectures/_fa17_cosc320... · Rooted tree –a free tree where one of ... its children for a min-heap). ... 3 if

9/11/2017

6

Sorting with Recursion(Heap Sort: Heap: Build_Heap)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

31

16

10

7 9 3

2 8 1

16 10 7 9 3 2 8 1

0 1 2 3 4 5 6 7 8 9

Build_Heap (A)

{

1 heap_size = |A|

2 For i = ((|A|/2) - 1) down to 0

3 HEAPIFY (A, i)

}

heap_size = 10

0

1 2

3 4 5 6

7 8 9

HEAPIFY(A, 1)

4

4

14

14

Sorting with Recursion(Heap Sort: Heap: Build_Heap)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

32

16

14 10

7 9 3

2 1

16 14 10 7 9 3 2 1

0 1 2 3 4 5 6 7 8 9

Build_Heap (A)

{

1 heap_size = |A|

2 For i = ((|A|/2) - 1) down to 0

3 HEAPIFY (A, i)

}

heap_size = 10

0

1 2

3 4 5 6

7 8 9

HEAPIFY(A, 3)

4

4

8

8

Sorting with Recursion(Heap Sort)

The Heap Sort algorithm takes advantage of the heap property.

1. The algorithm starts by building a heap out of the array.

2. Since the root of a heap is the largest element in the heap, we exchange it with the last element in the heap. The largest element is now in its correct final position!

3. Heapify sub array A[1 .. n-1]

4. Call Heap sort recursively with sub-heap

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

33

Sorting with Recursion(Heap Sort)

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

34

Θ(𝑛𝑙𝑜𝑔2𝑛)

Θ(𝑙𝑜𝑔2𝑛)

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

35

4

1 3

2 16 9 10

14 8 7

4 1 3 2 16 9 10 14 8 7

0 1 2 3 4 5 6 7 8 9

heap_size = 10 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

36

14 10

8 7 9 3

2 4

14 10 8 7 9 3 2 4

0 1 2 3 4 5 6 7 8 9

heap_size = 10 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

1

16

16 1

Page 7: PowerPoint Presentationfaculty.salisbury.edu/~stlauterburg/COSC320/lectures/_fa17_cosc320... · Rooted tree –a free tree where one of ... its children for a min-heap). ... 3 if

9/11/2017

7

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

37

10

8 7 9 3

2 4 16

10 8 7 9 3 2 4 16

0 1 2 3 4 5 6 7 8 9

heap_size = 9 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

HEAPIFY(A, 0)1

14

1 14

HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L Heapsize(A) and A[L] >A[i]

4 Largest = L;

5 else

6 Lagest = I;

7 if R Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

38

14

10

7 9 3

2 4 16

14 10 7 9 3 2 4 16

0 1 2 3 4 5 6 7 8 9

heap_size = 9 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

HEAPIFY(A, 1)

1

1

HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L Heapsize(A) and A[L] >A[i]

4 Largest = L;

5 else

6 Lagest = I;

7 if R Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY

8

8

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

39

14

8 10

7 9 3

2 16

14 8 10 7 9 3 2 16

0 1 2 3 4 5 6 7 8 9

heap_size = 9 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

HEAPIFY(A, 3)HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L Heapsize(A) and A[L] >A[i]

4 Largest = L;

5 else

6 Lagest = I;

7 if R Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY

1

1

4

4

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

40

8 10

4 7 9 3

2 16

8 10 4 7 9 3 2 16

0 1 2 3 4 5 6 7 8 9

heap_size = 9 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

14

1

14 1

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

41

8

4 7 9 3

2 14 16

8 4 7 9 3 2 14 16

0 1 2 3 4 5 6 7 8 9

heap_size = 8 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

1

1

HEAPIFY(A, 0)

HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L Heapsize(A) and A[L] >A[i]

4 Largest = L;

5 else

6 Lagest = I;

7 if R Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY

10

10

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

42

10

8

4 7 3

2 14 16

10 8 4 7 3 2 14 16

0 1 2 3 4 5 6 7 8 9

heap_size = 8 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

HEAPIFY(A, 1)

HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L Heapsize(A) and A[L] >A[i]

4 Largest = L;

5 else

6 Lagest = I;

7 if R Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY

1

1 9

9

Page 8: PowerPoint Presentationfaculty.salisbury.edu/~stlauterburg/COSC320/lectures/_fa17_cosc320... · Rooted tree –a free tree where one of ... its children for a min-heap). ... 3 if

9/11/2017

8

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

43

8 9

4 7 1 3

14 16

8 9 4 7 1 3 14 16

0 1 2 3 4 5 6 7 8 9

heap_size = 8 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

10

2

10 2

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

44

8

4 7 1 3

10 14 16

8 4 7 1 3 10 14 16

0 1 2 3 4 5 6 7 8 9

heap_size = 7 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L Heapsize(A) and A[L] >A[i]

4 Largest = L;

5 else

6 Lagest = I;

7 if R Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY

HEAPIFY(A, 0)2

9

2 9

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

45

9

8

4 7 1

10 14 16

9 8 4 7 1 10 14 16

0 1 2 3 4 5 6 7 8 9

heap_size = 7 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L Heapsize(A) and A[L] >A[i]

4 Largest = L;

5 else

6 Lagest = I;

7 if R Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY

HEAPIFY(A, 2)

2

2

3

3

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

46

8 3

4 7 1

10 14 16

8 3 4 7 1 10 14 16

0 1 2 3 4 5 6 7 8 9

heap_size = 7 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

2

2

9

9

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

47

3

4 7 1 9

10 14 16

3 4 7 1 9 10 14 16

0 1 2 3 4 5 6 7 8 9

heap_size = 6 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L Heapsize(A) and A[L] >A[i]

4 Largest = L;

5 else

6 Lagest = I;

7 if R Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY

2

2

HEAPIFY(A, 0)

8

8

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

48

8

3

4 1 9

10 14 16

8 3 4 1 9 10 14 16

0 1 2 3 4 5 6 7 8 9

heap_size = 6 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L Heapsize(A) and A[L] >A[i]

4 Largest = L;

5 else

6 Lagest = I;

7 if R Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY

HEAPIFY(A, 1)

2

2

7

7

Page 9: PowerPoint Presentationfaculty.salisbury.edu/~stlauterburg/COSC320/lectures/_fa17_cosc320... · Rooted tree –a free tree where one of ... its children for a min-heap). ... 3 if

9/11/2017

9

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

49

7 3

4 2 9

10 14 16

7 3 4 2 9 10 14 16

0 1 2 3 4 5 6 7 8 9

heap_size = 6 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

8

1

8 1

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

50

3

4 2 8 9

10 14 16

3 4 2 8 9 10 14 16

0 1 2 3 4 5 6 7 8 9

heap_size = 5 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

1

1

HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L Heapsize(A) and A[L] >A[i]

4 Largest = L;

5 else

6 Lagest = I;

7 if R Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY

HEAPIFY(A, 0)

7

7

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

51

7

3

2 8 9

10 14 16

7 3 2 8 9 10 14 16

0 1 2 3 4 5 6 7 8 9

heap_size = 5 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L Heapsize(A) and A[L] >A[i]

4 Largest = L;

5 else

6 Lagest = I;

7 if R Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY

HEAPIFY(A, 1)

1

1

4

4

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

52

3

1 8 9

10 14 16

3 1 8 9 10 14 16

0 1 2 3 4 5 6 7 8 9

heap_size = 5 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

7

4

7 4

2

2

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

53

3

1 7 8 9

10 14 16

3 1 7 8 9 10 14 16

0 1 2 3 4 5 6 7 8 9

heap_size = 4 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

2

4

2 4

HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L Heapsize(A) and A[L] >A[i]

4 Largest = L;

5 else

6 Lagest = I;

7 if R Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY

HEAPIFY(A, 0)

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

54

3

7 8 9

10 14 16

3 7 8 9 10 14 16

0 1 2 3 4 5 6 7 8 9

heap_size = 4 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

4

2

4 2

1

1

Page 10: PowerPoint Presentationfaculty.salisbury.edu/~stlauterburg/COSC320/lectures/_fa17_cosc320... · Rooted tree –a free tree where one of ... its children for a min-heap). ... 3 if

9/11/2017

10

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

55

7 8 9

10 14 16

7 8 9 10 14 16

0 1 2 3 4 5 6 7 8 9

heap_size = 3 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

1

2

1 2

4

4

HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L Heapsize(A) and A[L] >A[i]

4 Largest = L;

5 else

6 Lagest = I;

7 if R Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY

HEAPIFY(A, 0)

3

3

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

56

7 8 9

10 14 16

7 8 9 10 14 16

0 1 2 3 4 5 6 7 8 9

heap_size = 3 0

1 2

3 4 5 6

7 8 9

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

2

3 2

4

4

1

1

3

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

57

7 8 9

10 14 16

7 8 9 10 14 16

0 1 2 3 4 5 6 7 8 9

heap_size = 2 0

1 2

3 4 5 6

7 8 9

2

1 2

4

4

3

3

1HEAPIFY(A, 0)

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L Heapsize(A) and A[L] >A[i]

4 Largest = L;

5 else

6 Lagest = I;

7 if R Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

58

7 8 9

10 14 16

7 8 9 10 14 16

0 1 2 3 4 5 6 7 8 9

heap_size = 2 0

1 2

3 4 5 6

7 8 9

1

2 1

4

4

3

3

2HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

59

7 8 9

10 14 16

7 8 9 10 14 16

0 1 2 3 4 5 6 7 8 9

heap_size = 1 0

1 2

3 4 5 6

7 8 9

2

1 2

4

4

3

3

1HEAPIFY(A, 0)

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L Heapsize(A) and A[L] >A[i]

4 Largest = L;

5 else

6 Lagest = I;

7 if R Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY

Sorting with Recursion(Heap Sort)

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

60

7 8 9

10 14 16

7 8 9 10 14 16

0 1 2 3 4 5 6 7 8 9

heap_size = 1 0

1 2

3 4 5 6

7 8 9

2

1 2

4

4

3

3

1HEAPIFY(A, 0)

HEAPSORT(A)

{

1 Build_Heap(A);

2 for i = |A|-1 downto 1

{

3 swap(A[0], A[i])

4 heap_size = heap_size – 1;

5 HEAPIFY(A, 0)

}

}

Page 11: PowerPoint Presentationfaculty.salisbury.edu/~stlauterburg/COSC320/lectures/_fa17_cosc320... · Rooted tree –a free tree where one of ... its children for a min-heap). ... 3 if

9/11/2017

11

Priority Queues with Heaps

A priority queue is a data structure for maintaining a set S of elements each with an associated value called key.

We can use a heap to implement a priority queue

A priority queue needs to support the following operations.

1. Insert(S, x) : insert the element x into the set S.

2. Maximum(S): returns the elements of S with the largest key.

3. Extract_Max(S): removes and returns the elements of S with the largest key.

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

61

Priority Queues with Heaps

Applications with priority queues A priority queue can be used to schedule jobs on a shared

computer – each job with priority vector is resided in thepriority queue. A job with highest priority is chosen for nextservice.

A priority queue can also be used in an event-drivensimulator - The item in the priority queue are event to besimulated, each with an associated time of occurrence thatserves as its key. The event must be simulated in order oftheir time of occurrence, since the simulation of an evencan cause other events to be simulated in the future.

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

62

Priority Queues with Heaps (Extract Maximum)

Extract_Max(A)

{

1 if (heap_size < 1)

2 Error “heap underflow”

3 else

{

4 max = A[0];

5 A[0] = A[heap_size - 1];

6 heap_size = heap_size – 1;

7 HEAPIFY (A, 0);

8 return (max);

}

}

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

63

Θ(1)

Θ(𝑙𝑜𝑔2𝑛)

Priority Queues with Heaps (Extract Maximum)

18

16 10

14 7 9 3

2 8 1

18 16 10 14 7 9 3 2 8 1

0 1 2 3 4 5 6 7 8 9 10

4

4

heap_size = 11

max = 18

64COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

0

1 2

3 4 5 6

7 8 9 10

Extract_Max(A)

{

1 if (heap_size < 1)

2 Error “heap underflow”

3 else

{

4 max = A[0];

5 A[0] = A[heap_size -1];

6 heap_size = heap_size – 1;

7 HEAPIFY (A, 0);

8 return (max);

}

}

4

4

Priority Queues with Heaps (Extract Maximum)

10

14 7 9 3

2 8 1

10 14 7 9 3 2 8 1

0 1 2 3 4 5 6 7 8 9 10

4

4

heap_size = 10

max = 18

65COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

0

1 2

3 4 5 6

7 8 9 10

Extract_Max(A)

{

1 if (heap_size < 1)

2 Error “heap underflow”

3 else

{

4 max = A[0];

5 A[0] = A[heap_size -1];

6 heap_size = heap_size – 1;

7 HEAPIFY (A, 0);

8 return (max);

}

}

HEAPIFY(A, 0)4

16

4 16

HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L Heapsize(A) and A[L] >A[i]

4 Largest = L;

5 else

6 Lagest = I;

7 if R Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY

Priority Queues with Heaps (Extract Maximum)

10

7 9 3

2 8 1

10 7 9 3 2 8 1

0 1 2 3 4 5 6 7 8 9 10

heap_size = 10

max = 18

66COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

0

1 2

3 4 5 6

7 8 9

Extract_Max(A)

{

1 if (heap_size < 1)

2 Error “heap underflow”

3 else

{

4 max = A[0];

5 A[0] = A[heap_size -1];

6 heap_size = heap_size – 1;

7 HEAPIFY (A, 0);

8 return (max);

}

}

HEAPIFY(A, 1)

16

4

16 4

HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L Heapsize(A) and A[L] >A[i]

4 Largest = L;

5 else

6 Lagest = I;

7 if R Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY

14

14

Page 12: PowerPoint Presentationfaculty.salisbury.edu/~stlauterburg/COSC320/lectures/_fa17_cosc320... · Rooted tree –a free tree where one of ... its children for a min-heap). ... 3 if

9/11/2017

12

Priority Queues with Heaps (Extract Maximum)

10

7 9 3

2 1

10 7 9 3 2 1

0 1 2 3 4 5 6 7 8 9 10

heap_size = 10

max = 18

67COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

0

1 2

3 4 5 6

7 8 9

Extract_Max(A)

{

1 if (heap_size < 1)

2 Error “heap underflow”

3 else

{

4 max = A[0];

5 A[0] = A[heap_size -1];

6 heap_size = heap_size – 1;

7 HEAPIFY (A, 0);

8 return (max);

}

}

HEAPIFY(A, 3)

16

14

16 14

HEAPIFY(A, i)

{

1 L = LeftChild(i);

2 R = RightChild(i);

3 if L Heapsize(A) and A[L] >A[i]

4 Largest = L;

5 else

6 Lagest = I;

7 if R Heapsize(A) and A[R] > A[Largest]

8 Largest = r;

9 if Largest i

10 {

11 Swap (A[i], A[Largest]);

12 HEAPIFY(A, Largest);

13 } //end if

}// end of HEAPIFY

4

4

8

8

Priority Queues with Heaps (Extract Maximum)

10

7 9 3

2 1

10 8 7 9 3 2 4 1

0 1 2 3 4 5 6 7 8 9 10

heap_size = 10

max = 18

68COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

0

1 2

3 4 5 6

7 8 9

Extract_Max(A)

{

1 if (heap_size < 1)

2 Error “heap underflow”

3 else

{

4 max = A[0];

5 A[0] = A[heap_size -1];

6 heap_size = heap_size – 1;

7 HEAPIFY (A, 0);

8 return (max);

}

}

16

14

16 14

8

4

Heap_Insert inserts a node into heap A.

Increase the heap size by one

Insert new element in the correct location in the heap.

Uses comparisons and swaps to determine the correct location for the new element and insert it.

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

69

Priority Queues with Heaps (Insert)

Heap_Insert (A, key)

{

1 heap_size = heap_size + 1;

2 i = heap_size – 1 // highest index in the heap

3 while i > 0 and A[Parent(i)] < key

{

4 A[i] = A[Parent(i)];

5 i = Parent(i);

}

6 A[i] = key // copy the key value in the right location

}

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

70

Θ(1)

Θ(1)

O(𝑙𝑜𝑔2𝑛)

The running time of Heap_Insert on an n-element heap is O(𝑙𝑜𝑔2𝑛)

Priority Queues with Heaps (Insert)

Priority Queues with Heap (Insert)

10

7 9 3

2 1

10 8 7 9 3 2 4 1

0 1 2 3 4 5 6 7 8 9 10

Initial heap_size = 10

71COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

0

1 2

3 4 5 6

7 8 9

16

14

16 14

8

4

Insert _Heap(A, 15)

Heap_Insert (A, key)

{

1 heap_size = heap_size + 1;

2 i = heap_size – 1

3 while i > 0 and A[Parent(i)] < key

{

4 A[i] = A[Parent(i)];

5 i = Parent(i);

}

6 A[i] = key

}

heap_size = 11

10

15

Priority Queues with Heap (Insert)

10

7 9 3

2 1

10 8 7 9 3 2 4 1

0 1 2 3 4 5 6 7 8 9 10

Initial heap_size = 10

72COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

0

1 2

3 4 5 6

7 8 9

16

14

16 14

8

4

Insert _Heap(A, 15)

Heap_Insert (A, key)

{

1 heap_size = heap_size + 1;

2 i = heap_size – 1

3 while i > 0 and A[Parent(i)] < key

{

4 A[i] = A[Parent(i)];

5 i = Parent(i);

}

6 A[i] = key

}

10 i=10

7

7

heap_size = 11

15

Page 13: PowerPoint Presentationfaculty.salisbury.edu/~stlauterburg/COSC320/lectures/_fa17_cosc320... · Rooted tree –a free tree where one of ... its children for a min-heap). ... 3 if

9/11/2017

13

Priority Queues with Heap (Insert)

14 10

7 9 3

2 1

14 10 8 7 9 3 2 4 1

0 1 2 3 4 5 6 7 8 9 10

Initial heap_size = 10

73COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

0

1 2

3 4 5 6

7 8 9

16

16

8

4

Insert _Heap(A, 15)

Heap_Insert (A, key)

{

1 heap_size = heap_size + 1;

2 i = heap_size – 1

3 while i > 0 and A[Parent(i)] < key

{

4 A[i] = A[Parent(i)];

5 i = Parent(i);

}

6 A[i] = key

}

7

710

i=4

7

15

heap_size = 11

14

14

15

Priority Queues with Heap (Insert)

15 10

14 9 3

2 1

15 10 8 14 9 3 2 4 1

0 1 2 3 4 5 6 7 8 9 10

74COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

0

1 2

3 4 5 6

7 8 9

16

16

8

4

Heap_Insert (A, key)

{

1 heap_size = heap_size + 1;

2 i = heap_size – 1

3 while i > 0 and A[Parent(i)] < key

{

4 A[i] = A[Parent(i)];

5 i = Parent(i);

}

6 A[i] = key

}

7

710

COSC 320 - Fall 2017

Slide courtesy of Dr. Sang-Eon Park

75

Review: Sorting Algorithms

Algorithm Best Case Worst Case

Insertion Θ(𝑛) Θ(𝑛2)

Selection Θ(𝑛2) Θ(𝑛2)

Bubble Θ(𝑛2) Θ(𝑛2)

Shell Θ 𝑛𝑘

1 < 𝑘 < 2

Θ 𝑛𝑘

1 < 𝑘 < 2

Merge Θ(𝑛𝑙𝑜𝑔2𝑛) Θ(𝑛𝑙𝑜𝑔2𝑛)

Heap Θ(𝑛𝑙𝑜𝑔2𝑛) Θ(𝑛𝑙𝑜𝑔2𝑛)

Quick Θ(𝑛𝑙𝑜𝑔2𝑛) Θ(𝑛2)