Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

55
Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010

Transcript of Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Page 1: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Chapter 9Priority Queues, Heaps, Graphs

1

Fall 2010

Page 2: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Priority Queue

2

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

Page 3: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Applications of Priority Queue

3

Telephone Answering System Priority: the length of waiting (FIFO queue)

Airline Check-In System Priority: Travel Class

Hospital Emergency Room Priority: Severity of Injury

Page 4: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Implementation of a priority queue

9-4

There are many ways to implement a priority queue An Unsorted List

Enqueue: O(1) Dequeue: searching through the entire list O(N)

An Array-Based Sorted List Enqueue: O(N) Dequeue: O(1)

A Linked Sorted List Enqueuing again is O(N) Dequeue: O(1)

A Binary Search Tree On average, O(log2N) steps for both enqueue and dequeue

Any other choice?

Page 5: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Heap

5

A complete binary tree, where each node contains a value that is greater than or equal to the value of each of its children

Page 6: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Heaps

6

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

Page 7: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Heaps

7

C

A T

treePtr

50

20

18

30

10

Are these heaps? Shape & Order Properties

treePtr

Page 8: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Heaps

8

70

60

40 30

12

8 10

treePtr

Is this a heap? Shape & Order Properties

Page 9: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Heaps

9

70

60

40 30

12

8

treePtr

Can we use this fact to implement an efficient Priority Queue?

Whereis the

largestelement?

Page 10: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Heaps

10

Heap is good choice for implementing Priority Queue.

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 11: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Heaps

11

Shape propertyis restored. Canorder property be restored?

How?

Page 12: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

ReheapDown( )

12

Function: Restores the order property of heaps to the tree between root and bottom.

Precondition: The order property of heaps is violated only by the root node of the tree.

Postcondition: The order property applies to all elements of the heap.

Page 13: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

ReheapDown: illustration

13

Page 14: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

ReheapDown()

14

If the root is not a leaf nodeIf the value of the root is smaller than its largest child

Swap the value of the root with the value of this child.

ReheapDown the subtree with the new root value.

Why just compare the root with its children? How about the rest of the tree? Precondition

Base Cases The root of the current subtree is a leaf node. The value of the root is greater or equal to the values in both

its children.

Page 15: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Insert item into heap

15

Add ‘K’: Where must the new node be put?

Shape Property is kept, but Order Property?

Page 16: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

ReheapUp

16

Function: Restores the order property to the heap between bottom and root.

Precondition: The order property is satisfied from the root of the heap through the next-to-last leaf node; the last (bottom) leaf node violates the order property.

Postcondition: The order property applies to all elements of the heap from root through bottom.

Page 17: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

17

Page 18: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

If the root is not reached

If the value of the bottom node is larger than the value of its parent

Swap the value of the bottom node with the value of its parent

ReheapUp the subtree with the new bottom node.

Why just compare the “bottom” node with its parent? How about the rest of the tree? Precondition

Base Cases Reach the root of the tree The value of the bottom node is smaller or equal to the

values of its parent.

ReheapUp()

18

Page 19: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Heaps: how to store a heap?

19

70

0

60

1

40

3

30

4

12

2

8

5

root Number nodesleft to rightby level

Page 20: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Implementation of Heaps

20

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 indexbottom

root

Page 21: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Review

21

For any node tree.nodes[index] its left child is in tree.nodes[index*2 + 1] its right child is in tree.nodes[index*2 + 2] its parent is in tree.nodes[(index - 1)/2]

Page 22: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Heap: a data structure

22

struct HeapType

{

void ReheapDown(int root, int bottom);

void ReheapUp(int root, int bottom);

ItemType* elements; // Dynamic array

int numElements;

};

Page 23: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Recursive ReheapDown

23

