365 views

Uploaded by Siva Rajesh

save

- Vertex
- Famous Puzzels of Great Mathematicians
- Stacks and Recursion Lab
- Problem Set 1 Sol
- ABHVW04
- Skvortsova 1999
- Graph Theory
- Data_Warehouse_Design.ppt
- Thompson-Mooney - Acquiring Word-Meaning Mappings for Natural Language Interfaces
- Analysis and Design of Algorithms
- Senior_Mar11.pdf
- Recurrences and Methods for Solution
- Analysis and Design of Algorithms
- Parallel Simulation of the Shallow Water Equations on Structured Dynamically Adaptive Triangular Grids
- GraphTheoryKVI
- 1-s2.0-S0301479709002576-main
- Functions
- Support Strong Domination IN Fuzzy GRAPH
- Data Structure Interview
- Binary Trees
- sol13.pdf
- Graph Theory in Data Structure
- A Bipartite Graph Co-Clustering Approach to Ontology Mapping
- Graphs of bounded local treewidth
- Fqa
- Hilpert Holliday 2015 Classroom Networks d2
- cwhispers
- 150611-150703-Design and Analysis of Algorithms
- BinaryTrees.pdf
- Midterm 3 Review
- The Innovators: How a Group of Hackers, Geniuses, and Geeks Created the Digital Revolution
- Dispatches from Pluto: Lost and Found in the Mississippi Delta
- Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
- The Unwinding: An Inner History of the New America
- Yes Please
- Sapiens: A Brief History of Humankind
- The Prize: The Epic Quest for Oil, Money & Power
- The Emperor of All Maladies: A Biography of Cancer
- A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
- This Changes Everything: Capitalism vs. The Climate
- Grand Pursuit: The Story of Economic Genius
- Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
- John Adams
- Smart People Should Build Things: How to Restore Our Culture of Achievement, Build a Path for Entrepreneurs, and Create New Jobs in America
- The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
- Rise of ISIS: A Threat We Can't Ignore
- The World Is Flat 3.0: A Brief History of the Twenty-first Century
- The New Confessions of an Economic Hit Man
- Team of Rivals: The Political Genius of Abraham Lincoln
- Bad Feminist: Essays
- Steve Jobs
- Angela's Ashes: A Memoir
- How To Win Friends and Influence People
- The Incarnations: A Novel
- You Too Can Have a Body Like Mine: A Novel
- Extremely Loud and Incredibly Close: A Novel
- The Sympathizer: A Novel (Pulitzer Prize for Fiction)
- The Silver Linings Playbook: A Novel
- Leaving Berlin: A Novel
- The Light Between Oceans: A Novel
- The Flamethrowers: A Novel
- Brooklyn: A Novel
- The Rosie Project: A Novel
- The Blazing World: A Novel
- We Are Not Ourselves: A Novel
- The First Bad Man: A Novel
- Bel Canto
- The Master
- A Man Called Ove: A Novel
- The Love Affairs of Nathaniel P.: A Novel
- Life of Pi
- The Cider House Rules
- A Prayer for Owen Meany: A Novel
- The Perks of Being a Wallflower
- The Bonfire of the Vanities: A Novel
- Lovers at the Chameleon Club, Paris 1932: A Novel
- Beautiful Ruins: A Novel
- The Kitchen House: A Novel
- Interpreter of Maladies
- The Wallcreeper
- The Art of Racing in the Rain: A Novel
- Wolf Hall: A Novel
- A Tree Grows in Brooklyn

You are on page 1of 75

Chapter 1 : Review

Concept of algorithm Components of algorithms Relation to data structure Analysis and parameters Analysis and profiling Order notations

Chapter 2 : Different perspectives of algorithm

Numerical algorithm Numerical iterative algorithm Review of searching algorithm Review of sorting algorithm Recursion v/s iteration

Chapter 3 : Graph theory and related data structure

Introduction to graph theory Matrix representation Trees TSP

Chapter 4 : Divide and Conquer

Binary search Max- Min Merge sort Integer multiplication

Chapter 5 : Greedy method

Cassette filling Knapsack problem Job scheduling

Chapter 6 : Backtracking and Branch and Bound

Cassette filing Subset problem 8 queen problem

**Chapter 7 : Graph theoretic algorithms
**

Shortest path Minimal spanning trees Heap sort TSP

Chapter 1 REVIEW

1.1 Concept of algorithm

A common man’s belief is that a computer can do anything and everything that he imagines. It is very difficult to make people realize that it is not really the computer but the man behind computer who does everything. In the modern internet world man feels that just by entering what he wants to search into the computers he can get information as desired by him. He believes that, this is done by computer. A common man seldom understands that a man made procedure called search

Programming involves various activities form the stage of conceiving the problem upto the stage of creating a model to solve the problem. This implies that programming a computer is more important than the computer itself while solving a problem using a computer and this part of programming has got to be done by the man behind the computer. One can now experience that the focus is shifted from computer to computer programming and then to creating an algorithm. a designer of the information system should know what one frequently searches for.has done the entire job and the only support provided by the computer is the executional speed and organized storage of information. This set of instructions is termed as program. Take the brush . This is accomplished through a set of instructions created by the designer of the information system to search the right information matching the requirement of the user. which through the medium of the computer executes to perform all the activities as desired by a user. In the above instance. which generates automatically the program but it is the designer of the information system who has created this. It should be evident by now that it is not the computer. the right information is brought out.2 Characteristics of an algorithm Let us try to present the scenario of a man brushing his own teeth(natural denture) as an algorithm as follows. He should make a structured organization of all those details to store in memory of the computer. Thus. heart of problem solving. 1. The formal representation of this model as a sequence of instructions is called an algorithm and coded algorithm in a specific computer language is called a program. Based on the requirement. This is algorithm design. one should not quickly jump to a conclusion that coding is programming. Step 1. Even at this stage. the program is the one. Coding is perhaps the last stage in the process of programming.

Although unambiguous. he could possibly feel that this is the algorithm for cleaning a toilet. However the above two may not be sufficient to guarantee the termination of the algorithm. Effectiveness 5. Stop If one goes through these 6 steps without being aware of the statement of the problem. Such an ambiguity doesn’t an instruction an algorithmic step. Output 3. every algorithm should have the following five characteristic feature 1. Hence. . Rinse Step 5. An unambiguous step is called definite instruction. Therefore. which terminates with the production of correct output from the given input. The definiteness and effectiveness of an instruction implies the successful termination of that instruction. Thus every step should be made unambiguous. where to apply the tooth paste and where is the source of the tooth paste. paint brush. Even if the step 2 is rewritten as apply the tooth paste. Thus. Input 2. Termination Therefore. The step 1 may imply tooth brush.Step 2. an algorithm can be defined as a sequence of definite and effective instructions. Wash Step 6. Definiteness 4. to eliminate ambiguities yet the conflicts such as. toilet brush etc. while designing an algorithm care should be taken to provide a proper termination for algorithm. This is because of several ambiguities while comprehending every step. Apply the paste Step 3. need to be resolved. Start brushing Step 4. the act of applying the toothpaste is not mentioned. such unrealizable steps can’t be included as algorithmic instruction as they are not effective.

Example 1: Problem : finding the largest value among n>=1 numbers. 7. Input : the value of n and n numbers Output : the largest value Steps : 1. 2.1 Algorithmic Notations . 5.In other words. 3. Print BIG Stop Therefore look the next number called NEW. For the purpose of clarity in understanding. 6. viewed little more formally.2. 8. R=n-1 If R != 0 then it is implied that the list is still not exhausted. let us consider the following examples. The problem of writing down the correct algorithm for the above problem of brushing the teeth is left to the reader. an algorithm is a step by step formalization of a mapping function to map input set onto an output set. 4. End of algorithm Example 2: quadratic equation Example 3: listing all prime numbers between two limits n1 and n2. 1. Let the value of the first be the largest value denoted by BIG Let R denote the number of remaining numbers. Now R becomes R-1 If NEW is greater than BIG then replace BIG by the value of NEW Repeat steps 3 to 5 until R becomes zero.

• Repeat . Here block gets executed as long as the condition is true. it is expected that the reader be aware of PASCAL/C. 1. The pseudo code used resembles PASCAL and C language control structures. • There are two types of conditional looping. for the sake of completion we present the commonly employed control constructs present in the algorithms. A conditional statement has the following form If < condition> then Block 1 Else Block 2 If end. Hence. This pseudocode executes block1 if the condition is true otherwise block2 is executed. 2. The two types of loop structures are counter based and conditional based and they are as follows • For variable = value1 to value2 do Block For end Here the block is executed for all the values of the variable from value 1 to value 2. While (condition) do Block While end. However. Even otherwise atleast now it is required that the reader should know preferably C to practically test the algorithm in this course work.In this section we present the pseudocode that we use through out the book to describe algorithms. while type and repeat type.

To compute the area of a circle of a given circumference 5. To test whether a given point p(x. 4. Exercise 1. Devise the algorithm for the following and verify whether they satisfy all the features. Once we have more than one algorithm for a given problem. 1. CHAPTER 2 Different Perspective of Algorithms If there are more then one possible way of solving a problem. how do . then one may think of more than one algorithm for the same problem. Hence. Data domain is an important aspect to be known in the field of algorithms. It may be observed that the block is executed atleast once in repeat type. To test whether the three numbers represent the sides of a right angle triangle. 2.Block Until<condition> Here block is executed as long as condition is false. Note : it is always required to hand simulate with suitable input dataset. An algorithm that inputs three numbers and outputs them in ascending order. To locate a specific word in a dictionary. 3. it is necessary to know in what domains these algorithms are applicable.y) lies on x-axis or y-axis or in I/II/III/IV quadrant.

