0% found this document useful (0 votes)
120 views43 pages

Algorithm Design for CS Students

This document summarizes a lecture on algorithm design and analysis using brute force techniques. It discusses brute force as a straightforward approach that iterates through all possible solutions. Examples of problems solved using brute force include swapping variables, computing powers and factorials, sorting, searching, and matrix multiplication. Students were given examples and asked to design brute force algorithms for additional problems like finding maximums in arrays and matrices.

Uploaded by

moiza354
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views43 pages

Algorithm Design for CS Students

This document summarizes a lecture on algorithm design and analysis using brute force techniques. It discusses brute force as a straightforward approach that iterates through all possible solutions. Examples of problems solved using brute force include swapping variables, computing powers and factorials, sorting, searching, and matrix multiplication. Students were given examples and asked to design brute force algorithms for additional problems like finding maximums in arrays and matrices.

Uploaded by

moiza354
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 43

Design and Analysis of Algorithm

Tanveer Ahmed Siddiqui

Department of Computer Science


COMSATS University, Islamabad
Recap and Today Covered

Algorithm Design and Analysis Process


Understand the problem

Decide on : algorithm
design techniques etc.

Design an algorithm

Prove correctness

Analyze efficiency etc

Code the algorithm


Department of Computer Science
Reading Material

Read Chapter 3
Brute Force and Exhaustive Search

Department of Computer Science


Objectives

 How to Design Algorithm using Brute Force(BF) Approach


 Swapping two numbers
 Computing an (a > 0, n a nonnegative integer)
 Computing n!
 Add n numbers
 Decimal to Binary conversion
 Finding Maximum/Minimum Element in a list
 Multiply two n by n Matrices
 Selection Sort
 Bubble Sort
 Sequential Search/Linear Search
 Conversion of a 2D array into 1D array
 Find the value of polynomial
Department of Computer Science
Designing an Algorithm

 Designing an algorithm is essentially a


creative effort containing all the ingredients
of a thriller:
 Adventure
 Excitement
 Challenge
 Suspense.

Department of Computer Science


Designing an algorithm is easy if you
know

design
techniques

Department of Computer Science


What is algorithm designing technique?

 An algorithm design techniques (or “strategy”


or “paradigm”) is a general approach to solve
problems algorithmically
 It can be applicable to a variety of problems
from different area of computing.
 The design approach depends mainly on the
model chosen.

Department of Computer Science


Why do we need to know such techniques?

 They provide us guidance in designing


algorithms for new problems
 They represent a collection of tools useful
for applications

Department of Computer Science


What are the most used techniques?

 Brute force

 Decrease and conquer

 Divide and conquer

 Greedy technique

 Dynamic programming

 Backtracking

Department of Computer Science 9


Motivation Discussion

 How you solve this puzzle:


 Given a 3×3 board with 8 tiles (every tile has
one number from 1 to 8) and one empty space.
The objective is to place the numbers on tiles to
match the final configuration using the empty
space. We can slide four adjacent (left, right,
above, and below) tiles into the empty space.
 Initial state : any configuration
 Goal state : tiles in a specific order

Department of Computer Science 10


Motivation Discussion

State Space

Department of Computer Science


What is Brute
What is Brute Force? Force?
 A straightforward approach to solving a problem,
usually directly based on the problem’s statement
and definitions of the concepts involved.
 “Brute Force” means “Just do it!”
 Generally, it involved iterating through all possible
solutions until a valid one is found.
 Other Names
Generate and Test
 Exhaustive Search
 Can you name some problems that can be solved
with BF
 Examples:
Department of Computer Science 12
Known examples of
What is Brute Force?Brute Force
 How many examples do you know?
 Swapping two numbers
 Computing an (a > 0, n a nonnegative integer)
 Computing n!
 Add n numbers
 Decimal to Binary conversion
 Finding Maximum/Minimum Element in a list
 Multiply two n by n Matrices
 Selection Sort
 Bubble Sort
 Sequential Search/Linear Search
 Conversion of a 2D array into 1D array
 Find the value of polynomial
Department of Computer Science 13
Designing
Algorithms
using Brute
Force

Department of Computer Science


Example-1

 Problem: Design an algorithm that Exchange the


values of two variables say x and y.
 Solution:
 What is input
 Two numbers say x and y.
 What should be the output
 Interchange the value of x and y
 Which designing technique?
 Brute-Force

 Logic/Idea

Department of Computer Science


Example-1
 Logic/Idea
 Take a temporary variable temp to swap the
numbers.
 The contents of the first variable is copied into the
temp variable.
 Then, the contents of second variable is copied to the
first variable.
 Finally, the contents of the temp variable is copied
back to the second variable which completes the
swapping process.
Do you have another
idea to solve
exchange problem?

Department of Computer Science


Example-1

 Do you have another idea to solve exchange


problem?

Department of Computer Science


Example-2: Computing
Brute Force Example a n

 In RSA (Ron Rivest, Adi Shamir, and Leonard


Adleman) encryption algorithm we need to
compute an mod m for a > 1 and large n.
 Design an algorithm that computes an using BF
 Solution:
 What is input
 Two numbers say x and n.
 What should be the output
 Interchange the value of xn
 Which designing technique?
 Brute-Force

 Logic/Idea
Department of Computer Science 18
Computing
Brute Force Example a n

 Logic/Idea: x n = x × x × … … × x

n times

 First response: Multiply 1 by x n times which is


the “Brute Force” approach.

Department of Computer Science 19


Example
Induction Examples 3: Factorial
(4/4) of positive integer
3.3 Mathematical Inducti

 We want to compute n! = 1×2×……×n

