INF2220: algorithms and data structures Series 1

Similar documents
Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

Advanced Implementations of Tables: Balanced Search Trees and Hashing

Search Trees. Chapter 10. CSE 2011 Prof. J. Elder Last Updated: :52 AM

Data Structures and Algorithms Winter Semester

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

Binary Search Trees. Motivation

AVL Trees. Manolis Koubarakis. Data Structures and Programming Techniques

Fundamental Algorithms

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

Data Structures and Algorithms " Search Trees!!

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes.

Search Trees. EECS 2011 Prof. J. Elder Last Updated: 24 March 2015

Binary Search Trees. Lecture 29 Section Robb T. Koether. Hampden-Sydney College. Fri, Apr 8, 2016

Splay Trees. CMSC 420: Lecture 8

Topic 17. Analysis of Algorithms

HW #4. (mostly by) Salim Sarımurat. 1) Insert 6 2) Insert 8 3) Insert 30. 4) Insert S a.

Discrete Mathematics

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

Functional Data Structures

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2

Priority queues implemented via heaps

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation:

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

Problem. Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26

searching algorithms

Solutions. Prelim 2[Solutions]

Heaps and Priority Queues

Lecture 6 September 21, 2016

CS213d Data Structures and Algorithms

Prelim 2[Solutions] Solutions. 1. Short Answer [18 pts]

Tutorial Session 5. Discussion of Exercise 4, Preview on Exercise 5, Preparation for Midterm 1. Running Time Analysis, Asymptotic Complexity

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Algorithms and Their Complexity

Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.

2-INF-237 Vybrané partie z dátových štruktúr 2-INF-237 Selected Topics in Data Structures

CS Data Structures and Algorithm Analysis

What is Performance Analysis?

Written Homework #1: Analysis of Algorithms

CSE 417: Algorithms and Computational Complexity

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

CS361 Homework #3 Solutions

CMSC 132, Object-Oriented Programming II Summer Lecture 12

Binary Search Trees. Dictionary Operations: Additional operations: find(key) insert(key, value) erase(key)

ENS Lyon Camp. Day 2. Basic group. Cartesian Tree. 26 October

CS 310 Advanced Data Structures and Algorithms

Lecture 3: Big-O and Big-Θ

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2019

Algorithm. Executing the Max algorithm. Algorithm and Growth of Functions Benchaporn Jantarakongkul. (algorithm) ก ก. : ก {a i }=a 1,,a n a i N,

Measuring Goodness of an Algorithm. Asymptotic Analysis of Algorithms. Measuring Efficiency of an Algorithm. Algorithm and Data Structure

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

Data Structures and and Algorithm Xiaoqing Zheng

Time Analysis of Sorting and Searching Algorithms

Dictionary: an abstract data type

Problem Set 1. CSE 373 Spring Out: February 9, 2016

CSCI 3110 Assignment 6 Solutions

past balancing schemes require maintenance of balance info at all times, are aggresive use work of searches to pay for work of rebalancing

Premaster Course Algorithms 1 Chapter 3: Elementary Data Structures

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

Chapter 6. Self-Adjusting Data Structures

CS 61B Asymptotic Analysis Fall 2017

Dictionary: an abstract data type

arxiv: v1 [math.co] 22 Jan 2013

Assignment 5: Solutions

CS 240 Data Structures and Data Management. Module 4: Dictionaries

CS173 Running Time and Big-O. Tandy Warnow

Divide-and-Conquer Algorithms Part Two

Mon Tue Wed Thurs Fri

Computational Complexity

data structures and algorithms lecture 2

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

Introduction. An Introduction to Algorithms and Data Structures

Reminder of Asymptotic Notation. Inf 2B: Asymptotic notation and Algorithms. Asymptotic notation for Running-time

Problem-Solving via Search Lecture 3

CS 151. Red Black Trees & Structural Induction. Thursday, November 1, 12

More Asymptotic Analysis Spring 2018 Discussion 8: March 6, 2018

ASYMPTOTIC COMPLEXITY SEARCHING/SORTING

Each internal node v with d(v) children stores d 1 keys. k i 1 < key in i-th sub-tree k i, where we use k 0 = and k d =.

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

csci 210: Data Structures Program Analysis

Sorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort

25. Minimum Spanning Trees

25. Minimum Spanning Trees

Complexity Theory Part I

Fall 2016 Test 1 with Solutions

