Array-Based Structures Page 1

Similar documents
Chapter 3: Restricted Structures Page 1

DATA STRUCTURES USING C

Data Structures and Data Manipulation

CS 111 Classes I 1. Software Organization View to this point:

COMPUTER SCIENCE. Paper 1 (THEORY)

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

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

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

AP Computer Science Java Mr. Clausen Program 9A, 9B

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Section of DBMS Selection & Evaluation Questionnaire

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Symbol Tables. Introduction

Array Abstract Data Type

Binary search algorithm

6. Standard Algorithms

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:

AP Computer Science Java Subset

Searching Algorithms

Java Application Developer Certificate Program Competencies

Introduction to Data Structures

Contents. 9-1 Copyright (c) N. Afshartous

Lecture Notes on Linear Search

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

LINKED DATA STRUCTURES

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Basic Programming and PC Skills: Basic Programming and PC Skills:

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Pseudo code Tutorial and Exercises Teacher s Version

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Data Structures and Algorithms Written Examination

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Data Structures Using C++ 2E. Chapter 5 Linked Lists

CMSC 202H. ArrayList, Multidimensional Arrays

Computer Science III Advanced Placement G/T [AP Computer Science A] Syllabus

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Trace-Based and Sample-Based Profiling in Rational Application Developer

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Chapter 8: Bags and Sets

Introduction to Java

Computer Programming I

Database Programming with PL/SQL: Learning Objectives

AP Computer Science AB Syllabus 1

#820 Computer Programming 1A

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

Darshan Institute of Engineering & Technology PL_SQL

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Java (12 Weeks) Introduction to Java Programming Language

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Explain the relationship between a class and an object. Which is general and which is specific?

Project 4 DB A Simple database program

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

Sources: On the Web: Slides will be available on:

Why Use Binary Trees?

Analysis of Binary Search algorithm and Selection Sort algorithm

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Common Data Structures

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Chapter 14 The Binary Search Tree

Computer Programming I

Unit Storage Structures 1. Storage Structures. Unit 4.3

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

dictionary find definition word definition book index find relevant pages term list of page numbers

Raima Database Manager Version 14.0 In-memory Database Engine

Introduction to Programming System Design. CSCI 455x (4 Units)

TREE BASIC TERMINOLOGIES

Chapter 7: Additional Topics

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Java CPD (I) Frans Coenen Department of Computer Science

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

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

Easy-Cassandra User Guide

Programming Database lectures for mathema

Chapter 13: Query Processing. Basic Steps in Query Processing

Collections and iterators

Chapter 7: Sequential Data Structures

Parameter Passing in Pascal

cs2010: algorithms and data structures

The Fast Fourier Transform

Algorithms and Data Structures Written Exam Proposed SOLUTION

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Efficient Data Structures for Decision Diagrams

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

Lab Experience 17. Programming Language Translation

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

TECHNOLOGY Computer Programming II Grade: 9-12 Standard 2: Technology and Society Interaction

7.1 Our Current Model

Lecture Notes on Binary Search Trees

RARITAN VALLEY COMMUNITY COLLEGE ACADEMIC COURSE OUTLINE. CISY 105 Foundations of Computer Science

J a v a Quiz (Unit 3, Test 0 Practice)

Ordered Lists and Binary Trees

The C Programming Language course syllabus associate level

ADTs,, Arrays, Linked Lists

C++ Programming Language

Morris School District. AP Computer Science A Curriculum Grades 9-12

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

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Transcription:

Array-Based Structures Page 1 1 2 3 4 5 6 Array-Based Structures Chapter 2 Array: A Data Structure Case Study Arrays are rooted in the mathematical concept of subscripted variables Mathematicians used early computers to evaluate formulas with subscripted variables They needed a way to store these variables Arrays were conceived to fill that need Array s design goals Speed (because the variables would be used in many calculations) Low overhead (because a large number of variables to be represented) Array Design Considerations A dilemma was presented to the designers Most often speed and low overhead are mutually exclusive The solution was found in the characteristics of subscripted variables Each variable as a unique ordinal subscript A minimum and maximum value of the subscript All values stored in a set of variables are of one type Insert and Delete operations not allowed (only Fetch and Update) These characteristics permit contiguous memory model accessed using a mapping function Contiguous Memory Model Because There is a minimum and maximum value of the subscript, and All values stored in a set of variables are of one type the size of a contiguous memory area can be determined For example: For: 0 <= subscript <= 9 and type int (4 bytes), the size of the contiguous area is 40 bytes = 10 * 4 Mapping Function Because Each variable as a unique ordinal subscript that could be used as a node number, and The designers stored the nodes in sequential order in contiguous memory the address of a node (or variable) could be calculated The formula to perform the calculation is called a mapping function One-Dimensional Array Storage Scheme Gives rise to the mapping function:

