Math 15 - Spring Homework 5.2 Solutions

Similar documents
TA: Jade Cheng ICS 241 Recitation Lecture Notes #12 November 13, 2009

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

CSE 100 Minimum Spanning Trees Prim s and Kruskal

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

Homework Assignment #3 Graph

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

Homework 5: Graphs, Minimum Spanning Trees, and Dijkstra Shortest-Path

Data Structures - Binary Trees and Operations on Binary Trees

Chapter 20: Binary Trees

CSC148 Week 6. Larry Zhang

ANS:

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Name CPTR246 Spring '17 (100 total points) Exam 3

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

Trees Algorhyme by Radia Perlman

NANYANG TECHNOLOGICAL UNIVERSITY SEMESTER II EXAMINATION MH 1301 DISCRETE MATHEMATICS TIME ALLOWED: 2 HOURS

Computer Science 302 Spring 2017 (Practice for) Final Examination, May 10, 2017

Computer Science E-22 Practice Final Exam

CSE 100: GRAPH ALGORITHMS

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

March 20/2003 Jayakanth Srinivasan,

Dijkstra s algorithm for shortest paths when no edges have negative weight.

CPSC 331 Term Test #2 March 26, 2007

Minimum-Cost Spanning Tree. Example

CAD Algorithms. Categorizing Algorithms

Example. Minimum-Cost Spanning Tree. Edge Selection Greedy Strategies. Edge Selection Greedy Strategies

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY

Spanning Trees. Lecture 20 CS2110 Spring 2015

3. According to universal addressing, what is the address of vertex d? 4. According to universal addressing, what is the address of vertex f?

Spanning Trees 4/19/17. Prelim 2, assignments. Undirected trees

Spanning Trees. Lecture 22 CS2110 Spring 2017

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Data Structures Brett Bernstein

CSC148-Section:L0301

18 Spanning Tree Algorithms

Discrete Mathematics 2 Exam File Spring 2012

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee

Graphs Introduction and Depth first algorithm

CS 280 Problem Set 10 Solutions Due May 3, Part A

UNIT 3. Greedy Method. Design and Analysis of Algorithms GENERAL METHOD

CSE 100: B+ TREE, 2-3 TREE, NP- COMPLETNESS

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

Minimum spanning trees

Discrete Mathematics. Chapter 7. trees Sanguk Noh

LECTURE 3 ALGORITHM DESIGN PARADIGMS

Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

Union Find and Greedy Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 8

7.3 Spanning trees Spanning trees [ ] 61

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Algorithm Design Paradigms

ECE Spring 2018 Problem Set #0 Due: 1/30/18

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

Total Score /1 /20 /41 /15 /23 Grader

Binary Trees, Binary Search Trees

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE. Sample Final Exam

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Greedy algorithms. Given a problem, how do we design an algorithm that solves the problem? There are several strategies:

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Introduction to Algorithms May 14, 2003 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser.

Minimum Spanning Trees

Trees. (Trees) Data Structures and Programming Spring / 28

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Info 2950, Lecture 16

Greedy Approach: Intro

INF2220: algorithms and data structures Series 1

Introduction to Optimization

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Chapter 9 Graph Algorithms

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Associate Professor Dr. Raed Ibraheem Hamed

