Weighted Graphs and Greedy Algorithms

Similar documents
COMP 182: Algorithmic Thinking Prim and Dijkstra: Efficiency and Correctness

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Transform & Conquer. Presorting

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

COMP 251 Winter 2017 Online quizzes with answers

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee

CS 161: Design and Analysis of Algorithms

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

Department of Computer Science and Engineering Analysis and Design of Algorithm (CS-4004) Subject Notes

Heap: A binary heap is a complete binary tree in which each, node other than root is smaller than its parent. Heap example: Fig 1. NPTEL IIT Guwahati

Week 11: Minimum Spanning trees

Algorithm Design (8) Graph Algorithms 1/2

CSE 100 Minimum Spanning Trees Prim s and Kruskal

The minimum spanning tree problem

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

UNIT 3. Greedy Method. Design and Analysis of Algorithms GENERAL METHOD

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

Greedy Approach: Intro

Minimum Spanning Trees My T. UF

Announcements Problem Set 5 is out (today)!

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

Priority Queues. Priority Queues Trees and Heaps Representations of Heaps Algorithms on Heaps Building a Heap Heapsort.

Module 5 Graph Algorithms

Minimum Spanning Trees

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

22 Elementary Graph Algorithms. There are two standard ways to represent a

Design and Analysis of Algorithms - - Assessment

Design and Analysis of Algorithms

ROOT A node which doesn t have a parent. In the above tree. The Root is A. LEAF A node which doesn t have children is called leaf or Terminal node.

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Chapter 6 Heapsort 1

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

Lecture Summary CSC 263H. August 5, 2016

22 Elementary Graph Algorithms. There are two standard ways to represent a

Undirected Graphs. Hwansoo Han

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Data Structures Brett Bernstein

) $ f ( n) " %( g( n)

Chapter 9 Graph Algorithms

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

CSE 100: GRAPH ALGORITHMS

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

CS521 \ Notes for the Final Exam

Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees

Chapter 9 Graph Algorithms

Chapter 3 Trees. Theorem A graph T is a tree if, and only if, every two distinct vertices of T are joined by a unique path.

7.3 Spanning trees Spanning trees [ ] 61

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

Minimum Spanning Trees

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

