oop & data structure concepts

Upload: fahad-mustafa

Post on 28-Feb-2018

236 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 OOP & Data structure concepts

    1/16

    1

    Data Structure

    Assignment

  • 7/25/2019 OOP & Data structure concepts

    2/16

    2

    ADTIntroduction to ADTs

    Intro

    Abstract data types are purely theoretical entities, used (among other things) to simplify thedescription of abstract algorithms, to classify and evaluate data structures, and to formally describethe type systems of programming languages. However, an ADT may be implemented by specificdata types or data structures, in many ways and in many programming languages; or described ina formal specification language. ADTs are often implemented as modules: the module's interfacedeclares procedures that correspond to the ADT operations, sometimes with comments thatdescribe the constraints. This information hiding strategy allows the implementation of the moduleto be changed without disturbing the client programs.

    The term abstract data type can also be regarded as a generalised approach of a numberof algebraic structures, such as lattices, groups, and rings. The notion of abstract data types isrelated to the concept of data abstraction, important in object-oriented programming and design by

    contract methodologies for software development.

    Definition

    An abstract data type is defined as a mathematical model of the data objects that make up a datatype as well as the functions that operate on these objects. There are no standard conventions fordefining them. A broad division may be drawn between "imperative" and "functional" definitionstyles.

    WHO: Abstract Data Type

    WHAT: Abstract Data Type are data-type that combines the common functionalities from differentrelated objects into one package, so that those different but related objects interface can inheritfrom ADT thus making it more flexible and less coding for the programmer.

    WHERE: Anywhere, when you code, in your life wherever applicable.

    WHEN: Use it when you want to generalise a design. When you have many objects that havesimilar functionalities, you can Abstract the common functionalities into one class, and have theother objects inherit from them.

    WHY: You would create ADT for numerous reasons. First it generally leads to a flexible design if

    designed well. Second its less coding for the programmer. Third its more reusability. Fourth, itleads to easier maintenance. and Fifth, it creates the possibility of polymorphism.

  • 7/25/2019 OOP & Data structure concepts

    3/16

    3

    Sparse MatrixIntroSparse matrix is a matrix populated primarily with zeros. Sparse matrices are stored using special

    data structures, which allow to store only nonzero elements and save a lot of memory and CPUtime when working with such matrices.

    Specialized data structures allow to solve computational problems which are impossible to solvewhen matrices are stored using "conventional", dense storage.

    Sparse Storage FormatsStructure used for sparse matrix storage should provide following functionality:

    maintain list of nonzero elements and their valuesretrieve element by its indexes i/j

    enumerate all nonzero elements in the matrixinsert new element in the matrixmultiply sparse matrix by vector (or dense matrix)other service operations

    There are several popular data structures which are used to store sparse matrices, each with itsown benefits and drawbacks. ALGLIB package implements two such structures: hash table storage(also known as Dictionary of keys) and CRS (Compressed row storage).

    Hash table storage

    Sparse matrix can be stored as hash table which maps keys (row/column indexes) to values ofcorresponding elements. Such storage format has following features:

    matrix creation- requires to know only matrix size. It is not necessary to know amount ofnonzero elements, although you can benefit from such knowledge. It will allow you to allocaterequired amount of space during matrix creation and to avoid subsequent reallocations of thehash table during initialization of the matrix elements.time to insert element- O(1). Elements can be inserted in arbitrary order. However, when hashtable becomes nearly full we have to spend O(TableSize) time creating new, larger table andcopying elements from the old one.memory requirements- O("(2sizeof(int)+sizeof(double))) per one nonzero element in nearly

    full table. Here "is a small constant from [1.2,1.5] which is a consequence of the fact that hashtable require some amount of "spare memory".time to read element - O(1). Hash tables are quite fast and allow arbitrary access to elements.time to enumerateall nonzero elements - O(TableSize), where TableSize is a total size of hashtable (including both busy and free elements). Hash table allows to enumerate its entries, but itdoes not provide any ordering guarantee. Elements of the matrix will be retrieved in arbitraryorder.linear operationswith matrix (matrix-vector products, matrix-matrix products) - not supported.Hash table stores matrix elements in random order, which prevents efficient implementation ofthe linear algebra operations. Thus, in order to use matrix in linear operations you have toconvert it to CRS format.

    You may see that hash table storage allows easy creation and modification of sparse matrices. Wemay access (read and write) elements in arbitrary order, we do not have to know table size inadvance, we have fast random access to the table. However, this storage format is not good for

  • 7/25/2019 OOP & Data structure concepts

    4/16

    4

    and does not allow to use sparse matrix in linear algebra operations like matrix-vector product. Youhave to convert table to CRS format in order to do so.

    Thus, hash table storage can be classified as intermediate representation which is used when youwant to initialize sparse matrix.

    CRS representation

    CRS (Compressed Row Storage) is another representation which can be used to store sparsematrix. Matrix elements are ordered by rows (from top to bottom) and columns (within row - fromleft to right). Such storage format has following features:

    matrix creation- requires prior knowledge of both matrix size and nonzero elements count. It isrequire to tell in advance how many nonzero elements is stored in each row of the matrix.time to insert element- O(1). Elements can not be inserted in arbitrary locations. You can insertthem only in strict order - within row from left to right, rows are processed from top to bottom.

    memory requirements- O(sizeof(int)+sizeof(double)) per one nonzero element.time to read element- O(log(NonzeroElementsInRow)). Retrieval of the arbitrary elementrequires binary search in the corresponding row.time to enumerateall nonzero elements - O(NonzeroCount), where NonzeroCount is a numberof nonzero elements. CRS representation guarantees that elements will be retrieved in specificorder - rows are enumerated from top to bottom, within row elements are enumerated from left toright.linear algebra operations(matrix-vector products, matrix-matrix products) - are supported.

    You may see that CRS representation allows to use matrices in the linear algebra operations dueto efficient storage format. However, hard initialization is weakness of such representation: a) youhave to know in advance number of nonzero elements in the matrix, and b) you have to fill matrix

    in strict order (from left to right, from top to bottom).Thus, CRS representation is good for numerical work, but is not good for easy initialization. Luckily,there exists hash table storage format, which is good for initialization, and can be converted toCRS.

    Example

    #include#includevoid main(){int A[10][10],B[10][3],m,n,s=0,i,j;

    clrscr();printf("\nEnter the order m x n of the sparse matrix\n");scanf("%d%d",&m,&n);printf("\nEnter the elements in the sparse matrix(mostly zeroes)\n");for(i=0;i

  • 7/25/2019 OOP & Data structure concepts

    5/16

    5

    { for(j=0;j

  • 7/25/2019 OOP & Data structure concepts

    6/16

    6

    Why use Recursion?The problem we illustrated above is simple, and the solution we wrote works, but we probablywould have been better off just using a loop instead of bothering with recursion. Where recursiontends to shine is in situations where the problem is a little more complex. Recursion can be appliedto pretty much any problem, but there are certain scenarios for which youll find its particularly

    helpful. In the remainder of this article well discuss a few of these scenarios and, along the way,well discuss a few more core ideas to keep in mind when using recursion.

    Tail RecursionRecursive calls can occur at any point of the algorithm. For example, if we consider a for loop, thatruns from 0 to n-1, then we know that the loop body is executed repeatedly with different values ofn. Similarly, If the recursive call occurs at the end of the function, called tail recursion, the result issimilar to a loop. That is, function executes all the statements before recursive call before jumpinginto the next recursive call.Example

    public void foo(int n) { if (n == 1) return; else { System.out.println(n); foo(n-1);}}

    Head Recursion

    If the recursive call occurs at the beginning of the function, called head recursion, the functionsaves the state of the program before jumping into the next function call. That is, function waits toevaluate statements until the exit condition is reached. The state of the program is saved in a stack(we will learn more about stacks later in the course)Example

    void foo(int n) { If (n==0) return; else { foo(n-1); System.out.println(n); }}

    Self Referencing ClassesIntro

    Self-reference occurs in natural or formal languages when a sentence, idea or formularefers to itself. The reference may be expressed either directlythrough some intermediatesentence or formulaor by means of some encoding. In philosophy, it also refers to the ability of asubject to speak of or refer to itself: to have the kind of thought expressed by the first personnominative singular pronoun, the word "I" in English.

    The definition of class may refer to itself. One situation in which this occurs is when a classoperation has an object as a parameter where that object is of that same class as the oncecontaining the operation. Examples of where is occurs are the following:

    a Location object is to decide if it has the same screen coordinates as another location object.

    a Shape object is to decide if it has the same height and width as another shape object, or

    a File object is to copy itself to or from another File

  • 7/25/2019 OOP & Data structure concepts

    7/16

    7

    In each case the operation needs as its parameter an object in the same class as the onecontaining the operation.

    Example

    #include #include

    class Node{public:Node(int AGE, string NAME){next = 0;setAge(AGE);setName(NAME);

    }void setAge(int a){age = a;}int getAge(){return age;}void setName(string n){name = n;

    }string getName(){return name;}void setNextPtr(Node *node){Node *last = this;while (last->next)last = last->next;last->next = node;

    }Node *getNextPtr();

    private:int age;string name;Node *next;};

    void main(){Node *temp = 0;Node *temp2;int age, choice;string name;

  • 7/25/2019 OOP & Data structure concepts

    8/16

    8

    while(true){cout > choice;cout age;temp2 = new Node(age, name);cout setNextPtr(temp2);break;

    }

    }

    }

    Memory AllocationIntroMemory allocation is a process by which computer programs and services are assigned withphysical or virtual memory space.

    Memory allocation is the process of reserving a partial or complete portion of computer memory forthe execution of programs and processes. Memory allocation is achieved through a process knownas memory management.

    Memory allocation has two core types;

    Static Memory Allocation:The program is allocated memory at compdetime.

    Dynamic Memory Allocation: The programs are altocated wth memory atrun time.

    Static Memory Allocation

    Done at compile time,

    Global variables: variables declared ahead of time." such fixed arrays.

    Lifetime= entire runtime of programAdvantage: efficient execution time.Disadvantage?

    If we declare more static data space Aan we need, we waste space.

    If we declare less static space than we need, we are out of luck.

  • 7/25/2019 OOP & Data structure concepts

    9/16

    9

    Dynamic Memory Allocation Done at run time.

    Data structures can grow and shrink to fit changing data requirements.

    We can allocate (create) additional storage whenever we need them.

    We can de-allocate (free/delete) dynamic space whenever we are done with them.

    Advantage: we can always have exactly the amount of space required - no more, no less

    For example, with references to connect them we can use dynamicdata structures to create a chain of data structures called a linked listlist.

    Balance and AVL TreeIntro

    In computer science, an AVL tree is a self-balancing binary search tree. It was the first such

    data structure to be invented. In an AVL tree, the heights of the two child subtrees of any nodediffer by at most one; if at any time they differ by more than one, rebalancing is done to restore thisproperty. Lookup, insertion, and deletion all take O(log n) time in both the average and worstcases, where n is the number of nodes in the tree prior to the operation. Insertions and deletionsmay require the tree to be rebalanced by one or more tree rotations.

    The AVL tree is named after its two Soviet inventors, Georgy Adelson-Velskyand EvgeniiLandis, who published it in their 1962 paper "An algorithm for the organisation of information".

    PropertiesBalance factors can be kept up-to-date by knowing the previous balance factors and the

    change in height - it is not necessary to know the absolute height. For holding the AVL balanceinformation, two bits per node are sufficient.

    How to keep the tree balanced

    Still need to insert preserving the order of keys(otherwise search does not work)Insert as the new item arrive (cannot wait till have all items)So need to alter the tree as more item arrive.

    HashingIntroHashing is the process of mapping large amount of data item to a smaller table with the

    help of a hashing function. The essence of hashing is to facilitate the next level searching methodwhen compared with the linear or binary search. The advantage of this searching method is itsefficiency to hand vast amount of data items in a given collection (i.e. collection size).

    Types of Hashing

    There are two types of hashing which are as follows:

    Static Hashing

  • 7/25/2019 OOP & Data structure concepts

    10/16

    10

    Dynamic hashing

    Static Hashing

    Hashing provides a means for accessing data without the use of an index structure.

    Data is addressed on disk by computing a function on a search key instead.

    A bucket in a hash file is unit of storage (typically a disk block) that can hold cane ormore records.The hash function, h, is a function from the set of all search-keys, K, to the set of allbucket addresses, B.

    Insertion, deletion, and lookup are done in constant time.

    Dynamic Hashing

    More effective then static hashing when the database grows or shrinks;

    Extendable hashing splits and coalesces buckets appropriately with the database size.

    i.e. buckets are added and deleted on demand.

    Overflow HandlingIntro

    The API for user written fixed points function provides functions for some mathematicaloperations such as conversions. When these operations are performed, a loss of precision oroverflow may occur the tokens in the following tables allow you to control the way an API functionshandles precision loss and overflow. The data type of overflow handling methods isfXpModeOverflow.

    Overflow Logging StructureMath functions of the API:

    Such as the FXPConvert, can encounter overflow when carrying out an operation. These functionsprovide a mechanism to log the occurrence of overflow and to report that log back to caller.

    You can use a fixed point overflow logging structure in your S.function by defining a variable ofFXPOverflowlogs. Some API functions such as SSFXPConvert, accept a pointer to this structureas an argument.

    Token Description

    FXP_Overflow.STRUCTURE Structure overflow

    FXP_Overflow.WRAP Wrap overflow

  • 7/25/2019 OOP & Data structure concepts

    11/16

    11

    Standard Template LibraryIntro

    The Standard Library is a fundamental part of the C++ Standard. It provides C++

    programmers with a comprehensive set of efficiently implemented tools and facilities that can beused for most types of applications.

    In this article, I present an introduction/tutorial on the Standard Template Library, which is the mostimportant section of the Standard Library. I briefly present the fundamental concepts in the STL,showing code examples to help you understand these concepts. The article requires and assumesprevious knowledge of the basic language features of the C++, in particular, templates (bothfunction templates and class templates).

    Iterators

    In the example of reversing a C array, the arguments to reverse are clearly of type double*. Whatare the arguments to reverse if you are reversing a vector, though, or a list? That is, what exactlydoes reverse declare its arguments to be, and what exactly do v.begin() and v.end() return?

    The answer is that the arguments to reverse are iterators, which are a generalization of pointers.Pointers themselves are iterators, which is why it is possible to reverse the elements of a C array.Similarly, vector declares the nested types iterator and const_iterator. In the example above, thetype returned by v.begin() and v.end() is vector::iterator. There are also some iterators, suchas istream_iterator and ostream_iterator, that aren't associated with containers at all.

    Iterators are the mechanism that makes it possible to decouple algorithms from containers:

    algorithms are templates, and are parameterized by the type of iterator, so they are not restrictedto a single type of container. Consider, for example, how to write an algorithm that performs linearsearch through a range. This is the STL's find algorithm.

    template InputIterator find(InputIterator first, InputIterator last, const T& value) { while (first != last && *first != value) ++first; return first; }Find takes three arguments: two iterators that define a range, and a value to search for in thatrange. It examines each iterator in the range [first, last), proceeding from the beginning to the end,and stops either when it finds an iterator that points to value or when it reaches the end of the

    range.

    First and last are declared to be of type InputIterator, and InputIterator is a template parameter.That is, there isn't actually any type called InputIterator: when you call find, the compiler substitutesthe actual type of the arguments for the formal type parameters InputIterator and T. If the first twoarguments to find are of type int* and the third is of type int, then it is as if you had called thefollowing function.

    int* find(int* first, int* last, const int& value) { while (first != last && *first != value) ++first; return first;

    }

  • 7/25/2019 OOP & Data structure concepts

    12/16

    12

    ArrayListArrayList

    Arraylist is a class which implements List interface. It is widely used because of thefunctionality and flexibility it offers. Most of the developers choose Arraylist over Array as its a very

    good alternative of traditional java arrays.

    The issue with arrays is that they are of fixed length so if it is full we cannot add any more elementsto it, likewise if there are number of elements gets removed from it the memory consumption wouldbe the same as it doesnt shrink. On the other ArrayList can dynamically grow and shrink as per theneed. Apart from these benefits ArrayList class enables us to use predefined methods of it whichmakes our task easy.

    Once an array object has been created, what each individual component refers to can be changed,but the size of the array cannot be changed. ArrayList objects, however, can have their size

    changed. If a refers to an object of type ArrayList, then the statement a.add(expr) whereexpr evaluates to a value of type Thing causes an extra component to be added to the end of thearrayList object referred to by a which is set to refer to the object returned by the evaluation ofexpr. The value returned by a.size() is increased by 1, and the value returned by a.get(a.size()-1)with the new value of a.size() is the value returned by expr. This is a destructive change, so anyvariable which aliases a will see the same change in the object it refers to as it is the same object.So in our code for makeArrayListInt above, the statement

    ArrayList a = new ArrayList();

    declares a variable of type ArrayList called a, and sets it to a new arrayList of integers

    whose size is initially 0. Then every time a.add(0) is called in the loop, an extra component isadded to this arrayList object, every component added referring to an Integer of value 0.

    Linked ListIntro

    In computer science, a linked list is a linear collection of data elements, called nodes,pointing to the next node by means of a pointer. It is a data structure consisting of a group of nodeswhich together represent a sequence. Under the simplest form, each node is composed of dataand a reference (in other words, a link) to the next node in the sequence. This structure allows for

    efficient insertion or removal of elements from any position in the sequence during iteration. Morecomplex variants add additional links, allowing efficient insertion or removal from arbitrary elementreferences.

    Singly Linked List

    Singly linked lists contain nodes which have a data field as well as a 'next' field, whichpoints to the next node in line of nodes. Operations that can be performed on singly linked listsinclude insertion, deletion and traversal.

  • 7/25/2019 OOP & Data structure concepts

    13/16

    13

    Double Linked ListIntro

    In computer science, a doubly linked list is a linked data structure that consists of a set of

    sequentially linked records called nodes. Each node contains two fields, called links, that arereferences to the previous and to the next node in the sequence of nodes. The beginning andending nodes' previous and next links, respectively, point to some kind of terminator, typically asentinel node or null, to facilitate traversal of the list. If there is only one sentinel node, then the listis circularly linked via the sentinel node. It can be conceptualized as two singly linked lists formedfrom the same data items, but in opposite sequential orders.

    The two node links allow traversal of the list in either direction. While adding or removing a node ina doubly linked list requires changing more links than the same operations on a singly linked list,the operations are simpler and potentially more efficient (for nodes other than first nodes) becausethere is no need to keep track of the previous node during traversal or no need to traverse the listto find the previous node, so that its link can be modified.

    Use of Data StructureIntro

    In computer science, a data structure is a particular way of organizing data in a computerso that it can be used efficiently. Data structures can implement one or more particular abstractdata types (ADT), which specify the operations that can be performed on a data structure and thecomputational complexity of those operations. In comparison, a data structure is a concreteimplementation of the specification provided by an ADT.

    Different kinds of data structures are suited to different kinds of applications, and some are highlyspecialized to specific tasks. For example, relational databases commonly use B-tree indexes fordata retrieval, while compiler implementations usually use hash tables to look up identifiers.

    UseData structures provide a means to manage large amounts of data efficiently for uses such

    as large databases and internet indexing services. Usually, efficient data structures are key todesigning efficient algorithms. Some formal design methods and programming languagesemphasize data structures, rather than algorithms, as the key organizing factor in software design.Data structures can be used to organize the storage and retrieval of information stored in bothmain memory and in secondary memory.

  • 7/25/2019 OOP & Data structure concepts

    14/16

    14

    Object Oriented Concepts

    Object Oriented Programming:-Object-oriented programming (OOP) is a programming paradigm based on the concept of

    "objects", which may contain data, in the form of fields, often known as attributes; and code, in theform of procedures, often known as methods.

    Concepts:-

    ClassObjectInheritancePolymorphismAbstraction

    ClassIn object-oriented programming, a class is an extensible program-code-template for

    creating objects, providing initial values for state (member variables) and implementations ofbehaviour (member functions or methods). In many languages, the class name is used as thename for the class (the template itself), the name for the default constructor of the class (asubroutine that creates objects), and as the type of objects generated by instantiating the class;these distinct concepts are easily conflated.When an object is created by a constructor of the class, the resulting object is called an instance ofthe class, and the member variables specific to the object are called instance variables, to contrastwith the class variables shared across the class.

    ObjectIn computer science, an object can be a variable, a data structure, or a function, and as

    such, is a location in memory having a value and possibly referenced by an identifier.In the class-based object-oriented programming paradigm, "object" refers to a particular instanceof a class where the object can be a combination of variables, functions, and data structures.

    InheritanceIn object-oriented programming, inheritance is when an object or class is based on another

    object (prototypal inheritance) or class (class-based inheritance), using the same implementation

    (inheriting from an object or class) specifying implementation to maintain the same behavior(realizing an interface; inheriting behavior). It is a mechanism for code reuse and to allowindependent extensions of the original software via public classes and interfaces. The relationshipsof objects or classes through inheritance give rise to a hierarchy. Inheritance was invented in 1967for Simula. The term "inheritance" is loosely used for both class-based and prototype-basedprogramming, but in narrow use is reserved for class-based programming (one class inherits fromanother), with the corresponding technique in prototype-based programming being instead calleddelegation

    Types of Inheritance

    There are various types of inheritance, based on paradigm and specific language.

  • 7/25/2019 OOP & Data structure concepts

    15/16

    15

    SINGLE INHERITANCEwhere subclasses inherit the features of one superclass. A class acquires the properties of anotherclass.

    MULTIPLE INHERITANCEwhere one class can have more than one superclass and inherit features from all parent classes."Multiple Inheritance (object-oriented programming) was widely supposed to be very difficult to

    implement efficiently. For example, in a summary of C++ in his book on objective C Brd.Coxactually claimed that adding Multiple inheritance to C++ was impossible. Thus, multiple inheritanceseemed more of a challenge. Since I had considered multiple inheritance as early as 1982 andfound a simple and efficient implementation technique in 1984. I couldn't resist the challenge. Isuspect this to be the only case in which fashion affected the sequence of events.

    MULTILEVEL INHERITANCEwhere a subclass is inherited from another subclass. It is not uncommon that a class is derivedfrom another derived class as shown in the figure "Multilevel inheritance".

    MULTILEVEL INHERITANCE

    The class A serves as a base class for the derived class B, which in turn serves as a base class forthe derived class C. The class B is known as intermediate base class because it provides a link forthe inheritance between A and C. The chain ABC is known as inheritance path.

    PolymorphismIn programming languages and type theory, polymorphism (from Greek #$%&', polys,

    "many, much" and ($)*+, morph,, "form, shape") is the provision of a single interface to entitiesof different types. A polymorphic type is one whose operations can also be applied to values ofsome other type, or types. There are several fundamentally different kinds of polymorphism:

    Ad hoc polymorphism:when a function denotes different and potentially heterogeneous

    implementations depending on a limited range of individually specified types and combinations. Adhoc polymorphism is supported in many languages using function overloading.

    Parametric polymorphism:when code is written without mention of any specific type and thuscan be used transparently with any number of new types. In the object-oriented programmingcommunity, this is often known as generics or generic programming. In the functional programmingcommunity, this is often shortened to polymorphism.

    Subtyping (also called subtype polymorphism or inclusion polymorphism):when a namedenotes instances of many different classes related by some common superclass. In the object-oriented programming community, this is often simply referred to as polymorphism.

    HistoryAd hoc polymorphism and parametric polymorphism were originally described in

    Fundamental Concepts in Programming Languages, a set of lecture notes written in 1967 byBritish computer scientist Christopher Strachey. In a 1985 paper, Peter Wegner and Luca Cardelliintroduced the term inclusion polymorphism to model subtypes and inheritance. However,implementations of subtyping and inheritance predate the term "inclusion polymorphism", havingappeared with Simula in 1967.

  • 7/25/2019 OOP & Data structure concepts

    16/16

    16

    AbstractionIn object-oriented programming theory, abstraction involves the facility to define objects that

    represent abstract "actors" that can perform work, report on and change their state, and"communicate" with other objects in the system. The term encapsulation refers to the hiding ofstate details, but extending the concept of data type from earlier programming languages toassociate behavior most strongly with the data, and standardizing the way that different data types

    interact, is the beginning of abstraction. When abstraction proceeds into the operations defined,enabling objects of different types to be substituted, it is called polymorphism. When it proceeds inthe opposite direction, inside the types or classes, structuring them to simplify a complex set ofrelationships, it is called delegation or inheritance.Various object-oriented programming languages offer similar facilities for abstraction, all to supporta general strategy of polymorphism in object-oriented programming, which includes the substitutionof one type for another in the same or similar role. Although not as generally supported, aconfiguration or image or package may predetermine a great many of these bindings at compile-time, link-time, or loadtime. This would leave only a minimum of such bindings to change at run-time.