3.1. Decision: decision parameter is used to determine when to exit from the loop.1 Numerical Iterative Algorithm An iterative process can be illustrated with the flow chart given in fig 2.1. Initialization: all parameters are set to their initial values. 4. Constructive method is a procedure used to obtain the solution for a mathematical problem in finite number of steps and to some desired accuracy. 2. The functions of these four blocks are as follows: 1. which exhibits some average behavior. Update: decision parameter is updated and is transformed for next iteration. 2. There are four main blocks in the process viz. Computation: required computation is performed.. . and update. initialization. it is always complex to determine a data set. decision.we choose the best among them? The solution is to devise some data sets and determine a performance profile for each of the algorithms.1 Numerical Algorithms Numerical analysis is the theory of constructive methods in mathematical analysis. The following sections give a brief idea of the wellknown accepted algorithms. computation. A best case data set can be obtained by having all distinct data in the set. But. 2.

start with (x1(1). x2. x3 using the equations given below x1(k+1)=(b1-a12x2(k+1)-a13x3(k))/a11 x2(k+1)=(b2-a21x1(k+1)-a23x3(k))/a22 x3(k+1)=(b3-a31x1(k+1)-a32x3(k+1))/a33 for k=1. Numerical iterative methods are also applicable for obtaining the roots of the equation of the form f(x)=0. For example: consider the following set of equations.… This process is continued upto some desired accuracy.2. For large system of equations.x3(1))=(0. The various iterative methods used for this purpose are. Every iterative algorithm is infinite step algorithm. Bisection method: xi+2=(xi+xi+1)/2 2. the iteration required may be quite large. there is a guarantee of getting the convergent result.3. This iteration method requires generally a few iteration.Many problems in engineering or science need the solution of simultaneous linear algebraic equations.Falsi method: x2=(x0f(x1)+ x1f(x0))/ (f(x1)-f(x0)) . 10x1+2x2+x3= 9 2x1+20x2-2x3= -44 -2x1+3x2+10x3= 22. One of the iterative algorithms to solve system of simultaneous equations is Guass Siedel.0.0) as initial values and compute the values of we write the system of x1. Regula.x2(1). But. To solve the above set of equations using Guass Siedel iteration scheme. Iterative techniques have less roundoff error. 1.

1 Sequential search In this method. The linear search and binary search methods are relatively straight forward methods of searching. For both situations we must examine all n elements of the array so the order of magnitude or complexity of the sequential search is n.2 Searching Let us assume that we have a sequential file and we wish to retrieve an element matching with key ‘k’. Algorithm : sequential search Input : A. The algorithm for sequential search is as follows. we start to search from the beginning of the list and examine each element till the end of the list. 2.. There are a number of complex searching algorithms to serve the purpose of searching. search element Output : j –index of k Method : i=1 .2.3. If the item is not found and the list is exhausted the search returns a zero value. vector of n elements K. In the worst case the item is not found or the search item is the last (nth) element. i. we have to search the entire file from the beginning till the end to check whether the element matching k is present in the file or not. O(n).e. then. The execution time for this algorithm is proportional to n that is the algorithm executes in linear time. If the desired element is found we stop the search and return the index of that element. Newton Raphson method: x2= x1-f(x1)/f1(x1) 2.

