KEMBAR78
Algorithm analysis Greedy method in algorithm.pptx
Chapter 3:Greedy Algorithms
 General Characteristic of Greedy Algorithms
 Graph Minimum Spanning Tree (MST) - Kruskal’s and Prims’s
Algorithms
 Scheduling
Compiled by: Elias M.(PhD candidate.)
1
Greedy Algorithm - Introduction
 Greedy is a special type of algorithm, where we design an
algorithm to solve an optimization problem.
 An optimization problem means trying to find maximum or
minimum value which is optimized result.
 "Greedy Method finds out of many options, but you have to choose
the best option."
 In this method, we have to find out the best method/option out of
many present ways.
Compiled by: Elias M.(PhD candidate.) 2
Minimum Spanning Trees
• Spanning Tree
• A tree (i.e., connected, acyclic graph) which contains all the vertices
of the graph
• Minimum Spanning Tree
• Spanning tree with the minimum sum of weights
• Spanning forest
• If a graph is not connected, then there is a spanning tree for each
connected component of the graph
a
b c d
e
g g f
i
4
Compiled by: Elias M.(PhD candidate.) 3
8 7
8
11
1 2
7
2
4 14
9
10
6
Graph -Revision
Compiled by: Elias M.(PhD candidate.)
4
Multigraph
Graph -Revision
Compiled by: Elias M.(PhD candidate.)
5
Graph -Revision
Compiled by: Elias M.(PhD candidate.)
6
Applications of MST
• Find the least expensive way to connect a set of cities, terminals, computers,
etc.
Compiled by: Elias M.(PhD candidate.)
7
Problem
• A town has a set of houses and a
set of roads
• A road connects 2 and only
2 houses
• A road connecting houses u and v has a
repair cost w(u, v)
Goal: Repair enough (and no more) roads
such that:
1. Everyone stays connected
i.e., can reach every house from all other houses
2. Total repair cost is minimum
a
b c d
e
h g f
i
4
Compiled by: Elias M.(PhD candidate.)
8
8 7
8
11
1 2
7
2
4 14
9
10
6
Minimum Spanning Trees
• A connected, undirected graph:
• Vertices = houses, Edges =
roads
• A weight w(u, v) on each edge (u, v) 
E
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
10
6
Find T  E such
that:
1. T connects all vertices
2. w(T) = Σ(u,v)T w(u, v) is
minimized
Minimum Spanning Trees
Compiled by: Elias M.(PhD candidate.)
9
Properties of Minimum Spanning Trees
• Minimum spanning tree is not unique
• MST has no cycles – see why:
• We can take out an edge of a cycle, and still have the
vertices connected while reducing the cost
• # of edges in a MST:
• |V| - 1
10
Compiled by: Elias M.(PhD candidate.)
Growing a MST –Generic Approach
a
b c d
e
h g f
i
4
Compiled by: Elias M.(PhD candidate.)
8 7
8
11
1 2
7
2
4 14
9
10
6
• Grow a set A of edges (initially empty)
• Incrementally add edges to A
such that they would belong
to a MST
– An edge (u, v) is safe for A if and
only if A  {(u, v)} is also a
subset of some MST
Idea: add only “safe” edges
11
Generic MST algorithm
3.
1. A ← 
2. while A is not a spanning tree
do find an edge (u, v) that is safe for A
4. A ← A  {(u, v)}
5. return A a
b c d
h g f
i
4
Compiled by: Elias M.(PhD candidate.)
8 7
11
8
1 2
7
2
4 14
9
e
10
6
12
Minimum Spanning Trees
Compiled by: Elias M.(PhD candidate.) 13
• MST of a weighted, connected graph G is defined as: A spanning tree of G with
minimum total weight.
• Example: Consider the example of spanning tree:
• For the given graph there are three possible spanning trees. Among them the
spanning tree with the minimum weight 6 is the MST for the given graph
Compiled by: Elias M.(PhD candidate.)
Minimum Spanning Trees
14
Prim’s Algorithm
• The edges in set A always form a single tree
• Starts from an arbitrary “root”: VA = {a}
• At each step:
• Find a light edge crossing (VA, V - VA)
• Add this edge to A
• Repeat until the tree spans all vertices
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
10
6
Compiled by: Elias M.(PhD candidate.)
15
• STEP 1: Start with a tree, T0, consisting of one vertex
• STEP 2: “Grow” tree one vertex/edge at a time
• Construct a series of expanding sub-trees T1, T2, … Tn-1.
• At each stage construct Ti + 1 from Ti by adding the minimum weight edge connecting a
vertex in tree (Ti) to one vertex not yet in tree, choose from “fringe” edges (this is the
“greedy” step!)
• Algorithm stops when all vertices are included
Compiled by: Elias M.(PhD candidate.)
Prim’s Algorithm - The Method
16
How to Find Light Edges Quickly?
Use a priority queue Q:
• Contains vertices not yet
A
included in the tree, i.e., (V – V )
A
• V = {a}, Q = {b, c, d, e, f, g, h, i}
• We associate a key with each vertex v:
key[v] = minimum weight of any edge (u, v)
connecting v to VA
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
10
6
w1
w2
Key[a]=min(w1,w2)
a
17
Compiled by: Elias M.(PhD candidate.)
How to Find Light Edges Quickly? (cont.)
• After adding a new node to VAwe update the weights of all the
nodes adjacent to it
e.g., after adding a to the tree, k[b]=4 and k[h]=8
• Key of v is  if v is not adjacent to any vertices in VA
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
10
6
18
Compiled by: Elias M.(PhD candidate.)
Example
0         Q =
{a, b, c, d, e, f, g, h, i} VA
= 
Extract-MIN(Q)  a
a
4
8 7
8
11
1 2
7
2
4 14
10
6

b

c

d

i
9

e
h

g

f

a
b
h
4
8 7
8
11
1 2
7
2
4 14
9
10
6

c

d

i

e
g

f

key [b] = 4  [b] =
a
 [h] =
a
key [h] = 8
4      8 
Q = {b, c, d, e, f, g, h, i} VA = {a}
Extract-MIN(Q)  b

4

8
Compiled by: Elias M.(PhD candidate.)
19



Example
a
c
4
8 7
8
11
1 2
7
2
4 14
9
10
6
key [c] = 8  [c] = b
 [h] = a -
unchanged
key [h] = 8
8     8 
A
Q = {c, d, e, f, g, h, i} V = {a,
b}
Extract-MIN(Q)  c
a
d
h
8
g

f
i
4
8 7
8
11
1 2
7
2
4 14
9

e
10
6
key [d] = 7
key [f] =
4 key [i] =
2
 [d] =
c
 [f] =
c
 [i] =
c
7  4  8 2
Q = {d, e, f, g, h, i} VA= {a, b, c}
Extract-MIN(Q)  i


i
4
b

d

e
h
8
4
b
g

8
c
f

8
7
4
2
Compiled by: Elias M.(PhD candidate.) 20
Example
a
4
8 7
8
11
1 2
7
2
4 14
10
6
key [h] = 7
key [g] = 6
 [h] =
i
 [g] =
i
7  4 6 8
Q = {d, e, f, g, h} V = {a, b, c,
i}
A
Extract-MIN(Q)  f
a
b c d
h g f
4
8 7
8
11
1 2
7
2
4 14
9
10
6
4
b
7
d
9

e
key [g] = 2
key [d] = 7
key [e] = 10
7 10 2
8
 [g] = f
 [d] = c
unchanged
 [e] = f
Q = {d, e, g, h} V = {a, b, c, i,
f}
A
Extract-MIN(Q) 
g
8 
f
4
8
c
2
i
h
7
4 7

g
6
8
2
i
10
e
6 4
7 2
Compiled by: Elias M.(PhD candidate.) 21
Example
a
b c d
h
4
8 7
8
11
1 2
7
4 14
10
6
key [h] = 1  [h] =
g
7 10 1
Q = {d, e, h} V = {a, b, c, i, f,
g}
A
Extract-MIN(Q)
 h
a
c
h g f
4
8 7
8
11
1 2
7
4 14
10
6
9
10
e
7 10
Q = {d, e} VA= {a, b, c, i, f, g, h}
Extract-MIN(Q)  d
4 7
7
2 2
i
8
1
4
2
1
Compiled by: Elias M.(PhD candidate.)
22
4
b
f
4
7
d
9
10
e
g
2
8
2 2
i
Example
a
b c d
e
4
8 7
8
11
1 2
7
2
4 14
9
10
6
key [e] = 9  [e] =
f
9
Q = {e} V = {a, b, c, i, f, g, h,
d}
A
Extract-MIN(Q)  e
Q =  VA= {a, b, c, i, f, g, h, d, e}
4 7
10
h
1
g
2
f
4
2
i
8
9
Compiled by: Elias M.(PhD candidate.) 23
PRIM(V, E, w, r)
3.
4.
5.
1. Q ← 
2. for each u  V
do key[u] ← ∞
π[u] ← NIL
INSERT(Q, u)
6. DECREASE-KEY(Q, r, 0)
7. while Q  
► key[r] ← 0
8. do u ← EXTRACT-MIN(Q)
for each v  Adj[u]
do if v  Q and
w(u, v) < key[v]
then π[v] ←
u
9.
10.
11.
12. DECREASE-KEY(Q, v, w(u, v))
O(V) if Q is implemented as
a min-heap
Executed |V| times
Takes O(lgV)
O(lgV)
Min-heap
operations
: O(VlgV)
Executed O(E) times total
Constant
Takes O(lgV)
O(ElgV)
Total time: O(VlogV + ElogV) = O(ElogV)
Compiled by: Elias M.(PhD candidate.)
24
Prim’s Algorithm
Compiled by: Elias M.(PhD candidate.)
• Prim’s algorithm is a “greedy” algorithm
• Greedy algorithms find solutions based on a sequence of
choices which are “locally” optimal at each step.
• Nevertheless, Prim’s greedy strategy produces a
globally optimum solution!
25
Kruskal’s Algorithm –to find MST
• How is it different from Prim’s algorithm?
• Prim’s algorithm grows one tree all the time
• Kruskal’s algorithm grows
multiple trees (i.e., a forest)
at the same time.
• Trees are merged together
using safe edges
• Since an MST has exactly |V| - 1
edges, after |V| - 1 merges,
we would have only one component
u
Compiled by: Elias M.(PhD candidate.)
v
tree1
tree2
26
Example: Apply kruskal’s algorithm for the following graph to fin
MST
Kruskal’s Algorithm –to find MST
Compiled by: Elias M.(PhD candidate.)
27
Kruskal’s Algorithm –to find MST
Compiled by: Elias M.(PhD candidate.) 28
Kruskal’s Algorithm –to find MST
Compiled by: Elias M.(PhD candidate.)
29
Kruskal’s Algorithm –to find MST
Compiled by: Elias M.(PhD candidate.) 30
Kruskal’s Algorithm –to find MST
Compiled by: Elias M.(PhD candidate.) 31
1. A ← 
2. for each vertex v  V
3. do MAKE-SET(v)
4. sort E into non-decreasing order by w
5. for each (u, v) taken from the sorted list
6. do if FIND-SET(u)  FIND-SET(v)
7. then A ← A  {(u, v)}
8. UNION(u, v)
9. return A
- Running time: O(V+ElgE+ElgV)=O(ElgE)
- Since E=O(V2), we have lgE=O(2lgV)=O(lgV)
KRUSKAL(V, E, w) (cont.)
O(V)
O(ElgE)
O(E)
O(lgV)
O(ElgV)
32
• Efficiency of Kruskal’s algorithm is based on the time needed for sorting the edge weights of a
given graph.
• With an efficient sorting algorithm: Efficiency: Θ(|E| log |E| )
• Conclusion:
• Kruskal’s algorithm is an “edge based algorithm”
• Prim’s algorithm with a heap is faster than Kruskal’s algorithm.
Compiled by: Elias M.(PhD candidate.)
Kruskal’s Algorithm –Effeciency
33
Greedy Algorithm - Activity or Task Scheduling Problem
This is the dispute of optimally scheduling unit-time tasks on a single processor,
where each job has a deadline and a penalty that necessary be paid if the
deadline is missed.
Compiled by: Elias M.(PhD candidate.) 34
Greedy Algorithm - Activity or Task Scheduling Problem
Compiled by: Elias M.(PhD candidate.) 35
• Now, schedule A1
• Next schedule A3 as A1 and A3 are non-interfering.
• Next skip A2 as it is interfering.
• Next, schedule A4 as A1 A3 and A4 are non-interfering, then next, schedule A6 as
A1 A3 A4 and A6 are non-interfering.
• Skip A5 as it is interfering.
• Next, schedule A7 as A1 A3 A4 A6 and A7 are non-interfering.
• Next, schedule A9 as A1 A3 A4 A6 A7 and A9 are non-interfering.
• Skip A8 as it is interfering.
• Next, schedule A10 as A1 A3 A4 A6 A7 A9 and A10 are non-interfering.
• Thus the final Activity schedule is:
Greedy Algorithm - Activity or Task Scheduling Problem
Compiled by: Elias M.(PhD candidate.) 36
• Tasks –N
• Machines – N at most
• Tasks have duration
• Machines can only do one task at a time
• Overlapping duration – use different machines
• Allocate tasks machines ,minimize machines
Compiled by: Elias M.(PhD candidate.)
Greedy Algorithm - Activity or Task Scheduling Problem
37
• Order tasks by starting time
• Ascending order
• Each Job
• Allocate it to smallest numbered machines that is idle
• Process tasks in order of starting time
Greedy Algorithm - Activity or Task Scheduling Problem
Compiled by: Elias M.(PhD candidate.) 38
Greedy Algorithm - Activity or Task Scheduling Problem
Task A
Compiled by: Elias M.(PhD candidate.)
Task B
Task C
Task F
39
Greedy Algorithm - Activity or Task Scheduling Problem
Task G
Compiled by: Elias M.(PhD candidate.)
Task E
Task D
40
Thank You !
Compiled by: Elias M.(PhD candidate.)
41

