chapter 9 priority queues, heaps, and graphs. 2 goals describe a priority queue at the logical level...

64
Chapter 9 Priority Queues, Heaps, and Graphs

Upload: herbert-johnston

Post on 13-Jan-2016

229 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

Chapter 9

Priority Queues, Heaps, and Graphs

Page 2: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

2

Goals

• Describe a priority queue at the logical level and implement a priority queue as a list

• Describe the shape and order property of a heap and implement a heap in a nonlinked tree representation in an array

• Implement a priority queue as a heap

• Compare the implementations of a priority queue using a heap, a linked list, and a binary search tree

Page 3: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

3

Goals

• Define the following terms related to graphs:

Directed graph Complete graph

Undirected graphWeighted graph

Vertex Adjacency matrix

Edge Adjacency list

Path• Implement a graph using an adjacency

matrix to represent the edges

Page 4: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

4

Goals

• Explain the difference between a depth-first and a breadth-first search and implement these searching strategies using stacks and queues for auxiliary storage

• Implement a shortest-path operation, using a priority queue to access the edge with the minimum weight

• Describe a set at the logical level and implement a set both explicitly and implicitly

Page 5: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

5

Priority Queue

Priority QueueAn ADT in which only the item with the highest priority can be accessed

Page 6: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

6

Priority Queue

Items on a priority queue are made up of

<data, priority> pairs

Can you think of how a priority queue might be implemented as

An unsorted list?

An array-based sorted list?

A linked sorted list?

A binary search tree?

Page 7: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

7

Heaps

HeapA complete binary tree, each of whose elements contains a value that is greater than or equal to the value of each of its children

Remember?Complete tree

Page 8: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

8

Heaps

Heap with letters 'A' .. 'J'

Page 9: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

9

Heaps

Another heap with letters 'A' .. 'J'

Page 10: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

10

Heaps

C

A T

treePtr

50

20

18

30

10

Are these heaps?

treePtr

Page 11: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

11

Heaps

70

60

40 30

12

8 10

treePtr

Is this a heap?

Page 12: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

12

Heaps

70

60

40 30

12

8

treePtr

Can we use this fact to implement an efficient PQ?

Whereis the

largestelement?

Page 13: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

13

Heaps

We have immediate access to highest priority item BUT if we remove it, the structure isn't a heap

Can we "fix" the problem efficiently?

Page 14: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

14

Heaps

Shape propertyis restored. Canorder property be restored?

Page 15: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

15

Heaps

How?

Page 16: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

16

Heaps

Page 17: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

17

Heaps

template<class ItemType>

struct HeapType

{

void reheapDown(int root, int bottom);

ItemType* elements; // Dynamic array

int numElements;

}

Page 18: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

18

Heaps

70

0

60

1

40

3

30

4

12

2

8

5

root

Numbernodes

leftto

rightby level

Page 19: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

19

Heaps

70

0

60

1

40

3

30

4

12

2

8

5

tree[ 0 ]

[ 1 ]

[ 2 ]

[ 3 ]

[ 4 ]

[ 5 ]

[ 6 ]

70

60

12

40

30

8

elements

Use number as index

Page 20: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

20

Heaps

ReapDown(heap, root, bottom)

if heap.elements[root] is not a leaf

Set maxChild to index of child with larger value

if heap.elements[root] < heap.elements[maxChild]

Swap(heap.elements[root], heap.elements[maxChild])

ReheapDown(heap, maxChild, bottom)

What variables do we need?

Where are a node's children?

Page 21: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

21

Heaps

// IMPLEMENTATION OF RECURSIVE HEAP MEMBER FUNCTIONSvoid HeapType<ItemType>::ReheapDown(int root, int bottom)// Pre: ItemType overloads the relational operators// root is the index of the node that may violate the // heap order property// Post: Heap order property is restored between root and // bottom

