You are on page 1of 84

Geethanjali College of Engineering and Technology

Cheeryal (V), Keesara (M), Medchal District – 501 301 (T.S)

ALGORITHMS
LABORATORY WORKBOOK

DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING

Lab In-charge HOD-CSE

Algorithms Lab Page 1


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Geethanjali College of Engineering and
Technology
Cheeryal (V), Keesara (M), Medchal District – 501 301 (T.S)

(AUTONOMOUS)

CERTIFICATE
This is to certify that Mr./ Miss _________________________________________

has satisfactorily completed________ number of experiments in the Algorithms


Laboratory

Roll No: ____________ Branch: ____________________

Year: _______________ Academic Year: ______________

Head Faculty

Dept. of CSE In charge

Algorithms Lab Page 2


Internal Examiner External Examiner

ALGORITHMS LAB

Algorithms Lab Page 3


LIST OF PROGRAMS

ALGORITHMS LAB

S.NO. Name of the Programs

1 Sort a given set of elements using the Quick sort method and determine the time
required to sort the elements. Repeat the experiment for different values of n, the
number of elements in the list to be sorted and plot a graph of the time taken versus n.
The elements can be read from a file or can be generated using the random number
generator.
2 Using Open, implement a parallelized Merge Sort algorithm to sort a given set of
elements and determine the time required to sort the elements. Repeat the experiment
for different values of n, the number of elements in the list to be sorted and plot a
graph of the time taken versus n. The elements can be read from a file or can be
generated using the random number generator.
3 Implement Binary tree traversal techniques using recursion and without recursion.
Identify the best method. Justify your answer.

4 a. Print all the nodes reachable from a given starting node in a digraph using BFS
method.
b. Check whether a given graph is connected or not using DFS method.
5 Write and implement an algorithm determining articulation points and the
biconnected components in the given graph
6 Implement an algorithm to find the minimum cost spanning tree using
a) Prims algorithm
b) Kruskals Algorithm
7 From a given vertex in a weighted connected graph, find shortest paths to other
vertices using Dijkstra’s algorithm.

8 Implement Job Sequencing with Deadlines algorithm and Fast Job Sequencing with
Deadlines
9 Implement Matrix Chain multiplication algorithm. Parallelize this algorithm,
implement it using Open and determine the speed-up achieved.

10 Implement 0/1 Knapsack problem using Dynamic Programming.

11 Implement an algorithm to find the optimal binary search tree for the given list of
identifiers.

12 Find a subset of a given set S = {sl,s2,.....,sn} of n positive integers whose


Sum is equal to a given positive integer d. For example, if S= {1, 2, 5, 6, 8} and d = 9
there are two solutions {1, 2, 6} and {1, 8}.A suitable message is to be displayed if

Algorithms Lab Page 4


the given problem instance doesn't have a solution.

13 Implement N Queen's problem using Back Tracking.

14 WAP for Hamiltonian Cycle Problem

15 Implement the solution for TSP problem using Branch & Bound technique

Additional Programs

ALGORITHMS LAB CO’S,PO’S,PSO’S


Mapping

S.No. Name of the programs


1 Implement Matrix multiplication using OpenMP CO-2,PO’S:1,3,4,12

PSO’S:1

2 Implement an algorithm to find chromatic number of a given CO-2,PO’S:1,3,4,12


graph PSO’S:1

Algorithms Lab Page 5


Vision of the Institute

Geethanjali visualizes dissemination of knowledge and skills to students, who eventually


contribute to well being of the people of the nation and global community.

Mission of the Institute

 To impact adequate fundamental knowledge in all basic science and engineering technical
and Inter-Personals skills so students.

 To bring out creativity in students that would promote innovation, research and
entrepreneurship

 To Preserve and promote cultural heritage, humanistic and spiritual values promoting
peace and harmony in society

Vision of the Department

To produce globally competent and socially responsible computer science engineers


contributing to the advancement of engineering and technology which involves creativity and
innovation by providing excellent learning environment with world class facilities.

Mission of the Department

1. To be a center of excellence in instruction, innovation in research and scholarship, and


service to the stake holders, the profession, and the public.

2. To prepare graduates to enter a rapidly changing field as a competent computer science


engineer.

3. To prepare graduate capable in all phases of software development, possess a firm


understanding of hardware technologies, have the strong mathematical background necessary
for scientific computing, and be sufficiently well versed in general theory to allow growth
within the discipline as it advances.

4. To prepare graduates to assume leadership roles by possessing good communication skills,


the ability to work effectively as team members, and an appreciation for their social and
ethical responsibility in a global setting.

Algorithms Lab Page 6


PEO’s and PO’s

PROGRAM EDUCATIONAL OBJECTIVES

1. To provide graduates with a good foundation in mathematics, sciences and engineering


fundamentals required to solve engineering problems that will facilitate them to find
employment in industry and / or to pursue postgraduate studies with an appreciation for
lifelong learning.
2. To provide graduates with analytical and problem solving skills to design algorithms,
other hardware / software systems, and inculcate professional ethics, inter-personal skills
to work in a multi-cultural team.
3. To facilitate graduates to get familiarized with the art software / hardware tools, imbibing
creativity and innovation that would enable them to develop cutting-edge technologies of
multi-disciplinary nature for societal development.
PROGRAM OUTCOMES

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.
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.

Algorithms Lab Page 7


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.
PSO (Program Specific Outcome):

PSO 1: To identify and define the computing requirements for its solution under given
constraints.

PSO 2: To follow the best practices namely SEI-CMM levels and six sigma which varies from
time to time for software development project using open ended programming environment to
produce software deliverables as per customer needs.

Algorithms Lab Page 8


6. Course Objectives & Outcomes

Course Objectives:

Develop ability to
1. Realize the asymptotic performance of algorithms.
2. Understand the behavior of Greedy strategy, Divide and Conquer approach, Dynamic
Programming and branch and bound theory for several problem solving techniques.
3. Understand how the choice of data structures and algorithm design methods impact the
performance of programs.
4. Distinguish deterministic and non-deterministic algorithms and their computational complexities.

Course Outcomes:

After completion of the course, student can

16CS22L3.1 Analyze algorithms and estimate their best-case, worst-case and average-case behavior in
terms of time and space and execute the same through programming.

16CS22L3.2 Identify suitable problem solving technique for a given problem and design algorithms using
greedy strategy, divide and conquer approach, dynamic programming, and branch and bound theory
accordingly and execute the same through programming.

16CS22L3.3 Implement algorithm design method into appropriate data structures using programming.

16CS22L3.4 Design deterministic and non-deterministic algorithms for tractable and intractable
problems and categorize them as P Class/ NP Class/ NP-Hard/ NP-complete problems accordingly.

Mapping of Lab Course with Programme Educational Objectives

Course PEO’S PO’S & PSO’S


PEO1, PO1,
Algorithms
PEO2, PO2,PO3,PO4,PO6,PO7,PO9,PO10,PO1
Laboratory
PEO3 1,PO12,PSO1,PSO2

