EE 368. Week 6 (Notes)

Similar documents
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

TREES. Trees - Introduction

Data Structures Question Bank Multiple Choice

Binary Trees, Binary Search Trees

Choice of C++ as Language


INF2220: algorithms and data structures Series 1

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

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

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

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

Formal Languages and Automata Theory, SS Project (due Week 14)

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

CSE 373 Spring Midterm. Friday April 21st

ASYMPTOTIC COMPLEXITY

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

March 20/2003 Jayakanth Srinivasan,

Chapter 20: Binary Trees

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

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

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

1. Attempt any three of the following: 15

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

ASYMPTOTIC COMPLEXITY

Draw a diagram of an empty circular queue and describe it to the reader.

Complexity, General. Standard approach: count the number of primitive operations executed.

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

CS301 - Data Structures Glossary By

Algorithm Analysis. CENG 707 Data Structures and Algorithms

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Introduction to the Analysis of Algorithms. Algorithm

Algorithmic Complexity

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

CE 221 Data Structures and Algorithms

Analysis of Algorithms

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

Day 10. COMP1006/1406 Summer M. Jason Hinek Carleton University

Table of Contents. Chapter 1: Introduction to Data Structures... 1

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

Binary Trees. Height 1

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY THIRD SEMESTER B.TECH DEGREE EXAMINATION, JULY 2017 CS205: DATA STRUCTURES (CS, IT)

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

Stacks, Queues and Hierarchical Collections

Binary Trees

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

CSE 100 Advanced Data Structures

syntax tree - * * * * * *

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

ASTS, GRAMMARS, PARSING, TREE TRAVERSALS. Lecture 14 CS2110 Fall 2018

What is an algorithm?

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

Part 1: Multiple Choice Circle only ONE answer. Each multiple choice question is worth 3.5 marks.

12 Abstract Data Types

Introduction to Data Structure

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

ASSIGNMENTS. Progra m Outcom e. Chapter Q. No. Outcom e (CO) I 1 If f(n) = Θ(g(n)) and g(n)= Θ(h(n)), then proof that h(n) = Θ(f(n))

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

Associate Professor Dr. Raed Ibraheem Hamed

Data Structure and Algorithm, Spring 2013 Midterm Examination 120 points Time: 2:20pm-5:20pm (180 minutes), Tuesday, April 16, 2013

Recitation 9. Prelim Review

[ DATA STRUCTURES ] Fig. (1) : A Tree

Course Name: B.Tech. 3 th Sem. No of hours allotted to complete the syllabi: 44 Hours No of hours allotted per week: 3 Hours. Planned.

Lecture 5: Running Time Evaluation

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Algorithm Analysis and Design

ECE250: Algorithms and Data Structures Midterm Review

Using a Heap to Implement a Priority Queue

Scientific Computing. Algorithm Analysis

INF2220: algorithms and data structures Series 1

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

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

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

Algorithmic Analysis. Go go Big O(h)!

3 Trees: traversal and analysis of standard search trees

Lecture 26. Introduction to Trees. Trees

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

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

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

CSE 421: Introduction to Algorithms Complexity

Advanced Tree Data Structures

Summer Final Exam Review Session August 5, 2009

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2010

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

Analysis of Algorithm. Chapter 2

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

CSCI2100B Data Structures Trees

Algorithm. Algorithm Analysis. Algorithm. Algorithm. Analyzing Sorting Algorithms (Insertion Sort) Analyzing Algorithms 8/31/2017

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees


Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

INSTITUTE OF AERONAUTICAL ENGINEERING

Transcription:

EE 368 Week 6 (Notes) 1

Expression Trees Binary trees provide an efficient data structure for representing expressions with binary operators. Root contains the operator Left and right children contain the operands - which may each be expression trees Traversals? Generation? 2

Tree Traversal Operation (1) Preorder traversal (dept-first search) (2) Inorder traversal (symmetric order) (3) Postorder traversal 3

Tree Traversal Operation (1) Preorder traversal (dept-first search) (1) Visit the root (2) Traverse the left subtree in preorder (3) Traverse the right subtree in preorder (NLR traversal) 4

Example of Tree Traversal Operation 5

(2) Inorder Traversal (Symmetric Order) (1) Traverse the left subtree in inorder (LNR) (2) Visit the root (3) Traverse the right subtree in inorder --> infix expression (without parentheses) insert parentheses 6

(3) Postorder Traversal (LRN) (1) Traverse left subtree (2) Traverse right subtree (3) Visit root - postfix expression 7

Creation of Expression Trees (Using Postfix Expression) Algorithm uses a stack. It uses the same mechanism that is used for the evaluation of a postfix expression. Let P(n) represent a postfix expression, with n tokens, labeled from 1 to n. Let H(i) represent the stack height after the processing of i-th token. Let LCHILD(i), RCHILD(i) be the left and the right child of a node representing the i-th token. 8

Algorithm for Building Expression Trees (Using Postfix Expressions) 1. If i-th token is an operand, Stack it. It does not have any child. LCHILD (i) = RCHILD (i) = 0 2. If i-th token is a unary operator, then its right child is popped from the stack. RCHILD (i) = i - 1 No left child. LCHILD(I) = 0 Push the i-th token (operator) on the stack. 3. If i-th token is a binary operator, then its children are popped from the stack. RCHILD (i) = i - 1 LCHILD (i) = largest j, (j < i ) such that H(j) = H(i) Push the i-th token (operator) on the stack. 9

Example 10

Complexity Analysis and Sorting Read Chapter 2 Time complexity A Priori Estimate A Posteriori Testing (Measurement) Space Complexity 11

