 # 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.

## Presentation on theme: "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."— Presentation transcript:

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 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)

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]

Consider this example to determine the minimum spanning tree:

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.

Consider this example to determine the minimum spanning tree:

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 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.

Consider this example:

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.

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:

Total time in the systemSchedule 39[1, 2, 3] 33[1, 3, 2] 44[2, 1, 3] 43[2, 3, 1] 32[3, 1, 2] 37[3, 2, 1]

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

ProfitDeadlineJob 3021 3512 2523 4014 Consider this example:

Total ProfitSchedule 30 + 25 = 55[1, 3] 35 + 30 = 65[2, 1] 35 + 25 = 60[2, 3] 25 + 30 = 55[3, 1] 40 + 30 = 70[4, 1] 40 + 25 = 65[4, 1]

Huffman Code FrequencyCharacter 15A 5B 12C 17D 10E 25F

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

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?

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.

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 p n plus the optimal profit obtained from the first n-1 items, where the total weight cannot exceed W- w n 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

Download ppt "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."

Similar presentations