( D. Θ n. ( ) f n ( ) D. Ο%

6.1 Minimum Spanning Trees

March 20/2003 Jayakanth Srinivasan,

n 2 ( ) ( ) + n is in Θ n logn

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Week 12: Minimum Spanning trees and Shortest Paths

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

SPANNING TREES. Lecture 21 CS2110 Spring 2016

Spanning Trees. Lecture 20 CS2110 Spring 2015

CSE 100: B+ TREE, 2-3 TREE, NP- COMPLETNESS

Chapter 9. Priority Queue

Minimum-Cost Spanning Tree. Example

Example. Minimum-Cost Spanning Tree. Edge Selection Greedy Strategies. Edge Selection Greedy Strategies

Algorithm Design and Analysis

tree follows. Game Trees

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

COP 4531 Complexity & Analysis of Data Structures & Algorithms

23.2 Minimum Spanning Trees

CS 6783 (Applied Algorithms) Lecture 5

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

Minimum Spanning Trees

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Heaps. Heaps Priority Queue Revisit HeapSort

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

CSci 231 Final Review

Week 9 Student Responsibilities. Mat Example: Minimal Spanning Tree. 3.3 Spanning Trees. Prim s Minimal Spanning Tree.

Minimum Spanning Trees

n 2 C. Θ n ( ) Ο f ( n) B. n 2 Ω( n logn)

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

Exam 3 Practice Problems

Greedy algorithms. Given a problem, how do we design an algorithm that solves the problem? There are several strategies:

ECE 250 Algorithms and Data Structures

Lecture 19. Broadcast routing

Algorithms and Data Structures: Minimum Spanning Trees I and II - Prim s Algorithm. ADS: lects 14 & 15 slide 1

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

( ) n 3. n 2 ( ) D. Ο

Chapter 23. Minimum Spanning Trees

Minimum Spanning Trees

Total Points: 60. Duration: 1hr

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

Transcription:

COMP 182 Algorithmic Thinking Weighted Graphs and Greedy Algorithms Luay Nakhleh Computer Science Rice University

Reading Material Chapter 10, Section 6 Chapter 11, Sections 4, 5

Weighted Graphs In many real-world applications, graphs have weights associated with their nodes, edges, or both. In this set of slides we will focus on weighted graphs where the weights are associated with edges.

Why Weighted Graphs Internet graph: Edge weights could correspond to the amount of data flowing along them or their bandwidth Roadmap graph: Edge weights could correspond to lengths, numbers of gas stations, Food web graph: Edge weights could correspond to the total energy flow between prey and predator Social network graph: Edge weights could correspond to the frequency of contact

Weighted Graphs A weighted graph is a 3-tuple g=(v,e,w), where V is the set of nodes, E is the set of edges, and w:e R (R is the set of reals) is a function that assigns a weight to each edge. The definition applies to both directed and undirected graphs.

Representing Weighted Graphs Weighted graph 0 5 1 2 12 2 3

Representing Weighted Graphs Weighted graph 0 5 1 2 12 2 3 Adjacency matrix 0 1 2 3 0 5 12 1 5 2 2 2 3 12

Representing Weighted Graphs Weighted graph Adjacency matrix Adjacency list 0 5 1 0 1 2 3 0 5 12 0 (1,5) (3,12) 2 12 2 3 1 5 2 2 2 3 12 1 2 3 (0,5) (2,2) (1,2) (0,12)

Trees, Spanning Trees, and Minimum Spanning Trees A tree is a connected, acyclic graph. A spanning tree of a graph g=(v,e) is a connected, acyclic subgraph of g that contains all the nodes in V. The weight of a spanning tree of a weighted graph g=(v,e,w) is the sum of the weights of the edges in the tree. A minimum spanning tree (MST) of a weighted graph g=(v,e,w) is a spanning tree whose weight is minimum among all possible spanning trees of g.

Paths and Distances Paths and connectivity are defined as in the case of unweighted graphs. The weight of a path is the sum of the weights of the edges on the path. If the weight of an edge corresponds to length, then the length of a path is the sum of the lengths of the edges on the path the distance between two nodes is the length of a shortest path between them

Two Central Problems on Weighted Graphs The minimum-spanning tree problem: Input: A weighted graph g=(v,e,w). Output: The set of edges E T of an MST of g. The single source shortest-paths problem: Input: A weighted graph g=(v,e,w) and node i V. Output: The distance d j from node i for every node j V, and a shortest path from i to j.

Two Central Problems on Weighted Graphs It is easy to come up with brute-force algorithms for these two problems. However, these algorithms would be grossly inefficient. Efficient algorithms for both problems exist and they use an algorithmic strategy called greedy algorithms.

Greedy Algorithms A greedy algorithm constructs a solution to an optimization problem through a sequence of steps, each expanding a partially constructed solution obtained so far, until a complete solution to the problem is reached. In each step, the choice must be feasible: it has to satisfy the problem s constraints locally optimal: it has to be the best local choice for adding the next piece to the solution irrevocable: once made, it is not changed in subsequence steps of the algorithm.

The Minimum Spanning Tree Problem Input: A weighted graph g=(v,e,w). Output: The set of edges E T of an MST of g.

The Minimum Spanning Tree Problem Think greedy: feasibility: at every point, the sub-solution must be local optimality: the algorithm chooses the next edge that irrevocable: once an edge has been added to E T, then

The Minimum Spanning Tree Problem: Prim s Algorithm Algorithm 2: Prim. Input: Undirected, weighted graph g =(V,E,w). Output: E T, the edges of an MST of g. V T {x}; // Initialize the nodes of the MST to a randomly chosen nodes x E T ;; for i 1 to V 1 do Let e = {u, v} be a minimum-weight edge among all edges with one endpoint in V T and the other in V \ V T ; V T V T [ {u, v}; E T [ {{u, v}}; E T return E T ;

The Minimum Spanning Tree Problem: Prim s Algorithm Algorithm 2: Prim. Input: Undirected, weighted graph g =(V,E,w). Output: E T, the edges of an MST of g. V T {x}; // Initialize the nodes of the MST to a randomly chosen nodes x E T ;; for i 1 to V 1 do Let e = {u, v} be a minimum-weight edge among all edges with one endpoint in V T and the other in V \ V T ; V T V T [ {u, v}; E T [ {{u, v}}; E T return E T ; What is the running time of Prim s algorithm?

The Heap Data Structure

A heap is a binary tree (every node has 0, 1, or 2 children) with keys assigned to its nodes (one key per node) provided the following two conditions hold: The tree shape requirement: the binary tree is complete, that is, all its levels are full except possibly for the last level, where only some rightmost leaves may be missing. The parental dominance requirement: The key at each node is greater than or equal to the keys of its children. A min-heap is one where the parental dominance requirement means the key of a node is smaller than the keys of its children.

10 10 10 5 7 5 7 5 7 4 2 1 2 1 6 2 1

10 10 10 5 7 5 7 5 7 4 2 1 2 1 6 2 1

The root of a heap always contains its largest element. In a heap, a node along with all its descendants is also a heap. A heap can be implemented as an array by recording its elements in the top-down, left-to-right fashion. It is convenient to store the heap s elements in positions 1 through n, leaving H[0] either unused or putting there a sentinel whose value is greater than every key in the heap.

In the array representation of a heap, the parental node keys will be in the first n/2 positions of the array, while the leaf keys will occupy the last n/2 positions. The children of a key in position i will be in positions 2i and 2i+1 10 5 7 0 1 2 3 4 5 6 10 5 7 4 2 1 4 2 1 parents leaves

Bottom-up Heap Construction To construct a heap from a given list of keys, we first initialize the complete binary tree with n nodes by placing keys in the order given, and then heapify the tree. Heapification: Starting with the last parental node, the heapify algorithm checks whether the parental dominance holds for the key at this node. If it does not, the algorithm exchanges the node s key K with the larger key of its children and checks whether the parental dominance holds for K in its new positions. This process continues until the parental dominance requirement for K is satisfied. After completing the heapification of the subtree rooted at the current parental node, the algorithm proceeds to do the same for the node s immediate predecessor. The algorithm stops after this is done for the tree s root.

Bottom-up Heap Construction 2 9 7 6 5 8

Bottom-up Heap Construction 2 2 9 7 9 8 6 5 8 6 5 7

Bottom-up Heap Construction 2 2 2 9 7 9 8 9 8 6 5 8 6 5 7 6 5 7

Bottom-up Heap Construction 2 2 2 9 7 9 8 9 8 6 5 8 6 5 7 6 5 7 2 9 8 6 5 7

Bottom-up Heap Construction 2 2 2 9 7 9 8 9 8 6 5 8 6 5 7 6 5 7 2 9 9 8 2 8 6 5 7 6 5 7

Bottom-up Heap Construction 2 2 2 9 7 9 8 9 8 6 5 8 6 5 7 6 5 7 2 9 9 9 8 2 8 6 8 6 5 7 6 5 7 2 5 7

Bottom-up Heap Construction: The Pseudo-code HeapBottomUp Input: An array H[1..n] of orderable items Modifies: Array H[1..n] is arranged in-place to reflect the heap properties Output: None For i n/2 to 1 k i; v H[k]; heap false; While ( (not heap) and 2*k n) j 2*k; If j<n If H[j]<H[j+1] j j+1; If v H[j] heap true; Else H[k] H[j]; k j; H[k] v

Bottom-up Heap Construction: Computational Complexity Assume, for simplicity, that n=2 k -1 (for some natural number k). Let h be the height of the tree; then, h = log 2 n. In the worst case, each key on level i of the tree will travel to the leaf level h. Since moving to the next level down requires two comparisons, the total number of key comparisons involving a key on level i will be 2(h-i). Therefore, the total number of key comparisons in the worst case will be C worst (n) = hx 1 X 2(h i) = hx 1 2(h i)2 i = 2(n log 2 (n + 1)) i=0 level i keys i=0

Inserting a Key Into a Heap First, attach a new node with key K in it after the last leaf of the existing heap. Then, sift K up to its appropriate place in the new heap as follows. Compare K with its parent s key: if the parent s key is greater than or equal to K, stop; otherwise, swap these two keys and compare K with its new parent. This swapping continues until K is not greater than its last parent or it reaches the root. 9 6 8 2 5 7

Inserting a Key Into a Heap First, attach a new node with key K in it after the last leaf of the existing heap. Then, sift K up to its appropriate place in the new heap as follows. Compare K with its parent s key: if the parent s key is greater than or equal to K, stop; otherwise, swap these two keys and compare K with its new parent. This swapping continues until K is not greater than its last parent or it reaches the root. 9 6 8 2 5 7 10

Inserting a Key Into a Heap First, attach a new node with key K in it after the last leaf of the existing heap. Then, sift K up to its appropriate place in the new heap as follows. Compare K with its parent s key: if the parent s key is greater than or equal to K, stop; otherwise, swap these two keys and compare K with its new parent. This swapping continues until K is not greater than its last parent or it reaches the root. 9 9 6 8 6 10 2 5 7 10 2 5 7 8

Inserting a Key Into a Heap First, attach a new node with key K in it after the last leaf of the existing heap. Then, sift K up to its appropriate place in the new heap as follows. Compare K with its parent s key: if the parent s key is greater than or equal to K, stop; otherwise, swap these two keys and compare K with its new parent. This swapping continues until K is not greater than its last parent or it reaches the root. 9 9 10 6 8 6 10 6 9 2 5 7 10 2 5 7 8 2 5 7 8

Inserting a Key Into a Heap What is the running time?

Deleting the Maximum Element From a Heap Exchange the root s key with the last key K of the heap. Decrease the heap s size by 1. Heapify the smaller tree by sifting K down the tree exactly in the same way we did it in the bottom-up heap construction algorithm. That is, verify the parental dominance for K: if it holds, we are done; if not, swap K with the larger of its children and repeat this operation until the parental dominance condition holds for K in its new position.

Deleting the Maximum Element From a Heap 9 8 6 2 5 1

Deleting the Maximum Element From a Heap 9 8 6 2 5 1 1 8 6 2 5 9

Deleting the Maximum Element From a Heap 9 8 6 2 5 1 1 1 8 6 8 6 2 5 9 2 5

Deleting the Maximum Element From a Heap 9 8 6 2 5 1 1 1 8 8 6 8 6 5 6 2 5 9 2 5 2 1

Deleting the Maximum Element From a Heap What is the running time?

Back to Weighted Graph Problems

The Minimum Spanning Tree Problem: Prim s Algorithm Algorithm 2: Prim. Input: Undirected, weighted graph g =(V,E,w). Output: E T, the edges of an MST of g. V T {x}; // Initialize the nodes of the MST to a randomly chosen nodes x E T ;; for i 1 to V 1 do Let e = {u, v} be a minimum-weight edge among all edges with one endpoint in V T and the other in V \ V T ; V T V T [ {u, v}; E T [ {{u, v}}; E T return E T ;

The Minimum Spanning Tree Problem: Prim s Algorithm Algorithm 2: Prim. Input: Undirected, weighted graph g =(V,E,w). Output: E T, the edges of an MST of g. V T {x}; // Initialize the nodes of the MST to a randomly chosen nodes x E T ;; for i 1 to V 1 do Let e = {u, v} be a minimum-weight edge among all edges with one endpoint in V T and the other in V \ V T ; V T V T [ {u, v}; E T [ {{u, v}}; E T return E T ; What is the running time of Prim s algorithm?

The Minimum Spanning Tree Problem: Kruskal s Algorithm Algorithm 3: Kruskal. Input: Undirected, weighted graph g =(V,E,w). Output: E T, the edges of an MST of g. Sort E in non-decreasing order of the weights w(e i1 ) apple w(e i2 ) apple apple w(e i E ); E T ;; numedges 0; // The number of edges in the growing MST k 0; while numedges < V 1 do k k +1; if E T [ {e ik } is acyclic then E T E T [ {e ik }; numedges numedges +1; return E T ;

The Minimum Spanning Tree Problem: Kruskal s Algorithm Algorithm 3: Kruskal. Input: Undirected, weighted graph g =(V,E,w). Output: E T, the edges of an MST of g. Sort E in non-decreasing order of the weights w(e i1 ) apple w(e i2 ) apple apple w(e i E ); E T ;; numedges 0; // The number of edges in the growing MST k 0; while numedges < V 1 do k k +1; if E T [ {e ik } is acyclic then E T E T [ {e ik }; numedges numedges +1; return E T ; What is the running time of Kruskal s algorithm?

The Single-Source Shortest Paths Problem Input: A weighted graph g=(v,e,w) and node i V. Output: The distance d j from node i for every node j V, and a shortest path from i to j.

The Single-Source Shortest Paths Problem Think greedy: feasibility: at every point, the sub-solution must be local optimality: the algorithm chooses the next node that irrevocable: once the distance and shortest path to a node have been set, then

The Single-Source Shortest Paths Problem:Dijkstra s Algorithm Algorithm 1: Dijkstra. Input: Undirected, weighted graph g =(V,E,w); source node i. Output: Shortest paths, as well as their lengths, from i to every other node in g. X ;; foreach j 2 V do d j 1; // d j is the geodesic distance between i and j p j null ; // p j carries the node label of j s parent X X [ {j} ; // Set X contains all nodes not visited yet d i 0; while X 6= ; do Let k be a node with the minimum value of d k in the set X; if d k = 1 then break; X X \{k}; foreach neighbor h of k in the set X do if d k + w((k, h)) <d h then d h d k + w((k, h)); p h k; return p, d;

The Single-Source Shortest Paths Problem:Dijkstra s Algorithm Algorithm 1: Dijkstra. Input: Undirected, weighted graph g =(V,E,w); source node i. Output: Shortest paths, as well as their lengths, from i to every other node in g. X ;; foreach j 2 V do d j 1; // d j is the geodesic distance between i and j p j null ; // p j carries the node label of j s parent X X [ {j} ; // Set X contains all nodes not visited yet d i 0; while X 6= ; do Let k be a node with the minimum value of d k in the set X; if d k = 1 then break; X X \{k}; foreach neighbor h of k in the set X do if d k + w((k, h)) <d h then d h d k + w((k, h)); p h k; return p, d; What is the running time of Dijkstra s algorithm?

Questions?