Está en la página 1de 8

ASSIGNMENT 4 CSE 205 NAME: DEWANK SAINI CLASS: B.

TECH (CSE) SECTION: F1902 ROLL NO: RF1902B46

Part-A
1. Discuss the significance of various shortest path algorithms and make a comparison of the complexities of those algorithms. Ans. The time complexity of various sorting and searching algorithms are: Sorting: Sorting Technique Worst Case Average Case Bubble SortO ( n2) N*(N-1)/2 = O (n2 ) Insertion Sort O ( n2) N*(N-1)/2 = O (n2 ) Selection Sort O ( n2) N*(N-1)/2 = O (n2 ) Merge Sort O(n log n) O ( n log n) Heap Sort O (n log n) O (n log n) Quick Sort O (n2) O(n log n) Remember Merge Sort uses extra memory with the complexity of O (n) due to the extra array used. Now Quick Sort has a worst case complexity of O (n2) where the worst case is that the data is reverse sorted or sorted. However if we assume the partition of array to be in the ratio 1/10:9/10 then the running time is nearly equal to average case complexity of O (n log n) i.e. for all the practical cases complexity of Quick Sort is O(n log n). Searching: Searching Technique Worst Case Average Case Case Linear Search O (n) O (n) O (1) Binary Search O (log n) O (log n) O (1) Best

Selection of sorting algorithms depends on the particular problems for e.g. if the number of elements to be sorted are less than we can apply Insertion Sort or Selection Sort. Quick Sort is generally used for large input data due to its following properties: 1.) One of the fastest sorting algorithms 2.) No extra memory required. However its disadvantages are that the speed is guaranteed. Quick Sort is generally used to sort the locations in main memory. Merge Sort guarantees speed but requires extra memory hence if we are required to save the memory we shouldnt use merge sort.

Merge Sort is generally used to sort the data laying in the external memory where we have ample of extra space. Heap Sort is used particularly in the implementation of priority queue where the ones with highest priority (uses a Max Heap) get finished first. In searching algorithms we generally prefer binary search until there is a point where we cannot use it for e.g. in a Linked List we cant use binary search we have to make do with linear search. 2. Make a comparison of the various searching techniques and discuss the significance of the Hashing Functions. Ans. Linear Search 1.) Has a worst case running time of O (n). 2.) Has a Best case Running time of O (1) 3.) Has an average case complexity of O(n) 4.) Data may or may not be sorted. 5.) Can be used in any situation. 6.) Can be used in a Data Structure like Linked List where random access is not possible. Binary Search 1.) Has a worst case running time of O (log n). 2.) Has a Best case Running time of O (1) 3.) Has an average case complexity of O (log n). 4.) Data should be already sorted. 5.) Can only be used if we have direct access to every element. 6.) Cannot be used in a Linked List and its derivative Data structures since random access is not possible. Hashing functions are a significant part of effective searching. In practical or real situations we do not sort just random numbers or strings but we rather sort records in a file. Now let us assume a file F has n records and has K keys. Here keys are the primary keys which are distinct from each other and unique for every record R. Now we want to save the records into the Table T containing M memory locations. Now if we could represent the keys in such a way that they give us the memory location of the record in the table T then we can search sort and insert in constant time. This is the basis of hashing function i.e. it is a function H which maps K to L where L is a memory location in the Table T. There are generally 3 methods to map K to L. They are:1.) Divide. 2.) Mid Square Method.

3.) Folding. 3. Give a comparison between the different ways in which the Graphs can be implemented. Ans. 1.) Graphs specifically directed graphs could be used to show the linked structure of a web page. Since in a directed graph we have a directed edge from one node to another hence we can use that to represent whether or not one page is hyperlinked to another or not. 2.) Graphs can be used to represent a network along with its topology i.e. the nodes of the graph can act as network devices and the edges as the physical link of communication. 3.) Graphs can be used to represent circuits where the nodes represent the electronic devices such as gates etc. and edges represent the links between them. 4.) Weighted graphs can be used to find out the shortest paths between any two nodes which solve many problems in networks where we have to send the data in least time or in circuit analysis where the graph must occupy least amount of space.
4. Discuss the application of the Algorithms Breadth First search

and Depth First Search; also make a comparative study on both of them. Ans. Applications of Breadth First Search: 1.) Helps us to find all the nodes in one connected graph. 2.) Helps to find the shortest path between any two nodes of the graph. 3.) Help us to determine whether a graph is bi partite or not. 4.) Used in Garbage Collection by programming languages. (Cheneys Algorithm) Applications of Depth First Search: 1.) Helps us find the connected components of a graph. 2.) Used in Topological Sorting of vertices of a graph. 3.) Finding strongly connected components of a graph. The algorithms of Depth first Searching and Breadth First Searching are quite similar in mechanism. However the major difference lies in the fact that Depth first Search uses a stack in its implementation while a Breadth first search uses a queue. In Breadth First Search we process a starting node A and then process all the neighbors of A and the all the neighbors of neighbors of A. We assign a status bit to each of the node which ranges from 1 READY, 2 PROCESSING, 3 PROCESSED. We de queue a node from the front of