Algorithms Lab Page 9


Mapping of Lab Course outcomes with Programme outcomes:

Course Outcomes PO’s & PSO’s

Experiment 1 2 3 4 5 6 7 8 9 10 11 12 PSO1 PSO2


No.
16CS22L3.1 Analyze algorithms and Additional 3 3 3 2 - 1 1 - 2 2 1 3 3 1
estimate their best-case, worst-case Programs:
and average-case behavior in terms 1, 2
of time and space and execute the
same through programming.
16CS22L3.2 Identify suitable 1, 2, 3 3 1 3 - 1 1 - 2 2 1 2 3 1
problem solving technique for a 3,4,5,6, 7 ,
given problem and design algorithms 8, 9, 10,
using greedy strategy, divide and 11,12,13,14
conquer approach, dynamic ,15
programming, and branch and bound
theory accordingly and execute the
same through programming.
16CS22L3.3 Implement algorithm 1,2,3,4 3 3 1 3 - 1 1 - 2 2 1 3 3 1
design method into appropriate data
structures using programming.
16CS22L3.4 Design deterministic - 3 3 1 3 - 1 1 - 2 2 1 3 3 1
and non-deterministic algorithms for
tractable and intractable problems
and categorize them as P Class/ NP
Class/ NP-Hard/ NP-complete
problems accordingly.

Algorithms Lab Page 10


INDEX
S.No Name of Experiment Date of Date of Marks Signature Remarks
Exp. Submission

Algorithms Lab Page 11


INDEX

S.No Name of Experiment Date of Date of Marks Signature Remarks


Exp. Submission

Algorithms Lab Page 12


WEEK-1

Sort a given set of elements using the Quick sort method and determine the time required to
sort the elements. Repeat the experiment for different values of n, the number of elements in
the list to be sorted and plot a graph of the time taken versus n. The elements can be read
from a file or can be generated using the random number generator.
Quick Sort Method:
Quick Sort divides the array according to the value of elements. It rearranges elements of a given
array A[0..n-1] to achieve its partition, where the elements before position s are smaller than or
equal to A[s] and all the elements after position s are greater than or equal to A[s].
A[0]…A[s-1] A[s] A[s+1]…A[n-1]
All are <=A[s] all are >=A[s]

Algorithm: QUICKSORT (a [l...r])


//Sorts a sub array by quick sort
//Input: A sub array A [l...r] of A [0..n-1],defined by its left and right indices l and r
//Output: Sub array A [l...r] Sorted in non decreasing order
{ if l<r
{
s← Partition(A[l..r]); //s is a split position QUICKSORT(A[l..s-1]);
QUICKSORT (A[s+1..r]);
}
}
Algorithm: Partition (A [l..r])
//Partition a sub array by using its first element as its pivot
//Input: A sub array A [l...r] of A [0...n-1],defined by its left and right indices l and r (l<r)
//Output: A partition of A [l...r], with the split position returned as this function’s value
{
p ← A[l];
i ← l; j ← r+1; repeat
{
repeat
i ← i+1;
until A[i] >=p;
repeat
j ← j-1;
until A[j] <=p;
if (i<j) swap(A[i],A[j]);

} until i>=j
swap(A[l],A[j])
return j;
}

Algorithms Lab Page 13


Program

Algorithms Lab Page 14


Algorithms Lab Page 15
INPUT: OUTPUT:

Viva Questions
1. Explain partitioning in Quick sort.

2. What is stable sorting? Is Quick sort stable?

3. Explain the best case, worst case and average case time complexity of Quick sort.

4. Give the position of pivot 65 after partitioning in the set: 65 70 75 80 85 60 55 50 45

5. What is the output of quick sort after the 3rd iteration given the following sequence of
numbers: 65 70 75 80 85 60 55 50 45

Algorithms Lab Page 16


WEEK-2

Using OpenMP, implement a parallelized Merge Sort algorithm to sort a given set of
elements and determine the time required to sort the elements. Repeat the experiment for
different values of n, the number of elements in the list to be sorted and plot a graph of the
time taken versus n. The elements can be read from a file or can be generated using the
random number generator.
Merge Sort Algorithm:
Merge sort is a perfect example of a successful application of the divide-and-conquer technique.
1. Split array A[1..n] in two and make copies of each half in arrays B[1.. n/2 ] and C[1.. n/2 ]
2. Sort arrays B and C
3. Merge sorted arrays B and C into array A as follows:
a) Repeat the following until no elements remain in one of the arrays:
i. compare the first elements in the remaining unprocessed portions of the arrays
ii. copy the smaller of the two into A, while incrementing the index indicating the unprocessed
portion of that array
b) Once all elements in one of the arrays are processed, copy the remaining unprocessed elements
from the other array into A.
Algorithm: MergeSort (A [0...n-1])
//This algorithm sorts array A [0...n-1] by recursive mergesort. //Input: An array A [0...n-1] of
orderable elements.
//Output: Array A [0...n-1] sorted in non-decreasing order
{ if n>1
{ Copy A [0…└n/2┘-1] to B [0…└n/2┘-1]
Copy A [└n/2┘… n-1] to C [0…┌n/2┐-1] MergeSort (B [0…└n/2┘-1])
MergeSort (C [0…┌n/2┐-1])
Merge (B, C, A)
}
}

Algorithm: Merge (B [0…p-1], C [0...q-1], A [0...p+q-1]) //Merges two sorted arrays into one
sorted array.
//Input: Arrays B [0…p-1] and C [0...q-1] both sorted. //Output: Sorted array A [0...p+q-1] of the
elements of B and C.
{
i ← 0; j←0; k←0 while i<p and j<q do
{
if B[i] <= C[j]

A[k] ← B[i]; i← i+1


else
A[k] ← C[j]; j← j+1 k ← k+1
}
if i=p
copy C [j…q-1] to A[k…p+q-1]

Algorithms Lab Page 17


else
copy B [i…p-1] to A[k…p+q-1]
}

Complexity:
All cases have same efficiency: Θ (n log n)
Number of comparisons is close to theoretical minimum for comparison-based sorting: log n ! ≈ n
lg n - 1.44 n
Space requirement: Θ (n) (NOT in-place)

Program

Algorithms Lab Page 18


Algorithms Lab Page 19
INPUT: OUTPUT:

Viva Questions
1. Write and solve the recurrence relation giving the time complexity of merge sort

2. What is in-place sort? Is merge sort an in-place sort?

3. Suggest a way to make Merge sort an in-place sort.

4. Given two sorted lists of size m, n. What is the number of comparisons needed in the worst
case by the merge sort algorithm?

5. Given the initial sequence: 3, 41, 52, 26, 38, 57, 9, and 49 .Write the sequences to be merged at
the last step in the merge sort.

Algorithms Lab Page 20


WEEK-3

Implement Recursive and Non-recursive Binary tree traversal techniques. Identify the best
method. Justify your answer.
Algorithms

