SlideShare a Scribd company logo
1 of 43
Department of Computer Science
COMSATS University, Islamabad
Design and Analysis of Algorithm
Tanveer Ahmed Siddiqui
Department of Computer Science
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
Decide on : algorithm
design techniques etc.
Recap and Today Covered
Department of Computer Science
Reading Material
Read Chapter 3
Brute Force and Exhaustive Search
Department of Computer Science
 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
Objectives
Department of Computer Science
 Designing an algorithm is essentially a
creative effort containing all the ingredients
of a thriller:
 Adventure
 Excitement
 Challenge
 Suspense.
Designing an Algorithm
Department of Computer Science
Designing an algorithm is easy if you
know
design
techniques
Department of Computer Science
 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.
What is algorithm designing technique?
Department of Computer Science
 They provide us guidance in designing
algorithms for new problems
 They represent a collection of tools useful for
applications
Why do we need to know such techniques?
Department of Computer Science 9
 Brute force
 Decrease and conquer
 Divide and conquer
 Greedy technique
 Dynamic programming
 Backtracking
What are the most used techniques?
Department of Computer Science
 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
10
Motivation Discussion
Department of Computer Science
Motivation Discussion
State Space
Department of Computer Science
What is Brute 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:
12
What is Brute Force?
Department of Computer Science
What is 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
13
Known examples of Brute Force
Department of Computer Science
Designing
Algorithms
using Brute
Force
Department of Computer Science
 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
Example-1
Department of Computer Science
 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.
Example-1
Do you have another
idea to solve
exchange problem?
Department of Computer Science
 Do you have another idea to solve exchange
problem?
Example-1
Department of Computer Science
Brute Force Example
 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
18
Example-2: Computing an
Department of Computer Science
Brute Force Example
 Logic/Idea: x n =
 First response: Multiply 1 by x n times which is
the “Brute Force” approach.
19
Computing an
x × x × … … × x
n times
Department of Computer Science
Induction Examples (4/4)
3.3 Mathematical Induction
Example 3: Factorial of positive integer
 We want to compute n! = 1 × 2 × … … × n
Is there a
connection?
Department of Computer Science
 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
Example 4: Decimal to Binary Conversion
Department of Computer Science
 Problem: Design an algorithm for finding the
binary representation of a positive decimal
integer.
Representation
Example 4: Decimal to Binary Conversion
Department of Computer Science
 Problem: Design an algorithm that count
numbers of bits in a binary representation of a
given number n.
Representation
Your Turn
Department of Computer Science
Brute Force Examples
Some Examples
Department of Computer Science
 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.
 What should be the output
 The value of largest element in A. e.g., 10
 Which designing technique?
 Brute-Force
 Logic/Idea
4 1 8 9 3 7 10 2 3 1
Example-6: Largest element
Department of Computer Science
 Problem: Design an algorithm that find maximum
number in a given 2D array of nxn elements.
 Solution:
Your Turn
Department of Computer Science
 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
 A matrix C such that C = AB.
 Which designing technique?
 Brute-force
 Logic/Idea?
55 99 6 29 1 7 3
Example-7
Department of Computer Science
Brute Force Examples
Example 7: Matrix Multiplication
Department of Computer Science
Brute Force Examples
Example 7: Matrix Multiplication
Department of Computer Science
 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
Example-8: Searching
Department of Computer Science
Brute-Force Polynomial Evaluation
Problem: Design an algorithm that compute the
value of polynomial
p(x) = anxn + an-1xn-1 +… + a1x1 + a0
at a point x = x0
Example-9
Department of Computer Science
Brute-Force Polynomial Evaluation
Problem: Find the value of polynomial
p(x) = anxn + an-1xn-1 +… + a1x1 + a0
at a point x = x0
Brute-force algorithm
p  0.0
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
Example-9
Department of Computer Science
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
Example-9
Department of Computer Science
 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:
Example-10
:
Department of Computer Science
 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
Example-10
:
Department of Computer Science
 Logic/Idea
 But an alternative way of thinking about it is to
