32
CSE202: Algorithm Design and Analysis Ragesh Jaiswal, CSE, UCSD Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

  • Upload
    lamanh

  • View
    224

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

CSE202: Algorithm Design and Analysis

Ragesh Jaiswal, CSE, UCSD

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 2: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy Algorithms

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 3: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsJob scheduling

Problem

Job scheduling: You are given n jobs and you are supposed toschedule these jobs on a machine. Each job i consists of a durationT (i) and a deadline D(i). The lateness of a job w.r.t. a scheduleis defined as max(0,F (i)− D(i)), where F (i) is the finishing timeof job i as per the schedule. The goal is to minimise the maximumlateness.

Greedy strategiesSmallest jobs first.Earliest deadline first.

Algorithm

GreedyJobSchedule

- Sort the jobs in non-decreasing order of deadlines and schedulethe jobs on the machine in this order.

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 4: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsJob scheduling

Algorithm

GreedyJobSchedule

- Sort the jobs in non-decreasing order of deadlines and schedulethe jobs on the machine in this order.

Claim 1: There is an optimal schedule with no idle time (timewhen the machine is idle).

Definition

A schedule is said to have inversion if there are a pair of jobs (i , j)such that

1 D(i) < D(j), and2 Job j is performed before job i as per the schedule.

Claim 2: There is an optimal schedule with no idle time and noinversion.

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 5: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsJob scheduling

Claim 2: There is an optimal schedule with no idle time and noinversion.

Proof of Claim 2

Consider an optimal schedule O. First, if there is any idle time,we obtain another optimal schedule O1 without the idle time.Suppose O1 has inversions. Consider one such inversion (i , j).

Claim 2.1: If an inversion exists, then there exists a pair ofadjacently scheduled jobs (m, n) such that the schedule has aninversion w.r.t. (m, n).

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 6: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsJob scheduling

Claim 2: There is an optimal schedule with no idle time and noinversion.

Proof of Claim 2Consider an optimal schedule O. First, if there is any idle time, we obtain another optimal schedule O1 withoutthe idle time.Suppose O1 has inversions. Consider one such inversion (i, j).Claim 2.1: If an inversion exists, then there exists a pair of adjacently scheduled jobs (m, n) such that theschedule has an inversion w.r.t. (m, n).

Claim 2.2: Exchanging m and n does not increase the maximumlateness.

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 7: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Spanning Tree: Given a strongly connected graphG = (V ,E ), a spanning tree of G is a subgraph G ′ = (V ,E ′)such that G ′ is a tree.

Minimum Spanning Tree (MST): Given a strongly connectedweighted graph G = (V ,E ), a Minimum Spanning Tree of Gis a spanning tree of G of minimum total weight (i.e., sum ofweight of edges in the tree).

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 8: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Spanning Tree: Given a strongly connected graphG = (V ,E ), a spanning tree of G is a subgraph G ′ = (V ,E ′)such that G ′ is a tree.

Minimum Spanning Tree (MST): Given a strongly connectedweighted graph G = (V ,E ), a Minimum Spanning Tree of Gis a spanning tree of G of minimum total weight (i.e., sum ofweight of edges in the tree).

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 9: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Problem

Given a weighted graph G where all the edge weights are distinct,give an algorithm for finding the MST of G .

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 10: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Theorem

Cut property: Given a weighted graph G = (V ,E ) where all theedge weights are distinct. Consider a non-empty proper subset S ofV and S ′ = V \ S . Let e be the least weighted edge between anypair of vertices (u, v), where u is in S and v is in S ′. Then e isnecessarily present in all MSTs of G .

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 11: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Theorem

Cut property: Given a weighted graph G = (V ,E ) where all theedge weights are distinct. Consider a non-empty proper subset S ofV and S ′ = V \ S . Let e be the least weighted edge between anypair of vertices (u, v), where u is in S and v is in S ′. Then e isnecessarily present in all MSTs of G .

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 12: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Algorithm

Prim’s Algorithm(G)

- S ← {u} //u is an arbitrary vertex in the graph- T ← {}- While S does not contain all vertices

- Let e = (v ,w) be the minimum weight edge betweenS and V \ S

- T ← T ∪ {e}- S ← S ∪ {w}

Algorithm

Kruskal’s Algorithm(G)

- S ← E ; T ← {}- While the edge set T does not connect all the vertices

- Let e be the minimum weight edge in the set S- If e does not create a cycle in T

- T ← T ∪ {e}- S ← S \ {e}

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 13: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Algorithm

Prim’s Algorithm(G)

- S ← {u} //u is an arbitrary vertex in the graph- T ← {}- While S does not contain all vertices

- Let e = (v ,w) be the minimum weight edge betweenS and V \ S

- T ← T ∪ {e}- S ← S ∪ {w}

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 14: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Algorithm

Prim’s Algorithm(G)

- S ← {u} //u is an arbitrary vertex in the graph- T ← {}- While S does not contain all vertices

- Let e = (v ,w) be the minimum weight edge betweenS and V \ S

- T ← T ∪ {e}- S ← S ∪ {w}

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 15: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Algorithm

Prim’s Algorithm(G)

- S ← {u} //u is an arbitrary vertex in the graph- T ← {}- While S does not contain all vertices

