Download presentation

Presentation is loading. Please wait.

1
**College of Information Technology & Design**

Discrete Mathematics Algorithms University of Jazeera College of Information Technology & Design Khulood Ghazal

2
**Algorithms An algorithm :**

is a finite set of precise instructions for performing a computation or for solving a problem. OR it is a sequence of steps leads to the desire answer. How can we describe an algorithm ? 1. English language. 2.Pseudocode. 3.Computer language.

3
Algorithms Example 1: Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers. Solution :( using English language ) . Set the temporary maximum equal to the first integer in the sequence. (The temporary maximum will be the largest integer examined at any stage of the procedure.) Compare the next integer in the sequence to the temporary maximum, and if it is larger than the temporary maximum, set the temporary maximum equal to this integer. Repeat the previous step if there are more integers in the sequence. Stop when there are no integers left in the sequence. The temporary maximum at this point is the largest integer in the sequence.

4
**Algorithms n is the number of integers in the list.**

The pseudocode for Finding the Maximum Element in a Finite Sequence. n is the number of integers in the list. procedure max ( a1,a2,…,an : integers ) max := a1 for i := 2 to n if ai > max then max := ai return max { max is the largest element } Example : list a= { } max= n = 3 i=2 4>6 (F) i=3 10>6 (T) then max=10 i=4 return 10 if ai > max =F And i<=n

5
**Properties of Algorithms**

Properties that all algorithms generally share are: Input : An algorithm has input values from a specified set. Output : From each set of input values an algorithm produces output values from a specified set. The output values are the solution to the problem. Definiteness : The steps of the algorithm must be defined precisely. Correctness: An algorithm should produce the correct output values for each set of input values. Finiteness : An algorithm should produce the desired output after a finite (but perhaps large) number of steps for any input in the set. Effectiveness: It must be possible to perform each step of an algorithm exactly and in a finite amount of time. Generality: The function should be applicable for all problems of the desired form, not just for a particular set of input values.

6
**Searching Algorithms Problem of searching an ordered list.**

Given a list L of n elements that are sorted into a definite order (e.g., numeric, alphabetical), And given a particular element x, Determine whether x appears in the list, and if so, return its index (position) in the list.

7
**Search algorithm #1: Linear Search**

Searching Algorithms Search algorithm #1: Linear Search Basic idea : The algorithm begins by comparing x and a1 .when x = a1 the solution is the location of a1 (namely 1). when x a1 ,compare x with a2 ,if x= a2 ,the solution is the location of a2 (namely 2 ) . When xa2 ,compare x with a3 . continue this process, comparing x successively with each term of the list until a match is found ,where the solution is the location of that term, unless no match occurs, the solution is 0.

8
**Linear Search The pseudocode for the linear search algorithm**

n is the number of integers in the list. procedure linear search (x: integer, a1, a2, …, an: distinct integers) i := while (i n x ai ) i := i + 1 End if i n then location := i else location := 0 return location {index or 0 if not found} Example : list a={ } x= n=4 i=1 1 4 7 3 (T ^ T=T) i=2 2 4 76 (T ^ T=T) i=3 3 4 77 (T ^ F=F) 3 4 then location:=3 return 3 (index of 7)

9
**Search algorithm #2: Binary Search**

Discrete Mathematics and its Applications Search algorithm #2: Binary Search Basic idea: On each step, look at the middle element of the remaining list to eliminate half of it. <x <x <x >x (c) , Michael P. Frank

10
**Search algorithm #2: Binary Search**

Discrete Mathematics and its Applications Search algorithm #2: Binary Search Example : To search for 19 in the list. First split this list, which has 16 terms into two smaller list with eight term each; namely : Then compare 19 and the largest term in the first list , since 10<19 ,the search for 19 can be restricted to the list containing the numbers(12 13…….22 -9th to 16th terms ) next split this list into two smaller lists of four terms each ,namely : Since 16<19 the search restricted to the second of these lists(13th to 16th terms). The list is split into two lists, namely : Since 19 not <19 the search restricted to the first list ( _13th and 14th terms ) Next this list is split into two lists of one term each: Since 18<19 the search restricted to the second list ,the list containing 19 (14th term of the list) and 19 is located as 14th term of the original list . (c) , Michael P. Frank

11
**Search algorithm #2: Binary Search**

The pseudocode for the binary search algorithm n is the number of integers in the list. procedure binary search(x:integer, a1, a2, …, an: distinct integers) i := 1 {left endpoint of search interval} j := n {right endpoint of search interval} while i < j begin {while interval has >1 item} m := (i + j ) /2 {midpoint} if x > am then i := m+1 else j := m end if x = ai then location := i else location := 0 return location

Similar presentations

OK

CSE115/ENGR160 Discrete Mathematics 02/24/11 Ming-Hsuan Yang UC Merced 1.

CSE115/ENGR160 Discrete Mathematics 02/24/11 Ming-Hsuan Yang UC Merced 1.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on metals used in construction Ppt on regional trade agreements in africa Ppt on sikkim cultural Ppt on rural retailing in india Ppt on water softening techniques definition Ppt on purchase order Ppt on natural disaster for class 8 Ppt on instrument landing system theory Ppt on history of indian cricket Ppt on standing order act 1946 ford