Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lecture Slides on Brute Force - Analysis Of Algorithms | CS 3343, Study notes of Algorithms and Programming

Material Type: Notes; Class: Analysis Of Algorithms; Subject: Computer Science; University: University of Texas - San Antonio; Term: Fall 2006;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-kpd
koofers-user-kpd 🇺🇸

10 documents

1 / 5

Toggle sidebar

Related documents


Partial preview of the text

Download Lecture Slides on Brute Force - Analysis Of Algorithms | CS 3343 and more Study notes Algorithms and Programming in PDF only on Docsity! Chapter 3: Brute Force Adequacy is sufficient. (Adam Osborne) Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Bubble Sort and Selection Sort 3 Bubble Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Correctness of Bubble Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Selection Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Efficiency of Selection Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Brute-Force String Matching 7 Brute-Force String Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Analysis of Brute-Force String Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Closest-Pair and Convex-Hull 9 Closest Pair Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Closest-Pair Brute-Force Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 The Convex Hull Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Examples of Convex Sets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Example of Convex Hull . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Idea for Solving Convex Hull . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Development of Idea for Convex Hull. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Exhaustive Search 16 Exhaustive Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 TSP Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 TSP Solution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Knapsack Problem Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1 Knapsack Problem Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 2 Introduction Brute force is a straightforward approach to solving a problem without regard for efficiency. Example: an O(n) algorithm for an: algorithm Power(a, n) // Input: A real number a and an integer n ≥ 0 // Output: an result ← 1 for i ← 1 to n do result ← result ∗ a return result CS 3343 Analysis of Algorithms Chapter 3: Slide – 2 Bubble Sort and Selection Sort 3 Bubble Sort My bubble sort varies from the book. algorithm BubbleSort(A[0..n− 1]) // Sorts a given array by bubble sort // Input: An array A of orderable elements // Output: Array A[0..n− 1] in ascending order sorted ← false while ¬sorted do sorted ← true for j ← 0 to n− 2 do if A[j] > A[j + 1] then swap A[j] and A[j + 1] sorted ← false CS 3343 Analysis of Algorithms Chapter 3: Slide – 3 3 Correctness of Bubble Sort  If A is not sorted, sorted is set to false, and loop continues.  Once a pair of elements are swapped, they won’t be swapped again.  n elements have n(n− 1)/2 different pairs, so at most n(n− 1)/2 swaps, so loop must eventually exit.  The number of comparisons is Θ(n2). See book. CS 3343 Analysis of Algorithms Chapter 3: Slide – 4 Selection Sort algorithm SelectionSort(A[0..n− 1]) // Sorts a given array by selection sort // Input: An array A of orderable elements // Output: Array A[0..n− 1] in ascending order for i ← 0 to n− 2 do min ← i for j ← i + 1 to n− 1 do if A[j] < A[min] then min ← i swap A[i] and A[min] CS 3343 Analysis of Algorithms Chapter 3: Slide – 5 Efficiency of Selection Sort  Correct because A[i] is minimum of A[i..n− 1].  The i = 0 pass (outer loop iteration) performs n− 1 comparisons.  The i = 1 pass performs n− 2 comparisons.  The last i = n− 2 pass performs 1 comparison.  The number of comparisons C(n) is Θ(n2). C(n) = n−2 ∑ i=0 (n− 1− i) = n−1 ∑ k=1 k = (n− 1)n 2 ≈ n2 2 CS 3343 Analysis of Algorithms Chapter 3: Slide – 6 4
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved