Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 17: Spanning Trees Minimum Spanning Trees.

Similar presentations


Presentation on theme: "Lecture 17: Spanning Trees Minimum Spanning Trees."— Presentation transcript:

1 Lecture 17: Spanning Trees Minimum Spanning Trees

2 Introduction

3 Multicast Spanning Tree

4 Web Spiders

5 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

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

7 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 Q j (the queen of the jth column). Q 1 = 2 Q 1 = 4 Q 1 = 1 Q 1 = 3 1 2 3 4

8 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 G A - 1 - - 2 - - B 1 - 1 2 5 2 2 C - 1 - - - - 4 D - 2 - - 3 - 1 E 2 5 - 3 - 1 - F - 2 - - 1 - 2 G - 2 4 1 - 2 - Which data representation would you use in an implementation of a minimum spanning tree algorithm? Why?

9 Prim's Algorithm 1. Choose an arbitrary starting vertex v j 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. 2 C D F E A G B 4 2 3 5 1 2 1 2 1 2 1

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

11 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 ppt "Lecture 17: Spanning Trees Minimum Spanning Trees."

Similar presentations


Ads by Google