Vous êtes sur la page 1sur 6

Efficency Analysis for play() algorithm ---------------------------------------1. Input Sizes: p=state.getPlayedCards() c=count.length n=hand.size() 2.

Identify operations to count: The following operations are ones I want to count: 1. if (state.getPlayedCards().get(i).getNumber()==j) 2. if (hold < count[j]) 3. if (hand.get(i).getRank()==Rank.WILD_D4) 4. if (hand.get(i).getRank()==Rank.WILD) 5. if ((hand.get(i).getRank()==Rank.DRAW_TWO) && (hand.get(i).canPlayOn( upCard, calledColor))) 6. if ((hand.get(i).getRank()==Rank.SKIP) && (hand.get(i).canPlayOn(upCa rd, calledColor))) 7. if ((hand.get(i).getRank()==Rank.REVERSE) && (hand.get(i).canPlayOn( upCard, calledColor))) 8. if (hand.get(i).getRank()==upCard.getRank() && hand.get(i).getColor( )!=upCard.getColor() && ((hand.get(i).getColor()!=state.getMostRecentColorCal ledByUpcomingPlayers()[1]) || (hand.get(i).getColor()!=state.getMostRecentColorCal ledByUpcomingPlayers()[2])) && (hand.get(i).canPlayOn(upCard, calledColor))) 9. if ((hand.get(i).getRank()==Rank.REVERSE) && ((hand.get(i).getRank()==upCard.getRank()) || (hand.get(i).getColor()==upCard.getColor()) || (hand.get(i).getColor()!=upCard.getColor()))) 10. if ((hand.get(i).getRank()==upCard.getRank()) && (hand.get(i).getNum ber()==upCard.getNumber()) && (hand.get(i).getColor()!=upCard.getColor())) 11. if (hand.get(i).getNumber()==ranks[j] && (hand.get(i).canPlayOn(upCa rd, calledColor))) 3. Describe the best case and worst case scenario: Best case: The best case scenario is if the program goes through the par t which checks the played cards with the cards in the hand and sorts them, returns true for (hand.get(i).getRank()==Rank.WILD_D4) and returns a wild D4 card. Worst case: The worst case scenario is if the program goes through the p art which checks the played cards with the cards in the hand and sorts them, returns false for the if statements 3-11 from part 2 of the analysis and returns -1 at the end of the program. 4+5. Compute f(n) for the best and worst case scenarios and come up with "big Oh " for each of your algorithms For this part of the analysis, I am going to separate the different section s of my program, finding the f(n) and Big Oh for each separately first. At the end of the analysis, I will combine the big Oh of all the se ctions of the play() method. //Section 1

int[] ranks={0,1,2,3,4,5,6,7,8,9}; int[] count=new int[10]; int total=0; for (int j=0;j<10;j++){ for (int i=0;i<state.getPlayedCards().size();i++) if (state.getP layedCards().get(i).getNumber()==j) total++; count[j]=total; total=0; } count[0]*=2; -Best Case f(p)=10*p -Worst Case f(p)=10*p -Big Oh: O(p)

//Section 2 for (int i=1;i<count.length;i++) { int hold=count[i]; int hold1=ranks[i]; int j; for (j=i-1; j >= 0; j--) { if (hold < count[j]) { count[j+1]=count[j]; ranks[j+1]=ranks[j]; } else break; } count[j+1] = hold; ranks[j+1] = hold1; } -Best Case f(c)=(c-1)*[(c^2)/2]=(c^3)/2-(c^2)/2 -Worst Case f(c)=(c-1)*[(c^2)/2]=(c^3)/2-(c^2)/2 -Big Oh: O(c^3)

//Section 3 if (state.getNumCardsInHandsOfUpcomingPlayers()[0]<5){ for(int i=0;i<hand.size();i++) if (hand.get(i).getRank()==Rank.W ILD_D4) return i; for(int i=0;i<hand.size();i++) if (hand.get(i).getRank()==Rank.W ILD) return i; for(int i=0;i<hand.size();i++) if ((hand.get(i).getRank()==Rank. DRAW_TWO) && (hand.get(i).canPlayOn(upCard, calledColor))) return i; for(int i=0;i<hand.size();i++) if ((hand.get(i).getRank()==Rank. SKIP) && (hand.get(i).canPlayOn(upCard, calledColor))) return i; for(int i=0;i<hand.size();i++) if ((hand.get(i).getRank()==Rank. REVERSE) && (hand.get(i).canPlayOn(upCard, calledColor))) return i; } -Best Case f(n)=0 -Worst Case f(n)=n+n+n+n+n=5n -Big Oh: O(n)

