Decrease-and-Conquer. Dr. Yingwu Zhu

Similar documents
Exam study sheet for CS2711. List of topics

Binary Search Trees CMPSC 122

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Ordered Lists and Binary Trees

Data Structures and Algorithms Written Examination

Sample Questions Csci 1112 A. Bellaachia

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Converting a Number from Decimal to Binary

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Data Structures and Algorithms

Algorithms and Data Structures

CompSci-61B, Data Structures Final Exam

Data Structure [Question Bank]

Binary Search Trees (BST)

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Binary Heap Algorithms

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

International Journal of Software and Web Sciences (IJSWS)

Data Structures. Level 6 C Module Descriptor

Social Media Mining. Graph Essentials

DATA STRUCTURES USING C

TREE BASIC TERMINOLOGIES

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

IE 680 Special Topics in Production Systems: Networks, Routing and Logistics*

Any two nodes which are connected by an edge in a graph are called adjacent node.

Binary Heaps. CSE 373 Data Structures

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Data Structures. Chapter 8

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Algorithms Chapter 12 Binary Search Trees

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

From Last Time: Remove (Delete) Operation

Euclidean Minimum Spanning Trees Based on Well Separated Pair Decompositions Chaojun Li. Advised by: Dave Mount. May 22, 2014

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Algorithms and data structures

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Rotation Operation for Binary Search Trees Idea:

6. Standard Algorithms

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

PES Institute of Technology-BSC QUESTION BANK

Analysis of Algorithms I: Binary Search Trees

Data Structures in Java. Session 15 Instructor: Bert Huang

Data Structures and Data Manipulation

Cpt S 223. School of EECS, WSU

GRAPH THEORY LECTURE 4: TREES

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India Dr. R. M.

Symbol Tables. Introduction

Persistent Binary Search Trees

Binary Trees and Huffman Encoding Binary Search Trees

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

10CS35: Data Structures Using C

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

Analysis of Algorithms, I

Data Structures Fibonacci Heaps, Amortized Analysis

System Interconnect Architectures. Goals and Analysis. Network Properties and Routing. Terminology - 2. Terminology - 1

The ADT Binary Search Tree

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Data Structure with C

Simplified External memory Algorithms for Planar DAGs. July 2004

To My Parents -Laxmi and Modaiah. To My Family Members. To My Friends. To IIT Bombay. To All Hard Workers

A permutation can also be represented by describing its cycles. What do you suppose is meant by this?

Big Data and Scripting. Part 4: Memory Hierarchies

Lecture 18: Applications of Dynamic Programming Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

Load Balancing. Load Balancing 1 / 24

DATA STRUCTURES NOTES FOR THE FINAL EXAM SUMMER 2002 Michael Knopf

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Analysis of Binary Search algorithm and Selection Sort algorithm

Section IV.1: Recursive Algorithms and Recursion Trees

SCAN: A Structural Clustering Algorithm for Networks

A Fast Algorithm For Finding Hamilton Cycles

Part 2: Community Detection

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

DATA ANALYSIS II. Matrix Algorithms

Data Structures Using C++

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

CSE 326: Data Structures B-Trees and B+ Trees

Network (Tree) Topology Inference Based on Prüfer Sequence

Lecture 15 An Arithmetic Circuit Lowerbound and Flows in Graphs

Heaps & Priority Queues in the C++ STL 2-3 Trees

Small Maximal Independent Sets and Faster Exact Graph Coloring

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Dynamic Programming. Lecture Overview Introduction

CS473 - Algorithms I

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Java Software Structures

Transcription:

Decrease-and-Conquer Dr. Yingwu Zhu

Decrease-and-Conquer 1. Reduce problem instance to smaller instance of the same problem 2. Solve smaller instance 3. Extend solution of smaller instance to obtain solution to original instance

Fake-Coin Puzzle There are n identically looking coins one of which is fake. There is a balance scale but there are no weights; the scale can tell whether two sets of coins weigh the same and, if not, which of the two sets is heavier (but not by how much). Design an efficient algorithm for detecting the fake coin. Assume that the fake coin is known to be lighter than the genuine ones. How to solve this problem by applying decrease-and-conquer?

Fake-Coin Puzzle There are n identically looking coins one of which is fake. There is a balance scale but there are no weights; the scale can tell whether two sets of coins weigh the same and, if not, which of the two sets is heavier (but not by how much). Design an efficient algorithm for detecting the fake coin. Assume that the fake coin is known to be lighter than the genuine ones. Decrease by factor 2 algorithm Decrease by factor 3 algorithm

Binary search Another Example

Problems to Discuss Insertion sorting Graph traversal Generating Permutations & Subsets Selection problem BST

Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among the sorted A[0..n-2] Example: Sort 6, 4, 1, 8, 5 6 4 1 8 5 4 6 1 8 5 1 4 6 8 5 1 4 6 8 5 1 4 5 6 8

Pseudocode of Insertion Sort

A Different Look at Insertion Sort Apply decrease-and-conquer!

Graph Traversal Many problems require processing all graph vertices (and edges) in systematic fashion Data structures to represent a graph Adjacency matrix Adjacency list Graph traversal algorithms: Depth-first search (DFS) Breadth-first search (BFS)

Depth-First Search (DFS) Visits graph s vertices by always moving away from last visited vertex to unvisited one, backtracks if no adjacent unvisited vertex is available. Uses a stack a vertex is pushed onto the stack when it s reached for the first time a vertex is popped off the stack when it becomes a dead end, i.e., when there is no adjacent unvisited vertex Redraws graph in tree-like fashion (with tree edges and back edges for undirected graph)

Edge Types 1. Tree edge: parent-child 2. Forward edge: node to its descendant 3. Back edge: node to its ancestor 4. Cross edge: others