void HeapType::ReheapDown(int root, int bottom){ int maxChild, rightChild, leftChild ;

leftChild = root * 2 + 1 ; rightChild = root * 2 + 2 ; if ( leftChild < = bottom) //root has a left child, i.e., it is not a leaf node { //find the largest child if (leftChild == bottom) //only one child maxChild = leftChild; else { //find the larger child if (elements[leftChild] .ComparedTo(elements[rightChild])!= GREATER) maxChild = rightChild; else maxChild = leftChild; } if (elements[root] .ComparedTo(elements[maxChild])== LESS) //violates the order property { Swap(elements[root], elements[maxChild]); ReheapDown(maxChild, bottom); } }}

Page 24: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Recursive ReheapUp

24

void HeapType::ReheapUp(int root, int bottom)

{

int parent;

if (bottom > root) //haven’t reached root yet

{

parent = ( bottom - 1 ) / 2;

if (elements[parent].ComparedTo(elements[bottom])== LESS)

{ //violates the order property

Swap(elements[parent], elements[bottom]);

ReheapUp(root, parent);

}

}

}

Page 25: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Heaps/Priority Queues

25

How can heaps be used to implement Priority Queues?

Enqueue() Insert a node as the right-most leaf nodeApply the order property by calling ReheapUp()

Dequeue()Remove the root nodeMove the right-most leaf node to the root positionApply the order property by calling ReheapDown()

Page 26: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Why Heap is Good

26

Heap’s Order Property: Root has the largest value / highest priority

Heap’s Shape Property: Min. number of levels with N nodes of a binary

tree: log2N +1 A heap guarantees O(log2N) steps, even in the worst case

A complete tree is of minimum height. At most log2N levels exist above the leaf node. At most log2N levels exist below the root.

Page 27: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Comparison of Priority Queue Implementations

27

 

  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 28: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

GraphsGraphA data structure that consists of a set of nodes and a set of edges that relate the nodes to each otherVertexA node in a graphEdge (arc) A connection between two nodes in a graph,represented by a pairof vertices.

Adjacent verticesTwo vertices in a graph that are connected by an edge

28

Page 29: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

GraphsUndirected 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

29

Page 30: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Graphs

Formally a graph G is defined as follows

G = (V,E)where

V(G) is a finite, nonempty set of verticesE(G) is a set of edges (written as pairs of vertices)

30

Page 31: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Graphs

Vertex

Edge or arc

UndirectedGraphs have noarrows on theedges

31

Page 32: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Graphs

Directed edge

32

Page 33: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Graphs

What otherstructure is this ?

A tree is a acyclic graph.

33

Page 34: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Complete Graphs

How many edges ina directed graph with

N vertices?

How many edges in anundirected graph with

N vertices?

34

A graph in which every vertex is directly connected to every other vertex

Page 35: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Weighted Graphs

Weight

35

A graph in which each edge carries a value

Page 36: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Array-Based ImplementationAdjacency 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

36

Page 37: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Adjacency Matrix for Flight Connections

Where could marking variable be kept?37

Page 38: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Linked Implementation

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

38

Page 39: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Adjacency List Representation of Graphs

Where could a markingvariable go?

39

Page 40: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Graph Algorithms

Breadth-first search algorithmVisit all the nodes on one level before going to the next level

Depth-first search algorithmVisit all the nodes in a branch to its deepest point before moving up

Single-source shortest-path algorithmAn algorithm that displays the shortest path from a designated starting node to every other node in the graph

40

Page 41: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Graph Traversal Visits all the vertices, beginning with a

specified start vertex. No vertex is visited more than once, and

vertices are only visited if they can be reached – that is, if there is a path from the start vertex.

41

Page 42: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Breadth-First Traversal with Queue “Neighbors-First”. A queue data structure is needed. It holds

a list of vertices which have not been visited yet but which should be visited soon.

When visiting a vertex, add its neighbors to the queue. Neighbors are not added to the queue if they are already in the queue, or have already been visited.

Queue: FIFO.

42

Page 43: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

43

Graphs

We need to "mark" a vertex as visited ClearMarks MarkVertex(VertexType, vertex) Boolean IsMarked(VertexType, vertex)

Page 44: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Graphs

BFT result

1

2

3

4

5

6

7

44

Page 45: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

45

BreadthFirstSearchSet 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 46: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Depth-First Traversal with Stack “Neighbors-First”. A stack data structure is needed. It holds a list

of vertices which have not been visited yet but which should be visited soon.

When visiting a vertex, adds its neighbors to the stack. Neighbors are not added to the stack if they are already in the stack, or have already been visited.

Stack: LIFO.

46

Page 47: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Graphs

DFT result

1

2

3

4

5

6

7

47

Page 48: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

48

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 49: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Breadth-First vs. Depth-First Breadth-first Traversal: all one-flight solutions,

then all two-flight solutions, etc.

Austin

Dallas Houston Denver Chicago Atlanta Washington

49

Page 50: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Breadth-First vs. Depth-First Depth-first Traversal: keep going forward in one

direction; backtrack only when reach a dead end.

AustinDallasChicagoDenverAtlantaWashingtonHouston

50

Page 51: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Single-Source Shortest-Path A path through the graph is a sequence (v1, ..., vn) such that

the graph contains an edge e1 going from v1 to v2, an edge

e2 going from v2 to v3, and so on. Shortest-Path: the path whose weights have the smallest

sum.

If the weight is the distance between two cities shortest path: the route with minimum travelling

distance.

If the weight is the travelling time between two cities

shortest path: the route with shortest travelling time.

51

Page 52: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Applications of Shortest Path Traversal Google Maps finds the route with the shortest

travelling time. Networking Routing Protocols Games

52

Page 53: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Dijkstra's Algorithm Similar to Breath-First Traversal A FIFO queue for unvisited neighbors of a

visited vertex. A priority queue for the possible path

Each item of the priority queue has three data members fromVertex: last visited vertex on the path from the

starting vertex toVertex: next vertex to visit Distance : the min. distance from the starting vertex to

next vertex.

53

Page 54: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Dijkstra's Algorithm The priority-queue implemented with a

minimum heap Starts from the Starting vertex, the first edge

is itself (distance is 0). For each visited vertex, put its unvisited

neighbors into FIFO queue. Dequeue the FIFO queue and put the

possible path from the starting vertex to the neighbor into the priority queue.

Dequeue the priority queue one by one.

54

Page 55: Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.

Graphs

Start Here

Washington -> Atlanta 600

Washington ->Dallas 1300

Washington -> Atlanta -> Houston 1400

Washington -> Dallas -> Austin 1500

Washington -> Dallas -> Denver 2080

Washington -> Dallas -> Chicago 220055