while the instance is not solved schedule the

  • Slides: 53
Download presentation

 کﻤیﻨﻪﺳﺎﺯی ﺯﻣﺎﻥ کﻞ - ﺝ( ﺯﻣﺎﻥﺑﻨﺪی ﺍﺑﺘﺪﺍ کﺎﺭﻫﺎ ﺭﺍ ﺑﻪ ﺗﺮﺗیﺐ ﺻﻌﻮﺩی ﻣﺮﺗﺐ

کﻤیﻨﻪﺳﺎﺯی ﺯﻣﺎﻥ کﻞ - ﺝ( ﺯﻣﺎﻥﺑﻨﺪی ﺍﺑﺘﺪﺍ کﺎﺭﻫﺎ ﺭﺍ ﺑﻪ ﺗﺮﺗیﺐ ﺻﻌﻮﺩی ﻣﺮﺗﺐ ﻣیکﻨیﻢ ﻭ while ( the instance is not solved) { schedule the next job; // selection procedure and // feasibility check if ( there are no more jobs) // solution check the instance is solved; } 9

 ﺝ( ﺯﻣﺎﻥﺑﻨﺪی ﺑﺎ ﻣﻬﻠﺖ ﻣﻌیﻦ ﺍﻟگﻮﺭیﺘﻢ ﺣﺮیﺼﺎﻧﻪ ﺳﻄﺢ ﺑﺎﻻ کﺎﺭﻫﺎ ﺭﺍ ﺑﺮ ﺍﺳﺎﺱ

ﺝ( ﺯﻣﺎﻥﺑﻨﺪی ﺑﺎ ﻣﻬﻠﺖ ﻣﻌیﻦ ﺍﻟگﻮﺭیﺘﻢ ﺣﺮیﺼﺎﻧﻪ ﺳﻄﺢ ﺑﺎﻻ کﺎﺭﻫﺎ ﺭﺍ ﺑﺮ ﺍﺳﺎﺱ ﻣﻨﻔﻌﺘﺸﺎﻥ ﺑﻪ ﺻﻮﺭﺕ ﻧﺰﻭﻟی ﻣﺮﺗﺐ ﻣیکﻨیﻢ S=Ø while (the instance is not solved){ select next job; // selection procedure if (S is feasible with this job added) // feasibility check add this job to S; if (there are no more jobs) // solution check the instance is solved; } 20

Job Deadline Profit 1 3 40 2 1 35 3 1 30 4 3

Job Deadline Profit 1 3 40 2 1 35 3 1 30 4 3 25 5 1 20 6 3 15 7 2 10 ﺝ( ﺯﻣﺎﻥﺑﻨﺪی ﺑﺎ ﻣﻬﻠﺖ ﻣﻌیﻦ . کﺎﺭﻫﺎ ﺑﺮ ﺍﺳﺎﺱ ﻣﻨﻔﻌﺖ ﻣﺮﺗﺐﺷﺪﻩ ﻫﺴﺘﻨﺪ S is set to Ø. S is set to {1} because the sequence [1] is feasible. S is set to {1, 2} because the sequence [2, 1] is feasible. {1, 2, 3} is rejected because there is no feasible sequence for this set. S is set to {1, 2, 4} because the sequence [2, 1, 4] is feasible. {1, 2, 4, 5}is rejected because there is no feasible sequence for this set. {1, 2, 4, 6}is rejected because there is no feasible sequence for this set. {1, 2, 4, 7}is rejected because there is no feasible sequence for this set. . { ﺑﺮﺍﺑﺮ ﺧﻮﺍﻫﺪ ﺷﺪ 1, 2, 4} ﺩﺭ ﻧﻬﺎیﺖ ﺑﺎ S ﺩﺭ ﺍیﻦ ﻣﺜﺎﻝ 21

function J=schedule(deadline) n=length(deadline); J(1)=1; for i=2: n K=add(J, i, deadline); if feasible(K, deadline) J=K;

function J=schedule(deadline) n=length(deadline); J(1)=1; for i=2: n K=add(J, i, deadline); if feasible(K, deadline) J=K; end end 22 function r=feasible(K, deadline) n=length(K); r=1; for i=1: n if deadline(K(i))>i r=0; break; end end ﺝ( ﺯﻣﺎﻥﺑﻨﺪی ﺑﺎ ﻣﻬﻠﺖ ﻣﻌیﻦ ﺑﻪ گﻮﻧﻪﺍی ﺍﺿﺎﻓﻪ ﻣیکﻨﺪ کﻪ ﺩﺭ ﺩﻧﺒﺎﻟﻪ ﺑﻪ ﺩﺳﺖ آﻤﺪﻩ کﺎﺭﻫﺎ ﺑﺮ ﺍﺳﺎﺱ ﻣﻬﻠﺘﺸﺎﻥ ﻣﺮﺗﺐ ﺑﺎﺷﻨﺪ function K=add(J, i, deadline) n=length(J); for r=1: n if deadline(J(r))<=deadline(i) K(r)=J(r); else K(r)=i; K(r+1: n+1)=J(r: n); break; end

 ﺩ( کﺪ ﻫﺎﻓﻤﻦ Even though the capacity of secondary storage devices keeps getting

ﺩ( کﺪ ﻫﺎﻓﻤﻦ Even though the capacity of secondary storage devices keeps getting larger and their cost keeps getting smaller, the devices continue to fill up due to increased storage demands. Given a data file, it would therefore be desirable to find a way to store the file as efficiently as possible. The problem of data compression is to find an efficient method for encoding a data file. Next, we discuss the encoding method, called Huffman code, and a greedy algorithm for finding a Huffman encoding for a given file. 24

 ﺩ( کﺪ ﻫﺎﻓﻤﻦ A common way to represent a file is to use

ﺩ( کﺪ ﻫﺎﻓﻤﻦ A common way to represent a file is to use a binary code. In such a code, each character is represented by a unique binary string, called the codeword. A fixed-length binary code represents each character using the same number of bits. For example, suppose our character set is {a, b, c}. Then we could use 2 bits to code each character, 25

 ﺩ( کﺪ ﻫﺎﻓﻤﻦ Given this code, if our file is our encoding is

ﺩ( کﺪ ﻫﺎﻓﻤﻦ Given this code, if our file is our encoding is 26

 ﺩ( کﺪ ﻫﺎﻓﻤﻦ We can obtain a more efficient coding using a 27

ﺩ( کﺪ ﻫﺎﻓﻤﻦ We can obtain a more efficient coding using a 27 variable-length binary code. Such a code can represent different characters using different numbers of bits. In the current example, we can code one of the characters as 0. Since ‘b’ occurs most frequently, it would be most efficient to code ‘b’ using this codeword. However, then ‘a’ could not be coded as ‘ 00’ because … we would not be able to distinguish one ‘a’ from two ‘b's.

 ﺩ( کﺪ ﻫﺎﻓﻤﻦ So we could code as follows: Given this code, {ababcbbbc}

ﺩ( کﺪ ﻫﺎﻓﻤﻦ So we could code as follows: Given this code, {ababcbbbc} would be encoded as With this encoding it takes only 13 bits to represent the file, whereas the previous one took 18 bits 28

(Prefix codes) کﺪﻫﺎی پیﺶﻭﻧﺪی - ﺩ( کﺪ ﻫﺎﻓﻤﻦ One particular type of variable-length code

(Prefix codes) کﺪﻫﺎی پیﺶﻭﻧﺪی - ﺩ( کﺪ ﻫﺎﻓﻤﻦ One particular type of variable-length code is a prefix code. In a prefix code 29 no codeword for one character… constitutes the beginning of the codeword for another character. For example, if 01 is the code word for ‘a’, then 011 could not be the codeword for ‘b’. A fixed-length code is also a prefix code.

 ﺩ( کﺪ ﻫﺎﻓﻤﻦ Every prefix code can be represented by a binary tree

ﺩ( کﺪ ﻫﺎﻓﻤﻦ Every prefix code can be represented by a binary tree whose leaves are the characters that are to be encoded. The advantage of a prefix code is that we 30 not look ahead when parsing the file. need

 ﺩ( کﺪ ﻫﺎﻓﻤﻦ To parse, we start at the first bit on the

ﺩ( کﺪ ﻫﺎﻓﻤﻦ To parse, we start at the first bit on the left in the file and the root of the tree. We sequence through the bits, and go left or right down the tree depending on whether a 0 or 1 is encountered. When we reach a leaf, we obtain the character at that leaf; then we return to the root and repeat the procedure starting with the next bit in sequence. 31

 ﺩ( کﺪ ﻫﺎﻓﻤﻦ we need to use a priority queue. In a priority

ﺩ( کﺪ ﻫﺎﻓﻤﻦ we need to use a priority queue. In a priority queue, the element with the highest priority is always removed next. In this case, the element with the highest priority is the character with the lowest frequency in the file. 32

 ﺩ( کﺪ ﻫﺎﻓﻤﻦ If a priority queue is implemented as a heap, it

ﺩ( کﺪ ﻫﺎﻓﻤﻦ If a priority queue is implemented as a heap, it can be initialized in θ(n) time. Furthermore, each heap operation requires θ(lgn) time. Since there are n - 1 passes the algorithm runs in θ(nlgn) time. 38

 ﻩ( ﺍﻟگﻮﺭیﺘﻢ ﺣﺮیﺼﺎﻧﻪ ﺩﺭ ﻣﻘﺎیﺴﻪ ﺑﺎ ﺑﺮﻧﺎﻣﻪﻧﻮیﺴﻪ پﻮیﺎ The greedy approach and dynamic

ﻩ( ﺍﻟگﻮﺭیﺘﻢ ﺣﺮیﺼﺎﻧﻪ ﺩﺭ ﻣﻘﺎیﺴﻪ ﺑﺎ ﺑﺮﻧﺎﻣﻪﻧﻮیﺴﻪ پﻮیﺎ The greedy approach and dynamic programming are two ways to solve optimization problems. Often a problem can be solved using either approach. For example, the Shortest Paths problem is solved using dynamic programming and greedy approach. Often when the greedy approach solves a problem, the result is a simpler, more efficient algorithm. 39

 ﻩ( ﺍﻟگﻮﺭیﺘﻢ ﺣﺮیﺼﺎﻧﻪ ﺩﺭ ﻣﻘﺎیﺴﻪ ﺑﺎ ﺑﺮﻧﺎﻣﻪﻧﻮیﺴﻪ پﻮیﺎ On the other hand, it

ﻩ( ﺍﻟگﻮﺭیﺘﻢ ﺣﺮیﺼﺎﻧﻪ ﺩﺭ ﻣﻘﺎیﺴﻪ ﺑﺎ ﺑﺮﻧﺎﻣﻪﻧﻮیﺴﻪ پﻮیﺎ On the other hand, it is usually more difficult to 40 determine whether a greedy algorithm always produces an optimal solution. As the Change problem shows, not all greedy algorithms do. A proof is needed to show that a particular greedy algorithm always produces an optimal solution, whereas a counterexample is needed to show that it does not. Recall that in the case of dynamic programming we need only determine whether the principle of optimality applies.

 ﻩ( ﺍﻟگﻮﺭیﺘﻢ ﺣﺮیﺼﺎﻧﻪ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک A thief breaking

ﻩ( ﺍﻟگﻮﺭیﺘﻢ ﺣﺮیﺼﺎﻧﻪ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک A thief breaking into a jewelry store carrying a 41 knapsack. The knapsack will break if the total weight of the items stolen exceeds some maximum weight W. Each item has a value and a weight. The thief's dilemma is to maximize the total value of the items while not making the total weight exceed W. This problem is called the 0– 1 Knapsack problem.

 ﻩ( ﺍﻟگﻮﺭیﺘﻢ ﺣﺮیﺼﺎﻧﻪ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک where wi, pi,

ﻩ( ﺍﻟگﻮﺭیﺘﻢ ﺣﺮیﺼﺎﻧﻪ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک where wi, pi, and W are positive integers. Determine a subset A of S such that 42

 ﻩ( ﺍﻟگﻮﺭیﺘﻢ ﺣﺮیﺼﺎﻧﻪ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک The brute-force solution

ﻩ( ﺍﻟگﻮﺭیﺘﻢ ﺣﺮیﺼﺎﻧﻪ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک The brute-force solution is to consider all subsets of the n items; discard those subsets whose total weight exceeds W; and, of those remaining, take one with maximum total profit. there are 2 n subsets of a set containing n items. 43

 ﻩ( ﺍﻟگﻮﺭیﺘﻢ ﺣﺮیﺼﺎﻧﻪ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک An obvious greedy

ﻩ( ﺍﻟگﻮﺭیﺘﻢ ﺣﺮیﺼﺎﻧﻪ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک An obvious greedy strategy is to steal the items with the largest profit first; that is, steal them in nonincreasing order according to profit. This strategy, however, would not work very well if the most profitable item had a large weight in comparison to its profit. 44

 ﻩ( ﺍﻟگﻮﺭیﺘﻢ ﺣﺮیﺼﺎﻧﻪ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک Another obvious greedy

ﻩ( ﺍﻟگﻮﺭیﺘﻢ ﺣﺮیﺼﺎﻧﻪ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک Another obvious greedy strategy is to steal the lightest items first. This strategy fails badly when the light items have small profits compared with their weights. 45

 ﻩ( ﺍﻟگﻮﺭیﺘﻢ ﺣﺮیﺼﺎﻧﻪ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک a more sophisticated

ﻩ( ﺍﻟگﻮﺭیﺘﻢ ﺣﺮیﺼﺎﻧﻪ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک a more sophisticated greedy strategy is to steal the items with the largest profit per unit weight first. That is, we order the items in non-increasing order according to profit per unit weight, and select them in sequence. An item is put in the knapsack if its weight does not bring the total weight above W. 46

(Fractional) ﻩ( ﺍﻟگﻮﺭیﺘﻢ ﺣﺮیﺼﺎﻧﻪ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی کﺴﺮی In the Fractional Knapsack problem,

(Fractional) ﻩ( ﺍﻟگﻮﺭیﺘﻢ ﺣﺮیﺼﺎﻧﻪ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی کﺴﺮی In the Fractional Knapsack problem, the thief can take any fraction of the item. Our greedy algorithm never wastes any capacity in the Fractional Knapsack problem as it does in the 0– 1 Knapsack problem. As a result, it always yields an optimal solution. 48

 ﻩ( ﺑﺮﻧﺎﻣﻪﻧﻮیﺴی پﻮیﺎ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک If we can

ﻩ( ﺑﺮﻧﺎﻣﻪﻧﻮیﺴی پﻮیﺎ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک If we can show that the principle of optimality applies, we can solve the 0– 1 Knapsack problem using dynamic programming. To that end, let A be an optimal subset of the n items. There are two cases: either A or 49 it does not. contains itemn

 ﻩ( ﺑﺮﻧﺎﻣﻪﻧﻮیﺴی پﻮیﺎ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک If A does

ﻩ( ﺑﺮﻧﺎﻣﻪﻧﻮیﺴی پﻮیﺎ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک If A does not contain itemn, A is equal to an optimal subset of the first n-1 items. If A does contain itemn, the total profit of the items in A is equal to pn plus the optimal profit obtained when the items can be chosen from the first n -1 items under the restriction that the total weight cannot exceed W-wn. Therefore, the principle of optimality applies. 50

 ﻩ( ﺑﺮﻧﺎﻣﻪﻧﻮیﺴی پﻮیﺎ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک If for i>0

ﻩ( ﺑﺮﻧﺎﻣﻪﻧﻮیﺴی پﻮیﺎ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک If for i>0 and w>0, we let P[i][w] be the optimal profit obtained when choosing items only from the first i items under the restriction that the total weight cannot exceed w, The maximum profit is equal to P[n][W]. 51

 ﻩ( ﺑﺮﻧﺎﻣﻪﻧﻮیﺴی پﻮیﺎ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک We can determine

ﻩ( ﺑﺮﻧﺎﻣﻪﻧﻮیﺴی پﻮیﺎ ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک We can determine this value using a two 52 dimensional array P whose rows are indexed from 0 to n and whose columns are indexed from 0 to W. We compute the values in the rows of the array in sequence using the previous expression for P[i][w]. The values of P[0][w] and P[i][0] are set to 0.

 ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک ﻩ( ﺭﻭﺵ ﺑﻬﺒﻮﺩیﺎﻓﺘﻪ ﺑﺮﻧﺎﻣﻪﻧﻮیﺴی پﻮیﺎ for

ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک ﻩ( ﺭﻭﺵ ﺑﻬﺒﻮﺩیﺎﻓﺘﻪ ﺑﺮﻧﺎﻣﻪﻧﻮیﺴی پﻮیﺎ for a given n, we can create instances with arbitrarily large running times by taking arbitrarily large values of W. For example, the number of entries computed is in Θ (n ×n!) if W equals n!. If n = 20 and W = 20!, the algorithm will take thousands of years to run on a modern-day computer. When W is extremely large in comparison with n, this algorithm is worse than the bruteforce algorithm that simply considers all subsets. 53

 ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک ﻩ( ﺭﻭﺵ ﺑﻬﺒﻮﺩیﺎﻓﺘﻪ ﺑﺮﻧﺎﻣﻪﻧﻮیﺴی پﻮیﺎ The

ﺩﺭ ﻣﺴﺌﻠﻪ کﻮﻟﻪ پﺸﺘی ﺻﻔﺮ ﻭ یک ﻩ( ﺭﻭﺵ ﺑﻬﺒﻮﺩیﺎﻓﺘﻪ ﺑﺮﻧﺎﻣﻪﻧﻮیﺴی پﻮیﺎ The algorithm can be improved so that the worst-case number of entries computed is in Θ (2 n). With this improvement, it never performs worse than the brute-force algorithm and often performs much better. The improvement is based on the fact that it is not necessary to determine the entries in the ith row for every w between 1 and W. the only entries needed in the (n -1)st row are 54