You are on page 1of 15

What Are Algorithms

Chapter - 6
Algorithms
● Derived from name of Persian mathematician,
Mohammed ibn-Musa al-Khwarizmi.
● Mohammed lived in Baghdad between 780 and 850.
● Also called pseudo-codes.
● Digital system are dumb and need instructions.
● Algorithms are language independent and unambiguous.
● Predefined set of instructions.
● Speed and efficiency will be central focus while
developing algorithms.
● Initial step before developing software.
Search And Sort Algorithms
● Search algorithms search in arrays for item.
● Algorithms are:
○ Binary search
○ Linear search
● Sort algorithms arranges elements.
● Sort algorithms are:
○ Bubble sort based on swapping.
○ Merge sort based on divide and conquer approach.
○ Insertion sort compares and interchange elements.
○ Selection sort makes two sub arrays and uses smallest element.
Types Of Algorithms
● Different algorithms for various purposes.
● Varies on performance, complexity and efficiency.
● Developers select suitable algorithms for each service.
● Six major types of algorithms.
● Key types are:
○ Recursive algorithm
○ Greedy algorithm
○ Divide and conquer algorithm
○ Brute force algorithm
○ Dynamic programming algorithm
○ Backtracking algorithm
Recursive Algorithm
● Takes smaller inputs
● Repeats until a solution have been found.
● Tower of Hanoi and DFG graph searches are applications.
● Represents a natural way of thinking.
● More computational intensive and need more memory.
● Replacement for iterative algorithms.
Greedy Algorithm
● Intuitive algorithm to find optimal solution.
● Considers all data associated in a problem.
● Set rules for each step.
● Main applications in Dijkstra's algorithm and Huffman encoding.
● Incomplete data leads to failure in finding globally optimal solution.
● Five components in algorithm:
o Candidate set where solutions will be found.
o Selection function to find the best candidate.
o Feasibility function decides if candidate can be used.
o Objective function assigns value to solution.
o Solution function notifies if solution has been found.
Divide And Conquer Algorithm
● Entire problem divided into atomic sub-problems.
● Each sub-problems executed independently.
● Solutions combined.
● Used in merge sort, Strassen’s matrix multiplication and binary search.
● 3 step process:
○ Dividing the problems.
○ Solving sub-problems.
○ Merging the solutions.
Pictorial Representation Of Divide And Conquer
algorithm
Brute Force Algorithm
● Most straightforward algorithm.
● Finds more than one solution.
● Does not have pre-processing phase.
● Comparisons in any order possible.
● Not optimal.
● Higher time and space complexity.
● Iterates to find more than one solution.
Dynamic Programming Algorithm
● Uses partial past data.
● Higher efficiency and optimal solution.
● Concept of memorization used.
● Avoids repetition using past data.
● Overlapping sub-problems and optimal substructures can be solved.
● Bellman-Ford, Unix diff, traveling salesman are common applications.
Backtracking Algorithm
● Systematic approach.
● Solve problems incrementally.
● Deletes solutions that does not satisfy constraints.
● Sudoku and N-Queens are common applications.
● N-Queen based on fact no two queens attack each other.
● Algorithm delete solution if not possible.
A Solution For N-Queens Problem
Algorithm Developmental Process
● Obtain problem description
o Developers clarify requirements with clients.
● Problem analyzing
○ Develop facts from data and interconnections.
● Developing high-level algorithm
○ Develop solution plan.
● Refinement
○ Step-wise refinement.
○ Include minor details.
● Review
○ Ensures optimal solution only.
Run Time Analysis
● Give insights on time and space complexity.
● Efficiency measured using:
○ Omega (Ω) notation that expresses lower bound execution time.
○ Theta (Θ) notation that represents average execution time.
○ Big-o (O) notation represents higher bound of algorithm.
● Omega notation shows best case.
● Theta represents asymptotic behavior.
● Big-o represents worst case.
Food For Thought
● Develop an algorithm for string reversal.

Step-1: Start
Step-2: Take two variables i and j
Step-3: Do length (string)-1, to assign J at last position
Step-4: Do string [0], to assign i on first character.
Step-5: string [i] is exchanged with string[j]
Step-6: Increment i by 1
Step-7: Increment j by 1
Step-8: if (i>j) then go to Step-3
Step-9: Stop

You might also like