LP 1 Lab Manual - v1
LP 1 Lab Manual - v1
LABORATORY MANUAL
LABORATORY PRACTICE - I
BE-COMP
SEMESTER-I
1 a) Implement Parallel Reduction using Min, Max, Sum and Average operations.
b) Write a CUDA program that, given an N-element vector, find-
Test for input N and generate a randomized vector V of length N (N should be large). The
program should generate output as the two computed maximum values as well as the time
taken to find each value.
2 Vector and Matrix Operations-
Design parallel algorithm to
1. Add two large vectors
2. Multiply Vector and Matrix
3. Multiply two N × N arrays using n2 processors
3 Parallel Sorting Algorithms-
For Bubble Sort and Merger Sort, based on existing sequential algorithms, design and
implement parallel algorithm utilizing all resources available.
4 Parallel Search Algorithm-
Design and implement parallel algorithm utilizing all resources available. for
Binary Search for Sorted Array
Depth-First Search ( tree or an undirected graph ) OR
Breadth-First Search ( tree or an undirected graph) OR
Best-First Search that ( traversal of graph to reach a target in the shortest possible
path)
Group B
5 Implement Tic-Tac-Toe using A* algorithm
6 Implement 3 missionaries and 3 cannibals problem depicting appropriate graph. Use A*
algorithm.
7 Solve 8-puzzle problem using A* algorithm. Assume any initial configuration and define
goal configuration clearly
8 Use Heuristic Search Techniques to Implement Hill-Climbing Algorithm.
Group C
9 Download the Iris flower dataset or any other dataset into a DataFrame. (eg
https://archive.ics.uci.edu/ml/datasets/Iris ) Use Python/R and Perform following –
How many features are there and what are their types (e.g., numeric, nominal)?
Compute and display summary statistics for each feature available in the dataset.
(eg. minimum value, maximum value, mean, range, standard deviation, variance
and percentiles
Data Visualization-Create a histogram for each feature in the dataset to illustrate
the feature distributions. Plot each histogram.
Create a boxplot for each feature in the dataset. All of the boxplots should be
combined into a single plot. Compare distributions and identify outliers.
10 Download Pima Indians Diabetes dataset. Use Naive Bayes‟ Algorithm for classification
Load the data from CSV file and split it into training and test datasets.
Summarize the properties in the training dataset so that we can calculate
probabilities and make predictions.
Classify samples from a test dataset and a summarized training dataset.
11 Trip History Analysis: Use trip history dataset that is from a bike sharing service in the
United States. The data is provided quarter-wise from 2010 (Q4) onwards. Each file has 7
columns. Predict the class of user. Sample Test data set available here
https://www.capitalbikeshare.com/trip-history-data
12 Twitter Data Analysis: Use Twitter data for sentiment analysis. The dataset is 3MB in
size and has 31,962 tweets. Identify the tweets which are hate tweets and which are
not. Sample Test data set available here
https://datahack.analyticsvidhya.com/contest/practice-problem-twitter-sentiment-analysis/
GROUP A: ASSIGNMENTS
Aim: Implement parallel reduction using Min, Max, Sum and Average Operations.
Minimum
Maximum
Sum
Average
Pre-requisites:
Theory:
OpenMP:
OpenMP is a set of C/C++ pragmas (or FORTRAN equivalents) which provide the
programmer a high-level front-end interface which get translated as calls to threads (or other
similar entities). The key phrase here is "higher-level"; the goal is to better enable the
programmer to "think parallel," alleviating him/her of the burden and distraction of dealing
with setting up and coordinating threads. For example, the OpenMP directive.
OpenMP Core Syntax:
Following is the sample code which illustrates max operator usage in OpenMP :
#include <stdio.h>
#include <omp.h>
int main()
{
double arr[10];
omp_set_num_threads(4);
double max_val=0.0;
int i;
for( i=0; i<10; i++)
arr[i] = 2.0 + i;
#pragma omp parallel for reduction(max : max_val)
for( i=0;i<10; i++)
{
printf("thread id = %d and i = %d", omp_get_thread_num(), i);
if(arr[i] > max_val)
{
max_val = arr[i];
}
}
printf("\nmax_val = %f", max_val);
}
Following is the sample code which illustrates min operator usage in OpenMP :
#include <stdio.h>
#include <omp.h>
int main()
{
double arr[10];
omp_set_num_threads(4);
double min_val=0.0;
int i;
for( i=0; i<10; i++)
arr[i] = 2.0 + i;
#pragma omp parallel for reduction(min : min_val)
for( i=0;i<10; i++)
{
printf("thread id = %d and i = %d", omp_get_thread_num(), i);
if(arr[i] < min_val)
{
min_val = arr[i];
}
}
printf("\nmin_val = %f", min_val);
}
Following is the sample code which illustrates sum operation usage in OpenMP :
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
Following is the sample code which illustrates sum operation usage in OpenMP :
Conclusion: We have implemented parallel reduction using Min, Max, Sum and
Average Operations.
Experiment No: 01 b
Objective: To study and implement the operations on vector, generate o/p as two computed
max values as well as time taken to find each value.
Pre-requisites:
Theory:
Sequential Programming:
When solving a problem with a computer program, it is natural to divide the problem into a
discrete series of calculations; each calculation performs a specified task, as shown in
following Figure .Such a pro-gram is called a sequential program.
Parallel Programming:
➤ Task parallelism
➤ Data parallelism
Task parallelism arises when there are many tasks or functions that can be operated
independently and largely in parallel. Task parallelism focuses on distributing
functions across multiple cores.
Data parallelism arises when there are many data items that can be operated on at the
same time.
Data parallelism focuses on distributing the data across multiple cores.
CUDA :
parallel computations. Any applications that process large data sets can use a data-parallel
model
to speed up the computations. Data-parallel processing maps data elements to parallel
threads.
The first step in designing a data parallel program is to partition data across threads, with
each
thread working on a portion of the data.
The first step in designing a data parallel program is to partition data across threads, with
each
thread working on a portion of the data.
CUDA Architecture:
➤Host code
➤Device code
Host code runs on CPUs and device code runs on GPUs. An application executing on a
heterogeneous platform is typically initialized by the CPU. The CPU code is responsible for
managing the environment, code, and data for the device before loading compute-intensive
tasks on the device. With computational intensive applications, program sections often
exhibit a rich amount of data parallelism. GPUs are used to accelerate the execution of this
portion of data parallelism. When a hardware component that is physically separate from the
CPU is used to accelerate computationally intensive sections of an application, it is referred
to as a hardware accelerator. GPUs are arguably the most common example of a hardware
accelerator. GPUs must operate in conjunction with a CPU-based host through a PCI-Express
bus, as shown in Figure.
NVIDIA’s CUDA nvcc compiler separates the device code from the host code during
the compilation process. The device code is written using CUDA C extended with
keywords for labeling data-parallel functions, called kernels . The device code is
further compiled by
Nvcc . During the link stage, CUDA runtime libraries are added for kernel procedure
calls and explicit GPU device manipulation. Further kernel function, named
helloFromGPU, to print the string of “Hello World from GPU!” as follows:
helloFromGPU <<<1,10>>>();
Triple angle brackets mark a call from the host thread to the code on the device side.
A kernel is executed by an array of threads and all threads run the same code. The
parameters within the triple angle brackets are the execution configuration, which
specifies how many threads will execute the kernel. In this example, you will run 10
GPU threads.
Table lists the standard C functions and their corresponding CUDA C functions for memory
operations. Host and Device Memory Functions are follows.
Organizing Threads:
When a kernel function is launched from the host side, execution is moved to a device where
a large number of threads are generated and each thread executes the statements specified by
the
kernel function. The two-level thread hierarchy decomposed into blocks of threads and grids
of blocks, as shown in following figure:
All threads spawned by a single kernel launch are collectively called a grid . All
threads in a grid
share the same global memory space. A grid is made up of many thread blocks. A
thread block is a group of threads that can cooperate with each other using:
➤ Block-local synchronization
blockIdx.x
blockIdx.y
blockIdx.z
threadIdx.x
threadIdx.y
threadIdx.z
CUDA organizes grids and blocks in three dimensions. The dimensions of a grid and a block
are specified by the following two built-in variables:
These variables are of type dim3, that is used to specify dimensions. When defining a
variable of type dim3, any component left unspecified is initialized to 1.Each component in a
variable of type dim3 is accessible through its x,y,, and z fields, respectively, as shown in the
following example:
blockDim.x
blockDim.y
blockDim.
{
int i = threadIdx.x; // initialize i to thread ID
*c = a[55];
int main()
{
int i;
srand(time(NULL)); //makes use of the computer's internal clock to
control the choice of the seed
int a[SIZE];
int c;
cudaMemcpy(dev_a , a, SIZE*sizeof(int),cudaMemcpyHostToDevice);
//copy the array from CPU to GPU
min<<<1,SIZE>>>(dev_a,dev_c);
// call kernel function <<<number of blocks, number of
threads
cudaMemcpy(&c, dev_c, SIZE*sizeof(int),cudaMemcpyDeviceToHost);
// copy the result back from GPU to CPU
printf("\nmin = %d ",c);
return 0;
#include <cuda.h>
#include <stdio.h>
#include <time.h>
*c = a[0];
int main()
{
int i;
srand(time(NULL)); //makes use of the computer's internal clock to
control the choice of the seed
int a[SIZE];
int c;
cudaMemcpy(dev_a , a, SIZE*sizeof(int),cudaMemcpyHostToDevice);
printf("\nmax = %d ",c);
return 0;
}
int i;
printf("Input array");
// Initialize vectors on host
/*for( i = 0; i < n; i++ ) {
// h_a[i] = sin(i)*sin(i);
//printf("\n",i);
h_a[i]=i;
//printf("\n%d", h_a[i]);
//h_b[i]=i;
//h_b[i] = cos(i)*cos(i);
}*/
cudaMalloc(&h_a, size);
return 0;
}
int main( )
{
//int n = 100000;
int n=5; // Size of vectors
int i;
h_a[i]=i;
h_b[i]=i;
return 0;
}
Experiment No: 2
Title: Vector and Matrix Operations-
Design parallel algorithm to
1. Add two large vectors
2. Multiply Vector and Matrix
3. Multiply two N × N arrays using n2 processors
Aim: Implement nxn matrix parallel addition, multiplication using CUDA, use
shared memory.
Prerequisites:
- Concept of matrix addition, multiplication.
- Basics of CUDA programming
Objectives:
Student should be able to learn parallel programming, CUDA architecture and CUDA
processing flow
Theory:
A straightforward matrix multiplication example that illustrates the basic features of memory
and thread management in CUDA programs
• Leave shared memory usage until later
• Local, register usage
• Thread ID usage
Memory data transfer API between host and device
• P = M * N of size WIDTH x WIDTH
Without tiling:
• One thread handles one element of P
• M and N are loaded WIDTH times from global memory
M P
2
4 4
8 2
WIWI
WIWIDTDT Bl
3 DT 5G
2 DT H4 oc
H
H H ri k
d 1
1
Thread
• (2, 2)
•
•
Step 3: Host-side Main Program Code
int main(void) {
// Allocate and initialize the matrices
Matrix M = AllocateMatrix(WIDTH, WIDTH, 1);
Matrix N = AllocateMatrix(WIDTH, WIDTH, 1);
Matrix P = AllocateMatrix(WIDTH, WIDTH, 0);
// M * N on the device
MatrixMulOnDevice(M, N, P);
// Free matrices
FreeMatrix(M);
FreeMatrix(N);
FreeMatrix(P);
return 0;
}
Host-side code
// Matrix multiplication on the device
void MatrixMulOnDevice(const Matrix M, const Matrix N, Matrix P)
{
// Load M and N to the device
Matrix Md = AllocateDeviceMatrix(M);
CopyToDeviceMatrix(Md, M);
Matrix Nd = AllocateDeviceMatrix(N);
CopyToDeviceMatrix(Nd, N);
// Allocate P on the device
Matrix Pd = AllocateDeviceMatrix(P);
CopyToDeviceMatrix(Pd, P); // Clear memory
// Setup the execution configuration
dim3 dimBlock(WIDTH, WIDTH);
dim3 dimGrid(1, 1);
// Launch the device computation threads!
MatrixMulKernel<<<dimGrid, dimBlock>>>(Md, Nd, Pd);
// Read P from the device
CopyFromDeviceMatrix(P, Pd);
// Free device matrices
N
B
M L
O
Facilities:
Latest version of 64 Bit Operating Systems, CUDA enabled NVIDIA Graphics card
Input:
Two matrices
Output:
Multiplication of two matrix
Software Engg.:
Mathematical Model:
Conclusion:
We learned parallel programming with the help of CUDA architecture.
Questions:
1. What is CUDA?
2. Explain Processing flow of CUDA programming.
3. Explain advantages and limitations of CUDA.
4. Make the comparison between GPU and CPU.
5. Explain various alternatives to CUDA.
6. Explain CUDA hardware architecture in detail.
Assignment No: 3
Title: For Bubble Sort and Merger Sort, based on existing sequential algorithms, design and
implement parallel algorithm utilizing all resources available.
Aim: Understand Parallel Sorting Algorithms like Bubble sort and Merge Sort.
Prerequisites:
Student should know basic concepts of Bubble sort and Merge Sort.
Objective: Study of Parallel Sorting Algorithms like Bubble sort and Merge Sort
Theory:
i) What is Sorting?
Sorting is a process of arranging elements in a group in a particular order, i.e., ascending
order, descending order, alphabetic order, etc.
Characteristics of Sorting are:
• Arrange elements of a list into certain order
• Make data become easier to access
• Speed up other operations such as searching and merging. Many sorting algorithms
with different time and space complexities
Bubble Sort
The idea of bubble sort is to compare two adjacent elements. If they are not in the
right order,switch them. Do this comparing and switching (if necessary) until the end of the
array is reached. Repeat this process from the beginning of the array n times.
2. If k is even then
3. for i = 0 to (n/2)-1 do in parallel
4. If A[2i] > A[2i+1] then
5. Exchange A[2i] ↔ A[2i+1]
6. Else
7. for i = 0 to (n/2)-2 do in parallel
8. If A[2i+1] > A[2i+2] then
9. Exchange A[2i+1] ↔ A[2i+2]
10. Next k
Merge Sort
• Collects sorted list onto one processor
• Merges elements as they come together
• Simple tree structure
• Parallelism is limited when near the root
Theory:
To sort A[p .. r]:
1. Divide Step
If a given array A has zero or one element, simply return; it is already sorted. Otherwise,
splitA[p .. r] into two subarraysA[p .. q] and A[q + 1 .. r], each containing about half of the
elements of A[p .. r]. That is, q is the halfway point of A[p .. r].
2. Conquer Step
Conquer by recursively sorting the two subarraysA[p .. q] and A[q + 1 .. r].
3. Combine Step
Combine the elements back in A[p .. r] by merging the two sorted subarraysA[p .. q] and
A[q + 1 .. r] into a sorted sequence. To accomplish this step, we will define a procedure
MERGE (A, p, q, r).
Example:
1. Procedure parallelMergeSort
2. Begin
3. Create processors Pi where i = 1 to n
4. if i > 0 then recieve size and parent from the root
5. recieve the list, size and parent from the root
6. endif
7. midvalue= listsize/2
8. if both children is present in the tree then
9. send midvalue, first child
10. send listsize-mid,second child
11. send list, midvalue, first child
12. send list from midvalue, listsize-midvalue, second child
13. call mergelist(list,0,midvalue,list, midvalue+1,listsize,temp,0,listsize)
14. store temp in another array list2
15. else
16. call parallelMergeSort(list,0,listsize)
17. endif
18. if i >0 then
19. send list, listsize,parent
20. endif
21. end
INPUT:
1. Array of integer numbers.
OUTPUT:
1. Sorted array of numbers
FAQ
1. What is sorting?
2. What is parallel sort?
3. How to sort the element using Bubble Sort?
4. How to sort the element using Parallel Bubble Sort?
5. How to sort the element using Parallel Merge Sort?
6. How to sort the element using Merge Sort?
7. What is searching?
8. Different types of searching methods.
9. Time complexities of sorting and searching methods.
10. How to calculate time complexity?
11. What are space complexity of all sorting and searching methods?
12. Explain what is best, worst and average case for each method of searching and
sorting.
ALGORITHM ANALYSIS
1. Time Complexity Of parallel Merge Sort and parallel Bubble sort in best case is(
when all data is already in sorted form):O(n)
2. Time Complexity Of parallel Merge Sort and parallel Bubble sort in worst case is:
O(n logn)
3. Time Complexity Of parallel Merge Sort and parallel Bubble sort in average case is:
O(n logn)
APPLICATIONS
1. Representing Linear data structure & Sequential data organization : structure & files
2. For Sorting sequential data structure
CONCLUSION
Thus, we have studied Parallel Bubble and Parallel Merge sort implementation.
Experiment No: 4
Aim: Design and implement parallel algorithm utilizing all resources available. For
Binary Search for Sorted Array
Depth-First Search ( tree or an undirected graph ) OR
Breadth-First Search ( tree or an undirected graph) OR
Best-First Search that ( traversal of graph to reach a target in the shortest possible
path)
Outcome: Students will be understand the implementation of Binary search and BFS,
DFS
Pre-requisites:
Theory:
Binary Search:
Binary search runs in logarithmic time in the worst case, making O(log n) comparisons,
where n is the number of elements in the array, the O is Big O notation, and log is the
logarithm. Binary search takes constant (O(1)) space, meaning that the
space taken by the algorithm is the same for any number of elements in the array.Binary
search is faster than linear search except for small arrays, but the array must be sorted
first. Although specialized data structures designed for fast searching, such as hash
tables, can be searched more efficiently, binary search applies to a wider range of
problems.
How Binary Search Works?
For a binary search to work, it is mandatory for the target array to be sorted. We shall
learn the process of binary search with a pictorial example. The following is our sorted
array and let us assume that we need to search the location of value 31 using binary
search.
Now we compare the value stored at location 4, with the value being searched, i.e. 31.
We find that the value at location 4 is 27, which is not a match. As the value is greater
than 27 and we have a sorted array, so we also know that the target value must be in the
upper portion of the array.
We change our low to mid + 1 and find the new mid value again.
low = mid + 1
The value stored at location 7 is not a match, rather it is more than what we are
looking for. So, the value must be in the lower part from this location.
We compare the value stored at location 5 with our target value. We find that it is a
match.
#include<iostream>
#include<stdlib.h>
#include<omp.h> using
int low1,low2,high1,high2,mid1,mid2,found=0,loc=-1;
#pragma omp parallel sections { #pragma omp section {
low1=low; high1=mid;
while(low1<=high1) {
mid1=(low1+high1)/2;
mid2=(low2+high2)/2;
} } }
return loc;
int main(){
int *a,i,n,key,loc=-1;
Breadth-First Search :
Graph traversals
Graph traversal means visiting every vertex and edge exactly once in a well-defined order.
While using certain graph algorithms, you must ensure that each vertex of the graph is visited
exactly once. The order in which the vertices are visited are important and may depend upon
the algorithm or question that you are solving.
During a traversal, it is important that you track which vertices have been
visited. The most common way of tracking vertices is to mark them.
The distance between the nodes in layer 1 is comparitively lesser than the distance between
the nodes in layer 2. Therefore, in BFS, you must traverse all the nodes in layer 1 before you
move to the nodes in layer 2.
Traversing process
Code for BFS:
#include<iostream>
#include<stdlib.h>
#include<queue> using
namespace std;
class node{ public: node *left, *right; int data;};
else { q.push(temp->left); }
if(temp->right==NULL) { temp-
>right=new node; temp->right-
>left=NULL; temp->right-
>right=NULL; temp->right-
>data=data;
return root; }
else { q.push(temp->right); }
} }
qSize = q.size();
currNode = q.front();
int main(){
bfs(root);
return 0;
}
GROUP B: ASSIGNMENTS
Heuristic Search
Aim:
Use heuristic search techniques to implement best first search and A* algorithm.
Software Requirements:
NetBeans IDE
Program Execution:
Using BFS
run:
Enter No of nodes : 5
A : (B,3), (C,1)
B : (D,3), (E,2)
C:
D:
E:
A
C B
B
B
E D
D
D
Path :
A, B, D
BUILD SUCCESSFUL (total time: 1 minute 8 seconds)
Using A* algorithm
run:
Enter No of nodes : 4
Fx of node A = 6
Open List : A
Closed List : Empty
Open List : Empty
Closed List : A
Fx of node B = 5
Fx of node C = 6
Open List : B C
Open List : C
Closed List : A B
Fx of node D = 4
Open List : D C
Open List : C
Closed List : A B D
Path :
A, B, D
For Fn = 5 :
- a c
h b d
g f e
For Fn = 5 :
a c -
h b d
g f e
For Fn = 2 :
a b c
h - d
g f e
Software Requirements:
SWI-Prolog for Windows, Editor.
Theory:
A system that uses human expertise to make complicated decisions. Simulates reasoning
by applying knowledge and interfaces. Uses expert’s knowledge as rules and data within
the system. Models the problem solving ability of a human expert.
Components of an ES:
1. Knowledge Base
i. Represents all the data and information imputed by experts in the field.
ii. Stores the data as a set of rules that the system must follow to make
decisions.
2. Reasoning or Inference Engine
i. Asks the user questions about what they are looking for.
ii. Applies the knowledge and the rules held in the knowledge base.
iii. Appropriately uses this information to arrive at a decision.
3. User Interface
i. Allows the expert system and the user to communicate.
ii. Finds out what it is that the system needs to answer.
iii. Sends the user questions or answers and receives their response.
4. Explanation Facility
i. Explains the systems reasoning and justifies its conclusions.
Program Execution:
?- go.
1: Chloramphenicol
2: Amoxicillin
3: Ciprofloxacin
4: Azithromycin
true .
ChatBot
Aim:
Develop elementary chatbot for suggesting investment as per the customer needs.
Software Requirements:
NetBeans, program-ab library.
Set-up steps:
Download the program-ab file.
Program Execution:
Execute a ChatBot Jar file using following command
Java –jar dist/ChatBot.jar (Due to execution of jar file, aimlif folder updated
automatically)
Aimlif contains csv(comma separated files) which are the filed used for early
execution
Human : Hello
Robot : Some of the investment options are Public provident fund Mutual fund Equity shares
Real estate investment etc.
Robot : No limit
Human : Bye
Theory :
Hill Climbing is a technique to solve certain optimization problems. In this technique, we start
with a suboptimal solution and the solution is improved repeatedly until some condition is
maximized.
The idea of starting with a sub-optimal solution is compared to starting from the base of the
hill, improving the solution is compared to walking up the hill, and finally maximizing some
Hence, the hill climbing technique can be considered as the following phases −
Input :
As we are using Random function in java , we will get different input states each time we
execute the code.
Queens are placed on the board depending upon the generated random number.
{ queenPositions = generateQueens();
Department of Computer Engineering
ZES’s DCOER, Pune-411041 Page 55
Laboratory Practice-I BE (Comp)
board[queenPositions[i]][i] = 1;
}}
Therefore, for the following random numbers, the queens will be placed on the board
like this :
7
3
5
7
1
5
1
1
Corresponding Board:
00000000
00001011
00000000
01000000
00000000
00100100
00000000
10010000
The Goal Stack Planning Algorithms works will the stack. It starts by pushing the
unsatisfied goals into the stack. Then it pushes the individual subgoals into the stack and
its pops an element out of the stack. When popping an element out of the stack the
element could be either a predicate describing a situation about our world or it could be
an action that can be applied to our world under consideration. So based on the kind of
element we are popping out from the stack a decision has to be made. If it is a
Predicate. Then compares it with the description of the current world, if it is satisfied or
is already present in our current situation then there is nothing to do because already its
true. On the contrary if the Predicate is not true then we have to select and push
relevant action satisfying the predicate to the Stack.
So after pushing the relevant action into the stack its precondition should also has to be
pushed into the stack. In order to apply an operation its precondition has to be satisfied.
In other words the present situation of the world should be suitable enough to apply an
operation. For that, the preconditions are pushed into the stack once after an action is
pushed.
Input:
Consider the following where wish to proceed from the start to goal state.
No of Blocks : 4
Initial stage : (on b a)^(ontable c)^(ontable a)^(ontable d)^(clear b)^(clear c)^(clear
d)^(AE)
Final stage : (on c a)^(on b d)^(ontable a)^(ontable d)^(clear c)^(clear b)^(AE)
Output:
Set of actions to be taken:
1. (unstack b d)
2. (stack b d)
3. (pick c)
4. (stack c a)
Objective:
Theory:
Introduction:
A* algorithm is a best-first search algorithm in which the cost associated with a node is
f(n) = g(n) + h(n), where g(n) is the cost of the path from the initial state to node n and
h(n) is the heuristic estimate or the cost or a path from node n to a goal.
Thus, f(n) estimates the lowest total cost of any solution path going through node n. At
each point a node with lowest f value is chosen for expansion. Ties among nodes of equal
f value should be broken in favor of nodes with lower h values. The algorithm terminates
when a goal is chosen for expansion.
A* algorithm guides an optimal path to a goal if the heuristic function h(n) is admissible,
meaning it never overestimates actual cost. For example, since airline distance never
overestimates actual highway distance, and manhattan distance never overestimates actual
moves in the gliding tile.
For Puzzle, A* algorithm, using these evaluation functions, can find optimal solutions to
these problems. In addition, A* makes the most efficient use of the given heuristic
function in the following sense: among all shortest-path algorithms using the given
heuristic function h(n). A* algorithm expands the fewest number of nodes.
The main drawback of A* algorithm and indeed of any best-first search is its memory
requirement. Since at least the entire open list must be saved, A* algorithm is severely
space-limited in practice, and is no more practical than best-first search algorithm on
current machines. For example, while it can be run successfully on the eight puzzles, it
exhausts available memory in a matter of minutes on the fifteen puzzles.
A star algorithm is very good search method, but with complexity problems
To implement such a graph-search procedure, we will need to use two lists of node:
1) OPEN: nodes that have been generated and have had the heuristic function applied to
them but which have not yet been examined (i.e., had their successors generated). OPEN
is actually a priority queue in which the elements with the highest priority are those with
the most promising value of the heuristic function.
2) CLOSED: Nodes that have already been examined. We need to keep these nodes in
memory if we want to search a graph rather than a tree, since whether a node is generated,
we need to check whether it has been generated before
A * Algorithm:
4. If n is a goal node exit successfully with a solution path obtained by tracing back
the pointers from n to s.
5. Otherwise, expand n generating its children and directing pointers from each child
node to n.
6. Go to step 2.
• h1(S) = 8
• h2(S) = 3+1+2+2+2+3+3+2 = 18
f(n)=g(n)+h(n)
A* is commonly used for the common path finding problem in applications such as
games, but was originally designed as a general graph traversal algorithm.
n-queens problem
Objective:
Student will learn:
1. The basic concept of constraint satisfaction problem and backtracking.
2. General structure of N Queens problem.
Theory:
The N Queen is the problem of placing N chess queens on an N×N chessboard so that no
two queens attack each other. For example, following is a solution for 4 Queen problem.
The expected output is a binary matrix which has 1s for the blocks where queens are
placed.
For example, following is the output matrix for above 4 queen solution.
{ 0, 1, 0, 0}
{ 0, 0, 0, 1}
{ 1, 0, 0, 0}
{ 0, 0, 1, 0}
Generate all possible configurations of queens on board and print a configuration that
satisfies the given constraints.while there are untried conflagrations
{
generate the next configuration
if queens don't attack in this configuration then
{
print this configuration;
}
}
Backtracking Algorithm
Backtracking is finding the solution of a problem whereby the solution depends on the
previous steps taken.
In backtracking, we first take a step and then we see if this step taken is correct or not i.e.,
whether it will give a correct answer or not. And if it doesn’t, then we just come back and
change our first step. In general, this is accomplished by recursion. Thus, in backtracking,
we first start with a partial sub-solution of the problem (which may or may not lead us to
the solution) and then check if we can proceed further with this sub-solution or not. If not,
then we just come back and change it.
Thus, the general steps of backtracking are:
• start with a sub-solution
• check if this sub-solution will lead to the solution or not
• If not, then come back and change the sub-solution and continue again
The idea is to place queens one by one in different columns, starting from the leftmost
column. When we place a queen in a column, we check for clashes with already placed
queens. In the current column, if we find a row for which there is no clash, we mark this
row and column as part of the solution. If we do not find such a row due to clashes then
we backtrack and return false.
Algorithm:
3) Try all rows in the current column. Do following for every tried row.
a) If the queen can be placed safely in this row then mark this [row, column] as part of
the solution and recursively check if placing queen here leads to a solution.
b) If placing the queen in [row, column] leads to a solution then return true.
c) If placing queen doesn't lead to a solution then unmark this [row, column]
(Backtrack) and go to step (a) to try other rows.
3) If all rows have been tried and nothing worked, return false to trigger backtracking.
Objective:
Student will learn:
i) The Basic Concepts of Goal Stack Planing.
ii) General Algorithm for Goal Stack Planing.
iii) Logic of goal stack planning implementation.
Theory:
Introduction:
The planning in Artificial Intelligence is about the decision making tasks performed by
the robots or computer programs to achieve a specific goal.The execution of planning is
about choosing a sequence of actions with a high likelihood to complete the specific task.
Choose the best rule for applying the next rule based on the best available
heuristics.
Apply the chosen rule for computing the new problem state.
Detect dead ends so that they can be abandoned and the system’s effort is
directed in more fruitful directions.
Noninterleaved planners of the early 1970s were unable to solve this problem, hence it is
considered as anomalous.
When two subgoals G1 and G2 are given, a noninterleaved planner produces either a
plan for G1 concatenated with a plan for G2, or vice-versa.
In blocks-world problem, three blocks labeled as 'A', 'B', 'C' are allowed to rest on the flat
surface. The given condition is that only one block can be moved at a time to achieve the
goal.
This is one of the most important planning algorithms, which is specifically used by
STRIPS.
The stack is used in an algorithm to hold the action and satisfy the goal. A knowledge
base is used to hold the current state, actions.
Goal stack is similar to a node in a search tree, where the branches are created if there is a
choice of an action.
i. Start by pushing the original goal on the stack. Repeat this until the stack becomes
empty. If stack top is a compound goal, then push its unsatisfied subgoals on the stack.
ii. If stack top is a single unsatisfied goal then, replace it by an action and push the
action’s precondition on the stack to satisfy the condition.
iii. If stack top is an action, pop it from the stack, execute it and change the knowledge
base by the effects of the action.
Basic Idea to handle interactive compound goals uses goal stacks, Here the stack contains
:
goals,
operators -- ADD, DELETE and PREREQUISITE lists
a database maintaining the current situation for each operator used.
Consider the following where wish to proceed from the start to goal state.
Department of Computer Engineering
ZES’s DCOER, Pune-411041 Page 66
Laboratory Practice-I BE (Comp)
Start state:
Goal state:
Experiment
Title :
Download the Iris flower dataset or any other dataset into a DataFrame.
(eg. https://archive.ics.uci.edu/ml/datasets/Iris ) Use Python/R and Perform following –
• How many features are there and what are their types (e.g., numeric, nominal)?
• Compute and display summary statistics for each feature available in the dataset. (eg.
minimum value, maximum value, mean, range, standard deviation, variance and
percentiles
• Data Visualization-Create a histogram for each feature in the dataset to illustrate the
feature distributions. Plot each histogram.
• Create a boxplot for each feature in the dataset. All of the boxplots should be combined
into a single plot. Compare distributions and identify outliers
Aim :
Implement a dataset into a dataframe. Implement the following operations:
Prerequisites:
Fundamentals of R -Programming Languages
Objectives :
To learn the concept of how to display summary statistics for each feature Available in
the dataset
Theory:
How to Find the Mean, Median, Mode, Range, and Standard Deviation
Simplify comparisons of sets of number, especially large sets of number, by calculating
the center values using mean, mode and median. Use the ranges and standard deviations
of the sets to examine the variability of data.
Calculating Mean
The mean identifies the average value of the set of numbers. For example, consider the
data set containing the values 20, 24, 25, 36, 25, 22, 23.
Formula
To find the mean, use the formula: Mean equals the sum of the numbers in the data set
divided by the number of values in the data set. In mathematical terms: Mean=(sum of all
terms)÷(how many terms or values in the set).
Finding Divisor
Divide by the number of data points in the set. This set has seven values so divide by 7.
Finding Mean
Insert the values into the formula to calculate the mean. The mean equals the sum of the
values (175) divided by the number of data points (7). Since 175÷7=25, the mean of this
data set equals 25. Not all mean values will equal a whole number.
Calculating Range
Range shows the mathematical distance between the lowest and highest values in the data
set. Range measures the variability of the data set. A wide range indicates greater
variability in the data, or perhaps a single outlier far from the rest of the data. Outliers
may skew, or shift, the mean value enough to impact data analysis.
Calculating Range
To calculate range, subtract the lowest value from the highest value. Since 36-20=16, the
range equals 16.
.
Formula
Finding standard deviation requires summing the squared difference between each data
point and the mean [∑(x-µ)2], adding all the squares, dividing that sum by one less than
the number of values (N-1), and finally calculating the square root of the dividend.
Mathematically, start with calculating the mean.
Next, subtract the mean from each data point, then square each difference. The formula
looks like this: ∑(x-µ)2, where ∑ means sum, x represents each data set value and µ
represents the mean value. Continuing with the example set, the values become: 20-25=-5
and -52=25; 24-25=-1 and -12=1; 25-25=0 and 02=0; 36-25=11 and 112=121; 25-25=0 and
02=0; 22-25=-3 and -32=9; and 23-25=-2 and -22=4.
Divide the sum of the squared differences by one less than the number of data points. The
example data set has 7 values, so N-1 equals 7-1=6. The sum of the squared differences,
160, divided by 6 equals approximately 26.6667.
Standard Deviation
Calculate the standard deviation by finding the square root of the division by N-1. In the
example, the square root of 26.6667 equals approximately 5.164. Therefore, the standard
deviation equals approximately 5.164.
Evaluating Standard Deviation
Standard deviation helps evaluate data. Numbers in the data set that fall within one
standard deviation of the mean are part of the data set. Numbers that fall outside of two
standard deviations are extreme values or outliers. In the example set, the value 36 lies
more than two standard deviations from the mean, so 36 is an outlier. Outliers may
represent erroneous data or may suggest unforeseen circumstances and should be
carefully considered when interpreting data.
Facilities : Windows/Linux Operating Systems, RStudio, jdk.
Application:
1. The histogram is suitable for visualizing distribution of numerical data over a
continuous interval, or a certain time period. The histogram organizes large amounts
of data, and produces a visualization quickly, using a single dimension.
2. The box plot allows quick graphical examination of one or more data sets. Box plots
may seem more primitive than a histogram but they do have some advantages. They take
up less space and are therefore particularly useful for comparing distributions between
several groups or sets of data. Choice of number and width of bins techniques can heavily
influence the appearance of a histogram, and choice of bandwidth can heavily influence
the appearance of a kernel density estimate.
3. Data Visualization Application lets you quickly create insightful data visualizations, in
minutes.
Data visualization tools allow anyone to organize and present information intuitively.
They enables users to share data visualizations with others.
Input :
Structured Dataset : Iris Dataset
File: iris.csv
Output :
1. Display Dataset Details.
2. Calculate Min, Max,Mean,Varience value and Percentiles of probabilities also Display
Specific use quantile.
3. Dispaly the Histogram using Hist Function.
Conclusion:
Hence, we have studied using dataset into a dataframe and compare distribution and
identify outliers.
Questions:
1. What is Data visualization?
2. How to calculate min,max,range and standard deviation?
3. How to create boxplot for each feature in the daset?
4. How to create histogram?
5. What is dataset?
Experiment No:
Download Pima Indians Diabetes dataset. Use Naive Bayes Algorithm for
classification
Load the data from CSV file and split it into training and test datasets.
summarize the properties in the training dataset so that we can calculate
probabilities and make predictions.
Classify samples from a test dataset and a summarized training dataset
Let us say P(Fire) means how often there is fire, and P(Smoke) means how
often we see smoke, then:
P(Fire|Smoke) means how often there is fire when we can see smoke
P(Smoke|Fire) means how often we can see smoke when there is fire
Example: If dangerous fires are rare (1%) but smoke is fairly common
(10%) due to barbecues, and 90% of dangerous fires make smoke then:
P(Fire|Smoke) =P(Fire) P(Smoke|Fire)P(Smoke)
=1% x 90%10%
=9%
Applications:
Real time Prediction: Naive Bayes is an eager learning classifier and it is sure fast.
Thus, it could be used for making predictions in real time.
Multi class Prediction: This algorithm is also well known for multi class prediction
feature. Here we can predict the probability of multiple classes of target variable.
Text classification/ Spam Filtering/ Sentiment Analysis: Naive Bayes classifiers
mostly used in text classification (due to better result in multi class problems and
independence rule) have higher success rate as compared to other algorithms. As a
result, it is widely used in Spam filtering (identify spam e-mail) and Sentiment
Analysis (in social media analysis, to identify positive and negative customer
sentiments)
Recommendation System: Naive Bayes Classifier and Collaborative
Department of Computer Engineering
ZES’s DCOER, Pune-411041 Page 73
Laboratory Practice-I BE (Comp)
Filtering together builds a Recommendation System that uses machine learning and
data mining techniques to filter unseen information and predict whether a user would
like a given resource or not
1.
Input:
Structured Dataset : PimaIndiansDiabetes Dataset
File: PimaIndiansDiabetes.csv
Output:
1. Splitted dataset according to Split ratio.
2. Conditional probability of each feature.
3. visualization of the performance of an algorithm with confusion matrix
Experiment No:
Title: Trip History Analysis: Use trip history dataset that is from a bike sharing
service in the United States. The data is provided quarter-wise from 2010 (Q4)
onwards. Each file has 7 columns. Predict the class of user. Sample Test data
set available here https://www.capitalbikeshare.com/trip-history-data
E:g:
The rpart function builds a model of recursive partitioning and regression
trees. The following code snippet shows how to use the rpart function to
The rpart function has four parameters. The first parameter, Play - Out look
+ Temperature+ Hurnidi ty + Wind, is the model indicating that attribute
Play can be predicted based on attributes Outlook, Temperature, Humidity,
and Wind. The second parameter, method, is set to "class," telling R it is
building a classification tree. The third parameter, data, specifies the
dataframe containing those attributes mentioned in the formula. The fourth
parameter, control, is optional and controls the tree growth. In the preceding
example, control=rpart. control (minsplit=l) requires that each node have at
least one observation before attempting a split. The rminsplit= 1 makes
sense for the small dataset, but for larger datasets rminsplit could be set to
10% of the dataset size to combat overfitting. Besides minsp 1 it, other
parameters are available to control the construction of the decision tree. For
example, rpart. control (rnaxdepth=10, cp=O. 001) limits the depth of the
tree to no more than 10, and a split must decrease the overall lack of fit by a
factor of 0.001 before being attempted. The last parameter (parms) specifies
the purity measure being used for the splits. The value of sp 1 it can be
either information (for using the information gain) or g ini (for using the
Gini index).
For the tripadvisory dataset the following code snippet for rpart is used:
fit<-rpart(train$Member.type~.,data=train, method="class")
Input:
Structured Dataset : capitalbikeshare Dataset
File: capitalbikeshare-tripdata.csv
Output:
1. Splitted dataset according to Split ratio.
2. Summary of every node in the constructed decision tree
3. To generate predictions from a fitted rpart object
Conclusion: Hence, we have studied classification based on rpart package and its usage.
Questions:
1. What are decision tree?
2. What is rpart?
3. What are applications of rpart?
4. Advantages of decision trees?
Experiment No:
Title: Twitter Data Analysis: Use Twitter data for sentiment analysis. The dataset is
3MB in size and has 31,962 tweets. Identify the tweets which are hate tweets
and which are not.
Aim: Implement the sentiment analysis of twitter data. Implement the following
operations:
1. Sentiment analysis of twitter dataset.
2. Classify the tweets as positive and negative tweets from dataset.
II.Python Tweepy library: This library provides access to the entire twitter
RESTful API methods. Each method can accept various parameters and
return responses.
Input:
Structured Dataset : Twitter Dataset
File: Twitter.csv
Output:
1. Sentiment analysis of twitter dataset.
2. Categorization of tweets as positive and negative tweets..