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

Greedy and Dynamic Algorithm

Uploaded by

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

Greedy and Dynamic Algorithm

Uploaded by

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

Greedy Algorithms

&
Dynamic Programming
Greedy Approach
• Any algorithm that solves problems by selecting the
option that is locally optimal at each step is
considered greedy.
• A greedy heuristic can create locally optimal
solutions that approximate a globally optimal
solution in an acceptable amount of time, even if a
greedy method often fails to produce an optimal
solution.
• A heuristic function, also simply called a heuristic,
is a function that ranks alternatives in search
algorithms at each branching step based on
available information to decide which branch to
follow. For example, it may approximate the exact
solution.
Optimization Problems

• For most optimization problems you want to find, not just a solution,
but the best solution.
• A greedy algorithm sometimes works well for optimization
problems. It works in phases. At each phase:
• You take the best you can get right now, without regard for future
consequences.
• You hope that by choosing a local optimum at each step, you will end up at a
global optimum.
Example: Counting Money

• Suppose you want to count out a certain amount of


money, using the fewest possible bills and coins
• A greedy algorithm to do this would be:
At each step, take the largest possible bill or coin
that does not overshoot
• Example: To make $6.39, you can choose:
• a $5 bill
• a $1 bill, to make $6
• a 25¢ coin, to make $6.25
• A 10¢ coin, to make $6.35
• four 1¢ coins, to make $6.39
Greedy Algorithm Failure
• In some (fictional) monetary system, “#” come in 1
#, 7 #, and 10 # coins
• Using a greedy algorithm to count out 15 #, you
would get
• A 10 # piece
• Five 1 # pieces, for a total of 15 #
• This requires six coins
• A better solution would be to use two 7 # pieces
and one 1 # piece
• This only requires three coins
• The greedy algorithm results in a solution, but not
in an optimal solution
A Scheduling Problem

• You have to run nine jobs, with running times of 3, 5, 6, 10, 11, 14, 15,
18, and 20 minutes.
• You have three processors on which you can run these jobs.
• You decide to do the longest-running jobs first, on whatever processor is
available.
P1 20 10 3
P2 18 11 6

P3
15 14 5

• Time to completion: 18 + 11 + 6 = 35 minutes


• This solution isn’t bad, but we might be able to do better
Another Approach

• 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
P1 3 10 15
P2 5 11 18
P3
6 14 20

• 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
An Optimum Solution

• Better solutions do exist:


P1 20 14

P2 18 11 5

P3 15 10 6 3

• 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
•Designing Low cost networks, including
1. Computer networks
2. Telecommunications networks
3. Transportation networks
4. Water supply networks
5. Electrical grids
Minimum Spanning Trees
• Given: Connected, undirected, weighted graph, G
• Find: Minimum - weight spanning tree, T
• Example:

b 7 c
5 Acyclic subset of edges(E) that connects
a 1
all vertices of G.
3 -3
11
d e f
0 2 b c
5

a 1
3 -3

d e f
0
The Kruskal Algorithm
// input: a graph G with n nodes and m edges
// output: E: a MST for G

1. EG[1..m]  Sort the m edges in G in increasing weight order


2. E  {} //the edges in the MST
3. i  1 //counter for EG
4. While |E| < n - 1 do
if adding EG[i] to E does not add a cycle then
E  E  {EG[i]}
ii+1
5. return E
Is this algorithm Greedy?
Yes
Complexity: O(|E|log2 |E|)
Kruskal’s Algorithm

Make a disjoint set for each vertex

1 2
1 2 3 {1}{2}{3}{4}{5}{6}{7}
6 5
4 4 6

3 8
4 5 6

7
4 3

7
Kruskal’s Algorithm

Sort edges by weight


1 2
1 2 3 1: (1,2) {1}{2}{3}{4}{5}{6}{7}
5
2: (2,3)
6
4 4 6 3: (4,5)
3 8 3: (6,7)
4 5 6 4: (1,4)
4: (2,5)
7
4 3 4: (4,7)
5: (3,5)
7
Kruskal’s Algorithm

Add first edge to X if no cycle created


1 2
1 2 3 1: (1,2) {1}{2}{3}{4}{5}{6}{7}
5
2: (2,3)
6
4 4 6 3: (4,5)
3 8 3: (6,7)
4 5 6 4: (1,4)
4: (2,5)
7
4 3 4: (4,7)
5: (3,5)
7
Kruskal’s Algorithm

Merge vertices in added edges


1 2
1 2 3 1: (1,2) {1,2}{3}{4}{5}{6}{7}
5
2: (2,3)
6
4 4 6 3: (4,5)
3 8 3: (6,7)
4 5 6 4: (1,4)
4: (2,5)
7
4 3 4: (4,7)
5: (3,5)
7
Kruskal’s Algorithm

Process each edge in order


1 2
1 2 3 1: (1,2) {1,2}{3}{4}{5}{6}{7}
5
2: (2,3) {1,2,3}{4}{5}{6}{7}
6
4 4 6 3: (4,5)
3 8 3: (6,7)
4 5 6 4: (1,4)
4: (2,5)
7
4 3 4: (4,7)
5: (3,5)
7
Kruskal’s Algorithm

1 2
1 2 3 1: (1,2) {1,2}{3}{4}{5}{6}{7}
5
2: (2,3) {1,2,3}{4}{5}{6}{7}
6
4 4 6 3: (4,5) {1,2,3}{4,5}{6}{7}
3 8 3: (6,7)
4 5 6 4: (1,4)
4: (2,5)
7
4 3 4: (4,7)
5: (3,5)
7

Note that each set is a connected component of G


Kruskal’s Algorithm

1 2
1 2 3 1: (1,2) {1,2}{3}{4}{5}{6}{7}
5
2: (2,3) {1,2,3}{4}{5}{6}{7}
6
4 4 6 3: (4,5) {1,2,3}{4,5}{6}{7}
3 8 3: (6,7) {1,2,3}{4,5}{6,7}
4 5 6 4: (1,4)
4: (2,5)
7
4 3 4: (4,7)
5: (3,5)
7
Kruskal’s Algorithm

1 2
1 2 3 1: (1,2) {1,2}{3}{4}{5}{6}{7}
5
2: (2,3) {1,2,3}{4}{5}{6}{7}
6
4 4 6 3: (4,5) {1,2,3}{4,5}{6}{7}
3 8 3: (6,7) {1,2,3}{4,5}{6,7}
4 5 6 4: (1,4) {1,2,3,4,5}{6,7}
4: (2,5)
7
4 3 4: (4,7)
5: (3,5)
7
Kruskal’s Algorithm

Must join separate components


1 2
1 2 3 1: (1,2) {1,2}{3}{4}{5}{6}{7}
5
2: (2,3) {1,2,3}{4}{5}{6}{7}
6
4 4 6 3: (4,5) {1,2,3}{4,5}{6}{7}
3 8 3: (6,7) {1,2,3}{4,5}{6,7}
4 5 6 4: (1,4) {1,2,3,4,5}{6,7}
4: (2,5) rejected
7
4 3 4: (4,7)
5: (3,5)
7
Kruskal’s Algorithm
Done when all vertices in one set. Then they are all connected with
exactly |V| - 1 edges.
1 2
1 2 3 1: (1,2) {1,2}{3}{4}{5}{6}{7}
5
2: (2,3) {1,2,3}{4}{5}{6}{7}
6
4 4 6 3: (4,5) {1,2,3}{4,5}{6}{7}
3 8 3: (6,7) {1,2,3}{4,5}{6,7}
4 5 6 4: (1,4) {1,2,3,4,5}{6,7}
4: (2,5) rejected
7
4 3 4: (4,7) {1,2,3,4,5,6,7} done
5: (3,5)
7
The Prim’s Algorithm
// input: a graph G
// output: E: a MST for G

1. Select a starting node, v


2. T  {v} //the nodes in the MST
3. E  {} //the edges in the MST
4. While not all nodes in G are in the T do
4.1 Choose the node v’ in G − T such that there is a v in T:
weight(v, v’) is the minimum in
{weight(u, w) : w in G − T and u in T}
4.2 T  T  {v’}
4.3 E  E  {(v, v’)}
5. return E
Prim’s Algorithm
Choose arbitrary starting vertex,
set to 0 and delete min
1 2
1 2 3 S = {5}
1: ∞
6 5 2: ∞
4 4 6
3: ∞
3 8 4: ∞
4 5 6 5: 0
6: ∞
8 7: ∞
4 3

7
Prim’s Algorithm
Choose arbitrary starting vertex,
set to 0 and delete min
1 2
1 2 3 S = {5}
1: ∞
6 5 2: 4
4 4 6
3: 5
3 8 4: 3
4 5 6 6: 8
7: 8
8
4 3