Recursive Algorithms for In Order, Pre Order, and Post Order Binary tree traversals

Tree node = record


{
Type date; // Type is the data type of data.
Tree node *lchild, *rchild;
}

Algorithm InOrder(t)
// t is a binary tree. Each node of t has three fields: lchild, data, and rchild.
{
if t≠ 0 then
{
InOrder(tlchild);
Visit(t);
InOrder(trchild);
}
}

Algorithm PreOrder(t)
// t is a binary tree. Each node of t has three fields: lchild, data, and rchild.
{
if t≠ 0 then
{
Visit(t);
PreOrder(tlchild);
PreOrder(trchild);
}
}

Algorithm PostOrder(t)
// t is a binary tree. Each node of t has three fields: lchild, data, and rchild.
{
if t≠ 0 then
{
PostOrder(tlchild);
PostOrder(trchild);
Visit(t);

Algorithms Lab Page 21


}

Algorithms Lab Page 22


Non-Recursive Algorithms for In Order, Pre Order, and Post Order Binary tree traversals

In Order:
1) Create an empty stack S.
2) Initialize current node as root
3) Push the current node to S and set current = current->left until current is NULL
4) If current is NULL and stack is not empty then
a) Pop the top item from stack.
b) Print the popped item, set current = popped_item->right
c) Go to step 3.
5) If current is NULL and stack is empty then we are done.

Pre Order:
1) Create an empty stack S and push root node to stack.
2) Do following while S is not empty.
a) Pop an item from stack and print it.
b) Push right child of popped item to stack
c) Push left child of popped item to stack

Post Order:

Using Two Stacks:


1. Push root to first stack.
2. Loop while first stack is not empty
2.1 Pop a node from first stack and push it to second stack
2.2 Push left and right children of the popped node to first stack
3. Print contents of second stack

Using One Stack:

1.1 Create an empty stack


2.1 Do following while root is not NULL
a) Push root's right child and then root to stack.
b) Set root as root's left child.
2.2 Pop an item from stack and set it as root.
a) If the popped item has a right child and the right child
is at top of stack, then remove the right child from stack,
push the root back and set root as root's right child.
b) Else print root's data and set root as NULL.
2.3 Repeat steps 2.1 and 2.2 while stack is not empty.

Algorithms Lab Page 23


Program:

Algorithms Lab Page 24


Algorithms Lab Page 25
Algorithms Lab Page 26
Algorithms Lab Page 27
INPUT: OUTPUT:

Algorithms Lab Page 28


Viva Questions
1. The inorder and preorder traversal of a binary tree are d b e a f c g and a b d e c f g, respectively.
What is the postorder traversal of this binary tree?

2. What does the following function do for a given binary tree?


int fun(struct node *root)
{
    if (root == NULL)
       return 0;
    if (root->left == NULL && root->right == NULL)
       return 0;
    return 1 + fun(root->left) + fun(root->right);
}

3. Give a complete binary tree with 12 elements and write its inorder, preorder, and postorder
traversals.

4. A binary search tree contains the numbers 1, 2, 3, 4, 5, 6, 7, 8. When the tree is traversed in
pre-order and the values in each node printed out, the sequence of values obtained is 5, 3, 1, 2, 4,
6, 8, and 7. Write its post order traversal.  

5. The array representation of a complete binary tree contains the data in sorted order. Which
traversal of the tree will produce the data in sorted form?

Algorithms Lab Page 29


Week-4
a. Print all the nodes reachable from a given starting node in a digraph using BFS method.
b. Check whether a given graph is connected or not using DFS method.
Breadth First Search:
BFS explores graph moving across to all the neighbors of last visited vertex traversals i.e., it
proceeds in a concentric manner by visiting all the vertices that are adjacent to a starting vertex,
then all unvisited vertices two edges apart from it and so on, until all the vertices in the same
connected component as the starting vertex are visited. Instead of a stack, BFS uses queue.

Algorithm: BFS(v)
// A breadth-first search of G is carried out beginning at vertex v. For any node i, visited[i] =1 if i has
// already been visited. The graph G and array visited [] are global; visited [] is initialized to zero

{
u := v; // q is a queue of unexplored vertices.
visited[v] := 1;
repeat
{
for all vertices w adjacent from u do
{
if(visited[w] = 0) then
{
Add w to q; //w is unexplored.
Visited[w] := 1;
}
}
if q is empty then return; // No unexplored vertex.
Delete the next element , u from q; //Get first unexplored vertex.
} until (false);
}

Complexity:
BFS has the same efficiency as DFS:
it is Θ (n2) for Adjacency matrix representation and Θ(n+e) for Adjacency linked list representation.

Program:

Algorithms Lab Page 30


INPUT: OUTPUT:

Algorithms Lab Page 31


b. Depth First Search:
Depth-first search starts visiting vertices of a graph at an arbitrary vertex by marking it as having
been visited. On each iteration, the algorithm proceeds to an unvisited vertex that is adjacent to
the one it is currently in. This process continues until a vertex with no adjacent unvisited vertices
is encountered. At a dead end, the algorithm backs up one edge to the vertex it came from and
tries to continue visiting unvisited vertices from there. The algorithm eventually halts after
backing up to the starting vertex, with the latter being a dead end.

Algorithm: DFS (v)


// given an undirected (directed) graph G= (V, E) with n vertices and an array visited [] initially
// set to zero, this algorithm visits all vertices reachable from v. G and visited [] are global.

{
visited[v] := 1;
for each vertex w adjacent from v do
{
if (visited[w] = 0) then DFS(w);
}
}
Program:

Algorithms Lab Page 32


INPUT: OUTPUT:

Algorithms Lab Page 33


Viva Questions:

1. Differentiate graph and tree.

2. Explain the various ways of representing a graph and give representations of the following
graph.

3. Write BFS and DFS sequence for the above graph.

4. Define and explain stack and queue.

5. Write the applications of BFS.

6. Write the applications of DFS.

7. What is a spanning tree?

8. Explain BFS and DFS spanning tree.

Algorithms Lab Page 34


Week-5

Write and implement an algorithm determining articulation points and the biconnected
components in the given graph
Algorithm

Algorithm BiComp (u, v)


// u is a start vertex for depth first search. V is its parent if any in the depth first spanning tree. It is
// assumed that the global array dfn is initially zero and that the global variable num is initialized to
1. // n is the number of vertices in G.

