











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of various algorithm types, focusing on greedy algorithms. It explains how these algorithms work for optimization problems and provides examples, such as counting money and scheduling jobs. The document also discusses the limitations of the greedy algorithm and introduces alternative approaches.
Typology: Exercises
1 / 19
This page cannot be seen from the preview
Don't miss anything!












2
Simple recursive algorithms
Backtracking algorithms
Divide and conquer algorithms
Dynamic programming algorithms
Greedy algorithms
Branch and bound algorithms
Brute force algorithms
Randomized algorithms
4
Example: To make $6.39, you can choose:
a $5 bill
a $1 bill, to make $
a 25¢ coin, to make $6.
A 10¢ coin, to make $6.
four 1¢ coins, to make $6.
5
A 10 kron piece
Five 1 kron pieces, for a total of 15 krons
This requires six coins
This only requires three coins
7
What would be the result if you ran the shortest job first?
Again, the running times are 3 , 5 , 6 , 10 , 11 , 14 , 15 , 18 , and
20 minutes
That wasn’t such a good idea; time to completion is now
6 + 14 + 20 = 40 minutes
Note, however, that the greedy algorithm itself is fast
All we had to do at each stage was pick the minimum or maximum
8
This solution is clearly optimal (why?)
Clearly, there are other optimal solutions (why?)
How do we find such a solution?
One way: Try all possible assignments of jobs to processors
Unfortunately, this approach can take exponential time
Better solutions do exist:
10
A minimum spanning tree is a least-cost subset of the edges of a
graph that connects all the nodes
Start by picking any node and adding it to the tree
Repeatedly: Pick any least-cost edge from a node in the tree to
a node not in the tree, and add the edge and new node to the
tree
Stop when all nodes have been added to the tree
The result is a least-cost
(3+3+2+2+2=12) spanning tree
If you think some other edge should be in
the spanning tree:
Try adding that edge
Note that the edge is part of a cycle
To break the cycle, you must remove
the edge with the greatest cost
This will be the edge you just
added
3
3
3
3
2
2
2
4
4
4
11
A salesman must visit every city (starting from city A ), and wants
to cover the least possible distance
He can revisit a city (and reuse a road) if necessary
He does this by using a greedy algorithm: He goes to the next
nearest city from wherever he is
From A he goes to B
From B he goes to D
This is not going to result in a
shortest path!
The best result he can get now
will be ABDBCE , at a cost of 16
An actual least-cost path from A
is ADBCE , at a cost of 14
2
3 3
4
4 4
13
Always takes the shortest edge connecting a known node to
an unknown node
Always tries the lowest-cost remaining edge
Always takes the lowest-cost edge between nodes in the
spanning tree and nodes not yet in the spanning tree
14
Dijkstra’s algorithm finds the shortest paths from a given node to
all other nodes in a graph
Initially,
Mark the given node as known (path length is zero)
For each out-edge, set the distance in each neighboring node equal to the cost
(length) of the out-edge, and set its predecessor to the initially given node
Repeatedly (until all nodes are known),
Find an unknown node containing the smallest distance
Mark the new node as known
For each node adjacent to the new node, examine its neighbors to see whether
their estimated distance can be reduced (distance to known node plus cost of
out-edge)
If so, also reset the predecessor of the new node
16
Repeatedly (until all nodes are known), (n times)
Find an unknown node containing the smallest distance
Probably the best way to do this is to put the unknown nodes into a
priority queue; this takes k * O(log n) time each time a new node is
marked “known” (and this happens n times)
Mark the new node as known -- O(1) time
For each node adjacent to the new node, examine its neighbors to
see whether their estimated distance can be reduced (distance to
known node plus cost of out-edge)
If so, also reset the predecessor of the new node
There are k adjacent nodes (on average), operation requires constant
time at each, therefore O(k) (constant) time
Combining all the parts, we get:
O(1) + n(kO(log n)+O(k)), that is, O(nk log n) time
17
There are n white dots and n black dots, equally spaced, in a line
You want to connect each white dot with some one black dot,
with a minimum total length of “wire”
Example:
Total wire length above is 1 + 1 + 1 + 5 = 8
Do you see a greedy algorithm for doing this?
Does the algorithm guarantee an optimal solution?
Can you prove it?
Can you find a counterexample?
19