Algorithms
Discrete Structures
RIPHAH INTERNATIONAL UNIVERSITY LAHORE
Algorithms
What is an algorithm?
An algorithm is a finite set of precise instructions for performing a
computation or for solving a problem.
Algorithms
 Properties of algorithms:
• Input from a specified set,
• Output from a specified set (solution),
• Definiteness of every step in the computation,
• Correctness of output for every possible input,
• Finiteness of the number of calculation steps,
• Effectiveness of each calculation step and
• Generality for a class of problems.
Pseudocode
 An algorithm can also be described using a computer language.
Understanding an algorithm is complicated and difficult.
 Algorithm can be translated into any programming language. As
many programming languages are in common use, so instead of
using a particular computer language to specify algorithms, a form
of Pseudocode, will be used in this book.
 Pseudocode provides an intermediate step between an English
language description of an algorithm and an implementation of this
algorithm in a programming language.
Pseudocode
 Algorithm 1: Finding the Maximum Element in a Finite Sequence.
 procedure max(a1, a2, . . . , an: integers)
 max := a1
 for i := 2 to n
 if max < ai then max := ai
 return max{max is the largest element}
Pseudocode
 3 , 4 , 8 , 1 , 6 , 7 , 15 , 5 , 8 , 2
i ai max
3
2 (a2) 4 (3<4) 4
3 (a3) 8 (4<8) 8
4 (a4) 1 (8<1) 8
5 (a5) 6 (8<6) 8
6 (a6) 7 (8<7) 8
7 (a7) 15 (8<15) 15
8 (a8) 5 (15<5)15
9 (a9) 8 (15<8)15
10 (a10) 2 (15<2) 15
Linear Search:
 It is also called sequential search. It searches an element
sequentially by comparing the searching element with each
element in the list one by one.
 procedure linear search(x: integer, a1, a2, . . . , an: distinct integers)
 i := 1
 while (i ≤ n and x ≠ ai )
 i := i + 1
 if i ≤ n then location := i
 else location := 0
 return location {location is the subscript of the term that equals x,
or is 0 if x is not found}
Linear Search:
Search 5 from list 2 , 7 , 4 , 10 , 5 , 9
x=5 a1 a2 a3 a4 a5 a6
i=1 2 7 4 10 5 9
x=5 a1 a2 a3 a4 a5 a6
i=2 2 7 4 10 5 9
x=5 a1 a2 a3 a4 a5 a6
i=3 2 7 4 10 5 9
x=5 a1 a2 a3 a4 a5 a6
i=4 2 7 4 10 5 9
x=5 a1 a2 a3 a4 a5 a6
i=5 2 7 4 10 5 9
Element is found at Location=5
Linear Search:
Search 3 from list 2 , 7 , 4 , 10 , 5 , 9
x=3 a1 a2 a3 a4 a5 a6
i=1 2 7 4 10 5 9
x=3 a1 a2 a3 a4 a5 a6
i=2 2 7 4 10 5 9
x=3 a1 a2 a3 a4 a5 a6
i=3 2 7 4 10 5 9
Linear Search:
x=3 a1 a2 a3 a4 a5 a6
i=4 2 7 4 10 5 9
x=3 a1 a2 a3 a4 a5 a6
i=5 2 7 4 10 5 9
x=3 a1 a2 a3 a4 a5 a6
i=6 2 7 4 10 5 9
x=3 a1 a2 a3 a4 a5 a6
i=7 2 7 4 10 5 9
Element is not found Location=0
Binary Search:
 Binary Search algorithm is used to search an element from a list of
elements.
 This algorithm can be used when the list has terms occurring in
increasing order.
 It proceeds by comparing the element to be located to the middle
term of the list.
 The list is then split into two smaller sub lists of same size, or one of
these smaller lists has one fewer term than other.
 The search continues by restricting the search to the appropriate