picture the original array, but re-labelled - like this
Example-10
:
2-D array index [i][j] => 1-D array index [i*5 + j]
Department of Computer Science
Example-10
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];
}
}
2-D array index [i][j] => 1-D array index [i*5 + j]
Department of Computer Science
What is Brute Force?
 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.
38
Where to Apply?
Department of Computer Science
CONCLUSION
Department of Computer Science
What is Brute 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
40
What is Brute Force?
Department of Computer Science
What is 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!
41
Strength and Weakness of Brute Force
Department of Computer Science 42
 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
How we do it?
Department of Computer Science 43
 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
How we do it?

More Related Content

Similar to Design and Analysis of Algorithm Brute Force 1.ppt

Perform brute force
Perform brute forcePerform brute force
Perform brute forceSHC
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxskilljiolms
 
Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosluzenith_g
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms  notesAnalysis and Design of Algorithms  notes
Analysis and Design of Algorithms notesProf. Dr. K. Adisesha
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...TechVision8
 
Kk20503 1 introduction
Kk20503 1 introductionKk20503 1 introduction
Kk20503 1 introductionLow Ying Hao
 
19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdf19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdfGOWTHAMR721887
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptxKokilaK25
 
Neural networks with python
Neural networks with pythonNeural networks with python
Neural networks with pythonSimone Piunno
 
Stack squeues lists
Stack squeues listsStack squeues lists
Stack squeues listsJames Wong
 
Stacksqueueslists
StacksqueueslistsStacksqueueslists
StacksqueueslistsFraboni Ec
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues listsYoung Alista
 

Similar to Design and Analysis of Algorithm Brute Force 1.ppt (20)

Midterm
MidtermMidterm
Midterm
 
Perform brute force
Perform brute forcePerform brute force
Perform brute force
 
Big Oh.ppt
Big Oh.pptBig Oh.ppt
Big Oh.ppt
 
Es272 ch1
Es272 ch1Es272 ch1
Es272 ch1
 
Alg1
Alg1Alg1
Alg1
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptx
 
Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmos
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms  notesAnalysis and Design of Algorithms  notes
Analysis and Design of Algorithms notes
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
 
Cs2251 daa
Cs2251 daaCs2251 daa
Cs2251 daa
 
3 analysis.gtm
3 analysis.gtm3 analysis.gtm
3 analysis.gtm
 
Algorithms DM
Algorithms DMAlgorithms DM
Algorithms DM
 
Kk20503 1 introduction
Kk20503 1 introductionKk20503 1 introduction
Kk20503 1 introduction
 
19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdf19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdf
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 
Design & Analysis Of Algorithm
Design & Analysis Of AlgorithmDesign & Analysis Of Algorithm
Design & Analysis Of Algorithm
 
Neural networks with python
Neural networks with pythonNeural networks with python
Neural networks with python
 
Stack squeues lists
Stack squeues listsStack squeues lists
Stack squeues lists
 
Stacksqueueslists
StacksqueueslistsStacksqueueslists
Stacksqueueslists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 

Recently uploaded

Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?Watsoo Telematics
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 

Recently uploaded (20)

Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 