Solutions to Math 381 Quiz 2

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Math 15 - Spring Homework 2.6 Solutions 1. (2.6 # 20) The following graph has 45 vertices. In Sagemath, we can define it like so:

CSE 21 Mathematics for Algorithm and System Analysis

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

PROGRAMMING IN C++ (Regulation 2008) Answer ALL questions PART A (10 2 = 20 Marks) PART B (5 16 = 80 Marks) function? (8)

Programming II (CS300)

CS 4800: Algorithms & Data. Lecture 1 January 10, 2017

CMSC th Lecture: Graph Theory: Trees.

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

Your favorite blog : (popularly known as VIJAY JOTANI S BLOG..now in facebook.join ON FB VIJAY

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

tree follows. Game Trees

ALGORITHM DESIGN GREEDY ALGORITHMS. University of Waterloo

Binary Search Trees. See Section 11.1 of the text.

12 Abstract Data Types

Lecture 8: The Traveling Salesman Problem

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

a b c d a b c d e 5 e 7

CSE 373 Spring Midterm. Friday April 21st

These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions.

2. (a) Explain when the Quick sort is preferred to merge sort and vice-versa.

22 Elementary Graph Algorithms. There are two standard ways to represent a

Lecture 34 Fall 2018 Wednesday November 28

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

License. Discrete Mathematics. Tree. Topics. Definition tree: connected graph with no cycle. examples. c T. Uyar, A. Yayımlı, E.

Transcription:

Math 15 - Spring 2017 - Homework 5.2 Solutions 1. (5.2 # 14 (not assigned)) Use Prim s algorithm to construct a minimal spanning tree for the following network. Draw the minimal tree and compute its total weight. Prim s algorithm has precondition: N is a connected network. postcondition: T is a minimal spanning tree of N. The algorithm is: T <- e_1, where e_1 is the shortest edge of N while T does not contain all of N s vertices e <- the shortest edge between a vertex in T and a vertex not in T. Add edge e and the new vertex to T. There are two choices for e 1, so we pick the one on top, though it doesn t seem to matter in the end you get this minimal spanning tree, as illustrated by the sequence below, reading roughly left to right and down:

Math 15 Homework 2.4 Solutions - Page 2 of 5 1/30/17 2. (5.2 # 16) Kruskal s algorithm gives another way to find a minimal spanning tree in a network. Algorithm 5.13 Kruskal s Algorithm for constructing a minimal spanning tree. Preconditions: N is a connected network with n > 2 vertices. Postconditions: T is a minimal spanning tree of N. Append e 1 and its vertices to T, where e 1 is the shortest edge of N. for i {2,..., n 1} do e i the shortest edge whose addition to T will not form a circuit. Add edge e i (and its vertices) to T. (a) Is Kruskal s algorithm greedy? Explain. Yes, it s greed: it s grabbing the smallest things first without regard to anything. (b) Use Kruskal s algorithm to construct a minimal spanning tree of the network in Figure 5.10. ANS: You end up with the same spanning tree as in Prim s algo of exercise 14 above. (c) Use Kruskal s algorithm to construct a minimal spanning tree of the network in Figure 5.11. 3. (5.2 # 18 (not assigned)) Recall that a coloring of a graph is an assignment of colors to the vertices such that no two vertices of the same color are connected by an edge. The following algorithm attempts to produce a coloring for the vertices of a graph G using the fewest number of colors possible. C <- while (G has vertices left to color) do Pick a new color x not in C. C <- C union {x} Assign color x to as many vertices of G as possible, such that no edge connects vertices of the same color. (a) Which type of algorithm is this? ANS: This algorithms attempts to accomplish the long-term goal of coloring the graph by doing the most obvious short-term task at every opportunity. As such, it s a greedy algorithm. (b) Find a graph for which this algorithm fails to produce a coloring with the fewest possible number of colors. ANS: The first vertices colored would be the three labeled A, then the algorithm fails to three-color (see numbers) the following graph:

Math 15 Homework 2.4 Solutions - Page 3 of 5 1/30/17 4. (5.2 # 20) Let T be a binary tree whose nodes are elements of some set U. The following algorithm searches T for a target value t U and returns true if and only if t is a node in T. function Search (t U, T { binary trees}) if T is empty then return false if t = the root of T then return true return (Search(t, left subtree of T ) Search (t, right subtree of T )) (a) Write a top-down evaluation of Search (17, T ), where T is the tree in below. Search(17, T ) = Search(17, T 27 ) Search(17, T 31 ) = Search(17, ) Search(17, T 12 ) Search(17, T 17 ) Search(17, T 14 ) = false Search(17, T 92 ) Search(17, T 57 ) true Search(17, T 23 ) Search(17, T 88 ) = false Search(17, ) Search(17, ) Search(17, ) Search(17, ) true Search(17, ) Search(17, ) Search(17, Search(17, ) = false false false false false true false false false false = true (b) Which type of algorithm is this? Explain. This is an inorder traversal algorithm. You can implement this in Python as follows. First, create your tree (the simple way, with a list of lists.) T = [41, #r o o t [21,[], # l e f t s u b t r e e [12, [92,[],[]], [57,[],[]]]], [31, [17, [71,[],[]], [65,[],[]]], [14, [23,[],[]], [88,[],[]]]]] Here, then is the inorder traversal: def InSearch(t,T): print( )

Math 15 Homework 2.4 Solutions - Page 4 of 5 1/30/17 elif InSearch(t,T[1]) or t==t[0]: : print( InSearch(,t,T[2], ) ) return InSearch(t,T[2]) print( InSearch(17,T)) The print statement produces this output: InSearch( 17 [12, [92, [], []], [57, [], []]] ) InSearch( 17 [57, [], []] ) InSearch( 17 [31, [17, [71, [], []], [65, [], []]], [14, [23, [], []], [88, [], []]]] ) True By contrast, searching for 88 produces this output: InSearch( 88 [12, [92, [], []], [57, [], []]] ) InSearch( 88 [57, [], []] ) InSearch( 88 [31, [17, [71, [], []], [65, [], []]], [14, [23, [], []], [88, [], []]]] ) InSearch( 88 [65, [], []] ) InSearch( 88 [14, [23, [], []], [88, [], []]] ) InSearch( 88 [88, [], []] ) True Contrast this with the Python code for PreOrder and PostOrder traversals:

Math 15 Homework 2.4 Solutions - Page 5 of 5 1/30/17 def PreSearch(t,T): elif t==t[0]: : return PreSearch(t,T[1]) or PreSearch(t,T[2]) def PostSearch(t,T): elif PostSearch(t,T[1]) or PostSearch(t,T[2]): : return t==t[0] 5. (5.2 # 24) Write a divide-and-conquer algorithm that computes the sum of all elements of a finite set K = {k 1, k 2,..., k n } of integers. ANS: The pseudocode would look something like this: function DCSum(L) if L = {x} then return x X1 <- {x_1,x_2,...,x_floor(n/2)} X2 <- {x_floor(n/2)+1,...,x_n} return DCSum(X1) + DCSum(X2) In Python, we d have something like this: ## L i s t c o m p r e h e n s i o n f o r X = [ 0, 1, 2,..., 1 0 0 ] X = [i for i in range (101)] def DCSum(X): if len(x) == 1: return X[0] : X1 = X[:len(X)//2] X2 = X[len(X)//2:] return DCSum(X1)+ DCSum(X2) # T e s t print(dcsum(x)) Which returns, as expected, 5050