sub lists based on the comparison of the element to be located and
the middle term.
Binary Search:
 procedure binary search (x: integer, a1, a2, . . . , an: increasing integers)
 i := 1{i is left endpoint of search interval}
 j := n {j is right endpoint of search interval}
 while i < j
 m :=
 if x > am then i := m + 1
 else j := m
 if x = ai then location := i
 else location := 0
 return location{location is the subscript i of the term ai equal to x, or 0 if x is not
found}
Binary Search:
Search 18 from sequence 2 , 3 , 5 , 8 , 10 , 15 , 18 , 30
i m j
2 3 5 8 10 15 18 30
As 18>8 and i<j so
i m j
2 3 5 8 10 15 18 30
As 18>15 and i<j so
i m j
2 3 5 8 10 15 18 30
Element found at location 7
Binary Search:
Search 3 from sequence 2 , 3 , 5 , 8 , 10 , 15 , 18 , 30
i m j
2 3 5 8 10 15 18 30
As 3<8 and i<j so
i m j
2 3 5 8 10 15 18 30
Element found at location 2
Binary Search:
Search 6 from sequence 2 , 3 , 5 , 8 , 10 , 15 , 18 , 30
i m j
2 3 5 8 10 15 18 30
As 6<8 and i<j so
i m j
2 3 5 8 10 15 18 30
6>3 and i<j so
i m j
2 3 5 8 10 15 18 30
6>5 and i<j
i m j
2 3 5 8 10 15 18 30
As i=j so element not found in the list
Sorting
 Sorting is putting the elements into a list in which the elements are in
increasing order.
Bubble Sort:
 The bubble sort is one of the simplest sorting algorithms, but not one
of the most efficient.
 It puts a list into increasing order by successively comparing
adjacent elements, interchanging them if they are in the wrong
order.
 To carry out the bubble sort, we perform the basic operation that is,
interchanging a larger element with a smaller one following it,
starting at the beginning of the list, for full pass.
 We iterate this procedure until the sort is complete.
Bubble Sort:
 procedure bubblesort(a1, . . . , an : real numbers with n ≥ 2)
 for i := 1 to n − 1
 for j := 1 to n − i
 if aj > aj+1 then interchange aj and aj+1
 {a1, . . . , an is in increasing order}
Bubble Sort:
Sort the list 4 , 7 , 3 , 5 , 2 using Bubble Sort
Pass 1:
4 7 3 5 2
4 7 3 5 2
4 7 3 5 2
4 3 7 5 2
4 3 5 7 2
4 3 5 2 7
Bubble Sort:
Pass2:
4 3 5 2 7
4 3 5 2 7
3 4 5 2 7
3 4 5 2 7
3 4 2 5 7
Pass3:
3 4 2 5 7
3 4 2 5 7
3 4 2 5 7
3 2 4 5 7
Bubble Sort:
Pass4:
3 2 4 5 7
3 2 4 5 7
2 3 4 5 7
Sorted list is
2 3 4 5 7
Insertion Sort:
 Insertion Sort is a simple sorting algorithm, but it is usually not the most
efficient.
 To sort a list with n elements, the insertion sort begins with the second
element.
 This second element is compared with the first element and inserted
before the first element if it is smaller than the first element and after
the first element if it is greater than the first element.
 At this point, the first two elements are in correct order.
 The third element is then compared with the first element, and if it is
larger than the first element, it is compared with the second element,
it is inserted into the correct position among the first three elements
and so on.
Insertion Sort:
 procedure insertion sort(a1, a2, . . . , an: real numbers with n ≥ 2)
 for j := 2 to n
 i := 1
 while aj > ai
 i := i + 1
 m := aj
 for k := 0 to j − i − 1
 aj−k := aj−k−1
 ai := m
 {a1, . . . , an is in increasing order}
Insertion Sort:
Sort the list 4 , 7 , 3 , 5 , 2 using Insertion Sort.
Pass 1:
4 7 3 5 2
4 7 3 5 2
4 7 3 5 2
Pass 2:
4 7 3 5 2
4 7 3 5 2
3 4 7 5 2
Insertion Sort:
Pass 3:
3 4 7 5 2
3 4 7 5 2
3 4 5 7 2
Pass 4:
3 4 5 7 2
3 4 5 5 2
2 3 4 5 7
Sorted list is
2 3 4 5 7