- Let e = (v ,w) be the minimum weight edge betweenS and V \ S

- T ← T ∪ {e}- S ← S ∪ {w}

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 16: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Algorithm

Prim’s Algorithm(G)

- S ← {u} //u is an arbitrary vertex in the graph- T ← {}- While S does not contain all vertices

- Let e = (v ,w) be the minimum weight edge betweenS and V \ S

- T ← T ∪ {e}- S ← S ∪ {w}

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 17: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Algorithm

Prim’s Algorithm(G)

- S ← {u} //u is an arbitrary vertex in the graph- T ← {}- While S does not contain all vertices

- Let e = (v ,w) be the minimum weight edge betweenS and V \ S

- T ← T ∪ {e}- S ← S ∪ {w}

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 18: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Algorithm

Prim’s Algorithm(G)

- S ← {u} //u is an arbitrary vertex in the graph- T ← {}- While S does not contain all vertices

- Let e = (v ,w) be the minimum weight edge betweenS and V \ S

- T ← T ∪ {e}- S ← S ∪ {w}

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 19: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Algorithm

Kruskal’s Algorithm(G)

- S ← E ; T ← {}- While the edge set T does not connect all the vertices

- Let e be the minimum weight edge in the set S- If e does not create a cycle in T

- T ← T ∪ {e}- S ← S \ {e}

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 20: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Algorithm

Kruskal’s Algorithm(G)

- S ← E ; T ← {}- While the edge set T does not connect all the vertices

- Let e be the minimum weight edge in the set S- If e does not create a cycle in T

- T ← T ∪ {e}- S ← S \ {e}

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 21: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Algorithm

Kruskal’s Algorithm(G)

- S ← E ; T ← {}- While the edge set T does not connect all the vertices

- Let e be the minimum weight edge in the set S- If e does not create a cycle in T

- T ← T ∪ {e}- S ← S \ {e}

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 22: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Algorithm

Kruskal’s Algorithm(G)

- S ← E ; T ← {}- While the edge set T does not connect all the vertices

- Let e be the minimum weight edge in the set S- If e does not create a cycle in T

- T ← T ∪ {e}- S ← S \ {e}

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 23: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Algorithm

Kruskal’s Algorithm(G)

- S ← E ; T ← {}- While the edge set T does not connect all the vertices

- Let e be the minimum weight edge in the set S- If e does not create a cycle in T

- T ← T ∪ {e}- S ← S \ {e}

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 24: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Algorithm

Kruskal’s Algorithm(G)

- S ← E ; T ← {}- While the edge set T does not connect all the vertices

- Let e be the minimum weight edge in the set S- If e does not create a cycle in T

- T ← T ∪ {e}- S ← S \ {e}

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 25: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Algorithm

Prim’s Algorithm(G)

- S ← {u} //u is an arbitrary vertex in the graph- T ← {}- While S does not contain all vertices

- Let e = (v ,w) be the minimum weight edge betweenS and V \ S

- T ← T ∪ {e}- S ← S ∪ {w}

What is the running time of Prim’s algorithm?

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 26: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Algorithm

Prim’s Algorithm(G)

- S ← {u} //u is an arbitrary vertex in the graph- T ← {}- While S does not contain all vertices

- Let e = (v ,w) be the minimum weight edge betweenS and V \ S

- T ← T ∪ {e}- S ← S ∪ {w}

What is the running time of Prim’s algorithm? O(|E | · log |V |)

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 27: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Algorithm

Kruskal’s Algorithm(G)

- S ← E ; T ← {}- While the edge set T does not connect all the vertices

- Let e be the minimum weight edge in the set S- If e does not create a cycle in T

- T ← T ∪ {e}- S ← S \ {e}

Algorithm

Kruskal’s Algorithm(G)

- S ← E ; T ← {}- While the edge set T does not connect all the vertices

- //Note that G ′ = (V ,T ) contains dicsonnected components- Let e be the minimum weight edge in the set S- If e does not create a cycle in T- If u and v are in different components of G ′

- T ← T ∪ {e}- S ← S \ {e}

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 28: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Union-Find: Used for storing partition of a set of elements.The following two operations are supported:

1 Find(v): Find the partition to which the element v belongs.2 Union(u, v): Merge the partition to which u belongs with the

partition to which v belongs.

Consider the following data structure.

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 29: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Suppose we start from a full partition (i.e., each partitioncontains one element).

How much time does the following operation take:

Find(v):Union(u, v):

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 30: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Suppose we start from a full partition (i.e., each partitioncontains one element).

How much time does the following operation take:

Find(v): O(1)Union(u, v):

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 31: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

Greedy AlgorithmsMinimum Spanning Tree

Suppose we start from a full partition (i.e., each partitioncontains one element).

How much time does the following operation take:

Find(v): O(1)Union(u, v):

Claim: Performing k union operations takes O(k log k) time inthe worst case.

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis

Page 32: CSE202: Algorithm Design and Analysis - cseweb.ucsd.educseweb.ucsd.edu/~rajaiswal/cse202/Notes/Week-04/lec-1.pdf · is de ned as max(0;F(i) D(i)), where F(i) is the nishing time of

End

Ragesh Jaiswal, CSE, UCSD CSE202: Algorithm Design and Analysis