Array-Based Structures Page 2 Address of xi = Address of x[i] = Ao + (i io) * w where: io is the lowest value of the subscript (array index), Ao is the address of x0 7 Two-Dimensional Array Storage Scheme Mapping functions Address of Xij = Ao + (i * (jmax + 1) + j) * w Address of Xij = Ao + (j * (imax + 1) + i) * w for row major order for column major order 8 9 10 11 Design Goals Were Achieved Speed Mapping function locates a node quickly Only a calculation is necessary, not a memory search Low overhead The only overhead is: The address A0 The node width, w The minimum index i0 (not necessary in Java, = 0) Arrays Are a Data Structure Although most programmers use arrays, they fail to view them as a data structure The reason for this is that by default the data structure array is: A homogeneous structure A static structure (maximum number of nodes must be specified when the structure is created) Accessed in the node number mode Only the Fetch (a = x[2]) and Update (x[2] = 5) operations are allowed Design goals: speed and low overhead Programmer Defined Array-Based Structures These are data structures that Use arrays in their implementation Each array element is a node reference variable Each array element stores the address of a node Unlike arrays, these structures Are accessed in the key field mode Support all four basic operations The three examples are: Unsorted Array structure Sorted Array structure Unsorted-Optimized Array structure Unsorted Array Structure

Array-Based Structures Page 3 All elements of the array are initialized to null for this, any other array-based, structure Insert uses the next available null array element to store the new node location Delete Uses a sequential search Moves up all nodes below the deleted node (garbage collection technique) Fetch uses a sequential search Update combines a Delete and Insert 14 18 16 Unsorted Array Structure Insert Algorithm // No error checking performed // Store a deep copy of the node to be // inserted, e.g. newnode data[next] = newnode.deepcopy() // Prepare for the next insert next++; Unsorted Array Structure Delete Algorithm // Access the node (assumes the node is in // the structure) i = 0; while(targetkey!= data[i].key) // not found i++; // Move all the references up to eliminate // the node and collect the garbage for( j = i; j < next - 1; j++) data[j] = data[j + l]; next++; data[next] = null; Unsorted Array Structure Fetch Algorithm // Access the node (assumes the node is in // the structure) i = 0; while ( targetkey!= data[i].getkey() ) i++;

Array-Based Structures Page 4 // Return a clone of the node to the client return data[i].deepcopy(); 19 21 23 24 Speed of the Unsorted Array Structure Containing n Nodes Insert No loops, 3 memory accesses, O(1) Fetch Loop in the sequential search executes an average of n/2 times, with 2 memory accesses each time, O(n) Delete Each loop executes an average of n/2 times, each performing 2 memory accesses (4 total) each time, O(n) Update Combines a Delete and Insert Operation, 2n+3 memory accesses, O(n) Average operation:1.25n + 1.5 memory accesses, O(n) Sorted Array Structure All elements of the array are initialized to null Nodes are stored in sorted order based on key Fetch uses a binary search Delete Uses a binary search and then Moves up all node references below the deleted node (garbage collection technique) Insert First moves larger nodes references down in array Then places inserted node in its sorted order position Sorted Array Structure Fetch Algorithm (Page 1) // Access the node using a binary search // (assumes it is in the structure) low = 0; high = next 1; i = (low + high) / 2; Sorted Array Structure Fetch Algorithm (Page 2) // Binary search while (targetkey!= data[i].key) if( targetkey < data[i].key && high!= low) )

Array-Based Structures Page 5 // Move high down to eliminate // the upper half of array high = i 1; else // Move low up to eliminate // the lower half of the array low = i + 1; i = (low + high) / 2; 25 27 28 29 Sorted Array Structure Fetch Algorithm (Page 3) // Return the fetched node return data[i].deepcopy(); Sorted Array Structure Delete Algorithm (Page 1) // Access the node using a binary search // (assumes it is in the structure) low = 0; high = next 1; i = (low + high) / 2; Sorted Array Structure Delete Algorithm (Page 2) // Binary search while (targetkey!= data[i].key) if( targetkey < data[i].key && high!= low) ) // Move high down to eliminate // the upper half of array high = i 1; else // Move low up to eliminate // the lower half of the array low = i + 1; i = (low + high) / 2; Sorted Array Structure Delete Algorithm (Page 3) // Move node references up to delete // the node and collect the garbage for(j = i; j < next - 1; j++) data[j] = data[j + 1];