algorithm ends. The algorithm for binary search is as follows. For this method it is necessary to have the vector in an alphabetical or numerically increasing order.While(i<=n) { if(A[i]=k) { write(“search successful”) write(k is at location i) exit(). 2.2 Binary search Binary search method is also relatively simple method. if the mid value is lesser than X. The middle entry of the left-reduced list is examined in a similar manner. Now the list gets reduced to half the original list. The approximate mid entry is located and its key value is examined. The middle entry of the right-reduced list is examined and the procedure is continued until desired key is found or the search interval is exhausted. A search for a particular item with X resembles the search for a word in the dictionary. If the mid value is greater than X. then the list is chopped off at (mid+1) th location. This procedure is repeated until the item is found or the list has no more elements.2. } else i++ if end while end write (search unsuccessful). then the list is chopped off at the (mid-1)th location. On the other hand. Algorithm : binary search .

} else write (search unsuccessful). The . algorithm ends.Input : A.high=n While(low<=high-1) { mid=(low+high)/2 if(k<a[mid]) high=mid else low=mid if end } while end if(k=A[low]) { write(“search successful”) write(k is at location low) exit(). search element Output : low –index of k Method : low=1. 2. if end.3 Sorting One of the major applications in computer science is the sorting of information in a table. Sorting algorithms arrange items in a set according to a predefined ordering relation. vector of n elements K. The most common types of data are string information and numerical information.

size of the data set. above.13. a}is in descending order.16.16.13.16. degree of relative order already present in the data etc.ordering relation for numeric data simply involves arranging items in sequence from smallest to largest and from largest to smallest. This process is continued till the entire table is sorted.3.19. {a. not even one of them is best for all applications. The items in a set arranged in descending order is of the form {23.19. beyond}is in ascending order and { beyond. be. be. The items in a set arranged in non-decreasing order are {7. Performance of the methods depends on parameters like. abacus. a[1] a[2] 20 35 18 8 14 41 3 a[8] 39 The resulting array should be a[1] a[2] 3 8 14 18 20 35 39 a[8] 41 One way to sort the unsorted array would be to perform the following steps: • • Find the smallest element in the unsorted array Place the smallest element in position of a[1] . 2. become.11. But. There are numerous methods available for sorting information. Consider the unsorted array. abacus.1 Selection sort The idea in selection sort is to find the smallest value and place it in an order.7} Similarly for string information.11.23}. then find the next smallest and place in the right order. above. become. which is called ascending and descending order respectively.16.

2 Insertion sort Insertion sort is a straight forward method that is useful for small collection of data.The number of moves with this technique is always of the order O(n).i.3.e. The changes undergone by the array is shown in fig 2. Selecting an element from the unordered list could be simple if the first element of that list is selected. The array now becomes. The idea here is to obtain the complete solution by inserting an element from the unordered part into the partially ordered solution extending it by one element. a[1] a[2] 3 35 18 8 14 41 20 a[8] 39 Now find the smallest from a[2] to a[8] .. a[1] a[2] 3 8 18 35 14 41 20 a[8] 39 Repeat this process until the entire array is sorted. i. 2.. . 8 so exchange the values of a[2] and a[4] which results with the array shown below.e. the smallest element in the unsorted array is 3 so exchange the values of a[1] and a[7].2.

Now the array is of the form. but the preceding element is 3 which is less than x. so move 35 one position ahead and place 18 at that place. 8 is less than 35 and 8 is also less than 18 so move 35 and 18 one position right and place 8 at a[2]. 18. .. 18 is less than 35. Hence. The resulting array will be. If the selected element is less than the preceding element move the preceding element by one position and insert the smaller element. take the next element for insertion i.a[1] a[2] 20 35 18 8 14 41 3 a[8] 39 Initially the whole array is unordered. Select the next element to be inserted. a[1] a[2] 3 35 18 8 14 41 20 a[8] 39 Now we have one element in the sorted list and the remaining elements are in the unordered set. This process is carried till the sorted array is obtained. In the above array the next element to be inserted is x=35.e. a[1] a[2] 3 18 35 8 14 41 20 a[8] 39 Now the element to be inserted is 8. So select the minimum and put it in place of a[1] to act as sentinel.

3. For lengthy records it is quite time consuming. 2. If n is odd one file is of size one.4 illustrates the process of merge sort. These are merged pair wise to obtain n/2 files of size two. In the worst case. As illustrated in the example merge sort consists of several passes over the records being sorted. One of the disadvantages of the insertion sort method is the amount of movement of data.The changes undergone are shown in fig 2. These n/2 files are then merged pair wise and so on until we are left with only one file. the number of moves is of the order O(n 2). In the second pass the size of the files .3 Merge sort Merge sort begins by interpreting the inputs as n sorted files each of length one.3. In the first pass files of size one are merged. The example in fig 2.

being merged is two. the number whose factorial is to be found. each pass of merge sort takes O(n) time. with little duplication of tasks.1 When to use recursion Recursion can be used for repetitive computations in which each action is stated in terms of previous result. then recursion is suitable. -It is a technique of defining an object/process by itself.4 Recursion Recursion may have the following definitions: -The nested repetition of identical algorithm is recursion. Algorithm : factorial-recursion Input : n. two files can be merged in linear time. Since. it is always advisable to consider a tree structure for the problem. Output : f. As there are log2n passes the total time complexity is O(n log2n). There must be a decision criterion for stopping the process. If the structure is simple then use non-recursive form. 1. 2. A total of log2n passes are made over the data. In making the decision about whether to write an algorithm in recursive or non-recursive form. There are two conditions that must be satisfied by any recursive procedure. the factorial of n . 2.4. Each time a function calls itself it should get nearer to the solution. The recursion algorithm for finding the factorial of a number is given below. If the tree appears quite bushy. -Recursion is a process by which a function calls itself repeatedly until some specified condition has been satisfied. 2. In the ith pass the files being merged will be of size 2 i-1.

1. Restore the most recently saved parameters. local variable and return address and goto the latest return address.4. . Many programming languages do not support recursion. The general procedure for any recursive algorithm is as follows.2 Iteration v/s Recursion Demerits of recursive algorithms 1.Method : if(n=0) f=1 else f=factorial(n-1) * n if end algorithm ends. hence recursive mathematical function is implemented using iterative methods. Save the parameters. 2. 2. local variables and return addresses. If the termination criterion is reached perform final computation and goto step 3 otherwise perform final computations and goto step 1 3.

postponing the rest. Mathematical functions such as factorial and fibonacci series generation can be easily implemented using recursion than iteration. The iterative function obviously uses time that is O(n) where as recursive function has an exponential time complexity. It is also true that stack can be replaced by a recursive program with no stack. It divides the problem into pieces or selects out one key step. Iteration is more of a bottom up approach. Recursion is a top down approach to problem solving. 2.1.…etc. . A recursive procedure can be called from within or outside itself and to ensure its proper functioning it has to save in some order the return addresses so that. where the third number is the sum of preceding two numbers and so on. A fibonacci series is of the form 0.3.2. It is always true that recursion can be replaced by iteration and stacks. Even though mathematical functions can be easily implemented using recursion it is always at the cost of execution time and memory space.13.5. 4. In iterative techniques looping of statement is very much necessary. f(n-3) is computed thrice. For example. the recursion tree for generating 6 numbers in a fibonacci series generation is given in fig 2. The recursive programs needs considerably more storage and will take more time.5 that. a return to the proper location will result when the return to a calling statement is made. It can be noticed from the fig 2. Demerits of iterative methods 1. f(n2) is computed twice.8.5. f(n-4) is computed 5 times. It begins with what is known and from this constructs the solution step by step. 3.2.1.

5 Hashing Hashing is a practical technique of maintaining a symbol table.6. In hashing technique the address X of a variable x is obtained by computing an arithmetic function (hashing function) f(x). The function f(x) maps the possible variable onto the integers 0 through b-1. Thus f(x) points to the address where x should be placed in the table. Let T be the identifier space. where each variable occupies one slot. Then. The hash table is partitioned into several storing spaces called buckets and each bucket is divided into slots (fig 2. The size of the space from where the variables are drawn is called the identifier space. This address is known as the hash address. The memory used to store the variable using hashing technique is assumed to be sequential. A symbol table is a data structure which allows to easily determine whether an arbitrary element is present or not. n be the number of variables/identifiers in the hash table. If there are b buckets in the table.6).2. each bucket is capable of holding s variables. the ratio n/T is called the identifier density and a = n/sb is the loading density or loading factor. . The memory is known as hash table. Consider a sequential memory shown in fig 2.

G. A1.7. If a new identifier is hashed into a already complete bucket. Let f(x) be the function which maps on to a address equal to the position of the first character of the identifier in the set of English alphabet. A4. A. A2.If f(x1)=f(x2). A3. where x1and x2 are any two variables. The hashing table generated is as shown in fig 2. Z. L. Time taken to retrieve the identifiers is as follows. The identifier to be stored are GA. Synonyms are mapped onto the same bucket. A hashing table with single slot is as given below. E. ZA. collision occurs. D. then x1and x2 are called synonyms. Let there be 26 buckets with single slot. Search element (x) GA D A G L A2 A1 A3 A4 Z Search time (t) 1 1 1 2 1 2 3 5 6 1 .

ZA E

10 6 ∑t =39

Average retrieval time =(∑t)/n. The average retrieval time entirely depends on the hashing function. Exercise 2: 1. What are the serious short comings of the binary search method and sequential search method. 2. Know more searching techniques involving hashing functions 3. Implement the algorithms for searching and calculate the complexities 4. Write an algorithm for the above method of selection sort and implement the same. 5. Write the algorithm for merge sort method 6. Take 5 data set of length 10 and hand simulate for each method given above. 7. Try to know more sorting techniques and make a comparative study of them. 8. Write an iterative algorithm to find the factorial of a number 9. Write a recursive and iterative program for reversing a number 10. Write recursive and iterative program to find maximum and minimum in a list of numbers. 11. Write an algorithm to implement the hashing technique and implement the same 12. Hand simulate all algorithms for a 5 datasets.

**Chapter 3 GRAPH THEORY AND RELATED DATA STRUCTURES
**

3.1 Introduction to Graph Theory 3.1.1 What is graph? A graph G = (V, E) consists of a set of objects V = {v 1, v2, …} called vertices, and another set E = {e1, e2, …} whose elements are called edges. Each edge e k in E is identified with an unordered pair (vi, vj) of vertices. The vertices vi, vj associated with edge ek are called the end vertices of ek. The most common representation of graph is by means of a diagram, in which the vertices are represented as points and each edge as a line segment joining its end vertices. Often this diagram itself is referred to as a graph.

Fig 3-1.

In the Fig. 3-1 edge e1 having same vertex as both its end vertices is called a self-loop. There may be more than one edge associated with a given pair of vertices, for example e4 and e5 in Fig. 3-1. Such edges are referred to as parallel edges.

A graph that has neither self-loop nor parallel edges are called a simple graph, otherwise it is called general graph. It should also be noted that, in drawing a graph, it is what is immaterial whether the lines are drawn straight or curved, long or short: important is the incidence between the edges and vertices. A graph is also called a linear complex, a 1-complex, or a one-dimensional complex. A vertex is also referred to as a node, a junction, a point, 0-cell, or an 0-simplex. Other terms used for an edge are a branch, a line, an element, a 1-cell, an arc, and a 1-simplex. Because of its inherent simplicity, graph theory has a very wide range of applications in engineering, physical, social, and biological sciences, linguistics, and in numerous other areas. A graph can be used to represent almost any physical situation involving discrete objects and a relationship among them.

3.1.2 Finite and Infinite Graphs Although in the definition of a graph neither the vertex set V nor the edge set E need be finite, in most of the theory and almost all applications these sets are finite. A graph with a finite number of vertices as well as a finite number of edges is called a finite graph; otherwise, it is an infinite graph. 3.1.3 Incidence and Degree When a vertex vi is an end vertex of some edge e j, vi and ej are said to be incident with (on or to) each other. In Fig. 3-1, for example, edges e2, e6, and e7 are incident with vertex v4. Two nonparallel edges are said to be adjacent if they are incident on a common vertex. For example, e2 and e7 in Fig. 3-1 are adjacent. Similarly, two vertices are said to be adjacent if they are the end vertices of the same edge. In Fig. 3-1, v 4 and v5 are adjacent, but v1 and v4 are not.

A null graph of six vertices is shown in Fig. Two adjacent edges are said to be in series if their common vertex is of degree two. and d(v5) = 1. isolated vertices are vertices with zero degree. d(vi). 3-2 Graph containing isolated vertices. Such a graph.The number of edges incident on a vertex vi. without any edges. A vertex of degree one is called a pendent vertex or an end vertex. the sum of the degrees of all vertices in G is twice the number of edges in G.1. 3-2. Pendent vertex. 3-2. Since each edge contributes two degrees. with self-loops counted twice is called the degree. series edges and a pendant vertex. by definition. E). the two edges incident on v1 are in series. . d(v1) = d(v3) = d(v4) = 3. In other words. In other words. 3-2 is a pendant vertex. The degree of a vertex is sometimes also referred to as its valency. In the definition of a graph G = (V. every vertex in a null graph is an isolated vertex. are isolated vertices. Fig.4 Isolated vertex. Vertex v3 in Fig. there is no graph. 3-1. for example. and Null graph A vertex having no incident edge is called an isolated vertex. it is possible for the edge set E to be empty. In Fig. is called a null graph. In Fig. Although the edge set E may be empty. the vertex set V must not be empty. Vertex v4 and v7 in Fig. of vertex vi. 3. d(v2) = 4. In other words. 3-3. a graph must have at least one vertex. for example. otherwise.

3.Fig.2. = 0. many known results of matrix algebra can be readily applied to study the structural properties of graphs from an algebraic point of view. such as in electrical network analysis and operation research. .2 Matrix Representation of Graphs Although a pictorial representation of a graph is very convenient for a visual study. and otherwise. Besides. Matrices lend themselves easily to mechanical manipulations. as follows: The matrix element Aij = 1. Define an n by e matrix A =[aij]. matrices also turn out to be the natural way of expressing the problem.1 Incidence Matrix Let G be a graph with n vertices. if jth edge ej is incident on ith vertex vi. and no self-loops. 3-3 Null graph of six vertices. other representations are better for computer processing. In many applications of graph theory. 3. A matrix is a convenient and useful way of representing a graph to a computer. whose n rows correspond to the n vertices and the e columns correspond to the e edges. e edges.

Matrix A for a graph G is sometimes also written as A(G). 3-4. 0 and 1. The following observations about the incidence matrix A can readily be made: . The incidence matrix contains only two elements. Such a matrix is called a binary matrix or a (0. 1)-matrix. A graph and its incidence matrix are shown in Fig.(a) a v1 v2 v3 v4 v5 v6 0 0 0 1 0 1 b 0 0 0 1 0 1 c 0 0 0 1 1 0 d 1 0 0 0 1 0 e 0 1 0 1 0 0 (b) f 1 1 0 0 0 0 g 0 1 0 0 1 0 h 0 1 1 0 0 0 Fig. Such a matrix A is called the vertex-edge incidence matrix. 3-4 Graph and its incidence matrix. or simply incidence matrix.

Since every edge is incident on exactly two vertices. 3.3. 2. each column of A has exactly two 1’s. The sorting of mail according to zip code and the sorting of punched cards are done according to a tree (called decision tree or sorting tree). Parallel edges in a graph produce identical columns in its incidence matrix. Fig. It follows immediately from the definition that a tree has to be a simple graph. The graph in Fig 3-5 for instance. The genealogy of a family is often represented by means of a tree. A river with its tributaries and sub-tributaries can also be represented by a tree. A row with all 0’s. columns 1 and 2 in Fig. A tree is a connected graph without any circuits. especially for those interested in applications of graphs. 4.1 Some properties of Trees . 3. that is. 3.3 Trees The concept of a tree is probably the most important in graph theory. therefore. having neither a self-loop nor parallel edges (because they both form circuits). The number of 1’s in each row equals the degree of the corresponding vertex. is a tree. Tree Trees appear in numerous instances. for example. 3-4. 3-5. represents an isolated vertex.1.

or There is exactly one path between every pair of vertices in G. or G is circuit less and has n-1 edges. or G is a minimally connected graph. 2. T. Any connected graph with n vertices and n-1 edges is a tree. or G is connected and has n-1 edges. 3. .1. 4. Therefore a graph with n vertices is called a tree if 1. G is connected and is circuit less. 5. A graph is a tree if and only if it is minimally connected. 2. 4. A tree with n vertices has n-1 edges. 3. There is one and only one path between every pair of vertices in a tree.

0.11). Hence vertex start can be considered the root of the tree. 3. Each is of length four.11). and the path from the start vertex to a particular vertex v describes the monotonically increasing subsequence terminating in v.1.3. and so the tree is rooted.3 3. (1.2. this sequence contains four longest monotonically increasing subsequences. in Fig.3.11) and (0. Since no vertex can be of zero degree. and hence 2(n-1) degrees to be divided among n vertices. 3-6 vertex named start.8. 3-5 has several pendant vertices.2. for emphasis they are sometimes called free trees (or non rooted trees) to differentiate them from the rooted kind.13.8.3. Given a sequence of integers. we must have at least two vertices of degree one in a tree.0. As shown in Fig. Suppose that the sequence given to us is 4. it can be represented by a tree in which the vertices (except the start vertex) represent individual numbers in the sequence. Computer programmers refer to such a tree used in representing data as a data tree. . The reason is that in a tree of n vertices we have n-1 edges. 3-6 Tree of a monotonically increasing sequences in 4. 3-6.3 Rooted and Binary Tree A tree in which one vertex (called the root) is distinguished from all the others is called a rooted tree.2 Pendent Vertices in a Tree It is observed that a tree shown in the Fig.2. (4. This makes sense only if n ≥ 2.13.11.8.8. (1.8. A pendant vertex was defined as a vertex of degree one).Fig. An Application: The following problem is used in teaching computer programming.7. no two of which are the same find the largest monotonically increasing subsequence in it.1. that is.2.7.11.7. the term tree means trees without any root.8. However. is distinguished from the rest of the vertices.11).7. Generally. For instance.

3. is of particular interest. A given graph has numerous subgraphs. it is also quite appropriate to call a spanning tree a maximal tree subgraph or maximal tree of G. Binary Trees: A special class of rooted trees. 2e distinct combinations are possible. Out of these trees we are particularly interested in certain types of trees. This is why a spanning tree is sometimes referred to as a skeleton or scaffolding of G. Thus every binary tree is a rooted tree. binary identification problems. 3-6 Tree. called spanning trees. and each of the remaining vertices of degree one or three. . Since the vertex of degree two is distinct from all other vertices. Since spanning trees are the largest trees among all trees in G. it is a sort of skeleton of the original graph G. Now we shall study the tree as a subgraph of another graph. 3. this vertex serves as a root.Fig. and variable-length binary codes. some of these subgrphs will be trees. A binary tree is defined as a tree in which there is exactly one vertex of degree two. called binary rooted trees. Obviously. from e edges. since they are extensively used in the study of computer search methods. A tree T is said to be a spanning tree of a connected graph G if T is a subgraph of G and T contains all vertices of G.4 Spanning Trees So far we have discussed the trees when it occurs as a graph by itself. Since the vertices of G are barely hanging together in a spanning tree.

delete an edge from the circuit.5 Traveling-Salesman Problem A problem closely related to the question of Hamiltonian circuits is the Travelingsalesman problem. w(ei). . The length of a Hamiltonian path in a connected graph of n vertices is n-1. repeat the operation till an edge from the last circuit is deleted. w(ei) being the weight of edge ei.3. Such a graph is called a weighted graph. leaving a connected. a Hamiltonian path in a graph G traverses every vertex of G. it is its own spanning tree. with the minimum mileage traveled? Representing the cities by vertices and the roads between them by edges. Clearly.3. 3. This path is called a Hamiltonian path. Since a Hamiltonian path is a subgraph of a Hamiltonian circuit (which in turn is a subgraph of another graph). In this graph. If there are more circuits. There are. at which the walk also terminates. Given the distances between the cities. 3. If G has no circuit.5 Hamiltonian Paths and Circuits Hamiltonian circuit in a connected graph is defined as a closed walk that traverses every vertex of G exactly once. every graph that has a Hamiltonian circuit also has a Hamiltonian path. we are left with a path. with every edge ei there is associated a real number (the distance in miles. in what order should he travel so as to visit every city precisely once and return home. we get a graph. however. stated as follows: A salesman is required to visit a number of cities during a trip. many graphs with Hamiltonian paths that have no Hamiltonian circuits. A circuit in a connected graph G is said to be Hamiltonian if it includes every vertex of G. except of course the starting vertex. Hence a Hamiltonian circuit in a graph of n vertices consists of exactly n edges. circuit-free graph that contains all the vertices of G. This will still leave the graph connected. If G has a circuit. Hamiltonian path: If we remove any one edge from a Hamiltonian circuit. say).Finding a spanning tree of a connected graph G is simple.

Draw all simple graphs of one. of course) Hamiltonian circuits in a complete graph of n vertices can be shown to be (n-1)! / 2. the labor involved is too great even for a digital computer. because each Hamiltonian circuit has been counted twice. we have a complete weighted graph. two. results with (n-1)! choices. three and four vertices . This graph has numerous Hamiltonian circuits. However. No efficient algorithm for problems of arbitrary size has yet been found. for a large value of n. n-3 from the third. however. if each of the cities has a road to every other city. This follows from the fact that starting from any vertex we have n-1 edges to choose from the first vertex. some specific large-scale examples have been worked out. divided by 2. Theoretically. the problem of the traveling salesman can always be solved by enumerating all (n-1)!/2 Hamiltonian circuits. Exercise 3 1. and then picking the shortest one. There are also available several heuristic methods of solution that give a route very close to the shortest one. and we are to pick the one that has the smallest sum of distances (or weights). but do not guarantee the shortest. This number is. Since this problem has applications in operations research. n-2 from the second. The problem is to prescribe a manageable algorithm for finding the shortest route. The total number of different (not edge disjoint. calculating the distance traveled in each.In our problem. and so on. These being independent. although many attempts have been made.

3. 5. 7. 4.2. 9.2. developed. 10.4 and 5 Sketch all binary trees with six pendent edges Sketch all spanning trees of given graphs in this chapter Write incidence matrix for all the graphs developed Find the spanning trees for all the graphs developed Draw a graph which has Hamiltonian path but does not have List different paths from vertex1 to vertex n in each graph Explain what each vertex and edge represent is removed from it Hamiltonian circuit . Name 10 situations that can be represented by means of graphs. 3. 8. Draw a connected graph that becomes disconnected when any edge Draw all trees of n labeled vertices for n=1. 6.

simply the union of individual solutions. It is often possible to phrase any problem in terms of these general strategies. Backtracking and Branch and Bound evaluation are variations of dynamic programming idea. a stage is reached where the subproblems are small enough to be solved without further splitting.Q) //the data size is from p to q { Combining the solutions of the individuals we get the final conquering. A general binary divide and conquer algorithm is : Procedure D&C (P. . forward journey is divide and backward journey is Conquer. The techniques of Greedy Search. These general strategies are Divide and Conquer. Dynamic Programming. The basic idea of divide and conquer is to divide the original problem into two or more sub-problems which can be solved by the same technique. Combining need not mean. The most widely known and often used of these is the divide and conquer strategy. In precise.Chapter 4 DIVIDE AND CONQUER There are a number of general and powerful computational strategies that are repeatedly used in computer science. If it is possible to split the problem further into smaller and smaller sub-problems. Divide and Conquer involves four steps 1) Divide 2) Conquer [Initial Conquer occurred due to solving] 3) Combine 4) Conquer [Final Conquer]. All these strategies and techniques are discussed in the subsequent chapters.

