CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms.

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 26 Binary Search Trees.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
BST Data Structure A BST node contains: A BST contains
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 17: Binary Search Trees; Heaps.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Lecture 06: Tree Structures Topics: Trees in general Binary Search Trees Application: Huffman Coding Other types of Trees.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
1 Chapter 25 Trees Iterators Heaps Priority Queues.
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan.
Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
CS 615: Design & Analysis of Algorithms Chapter 5: Searching Brassard & Bratley Chap.: Chapter 9 Page &
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Heapsort. Heapsort is a comparison-based sorting algorithm, and is part of the selection sort family. Although somewhat slower in practice on most machines.
CSC 211 Data Structures Lecture 13
Starting at Binary Trees
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
David Stotts Computer Science Department UNC Chapel Hill.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Binary Search Trees (BST)
Binary Search Trees … From
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Chapter 4, Part II Sorting Algorithms. 2 Heap Details A heap is a tree structure where for each subtree the value stored at the root is larger than all.
CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms.
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
(c) University of Washington20-1 CSC 143 Java Trees.
Recursive Objects (Part 4)
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Section 8.1 Trees.
Week 11 - Friday CS221.
Design and Analysis of Algorithms
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Lecture 12 CS203 1.
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Presentation transcript:

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms Khawaja Mohiuddin Assistant Professor, Department of Computer Sciences Bahria University, Karachi Campus, Contact: Lecture # 7 – Searching

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Searching 2 Topics To Cover  Linear Search  Binary Search  Interpolation Search  Traversal  Types of Traversal  In-order Traversal  pre-order Traversal  post-order Traversal  Breadth first search (BFS)  Depth first search (DFS)

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Searching 3  Once we have a sorted list of items after applying the sorting algorithms, we can search for specific items relatively quickly in it.  Even though some programming libraries include searching tools, it is still important to understand the searching algorithms because sometimes you can do even better than the tools.  Following are some of the searching algorithms:  Linear Search  Also called “exhaustive search”  Simply loops through the items in the array, looking for the target item  Also works on unsorted lists  But can save time if the items are sorted. If the target value isn’t in the list, the algorithm can stop early if a greater value item is reached

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Searching 4  Linear Search (contd.) // Find the target item's index in the sorted array. // If the item isn't in the array, return -1. Integer: LinearSearch(Data values[], Data target) For i = 0 To - 1 // See if this is the target. If (values[i] == target) Then Return i // See if we have passed where the target would be. If (values[i] > target) Then Return -1 Next i // If we get here, the target is not in the array. Return -1 End LinearSearch

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Searching 5  Linear Search (contd.)  May need to loop through the entire array to conclude that an item isn’t there  So its worst-case behavior is O(N)  Even in the average case, the algorithm’s run time is O(N)  add up the number of steps required to search for every item in the array, you get N = N * (N + 1) / 2  Divide that total by N to get the average search time for all the N items, you get (N + 1) / 2, which is still O(N)  This algorithm is much slower than binary search or interpolation search  But, it has the advantage that it works on linked lists and unsorted lists

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Searching 6  Binary Search  The algorithm keeps track of the largest and smallest indices  Initially those bounds (min and max) are 0 and the largest index in the array  The algorithm then calculates the index halfway between min and max (mid)  If target is less than value at mid, reset max to search the left half of the array  If target is greater than value at mid, reset min to search the right half of the array  If target equals value at mid, return the index at mid.

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Searching 7  Binary Search (contd.) // Find the target item's index in the sorted array. // If the item isn't in the array, return -1. Integer: BinarySearch(Data values[], Data target) Integer: min = 0 Integer: max = - 1 While (min <= max) // Find the dividing item. Integer: mid = (min + max) / 2 // See if we need to search the left or right half. If (target < values[mid]) Then max = mid - 1 Else If (target > values[mid]) Then min = mid + 1 Else Return mid End While // If we get here, the target is not in the array. Return -1 End BinarySearch

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Searching 8  Binary Search (contd.)  At each step, the algorithm divides in half the number of items that might contain the target  If the array contains N items, after O(log N) steps, the section of the array that might hold the target contains only one item  So the algorithm either finds the item or concludes that it isn’t in the array  That means the algorithm has O(log N) run time

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Searching 9  Interpolation Search  Uses the value of the target item to guess where in the array it might lie  Achieves much faster search times  For example, if array contains 50 items with values between 1 and 100 and if the target value is 30, it lies about 30 percent of the way from the smallest to the largest value, so the item may be somewhere near index 15.  Depending on the distribution of the numbers in the array, this may not be exactly correct  But it should get you fairly close to the target item’s position.

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Searching 10  Interpolation Search (contd.) Integer: InterpolationSearch(Data values[], Data target) Integer: min = 0 Integer: max = values.Length - 1 While (min <= max) // Find the dividing item. Integer: mid = min + (max - min) * (target - values[min]) / (values[max] - values[min]) If (values[mid] == target) Then Return mid End While Return -1 End InterpolationSearch

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Searching 11  Interpolation Search (contd.)  Couple of problems:  The mid calculation can result in an overflow  A value of mid that is not between min and max  Can solve those problems by putting few conditional statements  The trickiest part of this algorithm is the statement that calculates mid  For example, if values[min] is 100, values[max] is 200, and target is 125, then: (target - values[min]) / (values[max] - values[min]) = ( ) / ( ) = 25 / 100 = 0.25  In the worst case, if the data is extremely unevenly distributed, and you’re looking for the worst possible target value, this algorithm has O(N) performance  If the distribution is reasonably uniform, the expected performance is O(log(log N)). (proving that is outside the scope of this course)

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Traversal and Its Types 12  The search that requires visiting every node in a structure is called Traversal.  The purpose of visiting a node could be to perform any operation.  Many algorithms search a tree structure for a particular node.  Binary trees have following types of traversals: Depth-first Traversal and Breadth-first Traversal.  Depth-first Traversal (3 sub-types): 1. Pre-order Traversal 2. In-order Traversal 3. Post-order Traversal  Breadth-first Traversal

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Traversal and Its Types 13  Pre-order Traversal  In a preorder traversal, the algorithm processes a node, and then its left child, and then its right child.  The full traversal order is D, B, A, C, E  i.e.: Visit D, Output D, Visit B, Output B, Visit A, Output A, Visit B, Visit C, Output C, Visit B, Visit D, Visit E, Output E, Visit D  The following pseudo-code shows a natural recursive implementation of this algorithm: TraversePreorder(BinaryNode: node) If (node.LeftChild != null) Then TraversePreorder(node.LeftChild) If (node.RightChild != null) Then TraversePreorder(node.RightChild) End TraversePreorder

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Traversal and Its Types 14  In-order Traversal  In an in-order or symmetric traversal, the algorithm processes a node’s left child, the node, and then the node’s right child.  The full traversal order is A, B, C, D, E.  i.e.: Visit D, Visit B, Visit A, Output A, Visit B, Output B, Visit C, Output C, Visit B, Visit D, Output D, Visit E, Output E, Visit D TraverseInorder(BinaryNode: node) If (node.LeftChild != null) Then TraverseInorder(node.LeftChild) If (node.RightChild != null) Then TraverseInorder(node.RightChild) End TraverseInorder

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Traversal and Its Types 15  Post-order Traversal  In a post-order traversal, the algorithm processes a node’s left child, and then its right child, and then the node.  The full traversal is A, C, B, E, D.  i.e.: Visit D, Visit B, Visit A, Output A, Visit B, Visit C, Output C, Visit B, Output B, Visit D, Visit E, Output E, Visit D, Output D TraversePostorder(BinaryNode: node) If (node.LeftChild != null) Then TraversePostorder(node.LeftChild) If (node.RightChild != null) Then TraversePostorder(node.RightChild) End TraversePostorder

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Traversal and Its Types 16  Breadth-first Traversal  In a breadth-first traversal, the algorithm processes all the nodes at a given level of the tree in left-to-right order before processing the nodes at the next level.  The full traversal is D, B, E, A, C.  This algorithm does not naturally follow the structure of the tree as the previous traversal algorithms do.  The tree shown in Figure 10-5 has no child link from node E to node A, so it’s not obvious how the algorithm moves from node E to node A.  One solution is to add a node’s children to a queue and then process the queue later, after you’ve finished processing the parents’ level.

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Traversal and Its Types 17  Breadth-first Traversal (contd.) TraverseBreadthFirst(BinaryNode: root) // Create a queue to hold children for later processing. Queue : children = New Queue () // Place the root node on the queue. children.Enqueue(root) // Process the queue until it is empty. While (children Is Not Empty) // Get the next node in the queue. BinaryNode: node = children.Dequeue() // Process the node. // Add the node's children to the queue. If (node.LeftChild != null) children.Enqueue(node.LeftChild) If (node.RightChild != null) children.Enqueue(node.RightChild) End While End TraverseDepthFirst

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Traversal and Its Types 18  Traversal Run Times  The recursive algorithms for pre-order, in-order and post-order traversal travel down the tree to the leaf nodes.  Then, as the recursive calls unwind, they travel back up to the root.  After an algorithm visits a node and then returns to the node’s parent, the algorithm doesn’t visit that node again.  That means the algorithms visit each node once. So if a tree contains N nodes, they all have O(N) run time.  The breadth-first traversal algorithm processes nodes as they move through a queue.  Each node enters the queue once, so if the tree has N nodes, the algorithm takes O(N) time.  In the worst case, if the tree is a perfect binary tree, its bottom level holds roughly half the total number of nodes, so if the tree holds N nodes, the queue holds O(N ÷ 2) = O(N) nodes.