Data Structures and Algorithms Introduction Data Structures n

  • Slides: 29
Download presentation
Data Structures and Algorithms Introduction

Data Structures and Algorithms Introduction

Data Structures n n 1. 2. Structures are methods of organizing large amounts of

Data Structures n n 1. 2. Structures are methods of organizing large amounts of data. Data Structure is a way of collecting and organizing data in such a way that we can perform operations on these data in an effective way. Data Structure is about rendering data elements in terms of some relationship, for better organization and storage. Types Primitive Data Structure: Ex : int, float, char, Boolean Abstract Data Structure: Linked List, Stack, Queue, Tree, Graph

Role of Algorithms n An algorithm is n n a sequence of computational steps

Role of Algorithms n An algorithm is n n a sequence of computational steps that transform input into output. any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output. describes a specific computational procedure for achieving that input/output relationship correct if for every input instance, it halts with correct output.

Role of Algorithms n Some Well-known Computational Problems n n Sorting, Searching, Shortest paths

Role of Algorithms n Some Well-known Computational Problems n n Sorting, Searching, Shortest paths in a graph Minimum spanning tree, Primality testing, Traveling salesman problem Knapsack problem, Chess, Towers of Hanoi, Program termination Example : Sorting Problem n n Input : A sequence of n numbers (a 1, a 2, a 3…an) Output : A permutation (a 1’, a 2’, a 3’, …. an’) of the input sequnce such that a 1’<a 2’<…. an’. n Example : Input : (31, 41, 59, 26, 41, 58) – instance n Output : (26, 31, 41, 58, 59) - solution

Role of Algorithms n Efficient Sorting algorithm depends on n Number of items to

Role of Algorithms n Efficient Sorting algorithm depends on n Number of items to be sorted n Extent to which items are already somewhat sorted n Possible restrictions on item values n Architecture of computer n Kind of storage devices to be used : main memory, disk , tapes

Role of Algorithms n 1. 2. Applications of Algorithms The Human Genome Project –

Role of Algorithms n 1. 2. Applications of Algorithms The Human Genome Project – Algorithms to 1. identify all the 100, 000 genes in human DNA, 2. determine the sequences of the 3 billion chemical base pairs that make up human DNA, 3. store this information in databases, and developing tools for data analysis. Internet – Fast Access and retrieve large data 1. To find good routes for data travel 2. using a search engine to quickly find pages on which particular information resides

Role of Algorithms n Applications of Algorithms 3. Elecctronic Commerce - Needs 1. public-key

Role of Algorithms n Applications of Algorithms 3. Elecctronic Commerce - Needs 1. public-key cryptography and digital signatures which are based on numerical algorithms and number theory. 4. Manufacturing and other commercial enterprises data 1. oil company - wish to know whereto place its wells in order to maximize its expected profit. 2. political candidate - want to determine where to spend money buying campaign advertising in order to maximize the chances of winning an election.

Role of Algorithms n n n Characteristics of Algorithms There may be many candidate

Role of Algorithms n n n Characteristics of Algorithms There may be many candidate solutions but finding one that does, or one that is “best, ” can present quite a challenge. They have practical applications – For example consider shortest path algorithm. There are variety of applications like n Trucking/ railroad company, has a financial interest in finding shortest paths through a road or rail because shorter paths results in lower labor and fuel costs. n routing node on the Internet may need to find the shortest path through the network in order to route a message quickly.

Role of Algorithms n Np complete Problems n Problems for which no efficient solution

Role of Algorithms n Np complete Problems n Problems for which no efficient solution is known n Interesting Facts n n No body has ever proven that an efficient algorithm for one cannot exist. if an efficient algorithm exists for any one of them, then efficient algorithms exist for all of them. several NP-complete problems are similar, but not identical, to problems for which we do know of efficient algorithms. Ex : Traveling Salesman Problem

Algorithms as a technology n computers may be fast, but they are not infinitely