Q)) } Sometimes. Illustration : Consider the data set with elements {12. This way of breaking down the problem has found wide application in sorting.68}. 4. selection and searching algorithm.46.18.M). First let us consider the simulation for successful cases. D&C(M+1.32.59.If size(P. this type of algorithm is known as control abstract algorithms as they give an abstract flow.62. Successful cases: .52.Q) Combine (D&C(P. Else p←m+1 End Algorithm.Q) is small Then Solve(P.Q) Else M ← divide(P.22.1 Binary Search: Algorithm: m← (p+q)/2 If (p ≤ m ≤ q) Then do the following Else Stop If (A(m) = Key Then ‘successful’ stop Else If (A(m) < key Then q=m-1.

3 units of time is required Key=32 P 1 1 3 4 Q 9 4 4 4 m 5 2 3 4 Search x x x successful To search 32. 3 units of time is required Key=18 P 1 1 Q 9 4 m 5 2 Search x successful To search 18. 4 units of time is required Key=46 P 1 Q 9 m 5 Search successful . 2 units of time is required Key=22 P 1 1 3 Q 9 4 4 m 5 2 3 Search x x successful To search 22.Key=12 P 1 1 1 Q 9 4 1 m 5 2 1 Search x x successful To search 12.

To search 46. 2 units of time is required Key=62 P 1 6 8 Q 9 9 9 m 5 7 8 Search x x successful To search 62. 1 unit of time is required Key=52 P 1 6 6 Q 9 9 6 m 5 7 6 Search x x successful To search 52. 4 units of time is required 3+2+3+4+1+3+2+4 Successful average search time= ------------------------- . 3 units of time is required Key=68 P 1 6 8 9 Q 9 9 9 9 m 5 7 8 9 Search x x x successful To search 68. 3 units of time is required Key=59 P 1 6 Q 9 9 m 5 7 Search x successful To search 59.

4 units of time is required Key=65 P 1 6 8 9 Q 9 9 9 9 m 5 7 8 9 Search x x x x To search 65. 4 units of time is required 4+4 Unsuccessful search time =-------------------2 average search time = (sum of unsuccessful search time + sum of Successful search time)/(n+(n+1)) .9 unsuccessful cases Key=25 P 1 1 3 4 Q 9 4 4 4 m 5 2 3 4 Search x x x x To search 25.

Algorithm: Max-Min (p.2 Max-Min Search: Max-Min search problem aims at finding the smallest as well as the biggest element in a vector A of n elements. q. min) { If (p = q) Then max = a(p) min = a(q) Else If ( p – q-1) Then .4. max. Following the steps of Divide and Conquer the vector can be divided into sub-problem as shown below. The search has now reduced to comparison of 2 numbers. The time is spent in conquering and comparing which is the major step in the algorithm.

min2) If End If End Algorithm End.q.min1) max-min(m+1.max2) min ← small(min1.m.max1.min2) max ← large(max1.max2. Illustration .If a(p) > a(q) Then max = a(p) min = a(q) Else max = a(q) min = a(p) If End Else m ← (p+q)/2 max-min(p.

The repeated addition method is left as an assignment for the reader. the list is broken into two equal halves.e. In the first call. The list is again broken down into two.06.91.14.12. This process is continued till the length of the list is either two or one. .1 4.1 left shift method 981*1234 3924 2943* 1962** 981*** 1210554 In this method.92}. A is multiplied by every digit of b starting from right to left..76.3 Integer multiplication There are various methods of obtaining the product of two numbers. the left shift method. Finally the products obtained by multiplying a by each digit of b is summed up to obtain the final product.. Then the maximum and minimum values are chosen from the smallest list and these values are returned to the preceding step where the length of the list is slightly big. Initially the max and min variables have null values. The detail description is shown in fig 4.Consider a data set with elements {82. a=981 is the multiplicand and b=1234 is the multiplier. On each multiplication the subsequent products are shifted one place left.36. Another way of finding the product is the one we generally use i. This process is continued till the entire list is searched.3.49. The reader is expected to find the product of some bigger numbers using the repeated addition method. 4.

which can be illustrated as follows. On every multiplication the product is shifted one place to the right and finally all the products obtained by multiplying ‘a’ by each digit of ‘b’ is added to obtain the final result.2 right shift method 981*1234 981 1962 *2943 **3924 1210554 In the above method.3 halving and doubling method Let a=981 and b=1234 The steps to be followed are 1. Repeat step 2 and step 1 till a<=1 . If a is odd store b 2. The product of two numbers can also be obtained by dividing ‘a’ and multiplying ‘b’ by 2 repeatedly until a<=1. 4. 4. A=a/2 and b=b*2 3.The above product can also be obtained by a right shift method.3.3. a is multiplied by each digit of b from leftmost digit to rightmost digit.

4 Speed up algorithm: In this method we split the number till it is easier to multiply. 09 is then multiplied by both 12 and 34 but.3. Exercise 4 . The number of shifts ‘n’ is decided as follows Multiplication sequence 09*12 09*34 81*12 81*34 shifts 4 2 2 0 108**** 306** 972** 2754 Sum=1210554 For 0981*1234. the products are shifted ‘n’ places left before adding. we split 0981 into 09 and 81 and 1234 into 12 and 34. 12 and 81 takes 2 shifts and so on. i.a 981 490 245 122 61 30 15 7 3 1 b 1234 2468 4936 9872 19744 39488 result 1234 ----------4936 --------19744 ---------- -78976 78976 157952 157952 315904 315904 631808 631808 Sum=1210554 The above method is called the halving and doubling method. multiplication of 34 and 81 takes zero shifts.. 34*09 takes 2 shifts.e. 4.

is called a feasible solution. 2. we are required to obtain the solution which is a series of subsets that satisfy some constraints or conditions. Hand simulate the algorithm for atleast 10 different numbers. Chapter 5 GREEDY METHOD Greedy method is a method of choosing a subset of the dataset as the solution set that results in some profit. Write a program to find the maximum and minimum of the list of n element with and without using recursion. Consider a problem having n inputs. If the inclusion of a particular data results with an optimal solution. Implement the same for verification. considering one input at a time and at each stage.1. This feasible solution finally obtained is called optimal solution. which satisfies these constraints. It is required to obtain the feasible solution that maximizes or minimizes the objective function. 3. 4. Write the algorithm to find the product of two numbers for all the methods explained. a decision is taken on whether the data chosen results with an optimal solution or not. then the data . If one can devise an algorithm that works in stages. Any subset.

i3. whenever a program is to be retrieved the tape is initially positioned at the start end. Let tj be the time required retrieving program ij where programs are stored in the order I = i1.is added into the partial solution set.. The general algorithm for the greedy method is 1) Choose an element e belonging to dataset D. …≤ ln.2.1 Cassette Filling Consider n programs that are to be stored on a tape of length L. if the inclusion of that data results with infeasible solution then the data is eliminated from the solution set. 5. On the other hand. tj =Σ lik k=1.e. 2) Check whether e can be included into the solution set S if Yes solution set is s ← s U e. ….j Now the problem is to store the programs on the tape so that MRT is minimized. 3) Continue until s is filled up or D is exhausted whichever is earlier.….. It is assumed that.e. The solution set obtained need not be a subset of data but may be the data set itself in a different sequence. Hence the ordering defined minimizes the retrieval time. From the above discussion one can observe that the MRT can be minimized if the programs are stored in an increasing order i. All programs can be stored on the tape iff the sum of the lengths of the programs is at most L. The time taken to access a program on the tape is called the mean retrieval time (MRT) i.in. i2. Each program I is of length li where i lies between 1 and n. Illustration . l1 ≤ l2 ≤ l3.

5. each item i is of weight wi and is associated with a profit pi.Assume that 3 sorted files are given. If a fraction xi of object i is placed into the knapsack. …. In order to find the solution..p2. 7. 3 and 5 units respectively. 15. All these three files are to be stored on to a tape S in some sequence that reduces the average retrieval time. Strategy 1 : non-increasing profit values . M=15. 4.p3… p7) = (10. one can follow three different srategies. then a profit pixi is earned. Let the length of files A. B and C be 7. (p1. The constrain is that all chosen objects should sum up to M Illustration Consider a knapsack problem of finding the optimal solution where. The table shows the retrieval time for all possible orders. w7) = (2. 1). 7. 5. 6. 1. 3. Order recording ABC ACB BAC BCA CAB CBA of Retrieval time 7+(7+3)+(7+3+5)=32 7+(7+5)+(7+5+3)=34 3+(3+7)+(3+7+5)=28 3+(3+5)+(3+5+7)=26 5+(5+7)+(5+7+3)=32 5+(5+3)+(5+3+7)=28 32/3 34/3 28/3 26/3 32/3 28/3 MRT 5. The problem of knapsack is to fill the available items into the knapsack so that the knapsack gets filled up and yields a maximum profit. 18. 3) and (w1.2 General Knapsack problem: Greedy method is best suited to solve more complex problems such as a knapsack problem. w2. In a knapsack problem there is a knapsack or a container of capacity M n items where.

1.e. space in M 15-4=11 11-5=6 6-2=4 4-4=0 PiXi 18*1=18 15*1=15 10*1=10 4/7*7=04 Profit= 47 units Strategy 2: non-decreasing weights The sequence of objects with non-decreasing weights is (e.b.3) then the sequence of objects with non-increasing profit is (f.0).c.66 e: P5/w5 =6/1=6 c: P3/w3 =15/5 = 3 f: P6/w6 =18/4 = 4.4/5.1.d.c.d.7.15.a. Strategy 2: maximum profit per unit of capacity used (This means that the objects are considered in decreasing order of the ratio Pi/wI) PiXI 6*1=6 3*1=3 10*1=10 5*1=05 18*1=18 4/5*15=12 Profit= 54 units space in M 15-1=14 14-1=13 13-2=11 11-3=8 8-4=4 4-4=0 a: P1/w1 =10/2 = 5 d: P4/w4 =7/7=1 g: P7/w7 =3/1=3 b: P2/w2 =5/3=1.1.e.g.c.1.Let (a.5 . Item chosen Quantity of item Remaining for inclusion included f 1 full unit C 1 full unit A 1 full unit d 4/7 unit The solution set is (1.1.6.g) represent the items with profit (10.0.g).18.f.d).4/7. Item chosen Quantity of item Remaining for inclusion included E 1 full unit G 1 full unit A 1 full unit b 1 full unit f 1 full unit c 4/5 unit The solution set is (1.0.5.b.f.0.1).b.a.

In the above problem it can be observed that. This type of knapsack problems is a general knapsack problem. To find the optimal solution and feasibility of jobs we are required to find a subset J such that each job of this subset can be completed by its deadline.1.0. If we assume that the sum of all weights exceeds M. 5.3 Job Scheduling: In a job-scheduling problem.f. . Sometimes it becomes necessary to take a fraction of some items to completely fill the knapsack. we are given a list of n jobs. Using this measure. Steps in finding the subset J are as follows: a) b) PiXI 6*1=6 10*1=10 18*1=18 15*1=15 3*1=3 2/3*5=3.1.2/3. is an optimal solution.33 Profit= 55.33 units space in M 15-1=14 14-2=12 12-4=8 8-5=3 3-1=2 2-2=0 Σ pi i ∈ J is the objective function chosen for optimization measure.c. if the sum of all the weights is ≤ M then all xi = 1.Hence.b.g. the next job to be included should be the one which increases Σ pi i ∈ J. The value of a feasible solution J is the sum of profits of all the jobs in J. A feasible solution with maximum sum of profits is to be obtained now.d) Item chosen Quantity of item Remaining for inclusion included E 1 full unit A 1 full unit F 1 full unit C 1 full unit g 1 full unit b 2/3 unit The solution set is (1.1. the sequence is (e.a. Every job i is associated with an integer deadline di ≥ 0 and a profit pi ≥ 0 for any job i. all xi’s cannot be one. profit is earned if and only if the job is completed within its deadline.1).

maxdelay(di)) = Min(5.5.p5) = (20.d3.1.p4.1) and maximum delay allowed (d1. See if this job is union with J is feasible or not.3) = 3. There are 3 units of time Time Slot [0-1] Job 1 2 3 4 yes yes yes 20 15 -5 40 [1-2] [2-3] Profit cannot accommodate .2.15.d5) = (2.p3.d2. Illustration: Consider 5 jobs with profits (p1.3. Hence there is a possibility of doing 3 jobs. If yes go to step (e) and continue else go to (iv) Search for the job with next maximum profit and go to step (b) f) Terminate when addition of no more jobs is feasible.c) Begin with J = ∅ and Σ pi = 0 i∈ J d) Add a job to J which has the largest profit e) Add another job to this J keeping in mind the following condition: i) ii) iii) iv) Search for job which has the next maximum profit. Here maximum number of jobs that can be completed is = Min(n.p2.d4.10.3).

