11
Lecture 17: Spanning Trees Minimum Spanning Trees

Lecture 17: Spanning Trees Minimum Spanning Trees

Embed Size (px)

Citation preview

Page 1: Lecture 17: Spanning Trees Minimum Spanning Trees

Lecture 17:

Spanning TreesMinimum Spanning Trees

Page 2: Lecture 17: Spanning Trees Minimum Spanning Trees

Introduction

Page 3: Lecture 17: Spanning Trees Minimum Spanning Trees

Multicast Spanning Tree

Page 4: Lecture 17: Spanning Trees Minimum Spanning Trees

Web Spiders

Page 5: Lecture 17: Spanning Trees Minimum Spanning Trees

using System;using System.Collections.Generic;using System.Linq;using System.Text;using 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(); } }}

Web Spider: Screen Scrape

Page 6: Lecture 17: Spanning Trees Minimum Spanning Trees

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).

Page 7: Lecture 17: Spanning Trees Minimum Spanning Trees

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).

Q1 = 2Q1 = 4Q1 = 1Q1 = 3

1 2 34

Page 8: Lecture 17: Spanning Trees Minimum Spanning Trees

Minimum Spanning Trees

C

D

F

E

A

G

B

4

2

3

5

1

2

1

2

2

1

2

1

The minimum spanning tree problem is to find the minimum weight tree embedded in a weighted graph that includes all the vertices.

Weighted graph data representations 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

matrix A B C D E F GA - 1 - - 2 - -B 1 - 1 2 5 2 2C - 1 - - - - 4D - 2 - - 3 - 1E 2 5 - 3 - 1 -F - 2 - - 1 - 2G - 2 4 1 - 2 -

Which data representation would you use in an implementation of a minimum spanning tree algorithm? Why?

Page 9: Lecture 17: Spanning Trees Minimum Spanning Trees

Prim's Algorithm

1. Choose an arbitrary starting vertex vj

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.

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.

Given a weighted graph G consisting of a set of vertices V and a set of edges E with weights, where Prepare a vertex set and an edge set to hold elements selected by Prim's Algorithm.

VvvandEvve jiji ,),(

2

C

D

F

E

A

G

B

4

2

3

5

1

2

1

2

1

2

1

Page 10: Lecture 17: Spanning Trees Minimum Spanning Trees

Kruskal's AlgorithmThe 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.

C

D

F

E

A

G

B

4

2

3

5

1

2

1

2

2

1

2

1

C

D

F

E

A

G

B

4

2

3

5

1

2

1

2

2

1

2

1

C

D

F

E

A

G

B

4

2

3

5

1

2

1

2

2

1

2

1

C

D

F

E

A

G

B

4

2

3

5

1

2

1

2

2

1

2

1

C

D

F

E

A

G

B

4

2

3

5

1

2

1

2

2

1

2

1

C

D

F

E

A

G

B

4

2

3

5

1

2

1

2

2

1

2

1

Page 11: Lecture 17: Spanning Trees Minimum Spanning Trees

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