You are on page 1of 15

CSE 115

Chapter 2 (Cont…)

Examples of Algorithmic
Problem Solving

CSE115 1
Example 1: Sequential Search
¾ Problem: Searching for a particular person’s name in a
telephone book.
¾ Formal Problem Specification:
¾ Let’s say that we have a list of 10,000 unsorted names called
N1, N2, N3 … N10,00 along with 10,000 telephone numbers of
those individuals denoted as T1, T2, T3, …, T10,000
¾ Assume that all names are unique and the list is not in
alphabetical order
¾ The algorithm takes the name of a specific person as input
called Name.
¾ If Name is found in position j ( 1 <= j <= 10000), the output of
the algorithm will be Tj
¾ If Name is not found in the book the output of the algorithm
will be the message: “I am sorry, but the name is not in the
directory.”

CSE115 2
Algorithm Discovery
¾ Algorithm Discovery:
¾ The process of finding a solution to a given
problem
¾ Most challenging and creative part of problem
solving process
¾ Involves equal parts of intelligence, hard work,
past experience, artistic skill, and good luck
¾ Gain experience by practicing (solve a lot of
problems)

CSE115 3
Sequential Search
¾ In the example the names are not in alphabetic
order so there is no clever way to speed up the
search
¾ One way of solving:
Step Operations
1 Get values for Name, N1, N2, … N10,000 and T1, T2, …, T10,000
2 If Name = N1 then print the value of T1
3 If Name = N2 then print the value of T2
. ..
. ..
10000 If Name = N9999 then print the value of T9999
10001 If Name = N10000 then print the value of T10000
10002 Stop

CSE115 4
C syntax for sequential search
# include <stdio.h>
algorithm
# include <string.h>
char name1 [] = "testing";
char name2 [] = "testing1";
char name[30] ;
long int t1 = 12345678;
long int t2 = 9101122;

int main()

{ printf ("enter the name to be searched\n");


gets(name);
if (strcmp(name,name1) == 0){
printf ("the telephone number is %ld\n",t1);
}
if (strcmp(name,name2) == 0){
printf ("the telephone number is %ld\n",t2);
}
printf("testing if");
return 0;
} CSE115 5
Problems with 1st algorithm
¾ The very simple algorithm that we have
just specified has several problems
¾ The solution is too long
¾ at 66 lines per page it will take 150 pages to
write out the 10002 steps
¾ It doesn’t stop even when it has found the
answer (common sense ?!)
¾ It doesn’t produce any result when the name is
not in the list, violating a fundamental
requirements

CSE115 6
Solution for the 1st algorithm
¾ Another way of solution:
¾ Use iteration, stop when you have found the solution and
produce a result even in case of lookup failure
Step Operation
1 Get values for Name, N1, N2, … N10,000 and T1, T2, …, T10,000
2 Set the value of i to 1 and set the value of Found to No
3 While both (Found = No) and (i <= 10,000) do steps 4 through 7
4 If Name is equal to the ith name on the List Ni then
5 Print the telephone number of that person, Ti
6 Set the value of Found to YES
Else (Name is not equal to Ni )
7 Add 1 to the value of i
8 If (Found = NO) then
Print the message ‘Sorry, the name is not in the directory’
9 Stop
CSE115 7
The Sequential Search
¾Some comments
¾ The algorithm is based on the assumption that
input data is not is alphabetical order
¾ For a sorted list the completely different
algorithm would have been designed/selected
¾ The selection of an algorithm to solve a
problem is greatly influenced by the way the
data are organized
¾ The data an algorithm processes decide how
faster the desired result can be produced

CSE115 8
Example 2: Finding Extremes
¾ Problem: Finding the largest/smallest value in a
list of numbers
¾ Used for finding the highest grade, salary or
lowest price
Formal problem specification:
Given a value n>=1 and a list containing exactly n unique
numbers called A1, A2, …, An find and print out both the
largest value in the list and the position in the list
where that largest value occurred.
¾ Example: if there are five values as
¾ 19, 41, 12, 63, 22 (n = 5) then
¾ Result: the algorithm will locate the largest value,63
and print the position 4
CSE115 9
Find Largest Algorithm
Step Operation
1 Get a value for n, the size of the list
2 Get values for A1, A2, …, An the list to be searched
3 Set the value of largest so far to A1
4 Set the value of location to 1
5 Set the value of i to 2
6 while ( i <= n) do
7 If Ai > the largest so far then
8 Set largest so far to Ai
9 Set location to i
10 Add 1 to the value of i
11 End of the loop
12 Print out the values of largest so far and location
13 Stop
CSE115 10
EXAMPLE3:Pattern Matching
¾ Formal definition of the Problem:
¾ You will be given some text composed of n characters that
will be referred to as T1 T2 …. Tn. You will also be given a
pattern of m characters, m <= n, that will be represented as
P1 P2 …. Pm. The algorithm must locate every occurrence of
the pattern within the text. The output of the algorithm is the
location in the text where each match occurred. For this
problem the location of a match is defined to be the index
position in the text where the match begins
¾ Example:
Text: to be or not to be, that is the question
Pattern: to
Output: Match starting at position 1, position 14

CSE115 11
Pattern Matching
¾Algorithm Discovery:
¾First, the pattern needs to be aligned under
a specific position
¾Second, the algorithm needs to check
whether there is a match at that position
¾Finally, the algorithm should slide the whole
pattern sequence ahead by one character
¾The first two steps can be considered to be
one part of the algorithm and the third to
be a separate part of the algorithm
¾High level algorithmic statements
CSE115 12
Pattern Matching Algorithm
¾ First Draft:
1 Get values for n and m, the size of the text and the pattern, respectively
2 Get values for both the text T1 T2 …. Tn and the pattern P1 P2 …. Pm
3 Set k, the starting location for the attempted match, to 1
4 Keep going until we have fallen off the end of the text
5 Attempt to match every character in the pattern beginning at position k of
the text
6 If there was a match then
7 Print the value of k, the starting location of the match
8 Add 1 to k, which slides the pattern forward one position
9 End of the loop
10 Stop

CSE115 13
Pattern Matching Algorithm (Final)
¾ Detail Specification:
Attempt to match every character in the pattern
beginning at the position k of the text
¾ When this statement is reached the pattern is aligned under
the text beginning with the kth character. Pictorially we are in
the following situation:
Text: T1 T2 T3…………TK TK+1TK+2 ………… TK+(m-1)……………..
Pattern: P1 P2 P3……………..Pm

Set the value of i to 1


Set the value of mismatch to NO
while both (i <= m) and (mismatch equals NO)
If Pi is not equal to Tk+(i-1) then
Set mismatch to YES
Else
Increment i by 1 (to move to the next character)
End of the loop

CSE115 14
The Final Algorithm
1 Get values for n and m, the size of the text and the pattern, respectively
2 Get values for both the text T1 T2 …. Tn and the pattern P1 P2 …. Pm
3 Set k, the starting location for the attempted match, to 1
4 while( k <= (n – m + 1)) do
5 Set the value of i to 1
6 Set the value of mismatch to NO
7 While both (i <= m) and (mismatch equals NO)
8 If Pi is not equal to Tk+(i-1) then
9 Set mismatch to YES
Else
10 Increment i by 1 (to move to the next character)
11 End of the loop
12 If (mismatch equals No) then
13 Print the message “There is a match at position”
14 Print the value of k
15 Increment k by 1
16 End of the Loop
17 Stop (End of Algorithm)
CSE115 15

You might also like