0% found this document useful (0 votes)
9 views39 pages

Greedy Method and Algorithms: Dr. Mohammed Al-Hubaishi

The document discusses various problem-solving methods including Divide-and-Conquer, Dynamic Programming, Greedy Technique, and Brute Force. It highlights the Greedy Method's approach to optimization problems, where locally optimal choices are made with the hope of finding a global optimum. Additionally, it covers graph-related algorithms and examples of standard greedy algorithms used in problem-solving.

Uploaded by

jale.cavus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views39 pages

Greedy Method and Algorithms: Dr. Mohammed Al-Hubaishi

The document discusses various problem-solving methods including Divide-and-Conquer, Dynamic Programming, Greedy Technique, and Brute Force. It highlights the Greedy Method's approach to optimization problems, where locally optimal choices are made with the hope of finding a global optimum. Additionally, it covers graph-related algorithms and examples of standard greedy algorithms used in problem-solving.

Uploaded by

jale.cavus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

1

Greedy
Method
and
Algorithms
DR. MOHAMMED AL-HUBAISHI

Dr. Mohammed Al-Hubaishi


2
Solve a problem using different methods

► Divide-and-Conquer
► Dynamic Programming
► Greedy Technique
► Brute Force and Exhaustive Search
► GRAPH TRAVERSAL TECHNIQUES
► BRANCH AND BOUND
► ….

Dr. Mohammed Al-Hubaishi


3
Feature Divide-and-Conquer Dynamic Programming Greedy Method Brute-Force/Exhaustive
Search

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

Dr. Mohammed Al-Hubaishi


7
Solve a problem using Divide-and-Conquer

Dr. Mohammed Al-Hubaishi


8
Dynamic Programming

Dr. Mohammed Al-Hubaishi


9
Greedy Method

Dr. Mohammed Al-Hubaishi


10
What is the problem of Greedy Method?

► Problem (P): I want to travel from Sakarya to Istanbul in 3 hours ?

the condition constrain is in 3 hours


Solutions (S):
1. S1 : travel by walk
2. S2 : take a bike
3. S3 : take a car
4. S4 : go by train
5. S5 : go by flight
Dr. Mohammed Al-Hubaishi
11
What is the problem of Greedy Method?

Problem (P): I want to travel from Sakarya to Istanbul in 3


hours ?

Dr. Mohammed Al-Hubaishi


12
What is the problem of Greedy Method?

Problem (P): I want to travel from Sakarya to Istanbul in 3


hours ?

Dr. Mohammed Al-Hubaishi


13
What is the problem of Greedy Method?

There are 5 ways to get from Sakarya to Istanbul by bus, train, taxi or car

Dr. Mohammed Al-Hubaishi


14
What is the problem of Greedy Method?

► Problem (P): I want to travel from Sakarya to Istanbul in 3 hours ?


► Many Solutions (S):
1. S1 : travel by walk the condition constrain is in 3 hours
2. S2 : take a bike Not feasible solutions

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

Dr. Mohammed Al-Hubaishi


15
What is the problem of Greedy Method?

► Problem (P): I want to travel from Sakarya to Istanbul in 3 hours ?


► Many Solutions (S):
minimization
1. S1 : travel by walk problems
2. S2 : take a bike
3. S3 : take a car
4. S4 : go by train
If we need to
5. S5 : go by flight minimum cost ?

Dr. Mohammed Al-Hubaishi


16
What is the problem of Greedy Method?

► Problem (P): I want to travel from Sakarya to Istanbul in 3 hours ?


► Many Solutions (S): one
1. S1 : travel by walk optimal solution solution

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)

Dr. Mohammed Al-Hubaishi


18
Optimization problem

► if the problem requires maximum results or minimum


results, we can call that type of the problem as:
Optimization Problem.
► Therefore, Greedy Method is used for solving
optimization problems.

Dr. Mohammed Al-Hubaishi


19
Questions

► What is the optimization problem ?


► What is the optimal solution ?
► What are the feasible solutions?
► What kind of problems Greedy Method is solving ?

Dr. Mohammed Al-Hubaishi


20
Greedy Method

► 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.

Dr. Mohammed Al-Hubaishi


21
Graphs
► graph: A data structure containing:
► a set of vertices V, (sometimes called nodes)
► a set of edges E, where an edge, represents a connection between 2 vertices.
► Graph G = (V, E)
► an edge is a pair (v, w) where v, w are in V

► the graph at right:


a b
► V = {a, b, c, d}
► E = {(a, c), (b, c), (b, d), (c, d)}

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

Dr. Mohammed Al-Hubaishi


23
Paths

► 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

Dr. Mohammed Al-Hubaishi


24
In Tictoc17 example: define V, E, and path

Dr. Mohammed Al-Hubaishi


25
Reachability, connectedness
A
v x
► reachable: Vertex a is reachable from b
if a path exists from a to b. d
U B Z
h
c e
► connected: A graph is connected if every a b W g
vertex is reachable from any other.
► Is the graph at top right connected? d f
c Y
e
► strongly connected: When every vertex
has an edge to every other vertex. a b

c d
Dr. Mohammed Al-Hubaishi
26
Weighted graphs

► weight: Cost associated with a given edge.


► Some graphs have weighted edges, and some are unweighted.
► Edges in an unweighted graph can be thought of as having equal weight (e.g. all
0, or all 1, etc.)
► Most graphs do not allow negative weights.

► 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?

Dr. Mohammed Al-Hubaishi


28
Directed graphs

► 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

Dr. Mohammed Al-Hubaishi


29
In Tictoc10 : Which V are Directed?

Dr. Mohammed Al-Hubaishi


30
Searching for paths

► Searching for a path from one vertex to another:


► Sometimes, we just want any path (or want to know there is a path).
► Sometimes, we want to minimize path length (# of edges).
► Sometimes, we want to minimize path cost (sum of edge weights).
$50 PVD
► What is the shortest path from MIA to SFO? $70 ORD
SFO

$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 :

1. Kruskal’s Minimum Spanning Tree


2. Prim’s Minimum Spanning Tree
3. Boruvka’s Minimum Spanning Tree
4. Reverse delete algorithm for MST
5. Problem Solving for Minimum Spanning Trees (Kruskal’s and Prim’s)
6. Dijkastra’s Shortest Path Algorithm
7. Dial’s Algorithm
8. Dijkstra’s Algorithm for Adjacency List Representation
9. Prim’s MST for adjacency list representation
10. Genetic Algorithm
11. Minimum cost to connect all cities
12. Max Flow Problem Introduction
13. Number of single cycle components in an undirected graph
Dr. Mohammed Al-Hubaishi
32
Standard Greedy Algorithms :

1. Activity Selection Problem


2. Egyptian Fraction
3. Job Sequencing Problem
4. Job Sequencing Problem (Using Disjoint Set)
5. Job Sequencing Problem – Loss Minimization
6. Job Selection Problem – Loss Minimization Strategy | Set 2
7. Huffman Coding
8. Efficient Huffman Coding for sorted input
9. Huffman Decoding
10. Water Connection Problem
11. Policemen catch thieves
12. Minimum Swaps for Bracket Balancing
13. Fitting Shelves Problem
14. Assign Mice to Holes
Dr. Mohammed Al-Hubaishi
33
(TicToc18) tic-0-to-5

Dr. Mohammed Al-Hubaishi


34
Depth First Search in Omnet++

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

Dr. Mohammed Al-Hubaishi


37
Find a Shortest Path

Dr. Mohammed Al-Hubaishi


38

Dr. Mohammed Al-Hubaishi


39

Dr. Mohammed Al-Hubaishi

You might also like