10. 3) Write the algorithm to implement knapsack problem 4) What is 0/1 knapsack.In the first unit of time job 2 is done and a profit of 15 is gained.2.27) and delays (2. 5) Write the algorithm for job scheduling method.1. 6) Solve for 4 job with profits (100. Exercise 5 1) Write the algorithm for solving cassette-filling problem on your own.1) . 2) When one medium is not enough to store all files how do you solve it.15. in the second unit job 1 is done and a profit 20 is obtained finally in the 3rd unit since the third job is not available 4th job is done and 5 is obtained as the profit in the above job 3 and 5 could not be accommodated due to their deadlines. write algorithm and know the difference between general knapsack and 0/1 knapsack.

which deal with searching a set of solutions. . or which ask for an optimal solution satisfying some constraints can be solved using the backtracking formulation. which restrict each vector element to be chosen from the given set. Explicit constraints are rules. The major advantage of this algorithm is that if it is realized that the partial vector generated does not lead to an optimal solution then that vector may be ignored. Backtracking is a depth first search with some bounding function.1 Backtracking Problems. This search is accomplished by using a free organization. The constraints may be explicit or implicit. actually satisfy the criterion function. Backtracking algorithm determine the solution by systematically searching the solution space for the given problem. All solutions using backtracking are required to satisfy a complex set of constraints.Chapter 6 BRANCH AND BOUND and BACKTRACKING 6. The backtracking algorithm yields the proper solution in fewer trials. which determine which of the tuples in the solution space. Implicit constraints are rules. The basic idea of backtracking is to build up a vector one component at a time and to test whether the vector being formed has any chance of success.