Array-Based Structures Page 6 next--; data[next] = null; 31 32 33 34 Sorted Array Structure Insert Algorithm (Page 1) // Find the new node's sorted order position // using a binary search, // targetkey is its key low = 0; high = next 1; i = (low + high) / 2; Sorted Array Structure Insert Algorithm (Page 2) // Binary search while (targetkey!= data[i].key) if( targetkey < data[i].key && high!= low) ) // Move high down to eliminate // the upper half of array high = i 1; else // Move low up to eliminate // the lower half of the array low = i + 1; i = (low + high) / 2; Sorted Array Structure Insert Algorithm (Page 3) // Move all the node references down to // "open up" a spot for the new node for( j = next; j >= i; j--) data[j] = data[j - 1]; next++; // Add a deep copy of the new node to // the structure data[i] = newnode.deepcopy(); Speed of the Sorted Array Structure Containing n Nodes Fetch

Array-Based Structures Page 7 Loop in the binary search executes, in the worst case, log2n times, with 2 memory accesses each time, O(log2n) Delete Loop to move up the node references executes an average of n/2 times, with 2 memory accesses each time, O(n) Insert Loop to move the node references down executes an average of n/2 times, with 2 memory accesses each time, O(n) Update Combines a Delete and Insert Operation, 2n memory accesses, O(n) Average operation: n + ½log2n memory accesses, O(n) 35 36 38 39 Unsorted-Optimized Structure All elements of the array are initialized to null The Insert algorithm is the same as the Unsorted Array structure, O(1) The Fetch and Delete algorithms of the Unsorted Array structure are modified to improve their speed Unsorted-Optimized Fetch Improvement A sequential search is still used to locate a node, however: After a node is fetched, its reference is swapped with the reference above it Most frequently fetched nodes bubble-up to the top of the array Reduces the average number of times subsequent sequential searches execute Nodes in most data bases do not have equal probability of being fetched Unsorted-Optimized Fetch Algorithm (Page 1) // Assumes the node is in the structure i = 0; // Sequential search while (targetkey!= data[i].key) i++; // Copy the node before it is relocated node = data[i].deepcopy(); Unsorted-Optimized Fetch Algorithm (Page 2) // Move the node reference up one position if (i!= 0) temp = data[i - 1]; data[i - 1] = data[i];

Array-Based Structures Page 8 data[i] = temp; // Return a copy of the node return node; 40 42 Unsorted-Optimized Delete Improvement A sequential search is still used to locate a node; however The loop to move up all the node references is eliminated Only the last node reference is moved; it overwrites the deleted node s reference This eliminates the n memory accesses doubling the speed of a Delete operation Unsorted-Optimized Delete Algorithm // Assumes the node is in the structure // access the node i = 0; // Sequential search while (targetkey!= data[i].key) i++; // Move the last node into the deleted // node's position data[i] = data[next - 1]; next++; 43 Speed of the Unsorted-Optimized Array Structure Containing n Nodes Insert (same as Unsorted Array Structure) No loops, 3 memory accesses, O(1) Fetch In the worst case, loop in the sequential search executes an average of n/2 times, with 2 memory accesses each time, O(n) Delete Loop in the sequential search executes an average of n/2 times, with 2 memory accesses each time, O(n) Update Combines a Delete and Insert Operation, n+3 memory accesses, O(n) Average operation: 0.75n + 1.5 memory accesses, O(n)