Lecture of algorithms and problem solving

  • 1.
  • 2.
    Algorithms What is analgorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a problem.
  • 3.
    Algorithms  Properties ofalgorithms: • Input from a specified set, • Output from a specified set (solution), • Definiteness of every step in the computation, • Correctness of output for every possible input, • Finiteness of the number of calculation steps, • Effectiveness of each calculation step and • Generality for a class of problems.
  • 4.
    Pseudocode  An algorithmcan also be described using a computer language. Understanding an algorithm is complicated and difficult.  Algorithm can be translated into any programming language. As many programming languages are in common use, so instead of using a particular computer language to specify algorithms, a form of Pseudocode, will be used in this book.  Pseudocode provides an intermediate step between an English language description of an algorithm and an implementation of this algorithm in a programming language.
  • 5.
    Pseudocode  Algorithm 1:Finding the Maximum Element in a Finite Sequence.  procedure max(a1, a2, . . . , an: integers)  max := a1  for i := 2 to n  if max < ai then max := ai  return max{max is the largest element}
  • 6.
    Pseudocode  3 ,4 , 8 , 1 , 6 , 7 , 15 , 5 , 8 , 2 i ai max 3 2 (a2) 4 (3<4) 4 3 (a3) 8 (4<8) 8 4 (a4) 1 (8<1) 8 5 (a5) 6 (8<6) 8 6 (a6) 7 (8<7) 8 7 (a7) 15 (8<15) 15 8 (a8) 5 (15<5)15 9 (a9) 8 (15<8)15 10 (a10) 2 (15<2) 15
  • 7.
    Linear Search:  Itis also called sequential search. It searches an element sequentially by comparing the searching element with each element in the list one by one.  procedure linear search(x: integer, a1, a2, . . . , an: distinct integers)  i := 1  while (i ≤ n and x ≠ ai )  i := i + 1  if i ≤ n then location := i  else location := 0  return location {location is the subscript of the term that equals x, or is 0 if x is not found}
  • 8.
    Linear Search: Search 5from list 2 , 7 , 4 , 10 , 5 , 9 x=5 a1 a2 a3 a4 a5 a6 i=1 2 7 4 10 5 9 x=5 a1 a2 a3 a4 a5 a6 i=2 2 7 4 10 5 9 x=5 a1 a2 a3 a4 a5 a6 i=3 2 7 4 10 5 9 x=5 a1 a2 a3 a4 a5 a6 i=4 2 7 4 10 5 9 x=5 a1 a2 a3 a4 a5 a6 i=5 2 7 4 10 5 9 Element is found at Location=5
  • 9.
    Linear Search: Search 3from list 2 , 7 , 4 , 10 , 5 , 9 x=3 a1 a2 a3 a4 a5 a6 i=1 2 7 4 10 5 9 x=3 a1 a2 a3 a4 a5 a6 i=2 2 7 4 10 5 9 x=3 a1 a2 a3 a4 a5 a6 i=3 2 7 4 10 5 9
  • 10.
    Linear Search: x=3 a1a2 a3 a4 a5 a6 i=4 2 7 4 10 5 9 x=3 a1 a2 a3 a4 a5 a6 i=5 2 7 4 10 5 9 x=3 a1 a2 a3 a4 a5 a6 i=6 2 7 4 10 5 9 x=3 a1 a2 a3 a4 a5 a6 i=7 2 7 4 10 5 9 Element is not found Location=0
  • 11.
    Binary Search:  BinarySearch algorithm is used to search an element from a list of elements.  This algorithm can be used when the list has terms occurring in increasing order.  It proceeds by comparing the element to be located to the middle term of the list.  The list is then split into two smaller sub lists of same size, or one of these smaller lists has one fewer term than other.  The search continues by restricting the search to the appropriate sub lists based on the comparison of the element to be located and the middle term.
  • 12.
    Binary Search:  procedurebinary search (x: integer, a1, a2, . . . , an: increasing integers)  i := 1{i is left endpoint of search interval}  j := n {j is right endpoint of search interval}  while i < j  m :=  if x > am then i := m + 1  else j := m  if x = ai then location := i  else location := 0  return location{location is the subscript i of the term ai equal to x, or 0 if x is not found}
  • 13.
    Binary Search: Search 18from sequence 2 , 3 , 5 , 8 , 10 , 15 , 18 , 30 i m j 2 3 5 8 10 15 18 30 As 18>8 and i<j so i m j 2 3 5 8 10 15 18 30 As 18>15 and i<j so i m j 2 3 5 8 10 15 18 30 Element found at location 7
  • 14.
    Binary Search: Search 3from sequence 2 , 3 , 5 , 8 , 10 , 15 , 18 , 30 i m j 2 3 5 8 10 15 18 30 As 3<8 and i<j so i m j 2 3 5 8 10 15 18 30 Element found at location 2
  • 15.
    Binary Search: Search 6from sequence 2 , 3 , 5 , 8 , 10 , 15 , 18 , 30 i m j 2 3 5 8 10 15 18 30 As 6<8 and i<j so i m j 2 3 5 8 10 15 18 30 6>3 and i<j so i m j 2 3 5 8 10 15 18 30 6>5 and i<j i m j 2 3 5 8 10 15 18 30 As i=j so element not found in the list
  • 16.
    Sorting  Sorting isputting the elements into a list in which the elements are in increasing order.
  • 17.
    Bubble Sort:  Thebubble sort is one of the simplest sorting algorithms, but not one of the most efficient.  It puts a list into increasing order by successively comparing adjacent elements, interchanging them if they are in the wrong order.  To carry out the bubble sort, we perform the basic operation that is, interchanging a larger element with a smaller one following it, starting at the beginning of the list, for full pass.  We iterate this procedure until the sort is complete.
  • 18.
    Bubble Sort:  procedurebubblesort(a1, . . . , an : real numbers with n ≥ 2)  for i := 1 to n − 1  for j := 1 to n − i  if aj > aj+1 then interchange aj and aj+1  {a1, . . . , an is in increasing order}
  • 19.
    Bubble Sort: Sort thelist 4 , 7 , 3 , 5 , 2 using Bubble Sort Pass 1: 4 7 3 5 2 4 7 3 5 2 4 7 3 5 2 4 3 7 5 2 4 3 5 7 2 4 3 5 2 7
  • 20.
    Bubble Sort: Pass2: 4 35 2 7 4 3 5 2 7 3 4 5 2 7 3 4 5 2 7 3 4 2 5 7 Pass3: 3 4 2 5 7 3 4 2 5 7 3 4 2 5 7 3 2 4 5 7
  • 21.
    Bubble Sort: Pass4: 3 24 5 7 3 2 4 5 7 2 3 4 5 7 Sorted list is 2 3 4 5 7
  • 22.
    Insertion Sort:  InsertionSort is a simple sorting algorithm, but it is usually not the most efficient.  To sort a list with n elements, the insertion sort begins with the second element.  This second element is compared with the first element and inserted before the first element if it is smaller than the first element and after the first element if it is greater than the first element.  At this point, the first two elements are in correct order.  The third element is then compared with the first element, and if it is larger than the first element, it is compared with the second element, it is inserted into the correct position among the first three elements and so on.
  • 23.
    Insertion Sort:  procedureinsertion sort(a1, a2, . . . , an: real numbers with n ≥ 2)  for j := 2 to n  i := 1  while aj > ai  i := i + 1  m := aj  for k := 0 to j − i − 1  aj−k := aj−k−1  ai := m  {a1, . . . , an is in increasing order}
  • 24.
    Insertion Sort: Sort thelist 4 , 7 , 3 , 5 , 2 using Insertion Sort. Pass 1: 4 7 3 5 2 4 7 3 5 2 4 7 3 5 2 Pass 2: 4 7 3 5 2 4 7 3 5 2 3 4 7 5 2
  • 25.
    Insertion Sort: Pass 3: 34 7 5 2 3 4 7 5 2 3 4 5 7 2 Pass 4: 3 4 5 7 2 3 4 5 5 2 2 3 4 5 7 Sorted list is 2 3 4 5 7