7
Prim’s Algorithm

1 2
1 2 3 S = {5}
1: ∞
6 5
S = {4,5} 2: 4
4 4 6
3: 5
3 8 4: 3
4 5 6 6: 8
7: 8
8
4 3

7
Prim’s Algorithm

1 2
1 2 3 S = {5}
1: 4
6 5
S = {4,5} 2: 4
4 4 6
3: 5
3 8 6: 8
4 5 6 7: 4

8
4 3

7
Prim’s Algorithm

1 2
1 2 3 S = {5}
1: 4
6 5
S = {4,5} 2: 4
4 4 6
3: 5
3 8 6: 8
4 5 6 7: 4

8
4 3

7
Prim’s Algorithm

1 2
1 2 3 S = {5}
1: 4
6 5
S = {4,5} 2: 4
4 4 6 S = {1,4,5} 3: 5
3 8 6: 8
4 5 6 7: 4

8
4 3

7
Prim’s Algorithm

1 2
1 2 3 S = {5}
2: 1
6 5
S = {4,5} 3: 5
4 4 6 S = {1,4,5} 6: 8
3 8 7: 4
4 5 6

8
4 3

7
Prim’s Algorithm

1 2
1 2 3 S = {5}
2: 1
6 5
S = {4,5} 3: 5
4 4 6 S = {1,4,5} 6: 8
3 8 S = {1,2,4,5} 7: 4
4 5 6

8
4 3

7
Prim’s Algorithm

1 2
1 2 3 S = {5}
3: 2
6 5
S = {4,5} 6: 8
4 4 6 S = {1,4,5} 7: 4
3 8 S = {1,2,4,5}
4 5 6

8
4 3

7
Prim’s Algorithm

1 2
1 2 3 S = {5}
3: 2
6 5
S = {4,5} 6: 8
4 4 6 S = {1,4,5} 7: 4
3 8 S = {1,2,4,5}
4 5 6 S = {1,2,3,4,5}
8
4 3

7
Prim’s Algorithm

1 2
1 2 3 S = {5}
6: 6
6 5
S = {4,5} 7: 4
4 4 6 S = {1,4,5}
3 8 S = {1,2,4,5}
4 5 6 S = {1,2,3,4,5}
8
4 3

7
Prim’s Algorithm

1 2
1 2 3 S = {5}
6: 6
6 5
S = {4,5} 7: 4
4 4 6 S = {1,4,5}
3 8 S = {1,2,4,5}
4 5 6 S = {1,2,3,4,5}
S = {1,2,3,4,5,7}
8
4 3

7
Prim’s Algorithm

1 2
1 2 3 S = {5}
6: 3
6 5
S = {4,5}
4 4 6 S = {1,4,5}
3 8 S = {1,2,4,5}
4 5 6 S = {1,2,3,4,5}
S = {1,2,3,4,5,7}
8
4 3

7
Prim’s Algorithm

1 2
1 2 3 E: S = {5}
6 5
{4,5} S = {4,5}
4 4 6 {1,4} S = {1,4,5}
3 8 {1,2} S = {1,2,4,5}
4 5 6 {2,3} S = {1,2,3,4,5}
{4,7} S = {1,2,3,4,5,7}
8
4 3 {6,7} S = {1,2,3,4,5,6,7}
7
Example of Prim’s Algorithm

Not in tree
a/0 5 b/ 7 c/

1 -3 Q=a b c d e f
11 3
0  
d/ e/ f/
0 2
Example of Prim’s Algorithm

a/0 5 b/5 7 c/

1 -3 Q=b d c e f
11 3
5 11  
d/11 e/ f/
0 2
Example of Prim’s Algorithm

a/0 5 b/5 7 c/7

1 -3 Q=e c d f
11 3
3 7 11 
d/11 e/3 f/
0 2
Example of Prim’s Algorithm

a/0 5 b/5 7 c/1

1 -3 Q=d c f
11 3
0 1 2
d/0 e/3 f/2
0 2
Example of Prim’s Algorithm

a/0 5 b/5 7 c/1

1 -3 Q=c f
11 3
1 2
d/0 e/3 f/2
0 2
Example of Prim’s Algorithm

a/0 5 b/5 7 c/1

1 -3 Q=f
11 3
-3
d/0 e/3 f/-3
0 2
Example of Prim’s Algorithm

a/0 5 b/5 7 c/1

1 -3 Q=
11 3

d/0 e/3 f/-3