{
dfn[u] := num; L[u] := num; num := num+1;
for each vertex w adjacent from u do
{
if (( v≠ w) and ( dfn[w] < dfn[u])) then
add (u, w) to the top of a stack s;

if (dfn[w] = 0) then
{
BiComp(w, u);
L[u] := min(L[u], L[w]);
if ( L[w] ≥ dfn [u] ) then
{
write (“New Bicomponent”);
repeat
{
Delete an edge from the top of stack s;
Let this edge be (x,y);
write (x,y);
} until (((x, y) = (u, w)) or (x, y) = (w, u)));
}
else if ( w ≠ v) then L[u] := min(L[u], dfn[w]);

Program:

Algorithms Lab Page 35


Algorithms Lab Page 36
Algorithms Lab Page 37
Input: Output:

Viva Questions:

1. What is an articulation point?

2. Compare tree edge, back edge, and cross edge.

3. Identify the articulation points and associated biconnected components in the following graph.

4. Give dfn value for each node in the above graph.

5. Compute L[] for each node in the above graph.

Algorithms Lab Page 38


Week-6
Implement an algorithm to find the minimum cost spanning tree using
a) Prims algorithm
b) Kruskals Algorithm
Prim’s Algorithm:

Prim’s algorithm finds the minimum spanning tree for a weighted connected graph G= (V, E) to
get an acyclic subgraph with |V|-1 edges for which the sum of edge weights is the smallest.
Consequently the algorithm constructs the minimum spanning tree as expanding sub-trees. The
initial subtree in such a sequence consists of a single vertex selected arbitrarily from the set V of
the graph’s vertices. On each iteration, we expand the current tree in the greedy manner by simply
attaching to it the nearest vertex not in that tree. The algorithm stops after all the graph’s vertices
have been included in the tree being constructed.
Algorithm prims (e, cost, n, t)
{
Let (k,l) be an edge of minimum cost in E;
Mincost :=cost[k,l];
T[1,1]:=k; t[1,2]:=l;
For I:=1 to n do
If (cost[i,l]<cost[i,k]) then near[i]:=l;
else near[i]:=k;
Near[k]:=near[l]:=0;
For i:=2 to n-1 do
{
Let j be an index such that near[j]≠0 and
Cost[j,near[j]] is minimum;
T[i,1]:=j;
t[i,2]:=near[j];
Mincost:=mincost+ Cost[j,near[j]]; Near[j]:=0;
For k:=0 to n do
If near((near[k]≠0) and (Cost[k,near[k]]>cost[k,j])) then
Near[k]:=j;
}
Return mincost;
}

Complexity: The time efficiency of prim’s algorithm will be in O (|E| log |V|).

Program:

Algorithms Lab Page 39


Algorithms Lab Page 40
INPUT: OUTPUT:

b. Kruskal's algorithm:

Kruskal’s algorithm finds the minimum spanning tree for a weighted connected graph G= (V, E)
to get an acyclic subgraph with |V|-1 edges for which the sum of edge weights is the smallest.
Consequently the algorithm constructs the minimum spanning tree as an expanding sequence of
sub graphs, which are always acyclic but are not necessarily connected on the intermediate stages
of algorithm. The algorithm begins by sorting the graph’s edges in non decreasing order of their
weights. Then starting with the empty subgraph, it scans the sorted list adding the next edge on
the list to the current subgraph if such an inclusion does not create a cycle and simply skipping
the edge otherwise.

Algorithm kruskal(E,cost,n,t)
//E is set of edges in G has ‘n’ vertices.
//cost[u,v] is cost of edge (u,v). t is set of edge in minimum cost spanning tree
// the final cost is returned.
{
for i=1 to n do
parent[i]=-1;
i=0; mincost=0.0;
while((i<n-1)and (heap not empty)) do
{
j=find(n); k=find(v);
if(j not equal k) than
{
i=i+1

Algorithms Lab Page 41


t[i,1]=u;
t[i,2]=v;
mincost=mincost+cost[u,v];
union(j,k);
}
}
if(i notequal n-1) then write(“No spanning tree”) else return
minimum cost;
}

Complexity: With an efficient sorting algorithm, the time efficiency of kruskal’s algorithm will
be in O(|E| log |E|).

Program:

Algorithms Lab Page 42


Algorithms Lab Page 43
INPUT: OUTPUT:

Viva Questions:
1. What are the applications of spanning tree?

2. Explain Prim’s algorithm to obtain a Minimum Spanning Tree (MST).

3. Explain Kruskal’s algorithm to obtain a MST.

4. Give MSP for the following graph using Prim’s and Kruskal’s algorithm.

5. Compare the performance of Prim’s and Kruskal’s algorithm in obtaining MST.

Algorithms Lab Page 44


WEEK-7

a. From a given vertex in a weighted connected graph, find shortest paths to other vertices
using Dijkstra’s algorithm.
Single Source Shortest Paths Problem:
For a given vertex called the source in a weighted connected graph, find the shortest paths to all
its other vertices. Dijkstra’s algorithm is the best known algorithm for the single source shortest
paths problem. This algorithm is applicable to graphs with nonnegative weights only and finds the
shortest paths to a graph’s vertices in order of their distance from a given source. It finds the
shortest path from the source to a vertex nearest to it, then to a second nearest, and so on. It is
applicable to both undirected and directed graphs

Algorithm ShortestPaths(v, cost, dist,n)


//dist[j], 1 ≤ j ≤ n, is set to the length of the shortest path from vertex v to vertex j in a digraph G
// with n vertices. Dist[v] is set to zero. G is represented by its cost adjacency matrix cost [1: n, 1:
n]
{
for i := 1 to n do
{
// Initialize S.
S[i] := false; dist[v] := cost[v, i];
}
S[v] :=true; dist[v] :=0.0; // Put v in S.
for num :=2 to n do
{
//Determine n-1 paths from v.
Choose u from among those vertices no in S such that dist[u] is minimum;
S[u] :=true; // Put u in S.
for (each w adjacent to u with S[w] = false) do
// Update distances.
if (dist[w] > dist[u] + cost[u, w])) then

dist[w] = dist[u] + cost[u, w];


}
}

Program:

Algorithms Lab Page 45


INPUT: OUTPUT:

Algorithms Lab Page 46


b. Implement Job Sequencing with Deadlines algorithm and Fast Job Sequencing with
Deadlines

Algorithm:
Algorithm JS (d, j, n)

//The job are ordered such that p[1]>p[2]…>p[n]

//j[i] is the ith job in the optimal solution

// Also at terminal d [ J[ i]<=d[ J {i+1],1<i<k

{
d[0]=J[0]=0;

J[1]=1; k=1; for i =1 to n do


{
// consider jobs in non increasing order of P[i];find the position for i and check feasibility insertion r=k;

while((d[J[r]]>d[i] )and (d[J[r]]! = r) do r =r-1;

if (d[J[r]]<d[I])and (d[I]>r))then
{
for q=k to (r+1) step –1 do J [q+1]=j[q] J[r+1]=i;
K=k+1;
}
}
return k;
}
Program:

Algorithms Lab Page 47


Algorithms Lab Page 48
INPUT: OUTPUT:

Viva Questions:

1. Explain Job Sequencing with Deadlines.

2. Define feasible solution and optimal solution.

3. Write all feasible solutions for following instance of JSD: n=4 p[1:4]=(100,10,15,27) and
d[1:4]=(2,1,2,1). Select the optimal solution.

