CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

Similar documents
Divide and Conquer. Recurrence Relations

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30,

CS483 Design and Analysis of Algorithms

CPS 616 DIVIDE-AND-CONQUER 6-1

Introduction to Divide and Conquer

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

Discrete Wiskunde II. Lecture 5: Shortest Paths & Spanning Trees

Divide and Conquer Algorithms

Mergesort and Recurrences (CLRS 2.3, 4.4)

Outline. 1 Introduction. Merging and MergeSort. 3 Analysis. 4 Reference

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

Divide and Conquer Algorithms

data structures and algorithms lecture 2

Algorithms Test 1. Question 1. (10 points) for (i = 1; i <= n; i++) { j = 1; while (j < n) {

Divide and Conquer CPE 349. Theresa Migler-VonDollen

Algorithm Design and Analysis

Lecture 2: Divide and conquer and Dynamic programming

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

COMP 382: Reasoning about algorithms

Asymptotic Analysis and Recurrences

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Recurrence Relations

Algorithm Analysis Divide and Conquer. Chung-Ang University, Jaesung Lee

CS483 Design and Analysis of Algorithms

Question Paper Code :

Divide-and-Conquer and Recurrence Relations: Notes on Chapter 2, Dasgupta et.al. Howard A. Blair

CS 2110: INDUCTION DISCUSSION TOPICS

Shortest Path Algorithms

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

Data Structures in Java

CS2223 Algorithms D Term 2009 Exam 3 Solutions

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

Divide-and-Conquer Algorithms and Recurrence Relations. Niloufar Shafiei

CS173 Running Time and Big-O. Tandy Warnow

Single Source Shortest Paths

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

Divide and Conquer. Andreas Klappenecker

1 Divide and Conquer (September 3)

IS 709/809: Computational Methods in IS Research Fall Exam Review

Algorithm Analysis Recurrence Relation. Chung-Ang University, Jaesung Lee

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

Introduction to Algorithms

Analysis of Algorithms - Using Asymptotic Bounds -

CMPS 2200 Fall Divide-and-Conquer. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

Introduction to Algorithms

CMPT 307 : Divide-and-Conqer (Study Guide) Should be read in conjunction with the text June 2, 2015

NATIONAL UNIVERSITY OF SINGAPORE CS3230 DESIGN AND ANALYSIS OF ALGORITHMS SEMESTER II: Time Allowed 2 Hours

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

CSCI 3110 Assignment 6 Solutions

Divide&Conquer: MergeSort. Algorithmic Thinking Luay Nakhleh Department of Computer Science Rice University Spring 2014

Single Source Shortest Paths

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

Divide and Conquer. Andreas Klappenecker. [based on slides by Prof. Welch]

Inf 2B: Sorting, MergeSort and Divide-and-Conquer

CS 4407 Algorithms Lecture: Shortest Path Algorithms

Contents Lecture 4. Greedy graph algorithms Dijkstra s algorithm Prim s algorithm Kruskal s algorithm Union-find data structure with path compression

Divide-and-Conquer Algorithms Part Two

Omega notation. Transitivity etc.

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17

Design and Analysis of Algorithms

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example

Divide and conquer. Philip II of Macedon

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

A design paradigm. Divide and conquer: (When) does decomposing a problem into smaller parts help? 09/09/ EECS 3101

CS 5321: Advanced Algorithms - Recurrence. Acknowledgement. Outline. Ali Ebnenasir Department of Computer Science Michigan Technological University

Data Structures and Algorithms

Algorithms and Data Structures (COMP 251) Midterm Solutions

Lecture 2: MergeSort. CS 341: Algorithms. Thu, Jan 10 th 2019

ITEC2620 Introduction to Data Structures

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University)

COMP Analysis of Algorithms & Data Structures

CS 5321: Advanced Algorithms Analysis Using Recurrence. Acknowledgement. Outline

COL106: Data Structures and Algorithms (IIT Delhi, Semester-II )

Advanced Analysis of Algorithms - Midterm (Solutions)

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm:

Introduction to Algorithms

COMP Analysis of Algorithms & Data Structures

CMPS 2200 Fall Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk. 10/8/12 CMPS 2200 Intro.

Analysis of Algorithms

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Dynamic Programming: Shortest Paths and DFA to Reg Exps

CPSC 320 (Intermediate Algorithm Design and Analysis). Summer Instructor: Dr. Lior Malka Final Examination, July 24th, 2009

CS Analysis of Recursive Algorithms and Brute Force

Divide-and-conquer: Order Statistics. Curs: Fall 2017

Divide and Conquer Strategy

Divide and Conquer. Arash Rafiey. 27 October, 2016

CSE 421 Algorithms: Divide and Conquer

Recurrence Relations

Review Of Topics. Review: Induction

Outline. Part 5. Computa0onal Complexity (3) Compound Interest 2/15/12. Recurrence Rela0ons: An Overview. Recurrence Rela0ons: Formal Defini0on

Partition and Select

CSE 591 Homework 3 Sample Solutions. Problem 1

Design and Analysis of Algorithms

Algorithms And Programming I. Lecture 5 Quicksort

Solving Recurrences. Lecture 23 CS2110 Fall 2011

Sorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort

Algorithms. Jordi Planes. Escola Politècnica Superior Universitat de Lleida

Data Structures and Algorithms

Transcription:

Greedy s

Greedy s Shortest path Claim 2: Let S be a subset of vertices containing s such that we know the shortest path length l(s, u) from s to any vertex in u S. Let e = (u, v) be an edge such that 1 u S, v V \ S, 2 (l(s, u) + W e ) is the least among all such cut edges. Then l(s, v) = l(s, u) + W e. Dijkstra s (G, s) - S {s} - d(s) 0 - While S does not contain all vertices in G - Let e = (u, v) be a cut edge across (S, V \ S) with minimum value of d(u) + W e - d(v) d(u) + W e - S S {v} What is the running time of the above algorithm? Same as that of the Prim s algorithm. O( E log V ).

Greedy s Shortest path Dijkstra s (G, s) - S {s} - d(s) 0 - While S does not contain all vertices in G - Let e = (u, v) be a cut edge across (S, V \ S) with minimum value of d(u) + W e - d(v) d(u) + W e - S S {v} Figure: d(s) = 0

Greedy s Shortest path Dijkstra s (G, s) - S {s} - d(s) 0 - While S does not contain all vertices in G - Let e = (u, v) be a cut edge across (S, V \ S) with minimum value of d(u) + W e - d(v) d(u) + W e - S S {v} Figure: d(s) = 0; d(a) = 1

Greedy s Shortest path Dijkstra s (G, s) - S {s} - d(s) 0 - While S does not contain all vertices in G - Let e = (u, v) be a cut edge across (S, V \ S) with minimum value of d(u) + W e - d(v) d(u) + W e - S S {v} Figure: d(s) = 0; d(a) = 1; d(e) = 9

Greedy s Shortest path Dijkstra s (G, s) - S {s} - d(s) 0 - While S does not contain all vertices in G - Let e = (u, v) be a cut edge across (S, V \ S) with minimum value of d(u) + W e - d(v) d(u) + W e - S S {v} Figure: d(s) = 0; d(a) = 1; d(e) = 9; d(d) = 15

Greedy s Shortest path Dijkstra s (G, s) - S {s} - d(s) 0 - While S does not contain all vertices in G - Let e = (u, v) be a cut edge across (S, V \ S) with minimum value of d(u) + W e - d(v) d(u) + W e - S S {v} Figure: d(s) = 0; d(a) = 1; d(e) = 9; d(d) = 15; d(c) = 17

Greedy s Shortest path Dijkstra s (G, s) - S {s} - d(s) 0 - While S does not contain all vertices in G - Let e = (u, v) be a cut edge across (S, V \ S) with minimum value of d(u) + W e - d(v) d(u) + W e - S S {v} Figure: d(s) = 0; d(a) = 1; d(e) = 9; d(d) = 15; d(c) = 17; d(b) = 21

Greedy s Shortest path Dijkstra s (G, s) - S {s} - d(s) 0 - While S does not contain all vertices in G - Let e = (u, v) be a cut edge across (S, V \ S) with minimum value of d(u) + W e - d(v) d(u) + W e - S S {v} Figure: The algorithm also implicitly produces a shortest path tree that gives the shortest paths from s to all vertices.

Course Overview Basic graph algorithms Design Techniques: Greedy s Divide and Conquer Dynamic Programming Network Flows Computational Intractability

Introduction You have already seen multiple examples of Divide and Conquer algorithms: Binary Search Quick Sort Multiplying two n-bit numbers in O ( n log 2 3) time.

Main Idea Main Idea: Divide the input into smaller parts. Solve the smaller parts and combine their solution.

Problem Given an array of unsorted integers, output a sorted array. MergeSort(A) - If ( A = 1) return(a) - Divide A into two equal parts A L and A R - B L MergeSort(A L ) - B R MergeSort(A R ) - B Merge(B L, B R ) - return(b)

MergeSort(A) - If ( A = 1) return(a) - Divide A into two equal parts A L and A R - B L MergeSort(A L ) - B R MergeSort(A R ) - B Merge(B L, B R ) - return(b)

MergeSort(A) - If ( A = 1) return(a) - Divide A into two equal parts A L and A R - B L MergeSort(A L ) - B R MergeSort(A R ) - B Merge(B L, B R ) - return(b)

MergeSort(A) - If ( A = 1) return(a) - Divide A into two equal parts A L and A R - B L MergeSort(A L ) - B R MergeSort(A R ) - B Merge(B L, B R ) - return(b)

MergeSort(A) - If ( A = 1) return(a) - Divide A into two equal parts A L and A R - B L MergeSort(A L ) - B R MergeSort(A R ) - B Merge(B L, B R ) - return(b)

MergeSort(A) - If ( A = 1) return(a) - Divide A into two equal parts A L and A R - B L MergeSort(A L ) - B R MergeSort(A R ) - B Merge(B L, B R ) - return(b)

MergeSort(A) - If ( A = 1) return(a) - Divide A into two equal parts A L and A R - B L MergeSort(A L ) - B R MergeSort(A R ) - B Merge(B L, B R ) - return(b)

MergeSort(A) - If ( A = 1) return(a) - Divide A into two equal parts A L and A R - B L MergeSort(A L ) - B R MergeSort(A R ) - B Merge(B L, B R ) - return(b)

MergeSort(A) - If ( A = 1) return(a) - Divide A into two equal parts A L and A R - B L MergeSort(A L ) - B R MergeSort(A R ) - B Merge(B L, B R ) - return(b)

MergeSort(A) - If ( A = 1) return(a) - Divide A into two equal parts A L and A R - B L MergeSort(A L ) - B R MergeSort(A R ) - B Merge(B L, B R ) - return(b)

MergeSort(A) - If ( A = 1) return(a) - Divide A into two equal parts A L and A R - B L MergeSort(A L ) - B R MergeSort(A R ) - B Merge(B L, B R ) - return(b) How do we argue correctness?

MergeSort(A) - If ( A = 1) return(a) - Divide A into two equal parts A L and A R - B L MergeSort(A L ) - B R MergeSort(A R ) - B Merge(B L, B R ) - return(b) How do we argue correctness? Proof of correctness of Divide and Conquer algorithms are usually by induction. Base case: This corresponds to the base cases of the algorithm. For the MergeSort, the base case is that the algorithm correctly sorts arrays of size 1. Inductive step: In general, this corresponds to correctly combining the solutions of smaller subproblems. For MergeSort, this is just proving that the Merge routine works correctly. This may again be done using induction and is left as an exercise.

MergeSort(A) - If ( A = 1) return(a) - Divide A into two equal parts A L and A R - B L MergeSort(A L ) - B R MergeSort(A R ) - B Merge(B L, B R ) - return(b) Let n be a power of 2 (e.g., n = 256) Let T (n) denote the worst case running time for the algorithm. Claim 1: T (1) c for some constant c.

MergeSort(A) - If ( A = 1) return(a) - Divide A into two equal parts A L and A R - B L MergeSort(A L ) - B R MergeSort(A R ) - B Merge(B L, B R ) - return(b) Let n be a power of 2 (e.g., n = 256) Let T (n) denote the worst case running time for the algorithm. Claim 1: T (1) c for some constant c. Claim 2: T (n) 2 T (n/2) + cn for all n 2.

MergeSort(A) - If ( A = 1) return(a) - Divide A into two equal parts A L and A R - B L MergeSort(A L ) - B R MergeSort(A R ) - B Merge(B L, B R ) - return(b) Let n be a power of 2 (e.g., n = 256) Let T (n) denote the worst case running time for the algorithm. Claim 1: T (1) c for some constant c. Claim 2: T (n) 2 T (n/2) + cn for all n 2. T (n) 2 T (n/2) + cn for n 2 and T (1) c is called a recurrence relation for the running time T (n). How do we solve such recurrence relation to obtain the value of T (n) as a function of n?

Let n be a power of 2 (e.g., n = 256) Let T (n) denote the worst case running time for the algorithm. Claim 1: T (1) c for some constant c. Claim 2: T (n) 2 T (n/2) + cn for all n 2. T (n) 2 T (n/2) + cn for n 2 and T (1) c is called a recurrence relation for the running time T (n). How do we solve such recurrence relation to obtain the value of T (n) as a function of n? Unrolling the recursion: Rewrite T (n/2) in terms of T (n/4) and so on until a pattern for the running time with respect to all levels of the recursion is observed. Then, combine these and get the value of T (n).

Recurrence relation for : T (n) 2 T (n/2) + cn for n 2 and T (1) c. How do we solve such recurrence relation to obtain the value of T (n) as a function of n? Unrolling the recursion: Rewrite T (n/2) in terms of T (n/4) and so on until a pattern for the running time with respect to all levels of the recursion is observed. Then, combine these and get the value of T (n).

Recurrence relation for : T (n) 2 T (n/2) + cn for n 2 and T (1) c. How do we solve such recurrence relation to obtain the value of T (n) as a function of n? So, the running time T (n) cn log n = O(n log n).

Solving recurrence relations Question: Suppose there is a divide and conquer algorithm where the recurrence relation for running time T (n) is the following: T (n) 2T (n/2) + cn 2 for n 2, and T (1) c. What is the solution of this recurrence relation in big-oh notation?

Master theorem Theorem Master Theorem: Let T (n) a T ( n b ) + c n k and T (1) c, Then? if a < b k T (n) =? if a = b k? if a > b k

End