Outline Resource Introduction Usage Testing Exercises. Linked Lists COMP SCI / SFWR ENG 2S03. Department of Computing and Software McMaster University

The null-pointers in a binary search tree are replaced by pointers to special null-vertices, that do not carry any object-data

Lecture 5: Loop Invariants and Insertion-sort

Optimal Tree-decomposition Balancing and Reachability on Low Treewidth Graphs

Slides for CIS 675. Huffman Encoding, 1. Huffman Encoding, 2. Huffman Encoding, 3. Encoding 1. DPV Chapter 5, Part 2. Encoding 2

Midterm 1 for CS 170

Module 1: Analyzing the Efficiency of Algorithms

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

Growth of Functions. As an example for an estimate of computation time, let us consider the sequential search algorithm.

CS 4104 Data and Algorithm Analysis. Recurrence Relations. Modeling Recursive Function Cost. Solving Recurrences. Clifford A. Shaffer.

CSCE 222 Discrete Structures for Computing. Review for Exam 2. Dr. Hyunyoung Lee !!!

1. [10 marks] Consider the following two algorithms that find the two largest elements in an array A[1..n], where n >= 2.

2.2 Asymptotic Order of Growth. definitions and notation (2.2) examples (2.4) properties (2.2)

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

Transcription:

Universitetet i Oslo Institutt for Informatikk I. Yu, D. Karabeg INF2220: algorithms and data structures Series 1 Topic Function growth & estimation of running time, trees (Exercises with hints for solution) Issued: 24. 08. 2016 Exercise 1 (Growth of functions) Order the following functions by growth rate: N, N, N 1.5, N 2, N log N, N log log N, N log 2 N, N log(n 2 ), 2/N, 2 N, 2 N/2, 37, N 2 log N, N 3. Indicate which functions grow at the same rate. Solution: [of 1]The first one is of course a anomaly, since it grows negatively. Hardly any algorithm will ever get faster on larger input. 2/N, 37, N, N, N log log(n), N log N, N log N 2, N log 2 N, N 1.5, N 2, N 2 log N, N 3, 2 N/2, 2 n Exercise 2 (O-notation) The statements below show some features of big-o notation for the functions f f(n) and g g(n). Determine whether each of the following statements is true or false and correct the formula in the latter case. 1. O(f g) = O(f) O(g) 2. O(f) < O(g) for f(n) log n C 1, g(n) log n C 2 some constants C 1, C 2 where C 1 < C 2 Solution: 1. TRUE 2. FALSE, O(f) = O(g) = O(log N) Exercise 3 (Analysis of running time) Estimate the running time of the following program fragments (a big-o analysis). In the fragments, the variable n, an integer, is the input. f o r ( i = 0 ; i < n ; i += 2) f o r ( j = 0 ; j < n ; j++)

