Lecture # 02 - New
Lecture # 02 - New
Lecture # 02 - New
Lecture No 1
Important Point There may exist several algorithms for solving the same
problem.
Understand
the
problem
PROBLEM:
Some friends of yours are working on techniques for coordinating
groups of mobile robots. Each robot has a radio transmitter that it
uses to communicate with a base station, and your friends find that
if the robots get too close to one another, then there are problems
with interference among the transmitters. So, a natural problem
arises how to plan the motion of the robots in such a way that each
robot gets to its intended destination, but in the process the robots
don’t come close enough together to cause interference problems.
Develop a
Model
Design
Techniques
Methods of
Specifying an
Algorithm
Control flow
if … then … [else …]
while … do … Expressions
repeat … until …
Assignment
for … do … (like in Java)
Indentation replaces braces Equality testing
Method declaration (like in Java)
Algorithm method (arg [, arg…]) n 2
Superscripts and other
mathematical
Input …
formatting allowed
Output …
Prove the
Correctness of
the Algorithm
Other way: ?
rely on some mathematical proof to ensure that the
algorithm will always yield the correct output.
Correctness of algorithm can be confirmed by the
uses of some:
Inference Rules
Proving Techniques.
Analyzing the
Complexity of
the algorithm
Logic/Idea
Take the first element and check whether it is in the rest of the array,
take the second element and check if it is in the rest of the array, and
so on…
Department of Computer Science
Example-1
Problem: Design an algorithm that check whether all
the elements in a given array of n elements are
distinct.
Logic/Idea
Take the first element and check whether it is in the rest of the array,
take the second element and check if it is in the rest of the array, and
so on…
Logic/Idea?
Take first element and find its distance from P, take the second
element and find its distance from P, and so on… . Store these
Department of Computer Science
distances in an array A and find minimum from A.
Design Technique
Brute Force
Algorithm Steps: Now, let’s outline the
algorithm to calculate the nearest location
based on longitude and latitude:
1. Define the reference point with its longitude and
latitude coordinates.
2. Iterate over the set of locations and calculate the
distance between each location and the reference
point using the Haversine formula.
3. Keep track of the minimum distance encountered so
far and the corresponding location.
4. After iterating over all the locations, the location
with the minimum distance is the nearest location.
Department of Computer Science
Design Technique
CONCLUSION