











































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












































Data Structures and Algorithms-course content. Introduction to data structures and algorithms: definitions and uses of data structures and algorithms, role of data structures and algorithms programming, choice of data structures and algorithms. Elementary data structures: list, queue, stack, tree, records, arrays; types of list: linear-linked list, doubly linked list, circular linked list, circular doubly linked list; types of queue: circular queue; types of trees: AVL tree, red black trees, b-trees; graphs; array based and pointer-based implementation of data structures, hashing, heap, linear, binary search algorithms; sorting algorithms; depth-first, breadth, hill-climbing, least-cost search algorithms using either a structured programming language or an OOP language such as C++, JAVA, C# 1.0 Overview.
- Data representation in a program/ memory deals with the generalization of data in terms of type. - A data type is a well-defined collection of data with a well-defined set of operations on it. - Data type of a variable is the set of values that the variable may assume. - A type is a set, and the elements of the set are called the value of the type. Examples: Type integer represents the set of all integers; type real represents the set of all real numbers. i.e. in C int, double, - Variable: - A variable is a named data storage location in your computer’s memory. - Variable names are used in a program to refer to the data stored memory. 1.1 Definitions What Are Data Structures and Algorithms? We begin with a definition for “algorithm: 1.1.1 Algorithm- A finite sequence of steps for accomplishing some computational task. An algorithm must: Have steps that are simple and definite enough to be done by a computer, and Terminate after finitely many steps Notice that an algorithm is a sequence of steps, not a program. You might use the same algorithm in different programs, or express the same algorithm in different languages, because an algorithm is an entity that is abstracted from implementation details. 1.1.2 Abstract data type (ADT)- : Def 1: A set of values (the carrier set), and operations on those values. Def 2: ADT is defined as a mathematical model with a collection of operations defined on that model. -Abstract Data Type (ADT)-defines the type of data that can be stored in a structure, and the available methods to operate on the data. - Example. In Java, an ADT is modelled as an interface and implemented as a **class****.
Properties of ADT Generalization Encapsulation (NB: Find out what the above two properties mean)
Correctness -- a data structure or algorithm is designed, to work as required for all possible inputs. Example: A sorting algorithm must arrange its inputs in ascending or descending order only. Efficiency implies fast runtime, and minimal use of computational resources. Robustness -- a program produces correct outputs for all inputs on any hardware platform on which the program is implemented. N.B Robustness includes the concept of scalability -- the capability of a data structure or algorithm to handle expanding input size without incurring complexity or causing inaccuracy or failure. Adaptability -- software must be able to be modified or evolved over time to meet environmental changes (e.g., increased CPU speed, client-server implementation, or new functionality to increase market demand). Example: The Year 2000 Problem is an excellent example of poor software design, where date fields in old software were not designed to be large enough to accommodate date codes for an interval greater than 100 years. Reusability -- a given piece of code must be able to be used as a component in different systems or in different application domains without significant modification. Selecting a Data Structure
Static memory allocation -Memory is allocated to the entities by the operating system and they remain reserved till the end of the program. Dynamic memory allocation - Memory is allocated dynamically to the entities of the program. The programmer has the freedom to allocate and free the program entities Data can be stored on a computer in generally one of three means: contiguous, linked or indexed allocation of memory. Prototypical examples: Contiguous allocation: arrays Linked allocation: linked lists indexed allocation 2.1.1 Contiguous Allocation- The definition of contiguous,( adj ., given by the Oxford English Dictionary) is touching , in actual contact, next in space; meeting at a common boundary, bordering, adjoining; while Meriam-Webster defines it as Touching or connected throughout in an unbroken sequence.
- it is a classical memory allocation model that assigns a process consecutive memory blocks (i.e. memory blocks having consecutive addresses). -When a process needs to execute, memory is requested by the process. The size of the process is compared with the amount of contiguous main memory available to execute the process. If sufficient contiguous memory is found, the process is allocated memory to start execution. Otherwise, it is added to a queue of waiting processes until sufficient free contiguous memory is available. An array stores n objects in a single contiguous space of memory. Unfortunately, if more memory is required, a request for new memory usually requires copying all information into the new memory. In general, you cannot request for the operating system to allocate to you the next n memory locations. Figure below shows an array of size four which is first filled. Next, if additional memory is required, a new array (in this case, double the size) is allocated, the information is copied over, and the old array is deallocated. 2.1.2 Linked Allocation- Linked allocation has each item of data stored together with a reference to successor or related data. Usually this reference will be in the form of an address, though other mechanisms exist.
C++ (old) 0 C++ (new) nullptr Symbolically Ø The Node class stores an individual node , but it is still necessary to have a data structure that allows the user to access and manipulate the linked list. At a minimum, such a class must store the address of the first node in the linked list (the head), but it is also useful to store the address of the last node (the tail), as well. A minimal C++ class might be defined as: template
-in this scheme, each file is provided with its own index block, which is an array of disk block pointers (addresses). -Matrices tend to use a hybrid of contiguous and indexed allocation e.g. the matrix below:
Can be stored either by storing the rows or the columns, as shown Storing a matrix by row or by column More ideally, the entries are stored in a contiguous block of memory, and the indices point into this structure, as shown in Figure below: Row-major order and column-major order for storing matrices C, C++ and python use row-major order while Matlab and FORTRAN use column-major order for storing matrices.
A graph representing an adjacency relation. Suppose we allow arbitrary relations between any two objects in a container
2.2.3.2 Linked Arrays Other hybrids are linked lists of arrays. It is also possible to link arrays.
3.1 Categories of data structures
Structured data type-Tree Structure
int scores[100]; // 100 ints, scores[0] to scores[99] char name[40]; // 40 chars, name[0] to name[39] Storage of Array in Main Memory
int billy[5]; // declaration of a new array billy[2] = 75; // access to an element of the array. Store a value to an array element Initializing single dimensional array Int num [5]; Int num [5] = {8, 2, 22, 56, 11} This assigns value 8 to array [0], 2 to array [1], 22 to array [2] etc. 8 2 22 56 11
For example, jimmy represents a multidimensional array of 3 by 5 elements of type int.
- Declared as Example 1. Give a statement that refers to the 9th^ element in array jimmy: A statement that passes the value of the 9th^ element of jimmy to a variable called a, Initializing a multidimensional array A one-dimensional array is usually processed via a ‘for’ loop. Similarly, a two-dimensional array may be processed with a nested for loop: 3.2.1.3 Arrays: Disadvantages The size of the array is fixed. Results to wastage of memory space when a large array space is initialized and elements fails to occupy the entire space. Slow inserts Slow deletes Inserting new elements at the front or somewhere at the middle is potentially expensive because existing elements need to be shifted over to make room. int jimmy [3][5]; jimmy[1][3]
3.2.2 Linear Lists