Algorithms as a technology n computers may be fast, but they are not infinitely fast. n Memory may be inexpensive, but it is not free. n n n Computing time is therefore a bounded resource, and so is space in memory. We should use these resources wisely, and algorithms that are efficient in terms of time or space will help us do so. So, We should know how to measure the efficiency of algorithms in terms of time and space.

Algorithms as a technology n Time complexity n n n Quantifies the amount of

Algorithms as a technology n Time complexity n n n Quantifies the amount of time taken by an algorithm to run as a function of the input. The time complexity of an algorithm is commonly expressed using big O notation. is commonly estimated by counting the number of elementary operations performed by the algorithm, where an elementary operation takes a fixed amount of time to perform. Space complexity n A measure of the amount of working storage an algorithm needs with respect to the input size.

Algorithms as a technology n n Asymptotic Notations are languages that allow us to

Algorithms as a technology n n Asymptotic Notations are languages that allow us to analyze an algorithm's running time by identifying its behavior as the input size for the algorithm increases. This is also known as an algorithm's growth rate. The time required by an algorithm falls under three types − n Best Case − Minimum time required for program execution. n Average Case − Average time required for program execution. n Worst Case − Maximum time required for program execution.

Algorithms as a technology n Asymptotic Notations n Upper Bounds: Big-Oh Notation (O) n

Algorithms as a technology n Asymptotic Notations n Upper Bounds: Big-Oh Notation (O) n n n Formal way to express the upper bound of an algorithm's running time. It measures the worst case time complexity or the longest amount of time an algorithm can possibly take to complete. Lower Bounds: Omega - Ω n n formal way to express the lower bound of an algorithm's running time It measures the best case time complexity or the best amount of time an algorithm can possibly take to complete.

Algorithms as a technology n Tight Bounds: Theta – Θ n n n n

Algorithms as a technology n Tight Bounds: Theta – Θ n n n n n Formal way to express both the lower bound and the upper bound of an algorithm's running time Common Asymptotic Notations Constant Logarithmic Linear n log n Quadratic Cubic Polynomial Exponential − − − − Ο(1) Ο(log n) Ο(n logn) Ο(n 2) Ο(n 3) nΟ(1) 2Ο(n)

Modular Programming n n n One of the basic rules concerning programming is to

Modular Programming n n n One of the basic rules concerning programming is to break the program down into modules. Each module is a logical unit and does a specific job. Its size is kept small by calling other modules. Modularity has several advantages. n n n It is much easier to debug small routines than large routines; It is easier for several people to work on a modular program simultaneously; A well-written modular program places certain dependencies in only one routing, making changes easier.

Abstract Data Types (ADTs) n An abstract data type (ADT) is a set of

Abstract Data Types (ADTs) n An abstract data type (ADT) is a set of operations. n Abstract data types are mathematical abstractions; n n n In an ADT’s definition, there is no mention of how the set of operations is implemented. Objects such as lists, sets, and graphs, along with their operations, can be viewed as abstract data types, just as integers, reals, and booleans are data types. Integers, reals, and booleans have operations associated with them, and so do ADTs.

Abstract Data Types (ADTs) n n n The basic idea is that the implementation

Abstract Data Types (ADTs) n n n The basic idea is that the implementation of the operations related to ADTs is written once in the program, and any other part of the program that needs to perform an operation on the ADT can do so by calling the appropriate function. If for some reason implementation details need to be changed, it should be easy to do so by merely changing the routings that perform the ADT operations. There is no rule telling us which operations must be supported for each ADT; this is a design decision.

The List ADT n The form of a general list: A 1, A 2,

The List ADT n The form of a general list: A 1, A 2, A 3, …, AN; n The size of this list is N; n An empty list is a special list of size 0; n n n For any list except the empty list, we say that Ai+1 follows (or succeeds) Ai (i<N) and that Ai-1 precedes Ai (i>1); The first element of the list is A 1, and the last element is AN. We will not define the predecessor of A 1 or the successor of AN. The position of element Ai in a list is i.