{ int maxChild ; int rightChild ; int leftChild ;

leftChild = root * 2 + 1 ; rightChild = root * 2 + 2 ;

Page 22: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

22

Heaps if (leftChild <= bottom) // ReheapDown continued { if (leftChild == bottom) maxChild = leftChld; else { if (elements[leftChild] <= elements[rightChild]) maxChild = rightChild; else maxChild = leftChild; } if (elements[root] < elements[maxChild]) { Swap(elements[root], elements[maxChild]); ReheapDown(maxChild, bottom); } }}

Page 23: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

23

Heaps

What other operations might our HeapType need?

Page 24: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

24

Heaps

Add K: Where must the new node be put?

Now what?

Page 25: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

25

Heaps

Page 26: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

26

void HeapType::ReheapUp(int root, int bottom)// Pre: ItemType overloads the relational operators// bottom is the index of the node that may violate// the heap order property. The order property is // satisfied from root to next-to-last node.// Post:Heap order property is restored between root and// bottom{ int parent; if (bottom > root) { parent = ( bottom - 1 ) / 2; if (elements[parent] < elements[bottom]) { Swap(elements[parent], elements[bottom]); ReheapUp(root, parent); } }}

Page 27: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

27

Heaps

How can heaps be used to implement Priority Queues?

Page 28: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

28

Priority Queuesclass FullPQ(){};class EmptyPQ(){};template<class ItemType>class PQType{public: PQType(int); ~PQType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType newItem); void Dequeue(ItemType& item);private: int length; HeapType<ItemType> items; int maxItems;};

Page 29: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

29

Priority Queuestemplate<class ItemType>PQType<ItemType>::PQType(int max){ maxItems = max; items.elements = new ItemType[max]; length = 0;}template<class ItemType>void PQType<ItemType>::MakeEmpty(){ length = 0;}template<class ItemType>PQType<ItemType>::~PQType(){ delete [] items.elements;}

Page 30: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

30

Priority Queues

DequeueSet item to root element from queueMove last leaf element into root positionDecrement lengthitems.ReheapDown(0, length-1)

EnqueueIncrement lengthPut newItem in next available positionitems.ReheapUp(0, length-1)

Page 31: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

31

Priority Queues

template<class ItemType>void PQType<ItemType>::Dequeue(ItemType& item){ if (length == 0) throw EmptyPQ(); else { item = items.elements[0]; items.elements[0] = items.elements[length-1]; length--; items.ReheapDown(0, length-1); }}

Page 32: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

32

Priority Queues

template<class ItemType>void PQType<ItemType>::Enqueue(ItemType newItem){ if (length == maxItems) throw FullPQ(); else { length++; items.elements[length-1] = newItem; items.ReheapUp(0, length-1); }}

Page 33: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

33

Comparison of Priority Queue Implementations

 

  Enqueue Dequeue

Heap O(log2N) O(log2N)

Linked List O(N) O(N)

Binary Search Tree

   

Balanced O(log2N) O(log2N)

Skewed O(N) O(N)  

Page 34: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

34

Graphs

GraphA data structure that consists of a set of models and a set of edges that relate the nodes to each otherVertexA node in a graphEdge (arc) A pair of vertices representing a connection between two nodes in a graphUndirected graphA graph in which the edges have no directionDirected graph (digraph) A graph in which each edge is directed from one vertex to another (or the same) vertex

Page 35: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

35

Graphs

Formally a graph G is defined as follows

G = (V,E)where

V(G) is a finite, nonempty set of vertices

E(G) is a set of edges (written as pairs of vertices)

Page 36: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

36

Graphs

Vertex

Edge or arc

UndirectedGraphshave noarrows on theedges

Page 37: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

37

Graphs

Directed edge

Page 38: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

38

Graphs

Whatother

structureis this

?

Page 39: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

39

Graphs

Adjacent verticesTwo vertices in a graph that are connected by an edgePathA sequence of vertices that connects two nodes in a graphComplete graphA graph in which every vertex is directly connected to every other vertexWeighted graphA graph in which each edge carries a value

Page 40: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

40

Graphs

How many edges ina directed graph with

N vertices?

How many edges in anundirected graph with

N vertices?

Page 41: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

41

Graphs

Weight

Page 42: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

42

Graph Algorithms

Depth-first search algorithmVisit all the nodes in a branch to its deepest point before moving up Breadth-first search algorithmVisit all the nodes on one level before going to the next level Single-source shortest-path algorithmAn algorithm that displays the shortest path from a designated starting node to every other node in the graph

