jawaharlal nehru engineering college -lm-pk.pdf · laboratory manual contents this manual is...

66
1 Jawaharlal Nehru Engineering College Laboratory Manual DESIGN AND ANALYSIS OF ALGORITHMS For Third Year Students CSE Dept: Computer Science & Engineering (NBA Accredited) Author JNEC, Aurangabad

Upload: others

Post on 22-Mar-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

1

Jawaharlal Nehru Engineering College

Laboratory Manual

DESIGN AND ANALYSIS OF ALGORITHMS

For

Third Year Students CSE

Dept: Computer Science & Engineering (NBA Accredited)

Author JNEC, Aurangabad

Page 2: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

2

FOREWORD

It is my great pleasure to present this laboratory manual for Third Year

engineering students for the subject of DESIGN AND ANALYSIS OF

ALGORITHMS keeping in view the vast coverage required for analysis of

computer algorithms.

As a student, many of you may be wondering about the subject and

exactly that has been tried through this manual.

As you may be aware that MGM has already been awarded with ISO

9000 certification and it is our aim to technically equip students taking

the advantage of the procedural aspects of ISO 9000 Certification.

Faculty members are also advised that covering these aspects in initial

stage itself, will relive them in future as much of the load will be taken

care by the enthusiastic energies of the students once they are

conceptually clear.

Dr. S.D.Deshmukh

Principal

Page 3: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

3

LABORATORY MANUAL CONTENTS

This manual is intended for the Third Year students of Computer Science

and Engineering in the subject of Design and Analysis of Algorithms.

This manual typically contains practical/Lab Sessions related to the

subject to enhance understanding.

Algorithmic is a branch of computer science that consists of designing

and analyzing computer algorithms

1. The “design” pertain to

i. The description of algorithm at an abstract level by means of

a pseudo language, and

ii. Proof of correctness that is, the algorithm solves the given

problem in all cases.

2. The “analysis” deals with performance evaluation (complexity

analysis).

Students are advised to thoroughly go though this manual rather than

only topics mentioned in the syllabus as practical aspects are the key to

understanding and conceptual visualization of theoretical aspects

covered in the books.

Good Luck for your Enjoyable Laboratory Sessions

Dr. V.B. Musande Mrs. Parminder Kaur

HOD, CSE Assoc. Prof., CSE Dept

Page 4: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

4

DOs and DON’Ts in Laboratory:

1. Make entry in the Log Book as soon as you enter the Laboratory.

2. All the students should sit according to their roll numbers starting from

their left to right.

3. All the students are supposed to enter the terminal number in the log book.

4. Do not change the terminal on which you are working.

5. All the students are expected to get at least the algorithm of the

program/concept to be implemented.

6. Strictly observe the instructions given by the teacher/Lab Instructor.

Instruction for Laboratory Teachers::

1. Submission related to whatever lab work has been completed should be

done during the next lab session. The immediate arrangements for printouts

related to submission on the day of practical assignments.

2. Students should be taught for taking the printouts under the observation of

lab teacher.

3. The promptness of submission should be encouraged by way of marking and

evaluation patterns that will benefit the sincere students.

Page 5: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

5

MGM’s

Jawaharlal Nehru Engineering College, Aurangabad

Department of Computer Science and Engineering

Vision of CSE Department:

To develop computer engineers with necessary analytical ability and human values who can creatively design, implement a wide spectrum of computer systems for welfare of the society.

Mission of the CSE Department:

I. Preparing graduates to work on multidisciplinary platforms associated with their

professional position both independently and in a team environment.

II. Preparing graduates for higher education and research in computer science and

engineering enabling them to develop systems for society development.

Programme Educational Objectives:

Graduates will be able to

I. To analyze, design and provide optimal solution for Computer Science & Engineering and multidisciplinary problems.

II. To pursue higher studies and research by applying knowledge of mathematics and fundamentals of computer science.

III. To exhibit professionalism, communication skills and adapt to current trends by engaging in lifelong learning.

Programme Outcomes (POs):

Engineering Graduates will be able to: 1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering

fundamentals, and an engineering specialization to the solution of complex

engineering problems. 2. Problem analysis: Identify, formulate, review research literature, and analyze

complex engineering problems reaching substantiated conclusions using first

principles of mathematics, natural sciences, and engineering sciences.

Page 6: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

6

3. Design/development of solutions: Design solutions for complex engineering

problems and design system components or processes that meet the specified needs with appropriate consideration for the public health and safety, and the cultural,

societal, and environmental considerations. 4. Conduct investigations of complex problems: Use research-based knowledge and

research methods including design of experiments, analysis and interpretation of data, and synthesis of the information to provide valid conclusions.

5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and

modern engineering and IT tools including prediction and modeling to complex

engineering activities with an understanding of the limitations.

6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the professional engineering practice.

7. Environment and sustainability: Understand the impact of the professional

engineering solutions in societal and environmental contexts, and demonstrate the knowledge of, and need for sustainable development.

8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of the engineering practice.

9. Individual and team work: Function effectively as an individual, and as a member

or leader in diverse teams, and in multidisciplinary settings.

10. Communication: Communicate effectively on complex engineering activities with

the engineering community and with society at large, such as, being able to comprehend and write effective reports and design documentation, make effective presentations, and give and receive clear instructions.

11. Project management and finance : Demonstrate knowledge and understanding of the

engineering and management principles and apply these to one’s own work, as a

member and leader in a team, to manage projects and in multidisciplinary environments.

12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in independent and life-long learning in the broadest context of technological

change.

Page 7: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

7

SUBJECT INDEX

Sr. No.

Title Page No.

Introduction 6

1 Program to implement Stack & Queue. 8

2 Program to implement heap sort. 13

3 Program for finding the maximum & minimum using divide & conquer method.

18

4 Program to implement merge sort or quick sort using divide and conquer method.

20

5 Program to implement knapsack problem using greedy method. 28

6 Program for finding minimum cost spanning tree using greedy method.

33

7 Program for finding all pairs shortest paths. 40

8 Program to implement traveling salesperson problem using

dynamic programming.

44

9 Program for finding shortest path for multistage graph using dynamic programming.

47

10 Program to implement 8-queens problem using backtrack method. 50

A practice question-answer set 54

Note: All the programs should be implemented using C language.

Page 8: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

8

Introduction

1 What is Algorithm?

* a clearly specified set of simple instructions to be followed to solve a problem

Takes a set of values, as input and

produces a value, or set of values, as output

* may be specified

In English as a computer program As a pseudo-code

2 Data structures

* Methods of organizing data

3 Program = algorithms + data structures 4 Why need algorithm analysis?

a. writing a working program is not good enough

b. The program may be inefficient!

c. If the program is run on a large data set, then the running

time becomes an issue

Algorithm Analysis

* We only analyze correct algorithms / programs

* An algorithm is correct If, for every input instance, it halts with the correct

output

* Incorrect algorithms

Might not halt at all on some input instances Might halt with other than the desired answer

* Analyzing an algorithm

Predicting the resources that the algorithm requires

Resources include

Memory Communication bandwidth

Computational time (usually most important)

* Factors affecting the running time

computer compiler

algorithm used

input to the algorithm

The content of the input affects the running time

typically, the input size (number of items in the input) is the main consideration

E.g. sorting problem the number of items to

be sorted

E.g. multiply two matrices together the total number of elements in the two matrices

* Machine model assumed

Instructions are executed one after another, with no

concurrent operations Not parallel computers

Page 9: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

9

Example

Calculate

Algorithm to solve this example:

int sum(int n)

{

int psum; psum=0; // Line 1

for(int i=0;i<=n;i++) // Line 2

psum=psum+i*i*i; // Line 3

return psum; // Line 4

}

This is how we calculate time complexity

* Lines 1 and 4 count for one unit each * Line 3: executed N times, each time four units

* Line 2: (1 for initialization, N+1 for all the tests, N for all the

increments) total 2N + 2

* total cost: 6N + 4 O(N)

Time complexity can be classified as:

* Worst-case running time of an algorithm

The longest running time for any input of size n An upper bound on the running time for any input

guarantee that the algorithm will never take longer

Example: Sort a set of numbers in increasing order; and the

data is in decreasing order

The worst case can occur fairly often E.g. in searching a database for a particular piece of

information

* Best-case running time

sort a set of numbers in increasing order; and the data is already in increasing order

* Average-case running time

May be difficult to define what “average” means, usually it is

taken as average of best and worst values.

N

i

i1

3

Page 10: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

10

1. Program to implement Stack & Queue.

Aim: Write a program to implement stack & queue.

Theory:

1. Implementation of Stack Stack is a data structure which works based on principle of last in first out (LIFO). In

computing world, stack data structure can be applied in many applications such as

parsing syntax of expressions, run-time memory management (used in Java virtual

machine) and solving search problem.

Figure 1.1 : Stack operations ( A, B ,C shows push and D,E shows Pop)

By seeing the figure above you can see that this data structure is really a restricted list.

You have restricted the access to one end of the list by using the pop and push operations.

The result of this restriction is that items in the list will be stored one on top of the other.

We must first remove all the items above it till you get to the bottom item. "Last-In, First-

Out" or LIFO, which is used to describe the behavior, since the last item to enter the stack

is the first item to leave the stack. The top item is the item always the last item to enter

the stack and it is always the first item to leave the stack since no other items can be

removed until the top item is removed.

Stack Operations:

push(new-item :item-type)

Adds an item onto the stack. top():item-type

Returns the last item pushed onto the stack. pop()

Removes the most-recently-pushed item from the stack.

is-empty():Boolean True iff no more items can be popped and there is no top item.

is-full():Boolean True iff no more items can be pushed.

get-size():Integer

Returns the number of elements on the stack.

Page 11: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

11

Algorithm:

PUSH

Step-1: Increment the 'top' by 1

Step-2: Check for the 'stack overflow' condition. If stack not overflowing then go to step-

3 else say "stack overflow" and quit

Step-3: Put the new element at the position pointed by 'top'

Program Snippets:

