Tải bản đầy đủ (.pptx) (19 trang)

Discrrete mathematics for computer science prim

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (2.24 MB, 19 trang )

Minimum Spanning Trees


Subgraph
• A graph G is a subgraph of graph H if
– The vertices of G are a subset of the
vertices of H, and
– The edges of G are a subset of the
edges of H.


Spanning Tree
• A spanning tree of a graph G is a
subgraph of G that is a tree and includes
all the vertices of G.
• Every connected graph has a spanning
tree
– Start with G. If G is a tree then it is a ST of G.
If not, G has a cycle. Remove any edge of the
cycle. The result is a connected subgraph of
G. Repeat. Process must terminate since the
number of edges is finite.


Spanning Trees


Spanning Trees

a spanning tree



Spanning Trees

another spanning tree
(can have many)


Weighted Graphs
• A weighted graph is a graph with a
mapping of edges to R+ assigning a
weight to each edge
• A minimum spanning tree of a
weighted graph is a spanning tree of
minimum total weight (sum of the
weights of the edges in the tree)


Minimum Spanning Trees
3

4
1

4
6

3

7
1


2


Minimum Spanning Trees
3

4
1

4
6

3

7
1

1. Choose any vertex

2


Minimum Spanning Trees
3

4
1

4

6

3

7
1

2

2. Choose minimum weight
edge connected to that vertex


Minimum Spanning Trees
3

4
1

4
6

3

7
1

2

3. Choose minimum weight

edge connected to chosen
vertices


Minimum Spanning Trees
3

4
1

4
6

3

7
1

2

4. Choose minimum weight
edge connected to chosen
vertices


Minimum Spanning Trees
3

4
1


4
6

3

7
1

2

5. Choose minimum weight
edge connected to chosen
vertices without creating a


Minimum Spanning Trees
3

4
1

4
6

3

7
1


2

6. Choose minimum weight
edge connected to chosen
vertices without creating a


Prim’s algorithm
• To find a MST of a connected weighted
graph G, we build a tree one edge at a
time
– Start with T being any node of G
– Pick a minimum weight edge that does not
create a cycle and that connects a node in
T to a node not in T. Add that edge to T.
– Repeat until all nodes of G have been
added to T.

• A “Greedy algorithm”


Proof that Prim’s algorithm works
• Invariant: At every stage, T is a
subgraph of a MST
• True initially since T is just a single node
• The algorithm runs until all nodes have
been added to T, so in the end the
invariant says that T is a MST.
• So we just need to show that each stage
of the algorithm preserves the invariant.



Proof that the algorithm maintains
the invariant
• Proof by contradiction. Suppose there is
a stage at which the invariant is true
before and false after.
• Let e be the edge added when the
invariant is first violated. Suppose e
joins node v in T to node w not in T.
• So T is a subgraph of a MST but T+e is
not.
• Let T’ be a MST of which T is a subgraph.
• Edge e is not in T’.


Proof that the algorithm maintains
the invariant
• Since T’ is a spanning tree, there is a path p from v to
w in T’. Path p does not go through e since e is not
part of T’. So p+e is a cycle consisting of two different
paths from u to v.
• Path p must include an edge f which has one end in T
and the other not in T.
• Edge f must have weight ≥ the weight of e, otherwise
the algorithm would have chosen to add edge f to T
instead of e.
• Removing f from T’ and adding e results in a spanning
tree of weight ≤ the weight of T’ and including T as a
subgraph, contradicting the assumption that T+e was

not a subgraph of a MST. ✔


Finis



×