the queue and enqueue all of its successors at the end of the queue and change their Status to 2 and the status of the de queued node to 3 and we repeat the process until queue is empty. In Depth First Search however we choose a starting node A and choose a path P originating at A and move along it and if we come across a node we change its Status to 2 and push it onto the stack. We do this until we hit a dead end i.e. end of the path. Then we start backtracking (Popping from the stack) until we have an alternate path to move along. This way we traverse whole of the graph depth first.

Part-B
5. If you have been given a task to do some project and to do the basic operations like searching and sorting there are many algorithms, which algorithm you will select in which circumstances and why, discuss in detail Ans. The type of the algorithms to be chosen depend on the specific problem for e.g. if our implementation uses a linked list data structure or its derivatives then we are limited to linear search. However for every other case where elements are randomly accessible binary search will work. Regarding sorting, the matter is much more complex and dynamic. If my data is in the form of a tree in general and a heap in particular then it makes sense to use Heap Sort. However if the data is being stored on a secondary storage device for e.g. as a database which contains several files which in turn contain several records like if the entries in the dictionary are stored in a file and we have to sort them alphabetically then Merge sort is a good idea since the extra space required in it will be available easily on the secondary device. However if we have something which we have to sort in place without using extra space and it is going to be stored in the main memory i.e. we do not store the data permanently we just have to sort them then Quick Sort is a good mechanism as it has speed, doesnt consume extra memory like Merge Sort. However all of this really matters when the size of the input is very high. If the input size if sufficiently low then the simpler sorting methods like Selection Sort and Insertion Sort will make do. 6. Discuss in detail that how the selection of the data structure impacts the quality of any searching or sorting algorithm.

Ans. If we select stack than we implement depth first search method and if we consider a queue than we implement breadth first search. Also searching is done in arrays and linked lists. So the quality of any searching or sorting algorithm depends on the daa structure used. 7. Discuss the application areas where the Hashing Functions can be applied, with the help of an example. Ans. Application : 1. Division method:
Perhaps the simplest of all the methods of hashing an integer x is to divide x by M and then to use the remainder modulo M. This is called the division method of hashing . In this case, the hash function is

Generally, this approach is quite good for just about any value of M. However, in certain situations some extra care is needed in the selection of a suitable value for M. For example, it is often convenient to make M an even number. But this means that h(x) is even if x is even; and h(x) is odd if x is odd. If all possible keys are equiprobable, then this is not a problem. The division method is extremely simple to implement. The following C++ code illustrates how to do it:
unsigned int const M = 1031; // a prime unsigned int h (unsigned int x) { return x % M; }

In this case, M is a constant. However, an advantage of the division method is that M need not be a compile-time constant--its value can be determined at run time. In any event, the running time of this implementation is clearly a constant. 2. Middle square method : In this section we consider a hashing method which avoids the use of division. Since integer division is usually slower than integer multiplication, by avoiding division we can potentially improve the running time of the hashing algorithm. We can avoid division by making use of the fact that a computer does finite-precision integer arithmetic. E.g., all arithmetic is done modulo W where is a power of two such that w is the word size of the computer.

The middle-square hashing method works as follows. First, we assume that M is a power of two, say . Then, to hash an integer x, we use the following hash function:

The following code fragment illustrates the middle-square method of hashing:


unsigned int const k = 10; // M==1024 unsigned int const w = bitsizeof (unsigned int); unsigned int h (unsigned int x) { return (x * x) >> (w - k); }

Since x is an unsigned int, the product x * x is also an an unsigned int. If 32-bit integers are used, the product is also a 32-bit integer. The final result is obtained by shifting the product w-k bits to the right, wherew is the number of bits in an integer. By definition, the right shift inserts zeroes on the left. Therefore, the result always falls between 0 and M-1.
3. Folding method :

a) break key up into binary segments (ASCII) b) XOR these together c) Calculate the numeric integer equivalent

8. Write a program in C++ to sort the strings of characters using

Selection Sort Algorithm. Ans. #include<stdlib.h> #include<iostream> #include<conio.h> #include<string.h> #define MAX 100 using namespace std; int main() { char a[MAX][MAX]; int size,pos;

cout<<"Type the number of names"<<endl; cin>>size; for(int i=0;i<size;i++) scanf("%s",a[i]); for(int i=0;i<size;i++) { char string1[MAX]; strcpy(string1,a[i]); char temp[MAX]; strcpy(temp,a[i]); for(int j=i;j<size;j++) { if(strcmp(string1,a[j])>0) { strcpy(string1,a[j]); pos=j; } if(strcmp(temp,string1)!=0) { strcpy(a[i],string1); strcpy(a[pos],temp); } } } for(int i=0;i<size;i++) { int len=strlen(a[i]); for(int j=0;j<len;j++) cout<<a[i][j]; } getch(); }

También podría gustarte