i2.l3)=(5. Let n=3 and (l1. Hence.in is called mean retrieval time(MRT) and is given by tj =Σ lik k=1.10.1.1 Cassette filling problem: There are n programs that are to be stored on a tape of length L.1. we are required to find a permutation for the n programs so that when they are stored on the tape. the best order of recording is 3. Hence. Every program ‘i’ is of length li. the contents of which add onto a predefined value M.there are n!=6 possible orderings. the time tj needed to retrieve program ij from a tape having the programs in the order i1. In this problem.2. All programs can be stored on the tape if and only if the sum of the lengths of the programs is at most L. .….l2.j In the optimal storage on tape problem.1.2 Subset problem: There are n positive numbers given in a set. 6. the MRT is minimized.1. The desire is to find all possible subsets of this set. it is assumed that whenever a program is to be retrieved.2.3). the tape is positioned at the start end. These orderings and their respective MRT is given in the fig 6.6. ….

0) and (0.. S represents a solution to the given problem and B represents a bounding criteria if no solution can be reached.e. Consider n=6 m=30 and w[1. (1.0.1. The label to the left of a node represents the item number chosen for insertion and the label to the right represents the space occupied in M. Completion of the tree structure is left as an assignment for the reader.0.10.1.15. i.1).w3. w = (w1.6]={5. If element w(i) is included in the subset then x(i) =1.0).1..2. 6.0. The problem is to place 8 queens on this board such that no two queens are attack can attack each other.3 8 queen problem: The 8 queen problem can be stated as follows.0.18}.n] assumes either 0 or 1 value. W=w[1..Let there be n elements in the main set. .3.0.0.1.12. Consider a chessboard of order 8X8..0. Illustration.n] represent the elements of the set.…. The partial backtracking tree is shown in fig 6.w2.13.wn) vector x = x[1.1. For the above problem the solution could be (1.

In other words the exploration of a new node cannot begin until the current node is completely explored. The figure shows a partial backtracking tree.Consider the problem of 4 queens.4 Chapter 7: .10. Completion of the tree is left as an assignment for the reader.2.1 Tape filling: The branch and bound tree for the records of length (5. 6.2 Branch and Bound: The term branch and bound refer to all state space search methods in which all possible branches are derived before any other node can become the E-node. backtracking solution for this is as shown in the fig 6.3. 6.3) is as shown in fig 6.

The edges can then be assigned weights which may be either the distance between the two cities connected by the edge or the average time to drive along that section of highway. It is assumed that all the weights associated with the edges are positive. The shortest path between v0 and some other node v is an ordering among a subset of the edges. The starting vertex of the path is referred to as the source and the last vertex the destination. Hence this problem fits the ordering paradigm.1 Single-source shortest path: 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 length of a path is now defined to be the sum of the weights of the edges on that path. with the distance to be traveled as weights on the edges.GRAPH THEORETIC ALGORITHMS 7. . A motorist wishing to drive from city A to B would be interested in answers to the following questions: 1) Is there a path from A to B? 2) If there is more than one path from A to B? Which is the shortest path? The problems defined by these questions are special case of the path problem we study in this section. The graphs are digraphs representing streets. The problem is to determine the shortest path from v0 to all the remaining vertices of G.E). Consider a digraph G=(V.

then he encounters many paths. The greedy way to generate these paths in non-decreasing order of path length. the next path to be constructed should be the next shortest minimum length path. The cost of the path is 10+15+20 = 45 units. we must conceive a multistage solution to the problem and also of an optimization measure. it is cheaper than travelling along the path connecting v1 and v2 directly i. a shortest path to the nearest vertex is generated. One can also notice that. If we have already constructed i shortest paths. For this measure to be minimized.. First.Example: Consider the digraph of fig 7-1. If a person is interested travel from v1 to v2. the path v1v2 that costs 50 units. Some of them are v1 v2 = 50 units v1v3v4 v2 = 10+15+20=45 units v1v5v4v2 = 45+30+20= 95 units v1v3v4v5v4v2 = 10+15+35+30+20=110 units 1) 2) 3) 4) The cheapest path among these is the path along v1v3v4v2. Then a shortest path to the second nearest vertex is generated. it is not possible to travel to v6 from any other node. A much simpler method would be to solve it using matrix representation. As an optimization measure we can use the sum of the lengths of all paths so far generated.e. then using this optimization measure. each individual path must be of minimum length. To formulate a greedy based algorithm to generate the cheapest paths. Let the numbers on the edges be the costs of travelling along that route. and so on. One possibility is to build the shortest paths one by one. The steps that should be followed is as follows. . Even though there are three edges on this path.