void push() {

if(top == SIZE - 1) {

printf("Stack is full (overflow)”) ; getch() ; return ;

} top++ ;

printf("Enter the element to PUSH : ") ; scanf("%d", &stack[top]) ; }

POP

Step-1: Check for the 'stack underflow' condition. If stack not empty then go to step-2

else say "stack underflow" and quit

Step-2: Display and remove element at the position pointed by 'top'

Step-3: Decrement the top by 1.

Program Snippets:

void pop() {

if(top == -1) {

printf("Stack is empty (underflow)”) ; getch() ; return ;

} printf("The POPPED element is : %d, stack[top]) ;

getch() ; top-- ;

}

void display()

{ if(top == -1) {

Page 12: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

12

printf("Stack is empty (underflow)”) ; getch() ;

return ; }

printf("The elements in stack from TOP are:") ; for(i = top ; i >= 0 ; i--) printf(" %d", stack[i]) ;

getch() ; }

Output:

For Push operation: Enter the element to push: 10

For Push operation: Enter the element to push: 20

For Push operation: Enter the element to push: 30

For Pop operation: The POPPED element is: 30

For Display operation: The elements in stack from TOP are: 20 10

Conclusion:

All operations except get-size() can be performed in O(1) time. get-size() runs in at worst

O(N).

2. Implementation of Queue Queue is a data structure which works based on principle of first in first out (FIFO). In

computing world, queue data structure can be applied in many applications FCFS

scheduling algorithm, buffer of printer.

Figure 1.2: Queue operations ( Shows Insert and Delete)

By seeing the figure above you can see that this data structure is really a restricted list.

You have restricted the access to two ends of the list by using the insert and delete

operations. The result of this restriction is that items in the list will be stored from the

REAR end and can be removed from the other end called FRONT.

Queue Operations:

insert(new-item :item-type) Adds an item onto the queue from rear end.

delete()

Removes the last entered item from the queue.

is-empty():Boolean True iff no more items can be deleted.

is-full():Boolean

Page 13: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

13

True iff no more items can be inserted. get-size():Integer

Returns the number of elements in the queue.

Algorithm:

INSERT

Step-1: Increment the 'rear' by 1

Step-2: Check for the 'queue overflow' condition. If queue not overflowing then go to

step-3 else say "queue overflow" and quit

Step-3: Put the new element at the position pointed by 'rear'

Program Snippets:

void insert() {

if(rear == SIZE - 1) { printf("Queue is full (overflow)”) ;

getch() ; return ;

} rear++ ; printf("Enter the element to insert : ") ;

scanf("%d", &queue[rear]) ; }

DELETE

Step-1: Check for the 'queue underflow' condition. If queue not empty then go to step-2

else say "queue underflow" and quit

Step-2: Display and remove element at the position pointed by 'front'

Step-3: Increment the front by 1.

Program Snippets:

void delete() {

if(front == -1) { printf("Queue is empty (underflow)”) ;

getch() ; return ;

} printf("The deleted element is : %d, queue[front]) ; getch() ;

front-- ;

Page 14: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

14

}

void display() {

if(front == -1) { printf("Queue is empty (underflow)”) ;

getch() ; return ;

} printf("The elements in Queue are:") ; for(i = rear ; i >= front; i--)

printf(" %d", queue[i]) ; getch() ;

}

Output:

For Insert operation: Enter the element to insert: 10

For Insert operation: Enter the element to insert: 20

For Insert operation: Enter the element to insert: 30

For Delete operation: The deleted element is: 10

For Display operation: The elements in queue are: 20 30

Conclusion:

All operations except get-size() can be performed in O(1) time. get-size() runs in at worst

O(N).

Page 15: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

15

2. Program to implement heap sort.

Aim: Write a program to implement heap sort.

Theory:

Heapsort (method) is a comparison-based sorting algorithm, and is part of the selection

sort family. Although somewhat slower in practice on most machines than a good implementation of quick sort, it has the advantage of a worst-case Θ (n log n) runtime.

Heapsort is an in-place algorithm.

It begins by building a heap out of the data set, and then removing the largest item and placing it at the end of the sorted array. After removing the largest item, it reconstructs the heap, removes the largest remaining item, and places it in the next open position from

the end of the sorted array. This is repeated until there are no items left in the heap and the sorted array is full. Elementary implementations require two arrays - one to hold the

heap and the other to hold the sorted elements.

Heapsort inserts the input list elements into a heap data structure. The largest value (in a max-heap) or the smallest value (in a min-heap) are extracted until none remain, the

values having been extracted in sorted order. The heap's invariant is preserved after each extraction, so the only cost is that of extraction.

During extraction, the only space required is that needed to store the heap. In order to achieve constant space overhead, the heap is stored in the part of the input array that has

not yet been sorted.

Max-Heap : The value sorted in a node is greater than or equal to the values stored at its children . Since there are no nodes at level l unless level l − 1 is completely filled, a heap

can be stored in an array level by level (beginning with the root), left to right within each level.

The ROOT is always stored at A[1]

PARENT(i) = [i/2]

LEFT-CHILD(i) = 2i

RIGHT-CHILD(i) = 2i+1

Length[A]: number of elements in the array A; Heapsize[A]: number of elements in the heap stored within

array A. Restore the heap property : If A[i]’s left subtree and right substree are Max-Heaps, but A[i] violates the heap property, i.e., A[i] is smaller than its children, Max-Heapify(A, i) is

called to let A[i] “float down” in the max-heap so that the subtree rooted at index i becomes a Max-Heap.

Algorithm Max-Heapify(A, i) // Restore the heap property for the tree rooted at i. When called, the left and right subtrees must be Max-Heaps.

if (A[i] ≥ A[2i]) AND A[i] >= A[2i+1]) return;

else let j be the largest child of i;

Page 16: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

16

exchange A[i] and A[j]; Max-Heapify(A, j);

Build a heap:

Use Max-Heapify in a bottom-up fashion to convert A[1..n] to a Max-Heap. Algorithm Build-Max-Heap(A)

Heapsize[A] = Length[A]; for i := Length[A]/2 downto 1

Max-Heapify(A, i); Heap Sort:

Algorithm Heapsort(A)

Build-Max-Heap(A); ---------------------------Θ (n) for i := n to 2 exchange A[1] and A[i];

Heapsize[A] := Heapsize[A] - 1; Max-Heapify (A, 1); ----------------------------Θ (lgn)

Remove Max element from the max heap tree:

Algorithm Extract-Max(A) // Remove and return the max. MAX := A[1];

Exchange A[1] and A[Heapsize[A]]; Heapsize := Heapsize - 1; Max-Heapify(A, 1);

return MAX;

Page 17: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

17

Figure 2.1: Building Max-Heap tree

Page 18: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

18

Figure 2.2: Sorting using Heap sort

Program Snippets:

void main() {

int *x,i,n; int temp; void heap(int *,int);

clrscr(); fflush(stdin);

printf(" Heap Sort"); printf("Enter How many Numbers : "); scanf("%d",&n);

x = (int *)malloc(n * sizeof(int)); for(i=0;i<n;i++)

{ fflush(stdin);

Page 19: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

19

scanf("%d",&x[i]); }

heap(x,n);

for(i=n-1;i>=1;i--) { temp = x[i];

x[i] = x[0]; x[0] = temp;

heap(x,i-1); }

printf("Resultant Array"); for(i=0;i<n;i++)

printf("%d ",x[i]); free(x); getch();

}

void heap(int *a,int n) { int i,temp;

for(i=n/2;i>=0;i--) {

if(a[(2*i)+1] < a[(2*i)+2] && (2*i+1)<=n && (2*i+2)<=n) { temp = a[(2*i)+1];

a[(2*i)+1] = a[(2*i)+2]; a[(2*i)+2] = temp;

} if(a[(2*i)+1] > a[i] && (2*i+1)<=n && i<=n) {

temp = a[(2*i)+1]; a[(2*i)+1] = a[i];

a[i] = temp; } }

}

Output: Heap Sort

Enter How many Numbers :10 4 1 3 2 16 9 10 14 8 7

Resultant Array : 1 2 3 4 7 8 9 10 14 16

Conclusion: Complexity of heap sort is : Θ (n)+O(nlgn) = O(nlgn).

Page 20: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

20

3. Program for finding the maximum & minimum using

divide & conquer method.

Aim: Write a program for finding the maximum & minimum using divide &

conquer method.

Theory:

Divide-and-Conquer Algorithms: These algorithms have the following outline: To

solve a problem, divide it into sub problems. Recursively solve the sub problems. Finally glue the resulting solutions together to obtain the solution to the original problem.

Progress here is measured by how much smaller the sub problems are compared to the original problem.

Finding Maximum/minimum numbers in a list:

Suppose we wish to find the minimum and maximum items in a list of numbers. How

many comparisons does it take? Algorithm StraightMaxMin(a,n,max,min)

{ max=min=a[1];

for i=2 to n do { if (a[i]>max) then max = a[i];

if (a[i]<min) then min = a[i]; }

}

Let us analyze the number of comparisons made for finding the maximum. It can be seen that the complexity will be proportional to the number of comparisons. To find the

maximum, we see from the for-loop that N–1 comparisons will be made. The algorithm for finding the minimum follows in a similar way. If we find the minimum after finding the maximum, it is enough to find the minimum among the remaining N–1 numbers

(assuming there are at least 2 numbers; otherwise, maximum and minimum will be the same). So to find the maximum and minimum of N numbers, we need (N–1) + (N–2) =

2N –3 comparisons. Now, let us see whether we can develop an algorithm whose complexity is better than that of solution 1 using the divide–and–conquer method.

This will be the basic principle of the algorithm to find the maximum and minimum of an array of numbers. The main idea of using the divide–and–conquer strategy is described

below: Divide array A into two sub-parts A1 and A2 such that A1 and A2 together

form A.

Find the maximum and minimum of each by recursive application of the algorithm.

The maximum and minimum of A can be computed from the maximum and minimum of A1 and A2 by making two comparisons.

Page 21: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

21

Algorithm MINMAX (A, L, U, mx, mn);

( A is an array ranging between L and U; mx and mn will have the maximum and minimum value respectively when the procedure terminates )

var l1 , l2 , U1,U2: integer ; ( Local Variables ) mx1,mx2,mn1,mn2: integer ; (Local Variables ) {

If (U == L) then max=min=a[L]; else If (U– L+1) = 2 then

{ if A[L] > A[U] then mx:= A[L]; ( maximum )

mn:= A[U]; ( minimum )

else mx:= A[U]; ( maximum )

mn:= A[L]; ( minimum ) } else

{ // Split A into two halves A1 and A2 with lower and upper indices to be l1 , U1 and l2 ,

U2 respectively; call MINMAX (A, l1 , U1 , mx1 , mn1 ); call MINMAX (A, l2, U2, mx2, mn2);

mx :=maximum (mx1 , mx2 ); (maximum returns the maximum of the two values)

mn := minimum (mn1 , mn2 ); (minimum returns the minimum of the two values)

}

}

Input: 22 13 -5 -8 15 60 17 31 47

Output: Maximum: 60

Minimum : -8

Conclusion: Thus we saw using StraightMaxMin algorithm, we need (N–1) + (N–2) = 2N –3 comparisons. If we solve the same problem using divide and conquer technique number

of comparisons are reduced to 3n/2 – 2. This saves 25% comparisons.

Page 22: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

22

4. Program to implement merge sort or quick sort using

divide & conquer method.

Aim: Write a program to implement merge sort or quick sort using divide

and conquer method. Theory:

Divide-and-Conquer Algorithms: These algorithms have the following outline: To solve a problem, divide it into sub problems. Recursively solve the sub problems. Finally

glue the resulting solutions together to obtain the solution to the original problem. Progress here is measured by how much smaller the sub problems are compared to the original problem.

You have already seen an example of a divide and conquer algorithm in 3 rd assignment.

First we will see MERGE SORT. The idea behind merge sort is to take a list, divide it into two smaller sub lists, conquer

each sub list by sorting it, and then combine the two solutions for the sub problems into a single solution. These three basic steps {divide, conquer, and combine {lie behind most

divide and conquer algorithms. With merge sort, we kept dividing the list into halves until there was just one element

left. In general, we may divide the problem into smaller problems in any convenient fashion. Also, in practice it may not be best to keep dividing until the instances are

completely trivial. Instead, it may be wise to divide until the instances are reasonably small, and then apply an algorithm that is faster on small instances.

Merge-sort is based on the divide-and-conquer paradigm. The Merge-sort algorithm can be described in general terms as consisting of the following three steps:

1. Divide Step

If given array A has zero or one element, return S; it is already sorted. Otherwise, divide A into two arrays, A1 and A2, each containing about half

of the elements of A. 2. Recursion Step

Recursively sort array A1 and A2. 3. Conquer Step

Combine the elements back in A by merging the sorted arrays A1 and A2

into a sorted sequence. We can visualize Merge-sort by means of binary tree where each node of the tree

represents a recursive call and each external node represent individual elements of given array A. Such a tree is called Merge-sort tree. The heart of the Merge-sort algorithm is conquer step, which merge two sorted sequences into a single sorted sequence.

Page 23: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

23

Figure 4.1: Binary tree for merge sort

To begin, suppose that we have two sorted arrays A1[1], A1[2], . . , A1[M] and A2[1], A2[2], . . . , A2[N]. The following is a direct algorithm of the obvious strategy of

successively choosing the smallest remaining elements from A1 to A2 and putting it in A.

Merging two sorted subarrays:

Algorithm Merge (low, high, mid) // a [low: high] is a global array containing two sorted subsets in a [low: mid} and in a

[mid+1: high]. The goal is to merge these two sets into a single set residing in a [low: high]. b [] is an auxiliary global array.

{ h=low;i=low;j=mid+1;

while((h<=mid) and ( j<=high)) do {

If(a[h]<=a[j]) then

{ b[i]=a[h]; h=h+1;

} else {

b[i]=a[j]; j=j+1; }

i=i+1; } if(h>mid) then

for(k=j to high do {

b[i]=a[k]; i=i+1; } else

for(k=h to mid do {

b[i]=a[k]; i=i+1; } for k=low to high do

a[k]=b[k];

Page 24: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

24

}

Merge Sort

Algorithm MergeSort(low,high)

// a [low: high] is a global array to be sorted. { if(low<high) then

{ // divide array into subarrays ;

mid=[(low+high)/2]; MergeSort(low,mid); MergeSort(mid+1,high);

// combine the sorted subarrays Merge(low,mid,high);

} }

Program Snippets:

void mergeSort(int numbers[], int temp[], int array_size) {

m_sort(numbers, temp, 0, array_size - 1); }

void m_sort(int numbers[], int temp[], int left, int right) {

int mid; if (right > left)

{ mid = (right + left) / 2; m_sort(numbers, temp, left, mid);

m_sort(numbers, temp, mid+1, right); merge(numbers, temp, left, mid+1, right);

} }

void merge(int numbers[], int temp[], int left, int mid, int right) {

int i, left_end, num_elements, tmp_pos; left_end = mid - 1; tmp_pos = left;

num_elements = right - left + 1; while ((left <= left_end) && (mid <= right))

{ if (numbers[left] <= numbers[mid]) {

temp[tmp_pos] = numbers[left]; tmp_pos = tmp_pos + 1;

Page 25: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

25

left = left +1; }

else {

temp[tmp_pos] = numbers[mid]; tmp_pos = tmp_pos + 1; mid = mid + 1;

} }

while (left <= left_end) { temp[tmp_pos] = numbers[left];

left = left + 1; tmp_pos = tmp_pos + 1;

} while (mid <= right) {

temp[tmp_pos] = numbers[mid]; mid = mid + 1;

tmp_pos = tmp_pos + 1; } for (i=0; i <= num_elements; i++)

{ numbers[right] = temp[right];

right = right - 1; } }

Input: 13 -5 -8 15 60 17 31 47

Output: Sorted numbers are: -8 -5 13 15 17 31 47 60

Conclusion:

Performance Analysis of Merge Sort

Let T(n) be the time taken by this algorithm to sort an array of n elements dividing A into sub arrays A1 and A2 takes linear time. It is easy to see that the Merge (A1, A2, A) also

takes the linear time. Consequently, T(n) = T(n/2) + T(n/2) + θ(n)

for simplicity T(n) = 2T (n/2) + θ(n)

The total running time of Merge sort algorithm is O(n lg n), which is asymptotically

optimal like Heap sort, Merge sort has a guaranteed n lg n running time. Merge sort

required (n) extra space. Merge is not in-place algorithm. The only known ways to

merge in-place (without any extra space) are too complex to be reduced to practical program.

Page 26: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

26

Now we will study Quick Sort

The basic concept is to pick one of the elements in the array as a pivot value around

which the other elements will be rearranged. Everything less than the pivot is moved left of the pivot (into the left partition). Similarly, everything greater than the pivot goes into

the right partition. At this point each partition is recursively quick sorted.

The Quick sort algorithm is fastest when the median of the array is chosen as the pivot value. That is because the resulting partitions are of very similar size. Each partition splits

itself in two and thus the base case is reached very quickly.

In practice, the Quick sort algorithm becomes very slow when the array passed to it is

already close to being sorted. Because there is no efficient way for the computer to find the median element to use as the pivot, the first element of the array is used as the pivot. So when the array is almost sorted, quick sort doesn't partition it equally. Instead, the

partitions are asymmetrical like in Figure 4.2. This means that one of the recursion branches is much deeper than the other, and causes execution time to go up. Thus, it is

said that the more random the arrangement of the array, the faster the Quicksort Algorithm finishes.

Figure 4.2: The ideal Quicksort on a random array

Page 27: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

27

Quick sort works by partitioning a given array A[p . . r] into two non-empty sub array A[p . . q] and A[q+1 . . r] such that every key in A[p . . q] is less than or equal to every key in

A[q+1 . . r]. Then the two sub arrays are sorted by recursive calls to Quick sort. The exact position of the partition depends on the given array and index q is computed as a part of

the partitioning procedure.

Algorithm QuickSort(p,q) // Sorts the elements a[p],……,a[q] which reside in the global array a[1 : n] into

ascending order. {

if( p < r) then // if there are more than one element { // divide P into two sub problems

j=Partition (a, p,q+1); // j is the position of the partitioning element

// solve the sub problems QuickSort (p, j -1); QuickSort (j + 1, q);

} }

As a first step, Quick Sort chooses as pivot one of the items in the array to be sorted. Then array is then partitioned on either side of the pivot. Elements that are less than or

equal to pivot will move toward the left and elements that are greater than or equal to pivot will move toward the right.

Partitioning the Array

Partitioning procedure rearranges the sub arrays in-place.

Algorithm Partition (a, m, p) // Within a[m],a[m+1],….,a[p-1] the elements are rearranged in such a manner that if

initially t=a[m] then after completion a[q]=t for some q between m and p-1, a[k] |<=t for m<=k<q, and a[k]>=t for q<k<p. q is returned. Set a[p]= ∞. {

v = a[m]; i=m; j=p; repeat

{ repeat i=i+1; until a[i] >=v;

repeat j = j-1;

until a[j] <=v; if i < j then exchange A[i] ↔ A[j]

} until ( i>= j); a[m]=a[j]; a[j]=v; return j;

}

Page 28: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

28

Partition selects the first key, a[p] as a pivot key about which the array will partitioned: Keys ≤ a[p] will be moved towards the left.

Keys ≥ a[p] will be moved towards the right.

Program Snippets:

void quickSort(int numbers[], int array_size)

{ q_sort(numbers, 0, array_size - 1);

} void q_sort(int numbers[], int left, int right)

{ int pivot, l_hold, r_hold;

l_hold = left; r_hold = right;

pivot = numbers[left]; while (left < right)

{ while ((numbers[right] >= pivot) && (left < right)) right--;

if (left != right) {

numbers[left] = numbers[right]; left++; }

while ((numbers[left] <= pivot) && (left < right)) left++;

if (left != right) { numbers[right] = numbers[left];

right--; }

} numbers[left] = pivot; pivot = left;

left = l_hold; right = r_hold;

if (left < pivot) q_sort(numbers, left, pivot-1); if (right > pivot)

q_sort(numbers, pivot+1, right); }

Input: 13 -5 -8 15 60 17 31 47

Output: Sorted numbers are: -8 -5 13 15 17 31 47 60

Page 29: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

29

Conclusion:

Performance Analysis of Quick Sort

The running time of quick sort depends on whether partition is balanced or unbalanced, which in turn depends on which elements of an array to be sorted are used for partitioning.

A very good partition splits an array up into two equal sized arrays. A bad partition, on other hand, splits an array up into two arrays of very different sizes. The worst partition

puts only one element in one array and all other elements in the other array. If the partitioning is balanced, the Quick sort runs asymptotically as fast as merge sort. On the other hand, if partitioning is unbalanced, the Quick sort runs asymptotically as slow as

insertion sort.

Best Case

The best thing that could happen in Quick sort would be that each partitioning stage divides the array exactly in half. In other words, the best to be a median of the keys in

A[p . . r] every time procedure 'Partition' is called. The procedure 'Partition' always split the array to be sorted into two equal sized arrays.

If the algorithm 'Partition' produces two regions of size n/2. The recurrence relation is then

T(n) = T(n/2) + T(n/2) + (n) = 2T(n/2) + (n)

OR

T(n) = (n lg n)

Page 30: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

30

5. Program to implement knapsack problem using greedy

method.

Aim: Write a program to implement knapsack problem using greedy

method. Theory:

Greedy algorithms are simple and straightforward. They are shortsighted in their approach in the sense that they take decisions on the basis of information at hand without

worrying about the effect these decisions may have in the future. They are easy to invent, easy to implement and most of the time quite efficient. Many problems cannot be solved correctly by greedy approach. Greedy algorithms are used to solve optimization problems

Greedy Approach

Greedy Algorithm works by making the decision that seems most promising at any

moment; it never reconsiders this decision, whatever situation may arise later.

As an example consider the problem of "Making Change".

Coins available are:

dollars (100 cents)

quarters (25 cents) dimes (10 cents)

nickels (5 cents) pennies (1 cent)

Problem Make a change of a given amount using the smallest possible number of

coins.

Informal Algorithm Start with nothing.

at every stage without passing the given amount. o add the largest to the coins already chosen.

Example Make a change for 2.89 (289 cents) here n = 2.89 and the solution contains 2 dollars (200 cents) 3 quarters (75 cents), 1 dime (10 cents) and 4 pennies (4 cents). The

algorithm is greedy because at every stage it chooses the largest coin without worrying about the consequences. Moreover, it never changes its mind in the sense that once a coin has been included in the solution set, it remains there.

To construct the solution in an optimal way. Algorithm maintains two sets. One contains chosen items and the other contains rejected items.

The greedy algorithm consists of four (4) function.

1. A function that checks whether chosen set of items provide a solution. 2. A function that checks the feasibility of a set.

Page 31: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

31

3. The selection function tells which of the candidates is the most promising. 4. An objective function, which does not appear explicitly, gives the value of a

solution.

Structure Greedy Algorithm

Initially the set of chosen items is empty i.e., solution set.

At each step

o item will be added in a solution set by using selection function.

o IF the set would no longer be feasible

reject items under consideration (and is never consider again).

o ELSE IF set is still feasible THEN

add the current item.

Definitions of feasibility

A feasible set (of candidates) is promising if it can be extended to produce not

merely a solution, but an optimal solution to the problem. In particular, the empty

set is always promising why? (because an optimal solution always exists)

A greedy strategy usually progresses in a top-down fashion, making one greedy choice after another, reducing each problem to a smaller one.

Greedy-Choice Property

The "greedy-choice property" and "optimal substructure" are two ingredients in the

problem that lend to a greedy strategy.

Greedy-Choice Property

It says that a globally optimal solution can be arrived at by making a locally optimal choice.

Knapsack Problem Statement We are given n objects and a knapsack or bag. Object i has a weight wi and the knapsack has a capacity m.

There are two versions of problem

I. Fractional knapsack problem

The setup is same, we can take fractions of items, meaning that the items can

be broken into smaller pieces so that we may decide to carry only a fraction of xi of item i, where 0 ≤ xi ≤ 1. If a fraction xi of object i is placed into the knapsack, then a profit pi xi is earned.

The objective is to obtain a filling of the knapsack that maximizes the total profit earned.

Page 32: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

32

The idea is to calculate for each object the ratio of value/cost, and sort them according to this ratio. Then you take the objects with the highest ratios and add them until you can’t

add the next object as whole. Finally add as much as you can of the next object.

So, for our example: v(weight) = {4, 2, 2, 1, 10}

c(profit) = {12, 1, 2, 1, 4} r = {1/3, 2, 1, 1, 5/2}

From this it’s obvious that you should add the objects: 5, 2, 3, and 4 and then as much as

possible of 1. We can choose objects like this:

Added object 5 (10$, 4Kg) completely in the bag. Space left: 11. Added object 2 (2$, 1Kg) completely in the bag. Space left: 10.

Added object 3 (2$, 2Kg) completely in the bag. Space left: 8. Added object 4 (1$, 1Kg) completely in the bag. Space left: 7.

Added 58% (4$, 12Kg) of object 1 in the bag. Filled the bag with objects worth 15.48$.

II. 0-1 knapsack problem

The setup is the same, but the items may not be broken into smaller pieces, so

we may decide either to take an item or to leave it (binary choice), but may not take a fraction of an item.

Algorithm fractional-knapsack (w, v, W) {

for i =1 to n do x[i] =0 weight = 0

while weight < W do i = best remaining item

if weight + w[i] ≤ W then x[i] = 1 weight = weight + w[i]

else x[i] = (w - weight) / w[i]

weight = W return x

}

Page 33: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

33

Program Snippets:

int n = 5; /* The number of objects */

int c[10] = {12, 1, 2, 1, 4}; /* c[i] is the *weight/ COST* of the ith object; i.e. what YOU PAY to take the object */

int v[10] = {4, 2, 2, 1, 10}; /* v[i] is the *profit VALUE* of the ith object; i.e.what YOU GET for taking the object */ int W = 15; /* The maximum weight you can take */

void simple_fill() {

int cur_w; float tot_v; int i, maxi;

int used[10];

for (i = 0; i < n; ++i) used[i] = 0; /* Not used the ith object yet */ cur_w = W;

while (cur_w > 0) { /* while there's still room*/

/* Find the best object */ maxi = -1; for (i = 0; i < n; ++i)

if ((used[i] == 0) &&((maxi == -1) || ((float)v[i]/c[i] > (float)v[maxi]/c[maxi])))

maxi = i; used[maxi] = 1; /* mark the maxi-th object as used */ cur_w -= c[maxi]; /* with the object in the bag, I can carry less */

tot_v += v[maxi]; if (cur_w >= 0)

printf("Added object %d (%d$, %dKg) completly in the bag. Space left: %d.\n", maxi + 1, v[maxi], c[maxi], cur_w); else {

printf("Added %d%% (%d$, %dKg) of object %d in the bag.\n", (int)((1 + (float)cur_w/c[maxi]) * 100), v[maxi], c[maxi], maxi + 1);

tot_v -= v[maxi]; tot_v += (1 + (float)cur_w/c[maxi]) * v[maxi]; }

} printf("Filled the bag with objects worth %.2f$.\n", tot_v);

}

Output:

Added object 5 (10$, 4Kg) completely in the bag. Space left: 11. Added object 2 (2$, 1Kg) completely in the bag. Space left: 10. Added object 3 (2$, 2Kg) completely in the bag. Space left: 8.

Added object 4 (1$, 1Kg) completely in the bag. Space left: 7. Added 58% (4$, 12Kg) of object 1 in the bag. Filled the bag with objects worth 15.48$.

Page 34: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

34

Conclusion:

Performance Analysis

If the items are already sorted into decreasing order of v i / wi, then the while-loop takes a time in O(n);

Therefore, the total time including the sort is in O(n log n). If we keep the items in heap with largest vi/wi at the root. Then

creating the heap takes O(n) time

while-loop now takes O(log n) time (since heap property must be restored after the removal of root)

Although this data structure does not alter the worst-case, it may be faster if only a small number of items are needed to fill the knapsack.

One variant of the 0-1 knapsack problem is when order of items are sorted by increasing

weight is the same as their order when sorted by decreasing value. The optimal solution to this problem is to sort by the value of the item in decreasing order. Then pick up the most valuable item which also has a least weight. First, if its

weight is less than the total weight that can be carried. Then deduct the total weight that can be carried by the weight of the item just pick. The second item to pick is the most

valuable item among those remaining. Keep follow the same strategy until we cannot carry more item (due to weight).

Page 35: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

35

6. Program for finding minimum cost spanning tree using greedy

method.

Aim: Write a program for finding minimum cost spanning tree using greedy

method.

Theory:

A spanning tree of a graph is any tree that includes every vertex in the graph. Little more formally, a spanning tree of a graph G is a sub graph of G that is a tree and contains all the vertices of G. An edge of a spanning tree is called a branch; an edge in the graph that

is not in the spanning tree is called a chord. We construct spanning tree whenever we want to find a simple, cheap and yet efficient way to connect a set of terminals (computers, cites, factories, etc.). Spanning trees are important because of following

reasons.

Spanning trees construct a sparse sub graph that tells a lot about the original graph.

Spanning trees a very important in designing efficient routing algorithms. Some hard problems (e.g., Steiner tree problem and traveling salesman problem)

can be solved approximately by using spanning trees.

Spanning trees have wide applications in many areas, such as network design, etc.

Here are some examples:

A graph G: Three (of the many possible) spanning trees from graph G:

A weighted graph G: The minimum spanning tree from weighted graph G:

Figure 6.1: Graph G and its spanning trees

To explain further upon the Minimum Spanning Tree (MST), and what it applies to, let's consider a couple of real-world examples:

Page 36: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

36

1. One practical application of a MST would be in the design of a network. For instance, a group of individuals, who are separated by varying distances, wish to

be connected together in a telephone network. Although MST cannot do anything about the distance from one connection to another, it can be used to determine the

least costly paths with no cycles in this network, thereby connecting everyone at a minimum cost.

2. Another useful application of MST would be finding airline routes. The vertices

of the graph would represent cities, and the edges would represent routes between the cities. Obviously, the further one has to travel, the more it will cost, so MST

can be applied to optimize airline routes by finding the least costly paths with no cycles.

To explain how to find a Minimum Spanning Tree, we will look at two algorithms: the Kruskal algorithm and the Prim algorithm. Both algorithms differ in their methodology,

but both eventually end up with the MST. Kruskal'’s algorithm uses edges, and Prim’s algorithm uses vertex connections in determining the MST.

Kruskal's Algorithm:

This is a greedy algorithm. A greedy algorithm chooses some local optimum (ie. picking

an edge with the least weight in a MST).

Kruskal'’s algorithm works as follows: Take a graph with 'n' vertices, keep adding the shortest (least cost) edge, while avoiding the creation of cycles, until (n - 1) edges have

been added. (NOTE: Sometimes two or more edges may have the same cost. The order in which the edges are chosen, in this case, does not matter. Different MSTs may result, but they will all have the same total cost, which will always be the minimum cost)

Algorithm KruskalMST

Input: A weighted, connected and undirected graph G = (V,E). Output: A minimal spanning tree of G.

Step 1: T .

Step 2: while T contains less than n-1edges do

Choose an edge (v,w) from E of the smallest weight.

Delete (v,w) from E. If the adding of (v,w)does not create cycle in T then

Add (v,w) to T. Else Discard (v,w).

end while

Prim's Algorithm:

This algorithm builds the MST one vertex at a time. It starts at any vertex in a graph (vertex A, for example), and finds the least cost vertex (vertex B, for example) connected to the start vertex. Now, from either 'A' or 'B', it will find the next least costly vertex

connection, without creating a cycle (vertex C, for example). Now, from either 'A', 'B', or 'C', it will find the next least costly vertex connection, without creating a cycle, and so on

it goes. Eventually, all the vertices will be connected, without any cycles, and an MST will be the result. (NOTE: Two or more edges may have the same cost, so when there is a

Page 37: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

37

choice by two or more vertices that is exactly the same, then one will be chosen, and an MST will still result)

Some important facts about spanning trees are as follows:

Any two vertices in a tree are connected by a unique path. Let T be a spanning tree of a graph G, and let e be an edge

of G not in T. The T+e contain a unique cycle.

Greediness: It is easy to see that this algorithm has the property that each edge is

examined at most once. Algorithms, like this one, which examine each entity at most once and decide its fate once and for all during that examination, are called greedy

algorithms. The obvious advantage of greedy approach is that we do not have to spend time reexamining entities.

Figure 6.2: Graph G

Applying Kruskal's Algorithm on the graph shown in figure 6.2 Using the above graph, here are the steps to the MST, using Kruskal's Algorithm:

1. N1 to N2 - cost is 1 - add to tree

2. N7 to N8 - cost is 1 - add to tree 3. N2 to N3 - cost is 2 - add to tree 4. N1 to N6 - cost is 3 - add to tree

5. N2 to N6 - cost is 4 - reject because it forms a circuit 6. N3 to N4 - cost is 4 - add to tree

7. N2 to N7 - cost is 5 - add to tree 8. N3 to N7 - cost is 6 - reject because it forms a circuit 9. N4 to N8 - cost is 6 - reject because it forms a circuit

10. N4 to N7 - cost is 7 - reject because it forms a circuit 11. N4 to N5 - cost is 7 - add to tree

We stop here, because n -1 edges have been added. We are left with the minimum spanning tree, with a total weight of 23.

Page 38: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

38

Program Snippets:

#define N 6 /* number of vertices */ #define M 15 /* number of edges in graph */

int U[N]; /* function prototypes */

void makeset (int i); int find (int i);

void merge (int p, int q); int equal (int p, int q); void initial (int n);

void test_univ (void); void pause (void); /* used mainly for test purposes */

/* function definitions */ int main()

{ int W[N][N] = {0,2,4,1,3,2, /* weighted graph */ 2,0,6,4,5,1,

4,6,0,4,2,1, 1,4,4,0,5,4, 3,5,2,5,0,6,

2,1,1,4,6,0}; int E[M][3]; /* complete set of edges */

int F[N-1][3]; /* set of edges in min. span. tree */ int num_edges = 0; /* num of edges in min. span. tree */ int next_edge = 0; /* next edge not yet considered */

int weight = 0; /* minimal spanning tree weight */ int a, b, c, i, j, k; /* counter/placeholder variables */

/* initialize set of edges */ k = 0;

for (i = 0; i < N; i++) for (j = 0; j < N; j++)

if (j > i) { E[k][0] = i; /* first vertex of edge */ E[k][1] = j; /* second vertex of edge */

E[k][2] = W[i][j]; /* weight of edge */ k++;

} /* display set of edges - before sort */ for (i = 0; i < M; i++)

{ for (j = 0; j < 3; j++) printf(" %3d", E[i][j]);

printf("\n"); }

pause();

Page 39: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

39

/* sort set of edges in non-decreasing order by weight - bubblesort */

for (i = M - 1; i > 0; i--) for (j = 0; j < i; j++)

if (E[j][2] > E[j+1][2]) { a = E[j][0]; b = E[j][1];

c = E[j][2]; E[j][0] = E[j+1][0];

E[j][1] = E[j+1][1]; E[j][2] = E[j+1][2]; E[j+1][0] = a;

E[j+1][1] = b; E[j+1][2] = c;

} /* display set of edges - after sort */

for (i = 0; i < M; i++) { for (j = 0; j < 3; j++)

printf(" %3d", E[i][j]); printf("\n"); }

/* create n disjoint subsets */

initial (N); /* initialize set of edges in min. span. tree to empty */

for (i = 0; i < N - 1; i++) for (j = 0; j < 3; j++)

F[i][j] = -1; /* '-1' denotes 'empty' */ test_univ();

/* find minimal spanning tree */

while (num_edges < N - 1) { a = E[next_edge][0]; b = E[next_edge][1];

i = find(a);

j = find(b); if (!equal(i, j))

{ merge (i, j); F[num_edges][0] = E[next_edge][0];

F[num_edges][1] = E[next_edge][1]; F[num_edges][2] = E[next_edge][2]; num_edges++;

test_univ();

}

Page 40: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

40

next_edge++; }

/* display edges comprising minimal spanning tree */

printf("\nMinimal Spanning Tree Edges:\n"); printf("F = ("); for (i = 0; i < N - 1; i++)

{ printf("(V%d,V%d)", F[i][0], F[i][1]); if (i < N - 2)

printf(", "); weight = weight + F[i][2]; }

printf(")\n"); printf("Minimal Spanning Tree Weight = %d\n", weight);

return (0); }

/*************** makeset() ***************/

void makeset (int i) { U[i] = i; }

/*************** find() ***************/

int find (int i) { int j;

j = i; while (U[j] != j)

j = U[j]; return (j); }

/*************** merge() ***************/

void merge (int p, int q) { if (p < q) U[q] = p;

else U[p] = q;

} /*************** equal() ***************/

int equal (int p, int q) { if (p == q)

return (1); else return (0);

}

/*************** initial() ***************/ void initial (int n)

Page 41: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

41

{ int i;

for (i = 0; i < n; i++) makeset(i);

} /*************** test() ***************/

void test_univ (void) /* test universe values */

{ int i; printf("\nThe disjoint subsets are:\n");

for (i = 0; i < N; i++) printf(" %3d", U[i]);

printf("\n"); }

/*************** pause() ***************/ void pause (void)

{ int i; printf("Press ENTER to continue... \n");

i = getchar(); }

Conclusion:

Performance analysis of Kruskal’s algorithm:

Creation of the priority queue

If there are e edges, it is easy to see that it takes O(elog e) time to insert the edges

into a partially ordered tree

Each deletemin operation takes O(log e) time in the worst case. Thus finding and deleting least-cost edges, over the while iterations contribute O(log e) in the worst case.

The total time for performing all the merge and find is : O(elog e) .

Page 42: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

42

7. Program for finding all pairs shortest paths.

Aim: Write a program for finding all pairs shortest paths.

Theory:

Dynamic programming is an optimization technique. Greedy vs. Dynamic Programming :

o Both techniques are optimization techniques, and both build solutions

from a collection of choices of individual elements. o The greedy method computes its solution by making its choices in a serial

forward fashion, never looking back or revising previous choices.

o Dynamic programming computes its solution bottom up by synthesizing them from smaller sub solutions, and by trying many possibilities and

choices before it arrives at the optimal set of choices. o There is no a priori litmus test by which one can tell if the Greedy method

will lead to an optimal solution.

o By contrast, there is a litmus test for Dynamic Programming, called The Principle of Optimality

Divide and Conquer vs. Dynamic Programming:

o Both techniques split their input into parts, find sub solutions to the parts, and synthesize larger solutions from smaller ones.

o Divide and Conquer splits its input at pre specified deterministic points (e.g., always in the middle)

o Dynamic Programming splits its input at every possible split points rather

than at pre-specified points. After trying all split points, it determines which split point is optimal.

Principle of Optimality

Definition: A problem is said to satisfy the Principle of Optimality if the sub solutions of an optimal solution of the problem are themselves optimal solutions for their sub problems.

Examples: o The shortest path problem satisfies the Principle of Optimality. o This is because if a,x1,x2,...,xn,b is a shortest path from node a to node b

in a graph, then the portion of xi to xj on that path is a shortest path from xi to xj.

o The longest path problem, on the other hand, does not satisfy the Principle of Optimality. Take for example the undirected graph G of nodes a, b, c, d, and e, and edges (a,b) (b,c) (c,d) (d,e) and (e,a). That is, G is a ring. The

longest (noncyclic) path from a to d to a,b,c,d. The sub-path from b to c on that path is simply the edge b,c. But that is not the longest path from b to

c. Rather, b,a,e,d,c is the longest path. Thus, the subpath on a longest path is not necessarily a longest path.

The All-Pairs Shortest Path Problem

Input: A weighted graph, represented by its weight matrix W.

Problem: find the distance between every pair of nodes.

Page 43: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

43

Dynamic programming Design: o Notation: A(k)(i,j) = length of the shortest path from node i to node j where

the label of every intermediary node is <= k.

A(0)(i,j) = W[i,j].

o Principle of Optimality: We already saw that any sub-path of a shortest

path is a shortest path between its end nodes. o Divide the paths from i to j where every intermediary node is of label <=k

into two groups:

1. Those paths that do go through node k 2. Those paths that do not go through node k.

o the shortest path in the first group is the shortest path from i to j where the label of every intermediary node is <= k-1.

o therefore, the length of the shortest path of group 1 is A(k-1)(i,j)

o Each path in group two consists of two portions: The first is from node i to node k, and the second is from node k to node j.

o the shortest path in group 2 does not go through K more than once, for otherwise, the cycle around K can be eliminated, leading to a shorter path in group 2.

o Therefore, the two portions of the shortest path in group 2 have their intermediary labels <= k-1.

o Each portion must be the shortest of its kind. That is, the portion from i to k where intermediary node is <= k-1 must be the shortest such a path from i to k. If not, we would get a shorter path in group 2. Same thing with the

second portion (from j to k). o Therefore, the length of the first portion of the shortest path in group 2 is

A(k-1)(i,k) o Therefore, the length of the 2nd portion of the shortest path in group 2 is

A(k-1)(k,j)

o Hence, the length of the shortest path in group 2 is A(k-1)(i,k) + A(k-1)(k,j) o Since the shortest path in the two groups is the shorter of the shortest paths

of the two groups, we get o A(k)(i,j)=min(A(k-1)(i,j), A(k-1)(i,k) + A(k-1)(k,j)).

Algorithm APSP(input: W[1:n,1:n];A[1:n,1:n]) {

for i=1 to n do for j=1 to n do

A(i,j) := W[i,j]; endfor endfor

for k=1 to n do for i=1 to n do

for j=1 to n do A(i,j)=min(A(i,j),A(i,k) + A(k,j)); endfor

endfor endfor

}

Page 44: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

44

The following procedure computes the above sequence of matrices by using this technique of repeated squaring.

Figure 7.1 Graph G and matrix D(4) shows shortest distance between every pair of

nodes

Program Snippets:

int d[n][n]; /* initialize d so that each edge (i,j) has the correct weight at d[i][j] and all other elements

of the array are set to INFINITY*/ /* n is number of vertices */ for(int k=0;k<n; k++)

{ for(int i=0;i<n; i++)

{ for(int j=0;j<n; j++) {

if(d[i][j]>d[i][k] + d[k][j]) {

d[i][j]=d[i][k]+d[k][j]; } }

}

Page 45: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

45

}

Input: A weighted graph, represented by its weight matrix W.

Output: A weight matrix representing shortest distance between every pair of nodes.

Conclusion:

Time Complexity Analysis: o The triple-for-loop that follows has a constant-time body, and thus takes

O(n3) time.

o Thus, the whole algorithm takes O(n3) time.

Page 46: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

46

8. Program to implement traveling salesperson problem

using dynamic programming.

Aim: Write a program to implement traveling salesperson problem using

dynamic programming. Theory:

The Traveling Salesman Problem (TSP) is a deceptively simple combinatorial problem. It can be stated very simply:

A salesman spends his time visiting n cities (or nodes) cyclically. In one tour he visits

each city just once, and finishes up where he started. In what order should he visit them to minimize the distance traveled?

Many TSP's are symmetric - that is, for any two cities A and B, the distance from A to B is the same as that from B to A. In this case you will get exactly the same tour length if

you reverse the order in which they are visited - so there is no need to distinguish between a tour and its reverse, and you can leave off the arrows on the tour diagram.

If there are only 2 cities then the problem is trivial, since only one tour is possible. For

the symmetric case a 3 city TSP is also trivial. If all links are present then there are (n-1)! Different tours for an n city asymmetric TSP. To see why this is so, pick any city as the first - then there are n-1 choices for the second city visited, n-2 choices for the third, and

so on. For the symmetric case there are half as many distinct solutions - (n-1)!/2 for an n city TSP. In either case the number of solutions becomes extremely large for large n, so

that an exhaustive search is impractical.

Figure 8.1 Graph G

For the graph G shown in figure 8.1, distance matrix:

D = {0 2 9 10, 1 0 6 4,

15 7 0 8, 6 3 12 0} Let g(i,S) be the length of shortest path starting at vertex i, going through all vertices in S,

and terminating at vertex 1.

g(2,ø) = c21 = 1 g(3, ø) = c31 = 15 g(4, ø) = c41 = 6

1 A

4

2

3

Page 47: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

47

k = 1, consider sets of 1 element:

Set {2}: g(3,{2}) = c32 + g(2, ø) = c32 + c21 = 7 + 1 = 8 p(3,{2}) = 2 g(4,{2}) = c42 + g(2, ø) = c42 + c21 = 3 + 1 = 4 p(4,{2}) = 2

Set {3}: g(2,{3}) = c23 + g(3, ø) = c23 + c31 = 6 + 15 = 21 p(2,{3}) = 3 g(4,{3}) = c43 + g(3, ø) = c43 + c31 = 12 + 15 = 27 p(4,{3}) = 3

Set {4}: g(2,{4}) = c24 + g(4, ø) = c24 + c41 = 4 + 6 = 10 p(2,{4}) = 4

g(3,{4}) = c34 + g(4, ø) = c34 + c41 = 8 + 6 = 14 p(3,{4}) = 4

k = 2, consider sets of 2 elements: Set {2,3}: g(4,{2,3}) = min {c42 + g(2,{3}), c43 + g(3,{2})} = min {3+21, 12+8}=

min {24, 20}= 20 p(4,{2,3} = 3

Set {2,4}: g(3,{2,4}) = min {c32 + g(2,{4}), c34 + g(4,{2})} = min {7+10, 8+4}= min {17, 12} = 12 p(3,{2,4} = 4

Set {3,4}: g(2,{3,4}) = min {c23 + g(3,{4}), c24 + g(4,{3})} = min {6+14, 4+27}= min {20, 31}= 20

p(2,{3,4} = 3 Length of an optimal tour:

f = g(1,{2,3,4}) = min { c12 + g(2,{3,4}), c13 + g(3,{2,4}), c14 + g(4,{2,3}) }= min {2 + 20, 9 + 12, 10 + 20} = min {22, 21, 30} = 21

Successor of node 1: p(1,{2,3,4}) = 3 Successor of node 3: p(3, {2,4}) = 4 Successor of node 4: p(4, {2}) = 2

Optimal TSP tour: 1 3 4 2 1

Algorithm TSP

Input :Number of cities n and array of costs c(i,j) i,j=1,..n (We begin from city number 1) Output:Vector of cities and total cost.

(* starting values *)

C=0 cost=0

visits=0 e=1 (*e=pointer of the visited city) (* determination of round and cost)

for r=1 to n-1 do choose of pointer j with

minimum=c(e,j)=min{c(e,k);visits(k)=0 and k=1,..,n} cost=cost+minimum e=j

C(r)=j end r-loop

C(n)=1 cost=cost+c(e,1)

Page 48: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

48

Program Snippets:

void travel(int n, const number W [] [],index P [] [], number & minlength)

{ index i, j, k; number D [1 .. n] [subset of V - {v1}];

for (i = 2; i <= n; i++)

D [i] [⊘] = W[i] [1]; for (k = 1; k <= n - 2; k++)

for (all subsets A V - {v1} V - {v1} containing k vertices) for(i such that i ≠ 1 and vi is not in A){ D [i] [A] = minimum (W [i] [j] + D [j] [A - {vj}]);

j: [vj ∊ A P[i] [A] = value of j that gave the minimum;

} D [1] [V - {v1}] = minimum (W[1] [j] + D[j] [V - {v1, vj}]);

2 ≤ j ≤ n P[1] [V - {v1}] = value of j that gave the minimum; minlength = D[1] [V - {v1}];

}

Conclusion:

• Time complexity

– Let N be the number of g(i,S) that have to be computed

N =

– Total time = O(n22n)

– This is better than enumerating all n! different tours

22

0

2)1(2

)1(

n

n

k

nk

nn

Page 49: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

49

9. Program to find shortest path for multistage graph using

dynamic programming.

Aim: Write a program to find shortest path for multistage graph using

dynamic programming. Theory: Definition: multistage graph G(V,E)

• A directed graph in which the vertices are partitioned into k≥2 disjoint sets Vi,

1≤i≤k

• If <u,v> є E, then u є Vi and v є Vi+1 for some I, 1≤i<k

• |V1|= |Vk|=1, and s(source) є V1 and t(sink) є Vk

• c(i,j)=cost of edge <i,j>

Find a minimum-cost path from s to t

Figure 9.1 (5-stage graph)

The vertex s in V1 is called the source; the vertex t in VK is called the sink. G is usually assumed to be a weighted graph. The cost of a path from node v to node w is sum of the

costs of edges in the path. The "multistage graph problem" is to find the minimum cost path from s to t. Each set Vi is called a stage in the graph.

• Many problems can be formulated as multistage graph problem

– An example: resource allocation problem

• n units of resource are to be allocated to r projects

• N(i,j) = net profit when j units of resource allocated to project i

• V(i,j) = vertex representing the state in which a total of j units have

already been allocated to projects 1,2,..,i-1

Page 50: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

50

• DP formulation

Every s to t path is the result of a sequence of k-2 decisions The principle of optimality holds (Why?) p(i,j) = a minimum-cost path from vertex j in Vi to vertex t

cost(i,j) = cost of path p(i,j)

• cost(k-1,j) = c(j,t) if <j,t> ∈ E, ∞ otherwise

• Then computing cost(k-2,j) for all j ∈ Vk-2

• Then computing cost(k-3,j) for all j ∈ Vk-3

• …

• Finally computing cost(1,s)

To find the shortest path of multi-stage graph shown in figure 9.1.

– Stage 5

• cost(5,12) = 0.0

– Stage 4

• cost(4,9) = min {4+cost(5,12)} = 4

• cost(4,10) = min {2+cost(5,12)} = 2

• cost(4,11) = min {5+cost(5,12)} = 5

– Stage 3

• cost(3,6) = min {6+cost(4,9), 5+cost(4,10)} = 7

• cost(3,7) = min {4+cost(4,9), 3+cost(4,10)} = 5

• cost(3,8) = min {5+cost(4,10), 6+cost(4,11)} = 7

– Stage 2

• cost(2,2) = min {4+cost(3,6), 2+cost(3,7), 1+cost(3,8)} = 7

• cost(2,3) = min {2+cost(3,6), 7+cost(3,7)} = 9

• cost(2,4) = min {11+cost(3,8)} = 18

• cost(2,5) = min {11+cost(3,7), 8+cost(3,8)} = 15

– Stage 1

• cost(1,1) = min {9+cost(2,2), 7+cost(2,3), 3+cost(2,4), 2+cost(2,5)} = 16

• Recording the path

– d(i,j) = value of l (l is a vertex) that minimizes c(j,l)+cost(i+1,l) in equation (5.5)

– In Figure 9.1

• d(3,6)=10; d(3,7)=10; d(3,8)=10

• d(2,2)=7; d(2,3)=6; d(2,4)=8; d(2,5)=8

• d(1,1)=2

– When letting the minimum-cost path 1,v2,v3,…,vk-1,t,

• v2 = d(1,1) = 2

• v3 = d(2,d(1,1)) = 7

• v4 = d(3,d(2,d(1,1))) = d(3,7) = 10

• So the solution (minimum-cost path) is 1 2 7 10 12 and its cost is 16

)},1(cos),({min),(cos

,1

litljcjit

EljVl i

Page 51: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

51

Algorithm / Program Snippets:

Void multistage(graph G, int k, int n, int p[] ) // The input is a k-stage graph G = (V,E) with n vertices indexed in order

// of stages. E is a set of edges and c[i][j] is the cost of <i, j>. // p[1 : k] is a minimum-cost path. {

float cost[MAXSIZE]; int d[MAXSIZE], r; cost[n] = 0.0;

for (int j=n-1; j >= 1; j--) { // Compute cost[j]. let r be a vertex such that <j, r> is an edge of G and c[j][r] + cost[r] is minimum;

cost[j] = c[j][r] + cost[r]; d[j] = r;

} // Find a minimum-cost path. p[1] = 1; p[k] =n ;

for ( j=2; j <= k-1; j++) p[j] = d[ p[ j-1 ] ]; }

Conclusion:

• Time complexity

Page 52: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

52

10. Program to implement 8-queens problem using backtrack

method.

Aim: Write a program to implement 8-queens problem using backtrack

method.

Theory: Backtracking is kind of solving a problem by trial and error. However, it is a well organized trial and error. We make sure that we never try the same thing twice. We also make sure that if the problem is finite we will eventually try all possibilities (assuming

there is enough computing power to try all possibilities).

General method

Useful technique for optimizing search under some constraints

Express the desired solution as an n-tuple (x1, . . . , xn) where each xi 2 Si, Si being a finite set

The solution is based on finding one or more vectors that maximize, minimize, or satisfy a criterion function P(x1, . . . , xn)

Sorting an array a[n] – Find an n-tuple where the element xi is the index of ith smallest element in a – Criterion function is given by a[xi] _ a[xi+1] for 1 _ i < n

– Set Si is a finite set of integers in the range [1,n] Brute force approach

– Let the size of set Si be mi – There are m = m1m2 · · ·mn n-tuples that satisfy the criterion function P – In brute force algorithm, you have to form all the m n-tuples to determine the

optimal solutions Backtrack approach

– Requires less than m trials to determine the solution – Form a solution (partial vector) and check at every step if this has any chance of success

– If the solution at any point seems not-promising, ignore it – If the partial vector (x1, x2, . . . , xi) does not yield an optimal solution, ignore

mi+1 · · ·mn possible test vectors even without looking at them

• All the solutions require a set of constraints divided into two categories: explicit and

implicit constraints Definition 1 Explicit constraints are rules that restrict each xi to take on values only from

a given set. – Explicit constraints depend on the particular instance I of problem being solved – All tuples that satisfy the explicit constraints define a possible solution space for

I – Examples of explicit constraints

* xi >= 0, or all nonnegative real numbers * xi = {0, 1} * li<= xi <= ui

Definition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy the criterion function.

Page 53: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

53

– Implicit constraints describe the way in which the xis must relate to each other. • Determine problem solution by systematically searching the solution space for the given

problem instance – Use a tree organization for solution space

• 8-queens problem Place eight queens on an 8 × 8 chessboard so that no queen attacks another queen

* Identify data structures to solve the problem * First pass: Define the chessboard to be an 8 × 8 array

* Second pass: Since each queen is in a different row, define the chessboard solution to be an 8-tuple (x1, . . . , x8), where xi is the column for ith queen

– Identify explicit constraints

* Explicit constraints using 8-tuple formulation are Si = {1, 2, 3, 4, 5, 6, 7, 8}, 1 <= i <= 8

Solution space of 88 8-tuples – Identify implicit constraints

* No two xi can be the same, or all the queens must be in different

columns · All solutions are permutations of the 8-tuple (1, 2, 3, 4, 5, 6, 7, 8)

· Reduces the size of solution space from 88 to 8! tuples

• No two queens can be on the same diagonal

• The solution above is expressed as an 8-tuple as 4, 6, 8, 2, 7, 1, 3, 5

1 2 3 4 5 6 7 8

1 Q

2 Q

3 Q

4 Q

5 Q

6 Q

7 Q

8 Q

Figure 10.1 (One Solution to 8-queen’s problem)

The 8 Queens Problem:

Given is a chess board. A chess board has 8x8 fields. Is it possible to place 8 queens on this board, so that no two queens can attack each other?

The n Queens problem:

Given is a board of n by n squares. Is it possible to place n queens (that behave exactly

like chess queens) on this board, without having any one of them attack any other queen?

1. Another example of a problem that can be solved with backtracking: o Place 8 queens on a 8x8 chess board so that no queen attack each other

(find all solutions)

Examples Solutions:

Page 54: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

54

Figure 10.2 (Solutions to 8-queen’s problem)

Algorithm: The Backtracking Algorithm for n-Queens

Problem: Position n queens on a chessboard so that no two are in the same row, column, or diagonal.

Inputs: positive integer n. Outputs: all possible ways n queens can be placed on an n x n chessboard so that no two queens threaten each other. Each output consists of an array of integers col indexed from

1 to n, where col[i] is the column where the queen in the ith row is placed.

Program Snippets:

include <stdio.h>

define TRUE 1 define FALSE 0

int *board;

int main() {

board=(int*)calloc(8+1,sizeof(int)); board++; //here goes the userinput no of queens doesn´t matter in this code

int col; int queens=2;//example from userinput

for(col=1;col<=8;col++) placeQueens(1,col,queens);

}

void placeQueens(int row,int col,int queens) {

int i; for(i=1;i<row;i++) {

if((board[i] == col) || ((row+col)==(i+board[i]))||((row-col)==(i-board[i]))) {

Page 55: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

55

check= FALSE; }

else check=TRUE;

} if(check==TRUE) {

board[row]=col; if(row==8)

{ for(i=1;i<=queens;i++) printf("(%d,%d)",i,board[i]);

printf("\n"); }

else { for(i=1;i<=8;i++)

placeQueens(row+1,i); }

} }

Page 56: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

56

A practice question answer set

1. What is data structure?

A data structure is a way of organizing data that considers not only the

items stored, but also their relationship to each other. Advance knowledge about the relationship between data items allows designing of efficient algorithms for

the manipulation of data. 2. List out the areas in which data structures are applied extensively?

Compiler Design, Operating System,

Database Management System, Statistical analysis package, Numerical Analysis,

Graphics, Artificial Intelligence,

Simulation 3. What are the major data structures used in the following areas: RDBMS, Network

data model & Hierarchical data model? RDBMS – Array (i.e. Array of structures)

Network data model – Graph Hierarchical data model – Trees

4. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

The heterogeneous linked list contains different data types in its nodes and

we need a link, pointer to connect them. It is not possible to use ordinary pointers for this. So we go for void pointer. Void pointer is capable of storing pointer to

any type as it is a generic pointer type.

5. Minimum number of queues needed to implement a priority queue? Two. One queue is used for actual storing of data and another for storing

priorities. 6. What is the data structure used to perform recursion?

Stack. Because of its LIFO (Last In First Out) property it remembers its ‘caller’; so the function knows where to return when the function has to return.

Recursion makes use of system stack for storing the return addresses of the function calls.

Every recursive function has its equivalent iterative (non-recursive)

function. Even when such equivalent iterative procedures are written, explicit stack is to be used.

7. What are the notations used in Evaluation of Arithmetic Expressions using prefix

and postfix forms?

Polish and Reverse Polish notations.

8. Convert the expression ((A + B) * C – (D – E) ^ (F + G)) to equivalent Prefix and Postfix notations.

Page 57: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

57

Prefix Notation: ^ - * +ABC - DE + FG Postfix Notation: AB + C * DE - - FG + ^

9. Sorting is not possible by using which of the following methods? (a) Insertion

(b) Selection (c) Exchange (d) Deletion

Answer:

(d)deletion

Explanation:

Using insertion we can perform insertion sort, using selection we can perform selection sort, using exchange we can perform the bubble sort (and other

similar sorting methods). But no sorting method can be done just using deletion.

10. A binary tree with 20 nodes has null branches. Answer:

21

Explanation:

Let us take a tree with 5 nodes (n=5)

It will have only 6 (ie,5+1) null branches. In general, a binary tree with n nodes has exactly n+1 null nodes.

11. What are the methods available in storing sequential files? Straight merging,

Natural merging, Polyphase sort, Distribution of Initial runs.

12. How many different trees are possible with 10 nodes?

Answer:

1014 Explanation:

For example, consider a tree with 3 nodes (n=3), it will have the maximum combination of 5 different (ie, 23 - 3 = 5) trees.

i ii iii iv v

Null Branches

Page 58: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

58

In general:

If there are n nodes, there exist 2n-n different trees.

13. List out a few of the applications of tree data-structure in compilers/compiler design.

The manipulation of Arithmetic expression,

Symbol Table construction, Syntax analysis.

14. List out a few of the applications that make use of Multilinked Structures.

Sparse matrix,

Index generation.

15. In tree construction which is the suitable efficient data structure? (a ) Array (b) Linked list

(c) Stack (d) Queue

(e) None Answer:

(b) Linked list

16. What type of the algorithm is used in solving the 8 Queens problem?

Backtracking 17. In an AVL tree, what is the condition for balancing to be done?

The ‘pivotal value’ (or the ‘Height factor’) is greater than 1 or less than–1.

18. What is the bucket size, when overlapping and collision occur at the same time? One. If there is only one entry possible in the bucket, when the collision

occurs, there is no way to accommodate the colliding value. This results in the

overlapping of values.

19. Traverse the given tree using Inorder, Preorder and Postorder traversals.

Inorder : D H B E A F C I G J Preorder: A B D H E C F G I J

A

B C

D E F G

H I J

Given tree:

Page 59: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

59

Postorder: H D E B F I J G C A

20. There are 8, 15, 13, 14 nodes were there in 4 different trees. Which of them could have formed a full binary tree?

15. In general:

There are 2n-1 nodes in a full binary tree.

By the method of elimination: Full binary trees contain odd number of nodes. So there cannot be full

binary trees with 8 or 14 nodes, so rejected. With 13 nodes you can form a complete binary tree but not a full binary tree. So the correct answer is 15.

Note:

Full and Complete binary trees are different. All full binary trees are complete binary trees but not vice versa.

21. In the given binary tree, using array, at which location can you store the node 4?

Answer:

At location 6 Explanation:

1 2 3 - - 4 - - 5

Root LC1 RC1 LC2 RC2 LC3 RC3 LC4 RC4

Where LCn means Left Child of node n and RCn means Right Child of node n

22. Sort the given values using Quick Sort?

65 70 75 80 85 60 55 50 45 Sorting takes place from the pivot value, which is the first value of the

given elements, this is marked bold. The values at the left pointer and right pointer are indicated using L and R respectively.

65 70L 75 80 85 60 55 50 45R

Since pivot is not yet changed the same process is continued after interchanging the values at L and R positions

65 45 75 L 80 85 60 55 50 R 70 65 45 50 80 L 85 60 55 R 75 70

65 45 50 55 85 L 60 R 80 75 70 65 45 50 55 60 R 85 L 80 75 70

1

2 3

4

5

Page 60: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

60

When the L and R pointers cross each other the pivot value is interchanged with the value at right pointer. If the pivot is changed it means that

the pivot has occupied its original position in the sorted order (shown in bold italics) and hence two different arrays are formed, one from start of the original

array to the pivot position-1 and the other from pivot position+1 to end.

60 L 45 50 55 R 65 85 L 80 75 70 R

55 L 45 50 R 60 65 70 R 80 L 75 85

50 L 45 R 55 60 65 70 80 L 75 R 85

In the next pass we get the sorted form of the array.

45 50 55 60 65 70 75 80 85

23. For the given graph, draw the DFS and BFS?

BFS: A X G H P E M Y J DFS: A X H P E Y M J G

24. Classify the Hashing Functions based on the various methods by which the key value is found.

Direct method, Subtraction method, Modulo-Division method,

Digit-Extraction method, Mid-Square method,

Folding method, Pseudo-random method.

25. What are the types of Collision Resolution Techniques and the methods used in each of the type?

Open addressing (closed hashing). The methods used include:

Overflow block.

Closed addressing (open hashing) The methods used include:

Linked list, Binary tree… 26. In RDBMS, what is the efficient data structure used in the internal storage

representation? B+ tree. Because in B+ tree, all the data are stored only in leaf nodes, that

makes searching easier. This corresponds to the records that shall be stored in leaf nodes.

A

H X

G P E Y

M J

The given graph:

Page 61: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

61

27. Draw the B-tree of order 3 created by inserting the following data arriving in

sequence – 92 24 6 7 11 8 22 4 5 16 19 20 78

28. Of the following tree structure, which is efficient considering space and time complexities?

(a) Incomplete Binary Tree (b) Complete Binary Tree

(c) Full Binary Tree Answer:

(b) Complete Binary Tree. Explanation:

By the method of elimination:

Full binary tree loses its nature when operations of insertions and deletions are done. For incomplete binary trees, extra storage is required and overhead of NULL node checking takes place. So complete binary tree is the better one since

the property of complete binary tree is maintained even after operations like additions and deletions are done on it.

29. What is a spanning Tree?

A spanning tree is a tree associated with a network. All the nodes of the

graph appear on the tree once. A minimal spanning tree is a spanning tree organized so that the total edge weight between nodes is minimized.

30. Does the minimal spanning tree of a graph give the shortest distance between any

2 specified nodes?

Answer:

No.

Explanation:

Minimal spanning tree assures that the total weight of the tree is kept at its minimum. But it doesn’t mean that the distance between any two nodes involved

in the minimum-spanning tree is minimum.

31. Convert the given graph with weighted edges to minimal spanning tree.

1 3

2 4

5 410

600

200

400

310

1421

2985

612

1

1 -

5 7 19 24

4 - 6 - 8 - 1

6 - 20 22 78

92

Page 62: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

62

The equivalent minimal spanning tree is:

32. Which is the simplest file structure?

(a) Sequential (b) Indexed

(c) Random Answer:

(a) Sequential

33. Is linked-list a linear or non-linear data structure?

According to Access strategies Linked list is a linear one. According to Storage, Linked List is a Non-linear one.

34. Draw a binary Tree for the expression : A * B - (C + D) * (P / Q)

35. For the following COBOL code, draw the Binary tree?

01 STUDENT_REC. 02 NAME.

03 FIRST_NAME PIC X(10).

-

* *

A B + /

C P D Q

1

2

3

4 5

410 612

200

310

Page 63: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

63

03 LAST_NAME PIC X(10). 02 YEAR_OF_STUDY.

03 FIRST_SEM PIC XX. 03 SECOND_SEM PIC XX.

Section II – Algorithms

1. What is an ‘algorithm’? An algorithm consists of a finite set of steps that may require one or more

operations. These operations should be definite and effective. An algorithm should produce one or more output’s and may have zero or more inputs. This consists of five distinct areas:

1. to device algorithms 2. to validate the algorithms

3. to express the algorithms 4. to analyze the algorithms 5. to test the programs for the algorithms

2. What is a ‘computational procedure’?

An algorithm that does not terminate is called ‘computational procedure’. Example for such ‘computational procedure’ is an ‘operating system’.

3. Define - ‘recursive algorithm’. An algorithm is said to be recursive if the same algorithm is invoked in the

body of the algorithm. Recursive algorithms can be divided into direct recursive and indirect recursive algorithms. Direct recursive:

An algorithm that calls itself. Indirect Recursive:

An algorithm ‘A’ is said to be indirect recursive if it calls another algorithm which in-turn calls algorithm ‘A’.

4. How can you classify performance analysis? ‘Performance analysis’ can be classified as:

i. priori analysis ii. posteriori analysis

STUDENT_REC

NAME YEAR_OF_STUDY

FIRST_NAME LAST_NAME FIRST_SEM SECOND_SEM

01

02 02

03 03 03 03

Page 64: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

64

Priori Analysis: The bounds of algorithms’ computing time are obtained by formulating a

function. Posteriori Analysis:

Testing the actual computation of space and time are recorded while the algorithm is executing.

5. Define - ‘Big- O’. For the function f(n)

f(n)=O(g(n)) iff there exist positive constants c and d such that:

f(n) <=c*g(n)

for all n,n>=d. This is defined to be the worst-time complexity of the function f(n).

For example: O(n)=3n+2 because,

3n+2 <=4n for all n>=2.

6. Give various computing times and their meaning.

Few of the important computing times are: Computing Time Meaning

O(1) : constant computing time O(n) : linear computing time

O(n*n) : quadratic computing time O(n*n*n) : cubic computing time O(2*2*2*2*..............*n) : exponential computing time

7. Give the most important ‘basic designs‘of algorithms.

There are five important basic designs for algorithms. They are: i. Divide and conquer, ii. The greedy method,

iii. Dynamic programming, iv. Back-tracking,

v. Branch and bound. 8. How do ‘divide and conquer’ algorithms work?

For a function to compute on n inputs the divide and conquer strategy suggests the inputs into a k distinct subsets, 1<k<=n, yielding k sub-problems.

These sub-problems must be solved and then a method must be found to combine the sub-solutions into a solution of the whole.

An example for this approach is ‘binary search’ algorithm. The time

complexity of binary search algorithm is O(log n).

9. What is Greedy Method? The greedy method suggests that one can devise an algorithm that works

in stages, considering one input at a time. At each stage, a decision is made

regarding whether a particular input is an optimal solution. An example for solution using greedy method is ‘knapsack problem’.

Page 65: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

65

10. What is Dynamic Programming? Dynamic Programming is an algorithm design method that can be used

when the solution to a problem can be viewed as the result of a sequence of decisions. An example for algorithm using dynamic programming is ‘multistage

graphs’. 11. What are the time complexities for the following algorithms?

Binary search : O(logn) Finding maximum and minimum for a given set of numbers

: O(3n/2-2) Merge Sort : O(nlogn) Insertion Sort : O(n*n);

Quick Sort : O(nlogn) Selection Sort : O(n)

12. What is the difference between Merge Sort and Quick sort?

Both Merge-sort and Quick-sort have same time complexity i.e. O(nlogn).

In merge sort the file a[1:n] was divided at its midpoint into sub-arrays which are independently sorted and later merged. Whereas, in quick sort the division into

two sub-arrays is made so that the sorted sub-arrays do not need to be merged latter.

13. Is there any optimum solution for Matrix multiplication? Yes. Divide and conquer method suggests Strassen’s matrix multiplication

method to be used. If we follow this method, the time complexity is O(n*n*n……..*2.81) times rather O(n*n*n*………*3) times.

14. Define ‘minimum cost spanning method’. Let G=(V,E) be an undirected connected graph. A sub-graph t =(V, E’) of

G is a spanning tree of G if and only if t is a tree. To find out minimum cost spanning method we have following method’s;

Prim’s Algorithm : O(n*n)

Kruskal’s Algorithm : O(e loge)

15. Define ’articulation points’. A Vertex V in a connected graph G is an articulation point if and only if

the deletion of vertex V together will all edges incident for disconnects the graph

into two or more non-empty Components.

16. Define ‘biconnected graph’. A graph G is biconnected if and only if it contains no articulation points.

17. What are ‘explicit’ and ‘implicit’ constraints? ‘Explicit constraints’ are rules that restrict each xi to take on values only

from a given set. ‘Implicit constraints’ are rules that determine which of the tuples in the solution space of i satisfy the criterion function.

Page 66: Jawaharlal Nehru Engineering College -LM-PK.pdf · LABORATORY MANUAL CONTENTS This manual is intended for the Third Year students of Computer Science and Engineering in the subject

66

Evaluation and marking system:

Basic honesty in the evaluation and marking system is absolutely essential and in the process impartial nature of the evaluator is required in the examination system to become popular amongst the students. It is a wrong

approach or concept to award the students by way of easy marking to get cheap popularity among the students to which they do not deserve. It is a

primary responsibility of the teacher that right students who are really putting up lot of hard work with right kind of intelligence are correctly

awarded.

The marking patterns should be justifiable to the students without any ambiguity and teacher should see that students are faced with unjust

circumstances.

The assessment is done according to the directives of the Principal/ Vice-Principal/ Dean Academics.