Slide 1

advertisement
Lecture 17:
Spanning Trees
Minimum Spanning Trees
Introduction
Multicast Spanning Tree
Web Spiders
Web Spider: Screen Scrape
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Net;
namespace screenscrape
{
class Program
{
static void Main(string[] args)
{
WebClient webClient = new WebClient();
const string strUrl = "http://www.anypage.com";
byte[] reqHTML;
reqHTML = webClient.DownloadData(strUrl);
UTF8Encoding objUTF8 = new UTF8Encoding();
string html = objUTF8.GetString(reqHTML);
Console.WriteLine(objUTF8.GetString(reqHTML));
Console.ReadKey();
}
}
}
N-Queens Problem
A classic backtracking algorithm is the solution to the N-Queens problem. In this
problem you are to place queens (chess pieces) on an NxN chessboard in such a
way that no two queens are directly attacking one another. That is no two queens
share the same row, column or diagonal on the board.
Backtracking Approach - Version 1: Until all queens are placed, choose the first
available location and put the next queen in this position. If queens remain to be
placed and no space is left, backtrack (by removing the last queens placed and
placing it in the next available position).
N-Queens: Version Two
Some analysis of this problem shows that, since N queens must be placed on an
NxN board, every row and column will have exactly one queen. That is, no two
queens can share a row or column, otherwise they would be attacking each
other. Using this simple observation we can redefine our algorithm to one in
which we are to associate each queen with 1 of n values. That is find a row
number i for each queen Qj (the queen of the jth column).
1
Q1 = 2
Q1 = 4
Q1 = 1
Q1 = 3
2
3
4
Minimum Spanning Trees
The minimum spanning tree problem is to find the minimum weight tree
embedded in a weighted graph that includes all the vertices.
1
Weighted graph data representations
B
A
edge list
AB 1
AE 2
BC 1
BD 2
BE 5
BF 2
BG 2
CG 4
DE 3
DG 1
EF 1
FG 2
A
B
C
D
E
F
G
A
1
2
-
matrix
B C D E
1 - - 2
- 1 2 5
1 - - 2 - - 3
5 - 3 2 - - 1
2 4 1 -
2
F
2
1
2
G
2
4
1
2
-
5
C
2
D
E
1
2
1
4
3
1
2
2
G
F
Which data representation would
you use in an implementation of a
minimum spanning tree algorithm?
Why?
Prim's Algorithm
Given a weighted graph G consisting of a set of vertices V and a set of edges E with
weights, where e  (vi , v j )  E and vi , v j  V Prepare a vertex set and an edge set to
hold elements selected by Prim's Algorithm.
1. Choose an arbitrary starting vertex vj
3. Include this edge in the edge list and its
vertices in the vertex list.
4.Repeat Steps 2 and 3 until all vertices are in
the vertex list.
B
1
2. Find the smallest edge e incident with with a
vertex in the vertex set whose inclusion in the
edge set does not create a cycle.
A
2
2
5
C
2
D
E
1
4
1
3
1
2
F
G
2
Kruskal's Algorithm
The minimum spanning tree problem can also be solved using Kruskal's Algorithm.
In this approach, we simply choose minimum-weight edges from the graph so long as
an edge does not create a cycle in the edge set. We stop choosing edges when every
vertex is a node for at least one of the edges in the set and the tree is connected.
B
1
A
2
2
D
4
1
3
E
E
2
5
3
4
G
F
2
2
E
1 C
2
1
3
4
G
2
2
F
2
B
1 C
2
5
2
D
1
E
2
G
1
2
F
3
A
D
1
4
1
1
2
2
E
2
B
5
2
D
2
A
2
D
1
F
1 C
2
5
2
G
1
1 C
1
E
3
1
A
4
1
B
1
2
D
2
F
2
2
G
1
A
1 C
2
5
2
B
1
A
1 C
2
5
B
1
3
4
G
1
F
2
2
Summary
Spanning Tree
A spanning tree includes all the nodes of a graph
A graph is connected IFF is has a spanning tree
Multicast Spanning Tree
Web Spiders
N-Queens Problem
Prim's & Kruskal's Algorithms
Download