Algorithm analysis Greedy method in algorithm.pptx

  • 1.
    Chapter 3:Greedy Algorithms General Characteristic of Greedy Algorithms  Graph Minimum Spanning Tree (MST) - Kruskal’s and Prims’s Algorithms  Scheduling Compiled by: Elias M.(PhD candidate.) 1
  • 2.
    Greedy Algorithm -Introduction  Greedy is a special type of algorithm, where we design an algorithm to solve an optimization problem.  An optimization problem means trying to find maximum or minimum value which is optimized result.  "Greedy Method finds out of many options, but you have to choose the best option."  In this method, we have to find out the best method/option out of many present ways. Compiled by: Elias M.(PhD candidate.) 2
  • 3.
    Minimum Spanning Trees •Spanning Tree • A tree (i.e., connected, acyclic graph) which contains all the vertices of the graph • Minimum Spanning Tree • Spanning tree with the minimum sum of weights • Spanning forest • If a graph is not connected, then there is a spanning tree for each connected component of the graph a b c d e g g f i 4 Compiled by: Elias M.(PhD candidate.) 3 8 7 8 11 1 2 7 2 4 14 9 10 6
  • 4.
    Graph -Revision Compiled by:Elias M.(PhD candidate.) 4 Multigraph
  • 5.
    Graph -Revision Compiled by:Elias M.(PhD candidate.) 5
  • 6.
    Graph -Revision Compiled by:Elias M.(PhD candidate.) 6
  • 7.
    Applications of MST •Find the least expensive way to connect a set of cities, terminals, computers, etc. Compiled by: Elias M.(PhD candidate.) 7
  • 8.
    Problem • A townhas a set of houses and a set of roads • A road connects 2 and only 2 houses • A road connecting houses u and v has a repair cost w(u, v) Goal: Repair enough (and no more) roads such that: 1. Everyone stays connected i.e., can reach every house from all other houses 2. Total repair cost is minimum a b c d e h g f i 4 Compiled by: Elias M.(PhD candidate.) 8 8 7 8 11 1 2 7 2 4 14 9 10 6 Minimum Spanning Trees
  • 9.
    • A connected,undirected graph: • Vertices = houses, Edges = roads • A weight w(u, v) on each edge (u, v)  E a b c d e h g f i 4 8 7 8 11 1 2 7 2 4 14 9 10 6 Find T  E such that: 1. T connects all vertices 2. w(T) = Σ(u,v)T w(u, v) is minimized Minimum Spanning Trees Compiled by: Elias M.(PhD candidate.) 9
  • 10.
    Properties of MinimumSpanning Trees • Minimum spanning tree is not unique • MST has no cycles – see why: • We can take out an edge of a cycle, and still have the vertices connected while reducing the cost • # of edges in a MST: • |V| - 1 10 Compiled by: Elias M.(PhD candidate.)
  • 11.
    Growing a MST–Generic Approach a b c d e h g f i 4 Compiled by: Elias M.(PhD candidate.) 8 7 8 11 1 2 7 2 4 14 9 10 6 • Grow a set A of edges (initially empty) • Incrementally add edges to A such that they would belong to a MST – An edge (u, v) is safe for A if and only if A  {(u, v)} is also a subset of some MST Idea: add only “safe” edges 11
  • 12.
    Generic MST algorithm 3. 1.A ←  2. while A is not a spanning tree do find an edge (u, v) that is safe for A 4. A ← A  {(u, v)} 5. return A a b c d h g f i 4 Compiled by: Elias M.(PhD candidate.) 8 7 11 8 1 2 7 2 4 14 9 e 10 6 12
  • 13.
    Minimum Spanning Trees Compiledby: Elias M.(PhD candidate.) 13
  • 14.
    • MST ofa weighted, connected graph G is defined as: A spanning tree of G with minimum total weight. • Example: Consider the example of spanning tree: • For the given graph there are three possible spanning trees. Among them the spanning tree with the minimum weight 6 is the MST for the given graph Compiled by: Elias M.(PhD candidate.) Minimum Spanning Trees 14
  • 15.
    Prim’s Algorithm • Theedges in set A always form a single tree • Starts from an arbitrary “root”: VA = {a} • At each step: • Find a light edge crossing (VA, V - VA) • Add this edge to A • Repeat until the tree spans all vertices a b c d e h g f i 4 8 7 8 11 1 2 7 2 4 14 9 10 6 Compiled by: Elias M.(PhD candidate.) 15
  • 16.
    • STEP 1:Start with a tree, T0, consisting of one vertex • STEP 2: “Grow” tree one vertex/edge at a time • Construct a series of expanding sub-trees T1, T2, … Tn-1. • At each stage construct Ti + 1 from Ti by adding the minimum weight edge connecting a vertex in tree (Ti) to one vertex not yet in tree, choose from “fringe” edges (this is the “greedy” step!) • Algorithm stops when all vertices are included Compiled by: Elias M.(PhD candidate.) Prim’s Algorithm - The Method 16
  • 17.
    How to FindLight Edges Quickly? Use a priority queue Q: • Contains vertices not yet A included in the tree, i.e., (V – V ) A • V = {a}, Q = {b, c, d, e, f, g, h, i} • We associate a key with each vertex v: key[v] = minimum weight of any edge (u, v) connecting v to VA a b c d e h g f i 4 8 7 8 11 1 2 7 2 4 14 9 10 6 w1 w2 Key[a]=min(w1,w2) a 17 Compiled by: Elias M.(PhD candidate.)
  • 18.
    How to FindLight Edges Quickly? (cont.) • After adding a new node to VAwe update the weights of all the nodes adjacent to it e.g., after adding a to the tree, k[b]=4 and k[h]=8 • Key of v is  if v is not adjacent to any vertices in VA a b c d e h g f i 4 8 7 8 11 1 2 7 2 4 14 9 10 6 18 Compiled by: Elias M.(PhD candidate.)
  • 19.
    Example 0        Q = {a, b, c, d, e, f, g, h, i} VA =  Extract-MIN(Q)  a a 4 8 7 8 11 1 2 7 2 4 14 10 6  b  c  d  i 9  e h  g  f  a b h 4 8 7 8 11 1 2 7 2 4 14 9 10 6  c  d  i  e g  f  key [b] = 4  [b] = a  [h] = a key [h] = 8 4      8  Q = {b, c, d, e, f, g, h, i} VA = {a} Extract-MIN(Q)  b  4  8 Compiled by: Elias M.(PhD candidate.) 19
  • 20.
       Example a c 4 8 7 8 11 1 2 7 2 414 9 10 6 key [c] = 8  [c] = b  [h] = a - unchanged key [h] = 8 8     8  A Q = {c, d, e, f, g, h, i} V = {a, b} Extract-MIN(Q)  c a d h 8 g  f i 4 8 7 8 11 1 2 7 2 4 14 9  e 10 6 key [d] = 7 key [f] = 4 key [i] = 2  [d] = c  [f] = c  [i] = c 7  4  8 2 Q = {d, e, f, g, h, i} VA= {a, b, c} Extract-MIN(Q)  i   i 4 b  d  e h 8 4 b g  8 c f  8 7 4 2 Compiled by: Elias M.(PhD candidate.) 20
  • 21.
    Example a 4 8 7 8 11 1 2 7 2 414 10 6 key [h] = 7 key [g] = 6  [h] = i  [g] = i 7  4 6 8 Q = {d, e, f, g, h} V = {a, b, c, i} A Extract-MIN(Q)  f a b c d h g f 4 8 7 8 11 1 2 7 2 4 14 9 10 6 4 b 7 d 9  e key [g] = 2 key [d] = 7 key [e] = 10 7 10 2 8  [g] = f  [d] = c unchanged  [e] = f Q = {d, e, g, h} V = {a, b, c, i, f} A Extract-MIN(Q)  g 8  f 4 8 c 2 i h 7 4 7  g 6 8 2 i 10 e 6 4 7 2 Compiled by: Elias M.(PhD candidate.) 21
  • 22.
    Example a b c d h 4 87 8 11 1 2 7 4 14 10 6 key [h] = 1  [h] = g 7 10 1 Q = {d, e, h} V = {a, b, c, i, f, g} A Extract-MIN(Q)  h a c h g f 4 8 7 8 11 1 2 7 4 14 10 6 9 10 e 7 10 Q = {d, e} VA= {a, b, c, i, f, g, h} Extract-MIN(Q)  d 4 7 7 2 2 i 8 1 4 2 1 Compiled by: Elias M.(PhD candidate.) 22 4 b f 4 7 d 9 10 e g 2 8 2 2 i
  • 23.
    Example a b c d e 4 87 8 11 1 2 7 2 4 14 9 10 6 key [e] = 9  [e] = f 9 Q = {e} V = {a, b, c, i, f, g, h, d} A Extract-MIN(Q)  e Q =  VA= {a, b, c, i, f, g, h, d, e} 4 7 10 h 1 g 2 f 4 2 i 8 9 Compiled by: Elias M.(PhD candidate.) 23
  • 24.
    PRIM(V, E, w,r) 3. 4. 5. 1. Q ←  2. for each u  V do key[u] ← ∞ π[u] ← NIL INSERT(Q, u) 6. DECREASE-KEY(Q, r, 0) 7. while Q   ► key[r] ← 0 8. do u ← EXTRACT-MIN(Q) for each v  Adj[u] do if v  Q and w(u, v) < key[v] then π[v] ← u 9. 10. 11. 12. DECREASE-KEY(Q, v, w(u, v)) O(V) if Q is implemented as a min-heap Executed |V| times Takes O(lgV) O(lgV) Min-heap operations : O(VlgV) Executed O(E) times total Constant Takes O(lgV) O(ElgV) Total time: O(VlogV + ElogV) = O(ElogV) Compiled by: Elias M.(PhD candidate.) 24
  • 25.
    Prim’s Algorithm Compiled by:Elias M.(PhD candidate.) • Prim’s algorithm is a “greedy” algorithm • Greedy algorithms find solutions based on a sequence of choices which are “locally” optimal at each step. • Nevertheless, Prim’s greedy strategy produces a globally optimum solution! 25
  • 26.
    Kruskal’s Algorithm –tofind MST • How is it different from Prim’s algorithm? • Prim’s algorithm grows one tree all the time • Kruskal’s algorithm grows multiple trees (i.e., a forest) at the same time. • Trees are merged together using safe edges • Since an MST has exactly |V| - 1 edges, after |V| - 1 merges, we would have only one component u Compiled by: Elias M.(PhD candidate.) v tree1 tree2 26
  • 27.
    Example: Apply kruskal’salgorithm for the following graph to fin MST Kruskal’s Algorithm –to find MST Compiled by: Elias M.(PhD candidate.) 27
  • 28.
    Kruskal’s Algorithm –tofind MST Compiled by: Elias M.(PhD candidate.) 28
  • 29.
    Kruskal’s Algorithm –tofind MST Compiled by: Elias M.(PhD candidate.) 29
  • 30.
    Kruskal’s Algorithm –tofind MST Compiled by: Elias M.(PhD candidate.) 30
  • 31.
    Kruskal’s Algorithm –tofind MST Compiled by: Elias M.(PhD candidate.) 31
  • 32.
    1. A ← 2. for each vertex v  V 3. do MAKE-SET(v) 4. sort E into non-decreasing order by w 5. for each (u, v) taken from the sorted list 6. do if FIND-SET(u)  FIND-SET(v) 7. then A ← A  {(u, v)} 8. UNION(u, v) 9. return A - Running time: O(V+ElgE+ElgV)=O(ElgE) - Since E=O(V2), we have lgE=O(2lgV)=O(lgV) KRUSKAL(V, E, w) (cont.) O(V) O(ElgE) O(E) O(lgV) O(ElgV) 32
  • 33.
    • Efficiency ofKruskal’s algorithm is based on the time needed for sorting the edge weights of a given graph. • With an efficient sorting algorithm: Efficiency: Θ(|E| log |E| ) • Conclusion: • Kruskal’s algorithm is an “edge based algorithm” • Prim’s algorithm with a heap is faster than Kruskal’s algorithm. Compiled by: Elias M.(PhD candidate.) Kruskal’s Algorithm –Effeciency 33
  • 34.
    Greedy Algorithm -Activity or Task Scheduling Problem This is the dispute of optimally scheduling unit-time tasks on a single processor, where each job has a deadline and a penalty that necessary be paid if the deadline is missed. Compiled by: Elias M.(PhD candidate.) 34
  • 35.
    Greedy Algorithm -Activity or Task Scheduling Problem Compiled by: Elias M.(PhD candidate.) 35
  • 36.
    • Now, scheduleA1 • Next schedule A3 as A1 and A3 are non-interfering. • Next skip A2 as it is interfering. • Next, schedule A4 as A1 A3 and A4 are non-interfering, then next, schedule A6 as A1 A3 A4 and A6 are non-interfering. • Skip A5 as it is interfering. • Next, schedule A7 as A1 A3 A4 A6 and A7 are non-interfering. • Next, schedule A9 as A1 A3 A4 A6 A7 and A9 are non-interfering. • Skip A8 as it is interfering. • Next, schedule A10 as A1 A3 A4 A6 A7 A9 and A10 are non-interfering. • Thus the final Activity schedule is: Greedy Algorithm - Activity or Task Scheduling Problem Compiled by: Elias M.(PhD candidate.) 36
  • 37.
    • Tasks –N •Machines – N at most • Tasks have duration • Machines can only do one task at a time • Overlapping duration – use different machines • Allocate tasks machines ,minimize machines Compiled by: Elias M.(PhD candidate.) Greedy Algorithm - Activity or Task Scheduling Problem 37
  • 38.
    • Order tasksby starting time • Ascending order • Each Job • Allocate it to smallest numbered machines that is idle • Process tasks in order of starting time Greedy Algorithm - Activity or Task Scheduling Problem Compiled by: Elias M.(PhD candidate.) 38
  • 39.
    Greedy Algorithm -Activity or Task Scheduling Problem Task A Compiled by: Elias M.(PhD candidate.) Task B Task C Task F 39
  • 40.
    Greedy Algorithm -Activity or Task Scheduling Problem Task G Compiled by: Elias M.(PhD candidate.) Task E Task D 40
  • 41.
    Thank You ! Compiledby: Elias M.(PhD candidate.) 41