Greedy Method and Algorithms: Dr. Mohammed Al-Hubaishi
Greedy Method and Algorithms: Dr. Mohammed Al-Hubaishi
Greedy
Method
and
Algorithms
DR. MOHAMMED AL-HUBAISHI
► Divide-and-Conquer
► Dynamic Programming
► Greedy Technique
► Brute Force and Exhaustive Search
► GRAPH TRAVERSAL TECHNIQUES
► BRANCH AND BOUND
► ….
Approach Breaks down problem into Breaks down problem into Makes the locally optimal Tries all possible solutions
independent subproblems, solves overlapping subproblems, choice at each step with to find the optimal one.
them recursively, and combines solves them recursively with the hope of finding a global
solutions. memoization to avoid optimum.
redundant calculations.
Efficiency Efficient for solvable problems Efficient for problems with Efficient for problems with Inefficient for large
with good subproblem structure overlapping subproblems and well-defined greedy problem sizes due to
(e.g., Merge Sort: O(n log n)). optimal substructure (e.g., choices (e.g., Huffman exponential time
Fibonacci sequence: O(n)). Coding: O(n log n)). complexity (e.g., Traveling
Salesman Problem: O(n!)).
Suitability Problems solvable by breaking Problems with overlapping Optimization problems Finding the optimal
down into independent subproblems and optimal where a greedy choice solution when no better
subproblems (e.g., Merge Sort, substructure (e.g., shortest leads to a good solution approach exists (e.g.,
Quick Sort). path, knapsack problem). (e.g., finding the closest breaking a complex
gas station). encryption).
4
Brute Force:
Brute Force is a straightforward approach where you try every possible solution until you find the correct
one. It doesn't involve any optimizations or clever techniques, just sheer computational power.
This method can be inefficient for large problem sets but is guaranteed to find the solution if it exists.
Example:
Let's say you have a lock with a 4-digit code, and you've forgotten the code. Using brute force, you would
start with 0000 and try each combination until you find the correct one. You would try 0000, then 0001,
then 0002, and so on, until you reach 9999. Eventually, you will find the correct code.
5
Exhaustive Search
Exhaustive Search:
Exhaustive Search is similar to Brute Force but may involve some optimizations or pruning of the search
space to reduce the number of combinations to be checked. It's still a systematic approach, but it might
use certain strategies to eliminate certain solutions early on.
Example:
Consider finding the smallest divisor of a given number.
To find the smallest divisor of the number 36. Instead of checking every number from 1 up to 36, we only
need to check up to the square root of 36, which is 6.
This optimization reduces the number of checks needed, making the search more efficient.
6
Brute Force and Exhaustive Search
Selection Sort
There are 5 ways to get from Sakarya to Istanbul by bus, train, taxi or car
3. S3 : take a car
4. S4 : go by train the solutions that can satisfying the condition
in the problem given, are the feasible solutions
5. S5 : go by flight
2. S2 : take a bike
3. S3 : take a car
Best Results
4. S4 : go by train
5. S5 : go by flight
minimization
problems
Dr. Mohammed Al-Hubaishi
17
What is the problem of Greedy Method?
Problems
► Buy a laptop, so I want to search for best Laptop for my requirements?
► Buy a house, so I want to search for best house for my requirements?
► Buy a Car, so I want to search for best Car for my requirements?
► Choose a new employee among many candidates?
Greedy method
► Select many options for the solution
► Add your requirements and filter the results (max or min)
► Select the best options for you (feasible solutions)
► Select the best options between them (optimal solution)
► You can filter and select from them n=4 input1 input2 input3 input4
due to a time-consuming and costly
process of selecting large numbers
(Sort, Type, Color,...etc.).
► So you can have your own
selection method, and based on
that method, you get some results
and choose the best one from
them. That is the optimal solution.
c d
► degree: number of edges touching a given vertex.
► at right: a=1, b=2, c=3, d=2 Dr. Mohammed Al-Hubaishi
22
Graph examples
► For each, what are the vertices and what are the edges?
► Paths through a network
► Web pages with links
► Methods in a program that call each other
► Road maps (e.g., Google maps)
► Airline routes
► Facebook friends
► Family trees
► path: A path from vertex A to Z is a sequence of edges that can be followed starting from a to reach b.
► can be represented as vertices visited, or edges taken
► example, one path from A to Z: {A, X, Z}
A
v u
► path length: Number of vertices d
B X Z
or edges contained in the path. h
c e
W g
► neighbor or adjacent: Two vertices
connected directly by an edge. f
► example: A and X Y
c d
Dr. Mohammed Al-Hubaishi
26
Weighted graphs
► example: graph of airline flights, weighted by miles between cities: 849 PVD
1843 ORD 2
SFO 14
802
43 LGA
17
337
7
2555 138 10
HNL 1233 99
LAX DF 1120
Dr. Mohammed Al-Hubaishi W MIA
27
What is weight in the Tictoc10 graph?
► directed graph ("digraph"): One where edges are one-way connections between
vertices.
► If graph is directed, a vertex has a separate in/out degree.
a b
► A digraph can be weighted or unweighted.
► Is the graph below connected? Why or why not?
c d e
f g
$2
Which path has the minimum cost?
0
$130
0
70
$80
$1 LGA
$60
$250 $140
HNL $120 00
LAX DF $110 $ 1
W MI
$500
Dr. Mohammed Al-Hubaishi
A
31
Greedy Algorithms in Graphs :
Shortest path: 0 1 2 9 7 8 3 4 5 6
Dr. Mohammed Al-Hubaishi
35
Dijkstras in Omnet++
Shortest path: 0 1 4 5
https://www.febspot.com/1929072
Dr. Mohammed Al-Hubaishi
36
Depth First Search