0 2
Example of Prim’s Algorithm

a/0 5 b/5 c/1

3 1 -3

d/0 e/3 f/-3


0
1. Google Maps
2. Finding Shortest Path
3. In IP routing to find Open shortest Path
First
4. In the telephone network
Dijkstra's Shortest Path Algorithm

Find shortest path from s to t.

23
2 3
9

s
18
14
2 6
6
30 4 19
11
15 5
5
6
20 16

t
7 44

46
47
Dijkstra’s Pseudo Code

Graph G, weight function w, root s


Dijkstra's Shortest Path Algorithm

S={ }
Q = ( s, 2, 3, 4, 5, 6, 7, t )



23
2 3
0 9

s
18
14  2 6
6 
30  4 19
11
15 5
5
6
20 16

t
7 44
distance label  
Dijkstra's Shortest Path Algorithm

S={ }
Q = ( s, 2, 3, 4, 5, 6, 7, t )

Extract-Min(Q)


23
2 3
0 9

s
18
14  2 6
6 
30  4 19
11
15 5
5
6
20 16

t
7 44
distance label  
Dijkstra's Shortest Path Algorithm

S={s}
Q = ( 2, 6, 7, 3, 4, 5, t )

decrease key


X
 9
23
2 3
0 9

s
18
14 X
 14 6
2
6 
30  4 19
11
15 5
5
6
20 16

t
7 44
distance label  15
X 
Dijkstra's Shortest Path Algorithm

S={s}
Q = ( 2, 6, 7, 3, 4, 5, t )

Extract-Min(Q)

X
 9
23
2 3
0 9

s
18
14 X
 14 6
2
6 
30  4 19
11
15 5
5
6
20 16

t
7 44
distance label  15
X 
Dijkstra's Shortest Path Algorithm

S = { s, 2 }
Q = ( 6, 7, 3, 4, 5, t )


X
 9
23
2 3
0 9

s
18
14 X
 14 6
2
6 
30  4 19
11
15 5
5
6
20 16

t
7 44

 15
X 
Dijkstra's Shortest Path Algorithm

S = { s, 2 }
Q = ( 6, 7, 3, 4, 5, t )

decrease key

X
 32
X
 9
23
2 3
0 9

s
18
14 X
 14 6
2
6 
30  4 19
11
15 5
5
6
20 16

t
7 44

 15
X 
Dijkstra's Shortest Path Algorithm

S = { s, 2 }
Q = ( 6, 7, 3, 4, 5, t )

X
 32
X
 9
23
2 3
0 9
Extract-Min(Q)
s
18
14 X
 14 6
2
6 
30  4 19
11
15 5
5
6
20 16

t
7 44

 15
X 
Dijkstra's Shortest Path Algorithm

S = { s, 2, 6 }
Q = ( 7, 3, 5, 4, t )

X
 32
X
 9
23
2 3
0 9

s
18
14 X
 14 6
2
6 
44
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

 15
X 
Dijkstra's Shortest Path Algorithm

S = { s, 2, 6 }
Q = ( 7, 3, 5, 4, t )

X
 32
X
 9
23
2 3
0 9

s
18
14 X
 14 6
2
6 
44
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

 15
X 
Extract-Min(Q)
Dijkstra's Shortest Path Algorithm

S = { s, 2, 6, 7 }
Q = ( 3, 5, t, 4 )

X
 32
X
 9
23
2 3
0 9

s
18
14 X
 14 6
2
6 
X 35
44
30 X 4 19
11
15 5
5
6
20 16

t
7 44

 15
X 
59 X
Dijkstra's Shortest Path Algorithm

S = { s, 2, 6, 7 }
Q = ( 3, 5, t, 4 )
Extract-Min(Q)

X
 32
X
 9
23
2 3
0 9

s
18
14 X
 14 6
2
6 
X 35
44
30 X 4 19
11
15 5
5
6
20 16

t
7 44

 15
X 
59 X
Dijkstra's Shortest Path Algorithm

S = { s, 2, 3, 6, 7 }
Q = ( 5, t, 4 )

X
 32
X
 9
23
2 3
0 9

s
18
14 X
 14 6
2
6 
X 34
X 35
44
30 X 4 19
11
15 5
5
6
20 16

t
7 44

 15
X 51 59 
X X
Dijkstra's Shortest Path Algorithm

S = { s, 2, 3, 6, 7 }
Q = ( 5, t, 4 )