Array-Based Structures Page 9 44 45 47 48 49 Best Array-Based Structure When all operations equally probable Unsorted-Optimized Array is the fastest Its average memory accesses of 0.75n + 1.5 is < Sorted Array (n + ½log2n) < Unsorted Array (1.25n + 1.5) When most operations are: Inserts: the Unsorted structures are the fastest Fetches: the Sorted structure is the fastest All three structures have the same density Density of the Array Based Structures Density = information bytes / total bytes Information bytes = n * w n is the number of nodes, w is the bytes per node Overhead = 4n + 4 4 bytes per array element + 4 bytes for next Density = n * w / (n * w + 4n + 4) = 1 / ( 1 + 4 / w + 4 / (n * w) ) As n gets large above approaches 1 / (1 + 4 / w) Implementation of a Data Structure As a Separate Class An data base application is coded as three separate classes The data structure class (e.g., the Optimized-Array) Implements the operation algorithms expanded to include error checking The node definition class (e.g., class Listing ) The application class Declares an object in the data structure class Invokes the data structure class operation methods The data structure and node classes include utility methods Error Checking in the Unsorted-Optimized Structure The Insert algorithm should return false if The structure is full There is insufficient system memory to perform the deep copy If the node is not in the structure The Fetch and Delete algorithms should return null and false respectively Error Checking in the Optimized-Unsorted Array s Insert Algorithm

Array-Based Structures Page 10 To detect structure full (array is completely used) An integer variable, size, is added to the data structure to store the size of the array The test for full is added to the front of the algorithm if (next == size) // structure full return false; To detect insufficient system memory In this case, Java s new operator returns null, So, after the deep copy is performed add if(data[next] == null) // no system memory return false; 50 51 52 53 Error Checking in the Unsorted-Optimized Fetch and Delete Algorithms The Boolean condition in the algorithms while loop is expanded while (i < next && targetkey!= data[i].key) When the loop ends In the Fetch algorithm we add if(i == next); // node not found return null; In the Delete algorithm we add Utility Methods if(i == next); // node not found return false; A method to output all nodes, show A constructor to allow the application to specify max number of nodes A method to input the contents of a node, input Dynamic Array-Based Structures Unlike arrays, array-based structures can expanded at run time (within the limits of system memory) The expansion algorithm is added to the Insert algorithm It uses one of two copy techniques The expansion does decrease the speed of the insert operation that triggered the expansion Array-Based Structure Expansion Algorithm Added to the Insert Operation When a full structure is detected 1- An array reference variable, temp, is set pointing to the structure s filled array, data 2- A larger array is declared, e.g., larger 3- The reference variable data is set pointing to the array larger

Array-Based Structures Page 11 4- The references from the filled array, temp, are copied into the array larger 5- temp is set to null to recycle the smaller array s storage 55 56 57 58 Array Copy Techniques Two techniques to copy the array temp into the array data Code a for loop for(int i=0; temp.length; i++) data[i] = temp[i]; Use Java s arraycopy method System.arraycopy(temp, 0, data, 0, temp.length); Use of arraycopy is 50% faster Generic Array-Based Structures Any type of node could be stored in the structure Certain considerations must be followed in the design of the structure The client application specifies the type of nodes that will be stored in the structure The generic features of Java are used in the structure s generic implementation Design Considerations for Generic Data Structures The node definition and the data structure are coded as two separate classes The data structure cannot mention the names of the data fields that make up a node If the structure is going to be encapsulated, a method to perform a deep copy of a node must be coded in the node definition class If the structure is going to be accessed in the key field mode, a method to determine if a given key is equal to the key of a node must be coded in the node definition class The data structure code cannot mention the name of the node class The data structure class cannot mention the key field s type Client Side Syntax of Generic Data Structures To declare instance, boston, of a generic data structure class, UOA, that will store Listing nodes, the client codes UAO <Listing> boston = new <Listing> UAO( ); Similarly, if the nodes were Car objects the declaration would be UAO <Car> garage = new <Car> UAO( ); An attempt to store nodes of any other type in these structures would result a

Array-Based Structures Page 12 compile error 59 Generic Implementation Techniques A generic placeholder is coded at the end of the class heading e.g., public class UOA<T> The array reference (data) is declared to be of type T e.g., private T[ ] data; The array is declared to be an array of Object references whose location is coerced into the variable data data = (T[ ]) new Object[100]; 60 Generic Implementation Techniques (continued) The type of the nodes stored in the structure is type T and the type of the key is Object public boolean update(object targerkey, T newnode) An interface (implemented by the node definition class) must contain the signatures of the deepcopy, tostring, and compareto methods In the data structure, these methods must operate on an instance of the interface 61 62 Java s API ArrayList Class A generic data structure Access is in the node number mode Supports the four basic operations Not encapsulated Stores objects, but primitive types are wrapped automatically in Wrapper objects expandable Client codes ArrayList <Car> garage = new ArrayList<Car> (200) for an initial capacity of 200 Car objects (default is a capacity of 10) Four Basic Operations in the Class ArrayList