The List ADT set of operations on the list ADT: n n n Print.

The List ADT set of operations on the list ADT: n n n Print. List Make. Empty Find: return the position of the first occurrence of a key Insert and Delete: insert and delete some key from some position in the list Find. Kth: return the element in some position Next and Previous: take a position as argument and return the position of the successor and predecessor

The List ADT Example: The list is 34, 12, 52, 16, 13 b n

The List ADT Example: The list is 34, 12, 52, 16, 13 b n n n Find(52) Insert(X, 3) Delete(52) The interpretation of what is appropriate for a function is entirely up to the programmer.

Simple Array Implementation of Lists n All these functions about lists can be implemented

Simple Array Implementation of Lists n All these functions about lists can be implemented by using an array. ü ü ü ü Print. List Make. Empty Find Insert Delete Next Previous

Simple Array Impl. of Lists Disadvantages: n n n Number of elements to be

Simple Array Impl. of Lists Disadvantages: n n n Number of elements to be stored in an array should be known in advance Chances of memory wastage or shortage Since elements of array are stored in consecutive memory locations Insertion and deletion are expensive. Because the running time for insertions and deletions is so slow and the list size must be known in advance, simple arrays are generally not used to implement lists.

Linked Lists n Linked list consists of a chain of elements, in which each

Linked Lists n Linked list consists of a chain of elements, in which each element is referred as a node. n A node is a basic building block of a linked list. n A node consists of two parts n Data : Refers to the information hold by the node n Next : Contains the address of next node in the List A 1 800 1000 A 2 712 800 A 3 992 712 A 4 692 992 A 5 0 692

Linked Lists n n Linked list is a data structure which contains list of

Linked Lists n n Linked list is a data structure which contains list of nodes which are not necessarily adjacent in memory. Each node contains the element and a pointer to a node containing its successor. We call this as the Next pointer. n The last Node’s Next pointer points to NULL; n Linked lists are used when number of data is not known in prior. A 1 A 2 A 3 A 4 A 5

Linked Lists - Operations n n To execute Print. List(L) or Find(L, Key), we

Linked Lists - Operations n n To execute Print. List(L) or Find(L, Key), we merely pass a pointer to the first element in the list and then traverse the list by following the Next pointers. The Delete command can be executed in one pointer change. A 1 A 2 A 3 A 4 A 5

Linked Lists - Operations n A 1 The Insert command requires obtaining a new

Linked Lists - Operations n A 1 The Insert command requires obtaining a new cell from the system by using a malloc call and then executing two pointer modifications A 2 A 3 X A 4 A 5

Linked Lists - Issues (1) There is no really obvious way to insert at

Linked Lists - Issues (1) There is no really obvious way to insert at the front of the list from the definitions given; (2) Deleting from the front of the list is a special case, because it changes the start of the list; careless coding will lose the list; (3) A third problem concerns deletion in general. Although the pointer moves above are simple, the deletion algorithm requires us to keep track of the cell before the one that we want to delete.

Linked Lists n One simple change solves all problems. We will keep a sentinel

Linked Lists n One simple change solves all problems. We will keep a sentinel node, referred to an a header or dummy or sentinel node. Header A 1 A 2 A 3 A 4 A 5 L n n To avoid the problems associated with deletions, we need to write a routing Find. Previous, which will return the position of the predecessor of the cell we wish to delete. If we use a header, then if we wish to delete the first element in the list, Find. Previous will return the position of the header.

Problems with LL & Alternatives n n LL allows traversal of the list in

Problems with LL & Alternatives n n LL allows traversal of the list in only one direction Deleting a node from the list requires keeping track of previous node. If the link in any node gets corrupted the remaining nodes of the list becomes inaccessible and unusable. Alternatives n Doubly Linked List n Circular Linked List