Beruflich Dokumente
Kultur Dokumente
11/14/2014
Strengths:
sum/product of n numbers
finding max/min in a list
searching
string matching
matrix multiplication
11/14/2014
Weaknesses:
11/14/2014
Selection Sort
Algorithm SelectionSort(A[0..n-1])
//The algorithm sorts a given array by selection sort
//Input: An array A[0..n-1] of orderable elements
//Output: Array A[0..n-1] sorted 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]
C(n)=(n-1)(n)/2
min j
(n2)
swap A[i] and A[min]
11/14/2014
Bubble Sort
Algorithm BubbleSort(A[0..n-1])
//The algorithm sorts array A[0..n-1] by bubble sort
//Input: An array A[0..n-1] of orderable elements
//Output: Array A[0..n-1] sorted in ascending order
for i 0 to n 2 do //bubble up the ith largest element
for j 0 to n-2-i do //j: the index of the left element in each pair
if A[j] > A[j+1]
C(n)=(n-1)(n)/2
swap A[j] and A[j+1]
(n2)
11/14/2014
Bubble Sort
Algorithm BubbleSort(A[0..n-1])
//The algorithm sorts array A[0..n-1] by bubble sort
//Input: An array A[0..n-1] of orderable elements
//Output: Array A[0..n-1] sorted in ascending order
for i 0 to n 2 do //bubble up the ith element
for j n-1 to i+1 do
if A[j] < A[j-1]
swap A[j] and A[j-1]
11/14/2014
Sequential Search
Algorithm SequentialSearch(A[0..n-1], K)
//Searches for a given value in a given array by sequential search
//Input: An array A[0..n-1] and a search key K
//Output: Returns the index of the first element of A that matches K
or 1 if there are no matching elements
i 0
Algorithm SequentialSearch2(A[0..n], K)
algorithm implements sequential search with a search
while i < n and A[i] K do //The
key as a sentinel
i i+ 1
A[n] = K
i 0
if i < n
while A[i] K do
return i
C(n)=n
ii+1
else
(n)
if i < n
return -1
return i
else
11/14/2014
return -1
P0
for i 1 to n do
power 1 // calculatingx i
for j 1 to i do
power power * x
Efficiency?
p p + a[i] * power
return p
11/14/2014
P a[0]
power 1
for i 1 to n do
power power * x // calculating x i
p p + a[i] * power
Efficiency?
return p
11/14/2014
The problem
Given:
Objective:
An example:
Pattern: name
Text: My name is Tom.
11/14/2014
10
2.
3.
Question: What is the last position that can still be the beginning
of a matching substring ?(assuming the texts positions are
indexed from 0 to n-1)
11/14/2014
1.
2.
11
Number of comparisons:
11/14/2014
12
13
Closest-Pair Problem
airplanes
post offices
statistical samples
DNA sequences, and so on
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.
11/14/2014
14
Efficiency:
How to make it faster?
11/14/2014
15
Exhaustive Search
E.g., traveling salesman problem, graph coloring problem (assign the smallest
number of colors to vertices of a graph so that no two adjacent vertices are the
same color.)
Method:
11/14/2014
16
Given n cities with known distances between each pair, find the
shortest tour that passes through all the cities exactly once
before returning to the starting city.
Alternatively: Find shortest Hamiltonian circuit in a weighted
connected graph.
Hamiltonian Circuit: a cycle that passes through all
the vertices of the graph exactly once.
Example:
b
5
3
4
11/14/2014
17
2+3+7+5
2+4+7+8
8+3+4+5
8+7+4+2
5+4+3+8
5+7+3+2
b
5
3
4
c
11/14/2014
Cost
= 17
= 21
= 20
= 21
= 20
= 17
11/14/2014
19
Total weight
{1}
{2}
{3}
{4}
{1,2}
{1,3}
{1,4}
{2,3}
{2,4}
{3,4}
{1,2,3}
{1,2,4}
{1,3,4}
{2,3,4}
{1,2,3,4}
2
5
10
5
7
12
7
15
10
15
17
12
17
20
22
Total value
$20
$30
$50
$10
$50
$70
$30
$80
$40
$60
not feasible
$60
not feasible
not feasible
not feasible
item
weight
value
1.
$20
2.
$30
3.
10
$50
4.
$10
11/14/2014
20
11/14/2014
21