4. Using greedy method write the Optimal Solution for following instance of JSD: n=5,
p[1:5]=(20, 15,10,5,11) and d[1:5]=(2,2,1,3,3).

5. Differentiate JS and FJS algorithms.

Algorithms Lab Page 49


WEEK-8

Implement Matrix Chain multiplication algorithm. Parallelize this algorithm, implement it


using Open and determine the speed-up achieved.
Algorithm:

Algorithm Matrix_chain(p,n)
{
for i := 1 to n do
m[i, i] :=0;
for l := 2 to n do
for i := 1 to n-l+1 do
{
j := i + l -1;
m[i, j] := 0;
for k := i to j-1 do
{
q := m[i, k] + m[k + 1, j] + p[i -1] * p[k] *p[j];
if q < m[i, j] then
{
m[i, j] := q;
S[i, j] := k;
}
}
}
return m and S;
}

Program:

Algorithms Lab Page 50


Algorithms Lab Page 51
INPUT: OUTPUT:

Viva Questions:

1. State the various properties of operations. Is matrix multiplication associative?

2. Write the various ways of performing the following chain of matrix multiplication: ABCD.

3. Compute the minimum number of multiplications required to multiply ABC, where A is a


10 X 30 matrix, B is a 30 X 5 matrix, and C is a 5 X 60 matrix.

4. State the equation holding the principle of optimality for a chain of matrix multiplication.

5. Given a chain of four matrices A1, A2, A3, and A4, with p0 = 5, p1 = 4, p2 = 6, p3 = 2, p4 =7. Find
m[1, 4].

Algorithms Lab Page 52


WEEK-9
Implement 0/1 Knapsack problem using Dynamic Programming.

Algorithm:
PW= record {float p; float w }

Algorithm DKnap(p, w, x, n, m)
{
// pair [] is an array of PW’s
b[0] :=1; pair[1].p :=pair[1].w := 0.0; //S0
t :=1; h :=1; //start and end of S0
b[1] :=next :=2; //Next free spot in pair[]
for i := 1 to n-1 do
{ // Generate Si
k :=t;
u := Largest(pair, w, t, h, i, m);
for j :=t to u do
{ // Generate Si-11 and merge.
pp := pair[j].p + p[i];
i-1
ww := pair[j].w + w[i]; // (pp, ww) is the next element in S 1
while ((k ≤ h) and (pair[k].w ≤ ww )) do
{
pair[next].p :=pair[k].p;
pair[next].w :=pair[k].w;
next :=next + 1; k :=k+1;
}
if ((k ≤ h) and (pair[k].w = ww )) then
{
if pp < pair[k].p then pp :=pair[k].p;
k := k +1;
}
if pp > pair[next – 1].p then
{
pair[next].p := pp; pair[next].w := ww;
next :=next + 1;
}
while ((k ≤ h) and (pair[k].p ≤ pair[next – 1].p)) do
k :=k + 1;
}
// Merge in remaining terms from Si-1.
while ( k ≤ h) do
{
pair[next].p :=pair[k].p;
pair[next].w :=pair[k].w;
next :=next + 1; k :=k+1;
}

Algorithms Lab Page 53


// Initialize for Si + 1
t := h +1; h := next -1; b[i +1] :=next;
}
TraceBack(p, w, pair, x, m, n);
}
Program:

Algorithms Lab Page 54


Algorithms Lab Page 55
INPUT: OUTPUT:

Viva Questions:
1. Differentiate fractional Knapsack and 0/1 Knapsack.

2. Define principle of optimality.

3. Write and explain the equation for 0/1 Knapsack holding principle of optimality.

4. Define merge and purge rules.

5. What is the time complexity of 0/1 Knapsack using dynamic programming? Is any
improvement possible? If so, explain.

Algorithms Lab Page 56


