Lecture 13: Tree Traversals Algorithms on Trees.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Review Binary Search Trees Operations on Binary Search Tree
Minimum Spanning Tree Sarah Brubaker Tuesday 4/22/8.
Greedy Algorithms Greed is good. (Some of the time)
10.4 Spanning Trees. Def Def: Let G be a simple graph. A spanning tree of G is a subgraph of G that is a tree containing every vertex of G See handout.
22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Lecture 3: Greedy Method Greedy Matching Coin Changing Minimum Spanning Tree Fractional Knapsack Dijkstra's Single-Source Shortest-Path.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank and Modified By Mingwu Chen Trees.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
Chapter 15 Graph Theory © 2008 Pearson Addison-Wesley. All rights reserved.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Lecture 17: Spanning Trees Minimum Spanning Trees.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Lecture 5: Backtracking Depth-First Search N-Queens Problem Hamiltonian Circuits.
Lecture 2: General Problem-Solving Methods. Greedy Method Divide-and-Conquer Backtracking Dynamic Programming Graph Traversal Linear Programming Reduction.
CSCI 115 Chapter 7 Trees. CSCI 115 §7.1 Trees §7.1 – Trees TREE –Let T be a relation on a set A. T is a tree if there exists a vertex v 0 in A s.t. there.
Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Prim's Algorithm This algorithm starts with one node. It then, one by one, adds a node that is unconnected to the new graph to the new graph, each time.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
Spanning Trees. A spanning tree for a connected, undirected graph G is a graph S consisting of the nodes of G together with enough edges of G such that:
Agenda Review: –Planar Graphs Lecture Content:  Concepts of Trees  Spanning Trees  Binary Trees Exercise.
Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs
1 Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible b locally optimal.
Introduction to State Space Search
Lecture 19 Minimal Spanning Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
Discrete Mathematics Chapter 10 Trees. Outline 10.1 Introduction to Trees 10.2 Applications of Trees 10.3 Tree Traversal 10.4 Spanning Trees 10.5 Minimal.
MATRIX FORM OF PRIM’S ALGORITHM. This network may be described using a Distance Matrix.
Discrete Mathematics Trees.
Depth-First Search N-Queens Problem Hamiltonian Circuits
Greedy Technique.
May 12th – Minimum Spanning Trees
Minimum Spanning Tree Chapter 13.6.
Discrete Mathematicsq
Spanning Trees.
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
12. Graphs and Trees 2 Summary
Design & Analysis of Algorithm Greedy Algorithm
Minimum Spanning Trees and Shortest Paths
Computer Science cpsc322, Lecture 14
Greedy Method     Greedy Matching     Coin Changing     Minimum Spanning Tree     Fractional Knapsack     Dijkstra's Single-Source Shortest-Path.
CSCE350 Algorithms and Data Structure
Back Tracking.
Graphs Chapter 13.
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
Graphs.
Graphs Part 2 Adjacency Matrix
Minimum spanning trees
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum spanning trees
CSCI2100 Data Structures Tutorial
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Networks Prim’s Algorithm
Kruskal’s Algorithm AQR.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Adversarial Search and Game Playing Examples
CSE 373: Data Structures and Algorithms
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Lecture 13: Tree Traversals Algorithms on Trees

Edge-List Representation of a Tree

Types of Tree Traversal

Depth-First Traversal of a Tree

Breadth-First Traversal of a Tree

Introduction (1) Search Trees (2) Decision Trees (3) Huffman Coding

Binary Search Trees

Building a Binary Search Sort Tree

Locating and Adding Items to a Binary Search Tree

Making a Binary Tree Full

Find the Counterfeit Coin 7 of the 8 coins weigh the same. The counterfeit is lighter. Find it in two weighings on a balance scale.

Decision Tree

Huffman Coding

Huffman Coding for 5 Symbols

Tic-Tac-Toe

Partial Game Tree for Tic-Tac-Toe

The Min-Max Game Strategy

Min-Max Game Strategy Values for NIM alpha-beta pruning

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 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 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(); }

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 4 2 3 Q1 = 2 Q1 = 4 Q1 = 1 Q1 = 3

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

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