Pseudocode of DFS

Recursive DFS How? T(n) =?

Example: DFS traversal of undirected graph a b c d e f g h DFS traversal stack: DFS tree:

Notes on DFS DFS can be implemented with graphs represented as: adjacency matrices: Θ(V 2 ) adjacency lists: Θ( V + E ) Time efficiency is proportional to the size of data structure. Yields two distinct ordering of vertices: order in which vertices are first encountered (pushed onto stack) order in which vertices become dead-ends (popped off stack) Applications: checking connectivity, finding connected components checking acyclicity (back edge in DFS trees)

Breadth-first search (BFS) Visits graph vertices by moving across to all the neighbors of last visited vertex Instead of a stack, BFS uses a queue Similar to level-by-level tree traversal Redraws graph in tree-like fashion (with tree edges and cross edges for undirected graph)

Pseudocode of BFS

Example of BFS traversal of undirected graph a b c d e f g h BFS traversal queue: BFS tree:

Notes on BFS BFS has same efficiency as DFS and can be implemented with graphs represented as: adjacency matrices: Θ(V 2 ) adjacency lists: Θ( V + E ) Yields single ordering of vertices (order added/deleted from queue is the same) Applications: same as DFS, but can also find paths from a vertex to all other vertices with the smallest number of edges

Dags and Topological Sorting A dag: a directed acyclic graph, i.e. a directed graph with no (directed) cycles a b a b a dag not a dag c d c d Arise in modeling many problems that involve prerequisite constraints (construction projects, document version control) Vertices of a dag can be linearly ordered so that for every edge its starting vertex is listed before its ending vertex (topological sorting). Being a dag is also a necessary condition for topological sorting be possible.

Topological Sorting Example Order the following items in a food chain tiger human fish sheep shrimp plankton wheat

Topological Sorting How to apply decrease-and-conquer here? Discussion.

Source Removal Algorithm Source removal algorithm (decrease by one and - conquer) Repeatedly identify and remove a source (a vertex with no incoming edges) and all the edges incident to it until either no vertex is left (problem is solved) or there is no source among remaining vertices (not a dag) Example: a b c d e f g h Efficiency: same as efficiency of the DFS-based algorithm

DFS-based Algorithm DFS-based algorithm for topological sorting Perform DFS traversal, noting the order vertices are popped off the traversal stack Reverse order solves topological sorting problem Back edges encountered? NOT a dag! Example: a b c d e f g h Efficiency:

Permutations and Subsets How to apply decrease-and-conquer to: Generate permutations Generate subsets

Generating Permutations Problem: permutation of n elements: 1,,n n! permutations

Generating Permutations Problem: permutation of n elements: 1,,n n! permutations Solution 1: decrease by one and conquer 1,,n-1 permutations Insert n into n positions for each of the permutation Minimal-change: insert n into a permutation from R-2-L and then switch direction every time a new permutation of [1,..,n-1] needs to be processed. Example: 123

Generating Permutations Solution 2: Johnson-Trotter Algorithm, p179 Each element is associate an arrow The element k is mobile if its arrow points to a smaller number adjacent to it: 3 2 4 1 Alg: initialize 1 2 n while the last permutation has a mobile element do find its largest mobile element k swap k and the adjacent integer k s arrow points to reverse the direction of all the elements that are larger than k add the new permutation to the list

Generating Subsets Problem: generating all 2^n subsets of set A={a1,, an} Discussion: how?

Generating Subsets Problem: generating all 2^n subsets of set A={a1,, an} Solution 1: decrease by one and conquer Generating all 2^(n-1) subsets for {a1,, an-1} Add an to each subset

Generating Subsets Problem: generating all 2^n subsets of set A={a1,, an} Solution 1: decrease by one and conquer Generating all 2^(n-1) subsets for {a1,, an-1} Add an to each subset Solution 2: Using a bit string of length n Example: 000, 001,., 111

Selection Problem Find the k-th smallest element in a list of n numbers k = 1 or k = n median: k = n/2 Example: 4, 1, 10, 9, 7, 12, 8, 2, 15 median =? More general: find k-th smallest element? Brute force approach? Discussion!

Algorithms for the Selection Problem Problem: select a k-th smallest element in an array More efficient solutions?

Algorithms for the Selection Problem Brute force: The sorting-based algorithm: Sort and return the k-th element Efficiency (if sorted by mergesort): Θ(nlog n) Better solution: How to apply decrease-and-conquer?

Algorithms for the Selection Problem all are A[s] all are A[s] s Better solution: A faster algorithm is based on using the quicksort-like partition of the list. Let s be a split position obtained by a partition: Assuming that the list is indexed from 1 to n: If s = k, the problem is solved; if s > k, look for the k-th smallest elem. in the left part; if s < k, look for the (k-s)-th smallest elem. in the right part. Note: The algorithm can simply continue until s = k.

Tracing the Median / Selection Algorithm Example: 4 1 10 9 7 12 8 2 15 Here: n = 9, k = 9/2 = 5

Binary Search Tree Algorithms BST Binary tree k Order property: For each node, all the nodes on its left subtree have data less than the node s data; all the nodes on its right subtree larger than the node s data Recursive data structure Empty tree Or, a root node, left subtree is a BST, and right subtree is a BST <k >k

Binary Search Tree Algorithms Several algorithms on BST requires recursive processing of just one of its subtrees, e.g., Searching Insertion of a new key Finding the smallest (or the largest) key k <k >k

Searching in Binary Search Tree Algorithm BTS(x, v) //Searches for node with key equal to v in BST rooted at node x if x = NIL return -1 else if v = K(x) return x else if v < K(x) return BTS(left(x), v) else return BTS(right(x), v) Efficiency worst case: T(n) = n average case: T(n) 2ln n 1.39log 2 n