Design and Analysis of Algorithm Brute Force 1.ppt

  • 1. Department of Computer Science COMSATS University, Islamabad Design and Analysis of Algorithm Tanveer Ahmed Siddiqui
  • 2. Department of Computer Science 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 Decide on : algorithm design techniques etc. Recap and Today Covered
  • 3. Department of Computer Science Reading Material Read Chapter 3 Brute Force and Exhaustive Search
  • 4. Department of Computer Science  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 Objectives
  • 5. Department of Computer Science  Designing an algorithm is essentially a creative effort containing all the ingredients of a thriller:  Adventure  Excitement  Challenge  Suspense. Designing an Algorithm
  • 6. Department of Computer Science Designing an algorithm is easy if you know design techniques
  • 7. Department of Computer Science  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. What is algorithm designing technique?
  • 8. Department of Computer Science  They provide us guidance in designing algorithms for new problems  They represent a collection of tools useful for applications Why do we need to know such techniques?
  • 9. Department of Computer Science 9  Brute force  Decrease and conquer  Divide and conquer  Greedy technique  Dynamic programming  Backtracking What are the most used techniques?
  • 10. Department of Computer Science  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 10 Motivation Discussion
  • 11. Department of Computer Science Motivation Discussion State Space
  • 12. Department of Computer Science What is Brute 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: 12 What is Brute Force?
  • 13. Department of Computer Science What is 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 13 Known examples of Brute Force
  • 14. Department of Computer Science Designing Algorithms using Brute Force
  • 15. Department of Computer Science  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 Example-1
  • 16. Department of Computer Science  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. Example-1 Do you have another idea to solve exchange problem?
  • 17. Department of Computer Science  Do you have another idea to solve exchange problem? Example-1
  • 18. Department of Computer Science Brute Force Example  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 18 Example-2: Computing an
  • 19. Department of Computer Science Brute Force Example  Logic/Idea: x n =  First response: Multiply 1 by x n times which is the “Brute Force” approach. 19 Computing an x × x × … … × x n times
  • 20. Department of Computer Science Induction Examples (4/4) 3.3 Mathematical Induction Example 3: Factorial of positive integer  We want to compute n! = 1 × 2 × … … × n Is there a connection?
  • 21. Department of Computer Science  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 Example 4: Decimal to Binary Conversion
  • 22. Department of Computer Science  Problem: Design an algorithm for finding the binary representation of a positive decimal integer. Representation Example 4: Decimal to Binary Conversion
  • 23. Department of Computer Science  Problem: Design an algorithm that count numbers of bits in a binary representation of a given number n. Representation Your Turn
  • 24. Department of Computer Science Brute Force Examples Some Examples
  • 25. Department of Computer Science  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.  What should be the output  The value of largest element in A. e.g., 10  Which designing technique?  Brute-Force  Logic/Idea 4 1 8 9 3 7 10 2 3 1 Example-6: Largest element
  • 26. Department of Computer Science  Problem: Design an algorithm that find maximum number in a given 2D array of nxn elements.  Solution: Your Turn
  • 27. Department of Computer Science  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  A matrix C such that C = AB.  Which designing technique?  Brute-force  Logic/Idea? 55 99 6 29 1 7 3 Example-7
  • 28. Department of Computer Science Brute Force Examples Example 7: Matrix Multiplication
  • 29. Department of Computer Science Brute Force Examples Example 7: Matrix Multiplication
  • 30. Department of Computer Science  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 Example-8: Searching
  • 31. Department of Computer Science Brute-Force Polynomial Evaluation Problem: Design an algorithm that compute the value of polynomial p(x) = anxn + an-1xn-1 +… + a1x1 + a0 at a point x = x0 Example-9
  • 32. Department of Computer Science Brute-Force Polynomial Evaluation Problem: Find the value of polynomial p(x) = anxn + an-1xn-1 +… + a1x1 + a0 at a point x = x0 Brute-force algorithm p  0.0 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 Example-9
  • 33. Department of Computer Science 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 Example-9
  • 34. Department of Computer Science  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: Example-10 :
  • 35. Department of Computer Science  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 Example-10 :
  • 36. Department of Computer Science  Logic/Idea  But an alternative way of thinking about it is to picture the original array, but re-labelled - like this Example-10 : 2-D array index [i][j] => 1-D array index [i*5 + j]
  • 37. Department of Computer Science Example-10 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]; } } 2-D array index [i][j] => 1-D array index [i*5 + j]
  • 38. Department of Computer Science What is Brute Force?  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. 38 Where to Apply?
  • 39. Department of Computer Science CONCLUSION
  • 40. Department of Computer Science What is Brute 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 40 What is Brute Force?
  • 41. Department of Computer Science What is 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! 41 Strength and Weakness of Brute Force
  • 42. Department of Computer Science 42  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 How we do it?
  • 43. Department of Computer Science 43  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 How we do it?