WEEK-10
Write a program to implement Optimal Binary Search Tree
Algorithm:
Algorithm OBST (p, q, n)
// given n distinct identifiers a1 < a2 < … <an and probabilities p[i], 1≤ i ≤ n, and q[i], 0≤ i ≤ n,
// this algorithm computes the cost c[I, j] of optimal binary search trees tij for identifiers ai+1, …, aj.
// It also computes r[i, j], the root of tij. w[i, j] is the weight of tij.
{
for i := 0 to n-1 do
{
// Initialize.
w[i, i] := q[i]; r[i, i] := 0; c[i, i] := 0;
// Optimal trees with one node.
w[i, i+1] := q[i] + q[i+1] + p[i+1];
r[i, i+1] := i + 1;
c[i, i+1] := q[i] + q[i+1] + p[i+1];
}
w[n, n] := q[n]; r[n, n] := 0; c[n, n] := 0;
for m := 2 to n do
for i :=0 to n-m do
{
j := i+m;
w[i, j] := w[i, j-1] + p[j] +q[j];
k := Find(c, r, i, j);
// A value of l in the range r[i, j-1] ≤ l ≤ r[i+1, j] that minimizes c[i, l-1] + c[l, j]
c[i, j] := w[i, j] + c[i, k-1] + c[k, j]
r[i, j] :=k;
}
write (c[0,n], w[0,n], r[0,n]);

Algorithm Find(c, r, i, j)
{
min=∞;
for m := r[i, j-1] to r[i+1, j] do
if ( c[i, m-1] + c[m, j] < min) then
{
min := c[i, m-1] + c[m, j]; l :=m;
}

return l;
}

Algorithms Lab Page 57


Program:

Algorithms Lab Page 58


Algorithms Lab Page 59
INPUT: OUTPUT:

Viva Questions:
1. Define binary tree and binary search tree.

2. Define Optimal Binary Search Tree (OBST).

3. What is the number of binary trees with n nodes?

4. What is the significance of internal nodes and external nodes in a binary tree?

5. What is the time complexity of OBST algorithm? Is any improvement possible? If so, explain.

Algorithms Lab Page 60


WEEK-11
a. Find a subset of a given set S = {sl,s2...sn} of n positive integers whose sum is equal to a given
Positive integer d. For example, if S= {1, 2, 5, 6, 8} and d = 9 there are two solutions {1, 2, 6} and
{1, 8}.A suitable message is to be displayed if the given problem instance doesn't have a solution.

Sum of Subsets
Subset-Sum Problem is to find a subset of a given set S= {s1, s2… s n} of n positive integers
whose sum is equal to a given positive integer d. It is assumed that the set’s elements are sorted in
increasing order. The state-space tree can then be constructed as a binary tree and applying
backtracking algorithm, the solutions could be obtained. Some instances of the problem may have
no solutions

Algorithm SumOfSub(s, k, r)
//Find all subsets of w[1…n] that sum to m. The values of x[j], 1≤ j < k, have already been
//determined. s=∑k-1 w[j]*x[j] and r =∑n w[j]. The w[j]’s in ascending order. It is assumed that
// w[1] ≤ m and ∑ w[i] ≥ m
{
// Generate left child. Note: s+w[k] ≤ m since Bk-1 is true.
x[k] := 1
if (s+w[k] = m) then write (x[1 : k]) //subset found
// There is no recursive call here as w[j]>0, 1 ≤ j ≤ n.
else if ( s + w[k]+w[k+1] ≤ m) then
SumOfSub( s + w[k], k+1, r-w[k])
//Generate right child and evaluate Bk.
if( (s + r - w[k] >= m) and (s + w[k+1] <= m) )
{
x[k] := 0
SumOfSub( s, k+1, r-w[k] )
}
}

Complexity:

Subset sum problem solved using backtracking generates at each step maximal two new subtrees, and the runnin
time of the bounding functions is linear, so the running time is O (2n).

Program:

Algorithms Lab Page 61


Algorithms Lab Page 62
INPUT: OUTPUT:

Viva:
1. Compare brute force approach and backtracking.

2. Differentiate explicit and implicit constraints.

3. Explain sum of subsets problem.

4. Draw fixed and variable tuple solution space tree for following instance of Sum-of-Subsets
problem: n=4, w[1:4]=(11,13,24,7) and m=31.

5. Explain the bounding functions for Sum-of-Subsets problem.

Algorithms Lab Page 63


b. Implement N Queen's problem using Backtracking.
The n-queens problem consists of placing n queens on an n x n checker board in such a way that they do not
threaten each other, according to the rules of the game of chess. Every queen on a checker square can reach
the other squares that are located on the same horizontal, vertical, and diagonal line. So there can be at most
one queen at each horizontal line, at most one queen at each vertical line, and at most one queen at each of
the 4n-2 diagonal lines. Furthermore, since we want to place as many queens as possible, namely exactly n
queens, there must be exactly one queen at each horizontal line and at each vertical line. The concept behind
backtracking algorithm which is used to solve this problem is to successively place the queens in columns.
When it is impossible to place a queen in a column (it is on the same diagonal, row, or column as another
token), the algorithm backtracks and adjusts a preceding queen

Algorithm NQueens (k, n)


// using backtracking, this procedure prints all possible placements of n queens on an n x n
// chessboard so that they are non-attacking
{
for i := 1 to n do
{
if (Place(k,i) )
{
x[k] := i
if (k = n) then write ( x[1...n])
else
Nqueens (k+1, n)
}
}
}

Algorithm Place (k, i)


// Returns true if a queen can be placed in kth row and ith column. Otherwise it returns false. x[]
is // a global array whose first (k-1) values have been set. Abs(r) returns the absolute value of r.
{
for j := 1 to k-1 do
{
if ( (x[j] = i // Two in the same column
or (Abs(x[j] - i) = Abs(j - k) )
// or in the same diagonal
then return false
return true;
}

Complexity:

The power of the set of all possible solutions of the n queen’s problem is n! And the bounding function takes
a linear amount of time to calculate, therefore the running time of the n queens problem is O (n!).

Algorithms Lab Page 64


Program:

Algorithms Lab Page 65


INPUT: OUTPUT:

Viva:
1. Explain N-Queens problem.

2. Draw the tree organization of the 4-Queesn solution space.

3. Number the nodes in above tree as in depth first search, breadth first search and D-Search.

4. Write and explain the condition to determine if two queens are on the same diagonal.

5. Give one solution for 8-queens problem.

Algorithms Lab Page 66


WEEK-12
WAP for Hamiltonian Cycle Problem
Algorithm:

Algorithm Hamiltonian (k)


// This algorithm uses the recursive formulation of backtracking to find all the Hamiltonian cycles
// of graph, The graph is stored as an adjacency matrix G[1:n, 1:n]. All cycles begin at node 1.
{
repeat
{ // Generate values for x[k].
NextValue(k); // Assign a legal next value to x[k].
if (x[k] = 0) then return;
if (k = n) then write (x[1:n]);
else Hamiltonian (k+1);
} until (false);
}

Algorithm NextValue(k)
// x[1: k-1] is a path of k-1 distinct vertices. If x[k] = 0, then no vertex has as yet been assigned to
//x[k]. After execution, x[k] is assigned to the next highest numbered vertex which does not already
//appear in x[1 : k-1] and is connected by an edge to x[k-1]. Otherwise x[k] = 0. If k = n, then in
//addition x[k] is connected to x[1].

{
repeat
{
x[k] := (x[k] +1) mod (n+1);
if (x[k] = 0) then return;
if (G[x[k-1], x[k]] ≠ 0) then
{ // Is there an edge?
for j := 1 to k-1 do if (x[j] = x[k]) then break; // Check for distinctness.
if (j = k) then // If true, then the vertex is distinct.
if ((k<n) or ((k = n) and G[x[n], x[1]] ≠ 0))
then return;
}
} until (false);
}

Program:

Algorithms Lab Page 67


Algorithms Lab Page 68
INPUT: OUTPUT:

Viva Questions:
1. What is Hamiltonian cycle?

2. Write all Hamiltonian cycles in the following graph.

3. Draw the portion of state space tree for Hamiltonian cycle for the above graph.

4. Is Hamiltonian cycle related to Traveling Sales person problem? If so, explain.

Algorithms Lab Page 69


WEEK-13
Implement the solution for TSP problem using Branch & Bound technique
Traveling Sales person problem:
Given n cities, a salesperson starts at a specified city (source), visit all n-1 cities only once and return tio the th
city from where he has started. The objective of this problem is to find a route through the cities that minimize
the cost and thereby maximizing the profit.
Algorithm TSP(start city, current city, next city, path)
//Purpose: Tof ind the solution for TSP problem using exhaustive search
//Input: The start city, current city,next city and the path //Output: The minimum distance
covered along with the path
Step 1: Check for the disconnection between the current city and the next city
Step 2: Check whether the travelling sales person has visited all the cities
Step 3: Find the next city to be visited
Step 4: Find the solution and terminate
Program:

Algorithms Lab Page 70


INPUT: OUTPUT:

Viva:
1. Explain Traveling Salesperson problem (TSP).

2. What is reduced cost matrix?

3. Write the reduced cost matrix for

4. Explain the procedure in obtaining a reduced cost matrix if tree edge (R, S) corresponding to
edge (i, j) in the graph.

5. What is the time complexity of Branch -and-Bound solution to TSP? How is it better than that
of dynamic programming solution?

Algorithms Lab Page 71


ADDITIONAL PROGRAMS

1. Implement Matrix multiplication using OpenMP

Algorithms Lab Page 72


INPUT: OUTPUT:

Algorithms Lab Page 73


2. Implement an algorithm to find chromatic number of a given graph

Algorithms Lab Page 74


INPUT: OUTPUT:

Algorithms Lab Page 75


OpenMP TUTORIAL
OpenMP is a means of achieving parallelism and therefore concurrency using the C and
FORTRAN languages, despite the fact that there are a few C++ OpenMP source code examples
out there on the Internet. The OpenMP APIs define certain things differently for both of those
languages most of the time, but they also define things the same way for certain elements some of
the time. In short, the idea is to find code blocks that can be parallelized by using the pragma
compiler directive. Other looping constructs eventually branch out of the loop when a condition is
met or a condition is a Boolean true. An OpenMP construct is defined to be a directive (pragma)
plus a block of code. It cannot be just any block of code: it must be a structured block. That is, a
block with one point of entry at the top and a single point of exit at the bottom. An OpenMP
program, therefore, cannot branch into or out of a structured block of code.

Definitions

One of the goals the designers had for OpenMP is for programs to execute and yield the same
results whether they use one thread or many threads. The term for a program to yield the same
results if it executed one thread or many is called sequentially equivalent. Incremental parallelism
refers to a programming practice (which is not always possible) in which a sequential program
evolves into a parallel program. That is, the programmer starts working with a sequential program
from the top down, block by block, and finds pieces of code that are better off executing in
parallel. Thus, parallelism is added incrementally. Having said that, OpenMP is a collection of
compiler directives, library routines, and environmental variables that specify shared-memory
concurrency in FORTRAN, C, and (soon) C++ programs. Note that in the Windows OS, any
memory that can be shared is shared. OpenMP directives demarcate code that can be executed in
parallel (called parallel regions), and control how code is assigned to threads. The threads in
OpenMP code operate under the fork-join model. When the main thread encounters a parallel
region while executing an application, a team of threads is forked off, and these threads begin to
execute the code within the parallel region. At the end of the parallel region, the threads within
the team wait until all other threads in the team have finished before being joined. The main
thread resumes serial execution with the statement following the parallel region. The implicit
barrier at the end of all parallel regions preserves sequential consistency. More to the point, an
executing OpenMP program starts a single thread. At points in the program where parallel
execution is desired, the program forks additional threads to form a team of threads. The threads
execute in parallel across a region of code called the parallel region. At the end of the parallel
region, the threads wait until the full team arrives, and then they join back together. At that point,
the original or master thread continues until the next parallel region (or end of the program).

All OpenMP pragmas have the same prefix of #pragma omp. This is followed by an OpenMP
directive construct and one or more optional clauses to modify the construct. OpenMP is an
explicitly parallel programming language. The compiler doesn't guess how to exploit
concurrency. Any parallelism expressed in a program is there because the programmer directed
the compiler to put it there. To create threads in OpenMP, the programmer designates blocks of
code that are to run in parallel. This is done in C and C++ with the pragma used to define a
parallel region within an application: use the parallel construct:

Algorithms Lab Page 76


#pragma omp parallel

Now, we will take a few simple examples. When compiled, this code is meant to print a string to
standard output console:

#include <stdio.h>
int main()
{
printf("E is the second vowel\n");
}

Output :"E is the second vowel".

Now, we add the compiler directive to define a parallel region in this simple program:

#include <stdio.h>
#include "omp.h"
int main()
{
#pragma omp parallel
{
printf("E is the second vowel\n");
}
}

With a dual-core processor, this is the output:

E is the second vowel


E is the second vowel

Now, we include a local variable:

#include <stdio.h>
#include "omp.h"
int main()
{
int i=5;
#pragma omp parallel
{
printf("E is equal to %d\n",i);
}
}
OpenMP is a shared-memory programming model. A good rule that holds in most cases is that a
variable allocated prior to the parallel region is shared between the threads. So the program prints:
E is equal to 5
E is equal to 5

Algorithms Lab Page 77


The OpenMP specification includes a set of environmental variables and API functions to enable
control over the program. One useful environmental variable is OMP_NUM_THREADS, which
will set the number of threads to be used for the team in each parallel region. The corresponding
API function to set the number of threads is omp_num_threads (). If a variable is declared inside a
parallel region, it is said to be local or private to a thread. In C, a variable declaration can occur in
any block. Such an example is shown below. Included is a call to a function called
omp_get_thread_num (). This integer function is part of the OpenMP runtime library. It returns an
integer unique to each thread that ranges from zero to the number of threads minus one.

#include <stdio.h>
#include <omp.h>
int main()
{
int i= 256; // a shared variable
#pragma omp parallel
{
int x; // a variable local or private to each thread
x = omp_get_thread_num();
printf("x = %d, i = %d\n",x,i);
}
}

Output

x = 0, i = 256
x = 1, i = 256

//note the value of x decrements, while the value of i remains the same

Synchronization

Synchronization is all about timing. Threads running within a process must sometimes access
resources, because the container process has created a handle table where the threads can access
resources by a handle identification number. A resource could be a Registry key, a TCP port, a
file, or any other type of system resource. It is obviously important for those threads to access
those resources in an orderly fashion. It is also obvious that two threads cannot execute
simultaneously in the same CRITICAL_REGION. For example, if one thread writes some data to
a message queue, and then another thread writes over that data, then we have data corruption.
More to the point, we have a race condition: two threads race to execute at a single instance
because they (think) appear to be scheduled that way. A race condition results in a serious system
crash. So, how does OpenMP handle these issues?

OpenMP has synchronization constructs that ensure mutual exclusion to your critical regions. Use
these when variables must remain shared by all threads, but updates must be performed on those
variables in parallel regions. The critical construct acts like a lock around a critical region. Only
one thread may execute within a protected critical region at a time. Other threads wishing to have

Algorithms Lab Page 78


access to the critical region must wait until no thread is executing the critical region. OpenMP
also has an atomic construct to ensure that statements will be executed in an atomic,
uninterruptible manner. There is a restriction on which types of statements you can use with the
atomic construct, and you can only protect a single statement. The single and master constructs
will control execution of statements within a parallel region so that only one thread will execute
those statements (as opposed to allowing only one thread at a time). The former will use the first
thread that encounters the construct, while the latter will allow only the master thread (the thread
that executes outside of the parallel regions) to execute the protected code.

The OpenMP runtime library is then expressed in compiler directives, but there are certain
language features that can only be handled by library functions. Here are a few of them:

 omp_set_num_threads () takes an integer argument and requests that the Operating


System provide that number of threads in subsequent parallel regions.
 omp_get_num_threads () (integer function) returns the actual number of threads in the
current team of threads.
 omp_get_thread_num () (integer function) returns the ID of a thread, where the ID ranges
from 0 to the number of threads minus 1. The thread with the ID of 0 is the master thread.

And, here is code that uses some OpenMP API functions to extract information about the
environment:

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

int main (int argc, char *argv[])


{
int nthreads, tid, procs, maxt, inpar, dynamic, nested;

/* Start parallel region */


#pragma omp parallel private(nthreads, tid)
{

/* Obtain thread number */


tid = omp_get_thread_num();

/* Only master thread does this */


if (tid == 0)
{
printf("Thread %d getting environment info...\n", tid);

/* Get environment information */


procs = omp_get_num_procs();
nthreads = omp_get_num_threads();
maxt = omp_get_max_threads();

Algorithms Lab Page 79


inpar = omp_in_parallel();
dynamic = omp_get_dynamic();
nested = omp_get_nested();

/* Print environment information */


printf("Number of processors = %d\n", procs);
printf("Number of threads = %d\n", nthreads);
printf("Max threads = %d\n", maxt);
printf("In parallel? = %d\n", inpar);
printf("Dynamic threads enabled? = %d\n", dynamic);
printf("Nested parallelism supported? = %d\n", nested);

}
}

Output

Thread 0 getting environment info...


Number of processors = 2
Number of threads = 2
Max threads = 2
In parallel? = 1
Dynamic threads enabled? = 0
Nested parallelism supported? = 0

Some More Core Concepts

In certain cases, a large number of independent operations are found in loops. Using the loop
work sharing construct in OpenMP, you can split up these loop iterations and assign them to
threads for concurrent execution. The parallel for construct will initiate a new parallel region
around the single for loop following the pragma, and divide the loop iterations among the threads
of the team. Upon completion of the assigned iterations, threads sit at the implicit barrier at the
end of the parallel region, waiting to join with the other threads. It is possible to split up the
combined parallel for construct into two pragmas: a parallel construct and the for construct, which
must be lexically contained within a parallel region. Here is an example of the former:

#include <stdlib.h>
#include <stdio.h>
#include <omp.h>
#define CHUNKSIZE 10
#define N 100

int main (int argc, char *argv[])


{

Algorithms Lab Page 80


int nthreads, tid, i, chunk;
float a[N], b[N], c[N];

/* Some initializations */
for (i=0; i < N; i++)
a[i] = b[i] = i * 1.0;
chunk = CHUNKSIZE;

#pragma omp parallel shared(a,b,c,nthreads,chunk) private(i,tid)


{
tid = omp_get_thread_num();
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
printf("Thread %d starting...\n",tid);

#pragma omp for schedule(dynamic,chunk)


for (i=0; i < N; i++)
c[i] = a[i] + b[i];
printf("Thread %d: c[%d]= %f\n",tid,i,c[i]);
}

} /* end of parallel section */

}
Number of threads = 2
Thread 1 starting...
Thread 0 starting...
Thread 0: c[10]= 20.000000
Thread 1: c[0]= 0.000000
Thread 0: c[11]= 22.000000
Thread 1: c[1]= 2.000000
Thread 0: c[12]= 24.000000
Thread 1: c[2]= 4.000000
Thread 0: c[13]= 26.000000
Thread 1: c[3]= 6.000000
Thread 0: c[14]= 28.000000
Thread 1: c[4]= 8.000000
Thread 0: c[15]= 30.000000
Thread 1: c[5]= 10.000000
Thread 0: c[16]= 32.000000
Thread 1: c[6]= 12.000000
Thread 0: c[17]= 34.000000
Thread 1: c[7]= 14.000000

Algorithms Lab Page 81


Thread 0: c[18]= 36.000000
Thread 1: c[8]= 16.000000
Thread 0: c[19]= 38.000000
Thread 1: c[9]= 18.000000
Thread 0: c[20]= 40.000000
Thread 1: c[30]= 60.000000
Thread 0: c[21]= 42.000000
Thread 1: c[31]= 62.000000
Thread 0: c[22]= 44.000000
Thread 1: c[32]= 64.000000
Thread 0: c[23]= 46.000000
Thread 1: c[33]= 66.000000

. . . . . . .

Thread 1: c[84]= 168.000000


Thread 1: c[85]= 170.000000
Thread 1: c[86]= 172.000000
Thread 1: c[87]= 174.000000
Thread 1: c[88]= 176.000000
Thread 1: c[89]= 178.000000
Thread 1: c[90]= 180.000000
Thread 1: c[91]= 182.000000
Thread 1: c[92]= 184.000000
Thread 1: c[93]= 186.000000
Thread 1: c[94]= 188.000000
Thread 1: c[95]= 190.000000
Thread 1: c[96]= 192.000000
Thread 1: c[97]= 194.000000
Thread 1: c[98]= 196.000000
Thread 1: c[99]= 198.000000
Thread 0: c[24]= 48.000000
Thread 0: c[25]= 50.000000
Thread 0: c[26]= 52.000000
Thread 0: c[27]= 54.000000
Thread 0: c[28]= 56.000000
Thread 0: c[29]= 58.000000

Clauses Used in the Data Environment

We begin by defining the terms we will use to describe the data environment in OpenMP. In a
program, a variable is a container (or more concretely, a storage location in memory) bound to a
name and holding a value. Variables can be read and written as the program runs (as opposed to
constants that can only be read). In OpenMP, the variable that is bound to a given name depends
on whether the name appears prior to a parallel region, inside a parallel region, or following a
parallel region. When the variable is declared prior to a parallel region, it is by default shared, and

Algorithms Lab Page 82


the name is always bound to the same variable. OpenMP, however, includes clauses that can be
added to parallel and to the work sharing constructs to control the data environment. These
clauses affect the variable bound to a name. A private (list) clause directs the compiler to create,
for each thread, a private (or local) variable for each name included in the list. The names in the
private list must have been defined and bound to share variables prior to the parallel region. The
initial values of these new private variables are undefined, so they must be explicitly initialized.
Furthermore, after the parallel region, the value of a variable bound to a name appearing in a
private clause for the region is undefined.

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#define N 50
#define CHUNKSIZE 5

int main (int argc, char *argv[])


{
int i, chunk, tid;
float a[N], b[N], c[N];
char first_time;

/* Some initializations */
for (i=0; i < N; i++)
a[i] = b[i] = i * 1.0;
chunk = CHUNKSIZE;
first_time = 'y';

#pragma omp parallel for \


shared(a,b,c,chunk) \
private(i,tid) \
schedule(static,chunk) \
firstprivate(first_time)

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


{
if (first_time == 'y')
{
tid = omp_get_thread_num();
first_time = 'n';
}
c[i] = a[i] + b[i];
printf("tid= %d i= %d c[i]= %f\n", tid, i, c[i]);
}
}

The output is as expected:

Algorithms Lab Page 83


tid= 0 i= 0 c[i]= 0.000000
tid= 1 i= 5 c[i]= 10.000000
tid= 0 i= 1 c[i]= 2.000000
tid= 1 i= 6 c[i]= 12.000000
tid= 0 i= 2 c[i]= 4.000000
tid= 1 i= 7 c[i]= 14.000000
tid= 0 i= 3 c[i]= 6.000000
tid= 1 i= 8 c[i]= 16.000000
tid= 0 i= 4 c[i]= 8.000000
tid= 1 i= 9 c[i]= 18.000000

. . . . . and so on

tid= 1 i= 47 c[i]= 94.000000


tid= 0 i= 43 c[i]= 86.000000
tid= 1 i= 48 c[i]= 96.000000
tid= 0 i= 44 c[i]= 88.000000
tid= 1 i= 49 c[i]= 98.000000

Algorithms Lab Page 84

You might also like