Minimum Spanning Tree Algorithms - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Minimum Spanning Tree Algorithms

Description:

Input: Set of coins of different denominations. while (more coin-sizes && change amount owed) ... We will show two ways to build a minimum spanning tree. ... – PowerPoint PPT presentation

Number of Views:86
Avg rating:3.0/5.0
Slides: 23
Provided by: csHel
Category:

less

Transcript and Presenter's Notes

Title: Minimum Spanning Tree Algorithms


1
Minimum Spanning Tree Algorithms
  • Introduction to Greedy Technique
  • Prims Algorithm

2
The Greedy Technique(Method)
  • The greedy technique
  • Problems explored
  • The coin changing problem
  • Minimum spanning trees
  • Define the problem
  • Present different algorithms and their analysis

Greedy/Prims 2
3
Optimization problems
  • An optimization problem Given a problem
    instance, a set of constraints and an objective
    function. Find a feasible solution for the given
    instance for which the objective function has an
    optimal value
  • either maximum or minimum depending on the
    problem being solved.
  • A feasible solution is a solution that satisfies
    the problems constraints
  • The constraints specify the limitations on the
    required solutions. For example in the knapsack
    problem we require that the items in the knapsack
    will not exceed a given weight

Greedy/Prims 3
4
The Greedy Technique(Method)
  • Greedy algorithms make good local choices in the
    hope that they result in an optimal solution.
  • They result in feasible solutions.
  • Not necessarily an optimal solution.
  • A proof is needed to show that the algorithm
    finds an optimal solution.
  • A counter example shows that the greedy algorithm
    does not provide an optimal solution.

Greedy/Prims 4
5
Pseudo-code for Greedy Algorithm
  • set Greedy (Set Candidate)
  • solution new Set( )
  • while (Candidate.isNotEmpty())
  • next Candidate.select() //use selection
    criteria,
  • //remove from Candidate and
    return value
  • if (solution.isFeasible( next)) //constraints
    satisfied
  • solution.union( next)
  • if (solution.solves()) return solution
  • return null

Greedy/Prims 5
6
Pseudo code for greedy cont.
  • The method select() chooses a candidate based on
    a local selection criteria, removes it from
    Candidate, and returns its value.
  • The method isFeasible() checks whether adding the
    selected value to the current solution can result
    in a feasible solution (no constraints are
    violated).
  • The method solves() checks whether the problem is
    solved.

Greedy/Prims 6
7
Coin changing problem
  • Problem Return correct change using a minimum
    number of coins.
  • A greedy solution (next slide)
  • American money
  • The change 37 cents.
  • The coins are 1 quarter, 1 dime, 2 cents.
  • Solution is optimal.
  • Is it optimal for any set of coin sizes?

(12,D,N,P/15)
Greedy/Prims 7
8
A greedy solution
  • Input Set of coins of different denominations
  • while (more coin-sizes changeltamount owed)
  • Choose the largest remaining coin-size //
    Selection
  • // feasibility check
  • If (adding the coin does not make the change
    exceed the amount owed )
  • then add coin to change
  • else delete coin-size
  • // solution check
  • If ( total value of the change equals amount
    owed)return change

Greedy/Prims 8
9
What is A Spanning Tree?
  • A spanning tree for an undirected graph G(V,E)
    is a subgraph of G that is a tree and contains
    all the vertices of G
  • Can a graph have more than one spanning tree?
  • Can an unconnected graph have a spanning tree?

a
u
b
e
v
c
f
d
Greedy/Prims 9
10
Minimal Spanning Tree.
  • The weight of a subgraph is the sum of the
    weights of it edges.
  • A minimum spanning tree for a weighted graph is a
    spanning tree with minimum weight.
  • Can a graph have more then one minimum spanning
    tree?

a
4
4
9
3
u
b
e
2
14
10
15
v
c
f
3
8
d
Greedy/Prims 10
11
Example of a Problem that Translate into a MST
  • The Problem
  • Several pins of an electronic circuit must be
    connected using the least amount of wire.
  • Modeling the Problem
  • The graph is a complete, undirected graph G (
    V, E ,W ), where V is the set of pins, E is the
    set of all possible interconnections between the
    pairs of pins and w(e) is the length of the wire
    needed to connect the pair of vertices.
  • Find a minimum spanning tree.

