Heaps Chapter 6 in CLRS. Motivation Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.

Slides:



Advertisements
Similar presentations
Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Theory of Computing Lecture 8 MAS 714 Hartmut Klauck.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
Binary Heap viewed as an array viewed as a binary tree Left(i) = 2*i Right(i) = 2*i + 1 Parent(i) = i.
Chapter 9 Graph algorithms. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
CSE 421 Algorithms Richard Anderson Dijkstra’s algorithm.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the path begins is the source vertex.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Heapsort Chapter 6. Heaps A data structure with  Nearly complete binary tree  Heap property: A[parent(i)] ≥ A[i] eg. Parent(i) { return } Left(i) {
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Prim’s Algorithm and an MST Speed-Up
1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.
Chapter 9: Graphs Spanning Trees Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort.
Ch. 6: Heapsort n nodes. Heap -- Nearly binary tree of these n nodes (not just leaves) Heap property If max-heap, the max-heap property is that for every.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
Data Structures Haim Kaplan & Uri Zwick December 2013 Binary Heaps 1.
Data Structure & Algorithm II.  In a multiuser computer system, multiple users submit jobs to run on a single processor.  We assume that the time required.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
David Luebke 1 12/23/2015 Heaps & Priority Queues.
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
1 Sections 2.5 and 4.4 Priority Queues and Dijkstra’s Algorithm For Shortest Paths Some Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley.
Graphs Upon completion you will be able to:
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
"Teachers open the door, but you must enter by yourself. "
ליאור שפירא, חיים קפלן וחברים
Single-Source Shortest Path
Heaps, Heap Sort and Priority Queues
Binomial heaps, Fibonacci heaps, and applications
Heapsort.
"Teachers open the door, but you must enter by yourself. "
Shortest Path Algorithms
Shortest path algorithm
Minimum Spanning Tree Algorithms
Minimum Spanning Tree Neil Tang 4/3/2008
Weighted Graphs & Shortest Paths
Autumn 2016 Lecture 10 Minimum Spanning Trees
Shortest Paths.
CSC 380: Design and Analysis of Algorithms
Graph Algorithms: Shortest Path
CSE 417: Algorithms and Computational Complexity
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Winter 2019 Lecture 10 Minimum Spanning Trees
Presentation transcript:

Heaps Chapter 6 in CLRS

Motivation Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees

Motivation Want to find the shortest route from New York to San Francisco Model the road-map with a graph

A Graph G=(V,E) V is a set of vertices E is a set of edges (pairs of vertices)

Model driving distances by weights on the edges V is a set of vertices E is a set of edges (pairs of vertices)

Source and destination V is a set of vertices E is a set of edges (pairs of vertices)

Dijkstra’s algorithm Assume all weights are non-negative Finds the shortest path from some fixed vertex s to every other vertex

s s1s s3s3 s2s2 s4s Example

Maintain an upper bound d(v) on the shortest path to v 0 ∞ ∞ ∞ ∞ s s1s s3s3 s2s2 s4s Example

s s1s s3s3 s2s2 s4s Maintain an upper bound d(v) on the shortest path to v 0 ∞ ∞ ∞ ∞ A node is either scanned ( in S) or labeled ( in Q) Initially S =  and Q = V

s s1s s3s3 s2s2 s4s Pick a vertex v in Q with minimum d(v) and add it to S 0 ∞ ∞ ∞ ∞ Initially S =  and Q = V

s s1s s3s3 s2s2 s4s Pick a vertex v in Q with minimum d(v) and add it to S 0 ∞ ∞ ∞ ∞ Initially S =  and Q = V v w 15 For every edge (v,w) where w in Q relax(v,w)

s s1s s3s3 s2s2 s4s Relax(v,w) If d(v) + w(v,w) < d(w) then d(w) ← d(v) + w(v,w) π(w) ← v 0 ∞ ∞ ∞ ∞ v w For every edge (v,w) where w in Q relax(v,w) 15

s s1s s3s3 s2s2 s4s ∞ ∞ ∞ ∞ S = {s} Relax(s,s 4 )

s s1s s3s3 s2s2 s4s ∞ ∞ ∞ Relax(s,s 4 ) S = {s}

s s1s s3s3 s2s2 s4s ∞ ∞ ∞ Relax(s,s 4 ) Relax(s,s 3 ) S = {s}

s s1s s3s3 s2s2 s4s ∞ ∞ 10 Relax(s,s 4 ) Relax(s,s 3 ) S = {s}

s s1s s3s3 s2s2 s4s ∞ ∞ 10 Relax(s,s 4 ) Relax(s,s 3 ) Relax(s,s 1 ) S = {s}

s s1s s3s3 s2s2 s4s ∞ 10 Relax(s,s 4 ) Relax(s,s 3 ) Relax(s,s 1 ) S = {s}

s s1s s3s3 s2s2 s4s ∞ 10 S = {s} Pick a vertex v in Q with minimum d(v) and add it to S

s s1s s3s3 s2s2 s4s ∞ 10 S = {s,s 1 } Pick a vertex v in Q with minimum d(v) and add it to S

s s1s s3s3 s2s2 s4s ∞ 10 S = {s,s 1 } Pick a vertex v in Q with minimum d(v) and add it to S Relax(s 1,s 3 )

s s1s s3s3 s2s2 s4s ∞ 9 S = {s,s 1 } Pick a vertex v in Q with minimum d(v) and add it to S Relax(s 1,s 3 )

s s1s s3s3 s2s2 s4s ∞ 9 S = {s,s 1 } Pick a vertex v in Q with minimum d(v) and add it to S Relax(s 1,s 3 ) Relax(s 1,s 2 )

s s1s s3s3 s2s2 s4s S = {s,s 1 } Pick a vertex v in Q with minimum d(v) and add it to S Relax(s 1,s 3 ) Relax(s 1,s 2 )

s s1s s3s3 s2s2 s4s S = {s,s 1 } Pick a vertex v in Q with minimum d(v) and add it to S

s s1s s3s3 s2s2 s4s S = {s,s 1,s 2 } Pick a vertex v in Q with minimum d(v) and add it to S

s s1s s3s3 s2s2 s4s S = {s,s 1,s 2 } Pick a vertex v in Q with minimum d(v) and add it to S Relax(s 2,s 3 )

s s1s s3s3 s2s2 s4s S = {s,s 1,s 2 } Pick a vertex v in Q with minimum d(v) and add it to S Relax(s 2,s 3 )

s s1s s3s3 s2s2 s4s S = {s,s 1,s 2 } Pick a vertex v in Q with minimum d(v) and add it to S

s s1s s3s3 s2s2 s4s S = {s,s 1,s 2,s 3 } Pick a vertex v in Q with minimum d(v) and add it to S

s s1s s3s3 s2s2 s4s S = {s,s 1,s 2,s 3,s 4 } Pick a vertex v in Q with minimum d(v) and add it to S

s s1s s3s3 s2s2 s4s S = {s,s 1,s 2,s 3,s 4 } When Q =  then the d() values are the distances from s The π function gives the shortest path tree

s s1s s3s3 s2s2 s4s S = {s,s 1,s 2,s 3,s 4 } When Q =  then the d() values are the distances from s The π function gives the shortest path tree

Implementation of Dijkstra’s algorithm We need to find efficiently the vertex with minimum d() in Q We need to update d() values of vertices in Q

Required ADT Maintain items with keys subject to Insert(x,Q) min(Q) Deletemin(Q) Decrease-key(x,Q,Δ)

Required ADT Insert(x,Q) min(Q) Deletemin(Q) Decrease-key(x,Q,Δ): Can simulate by Delete(x,Q), insert(x-Δ,Q)

Insert(x,Q) min(Q) Deletemin(Q) Decrease-key(x,Q,Δ): Can simulate by Delete(x,Q), insert(x-Δ,Q) How many times we do these operations ? n = |V| n n m = |E|

Do we know an algorithm for this ADT ? Insert(x,Q) min(Q) Deletemin(Q) Decrease-key(x,Q,Δ): Can simulate by Delete(x,Q), insert(x-Δ,Q) Yes! Use balanced binary search trees (RB-trees)

Heap Heap is  A complete binary tree  The items at the children of v are greater than the one at v Heap-ordered tree

Representing a heap with an array  Get the elements from top to bottom, from left to right Array Representation Q

Array Representation Left(i): 2i+1 Right(i): 2i+2 Parent(i):  (i-1)/2  Q

Find the minimum Return Q[0] Q

Delete the minimum Q

Delete the minimum Q

Delete the minimum Q

Delete the minimum Q

Delete the minimum Q

Delete the minimum Q Q[0] ← Q[size(Q)-1] Heapify-down(Q,0) size(Q) ← size(Q)-1

Heapify-down(Q,i) l ← left(i) r ← right(i) smallest ← i if l < size(Q) and Q[l] < Q[smallest] then smallest ← l if r < size(Q) and Q[r] < Q[smallest] then smallest ← r if smallest > i then Q[i] ↔ Q[smallest] Heapify-down(Q, smallest)

Inserting an item Q Insert(15,Q)

Inserting an item Q Insert(15,Q) Q[size(Q)] ← Heapify-up(Q,size(Q)) size(Q) ← size(Q) + 1

Inserting an item Q 29

Inserting an item Q 29

Inserting an item Q 29

Q Heapify-up Heapify-up(Q, i) while i > 0 and Q[i] < Q[parent(i)] do Q[i] ↔ Q[parent(i)] i ← parent(i)

Other operations Decrease-key(x,Q,Δ) Delete(x,Q) Can implement them easily using heapify

Heapsort (Williams, Floyd, 1964) Put the elements in an array Make the array into a heap Do a deletemin and put the deleted element at the last position of the array

Put the elements in the heap Q

Q Make the elements into a heap

Q Heapify-down(Q,4)

Q Heapify-down(Q,4)

Q Heapify-down(Q,3)

Q Heapify-down(Q,3)

Q Heapify-down(Q,2)

Q Heapify-down(Q,2)

Q Heapify-down(Q,1)

Q Heapify-down(Q,1)

Q Heapify-down(Q,1)

Q Heapify-down(Q,0)

Q Heapify-down(Q,0)

Q Heapify-down(Q,0)

Q Heapify-down(Q,0)

We have at most n/2 nodes heapified at height 1 How much time does it take to build the heap this way ? We have at most n/4 nodes heapified at height 2 We have at most n/8 nodes heapified at height 3 Total time =

We have at most n/2 nodes heapified at height 1 How much time does it take to build the heap this way ? We have at most n/4 nodes heapified at height 2 We have at most n/8 nodes heapified at height 3 Total time =

Summary We can build the heap in linear time We still have to deletemin the elements one by one in order to sort that will take O(nlog(n))

An example Given an array of length n, find the k smallest numbers. 77