Download Lecture Slides for Algorithm Basics - Data Structure and Algorithms | CS 245 and more Exams Data Structures and Algorithms in PDF only on Docsity!
Data Structures and Algorithms^ CS245-2009S-01Algorithm Basics
David GallesDepartment of Computer ScienceUniversity of San Francisco
01-0:^ Syllabus^ Office Hours^ Course Text^ Prerequisites^ Test Dates & Testing Policies^ Check dates now!^ Grading Policies
01-2:^ How to Succeed^ Come to class. Pay attention. Ask questions.^ A question as vague as “I don’t get it” isperfectly acceptable.^ If you’re confused, there’s a good chancesomeone else is confused as well.
01-3:^ How to Succeed^ Come to class. Pay attention. Ask questions.^ A question as vague as “I don’t get it” isperfectly acceptable.^ If you’re confused, there’s a good chancesomeone else is confused as well.^ Come by my office^ I am^ very
available to students.
01-5:^ How to Succeed^ Come to class. Pay attention. Ask questions.^ A question as vague as “I don’t get it” isperfectly acceptable.^ If you’re confused, there’s a good chancesomeone else is confused as well.^ Come by my office^ I am^ very
available to students. Start the homework assignments and projectsearly Projects in this class are significantly harderthan CS112 Read the notes.
01-6:^ What is an algorithm?^ An algorithm is a step-by-step method for solving aproblem.^ Each step must be well defined.^ Algorithm^6 =
Computer Program. A program is an^ implementation
of an algorithm. Can have different implementations of the samealgorithm^ Different Languages^ Different Coding Styles
01-8:^ Example: Selection Sort Java Code: for (int i=0; i<A.length - 1; i++) {smallest = i;for (j=i+1; j<A.length; j++) {if (A[j] < A[smallest])smallest = j;tmp = A[i];A[i] = A[smallest];A[smallest] = tmp;}
01-9:^ Problem I: Bridge Four people want to cross a bridge^ The bridge can only hold two people at the sametime^ Each person requires a different amount of time tocros the bridge (1, 2, 5, and 8 minutes)^ It is pitch black, and they have only 1 flashlightwhich they need to shuttle back and forth acrossthe bridge
01-11:^ Problem II: 8 Queens^ Standard 8x8 Chessboard^ Place 8 Queens on this board, so that no Queenattacks another Queen.^ Queens can move horizontally or diagonallyany number of squares
Q
01-12:^ 8-Queens Data Structures^ Two-dimensional array of characters.^ List of x-y coordinates of each queen.^ Array of integers:^ Each element in this array represents a column^ Value stored in element
i^ represents the row in
which the queen at column
i^ is located.
01-14:^ 9 Coins^ There are 9 coins. 8 are good, but one iscounterfeit. The counterfeit coin is ligher than theother coins.^ You have a balance scale, that can compare theweights of two sets of coins^ Can you determine which coin is counterfeit, usingthe scale only 2 times?^ If there are 27 coins, one lighter and counterfeit,can you find it using the scale 3 times?
01-15:^ 9 Coins^ First, let’s ensure that it’s possible^ There are 9 possible cases: Coin 1 is bad, coin 2 isbad, coin 3 is bad, etc.^ Can we distinguish between 9 different casesusing two weighings?
01-17:^ 9 Coins^ Weigh Coins 1,2,3 against 4,5,6^ What are the possible outcomes?^ What will they tell us?
01-18:^ 9 Coins^456
123 456
123456 Bad coin^ is^ in{4,5,6}
Bad coin is in{7,8,9}
Bad^ coin^ is^ in{1,2,3}