Greedy/Prims 11
12
Greedy Choice
  • We will show two ways to build a minimum spanning
    tree.
  • A MST can be grown from the current spanning tree
    by adding the nearest vertex. (Prim's algorithm)
  • A MST can be grown from a forest of spanning
    trees by adding the smallest edge. (Kruskal's
    algorithm)

Greedy/Prims 12
13
Notation
  • Tree-vertices in the tree constructed so far
  • Non-tree vertices rest of vertices

Greedy/Prims 13
14
The Dijkstra/Prim algorithm Main Idea
  • Select a vertex to be a tree-node
  • while (there are non-tree vertices)
  • select an edge of minimum weight between a tree
    node and a non-tree node
  • add the selected edge and its new vertex to the
    tree
  • if (all vertices are tree-nodes) solved.

Greedy/Prims 14
15
Implementation Issues
  • How is the graph implemented?
  • We repeatedly need to update the distance of each
    vertex to the MST until the shortest distance is
    found, there must be fast access to all the
    adjacent vertices.
  • How is the set of non-tree vertices represented?
  • A priority queue PQ is used with the priority
    Dv equal to the minimum distance of each
    non-tree vertex v to the tree.
  • There will be additional data structures needed
    to keep track of bookkeeping between the
    algorithm and the priority queue. They will not
    be presented in the pseudo code.

Greedy/Prims 15
16
Priority Queue
  • The PQ contains an element pair (priority, data)
    for all non tree vertices,and an additional data
    structure handle.
  • The priority (D) is the minimum distance of the
    non tree vertices to the tree so far.
  • distance Dv is the shortest edge connecting
    the vertex v to the tree found so far
  • The data element contains a vertex object, and an
    edge object
  • edge is set by Prims algorithm.
  • handlev provides the location of the non-tree
    vertex v in PQ. (Note that make-heap, extractMin,
    and decreasePriorityValue update handle)

Greedy/Prims 16
17
Prims Algorithm
1. for each u ?V2. do D u ? ?3. D r
? 0 4. PQ ?make-heap(D,V, )//No edges 5. T ?
?6.7. while PQ ? ? do 8. (u,e ) ?
PQ.extractMin() 9. add (u,e) to T10.
for each v ? Adjacent (u ) 11. do if v ?PQ
w( u, v ) lt D v 12. then D v
? w (u, v) 13. PQ.decreasePriorityV
alue ( Dv, v, (u,v )) 14. return T // T is
a mst.
  • Lines 1-5 initialize the priority queue PQ to
    contain all Vertices. Ds for all vertices except
    r, are set to infinity.
  • r is the starting vertex of the TThe T so far
    is empty
  • Add closest vertex and edge to current T
  • Get all adjacent vertices,update D of each
    adjacent non-tree vertex, save the current
    minimum weight edge, and restore the heap
    property.
  • Note that handlev can be used to check v ?PQ

Greedy/Prims 17
18
Prims AlgorithmInitialization
  • Prim (G )
  • 1. for each u ?V2. do D u ? ?3. D r
    ? 0
  • 4. PQ ?make-heap(D,V, )//No edges 5. T ? ?

Greedy/Prims 18
19
Building the MST
  • // solution check
  • 7. while PQ ? ? do//Selection and
    feasibility 8. (u,e ) ? PQ.extractMin()
    // T contains the solution so far . 9. add
    (u,e) to T10. for each v ? Adjacent (u )
    11. do if v ? PQ w( u, v ) lt D v
    12. then D v ? w (u, v) 13.
    PQ.decreasePriorityValue
  • (Dv, v, (u,v) ) 14. return T

Greedy/Prims 19
20
  • Prim (G)1. for each u ?V2. do D u ? ?3.
    D r ? 0
  • 4. PQ ?make-heap(D,V, )5. T ? ?

D 0, ?, , ?
PQ ( 0,(a,?)), (?,(b,?)), ...(?,(h,?))
T
r
G
a
4
6
9
5
e
b
g
2
14
10
15
f
c
h
3
8
d
Greedy/Prims 20
21
// relaxation 11. do if v ?PQ w( u, v ) lt
D v 12. then D v ? w (u, v) 13.
PQ.decreasePriorityValue ( Dv, v,
(u,v))
  • 7. while PQ ? ? do 8. (u,e) ?
    PQ.extractMin()9. add (u,e) to T10. for
    each v ? Adjacent (u ) 11. // relaxation
    operation15. return T

D 0,
r
G
a
T
4
6
9
5
e
b
g
PQ
2
14
10
15
f
c
h
3
8
d
Greedy/Prims 21
22
Homework- lab? Trees
  • What is a connected graph?
  • A tree is a connected graph with no cycles. Other
    definitions?
  • How many edges does a tree have?
  • What happens when an edge is added to a
    connected graph? a tree?
  • What happens when an edge is removed from a tree?
  • When an edge is removed from a cycle in a
    connected graph, does the graph remain connected?

Greedy/Prims 22
Write a Comment
User Comments (0)
About PowerShow.com