chapter 4 the greedy approach. minimum spanning tree a tree is an acyclic, connected, undirected...

22
Chapter 4 The Greedy Approach

Upload: tony-benton

Post on 15-Dec-2015

226 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E

Chapter 4

The Greedy Approach

Page 2: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E

Minimum Spanning Tree

• A tree is an acyclic, connected, undirected graph.

• A spanning tree for a given graph G=<E,V>, where E is the edge set and V is the vertex set, is a connected sub-graph that contains all the vertices in G and is a tree.

• If G is a weighted graph, then the spanning tree will have a total weight. The same graph may have different spanning trees, not every spanning tree has the minimum weight. Such a tree is called minimum spanning tree. (Spanning tree with minimum weight)

Page 3: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E

Prim’s Algorithm

• In Prim’s algorithm we are maintaining two arrays nearest and distance, where i = 2, …, n

• nearest [i] = index of the vertex in Y nearest to vi

• distance [i] = weight on edge between vi and the vertex indexed by nearest [i]

Page 4: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E

Consider this example to determine the minimum spanning tree:

Page 5: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E
Page 6: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E

Kruskal’s Algorithm• In Kruskal’s algorithm, we need a disjoint set abstract data type,

which consists of data types index and set pointer, and routines initial, find, merge, and equal, such that if we declare

- index i ; - set pointer p, q ;• Then • initial(n) initializes n disjoint subsets, each of which contains exactly

one of the indices between 1 and n.• p = find (i) makes p point to the set containing index i.• merge (p, q) merges the two sets, to which p and q point, into the

set.• equal (p, q) returns true if p and q both point to the same set.

Page 7: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E

Consider this example to determine the minimum spanning tree:

Page 8: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E
Page 9: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E

Dijkstra’s Algorithm for Single-Source Shortest Path

• We have arrays touch and length, for i = 2, … n,

• touch [i] = index of vertex v in Y such that the edge < v, vi> is the last edge on the current shortest path from v1 to vi using only vertices in Y as intermediates.

• length [i] = length of the current shortest path from v1 to vi using only vertices in Y as intermediates.

Page 10: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E

Consider this example :

Page 11: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E
Page 12: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E

Scheduling

• We will study two different types of scheduling problems.• First, time in the system which is the time spent both

waiting and being served. In a customer based serving environment, each customer (job) in the queue might require different amount of service time. An optimal schedule minimizing total time in the system can be developed. This problem has many application areas.

• The second is scheduling with deadlines where each customer takes same amount of time to complete, but has a deadline by which it must start to yield a profit associated with the job. The goal is to maximize the profit.

Page 13: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E

Minimizing Total Time in the System

• Simple solution: Consider all possible schedules and compute the minimum total time in the system.

• Example: Assume there are 3 jobs and the service times for them are t1=5, t2=10, t3=4

• Assume the schedule is [1, 2, 3], then the total time in the system is:

Page 14: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E

ScheduleTotal time in the system

[1 ,2 ,3]39

[1 ,3 ,2]33

[2 ,1 ,3]44

[2 ,3 ,1]43

[3 ,1 ,2]32

[3 ,2 ,1]37

Page 15: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E

Scheduling with Deadlines

• In this scheduling approach:

-Each job takes one unit of time

-If job starts before or at its deadline, profit

is obtained, otherwise no profit

-Goal is to schedule jobs to maximize the

total profit

Page 16: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E

JobDeadlineProfit

1230

2135

3225

4140

Consider this example:

Page 17: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E

ScheduleTotal Profit

[1 ,3]30 + 25 = 55

[2 ,1]35 + 30 = 65

[2 ,3]35 + 25 = 60

[3 ,1]25 + 30 = 55

[4 ,1]40 + 30 = 70

[4 ,1]40 + 25 = 65

Page 18: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E

Huffman Code

CharacterFrequency

A15

B5

C12

D17

E10

F25

Page 19: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E

We will construct the tree (using Huffman’s Algorithm)

Page 20: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E

A Greedy Approach to the 0-1 Knapsack Problem

• An example: A thief breaks into a jewelry store carrying a knapsack. Given n items S={item1, item2, …, item n}, each item having a weight wi and providing a profit pi, which items should the thief put into its knapsack with maximum capacity W in order to obtain the maximum profit?

Page 21: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E

A Greedy Approach to the Fractional Knapsack Problem

• In the Fractional Knapsack problem, the thief does not have to steal all of an item, but rather can take any fraction of the item.

Page 22: Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E

A Dynamic Programming Approach to the 0-1 Knapsack Problem

• Let A be an optimal subset of n items. There are two cases,

• If A contains item n , then the total profit of items in A is equal to pn plus the optimal profit obtained from the first n-1 items, where the total weight cannot exceed W- wn

• If A does not contain item n , then the total profit of items in A is equal to the optimal subset of the first n-1 items