1 can be continued as follows . Step 3: find out the column in which the minimum is present. In the above example the minimum is 25. Repeat step 3 followed by step 4 till all vertices are covered or single column is left. Initially retain only row v1. Step 4: compute a matrix by eliminating v1 and v3 columns. for the above example it is column v3. The resulting matrix is V2 50 10+inf 50 V4 Inf 10+15 25 V5 45 10+inf 45 V6 Inf 10+inf inf V1Vw V1V3Vw Minimum Step 5: find the minimum in each column. Hence. The second row is computed by adding 10 to all values of row v3. Now select the minimum from the resulting row. The solution for the fig 7.1 is given below V1 V1 V2 V3 V4 V5 V6 Inf 20 Inf Inf Inf V2 50 Inf 20 Inf Inf V3 10 15 Inf Inf Inf V4 Inf Inf 15 30 3 V5 45 10 inf 35 Inf V6 Inf Inf Inf Inf Inf - Step 2: consider v1 to be the source and choose the minimum entry in the row v1.Step 1: find the adjacency matrix for the given graph. this is the node that has to be next visited. The adjacency matrix for fig 7. In the above table the minimum in row v1 is 10.

A sub-graph t = (V. .2 Minimum-Cost spanning trees Let G=(V. Above figure shows the complete graph on four nodes together with three of its spanning tree.V1Vw V1V3V4Vw Minimum V2 50 25+20 45 V5 45 25+35 45 V6 Inf 25+inf inf V1Vw V1V3V4V2V w Minimum V5 45 45+10 45 V6 Inf 45+inf inf V6 Inf 45+inf inf V1Vw V1V3V4V2V5Vw Minimum Finally the cheapest path from v1 to all other vertices is given by V1V3V4V2V5. 7.E) be an undirected connected graph.E 1) of G is a spanning tree of G if and only if t is a tree.

For example. Adding an edge from B to the spanning tree creates a cycle.v) to be included in A is a . The next edge to include is chosen according to some optimization criterion. the edges have weights assigned to them. The next edge(u. There are two possible ways to interpret this criterion.Spanning trees have many applications. Kirchoff’s second law is used on each cycle to obtain a circuit equation. A minimal subgraph with n vertices must have at least n-1 edges and all connected graphs with n-1 edges are trees. In the first. the length of the link. Let B be the set of network edges not in the spanning tree. First. then the selection of links contains a cycle. if A is the set of edges selected so far. the set of edges so far selected form a tree. and so on. Removal of any one of the links on this cycle results in a link selection of less const connecting all cities. Thse weights may represent the cost of construction. then A forms a tree. the spanning trees of G represent all feasible choice. If this is not so. 7. We are therefore interested in finding a spanning tree of G. one would then wish to select cities to have minimum total cost or minimum total length. Given such a weighted graph. this problem fits the subset paradigm. Another application of spanning trees arises from the property that a spanning tree is a minimal sub-graph G’ of G such that V(G’) = V(G) and G’ is connected. In practical situations. they can be used to obtain an independent set of circuit equations for an electric network. with minimum cost since the identification of a minimum-cost spanning tree involves the selection of a subset of the edges. If the nodes of G represent cities and the edges represent possible communication links connecting two cities. In either case the links selected have to form a tree.1 Prim’s Algorithm A greedy method to obtain a minimum-cost spanning tree builds this tree edge by edge. then the minimum number of links needed to connect the n cities is n-1. Thus. 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.2. a spanning tree for the electric network is obtained.

v5). Now consider v1 and vk as one subgraph and connect this subgraph to its closest neighbor.5 Inf Inf 19. which is either (v1. Consider the graph shown in fig 7. Next regard the tree with v1 vk and vi as one subgraph and continue the process until all n vertices have been connected by n-1 edges. which has the smallest entry in row1 of table) say Vk.v5) is v4 as it is the smallest . There are 6 vertices and 12 edges. Let this new vertex be vi.v)} is also a tree.v2) or (v1. V1 V2 V3 V4 V5 V6 V1 10 16 11 10 17 V2 10 9.5 7 Inf 12 V4 11 Inf 7 8 7 V5 10 Inf Inf 8 9 V6 17 19.e. The closest neighbor of the subgraph (v1. v3. to the vertex.5 V3 16 9. The weights are tabulated in table given below. v5). Tabulate the given weights of the edges of g in an n by n table.3. The corresponding algorithm is known as prim’s algorithm.minimum-cost edge not in A with the property that A U {(u.…vn..5 12 7 9 - Start with v1 and pick the smallest entry in row1. For Prim’s algorithm draw n isolated vertices and label them v1. v2. Start from vertex v1 and connect it to its nearest neighbor (i. Set the non existent edges as very large. Let us pick (v1.

The Kruskal algorithm can be illustrated as folows. Thus t may not be a tree at all stages in the algorithm. this method is due to kruskal. V1 to v2 =10 V1 to v3 = 16 V1 to v4 = 11 V1 to v5 = 10 V1 to v6 = 17 V2 to v3 = 9. The weight of this tree is 41. For fig 7. Next select a smallest edge that makes no circuit with previously selected edges.3 kruskal solution is as follows.4.5 V2 to v6 = 19.2.3 Kruskal’s Algorithm: There is a second possible interpretation of the optimization criteria mentioned earlier in which the edges of the graph are considered in non-decreasing order of cost.5. In fact. This interpretation is that the set t of edges so far selected for the spanning tree be such that it is possible to complete t into a tree. Continue this process until (n-1) edges have been selected and these edges will constitute the desired shortest spanning tree. 7.v6) (v4.5 V3 to v4 = 7 V3 to v6 =12 V4 to v5 = 8 . v2) in that sequence. it will generally only be a forest since the set of edges t can be completed into a tree if and only if there are no cycles in t. The three remaining edges selected following the above procedure turn out to be (v4.in the rows v1 and v5. The resulting shortest spanning tree is shown in fig 7. list out all edges of graph G in order of non-decreasing weight.v3) and (v3.

V4 to v6 = 7 V5 to v6 = 9 The above path in ascending order is V3 to v4 = 7 V4 to v6 = 7 V4 to v5 = 8 V5 to v6 = 9 V2 to v3 = 9. i. In its simplest form it requires us to determine whether there exists a path in the given graph G=(V.E) such that this path starts at vertex v and ends at vertex u. now select v4 to v6 and then v4 to v5.. which is similar to the figure 7. A more general form is to determine for a given starting . Connect v2 and v3 and finally connect v1 and v5.5 Select the minimum.4. 7. we have a minimum spanning tree.e.3 Techniques for graphs: A fundamental problem concerning graphs is the reachability problem.5 V1 to v5 = 10 V1 to v2 =10 V1 to v4 = 11 V3 to v6 =12 V1 to v3 = 16 V1 to v6 = 17 V2 to v6 = 19. v3 to v4 connect them. Thus. now if we select v5 to v6 then it forms a circuit so drop it and go for the next.

1 Breadth first search: In Breadth first search we start at vertex v and mark it as having been reached.Vertex v belonging to V all vertices u such that there is a path from v to u.3. 2) Depth first search. The vertex v at this time is said to be unexplored. The 2 search methods for this are : 1) Breadth first search. This latter problem can be solved by starting at vertex v and systematically searching the graph G for vertices that can be reached from v. The list of unexplored vertices acts as a queue and can be represented using any of the standard queue representations. All unvisited vertices adjacent from v are visited next. The first vertex on this list is the next to be explored. When this new vertex has been explored. 7. the exploration of u continues. Exploration continues until no unexplored vertex is left.3. This search process is best-described recursively. Algorithm DFS(v) { visited[v]=1 for each vertex w adjacent from v do { If (visited[w]=0)then . A vertex is said to have been explored by an algorithm when the algorithm has visited all vertices adjacent from it. At this time the exploration of the new vertex u begins. There are new unexplored vertices. The newly visited vertices have not been explored and are put onto the end of the list of unexplored vertices. The search terminates when all reached vertices have been fully explored.2 Depth first search: A depth first search of a graph differs from a breadth first search in that the exploration of a vertex v is suspended as soon as a new vertex is reached. 7. Vertex v has now been explored.

4 Heaps and Heap sort: A heap is a complete binary tree with the property that the value at each node is atleast as large as the value at its children. return (true). until it is less than or equal to one of these values.n) { // Insert a[n] into the heap which is stored in a[1:n-1] I=n. To insert an element into the heap. Algorithm Insert(a. item=a[n]. The definition of a max heap implies that one of the largest elements is at the root of the heap.DFS(w). one adds it “at the bottom” of the heap and then compares it with its parent. Algorithm insert describes this process in detail. } and (a[ I!/2 ] < item)) do . } } 7. } a[I]=item. while( (I>n) { a[I] = a[I/2]. If the elements are distinct then the root contains the largest item. grandparent. great grandparent and so on. I=I/2. A max heap can be implemented using an array an[ ].

It is clear from the algorithm and the figure that the time for insert can vary.. we use an algorithm called Adjust. The maximum elements from the max heap a[1. inserting new elements takes O(log n) time in the worst case. Thus if there are n elements in the heap.n] as a complete binary tree. It regards a[1. then adjust will rearrange elements of a[ ] such that the tree rooted at I is also a max heap..The figure shows one example of how insert would insert a new value into an existing heap of five elements. If the subtrees rooted at 2I and 2I+1 are max heaps. To delete the maximum key from the max heap. Adjust takes as input the array a[ ] and integer I and n. In the worst case the number of executions of the while loop is proportional to the number of levels in the heap. In the best case the new elements are correctly positioned initially and no new values need to be rearranged.n] can be deleted by deleting the root of the corresponding .

and finally we call Adjust(a. Algorithm Adjust(a. while (j<=n) do { if ((j<=n) and (a[j]< a[j+1])) then j=j+1. // a position for item is found a[i/2]=a[j]. i. } x=a[1]. a[1]=a[n]. a[n]. } a[j/2]=item. The last element of the array.complete binary tree.n. is copied to the root. //compare left and right child and let j be the right //child if ( item >= a[I]) then break.e.. } Algorithm Delmac(a.n-1).I.n) { j=2I.n] and store it in x { if (n=0) then { write(‘heap is empty”).x) // Delete the maximum from the heap a[1. return (false).1. j=2I. . item=a[I].

n) { for i=1 to n do Insert(a. S. deleting the maximum can be done in O(log n) time. Dromey 3.R. Horowitz. Graph Theory with applications to engineering and computer science. } } References: 1.1.i.Adjust(a.Rajasekaran 2. Therefore if there are n elements in the heap. S. for i= n to 1 step –1 do { Delmax(a. Horowitz.Sahni. To sort n elements.G.x). Return(true). Fundamentals of computer algorithms – E. Algorithm sort(a.n-1). Fundamentals of computer algorithms – E. it suffices to make n insertions followed by n deletions from a heap since insertion and deletion take O(log n) time each in the worst case this sorting algorithm has a complexity of O(n log n). Fundamentals of datastructures.Narsingh Deo 4. How to solve it by computer.i). a[i]=x. } Note that the worst case run time of adjust is also proportional to the height of the tree.Sahni 5. S.Tremblay and Sorenson .

Write programs to implement the searching techniques. Write a program to find the factorial of a number 17. Write a program to test whether a given point p(x. 11. Write programs to implement the sorting techniques. Write a program to compute the area of a circle 5. Write a program to find the sum of n numbers 15. Write a program to find whether a given number is prime or not. 10. 4. Write a recursive and iterative program for reversing a number 20. Write a program to implement the binary search 18. Write a program to compute the circumference of a circle 6. Write recursive and iterative program to find maximum and minimum in a list of numbers. Fundamentals of datastructures in PASCAL -E. 2. Write a program to find the average of n numbers 16. Write a recursive function to find the factorial of a number.y) lies on x-axis or y-axis or in I/II/III/IV quadrant. S. 7. 3. .Sahni CYCLE 1. Write a program to implement the sequential search Write a program for the method of selection sort Write a program for the method of merge sort 19. Write a program to test whether the three numbers represent the sides of a right angle triangle. Horowitz. Write a program to find the factorial of a number 12. Write a program that inputs three numbers and outputs them in ascending order. Write a program to list all prime numbers between two limits n1 and n2 8.6. Write a program to exchange the values of two variables with and without using a temporary variable 13. Write a program to find the roots of a quadratic equation. Write a program to find the number of students who have scored above 60 percent 14. 9.

a) Profiling a) One b) Compiling b) No c) two c) Testing d) none of the above 3) An algorithm produces -----------------.set of steps a) Null a) initialization a) beginning b) infinite b) Decision b) end of list c) Finite c) updation d) None of these d) computation e) all the four d) none of these 5) One of the main blocks of any iterative algorithm is 6) In sequential search. Write the algorithm to implement knapsack problem 23. Write a program to generate the fibonacci series. Write a program to solve 4 queen problem 26.21.outputs d) More than Zero 4) An algorithm is composed of -------. Write a program to implement heap sort 25. Write a program to implement kruskals method 27.is the process of executing a correct program on data sets and measuring the time and space it takes to compute the results. Write a program to implement Prim’s algorithm MODEL QUESTION PAPER I Answer the following 25 marks 1) One of the characteristics of algorithm is a) Definiteness b) Efficiency c) Both a & b d) Only a 2) ----------. Write a program to find mean median and standard deviation 24. search always begins at a) beginning of the list b) end of the list c) middle of the list d) none of these 9) --------.is not a sorting technique a) Binary Sort b) Bubble sort c)Quick sort d) Heap sort 10) The number of moves in selection sort is of order ----------------- . search always begins at the c) middle of the list 7) Binary search can be employed on an unsorted list (True/False) 8) In Binary search. 22.

