Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 380: Design and Analysis of Algorithms

Similar presentations


Presentation on theme: "CSC 380: Design and Analysis of Algorithms"— Presentation transcript:

1 CSC 380: Design and Analysis of Algorithms
Dr. Curry Guinn

2 Quick Info Dr. Curry Guinn CIS 2015 guinnc@uncw.edu
Office Hours: MWF: 10:00am-11:00m and by appointment

3 Today Brute force! Quiz 4 due Thursday night
Homework 3 due next Sunday night!

4 Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing an (a > 0, n a nonnegative integer) Computing n! Multiplying two matrices Searching for a key of a given value in a list A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

5 } } Sorting Algorithms There are many strategies for sorting arrays.
Among them: Bubble sort Selection sort Insertion sort Shell sort Merge sort Quicksort } Horrible } Not too bad } Among the very best

6 Selection and Bubble Sort
Selection and Bubble Sort are similar: both use the Simple Sorting Strategy of the previous slide. Selection and Bubble Sort differ in how they implement the step: Move the largest of A[0..last] to A[last]

7 Bubble Sort Bubble Sort moves the largest entry to the end of A[0..last] by comparing and swapping adjacent elements as an index sweeps through the unsorted portion of the array: //Compare A[0], A[1], no swap //Compare A[1], A[2], swap //Compare A[2], A[3], swap //Compare A[3], A[4], swap //Largest is at A[4]

8 Bubble Sort 3 7 5 2 6 1 4

9 BubbleSort . 3 7 5 2 6 1 4 ^ ^

10 3 7 5 2 6 1 4 3 7 5 2 6 1 4 ^ ^

11 3 7 5 2 6 1 4 3 7 5 2 6 1 4 3 5 7 2 6 1 4 ^ ^

12 3 7 5 2 6 1 4 3 7 5 2 6 1 4 3 5 7 2 6 1 4 3 5 2 7 6 1 4 ^ ^

13 3 7 5 2 6 1 4 3 7 5 2 6 1 4 3 5 7 2 6 1 4 3 5 2 7 6 1 4 3 5 2 6 7 1 4 ^ ^

14 3 7 5 2 6 1 4 3 7 5 2 6 1 4 3 5 7 2 6 1 4 3 5 2 7 6 1 4 3 5 2 6 7 1 4 3 5 2 6 1 7 4 ^ ^

15 End of Pass 1 3 7 5 2 6 1 4 Note that the last element must be the biggest 3 7 5 2 6 1 4 3 5 7 2 6 1 4 3 5 2 7 6 1 4 3 5 2 6 7 1 4 3 5 2 6 1 7 4 3 5 2 6 1 4 7

16 Bubble Sort: Pass 2 3 5 2 6 1 4 7 ^ ^

17 Bubble Sort: Pass 2 3 5 2 6 1 4 7 3 5 2 6 1 4 7 ^ ^

18 Bubble Sort: Pass 2 3 5 2 6 1 4 7 3 5 2 6 1 4 7 3 2 5 6 1 4 7 ^ ^

19 Bubble Sort: Pass 2 3 5 2 6 1 4 7 3 5 2 6 1 4 7 3 2 5 6 1 4 7 3 2 5 6 1 4 7 ^ ^

20 Bubble Sort: Pass 2 3 5 2 6 1 4 7 3 5 2 6 1 4 7 3 2 5 6 1 4 7 3 2 5 6 1 4 7 3 2 5 1 6 4 7 ^ ^

21 Bubble Sort: Pass 2 3 5 2 6 1 4 7 3 5 2 6 1 4 7 3 2 5 6 1 4 7 3 2 5 6 1 4 7 3 2 5 1 6 4 7 3 2 5 1 4 6 7 Note that the last 2 elements are the largest and in order

22 Bubble Sort: Pass 3 3 2 5 1 4 6 7 ^ ^

23 Bubble Sort: Pass 3 3 2 5 1 4 6 7 2 3 5 1 4 6 7 ^ ^

24 Bubble Sort: Pass 3 3 2 5 1 4 6 7 2 3 5 1 4 6 7 2 3 5 1 4 6 7 ^ ^

25 Bubble Sort: Pass 3 3 2 5 1 4 6 7 2 3 5 1 4 6 7 2 3 5 1 4 6 7 2 3 1 5 4 6 7 ^ ^

26 Bubble Sort: Pass 3 3 2 5 1 4 6 7 2 3 5 1 4 6 7 2 3 5 1 4 6 7 2 3 1 5 4 6 7 2 3 1 4 5 6 7 The last 3 elements are the largest and in order

