Unit - Ii
Unit - Ii
Introduction The General Method Binary Search Finding the Maximum and Minimum Merge Sort Quick Port Heap Sort Strassens Matrix Multiplication. The General Method Knapsack Problem Minimum Spanning Tree Optimal Storage on Tapes Single Source Shortest path
2.0 Introduction
This chapter deals with the concepts and different types of sorting. Sorting is one of the techniques used to arrange data in an order. There are basically two types of sorting - internal sort and external sort. In this chapter, various types of sorting techniques based on Divide and Conquer method are dealt in detail.
2.1 Objective
The objective of this lesson is giving the skill about the sorting concept. The content of this lesson starts sorting and deals with different sorting techniques and their algorithms. It gives the clear idea and knowledge about internal sorting and external sorting. At end of this lesson youll able to sharp your skill and knowledge about Strassens matrix, Knapsack problem and single source shortest path.
Page 37
Solve (I) N=size(I); If (n smallsize) Solution=directlysolve(I); Else divide I into I1..Ik for each i [1..k}; Si=solve(i); solution=combine(S1..Sk); return solution; With the base case T(n)=B(n) for n smallsize. For many DAC algorithm either the divide is step or the combine step is very simple and the recurrence equation for T is simpler than the general form. First decide how to partition the list into sublist after they are sorted, how to combine the sublist into one. There are various methods available that are given below: Binary Search Straight MaxMin Recursive Algorithm Merge Sort Quick Sort Heap Sort
2.2.2Binary Search
A binary search tree is a binary tree in which the nodes are labeled as an element in a set or equivalent value. The major property of the binary tree is that all elements stored in the left subtree of any node x are all less than the element stored at x and all the elements stored at right subtree of x greater than the elements stored at x. The data in an array is sorted in increasing numerical order or equivalently alphabetically. Then there is an extremely efficient searching algorithm called binary search, which can be used to find the location of a given data. This approach requires that the entries in the list be of a scalar or other type and that the list is already in order. Order list is a list in which the entry contains a key such that the keys are in order. That is if entry I comes before J in the list then the key entry I is less than or equal to the entry J. The operation of order list includes all those for ordinary list. For example consider searching of an element in an array. Suppose one is searching the data 30 in the array of size 170. One will search at random towards the later half of the array. If the array value is less than 30 one will search at left; if it is greater, search at right. Repeat the process until 30 was found. Binary search requires the sorted data to operate on. It cannot be guessed which division has the data item required so, divide the list into two
Page 38
The above table has sorted list of size 11. The first comparison is with the middle Element index value 6. that is, Leo. This eliminates the first 5 data elements in the table, then the second comparison is with the middle index from 7 to 11 that is, 9 Sagittarius, this search removes 7 to 9 index values. The third comparison is with middle element from 9 to 11. that is, 10 Scorpio. Finally find the search value in the third comparison. If this value search is done in sequential search in takes 10 comparisons. The algorithm of binary search is given below. Procedure binary search This algorithm represents the binary search method to find a required item in a sorted list in ascending order. Input: sorted list of size N, Target Value T Output: Position of T in the List=I Algorithm binsearch(t,n,I) Var max,min,mid:integer; Begin Max=n min=1 Found:=false; Repeat mid=(min + max) div 2 if (T<list[mid]) then max=mid-1 else if (t>mid[list]) min=mid+1 else begin I=mid Found =true End if Until( found or (max<min)) Page 39
1 0
1 2
1 4
Figure 2.1 Binary decision tree for binary search, n=15 In binary search method always the key in the middle of the list currently being examined is used for comparison. The splitting of the list can be exemplified through a binary decision tree in which the value of a node is the index of the key that is being tested. Suppose there are 14 nodes then the first key is compared with the location 7. If the key is less than the key at location 3 and is greater than the key, location 11 is tested. The process of binary tree diagram is shown in figure 2.1 of having 14 nodes.
1,9,22,47 5 1,5,22,15
3 4 6 8
6,9,60,47
7
1,3,22,-5
1
4,5,-8,15
2
6,7,60,17
8,9,31,47
1,2,22,13
2,3,-5,-5
Figure 2.2 for tree Recursive calls MaxMin A better way of keeping track of recursive calls is to build a tree by adding a node each time a new call is made. For this algorithm each has four items of information. The root node contains 1, 9 that is values of i and j are initialized to max and min. The execution of the procedure produces the two new calls to MaxMin, where i and j have the values 1,5 and 6,9 respectively and it splits the subset into two subsets of approximately equal size. From the above figure it is known that the circled numbers the upper left corner of each node represent the orders in which max and min values are assigned.
2.2.4Merge Sort
The most common algorithm used in an external sorting that is for the problem in which data is stored in disks or magnetic tapes. Merge sort is an excellent sorting method. This technique is used to sort the files in the following way. Divide the files into two equal sized sub files and the sort the sub files separately, then merge the sorted files into one. Algorithm MergeSort(l,h) //a[l:h] is a gloabal array to be sorted //small(p) is true if there is only one element sort. //In this case the list already sorted. // if there are more than one element Divide p into subproblem { if (l<h)
Page 42
Sorted
Figure 2.3 for merge strategy The A array values are given below that is 25, 57, 49, 36, 13, 98, 80, 30 In the first stage divide the array value into equal size a1 =25,49,13,80. And a2=57,36,98,30 and compare the equal index values of both arrays. Comparison operation stages are given below: Original Files I st Stage II nd Stage III rd Stage [25] [25 [57] 57] [49] [49 [36] 36] [13] [98] [80] [80 [30] 30]
[13 98]
[25 [13
36 25
49 30
57] 36
[13 49
30 57
80 80
90] 90]
Page 43
2.2.5Quick Sort
Quick sort is widely used for internal sorting method. It was invented by C.A.R.Hoare in 1960. Its popularity is based on the easy implementation, moderate use of the resources and acceptable and variety of sorting cases. The basis of quicksort is the divide and conquers strategy. That is, divide the problem into sub problems until the solved subproblems sorted are found. In the quick sort the array is divided into two sub arrays, so that the sorted subarrays do not need to be merged later. In this sorting, the list is partitioned into lower and upper sublists. This method moves data in the correct direction just sufficient for it to arrive at final place in the array, therefore it reduces the unnecessary swaps and puts the item in great distance in one move. The first item in the array is called pivot. Partition the entries so that all those keys less than the pivot come one side (sublist) and all those with greater than key on other sublist. This procedure is applied recursively on either parts of the array until the whole array is sorted. This mechanism is applied in array A using the follow steps. 1. Remove the first data item in the list and call that item as A1, mark the position of the item A1 and scan the entire array from right to left, comparing the data items values with A1 value. When the first smallest value is found, remove it from its current position and put in position a(1). 2. Scan the line from left to right beginning with array a(2) position compare data item value with A1. When you find the a(n+1) value is greater than the A1 value, extract it and store in the position marked by parenthesis. 3. Beginning from right to left, scan the line of value a(n-2) looking for a value smaller than A1. When it is found, extract it and store it in the position marked by parenthesis. 4. Begin the scan from left to right start with value a(3) find a value greater than A1 value, remove it, mark its position and store it inside the parentheses. 5. Now scan the line from right to left; start with position a(n-3) when no value is smaller than A1 value, come to the parenthesis position put the A1 value in a[5]. Finally observe that the first value is put in the middle location and all the values in the left of A1 are less than A1 value and all the values to the right side are greater than A1. This process can now apply recursively to second segment of the array on the left and right of A1 value. Page 44
Procedure of the Quicksort is given below: Quicksort(int a[],int x, int i) {int l,r,v; if (i>x) {v=a[i], l=x-1,r=1; for(;;) { while(a[++l]>v); while(a[--r]<v); if (l==r) break; swap(a,l,r) } swap(a,l,i) quicksort(a,x,l-1) quicksort(a,l+1,i) } } The average run time stack efficiency of the quick sort o(n(log2n)) is the best one that has been achieved by large array of size n. In worst case the array is already sorted, the efficiency of the quicksort may drop down to o(n2) due to the right to left scan all the way to the last left boundary. The performance can be improved by keeping in mind the following tips: 1. Switch to a faster sorting scheme like insertion sort when the sublist size becomes comparatively small. 2. Use a better dividing element I in the implementation.
Page 45
Page 46
Page 47
1 1 5 1 1 2 6 1 0 2 1 7
The diagrammatic structure of the heap sort basic diagram is given above using II phase. Swap 6 & 10
1 1 5 1 1 0 0 4 6 2 1 2 1 7
7 8 Swap 7 & 8
1 1 5 1 1 2 6 4 1 0 2 Page 48 1 7
7 8
swap 5 & 17
1 1 5 1 1 0 4 6 2 1 2 1 7
8 7
swap 1 & 10 1 1 1 1 0 4 6 2 1 7 1 2 5
8 7
Page 49
Swap 1 & 6
Phase I activated
Swap 11 & 17
Page 50
Swap 12 & 11
Swap 2 & 17
Swap 2 & 12
Swap 2 & 11
Swap 1 & 12
Page 51
17
swap 1 & 5
swap 4 & 11
Second phase activated The diagrammatic representation of the heap sort is given above. In general, the heap sort does not perform better than the quick sort. Only when the array is nearly sorted to begin with does the heap sort algorithm gain an advantage.
Page 52
A12 A22
---- (2.9)
--(2.10)
If n=2, then formulas (2.9) and (2.10) are computed using a multiplication operation for the elements of A and B. These elements are typically floating point numbers. For n>2, the elements of C can be computed using matrix multiplication and addition operations applied to matrices of size n/2Xn/2. Since n is a power of 2, these matrix products can be recursively computed by the same algorithm we are using for the n X n case. This algorithm will continue applying itself to smaller-sized sub matrices until n becomes suitably small(n=2) so that the product is computed directly. To compute AB using (2.10) we need to perform eight multiplications of n/2 x n/2 matrices and four additions of n/2 x n/2 matrices. Since two n/2 x n/2 matrices can be
Page 53
n 2
This recurrence can be solved in the same way as earlier recurrences to obtain T(n) = O(n3). Hence no improvement over the conventional method has been made. Since matrix multiplications are more expensive than matrix additions (O(n3) versus O(n2)), one can attempt to reformulate the equations for Cij so as to have fewer multiplications and possibly more additions. Volker strassen has discovered a way to compute the Cijs of (2.10) using only seven multiplications and 18 additions or subtractions. His method involves first computing the seven n/2 x n/2 matrices P,Q,R,S,T,U and V can be computed using seven matrix multiplications and 10 matrix additions or subtractions. The Cijs require an additional 8 additions or subtractions. P Q R S T U V = = = = = = = (A11+A22)(B11+B22) (A21+A22)B11 A11(B12-B22) A22(B21-B11) (A11+A12)B22 (A21-A11)(B11+B12) (A12-A22)(B21+B22)
--(2.11)
C11 = P+S-T+V C12 = R+T C21 = Q+S C22 = P+R-Q+U The resulting recurrence relation for T(n) is b T(n)= 7T(n/2)+an
2
--(2.12)
n 2 n>2
--(2.13)
where a and b are constants. Working with this formula, we get T(n) = = an2[1+7/4+(7/4)2+.+(7/4)k-1] + 7kT(1) cn2(7/4)log2n + 7 log2n, c a constant cnlog24+log27-log24+ nlog27
Page 54
The Greedy Algorithm Introduction This chapter deals with the general greedy method which is a straightforward design technique. In optimization problems the algorithms need to make a different choice and those overall results is best according to some limited short-term criterion that is not too expensive to calculate. This chapter presents the algorithm for finding minimum spanning tree in an undirected graph according to R.C. Prim; a more closely related algorithm is for finding single source shortest path in directed and undirected graph as per E.W .Dijkistra, and a second algorithm for finding minimum spanning tree, according to J.B. Kruskal. All the above three-algorithm use the priority queues. 2.2.8 The General Method
This method has several algorithms namely, Dijkstras, Kruskals etc. For any individual stage greedy algorithm selects that option, which is locally optimal in some particular sense, for making changes, produces an overall optimal solution only because of special properties. Feasible solution: - some of the problems have n inputs and require us to obtain a subset that satisfies some constraints; any subset that satisfies these constraint is called feasible solution. Object function: - one needs to find a feasible solution that either maximizes or minimizes a given problem. Optimal solution: - A feasible solution that does this is called as optimal solution. One usually requires a feasible solution but not necessarily an optimal solution. In this method the algorithm work is planned in stages, considering one input at a time. At each stage a decision is made regarding whether a specific input is optimal or not. Considering the inputs in an order determined by some selection procedure does this. The next input is included into the partially constructed optimal solution only if it does not result in an infeasible solution. The selection procedure itself is based on some optimization measure, which may be the objective function. In fact, several different optimization measures may be possible for a given problem and these will result in algorithms that generate suboptimal solutions. This version of the greedy technique is called the subset paradigm. Algorithm 3.1 explains the greedy method control abstraction for the subset paradigm.
Page 55
The greedy method can be applied to solve the knapsack problem. Let us consider n objects. Object i has a weight wi and the knapsack has a capacity m. If a fraction x i, 0 xi 1, of object i is placed into the knapsack, then a profit of pixi is earned. The objective is to obtain a filling of the knapsack that maximizes the total profit earned. Since the knapsack capacity is m, it is required that the total weight of all chosen objects to be at most m. Formally, the problem can be stated as maximize pixi 1 i n subject to wixi m 1 i n and 0 xi 1, 1 i n The profits and weights are positive numbers. A feasible solution is any set(x1,,xn) satisfying (2.2) and (2.3) above. An optimal solution is a feasible solution for which (2.1) is maximized. Example 2.1: --(2.1)
--(2.2)
--(2.3)
Page 56
Page 57
Algorithm GreedyKnapsack(m,n) //p[1:n] and w[1:n] contain the profits and weights //respectively of the n objects ordered such that //p[i]/w[i] p[i+1]/w[i+1]. m is the knapsack size //and x[1:n] is the solution vector. { for i:= 1 to n do x[i]:=0.0; //Initialize x. U:=m; for i:=1 to n do { if (w[i]>U)then break; x[i]:=1.0; U:=U-w[i]; } if (i n) then x[i]:= U/w[i]; } Algorithm 2.2: Algorithm for greedy strategies for the knapsack problem Theorem 2.1: If p1/w1 p2/w2 .. pn/wn. Then Greedy Knapsack generates an optimal solution to the given instance of the knapsack problem.
Proof:
Let x=(x1,,xn)be the solution generated by Greedy Knapsack. If all the xi equal one, then clearly the solution is optimal. so, let j be the least index such that xj 1. From the algorithm it follows that xi=1 for 0 i 1. Let y=(y1,,yn) be an optimal solution. From Lemma 2.2, one can assume that wiyi=m. Let k be the least index such that yk xk. Clearly, such a k must exist. It also follows that yk<xk. To see this, consider the three possibilities k<j, k=j, or k>j. 1. If k<j, then xk=1. But, yk xk, and so yk<xk. 2. If k=j, then since wixi=m and yi=xi for 1 i j, Page 58
Now suppose one increases yk to xk and decrease as many of (yk+1,,yn) as necessary so that the total capacity used is still m. This results in a new solution z=(z1, ,zn) with zi=xi, 1 i k, and k<i n wi(yi-zi)-wk(zk-yk). Then, for z we have
p z
i i
p y +(z -y )w
i i k k
1 i n
1 i k
piyi+ 1 i k p y
1 i k
i i
(yi-zi)wi k<i n
pk/wk
if pizi > piyi, then y could not have been an optimal solution. If these sums are equal, then either z=x and x is optimal or z x. In the latter case, repeated use of the above argument will either show that y is not optimal or transform y into x and thus show that x too is optimal. 2.2.10 Minimum-Cost Spanning Trees Definition: A spanning tree for a connected, undirected graph, G=(V,E) is a sub graph of G that is an undirected tree and contains all the vertices of G. In a weighted graph G =(V,E,W), the weight of a subgraph is the sum of the weights of the edges in the subgraph. A Minimum spanning tree for a weighted graph is a spanning tree with minimum weight. Applications of Spanning tree a. To find the cheapest way to connect a set of terminals (like cities, computers, factories by using roads, wires or telephone lines) MST for the graph wit an edge for each possible connection weighted by the cost of that connection is used. b. MST is used in routing algorithms, that is, for finding efficient paths through a graph that visits every vertex. Example: Figure (3.1) shows the complete graph on four nodes together with three of its spanning trees.
Page 59
Figure 2.5 An undirected graph and three of its spanning trees Another application of spanning trees arises from the property that a spanning tree is a minimal subgraph G of G such that V(G)=V(G) and G is connected. Any connected graph with n vertices must have at least n-1 edges and all connected graphs with n-1 edges are trees. If the nodes of G represent cities and the edges represent possible communication links connecting two cities, then the minimum number of links needed to connect the n cities is n-1. The spanning trees of G represent all feasible choices. In practical situations, the edges have weights assigned to them. These weights may represent the cost of construction, the length of the link and so on. Given such a weighted graph, one would then wish to select cities to have minimum total cost or minimum total length. In either case the links selected have to form a tree. If this is not so, then the selection of links contains a cycle. Removal of any one of the links on this cycle results in a link selection of less cost connecting all cities. One is therefore interested in finding a spanning tree of G with minimum cost. Figure (2.6) shows a graph and one of its minimum-cost spanning trees. Since the identification of a minimum-cost spanning tree involves the selection of a subset of the edges, this problem fits the subset paradigm.
28 10
1 2 14 6 7 Page 60 16 3
1 10 14 16 6 25 5 22 4 12 7 3 (b) 2
Figure 2.6 (a) and (b): A graph and its minimum cost spanning tree
Prims Algorithm A Greedy method to obtain a minimum-cost spanning tree builds this tree edge by edge. The next edge to include is chosen according to some optimization criterion. The simplest such criterion is to choose an edge that results in a minimum increase in the sum of the costs of the edges so far included. There are two possible ways to interpret this criterion. In the first, the set of edges so far selected form a tree. The next edge (u,v) to be included in A is a minimum-cost edge not in A with the property that AU{(u,v)} is also a tree. The corresponding algorithm is known as Prims algorithm. Example Figure (2.7) shows the working of Prims method on the graph of Figure (2.6(a)). The spanning tree obtained is shown in Figure (2.6(b)) and has a cost of 99. Having seen how Prims method works, let us obtain a pseudo code algorithm to find a minimum-cost spanning tree using this method. The algorithm will start with a tree that includes only a minimum-cost edge of G. Then, edges are added to this tree one by one. The next edge (i,j) to be added is such that i is a vertex already included in the tree, j Page 61
(a)
5 4
(6,5) = 25 10
1 2
6 25
(b)
Page 62
5 4
(5,7) = 24 (5,4) = 22 10 6 25 5 22
1 2
(c)
(4,3) = 12 10
1 2
6 25
(d)
12 5 22 4
(3,2) = 16 10
1 2 16 6 7 3 (e)
Page 63
1 (2, 7) = 14 10 14 6 25 12 5 22 4 7 16 3 (f) 2
Figure 2.7: Stages in Prims algorithm If ones stores the nodes not yet included in the tree as a red-black tree, Lines 18 and 19 take O(log n) time. The for loop of line 23 has to examine only the nodes adjacent to j. Thus its overall frequency is O(|E|). Updating in lines 24 and 25 also takes O(log n) time. Thus the overall run time is O((n+|E|) log n). One can also start the algorithm with a tree consisting of any arbitrary vertex and no edge. Then edges can be added one by one. The changes needed are in lines 9 to 17. The following lines can replace these lines. mincost := 0; for i:=2 to n do near[i] := 1; //Vertex 1 is initially in t. near[1]:=0; for i := 1 to n-1 do { // Find n-1 edges for t, Algorithm Prim(E,cost,n,t) //E is the set of edges in G. // cost[1:n,1:n] is the cost adjacency matrix of an n vertex graph // such that cost[i,j] is either a positive real number //or if no edge (i,j) exists. //A minimum spanning tree is computed and stored as a set of edges
Page 64
Page 65
6 5 Spanning Tree 4
The edges of the graph are considered for inclusion in the minimum cost spanning tree in the order (2, 3), (2, 4), (3, 4), (2, 6), (4, 6), (1, 2), (1, 2), (4, 5), (1, 5), (1, 6) and (5, 6). They are taken in the ascending order of their associated costs. The tree so constructed is called T, which is built edge by edge. The collection of nodes are {1, 2, 3, 4, 5, 6}.
Page 66
(2, 3) (2, 4)
5 6
(3, 4) (2, 6)
10 11
Rejected Accepted
(4, 6) (1, 2)
14 16
Rejected Accepted
(4, 5)
18
Accepted
6 5 4 (1, 5) and (1, 6) are also rejected because they too will form a cycle.
t:= ; while ((t has less than n-1 edges) and (E ))do { Choose an edge (v,w) from E of lowest cost; Delete (v,w) from E; if (v,w) does not create a cycle in t then add(v,w) to t; else discard (v,w); }
Algorithm 2.4: Early form of minimum-cost spanning tree algorithm due to Kruskal Algorithm Kruskal(E,cost,n,t) //E is the set of edges in G. G has n vertices. //Cost[u,v] is the cost of edge(u,v). t is the set //of edges in the minimum-cost spanning tree. The //final cost is returned. Page 67
Page 68
(a) 8
a
6
6 8 11
b
(b) a 6 8 9 b (c) Figure 2.8: A Boruvka step 2.2.11 Optimal Storage on Tapes There are n programs that are to be stored on a computer tape of length l. Associated with each program i is a length li, 1 i n. Clearly, all programs can be stored on the tape if and only if the sum of the lengths of the programs is at most l. One assumes that whenever a program is to be retrieved from this tape, the tape is initially positioned at the front. Hence, if the programs are stored in the order I=i1,i2,,in, the time tj needed to retrieve program ij is proportional to 1 k j lik. If all programs are retrieved equally often, then the expected or mean retrieval time (MRT) is (1/n) 1 j ntj. In the optimal storage on tape problem, it is required to find a permutation for the n programs so that when they are stored on the tape in this order the MRT is minimized. This problem fits the ordering paradigm. Minimizing the MRT is equivalent to minimizing d(I)= 1 j n1 k jlik. Following theorem shows that the MRT is minimized when programs are stored in this order. Theorem: If l1 l2 . ln, then the ordering ij=j,1 k j, minimizes n k lij Page 70 c
k=1
if there exist a and b such that a<b and lia>lib, then interchanging ia and ib results in a permutation I with
d(I) = (n-k+1)lik + (n-a+1)lib+(n-b+1)lia k k a k b Subtracting d(I) from d(I), one obtains d(I)-d(I) = (n-a+1)(lia-lib) + (n-b+1)(lib-lia) = (b-a)(lia-lib) > 0 Hence, no permutation that is not in non-decreasing order of the lis can have minimum d. It is easy to see that all permutations in nondecreasing order of the lis have the same d value. Hence, the ordering defined by ij=j, 1 j n, minimizes the d value. The tape storage problem can be extended to several tapes. If there are m>1 tapes, T0,,Tm-1, then the programs are to be distributed over these tapes. For each tape a storage permutation is to be provided. If Ij is the storage permutation for the subset of programs on tape j, then d(Ij) is as defined earlier. The total retrieval time (TD) is 0 j m-1 d(Ij).The objective is to store the programs in such a way as to minimize TD. The obvious generalization of the solution for the one-tape case is to consider the programs in nondecreasing order of lis. The program currently being considered is placed on the tape that results in the minimum increase in TD. This tape will be the one with the least amount of tape used so far. If there is more than one tape with this property, then the one with the smallest index can be used. If the jobs are initially ordered so that l1 l2 . ln, then the first m programs are assigned to tapes T0,,Tm-1 respectively. The next m programs will be assigned to tape Ti mod m. On any given tape the programs are
Page 71
In any storage pattern for m tapes, let ri be one greater than the number of programs following program i on its tape. Then the total retrieval time TD is given by n TD = i=1 rili
In any given storage pattern, for any given n, there can be at most m programs for which ri=j. From Theorem it follows that TD is minimized if the m longest programs have ri=1, the next m longest programs have ri=2 and so on. When programs are ordered by length, that is l1 l2 . ln, then this minimization criteria is satisfied if ri=[(ni+1)/m]. Observe that Algorithm 2.6 results in a storage pattern with these ris. The Proof of theorem shows that there are many storage patterns that minimize TD. If we compute ri=[n-i+1)/m] for each program i, then so long as all programs with the same ri are stored on different tapes and have ri-1 programs following them, TD is the same. If n is a multiple of m, then there are at least (m!) n/m storage patterns that minimize TD. 2.2.12 Single-Source Shortest Paths Graphs can be used to represent the highway structure of a state or country with vertices representing cities and edges representing sections of highway. The edges can then be assigned weights, which may be either the distance between the two cities
Page 72
The problems defined by these questions are special cases of the path problem we study in this section. The length of a path is now defined to be the sum of the weights of the edges on that path. The starting vertex of the path is referred to as the source and the last vertex the destination. In the problem consider, a directed graph G=(V,E), a weighting function cost for the edges of G and a source vertex vo. The problem is to determine the shortest paths from vo to all the remaining vertices of G. It is assumed that all the weights are positive. The shortest path between v o and some other node v is an ordering among a subset of the edges. Hence this problem fits the ordering paradigm. Example: Consider the directed graph of Figure (2.9(a)). The numbers on the edges are the weights. If node 1 is the source vertex, then the shortest path from 1 to 2 is 1,4,5,2. The length of this path is 10+15+20=45. Even though there are three edges on this path, it is shorter than the path 1,2 which is of length 50. There is no path from 1 to 6. Figure (2.9(b)) lists the shortest paths from node 1 to nodes 4, 5, 2 and 3, respectively. The paths have been listed in nondecreasing order of path length. 45 50 1 15 20 2 35 10 3 30 10 20
5 3 Graph
Figure 2.9: Graph and shortest paths from vertex 1 to all destinations To formulate a greedy-based algorithm to generate the shortest paths, one must conceive of a multistage solution to the problem and also of an optimization measure.
Page 73
2.
3.
The above observations lead to a simple Algorithm 2.7 for the single source shortest path problem. This algorithm (known as Dijkstras algorithm) only determines the lengths of the shortest paths from v0 to all other vertices in G. The generation of the paths requires a minor extension to this algorithm and is left as an exercise.
Page 74
Page 75
6 Page 76
0 1200
0 1500 1000
250 0
900 0
1400 1000 0
Iteration Initial 1 2 3 4 5 6
Distance CHI BOST [4] [5] 1500 0 1250 0 1250 0 1250 0 1250 0 1250 0 1250 0
55
2
5 40
3
20
30 50
5
15 35 10
(a)
A Graph
1 Page 77
2
5 40 20
30
15 10
55 25 2 5 5 a
1 45 3 4 30 8
15 10
6 (C) Shortest path spanning tree from vertex 1 Figure 2.12: Graphs and Spanning Trees
Revision Points
Sorting Page 78
Intext Questions
1. Describe the general method for Divide and Conquer in detail. 2. Explain the binary search in detail. 3. Describe version of MaxMin derived against Straight MaxMin method. Count Comparisons. 4. Explain the Merge Sort algorithm in detail with suitable example. 5. How is Strassens Matrix Multiplication done? 6. Define feasible solution. 7. Define optimal solution. 8. What is Knapsack problem? 9. Define Prims algorithm. 10. Explain KrusKals Algorithm 11. Define the shortest path-spanning tree. 12. How is the Optimal Storage on Tapes done? 13. Explain the method to generate Shortest Paths. 14. Define the Spanning tree. 15. Define MRT.
2.5 Summary
Page 79
Page 80
Supplementary Materials
1. Ellis Horowitz, Sartaj Sahni, Fundamentals of Computer Algorithms, Galgotia Publications, 1997. 2. Aho, Hopcroft, Ullman, Data Structures and Algorithms, Addison Wesley, 1987. 3. Jean Paul Trembly & Paul G.Sorenson, An introduction to Data Structures with Applications, McGraw-Hill, 1984.
Assignments
1. Analyze how searching is important and necessary for real time systems. 2. Discuss in detail about shortest path algorithm.
Learning Activities
1. Collect information on sorting techniques from internet. 2. Collect research reports and information on KrusKals Algorithm.
Page 81
Page 82