Is there a
connection?

Department of Computer Science


Example 4: Decimal to Binary Conversion
 Problem: Design an algorithm for finding the
binary representation of a positive decimal
integer.
 Solution:
 What is input
 Positive integer n.
 What should be the output
 Binary string bk bk bk-1 bk-2………….. b1 b0
 Which designing technique?
 Brute-Force

 Logic/Idea

Department of Computer Science


Example 4: Decimal to Binary Conversion
 Problem: Design an algorithm for finding the
binary representation of a positive decimal
integer.
Representation

Department of Computer Science


Your
Turn
 Problem: Design an algorithm that count
numbers of bits in a binary representation of a
given number n.
Representation

Department of Computer Science


BruteSome
ForceExamples
Examples

Department of Computer Science


Example-6: Largest element
 Problem: Design an algorithm that find the
maximum number in a given array of n elements.
 Solution:
 What is input
 An array A of n numbers e.g. 4 1 8 9 3 7 10 2 3 1

 What should be the output


 The value of largest element in A. e.g., 10
 Which designing technique?
 Brute-Force

 Logic/Idea

Department of Computer Science


Your Turn

 Problem: Design an algorithm that find maximum


number in a given 2D array of nxn elements.
 Solution:

Department of Computer Science


Example-7
 Problem: Design an algorithm that multiply two matrices
A and B
 Solution:
 What is input
 Two 2D array A and B of compatible size.
 What should be the output 55 99 6 29 1 7 3
 A matrix C such that C = AB.
 Which designing technique?
 Brute-force

 Logic/Idea?

Department of Computer Science


Example 7:Brute
Matrix Multiplication
Force Examples

Department of Computer Science


Example 7:Brute
Matrix Multiplication
Force Examples

Department of Computer Science


Example-8: Searching

 Problem: Design an algorithm that find a key in a


given array of n elements.
 Solution:
 What is input
 An array A of n numbers and a key say K
 What should be the output
 The location of key if found otherwise return false
 Which designing technique?
 Brute-Force

 Logic/Idea

Department of Computer Science


Brute-Force PolynomialExample-9
Evaluation

Problem: Design an algorithm that compute the


value of polynomial
p(x) = anxn + an-1xn-1 +… + a1x1 + a0

at a point x = x0

Department of Computer Science


Brute-Force PolynomialExample-9
Evaluation

Problem: Find the value of polynomial


p(x) = anxn + an-1xn-1 +… + a1x1 + a0

at a point x = x0
Brute-force
p  0.0
algorithm
for i  n downto 0 do
power  1
for j  1 to i do //compute xi
power  power  x
p  p + a[i]  power

return p

Department of Computer Science


Example-9
Polynomial Evaluation: Improvement
We can do better by evaluating from right to left:
Better brute-force algorithm
p  a[0]
power  1
for i  1 to n do
power  power  x
p  p + a[i]  power
return p

Department of Computer Science


:
Example-10
 Problem: Design an algorithm convert a 2D array
into 1D array
 Solution:
 What is input
 An 2D-array A[i][j]. e. g. int [5][5]
 What should be the output
 You could picture the conversion
 to the corresponding 1-D array
 like this:

Department of Computer Science


:
Example-10
 Which designing technique?
 Brute-Force

 Logic/Idea
 A 1-D array looks like this:int [5] :

 You could picture the conversion to the corresponding 1-D array


like this

Department of Computer Science


:
Example-10
 Logic/Idea
 But an alternative way of thinking about it is to
picture the original array, but re-labelled - like this

2-D array index [i][j] => 1-D array index [i*5 + j]

Department of Computer Science


Example-10

2-D array index [i][j] => 1-D array index [i*5 + j]

ALGORITHM Convert2d_into_1D(arr2D[i][j], arr1D [n*m])


Input:
OutPut:
for (i = 0; i < n; ++i)
{
for (j = 0; j < m; ++j)
{ // mapping 2D array to 1D array
arr1D[i * m + j] = arr2D[i][j];
}
}

Department of Computer Science


Where
What is Brute Force?to Apply?

 Numerical problems, searching, sorting, etc.


 Acceptable efficiency
 Can be used for large problem instances
 Combinatorial problems
 Exhaustive search
 Set of candidate solutions grows very fast
 Used only for reduced size instances.
 Let us apply Brute force approach for solving
sorting problem.

Department of Computer Science 38


CONCLUSION

Department of Computer Science


What is Brute
What is Brute Force? Force?

 The (most) straightforward approach for solving


a problem.
 “Brute Force” means “Just do it!”
 Directly based on
 The problem statement
 The definitions involved
 Generally, it involved iterating through all possible
solutions until a valid one is found.
 Other Names
 Generate and Test
 Exhaustive Search

Department of Computer Science 40


Strength and Weakness of
What is Brute Force? Brute Force

 Strengths
 Simplicity
 Applicable to different kinds of problems
 Weaknesses
 (Very!) Low efficiency in some cases
 Useful only for instances of (relatively) small size!

Department of Computer Science 41


How we do it?
 Algorithms should be learn at a higher level
with linkages to prior knowledge and with each
other so as to see how one algorithm is different
from the rest and what it does and does not
perform, and why?
 In this integrative approach of learning the initial
investment is large but it is essential for
meaningful learning

Department of Computer Science 42


How we do it?
 Intuition First & Formalism Later
 Common & Small Building Blocks
 Design of Cruder versions providing a ladder
 Visual patterns or puzzles
 We do not tell the story of an algorithm?
 The interesting story connects all characters and
provides a panoramic picture
 Platforms for Comparisons

Ladder

Department of Computer Science 43

You might also like