没有幻灯片标题 - Zhejiang University

Download Report

Transcript 没有幻灯片标题 - Zhejiang University

§5 Minimum Spanning Tree
【Definition】 A spanning tree of a graph G is a tree which
consists of V( G ) and a subset of E( G )
〖Example〗 A complete graph and three of its spanning trees
Note:
 The minimum spanning tree is a tree since it is acyclic -- the
number of edges is |V| – 1.
 It is minimum for the total cost of edges is minimized.
 It is spanning because it covers every vertex.
 A minimum spanning tree exists iff G is connected.
 Adding a non-tree edge to a spanning tree, we obtain a cycle.
1/9
§5 Minimum Spanning Tree
Greedy Method
Make the best decision for each stage, under the
following constrains :
(1) we must use only edges within the graph;
(2) we must use exactly |V| 1 edges;
(3) we may not use edges that would produce a cycle.
1. Prim’s Algorithm – grow a tree
/* very similar to Dijkstra’s algorithm */
2
v1
4
1
2
v3
5
2/9
3
v4
8
v6
v2
1
10
7
v5
4
6
v7
§5 Minimum Spanning Tree
2. Kruskal’s Algorithm – maintain a forest
void Kruskal ( Graph G )
T = O( |E| log |E| )
{ T={};
while ( T contains less than |V| 1 edges
&& E is not empty ) {
choose a least cost edge (v, w) from E ; /* DeleteMin */
delete (v, w) from E ;
if ( (v, w) does not create a cycle in T )
add (v, w) to T ; p.341
/* Union /9.15
Find */
else
discard (v, w) ; A test case and
}
a discussion on uniqueness.
if ( T contains fewer than |V| 1 edges )
Error ( “No spanning tree” ) ;
}
Home work:
A more detailed pseudocode is given by Figure 9.58 on p.321
3/9
§6 Applications of Depth-First Search
/* a generalization of preorder traversal */
void DFS ( Vertex V ) /* this is only a template */
{ visited[ V ] = true; /* mark this vertex to avoid cycles */
for ( each W adjacent to V )
if ( !visited[ W ] )
DFS( W );
} /* T = O( |E| + |V| ) as long as adjacency lists are used */
1. Undirected Graphs
DFS ( 0 )
0
1
2
4
7
5
6
4/9
3
8
void ListComponents ( Graph G )
{ for ( each V in G )
if ( !visited[ V ] ) {
DFS( V );
printf(“\n“);
}
0146523
}
78
§6 Applications of Depth-First Search
2. Biconnectivity
 v is an articulation point if G’ = DeleteVertex( G, v ) has at least 2
connected components.


 G is a biconnected graph if G is connected and has no articulation
points.
Biconnected
Articulation
 A biconnected
component
is
a
maximal
biconnected subgraph.
graph
point

〖Example〗
0
8
1
7
2
3
4
5
6
8
1
7
1
2
Connected graph
5/9
9
0

7
3
4
3
5
5
6
Biconnected components
9
7
Note: No edges can
be shared by two or
more biconnected
components. Hence
E(G) is partitioned
by the biconnected
components of G.
§6 Applications of Depth-First Search
Finding the biconnected components of a connected undirected G
 Use depth first search to obtain a spanning tree of G
Depth first spanning tree
DFS ( 3 )
0
9
8
0 4
8
9
3
Depth first
3
Back(Num)
edges
number
Note:
an
4 1 5 5
1
7 7
::= (u,Ifv)uistree
ancestor
2
0
5
and u (orofv)v,is
then
Num( uof
)
2 2 6 6
2
3
5
an ancestor
< Num(
).
v (orvu).
1 3 7 7
4
6
1
6
0 4
8 9
8 9
 Find the articulation points in G
 The root is an articulation point iff it has at least 2 children
 Any other vertex u is an articulation point iff u has at least 1
child, and it is impossible to move down at least 1 step and
then jump up to u’s ancestor.
6/9
§6 Applications of Depth-First Search
0
Low( u)  m in {Num( u),
3
m in {Low( w ) | w is a ch ildof u },
m in {Num( w ) | ( u, w ) is a back e dge} }
4 1 5 5
2 2
6 6
1 3
7 7
0 4
8 9
vertex 0 1 2 3 4 5 6 7 8 9
Num 4 3 2 0 1 5 6 7 9 8
Low 4 0 0 0 0 5 5 5 9 8
8 9
Therefore, u is an articulation point iff
(1) u is the root and has at least 2 children; or
(2) u is not the root, and has at least 1 child such that
Low( child )  Num( u ).
Please read the pseudocodes on p.327 and p.329 for more details.
7/9
3. Euler Circuits
§6 Applications of Depth-First Search
Draw each line exactly once without lifting your pen from the
paper – Euler tour
Draw each line exactly once without lifting your pen from the
paper, AND finish at the starting point – Euler curcuit
〖Proposition〗 An Euler circuit is possible only if the graph is
connected and each vertex has an even degree.
〖Proposition〗 An Euler tour is possible if there are exactly two
vertices having odd degree. One must start at one of the odd-degree
vertices.
8/9
§6 Applications of Depth-First Search
DFS
2
3
6
8
1
4
5
7
9Home
work:10
11
12
Self-study the digraphs
Note:
and strong components
The path should be maintained as a linked list.
on
p.
331-334
For each adjacency list, maintain a pointer to the last edge
scanned.
and do p.342 9.27
T = O( |E| + |V| )
Find a simple cycle in an undirected graph that visits every
vertex – Hamilton cycle
9/9