27 Bubble Sort: Pass 4 2 3 1 4 5 6 7 ^ ^

28 Bubble Sort: Pass 4 2 3 1 4 5 6 7 2 3 1 4 5 6 7 ^ ^

29 Bubble Sort: Pass 4 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 ^ ^

30 Bubble Sort: Pass 4 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 2 1 3 4 5 6 7 The last 4 elements are the largest and in order

31 Bubble Sort: Pass 5 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 2 1 3 4 5 6 7 ^ ^

32 Bubble Sort: Pass 5 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 2 1 3 4 5 6 7 1 2 3 4 5 6 7 ^ ^

33 Bubble Sort: Pass 5 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 2 1 3 4 5 6 7 1 2 3 4 5 6 7 The last 5 elements are the largest and in order

34 Bubble Sort: Pass 6 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 2 1 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 ^ ^

35 Bubble Sort: Pass 6 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 2 1 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 The last 6 elements are the largest and in order

36 Bubble Sort is Done 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 2 1 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 What is the complexity of this algorithm?

37 Bubble Sort def bubbleSort(alist):
for passnum in range(len(alist)-1,0,-1): for i in range(passnum): if alist[i]>alist[i+1]: temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp

38 Selection Sort Selection sort, like Bubble sort, is based on a strategy that repeatedly executes the step: Move the largest of A[0..last] to A[last]

39 Selection Sort Selection Sort moves the largest entry to the end of A[0..last] by determining the position positionofMax of the largest entry, and then swapping A[positionOfMax] with A[last]: Selection Sort uses 2 variables: fillslot: keeps track of the portion A[0.. fillslot] that has already been examined: fillslot will range from 0 to last. positionOfMax: keeps track of the position of the largest entry in A[0.. fillslot]. When fillslot equals last, the variable positionOfMax will be the position of the largest entry in A[0..last].

40 Selection Sort def selectionSort(alist):
for fillslot in range(len(alist)-1,0,-1): positionOfMax=0 for location in range(1,fillslot+1): if alist[location]>alist[positionOfMax]: positionOfMax = location temp = alist[fillslot] alist[fillslot] = alist[positionOfMax] alist[positionOfMax] = temp

41 Why is selection sort typically faster than bubble sort?
Bubble sort has to do O(n2) comparisons and it may do up to O(n2) swaps. Selection sort has to do O(n2) comparisons but only O(n) swaps.

42 Analysis of Selection Sort
Time efficiency: Space efficiency: Stability: A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

43 Brute-Force String Matching
pattern: a string of m characters to search for text: a (longer) string of n characters to search in problem: find a substring in the text that matches the pattern Brute-force algorithm Step 1 Align pattern at beginning of text Step 2 Moving from left to right, compare each character of pattern to the corresponding character in text until all characters are found to match (successful search); or a mismatch is detected Step 3 While pattern is not found and the text is not yet exhausted, realign pattern one position to the right and repeat Step 2 A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

44 Examples of Brute-Force String Matching
Pattern: Text: Pattern: happy Text: It is never too late to have a happy childhood. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

45 Pseudocode and Efficiency
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

46 Closest-Pair Problem Find the two closest points in a set of n points (in the two-dimensional Cartesian plane). Brute-force algorithm Compute the distance between every pair of distinct points and return the indexes of the points for which the distance is the smallest. Draw example. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

47 Closest-Pair Brute-Force Algorithm (cont.)
The basic operation of the algorithm is computing the Euclidean distance between two points. The square root is a complex operation who’s result is often irrational, therefore the results can be found only approximately. Computing such operations are not trivial. One can avoid computing square roots by comparing distance squares instead. Efficiency: How to make it faster? A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

48 Brute-Force Strengths and Weaknesses
wide applicability simplicity yields reasonable algorithms for some important problems (e.g., matrix multiplication, sorting, searching, string matching) Weaknesses rarely yields efficient algorithms some brute-force algorithms are unacceptably slow not as constructive as some other design techniques A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

49 Exhaustive Search A brute force solution to a problem involving search for an element with a special property, usually among combinatorial objects such as permutations, combinations, or subsets of a set. Method: generate a list of all potential solutions to the problem in a systematic manner (see algorithms in Sec. 5.4) evaluate potential solutions one by one, disqualifying infeasible ones and, for an optimization problem, keeping track of the best one found so far when search ends, announce the solution(s) found A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

50 For Next Class, Friday Quiz 4 due Thursday night
Homework 3 due next Sunday night!


Download ppt "CSC 380: Design and Analysis of Algorithms"

Similar presentations


Ads by Google