0% found this document useful (0 votes)
12 views23 pages

Algorithms Lab

The document is a course material for the Algorithms Lab (XCS 356) at A.V.C. College of Engineering for the 2010-2011 academic year. It outlines various algorithms to be implemented in C, including sorting algorithms, shortest path algorithms, and graph traversal techniques. Additionally, it includes practical examination details and a list of programs with sample inputs and outputs.

Uploaded by

manikandan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views23 pages

Algorithms Lab

The document is a course material for the Algorithms Lab (XCS 356) at A.V.C. College of Engineering for the 2010-2011 academic year. It outlines various algorithms to be implemented in C, including sorting algorithms, shortest path algorithms, and graph traversal techniques. Additionally, it includes practical examination details and a list of programs with sample inputs and outputs.

Uploaded by

manikandan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

A.V.C.

COLLEGE OF ENGINEERING
MANNAMPANDAL, MAYILADUTHURAI-609 305

COURSE MATERIAL
FOR THE SUBJECT OF

ALGORHTHMS LAB

Subject Code : XCS 356

Semester : V SEMESTER

Department : SOFTWARE ENGINEERING & IT [PG]

Academic Year : 2010-2011

Name of the Faculty : [Link], [Link]

Designation and Dept : Lecturer


UNIVERSITY SYLLABUS

XCS 356 – ALGORITHMS LAB

Implementation of following problems using C

1. Binary Search Algorithm

2. Finding Maximum and Minimum of a given list

3. Merge sort

4. Quick sort using divide-and-conquer algorithm

5. Shortest path algorithms (any 2 algorithms)

6. Traversals and Searching in Graphs

7. Minimal Spanning Tree Algorithm

8. Knapsack problem
ANNA UNIVERSITY, TRICHY.

[Link] DEGREE PRACTICAL EXAMINATION – April 2009

[Link] Software Engineering – V Semester

XCS 356 – ALGORITHMS LAB

Time: 3 Hours Maximum: 100 Marks

College Code & Name: 801 – A.V.C COLLEGE OF ENGINEERING

Date: 24.10.2009 Session: FN Batch: I

1. Implement a C program for Bubble sort.


2. Develop a C program to find the solution of Knapsack Problem.

Aim& Program Debugging Output & Record Viva Total


Algorithm Coding (20) Result (10) (10) (100)
(15) (25) (20)

INTERNAL EXAMINER EXTERNAL EXAMINER


LIST OF PROGRAMS

CONTENTS

[Link] TABLE OF CONTENTS PAGE NUMBER

1 LINEAR SEARCH 1

2 BINARY SEARCH 2

3 FINDING MAXIMUM AND 3


MINIMUM OF N NUMBERS
4 MARGE SORT 4

5 QUICK SORT 5

6 SHORTEST PATH (DIJIKSTAR’S) 6

7 SHORTEST PATH (WARSHALL’S) 8

8 GRAPH TRAVERSAL (BREADTH- 10


FIRST SEARCH)
9 GRAPH TRAVERSAL (DEPTH- 12
FIRST SEARCH)
10 MINIMUM SPANNING TREE 14

11 KNAPSACK PROBLEM 16

12 TOWERS OF HANOI 17

13 BUBBLE SORT 18
1

[Link] LINEAR SEARCH

AIM:
To Write a C program to search an element using the Linear search method.

ALGORITHM:

Step 1: Start the process


Step 2: [Initialize search]
I < -1
K[N+1]<-X
Step 3: [Search the Vector]
Repeat while K[I] !=X
I<-I+1
Step 4: [Successful Search]
If I=N+1 then Write (“Unsuccessful”)
Return(0)
Step 5: Otherwise Write(“Successful Search”)
Return(1)
Step 6: Stop the process

Sample Input:

Enter the limit: 5


Enter the elements: 78 88 89 98 65
Enter the element to be searched: 65

Sample Output:

The element is in the 5th position

Result
Thus the above program is successfully executed and the result are verified.

A.V.C.C.E Course Material Department of Software Engineering & IT


[PG]
2

[Link]: 2 BINARY SEARCH

AIM:
To write a C program to search an element using the Binary search methods.

ALGORITHM:

Step 1: Start the process


