Download presentation
Presentation is loading. Please wait.
1
Data Structures and Algorithms Introduction
2
Data Structures 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
3
Role of Algorithms An algorithm is
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.
4
Role of Algorithms Some Well-known Computational Problems
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 Input : A sequence of n numbers (a1,a2,a3…an) Output : A permutation (a1’,a2’,a3’,….an’) of the input sequnce such that a1’<a2’<….an’. Example : Input : (31,41,59,26,41,58) – instance Output : (26,31,41,41,58,59) - solution
5
Role of Algorithms Efficient Sorting algorithm depends on
Number of items to be sorted Extent to which items are already somewhat sorted Possible restrictions on item values Architecture of computer Kind of storage devices to be used : main memory, disk , tapes
6
Role of Algorithms Applications of Algorithms
The Human Genome Project – Algorithms to identify all the 100,000 genes in human DNA, determine the sequences of the 3 billion chemical base pairs that make up human DNA, store this information in databases, and developing tools for data analysis. Internet – Fast Access and retrieve large data To find good routes for data travel using a search engine to quickly find pages on which particular information resides
7
Role of Algorithms Applications of Algorithms
3. Elecctronic Commerce - Needs public-key cryptography and digital signatures which are based on numerical algorithms and number theory. 4. Manufacturing and other commercial enterprises data oil company - wish to know whereto place its wells in order to maximize its expected profit. political candidate - want to determine where to spend money buying campaign advertising in order to maximize the chances of winning an election.
8
Role of Algorithms 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 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. routing node on the Internet may need to find the shortest path through the network in order to route a message quickly.
9
Role of Algorithms Np complete Problems
Problems for which no efficient solution is known Interesting Facts 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
10
Algorithms as a technology
computers may be fast, but they are not infinitely fast. Memory may be inexpensive, but it is not free. 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.
11
Algorithms as a technology
Time complexity 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 A measure of the amount of working storage an algorithm needs with respect to the input size.
12
Algorithms as a technology
Asymptotic Notations 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 − Best Case − Minimum time required for program execution. Average Case − Average time required for program execution. Worst Case − Maximum time required for program execution.
13
Algorithms as a technology
Asymptotic Notations Upper Bounds: Big-Oh Notation (O) 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 - Ω 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.
14
Algorithms as a technology
Tight Bounds: Theta – Θ Formal way to express both the lower bound and the upper bound of an algorithm's running time Common Asymptotic Notations Constant - Ο(1) Logarithmic − Ο(log n) Linear − Ο(n) n log n − Ο(n logn) Quadratic − Ο(n2) Cubic − Ο(n3) Polynomial − nΟ(1) Exponential − 2Ο(n)
15
Modular Programming 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. 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.
16
Abstract Data Types (ADTs)
An abstract data type (ADT) is a set of operations. Abstract data types are mathematical abstractions; 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.
17
Abstract Data Types (ADTs)
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.
18
The List ADT The form of a general list: A1, A2, A3, …, AN;
The size of this list is N; An empty list is a special list of size 0; 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 A1, and the last element is AN. We will not define the predecessor of A1 or the successor of AN. The position of element Ai in a list is i.
19
The List ADT set of operations on the list ADT: PrintList MakeEmpty
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 FindKth: return the element in some position Next and Previous: take a position as argument and return the position of the successor and predecessor
20
The List ADT Example: The list is 34, 12, 52, 16, 13b Find(52)
Insert(X, 3) Delete(52) The interpretation of what is appropriate for a function is entirely up to the programmer.
21
Simple Array Implementation of Lists
All these functions about lists can be implemented by using an array. PrintList MakeEmpty Find Insert Delete Next Previous
22
Simple Array Impl. of Lists
Disadvantages: 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.
23
Linked Lists Linked list consists of a chain of elements, in which each element is referred as a node. A node is a basic building block of a linked list. A node consists of two parts Data : Refers to the information hold by the node Next : Contains the address of next node in the List A1 A2 A3 A4 A5 800 712 992 692 1000
24
Linked Lists 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. The last Node’s Next pointer points to NULL; Linked lists are used when number of data is not known in prior. A1 A2 A3 A4 A5
25
Linked Lists - Operations
To execute PrintList(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. A1 A2 A3 A4 A5
26
Linked Lists - Operations
The Insert command requires obtaining a new cell from the system by using a malloc call and then executing two pointer modifications A1 A2 A3 A4 A5 X
27
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.
28
Linked Lists One simple change solves all problems. We will keep a sentinel node, referred to an a header or dummy or sentinel node. A1 A2 A3 A4 A5 Header L To avoid the problems associated with deletions, we need to write a routing FindPrevious, 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, FindPrevious will return the position of the header.
29
Problems with LL & Alternatives
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 Doubly Linked List Circular Linked List
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.