//Section 4 if (state.getNumCardsInHandsOfUpcomingPlayers()[1]<5||state.getNumCardsI nHandsOfUpcomingPlayers()[2]<5){ for(int i=0;i<hand.size();i++) if (hand.get(i).getRank()==Rank.W ILD_D4) return i; for(int i=0;i<hand.size();i++) if (hand.get(i).getRank()==Rank.W ILD) return i; for(int i=0;i<hand.size();i++){ if (hand.get(i).getRank()==upCard.getRank() && hand.get( i).getColor()!=upCard.getColor() && ((hand.get(i).getColor()!=state.getMostRecentColorCal ledByUpcomingPlayers()[1]) || (hand.get(i).getColor()!=state.getMostRecentColorCal ledByUpcomingPlayers()[2])) && (hand.get(i).canPlayOn(upCard, calledColor))) return i; } } -Best Case f(n)=0 -Worst Case f(n)=n+n+n=3n -Big Oh: O(n)

//Section 5 if (state.getTotalScoreOfUpcomingPlayers()[2]>state.getTotalScoreOfUpcomi ngPlayers()[0]){ for(int i=0;i<hand.size();i++) if ((hand.get(i).getRank()==Rank. REVERSE) && ((hand.get(i).getRank()==upCard.getRank()) || (hand.get(i).getColor()==upCard.getColor()) || (hand.get(i).getColor()!=upCard.getColor()))) return i; } -Best Case f(n)=0 -Worst Case f(n)=n -Big Oh: O(n)

//Section 6 for(int i=0;i<hand.size();i++) if ((hand.get(i).getRank()==Rank.DRAW_TWO ) && (hand.get(i).canPlayOn(upCard, calledColor))) return i; for(int i=0;i<hand.size();i++) if (hand.get(i).getRank()==Rank.WILD_D4) return i; for(int i=0;i<hand.size();i++) if (hand.get(i).getRank()==Rank.WILD) ret urn i; for(int i=0;i<hand.size();i++) if ((hand.get(i).getRank()==Rank. SKIP) && (hand.get(i).canPlayOn(upCard, calledColor))) return i; for(int i=0;i<hand.size();i++) if ((hand.get(i).getRank()==Rank. REVERSE) && (hand.get(i).canPlayOn(upCard, calledColor))) return i;

-Best Case f(n)=1 -Worst Case f(n)=n+n+(n*(n+n))=n+n+2n^2=2n(n+1) -Big Oh: O(n^2)

//Section 7 for(int i=0;i<hand.size();i++){ if ((hand.get(i).getRank()==upCard.getRank()) && (hand.get(i).ge tNumber()==upCard.getNumber()) && (hand.get(i).getColor()!=upCard.getColor())) return i; } -Best Case f(n)=1 -Worst Case f(n)=n -Big Oh: O(n)

//Section 8 for(int j=0;j<10;j++){ for(int i=0;i<hand.size();i++){ if (hand.get(i).getNumber()==ranks[j] && (hand.get(i).ca nPlayOn(upCard, calledColor))) return i; } } -Best Case f(n)=1 -Worst Case f(n)=10*n -Big Oh: O(n)

return -1; } //Overall Program -Big Oh: O(p)+O(c^3)+O(n)+O(n)+O(n)+O(n^2)+O(n)+O(n)=O(p+c^3+n^2)

Efficency Analysis for callColor() algorithm ---------------------------------------1. Input Sizes: n=hand.size() 2. Identify operations to count: The following operations are ones I want to count:

1. switch (hand.get(i).getColor()) { case RED: red++; break; case YELLOW:yellow++; break; case GREEN: green++; break; case BLUE: blue++; break; } 2. if (red>yellow){ if (red>green){ if (red>blue){ return Color.RED; } else return Color.BLUE; } else if (green>blue){ return Color.GREEN; } else return Color.BLUE; } else if (yellow>green){ if (yellow>blue){ return Color.YELLOW; } else return Color.BLUE; } else if (green>blue){ return Color.GREEN; } else return Color.BLUE; 3. Describe the best case and worst case scenario: Best case scenario: The best case scenario would occur if the following sta tements are true: (red>yellow), (red>green) and (red>blue). This would return Color.Red. Worst case scenario: The worst case scenario would be if all the if stateme nts in operation 2 from part 2 of this analysis were false and the program ended up returning Color.Blue. 4+5. Compute f(n) for the best and worst case scenarios and come up with "big Oh " for each of your algorithms For this part of the analysis, I am going to separate the different section s of my program, finding the f(n) and Big Oh for each separately first. At the end of the analysis, I will combine the big Oh of all the se ctions of the callColor() method. //Section 1 int red=0,yellow=0,green=0,blue=0; for (int i=0;i<hand.size();i++){ switch (hand.get(i).getColor()) { case RED: red++; break; case YELLOW:yellow++; break; case GREEN: green++; break; case BLUE: blue++; break; } }

-Best Case f(n)=1 -Worst Case f(n)=n -Big Oh: O(n)

//Section 2 if (red>yellow){ if (red>green){ if (red>blue){ return Color.RED; } else return Color.BLUE; } else if (green>blue){ return Color.GREEN; } else return Color.BLUE; } else if (yellow>green){ if (yellow>blue){ return Color.YELLOW; } else return Color.BLUE; } else if (green>blue){ return Color.GREEN; } else return Color.BLUE; } -Best Case f(n)=3 -Worst Case f(n)=7 -Big Oh: O(1) //Overall Program Big Oh: O(n)+O(1)=O(n)

Vous aimerez peut-être aussi