Step 2: Assign low:=1; high:=n;
Step 3: While (low = high) do the following steps
Step 4: To find out the middle value using the formula mid := [(low+high)/2\
Step 5: To check the condition if ( x<a[mid]) then high := mid + 1;
Step 6: If the condition has the value that is (x>a[mid])
Then low := mid +1
Step 7: Otherwise return the mid value
Step 8: Stop the process

Sample Input:

Enter the limit: 5


Enter the Element: 3 12 25 42 58
Enter the elements to be searched: 12

Sample output:

The element is in the 2nd position

Result

Thus the above program is successfully executed and the results are verified.

A.V.C.C.E Course Material Department of Software Engineering & IT


[PG]
3

[Link]: 3 FINDING MAXIMUM AND MINIMUM

AIM:
To write a C program to find the Maximum and Minimum of a given list.

ALGORITHM:

Step 1: Start the process


Step 2: Assign max := min := a[1]
Step 3: for I:=2 to n the following steps
Step 4: To check the condition if (a[I]>max)
Step 5: If the above condition is true value then max:=a[I]
Step 6: Otherwise to check the condition if (a[I]<max)
Step 7: If the above condition is false value then min:=a[I]
Step 8: Stop the process

Sample Input:

Enter the limit: 5


Enter the elements: 15 13 25 42 58

Sample Output:

Max value = 58

Min value = 13

Result

Thus the above program is successfully executed and the results are verified.

A.V.C.C.E Course Material Department of Software Engineering & IT


[PG]
4

[Link]: 4 MERGE SORT

AIM:

To write a C program to start the numbers using the merge sort.

ALGORITHM:

Step 1: Start the Process


Step 2: Initialize a [low:high] is a global array to be sorted
Step 3: Small(P) is true if there is only one element to sort
Step 4: If there are more than one element to check the condition if (low<high)
Step 5: If the above condition is true value then divide P into sub problems
Step 6: Find where to split the set that is Mid:=[(low + high)/2]
Step 7: Solve the sub problems Mergesort(low,mid) and Mergesort(mid+1,high)
Step 8: Combine the solution Merge(low,mid,hith)
Step 9: Stop the process

Sample Input:

Enter the limit: 6

Enter the elements: 18 91 25 72 48 12

Sample Output:

The sorted list is: 12 18 25 48 72 91

Result

Thus the above program is successfully executed and the results are verified.

A.V.C.C.E Course Material Department of Software Engineering & IT


[PG]
5

[Link]: 5 QUICK SORT

AIM:

To write a C program to sort the numbers using the Quick sort

ALGORITHM:

Step 1: Start the process


Step 2: Initialize v:=a[m]; I:=m; and j:=p;
Step 3: Repeat the following steps until the step 7 is true.
Step 4: Repeat the process for i is incremented by one until the a[I] = v
Step 5: Repeat the process for I is decremented by one until the a[I]=v
Step 6: if(i<j) is true then interchange(a,i)
Step 7: until (i= =j)
Step 8: Assign a[m]:=a[j]; a[j]:=v;
Step 9: Exchange a[i] with a[j]
Step 10: Interchange p:= a[i]; a[i]:=a[j] and a[j] := p;
Step 11: Stop the process

Sample Input:

Enter the limit: 6

Enter the elements: 18 91 25 72 48 45

Sample Output:

The Sorted elements: 18 25 45 48 72 91

Result

Thus the above program is successfully executed and the results are verified.

A.V.C.C.E Course Material Department of Software Engineering & IT


[PG]
6

[Link] SHORTEST PATH (DIJIKSTRA’S)

AIM
To write a C program to find the shortest path using Dijikstra’s algorithm

ALGORITHM

Step 1: Start the process


Step 2: Assign S[0] = TRUE and d[0] = 0
Step 3: Initialize for ( V=1;V<n;V++)
Step 4: S[V] = False
Step 5: d[V] = adjmatrix[0][V];
Step 6: Initialize for (W=1;W<n;W++) choose a vertex W in V-S such that d[W]
Step 7: Add W to S
Step 8: for each vertex W in V-S do
Step 9: if(min+adjmatrix[V][W]<d[w])
Step 10: d[w] = min + adjmatrix[V][W]
Step 11: Stop the process

Sample Input:

Input the number of vertices in the graph: 4

Input adjacency matrix

0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0

Adjacency matrix

0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0

Input starting vertex: 1

Input destination: 3

A.V.C.C.E Course Material Department of Software Engineering & IT


[PG]
7

Sample Output:

Shortest Path: 1 -> 0 -> 2 -> 3


Minimum Distance = 2

Result

Thus the above program is successfully executed and the results are verified.

A.V.C.C.E Course Material Department of Software Engineering & IT


[PG]
8

[Link]: 7 SHORTEST PATH (WARSHALL’S)

AIM

To write a C program to find the shortest path using Warshall’s Algorithm

ALGORITHM

Step 1: Start the process


Step 2: [Initialization of path matrix]
Repeat through step 3 for j = 0,1,2,………….n-1
Repeat through step 3 for j = 0,1,2,………….n-1
Step 3: [Test the condition and assign accordingly value to path matrix]

If (a[i][j] = = 0 )

P[i][j] = 0

Else

P[i][j]=1
Step 4: [Evaluate path matrix]
Repeat through step 5 for k = 0,1,2,………n-1
Repeat through step 5 for I = 0,1,2,……….n-1
Repeat through step 5 for j = 0,1,2,……….n-1
Step 5: p[i][j]=p[i][j]|||(p[i][k]&p[k][j])
Step 6: Stop the process

Sample Input:

Input the number of vertices in the graph: 4

Input adjacency matrix

0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0

Adjacency matrix

0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0

A.V.C.C.E Course Material Department of Software Engineering & IT


[PG]
9

Sample Output:

Path matrix

1 1 0 1
1 1 0 1
1 1 0 1
1 1 0 1

Result

Thus the above program is successfully executed and the results are verified.

A.V.C.C.E Course Material Department of Software Engineering & IT


[PG]
10

[Link]: 8 GRAPH TRAVERSAL (BREADTH-FIRST SEARCH)

AIM:
To write a C program to demonstrate Breadth-First search technique.

ALGORITHM
Step 1: Start the process
Step 2: Consider any vertex in the graph. Process the vertex and mark it as visited.
Step 3: Using the adjacency matrix of the graph proceed to the next vertex which
has an edge connection wise the vertex considered in step 2.
Step 4: Backtract to the vertex considered in step 2 descend along an edge
towards an unvisited vertex and mark the new vertex as visited
Step 5: Repeat step 4 until all vertices adjacent to the node in step 1 have been
Marked as visited.
Step 6: Repeat step 2 and step 5 starting from vertex visited in step 3, then start
again from vertices visited in step 4 in the order visited.
Step 7: Stop the process

Sample Input

Enter the [Link] Nodes: 5

Enter the adjacency matrix…..

Enter the value of 1,2 element: 1

Enter the value of 1,3 element: 0

Enter the value of 1,4 element: 1

Enter the value of 1,5 element: 0

Enter the value of 2,1 element: 0

Enter the value of 2,3 element: 1

Enter the value of 2,4 element: 1

Enter the value of 2,5 element: 0

Enter the value of 3,1 element: 0

Enter the value of 3,2 element: 0

Enter the value of 3,4 element: 0

A.V.C.C.E Course Material Department of Software Engineering & IT


[PG]
11

Enter the value of 3,5 element: 1

Enter the value of 4,1 element: 0

Enter the value of 4,2 element: 0

Enter the value of 4,3 element: 1

Enter the value of 4,5 element: 1

Enter the value of 5,1 element: 1

Enter the value of 5,2 element: 0

Enter the value of 5,3 element: 0

Enter the value of 5,4 element: 0

Sample output

Nodes are visited in this order -> 1 -> 2 -> 3 -> 4 -> 5

Result
Thus the above program is successfully executed and the results are verified.

A.V.C.C.E Course Material Department of Software Engineering & IT


[PG]
12

[Link]: 9 GRAPH TRAVERSAL (DEPTH – FIRST SEARCH)

AIM

To write a C program to demonstrate Depth-first search techniques.

ALGORITHM

Step 1: Start the process


Step 2: Consider that the DFS is beginning from the starting vertex A.
Process the vertex A and mark it as visited.
Step 3: Using the adjacency matrix of the graph find the vertex along the path
Which begins vertex A, that has not been visited yet. Process the vertex
and consider this as new vertex and mark the vertex as visited.
Step 4: Repeat step 3 using the new search vertex. If no vertex(i.e., if a dead end
is reached) back track to the previous node and continue the search from
there.
Step 5: When backtracking to the previous search node in step 4 is impossible, the
search from the originally chosen search node is complete.
Step 6: If the graph still contains unvisited nodes, choose any vertex that has not
been visited and repeat step 2 to step 5.
Step 7: Stop the process

Sample Input

Enter the [Link] Nodes: 5

Enter the adjacency matrix…..

Enter the value of 1,2 element: 1

Enter the value of 1,3 element: 0

Enter the value of 1,4 element: 1

Enter the value of 1,5 element: 0

Enter the value of 2,1 element: 0

Enter the value of 2,3 element: 1

Enter the value of 2,4 element: 1

Enter the value of 2,5 element: 0

Enter the value of 3,1 element: 0

A.V.C.C.E Course Material Department of Software Engineering & IT


[PG]
13

Enter the value of 3,2 element: 0

Enter the value of 3,4 element: 0

Enter the value of 3,5 element: 1

Enter the value of 4,1 element: 0

Enter the value of 4,2 element: 0

Enter the value of 4,3 element: 1

Enter the value of 4,5 element: 1

Enter the value of 5,1 element: 1

Enter the value of 5,2 element: 0

Enter the value of 5,3 element: 0

Enter the value of 5,4 element: 0

Sample output

Nodes are visited in this order -> 1 -> 2 -> 3 ->5 -> 4

Result
Thus the above program is successfully executed and the results are verified.

A.V.C.C.E Course Material Department of Software Engineering & IT


[PG]
14

[Link] 10 MINIMUM SPANNING TREE

AIM
To write a C program to find the minimum cost for the spanning tree using the
Prims’ algorithm.

ALGORITHM

Step 1: Start the process


Step 2: Prim(int vertex[n][n])
Step 3: Set S[1]=True and path[1]=1
Step 4: while (V<=n) repeat step 5 to step 19
Step 5: while (W<=n) repeat step 6 to step 10
Step 6: if (S[W]= = TRUE) {W++; Continue};
Step 7: if (vertex[V][W]<min) step 8 to 9
Step 8: min=vertex[V][W]
Step 9: Store = W
Step 10: Increment W++;
Step 11: if(store) step 12 to 18
Step 12: path[k]=store
Step 13: S[store]=TRUE;
Step 14: increment K;
Step 15: Assign V=store
Step 16: min=INFINITY; and W=1;
Step 17: store = 0;
Step 18: Else step 19 to step 20
Step 19: assign V=1;
Step 20: Min=INFINITY and W=1
Step 21: stop process

Sample Input

Enter the [Link] Verices: 3

Input Weighted matrix

5
7
9
2
4
6
7
9
1

A.V.C.C.E Course Material Department of Software Engineering & IT


[PG]
15

Sample output

2 1
3 2

Result
Thus the above program is successfully executed and the results are verified.

A.V.C.C.E Course Material Department of Software Engineering & IT


[PG]
16

[Link]: 11 KNAPSACK PROBLEM

AIM
To write a C program to perform the Knapsack problem

ALGORITHM

Step 1: Start a process


Step 2: P[1:n] and W[1:n] contain the profits and weights respectively.
Step 3: for i:=1 to n do x[i]:=0.0
Step 4: Assign U:=m
Step 5: for i=1 to n do the following steps
Step 6: if (w[i]>u) then break
Step 7: Otherwise x[i]:=1.0; U := U –w[i]
Step 8: if (i=n) then x[i] := U/w[i]
Step 9: Stop the process

Sample Input

Enter the Maximum capacity of the Knapsack: 50

Enter the [Link] Objects: 2

Enter the profit and weight 10 20

Enter the profit and weight 30 20’

Sample Output

Maximum profit = 40.000

Knapsack problem

Weight Ratio

20 0.500000
-----------------------------------------
20 1.500000
Result

Thus the above program is successfully executed and the results are verified.

A.V.C.C.E Course Material Department of Software Engineering & IT


[PG]
17

[Link]: 12 TOWER OF HANOI

AIM
To write a C program to perform the Towers of Hanoi problem

ALGORITHM

Step 1: Start the process


Step 2: Declare the variable for Tower X,Y,Z and disc n
Step 3: if (n<0) then print wrong entry
Step 4: if (n = = 1) then move the disc from Tower X to Tower Z
Step 5: otherwise if (n=2) then n-1 disc move from Tower X to Tower Y
Using Tower Z is the intermediate Tower, then disc 1 is move from
Tower X to Tower Z.
Step 6: The remaining n-1 disc move from Tower Y to Tower Z using the
Tower X
Step 7: Stop the process

Sample Input

Enter the [Link] Disc: 3

Sample Output

Tower of Hanoi for 3 discs

Move disc from X to Z


Move disc from X to Y
Move disc from Z to Y
Move disc from X to Z
Move disc from Y to X
Move disc from Y to Z
Move disc from X to Z

Result
Thus the above program is successfully executed and the results are verified.

A.V.C.C.E Course Material Department of Software Engineering & IT


[PG]
18

[Link]: 13 BUBBLE SORT

AIM

To write a C program to sort the numbers using the Bubble sort.

ALGORITHM

Step 1: Start the process


Step 2: Where n represents the [Link] elements in the list
Step 3: Initialize I =0
Step 4: Repeat through step 7 while (i<n)
Step 5: Initialize j = 0
Step 6: Repeat through step 7 while (j<n-i)
Step 7: If (list[j]<list[j+1]
Temp = list[j]
list[j+1] = list[j]
list[j] = temp
Step 8: Stop the process

Sample Input

Enter the limit: 5

Enter the elements: 15 95 22 70 48

Sample Output

The sorted list is: 15 22 48 70 95

Result

Thus the above program is successfully executed and the results are verified.

A.V.C.C.E Course Material Department of Software Engineering & IT


[PG]

You might also like