35.is a technique of defining a process by itself a) Procedure b) Function c) Program d) None of these 14) Factorial of 4 is -------a) 24 b) 12 c) 4 d) 6 15) A graph that has neither self-loop nor parallel edges are called -------------a) general graph b) cyclic graph c) directed graph d) simple graph 16) A vertex having no incident edge is called --------a) pendent vertex b) vertex .20.39.18.41} is in a) ascending order b) decending order c) unsorted d) random order 12) The time complexity of merge sort is ---------a) O(nlogn) b) O(n2) c) O(n/2) d) None of these 13) -------.14.8.a) O(n2) b) O(nlogn) c) O(n) d) None of these 11) {3.

Max(di)) d) None of the above 22) What is a Heap? 23) What is a rooted tree? 24) A tree with n vertices contains ------.edges a) n .is a connected graph without any circuit a) Null graph b) Tree c) Simple graph d) None of these 20) In an 8-queen problem the size of chessboard will be a) 8 X 8 b) 4 X 4 c) 8 X 4 d) 8 X 9 21) Maximum number of jobs that can be completed in scheduling N jobs with deadline di is a) N jobs b) M jobs c) Min(N.c) isolated vertex d) none of these 17) Two adjacent edges are said to be in series if their common vertex is of degree --a) 1 b) 2 c) 3 d) 4 18) A graph without any edges and vertices is called a a) null graph b) simple graph c) complex graph d) general graph 19) ---------.

11) Demonstrate a procedure with an example to create Hash table. 5) Explain a general algorithm to find the maximum and minimum of n elements in a list.4.b) 2n c) n-1 d) 0 25) What is a computational procedure? Answer any ten 50 marks 1) Explain the different properties of an algorithm. Find an optimal sequencing of jobs.3. 10) Write a module.1.30) (d1. 4) Explain and illustrate divide and conquer algorithm to sort a set of elements.9.18.d7) = (1. 2) Briefly explain the traveling salesman problem. 12) Demonstrate the stage by stage implementation of Backtracking method to solve a 4queen problem. given N=7. 9) Explain recursive and non-recursive algorithms.2).1.3.7.6.…. 8) Describe prim’s algorithm to determine a minimum cost spanning tree. 6) Obtain an optimal merge pattern for sorted files with lengths (2. (p1.3. 13) Demonstrate the stage by state implementation of Branch and Bound method to solve 4 queen problem. 14) Discuss Briefly the 8-queen’s problem . Hand simulate the algorithm to compute 52*49. Bring out the difference between definiteness and effectiveness of an algorithm.….p7) = (3. 3) Explain and illustrate divide and conquer algorithm to search an element. which multiplies two single digit integers using repeated addition algorithm. 7) Explain the principle of greedy method.20.5.13) using greedy method.2.5.

2) Illustrate any two sorting techniques and 2 searching techniques. .3.6.5. Write down the algorithm with necessary details and justifications comment on the time performance of the algorithm.1. (p1.1). Write down the algorithm with necessary details.p7) = (10.18.7. Comment on the performance of the algorithm.4. Show all hand computations step by step clarly.3) and (w1….7.….15.w7) = (2.m=15.15) Explain briefly the Knapsack problem. Find an optimal solution for the knapsack instance N=7.5. i) ii) iii) Greedy method and Backtracking (or) Greedy method and Branch and Bound (or) Backtracking and Branch and Bound. TERM END ASSIGNMENTS 1) Illustrate the applications of the following design strategies to objtain shortest routes and the longest routes from the vertex V to all other vertices in the given network. Show all hand computations step by step clearly.

- VertexUploaded byJasonYu
- Famous Puzzels of Great MathematiciansUploaded byGan-Od Baasantseren
- Stacks and Recursion LabUploaded byPradeep Dasigi
- Problem Set 1 SolUploaded byAditya Mishra
- ABHVW04Uploaded byAnonymous RrGVQj
- Skvortsova 1999Uploaded bybabak
- Graph TheoryUploaded bylog
- Data_Warehouse_Design.pptUploaded byjlecio
- Thompson-Mooney - Acquiring Word-Meaning Mappings for Natural Language InterfacesUploaded byTomás Asurmendi
- Analysis and Design of AlgorithmsUploaded byManjunath Bj
- Senior_Mar11.pdfUploaded bymyasmreg
- Recurrences and Methods for SolutionUploaded byH Shamas Murtaza
- Analysis and Design of AlgorithmsUploaded byTriyono Setyo Nugroho
- Parallel Simulation of the Shallow Water Equations on Structured Dynamically Adaptive Triangular GridsUploaded byjuanarcos_778612
- GraphTheoryKVIUploaded byDeept Gautam
- 1-s2.0-S0301479709002576-mainUploaded byUbaid Ahmed Mughal
- FunctionsUploaded byapi-3831209
- Support Strong Domination IN Fuzzy GRAPHUploaded byIOSRjournal
- Data Structure InterviewUploaded byKumaran700
- Binary TreesUploaded byArul prakash
- sol13.pdfUploaded byAashish D
- Graph Theory in Data StructureUploaded byjsaddam709
- A Bipartite Graph Co-Clustering Approach to Ontology MappingUploaded byDuy Hoa Ngo
- Graphs of bounded local treewidthUploaded bymacngocthan
- FqaUploaded byMahesh
- Hilpert Holliday 2015 Classroom Networks d2Uploaded byAlvaroSilva
- cwhispersUploaded byMehar Malik
- 150611-150703-Design and Analysis of AlgorithmsUploaded bySmart Cool
- BinaryTrees.pdfUploaded byPradeep Kumar
- Midterm 3 ReviewUploaded byPri Garimalla