Page 43: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

43

Graphs

Operations: The usual suspects

IsFull

IsEmpty

Initialize

Insert requires two operations

Add vertex

Add Edge

Others? Let's see.

Page 44: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

44

Graphs

DepthFirstSearchSet found to falsestack.Push(startVertex)do

stack.Pop(vertex)if vertex = endVertex

Write final vertexSet found to true

elseWrite this vertexPush all adjacent vertices onto stack

while !stack.IsEmpty() AND !foundif (!found)

Write "Path does not exist"

Page 45: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

45

Graphs

Try depth first search from Austin

Page 46: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

46

Graphs

Do you see a problem? Do you have a solution?

Page 47: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

47

Graphs

We need to "mark" a vertex as visited

ClearMarks

MarkVertex(VertexType, vertex)

Boolean IsMarked(VertexType, vertex)

Page 48: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

48

GraphsBreadthFirstSearchSet found to falsequeue.Enqueue(startVertex)do

queue.Dequeue(vertex)if vertex = endVertex

Write final vertexSet found to true

elseif (vertex is not marked)

Mark vertexWrite vertexGet a queue of outgoing vertices from vertexwhile (vertexQ is not empty)

vertexQ.Dequeue(Item)if (item is not marked)

queue.Enqueue(item)while queue IsEmpty() AND !foundif (!found)

Write "Path does not exist"

Page 49: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

49

Graphs

Try depth breadth search from Austin

Page 50: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

50

Graphs

DFS uses a stack; BFS uses a queue

Page 51: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

51

Graphs

BFS result

Page 52: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

52

Graphs

What other operation does the Graph ADT need?

Page 53: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

53

Array-Based Implementation

Adjacency MatrixFor a graph with N nodes, an N by N table that shows the existence (and weights) of all edges in the graphMapping ArrayAn array that maps vertex names into array indexesMarkingAssociate a Boolean variable with each vertex: true if visited, false if not yet visited

Page 54: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

54

Adjacency Matrix for Flight Connections

Where could marking variable be kept?

Page 55: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

55

Linked Implementation

Adjacency List

A linked list that identifies all the vertices to which a particular vertex is connected; each vertex has its own adjacency list

Page 56: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

56

Adjacency List Representation of Graphs

Wherecould

amarkingvariable

go?

Page 57: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

57

Sets

Set

An unordered collection of distinct values (items or components), chosen from the possible values of a single data type, called the component or base type

Base type

The type of the items in the set

Cardinality

The number of items in a setSubsetA set contained within another set

Page 58: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

58

SetsUniversal setA set that contains all the values of the base typeEmpty setA set with no membersSet unionA set made up of all the items in either setsSet intersectionA set made up of all the items in both setsSet differenceA set made up of all the items in the first set that are not in the second set

Page 59: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

59

Sets

GivenSetA = {X, Y, Z, A, B}SetB = {A, C, W, Z}

What is SetA union SetB?What is SetA intersection SetB?What is SetA - SetB (difference)?Give a subset of SetAGive a proper subset of SetB

Page 60: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

60

Sets

Beware – At the Logical Level sets can not contain

duplicates – Inserting a duplicate item into a set does

not change the set–  Sets are not ordered

This does not mean that we cannot implement a set using an ordered structure with duplicates

Page 61: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

61

Sets

Explicit implementation

Each item in the base type has a representation in each instance of a set; the representation is either true (item is in the set) or false (item is not in the set)

Bit Vector 

An array of Boolean flags, with each item in the base type mapped to a specific flag (index) Algorithms use Boolean operations

To what is space for each set instance proportional?

Page 62: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

62

Sets

If sets A and B are represented as bit vectors, what Boolean operation would implement set union?

Set intersection?

Set difference?

Page 63: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

63

Sets

Implicit implementation (List)The items in an instance of a set are on a listthat represents the set; those items that are not on the list are not in the set  Algorithms use ADT List operations

To what is space for each set instance proportional?

Page 64: Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe

64

Sets

If sets A and B are represented as lists, what list operations would implement set union?

Set intersection?

Set difference?

Which list implementation would be more efficient?