Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Algorithm

algorithm

Uploaded by

miskbadr64
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Algorithm

algorithm

Uploaded by

miskbadr64
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Algorithms-CS101

What are Algorithms?


Algorithms are a set of instructions or steps designed to perform a specific task or to solve
a particular problem. They are essential in computer science for data processing, calculations,
and other tasks.

Algorithm Design Techniques (Definition + Applications + Algorithms)


Algorithm
Definition Applications Examples
Design Technique
Brute Force Simple and straightforward Finding the Naive string
method of solving problems maximum element matching, Bubble
by trying all possible in an array, checking sort.
solutions until the correct all substrings.
one is found.

Divide and Conquer Breaking down a problem Sorting algorithms, Merge sort,
into smaller subproblems, binary search. Quicksort.
solving each subproblem
independently, and
combining their solutions
to solve the original
problem.

Dynamic Solving problems by Optimization Fibonacci sequence,


Programming breaking them down problems, Knapsack problem.
into simpler subproblems sequence alignment.
and storing the results of
subproblems to avoid
redundant work.

Greedy Algorithms Making the locally optimal Optimization Dijkstra's algorithm,


choice at each step with problems, Prim's algorithm.
the hope of finding the scheduling.
global optimum.

Backtracking Building solutions Constraint N-Queens problem,


incrementally and satisfaction Sudoku solver.
abandoning solutions problems, puzzles.
that fail to satisfy the
constraints of the problem.

Randomized Using random numbers to Optimization Quick sort


Algorithms make decisions during the problems, (randomized),
algorithm’s execution. Monte Carlo Randomized
methods. algorithms for
median finding.

Linear Programming Optimizing a linear Operations research, Simplex algorithm,


objective function economics. Interior-point
subject to linear methods.
equality and inequality
constraints.
Algorithms-CS101
Branch and Bound Systematically enumerating Optimization Traveling salesman
candidate solutions by problems, problem, Knapsack
means of state-space combinatorial problem.
search. problems.

Reduction Transforming a problem Problem-solving Reducing a problem


(Transform and into a different version strategies, proving to graph theory,
Conquer) or into another NP-completeness. reducing a sorting
problem entirely. problem to a
selection problem.

Minimum Spannin A subset of edges in a Network design, Kruskal's algorithm,


Trees weighted graph that circuit design. Prim's algorithm.
connects all the vertices
without any cycles and
with the minimum possible
total edge weight.

Classification by Design Approach (Definition + Illustration)


• Top-Down Approach
◦ Definition: Breaking down a system into smaller subsystems to understand its compositional
subcomponents.
◦ Illustration: Recursively solving subproblems (e.g., Quicksort).

Pivot
38 27 43 10

<=10 10 <

Pivot
10 38 27 43

<=43 43 <

Pivot
10 38 27 43

<=27 27<

10 27 38 43

10 27 38 43
Algorithms-CS101
• Bottom-Up Approach
◦ Definition: Building a system from the smallest subsystems up to the overall system.
◦ Illustration: Combining solutions to subproblems to solve the main problem (e.g., Merge sort).

38 27 43 10

38 27 43 10

38 27 43 10

27 38 10 43

10 27 38 43

Discussion on Important Algorithms


Algorithm Definition Complexity Why When
Selection Repeatedly O(n^2) Simple to Suitable for
Sort finding the understand and small arrays or
minimum implement; when the cost
element from useful for small of swapping
the unsorted datasets or elements is low. 22 13 64 12 33
part and when memory
putting it at usage is a
the beginning. concern.

Insertion Building a O(n^2) Efficient for Ideal for small


Sort sorted array small dataset arrays or arrays
one element or nearly sorted that are already 2 4 8 9 3 6
at a time by data; in-place partially sorted.
inserting and stable sort.
elements into 2 3 4 8 9 6
their correct
position.

Bubble Repeatedly O(n^2) Easy to Rarely used in


Sort swapping implement and practice, mainly
adjacent understand; can for educational 15 11 16 12 14 13
elements if be optimized to purposes or very
they are in stop early if the small datasets.
the wrong array is already
order. sorted. 11 15 16 12 14 13
Algorithms-CS101
Merge Dividing the O(n log n) Efficient and Suitable for
4 2
Sort array into stable sort with large datasets,
halves, sorting a predictable especially when
each half, and O(n log n) time stability is
merging the complexity. required or 4 2
sorted halves. when data
cannot fit into
memory. 2 4

Quicksort Partitioning O(n log n) Highly efficient Preferred for


the array into on for large general-purpose
pivot
two parts, average datasets; sorting,
sorting each average-case particularly 2 7 3 9 1 6 8 4
part, and time complexity when space
combining is O(n log n); is limited, and
them. 2 1 3 4 7 6 8 9
in-place sort. average
performance <4 pivot >4
is critical.

Tower of Moving disks O(2^n) Classic problem Primarily used


Hanoi from one rod for for educational
to another, understanding purposes to
following recursion and teach recursion.
specific rules. algorithmic
problem-solving.

Breadth- Exploring all O(V + E) Useful for Ideal for finding Source Node
First neighbors at finding the the shortest
Layer 0 0
Search the present shortest path path or Layer 1
(BFS) depth before in unweighted level-order 1 2 3
moving on to graphs; explores traversal
nodes at the all neighbors in graphs or
next depth before moving trees. 4 5 6 7
level. on to the next Layer 2
level. Output: 0, 1, 2, 3, 4, 5, 6, 7

Depth- Exploring as O(V + E) Explores as Suitable for


First far as possible deep as possible scenarios where 0 Source Node

Search along each before you need to


(DFS) branch before backtracking; explore all 1 2 3
backtracking. useful for possible paths,
pathfinding such as maze
and topological solving or 4 5 6 7
sorting. topological
sorting. Output: 0, 1, 4, 5, 2, 6, 3, 7

Binary Repeatedly O(log n) Extremely Ideal for


Search dividing the efficient for searching in
search interval searching in large, sorted 0 1 2 3 4 5 6
in half and sorted arrays; datasets where 14 19 26 27 31 33 35
eliminating O(log n) time quick lookup
the half that complexity. times are Left Sub-Array Right Sub-Array
does not essential.
contain the Middle value
target value.
Algorithms-CS101
Inorder Traverse the O(n) Provides nodes Useful for 1
left subtree, in non- binary search
visit the root decreasing trees and for 2 3
node, then order for binary applications
traverse the search trees; where nodes
right subtree. 4 5 6 7
useful for need to be
retrieving sorted processed in Inorder Traversal
data. sorted order.
4 2 5 1 6 3 7

Preorder Visit the root O(n) Used to create a Useful for prefix 1
node, then copy of the tree notation
traverse the or to get a prefix expressions 2 3
left subtree, expression of and when you
followed by an expression need to process
the right 4 5 6 7
tree. the root node
subtree. before the Preorder Traversal
subtrees.
1 2 4 5 3 6 7

Postorder Traverse the O(n) Used to delete Useful for 1


left subtree, or free nodes deleting trees
then the right in a tree; and for 2 3
subtree, and useful for applications
visit the root postfix where nodes
node last. 4 5 6 7
expression need to be
of an expression processed after Postorder Traversal
tree. their subtrees.
4 5 2 6 7 3 1

You might also like