Time Complexity Definition: Amount of Work Done by a Program Two Possible ways: 1. Count the number of arithmetic and logical operations performed in a program. Obtain a weighted sum of these operations (weights being proportional to the time a computer takes to perform the operations.) Limitations: Practically not desirable. t add, t sub t nul, t div,.... T C = t add C add + t sub C sub + t nul C nul +.... Analysis is limited to a specific machine. 12

Time Complexity Definition: Amount of Work Done by a Program Two Possible ways: 1. Count the number of arithmetic and logical operations performed in a program. Obtain a weighted sum of these operations (weights being proportional to the time a computer takes to perform the operations.) T C = + t sub C sub + t nul C nul + t div C div +.... Limitations: Practically not desirable. Analysis is limited to a specific machine. t add C add t s add, Cs add + td add + Cd add.... 13

Time Complexity 2. Count the number of program steps A program step is a meaningful segment of a program that has an execution time associated with it. It does not depend on the run time. For a given language number of program steps can be determined for each statement. 14

Time Complexity A further abstraction is needed, since we do not want complexity to be dependent upon the language and style of programming. We need to isolate operation fundamental to the problem under study (ignore the bookkeeping). The amount of time is then proportional to fundamental operations. What if the total number of operations performed are substantially higher than the fundamental operations? Define classes of algorithms!! 15

Problem Some Examples of Reasonable Choices of Basic Operations Operation 1. Find X in a list of names. Comparison of X with an entry in the list 2. Multiply two matrices with Multiplication of two real numbers real entries. (or multiplication and addition of real numbers.) 3. Sort a list of numbers. Comparison of two list entries. 4. Traverse a binary tree Traversing a link. (Here, setting a represented as a linked pointer would be considered a basic structure where each node operation rather than bookkeeping.) contains pointers to its left and right children.) 16

How to Present Analytical Results Concisely Algorithm Performance Depends on: - size of the input data - characteristics of the input data Example: How performance results should be expressed? 17

How to Present Analytical Results Concisely - Average Case Analysis - Worst-Case Analysis How about the best (optimal in the worst sense) algorithm? 18

Average Case Analysis Compute the number of operations performed for each input of size n and then take the average. In practice, some inputs occur more frequently than others. Let D n be the set of inputs of size n, for the problem under consideration. Let p(i) be the probability that input I occurs and let t(i) be the number of basic operations performed by the algorithm on input I. 19

Average Case Analysis (cont.) Average Complexity: A(n) = I?? D n p(i). t(i) We need a careful evaluation of t(i) and need to know function p(i), through experience. 20

Average Case Analysis Compute the number of operations performed for each input of size n and then take the average. In practice, some inputs occur more frequently than others. Insert Drawings 21

Worst-Case Analysis The maximum number of operations performed on any input of size n. Provides an upper bound. Worst-Case Complexity: W(n) = max I?? D n t (I) Easy to compute!! 22

Example: Sequential Search Assume k is an array of n records. We want to find the first i such that k[i] = KEY. If search is successful, return i else -1. for (i = 0; I < n; i++) if (KEY = = k(i)) return (-1); D n = insert pictures 23

Worst-Case Analysis of the Search Example Note: Input for which an algorithm behaves worst depends on the particular algorithm, not on the problem. If an algorithm searches the list from the bottom up, then the worst case would occur if search key is only in the first position (or is not present at all). 24

Some Merits of Selected Measure of Complexity Notion of fundamental operation is acceptable if the total number of operations performed is roughly proportional to the number of fundamental operations. If an algorithm does t(i), operations, the total number of operations (or total execution time) will be ct(i). c is a constant and depends on the computer and algorithm. Similarly, we can describe ca(n) and cw(n). 25

Some Merits of Selected Measure of Complexity (cont.) c may need to be determined to find the execution time. Various algorithm with great difference in performance exist, so that c becomes unimportant. 26

Complexity Terminology We need a language to describe time complexity of algorithms Big O notation. We can compare the growth function of an algorithm with the grown function of another directly. Example: # of program steps Algorithm 1:.01 n 2 + f(n) Algorithm 2: 10 n 2 h(n) 27

Complexity Terminology (cont.) For small n, Algorithm 1 runs more slowly then Algorithm 2, but as n gets larger Algorithm 2 becomes the slower. Asymptotic Value Behavior 28

Complexity Terminology Interested mostly with the asymptotic bound of a growth function, or the order of a growth function, g(n); denoted O(g(n)). Definition: Given the functions f(n) and g(n) f(n) is on the order of g(n) If there exists positive integers a, b such that f(n) < a. g(n) for all n > b f(n) is 0(g(n)) 29

Example: f(n) =.01 n 2 + 100 n g(n) = n 2 Is f(n) 0(g(n)) 0(n 2 )? f(n) < a g(n) for all n > b.01 n 2 + 100n < a n 2? a = 1 a? b? n > 102 = b 30

Complexity Terminology if f(n) = n 2 + 100n ls f(n) 0(n 2 ) n 2 + 100n < an 2 for all n > b a = 2 b = 100 n 2 + 100n < 2n 2 n > 100 Also f(n) is 0(h(n)) where h(n) = n 3 because: n 2 + 100n < an 3 for all n > b a = 1 b = 11 f(n) is 0(n 3 ) Also f(n) is 0(t(n)) where t(n) = 2 n etc. 31

Complexity Terminology If f(n) is 0(g(n)) then eventually f(n) < g(n); f(n) is bounded by g(n) (asymptotically upper bounded). Transitivity Property: If f(n) is 0(g(n)) and g(n) is 0(h(n)) then f(n) is 0(h(n)) 32

Efficiency Terminology (Summary) 0(1) means a constant computing time. 0(log n) is called logarithmic 0( n) 0(n) is called linear. 0(n log n) 0(n n) 0(n 2 ) is quadratic. 0(n 3 ) is cubic. 0(2 n ) is exponential. 0(n n ) 33

34