1 / 11

Lecture 17:

Lecture 17:. Spanning Trees Minimum Spanning Trees. Introduction. Multicast Spanning Tree. Web Spiders. Web Spider: Screen Scrape. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; namespace screenscrape { class Program

Download Presentation

Lecture 17:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 17: Spanning Trees Minimum Spanning Trees

  2. Introduction

  3. Multicast Spanning Tree

  4. Web Spiders

  5. Web Spider: Screen Scrape using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; namespace screenscrape { classProgram { staticvoid Main(string[] args) { WebClient webClient = newWebClient(); conststring strUrl = "http://www.anypage.com"; byte[] reqHTML; reqHTML = webClient.DownloadData(strUrl); UTF8Encoding objUTF8 = newUTF8Encoding(); string html = objUTF8.GetString(reqHTML); Console.WriteLine(objUTF8.GetString(reqHTML)); Console.ReadKey(); } } }

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

  8. B 1 A 1 C 2 5 2 2 4 D 1 2 3 E G 1 2 F 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. 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. 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. B 1 A 1 C 2 5 2 2 D 4 1 3 E G 1 2 F 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. 2

  10. B B B 1 1 1 A A A C 1 C 1 C 1 2 2 2 5 5 5 2 2 2 2 2 2 D D D 4 4 4 1 2 1 2 1 2 3 3 3 E E E G G G 1 2 1 2 1 2 F F F B B 1 B 1 1 A A C 1 A C 1 C 1 2 2 2 5 2 5 2 2 5 2 2 D 2 D 4 D 4 1 4 2 1 2 1 2 3 E 3 G E 3 G E G 1 2 1 2 1 2 F F F 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

More Related