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

Type of Algorithm - Discrete Mathematics - Lecture Slides, Slides of Discrete Mathematics

During the study of discrete mathematics, I found this course very informative and applicable.The main points in these lecture slides are:Type of Algorithm, Finite Set of Precise Instructions, Maximum Element, Programming Language, Pseudocode, Element Running Time, Worst Case Scenario, Properties of Algorithms, Searching Algorithms, Linear Search

Typology: Slides

2012/2013

Uploaded on 04/27/2013

atasi
atasi 🇮🇳

4.6

(32)

136 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithms
1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Type of Algorithm - Discrete Mathematics - Lecture Slides and more Slides Discrete Mathematics in PDF only on Docsity!

Algorithms

What is an algorithm?

  • An algorithm is “a finite set of precise instructions for performing a computation or for solving a problem” - A program is one type of algorithm - All programs are algorithms - Not all algorithms are programs! - Directions to somebody’s house is an algorithm - A recipe for cooking a cake is an algorithm - The steps to compute the cosine of 90° is an algorithm

Algorithm 1: Maximum element

  • Given a list, how do we find the maximum

element in the list?

  • To express the algorithm, we’ll use

pseudocode

  • Pseudocode is kinda like a programming language, but not really

Algorithm 1: Maximum element

  • Algorithm for finding the maximum element in

a list:

procedure max ( a 1 , a 2 , …, a (^) n : integers) max := a 1 for i := 2 to n if max < a (^) i then max := a (^) i

{ max is the largest element}

Maximum element running time

  • How long does this take?
  • If the list has n elements, worst case scenario

is that it takes n “steps”

  • Here, a step is considered a single step through the list

Properties of algorithms

  • Algorithms generally share a set of properties:
    • Input: what the algorithm takes in as input
    • Output: what the algorithm produces as output
    • Definiteness: the steps are defined precisely
    • Correctness: should produce the correct output
    • Finiteness: the steps required should be finite
    • Effectiveness: each step must be able to be performed in a finite amount of time
    • Generality: the algorithm should be applicable to all problems of a similar form

Algorithm 2: Linear search

  • Given a list, find a specific element in the list
    • List does NOT have to be sorted!

procedure linear_search ( x : integer; a 1 , a 2 , …, a (^) n : integers) i := 1 while ( in and xa (^) i ) i := i + 1 if in then location := i else location := 0

{ location is the subscript of the term that equals x, or it is 0 if x is not found}

Algorithm 2: Linear search, take 1

11

procedure linear_search ( x : integer; a 1 , a 2 , …, an : integers)

i := 1 while ( in and xai )

i := i + 1 if in then location := i

else location := 0

i := 1 while ( in and xai )

i := i + 1 if in then location := i

else location := 0

a 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 10

i 23456781

x 3

location 8

Linear search running time

  • How long does this take?
  • If the list has n elements, worst case scenario

is that it takes n “steps”

  • Here, a step is considered a single step through the list

Algorithm 3: Binary search

  • Given a list, find a specific element in the list
    • List MUST be sorted!
  • Each time it iterates through, it cuts the list in half

procedure binary_search ( x : integer; a 1 , a 2 , …, an : increasing integers) i := 1 { i is left endpoint of search interval } j := n { j is right endpoint of search interval } while i < j begin m := ( i + j )/2 { m is the point in the middle } if x > am then i := m + else j := m end if x = ai then location := i else location := 0 { location is the subscript of the term that equals x, or it is 0 if x is not found}

Algorithm 3: Binary search, take 2

16

a 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 10

i m j

i := 1 j := n

procedure binary_search ( x : integer; a 1 , a 2 , …, a (^) n : increasing integers) while i < j begin m := ( i + j )/2 if x > am then i := m + else j := m end

if x = ai then location := i else location := 0

i := 1 j := n

while i < j begin m := ( i + j )/2 if x > am then i := m + else j := m end

if x = ai then location := I else location := 0

x 15

location 0

Algorithm 3: Binary search

  • A somewhat alternative view of what a binary

search does…

Sorting algorithms

  • Given a list, put it into some order
    • Numerical, lexicographic, etc.
  • We will see two types
    • Bubble sort
    • Insertion sort

Algorithm 4: Bubble sort

  • One of the most simple sorting algorithms
    • Also one of the least efficient
  • It takes successive elements and “bubbles” them up the list

procedure bubble_sort ( a 1 , a 2 , …, a (^) n ) for i := 1 to n - for j := 1 to n - i if a (^) j > a (^) j + then interchange a (^) j and a (^) j + { a 1 , …, a (^) n are in increasing order }