X
 32
X
 9
23
2 3
0 9

s
18
14 X
 14 6
2
6 
X 34
X 35
44
30 X 4 19
11
15 5
5
6
20 16
Extract-Min(Q)

t
7 44

 15
X 51 59 
X X
Dijkstra's Shortest Path Algorithm

S = { s, 2, 3, 5, 6, 7 }
Q = ( 4, t )

X
 32
X
 9
23
2 3
0 9

s
18
14 X
 14 6
2
6 45 X

X 34
X 35
44
30 X 4 19
11
15 5
5
6
20 16

t
7 44

 15
X 50 51
X 59 
X X
Dijkstra's Shortest Path Algorithm

S = { s, 2, 3, 5, 6, 7 }
Q = ( 4, t )

X
 32
X
 9
23
2 3
0 9

s
18
14 X
 14 6
2
6 45 X

X 34
X 35
44
30 X 4 19
11
15 5 Extract-Min(Q)
5
6
20 16

t
7 44

 15
X 50 51
X 59 
X X
Dijkstra's Shortest Path Algorithm

S = { s, 2, 3, 4, 5, 6, 7 }
Q=(t)

X
 32
X
 9
23
2 3
0 9

s
18
14 X
 14 6
2
6 45 X

X 34
X 35
44
30 X 4 19
11
15 5
5
6
20 16

t
7 44

 15
X 50 51
X 59 
X X
Dijkstra's Shortest Path Algorithm

S = { s, 2, 3, 4, 5, 6, 7 }
Q=(t)

X
 32
X
 9
23
2 3
0 9

s
18
14 X
 14 6
2
6 45 X

X 34
X 35
44
30 X 4 19
11
15 5
5
6
20 16

t
7 44
Extract-Min(Q) 50 51
X 59 
X X
 15
X
Dijkstra's Shortest Path Algorithm

S = { s, 2, 3, 4, 5, 6, 7, t }
Q=()

X
 32
X
 9
23
2 3
0 9

s
18
14 X
 14 6
2
6 45 X

X 34
X 35
44
30 X 4 19
11
15 5
5
6
20 16

t
7 44

 15
X 50 51
X 59 
X X
Dynamic Programming
Fibonacci Numbers

Computing the nth Fibonacci number recursively:


F(n) = F(n-1) + F(n-2)
F(0) = 0 int Fib(int n)
{
F(1) = 1 if (n <= 1)
Top-down approach return 1;
else
return Fib(n - 1) + Fib(n - 2);
}

F(n)

F(n-1) + F(n-2)

F(n-2) + F(n-3) F(n-3) + F(n-4)


Fibonacci Numbers

Fib(5)
+

Fib(4) Fib(3)
+ +

Fib(3) Fib(2) Fib(2) Fib(1)


+ + +

Fib(2) Fib(1) Fib(1) Fib(0) Fib(1) Fib(0)


+

Fib(1) Fib(0)
Dynamic programming

• Algorithm design technique


• A technique for solving problems that have
1. an optimal substructure property (recursion)
2. overlapping subproblems
• Idea: Do not repeatedly solve the same subproblems,
but solve them only once and store the solutions in a
dynamic programming table
Example: Fibonacci numbers

• F(0)=0; F(1)=1; F(n)=F(n-1)+F(n-2) for n  2


0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

Optimal substructure
An optimal solution to a problem
(instance) contains optimal
solutions to subproblems.
Recursion
Example: Fibonacci numbers

• F(0)=0; F(1)=1; F(n)=F(n-1)+F(n-2) for n  2


• Implement this recursion directly:
F(n)
n F(n-1) same F(n-2) n/2
subproblem
F(n-2) F(n-3) F(n-3) F(n-4)
F(n-3) F(n-4)F(n-4) F(n-5) F(n-4) F(n-5)F(n-5) F(n-6)
• Runtime is exponential: 2n/2 ≤ T(n) ≤ 2n
• But we are repeatedly solving the same subproblems
Overlapping subproblems
A recursive solution contains a
“small” number of distinct
subproblems repeated many times.

The number of distinct Fibonacci


subproblems is only n.
Bottom-up dynamic- programming algorithm

• Store 1D DP-table and fill:


F: 0 1 1 2 3 5 8

fibDP(n)
F[0]  0
F[1]  1
for (i  2, i≤ n, i++)
F[i]  F[i-1]+F[i-2]
return F[n]
• Time = (n), space = (n)

You might also like