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.

Slides:



Advertisements
Similar presentations
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
Advertisements

CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Abstract Data Types and Subprograms
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Graphs CS3240, L. grewe.
1 Nell Dale Chapter 9 Trees Plus Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 A Two-Level Binary Expression ‘-’ ‘8’ ‘5’ treePtr INORDER TRAVERSAL : has value 3 PREORDER TRAVERSAL: POSTORDER TRAVERSAL: 8 5 -
Priority Queues, Heaps CS3240, L. grewe 1. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe the.
Heaps CS 308 – Data Structures.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
1 Nell Dale Chapter 9 Trees Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Chapter 9 Heaps and Priority Queues. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape must.
Graphs CS 308 – Data Structures. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Using Search in Problem Solving
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Important Problem Types and Fundamental Data Structures
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Modified from the slides by Sylvia Sorkin, Community College of Baltimore County.
9 Priority Queues, Heaps, and Graphs. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape.
1 Nell Dale Chapter 9 Trees Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Heaps and Priority Queues CS 3358 – Data Structures.
Chapter 9 Abstract Data Types and Algorithms. 2 Abstract Data Types Abstract data type A data type whose properties (data and operations) are specified.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets.
Graphs. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other The set of.
9-1 Abstract Data Types Abstract data type A data type whose properties (data and operations) are specified independently of any particular implementation.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Chapter 8 Abstract Data Types and Subprograms. 2 Chapter Goals Distinguish between an array-based visualization and a linked visualization Distinguish.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
– Graphs 1 Graph Categories Strong Components Example of Digraph
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
Graphs and Shortest Paths Using ADTs and generic programming.
Heaps CS 302 – Data Structures Sections 8.8, 9.1, and 9.2.
Chapter 9 Heaps and Priority Queues Lecture 18. Full Binary Tree Every non-leaf node has two children Leaves are on the same level Full Binary Tree.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Chapter 9 Priority Queues, Heaps, and Graphs
UNIT – III PART - II Graphs By B VENKATESWARLU, CSE Dept.
Csc 2720 Instructor: Zhuojun Duan
Lecture 22 Chapter 9 Sets.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Graphs.
Yan Shi CS/SE 2630 Lecture Notes
Chapter 9 Priority Queues and Sets
Heaps and Priority Queues
Chapter 10 The Graph ADT.
Important Problem Types and Fundamental Data Structures
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
C++ Plus Data Structures
Chapter 9 The Priority Queue ADT
Sets CS3240, L. grewe.
Heaps Chapter 6 Section 6.9.
Presentation transcript:

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 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

3 Goals Define the following terms related to graphs: Directed graphComplete graph Undirected graphWeighted graph VertexAdjacency matrix EdgeAdjacency list Path Implement a graph using an adjacency matrix to represent the edges

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

5 Priority Queue An ADT in which only the item with the highest priority can be accessed

6 Priority Queue Items on a priority queue are made up of 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?

7 Heaps Heap A 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

8 Heaps Heap with letters 'A'.. 'J'

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

10 Heaps C A T treePtr Are these heaps? treePtr

11 Heaps treePtr Is this a heap?

12 Heaps treePtr Can we use this fact to implement an efficient PQ? Where is the largest element?

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?

14 Heaps Shape property is restored. Can order property be restored?

15 Heaps How?

16 Heaps

17 Heaps template struct HeapType { void reheapDown(int root, int bottom); … ItemType* elements; // Dynamic array int numElements; }

18 Heaps root Number nodes left to right by level

19 Heaps tree [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] elements Use number as index

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?

21 Heaps // IMPLEMENTATION OF RECURSIVE HEAP MEMBER FUNCTIONS void HeapType ::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 * ; rightChild = root * ;

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); }

23 Heaps What other operations might our HeapType need?

24 Heaps Add K: Where must the new node be put? Now what?

25 Heaps

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); }

27 Heaps How can heaps be used to implement Priority Queues?

28 Priority Queues class FullPQ(){}; class EmptyPQ(){}; template 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 items; int maxItems; };

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

30 Priority Queues Dequeue Set item to root element from queue Move last leaf element into root position Decrement length items.ReheapDown(0, length-1) Enqueue Increment length Put newItem in next available position items.ReheapUp(0, length-1)

31 Priority Queues template void PQType ::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); }

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

33 Comparison of Priority Queue Implementations EnqueueDequeue HeapO(log 2 N) Linked ListO(N) Binary Search Tree BalancedO(log 2 N) SkewedO(N)

34 Graphs Graph A data structure that consists of a set of models and a set of edges that relate the nodes to each other Vertex A node in a graph Edge (arc) A pair of vertices representing a connection between two nodes in a graph Undirected graph A graph in which the edges have no direction Directed graph (digraph) A graph in which each edge is directed from one vertex to another (or the same) vertex

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)

36 Graphs Vertex Edge or arc Undirected Graphs have no arrows on the edges

37 Graphs Directed edge

38 Graphs What other structure is this ?

39 Graphs Adjacent vertices Two vertices in a graph that are connected by an edge Path A sequence of vertices that connects two nodes in a graph Complete graph A graph in which every vertex is directly connected to every other vertex Weighted graph A graph in which each edge carries a value

40 Graphs How many edges in a directed graph with N vertices? How many edges in an undirected graph with N vertices?

41 Graphs Weight

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

43 Graphs Operations: The usual suspects IsFull IsEmpty Initialize Insert requires two operations Add vertex Add Edge Others? Let's see.

44 Graphs DepthFirstSearch Set found to false stack.Push(startVertex) do stack.Pop(vertex) if vertex = endVertex Write final vertex Set found to true else Write this vertex Push all adjacent vertices onto stack while !stack.IsEmpty() AND !found if (!found) Write "Path does not exist"

45 Graphs Try depth first search from Austin

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

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

48 Graphs BreadthFirstSearch Set found to false queue.Enqueue(startVertex) do queue.Dequeue(vertex) if vertex = endVertex Write final vertex Set found to true else if (vertex is not marked) Mark vertex Write vertex Get a queue of outgoing vertices from vertex while (vertexQ is not empty) vertexQ.Dequeue(Item) if (item is not marked) queue.Enqueue(item) while queue IsEmpty() AND !found if (!found) Write "Path does not exist"

49 Graphs Try depth breadth search from Austin

50 Graphs DFS uses a stack; BFS uses a queue

51 Graphs BFS result

52 Graphs What other operation does the Graph ADT need?

53 Array-Based Implementation Adjacency Matrix For a graph with N nodes, an N by N table that shows the existence (and weights) of all edges in the graph Mapping Array An array that maps vertex names into array indexes Marking Associate a Boolean variable with each vertex: true if visited, false if not yet visited

54 Adjacency Matrix for Flight Connections Where could marking variable be kept?

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

56 Adjacency List Representation of Graphs Where could a marking variable go ?

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 set Subset A set contained within another set

58 Sets Universal set A set that contains all the values of the base type Empty set A set with no members Set union A set made up of all the items in either sets Set intersection A set made up of all the items in both sets Set difference A set made up of all the items in the first set that are not in the second set

59 Sets Given SetA = {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 SetA Give a proper subset of SetB

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

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?

62 Sets If sets A and B are represented as bit vectors, what Boolean operation would implement set union? Set intersection? Set difference?

63 Sets Implicit implementation (List) The items in an instance of a set are on a list that 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?

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?