f o r ( i n t i = 0 i < n ; i ++) f o r ( i n t j = 0 j < n ; j++) f o r ( j = 0 ; j < n n ; j++) f o r ( j = 0 ; j < i ; j++) f o r ( j = 0 ; j < n n ; j++) f o r ( k = 0 ; k < j ; k++) f o r ( i = 1 ; i < n ; i = i 2) Solution: [Analysis of running time] O(N) O(N) O(N 2 ) O(N) O(N 3 ) O(N 2 ) O(N 5 ) O(log N) Note that of course the analysis of given programs (in this exercise more of simple program fragments/loops) is a (much) simpler problem than runtime complexity analysis of a problem. When analysing a problem, one is typically interested in finding an estimation (for instance a worst-case estimation) of the best of all possible algorithms! The same remark of course applies to the next exercise. Exercise 4 (Analysis of running time) 1. The following program snippet sorts an integer array int[] A = new int[n]. What s the order of running time for that in big-o notation. f o r ( i n t i = 0 ; i < n ; i ++) { minj = i ; f o r ( j = i + 1 ; j < n ; j++) { i f (A[ j ] < A[ minj ] ) { minj = j ; bytt ( i, minj ) ; 2. What s the order of running time for that in big-o notation for the following fragment. Pay attention especially for the conditional inside the second nested loop. f o r ( i n t i = 1 ; i <= n ; i ++) { f o r ( i n t j =1; j <= i i ; j++) { i f ( j % i == 0) { f o r ( i n t k = 0 ; k < j ; k++ ) 2

3. For a given n > 0, which value will be found in the variable L2 after executing the following program snippet. The answer should be given as a function of n. Of which order is the running time of this program in big-o notation? i = 1 ; L2 = 1; w h i l e ( i <= n ) { i = i 2 ; L2++; Solution: 1. O(N 2 ) 2. O(N 4 ) 3. O(log 2 N) Exercise 5 Big-O Fibonacci The following program will calculates the n th Fibonacci number. running time in big-o notation: Determine the i n t f i b ( i n t n ) { i f ( n<=1) { r e t u r n 1 ; r e t u r n f i b ( n 1) + f i b ( n 2); Solution: O(2 n ) Exercise 6 (Terminology of trees and tree traversal) For the given tree, determine what is the root? which are the leaves what s the tree s height Give the result of preorder, postorder, inorder, and level-order traversal. For all the nodes of the tree name the parent list the children list the siblings compute the height, depth, and size. 3

Nodes Parent Children Siblings Height Depth Size a - b, c - 4 0 13 b a d c 3 1 5 c a e, f b 3 1 7 d b g, h e, f 2 2 4 e c i d, f 1 2 2 f c j d, e 2 2 4 g d k h, i, j 1 3 2 h d - g, i, j 0 3 1 i e - g, h, j 0 3 1 j f l, m g, h, i 1 3 3 k g - l, m 0 4 1 l j - k, m 0 4 1 m j - k, l 0 4 1 a / \ / \ / \ b c \ / \ d e f / \ / / g h i j \ / \ k l m Solution: [Terminology of trees and tree traversal] root: a leaves: k, h, i, l, m tree s depth: 4 preorder: a, b, d, g, k, h, c, e, i, f, j, l, m postorder: k, g, h, d, b, i, e, l, m, j, f, c, a inorder: b, g, k, d, h, a, i, e, c, l, j, m, f Exercise 7 (Binary search tree - insertion and deletion) Show the result of inserting 6, 4, 8, 5, 1, 9, 7, 11, 2 into an initially empty binary search tree. Show the result of first deleting 1 (from the previously constructed tree), and then 6. Solution: [Binary search tree - insertion and deletion] 4

6 7 / \ / \ / \ / \ 4 8 4 8 / \ / \ / \ \ 1 5 7 9 2 5 9 \ \ \ 2 11 11 Exercise 8 (Non-unique search keys) We use a binary search tree to store a number of elements containing an integer value (of type int) together with a number of other data. We assume that each node has a pointer to its left, resp. right child, as usual. Different from most examples in the lecture, we allow here that different elements can have the same value the other data can be different and they are supposed to be stored in different nodes. 1. A possible solution does the following when inserting a value: goes further down the tree, if hitting an object carrying the same value. We can always chooses to go down further in the right subtree if we encounter an object with the same value. Write a insert-method that implements this idea and sketch some typical trees that result in that implementation. 2. In which order will we get out object sharing the same value if we print trees that result from above under 1, when we follow an infix traversal? What happens if we follow down the left children instead the right ones, as in 1? 3. In this part of the exercise, the nodes with the same value should be put into a list starting at the first node with that value. This list, however, requires additional pointers, but we can do it as follows: If we insert an object and there is alread exactly one with this value in the tree, so link we that element into the between the old node and its right-hand subtree. If later on more object with the same value should be inserted, they well be linked into a list from that second object where the left-child-pointer is used as list-pointer. Sketch some examples, and write an insert method based in that idea. 4. when printing out trees constructed as described under 3 in infix order, nodes with the same values still end up in one batch. But in which order are they actually processed? Write a modified print-method which prints nodes with the same values in the order they had been filled in originally. Exercise 9 (Frequency Tree) We want to use a binary search tree to analyze the play Vildanden from Henrik Ibsen. First read all the words which are separated by, e.g. ;,?, etc, from the file and insert them into an initially binary search tree. All the words which are different from upper case and lower case are considered to be the same. Each node in the tree is corresponding to a unique word in the file. Each node should remember the frequency of the corresponding word appear in the play. Create a separate frequency tree which is sorted on the frequency of each word. 5

Write a sorted list of the N most frequently used words (e.g. N = 20). Write a list of all words having frequency between X and Y (which may be equal). Calculate the depth of the left and right subtrees for both the binary search tree and the frequency tree. Are the left and right subtrees balanced? If not, propose a way to make the tree more balanced, and implement and test the proposal. 6