AI Report
AI Report
Artificial Intelligence
CSCL-4101
Semester 3rd
Name: _______________________________________________
List of Experiments
Instructor:
Prerequisites:
Objectives:
The objective of the course is to present an overview of artificial intelligence (AI) principles and
approaches. Develop a basic understanding of the building blocks of AI as presented in terms of
intelligent agents: Search, Knowledge representation, inference, logic, and learning
Contents:
Introduction to Python Programming, Integrated Development Environment (IDE), Google Colab
online laboratory, Jupyter notebook. Introduction to AI and Algorithms, Basic Programming in
Python: Data Types and Variables, Types of Operators and Operator
Precedence, Advance data types, List/tuple/dictionaries/slicing, Conditional statements (If-Else,
Nested if-else and if-elif-else ladder), Understanding of Control Structure: For Loop, While Loop),
Concept of Functions (built-in, user defined functions, Recursion), Concept of Arrays, Structures,
Familiarization with Python packages for AI: Pandas, Numpy, Sci-kit learn, Mat-plot library,
Machine Learning: Artificial Neural Network (Single Layer Perceptron, Multi Layer Perceptron,
KNN, K-mean clustering, Decision Tree, Regression, Naïve Bayes, SVM Apriori Algorithm ,
market basket analysis, Principal Component Analysis PCA. Optimization: Genetic Algorithm
GA, Particle Swarm Optimization PSO. Solution search strategies :Breath-first search, Depth first
search, Best First search, Beam search, Bi-directional search, Uniform cost search, Iterative
BT
CLO Description Domain
Level*
Understand key components in the field of artificial
1 C 2
intelligence
2 Implement classical artificial intelligence techniques C 3
Analyze artificial intelligence techniques for practical
3 C 4
problem solving
*BT=Bloom’s Taxonomy, C=Cognitive domain, P=Psychomotor domain, A=Affective domain
PLO DESCRIPTION
Ability to apply knowledge of mathematics, science, computing fundamentals andany of
a
its specializations to solve complex problems.
Ability to identify, formulate, research literature, and analyze complex problems reaching
b substantiated conclusions using basic principles of mathematics, natural sciences and
computer science and Artificial intelligence
Ability to design solutions for complex problems and design software systems,
c components or processes that meet specified needs with appropriate considerationfor
public health and safety, cultural, societal, and environmental considerations.
Experiment Performance
Performan
ce between Performan
Below ce between
Below Satisfactor
m Average Satisfactor Excellent
Average y
and y and
Satisfactor Excellent
y
(50% of (75% of
(0% of m) (25% of m) (100% of m)
m) m)
Equipment Improper Casual Proper
4
Handling handling handling handling
Procedur
e of
Get correct
taking
Not able to readings
Measureme readings
6 get through
nts is proper
readings proper
but the
procedure
readings
are
incorrect
Does not Knows
know about Knows
about basic basic about basic
theory theory theory
regarding regarding regarding
Viva 10
experiment experime experiment
Does not nt Replies
completely Does not sufficient
reply all reply all questions
questions questions
Total Marks 20
Marks
Obtained
Performan
Performan
ce between
ce
Below
Below Satisfactor between
m Average Excellent
Average y Satisfacto
and
ry and
Satisfactor
Excellent
y
(50% of (75% of
(0% of m) (25% of m) (100% of m)
m) m)
Knows
about
Does not basic
Knows
know about theory
about basic
basic theory regardin
theory
regarding g
regarding
respective respectiv
respective
course (and e course
Viva 15 course (and
project, if (and
project, if
applicable) project,
applicable)
Does not if
Replies
completely applicabl
sufficient
reply all e)
questions
questions Does not
reply all
questions
Total
15
Marks
Marks
Obtained
d
Report Viva Heads
Total
on skills Presentation
Marks
Marks
Obtaine
Marks
3
6
3
3
15
(m)
Performanc
e below
Poor
Similarity Does not know Listen but Bad packaging (0% of m)
index >19% about basic do not Unacceptable Poor
Do not contain theory regarding understand Hardware (No use (20% of m)
theoretical project along with of aesthetic sense,
background Does not non- if applicable)
Diagrams are completely reply effective Hardware not
all hardware and reply Performanc
copy-paste working
theory related e between
questions Poor and
(SZABIST)
Satisfactory
(40% of m)
Similarity Knows about Listen and Average
Artificial Intelligence BS (AI)
Plot 67 Street 9, H-8/4 Karachi Campus: 99 3rd Ave, Block 5 Clifton Karachi City, Pakistan
background hardware and aesthetic sense, if
Diagrams are theory related applicable)
in good order. questions Hardware
working
Artificial Intelligence BS (AI)
Shaheed Zulfikar Ali Bhutto Institute of Science and Technology
(SZABIST)
Marks Evaluation
Marks
Experiment No. Class Experiment Experiment
Total
Participation Performance Reporting
(1)
(0.3) (0.5) (0.2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Total
Instructor’s signature
Performance Performance
Performance between Poor between
Marks Poor Satisfactory Excellent
below Poor and Satisfactory
Heads
Satisfactory and Excellent
Understanding
6
of code
Communication
3
skills
Viva 3
Report 3
Total Marks 15
Marks
Obtained
Always pay attention to what you are doing and you’re surrounding during the experiments and
notify the Instructor for any unlikely event or mishap and leave the Laboratory with the permission
of Instructor immediately.
All students must read and understand the information in this document with regard to laboratory
safety and emergency procedures prior to the first laboratory session.
Your personal laboratory safety depends mostly on YOU. Effort has been made to address
situations that may pose a hazard in the lab but the information and instructions provided cannot
be considered all-inclusive.
Students must adhere to written and verbal safety instructions throughout the academic term. Since
additional instructions may be given at the beginning of laboratory sessions, it is important that all
Objective:
To discuss and elaborate AI and algorithms, python programming fundamentals and get
familiarization with Integrated Development Environment (IDE), Google Colab online laboratory,
Jupyter notebook.
Software and Tools:
● 1.6 GHz or faster processor
● 4 GB of RAM
● 20 GB of available hard disk space
● Windows 7 or later
● Spyder (Python 3.9)
● Google Colab
Examples of AI Applications
• Siri, Alexa and other smart assistants
• Self-driving cars (Driver-less Cars)
• Robo-advisors
• Conversational bots (Chat Bots)
• Email spam filters
• Netflix's recommendations (Recommendation system)
1. Narrow AI:
Sometimes referred to as "Weak AI," this kind of artificial intelligence operates within a limited
context and is a simulation of human intelligence. Narrow AI is often focused on performing a
single task extremely well and while these machines may seem intelligent, they are operating under
far more constraints and limitations than even the most basic human intelligence.
Example:
• Google search
• Image recognition software
• Siri, Alexa and other personal assistants
• Self-driving cars
• IBM's Watson
Reactive Machines
A reactive machine follows the most basic of AI principles and, as its name implies, is capable of
only using its intelligence to perceive and react to the world in front of it. A reactive machine
cannot store a memory and as a result cannot rely on past experiences to inform decision making
in real-time. Perceiving the world directly means that reactive machines are designed to complete
only a limited number of specialized duties.
A famous example of a reactive machine is Deep Blue, which was designed by IBM in the 1990‘s
as a chess-playing supercomputer and defeated international grandmaster Gary Kasparov in a
game.
Another example of a game-playing reactive machine is Google‘s AlphaGo. AlphaGo is also
incapable of evaluating future moves but relies on its own neural network to evaluate developments
of the present game, giving it an edge over Deep Blue in a more complex game. AlphaGo also
bested world-class competitors of the game, defeating champion Go player Lee Sedol in 2016.
There are three major machine learning models that utilize limited memory artificial intelligence:
Long Short Term Memory (LSTM) - Which utilizes past data to help predict the
next item in a sequence. LTSMs view more recent information as most important when
making predictions and discounts data from further in the past, though still utilizing it to
form conclusions.
Theory of Mind:
Theory of Mind is just that theoretical. We have not yet achieved the technological and scientific
capabilities necessary to reach this next level of artificial intelligence.
The concept is based on the psychological premise of understanding that other living things have
thoughts and emotions that affect the behavior of one‘s self. In terms of AI machines, this would
mean that AI could comprehend how humans, animals and other machines feel and make decisions
through self-reflection and determination, and then will utilize that information to make decisions
of their own. Essentially, machines would have to be able to grasp and process the concept
of ―mind,‖ the fluctuations of emotions in decision making and a litany of other psychological
concepts in real time, creating a two-way relationship between people and artificial intelligence.
Self-Awareness:
Introduction to Python
Python is an open source and cross-platform programming language that has become increasingly
popular over the last ten years. It was released in 1991. It is an interpreted, objectoriented, high-
level programming language with dynamic semantics developed by Guido van Rossum. Latest
version is 3.7.0. Python is a multi-purpose programming languages (due to its many extensions).
Python is an interpreted programming language, this means that as a developer you write Python
(.py) files in a text editor and then put those files into the python interpreter to be executed.
Depending on the Editor you are using, this is either done automatically, or you need to do it
manually.
Examples: scientific computing and calculations, simulations, web development (Django Web
framework), etc.
Introduction to Anaconda:
Anaconda offers the easiest way to perform Python/R data science and machine learning on a
single machine. You can work with thousands of open-source packages and libraries with it.
Anaconda is a distribution package, where you get Python compiler, Python packages and the
Spyder editor. Anaconda includes Python, Jupyter Notebook, and other commonly used packages
for scientific computing and data science.
Jupyter Notebook:
The Jupyter Notebook is an open-source web application that allows you to create and share
documents that contain live code, equations, visualizations and text.
In the next window, select the user variable named Path and click Edit. . . to change its
value. Select "New" and add the path where "python.exe" is located. See Figure 3.6.
The Default Location is:
Click Save and open the Command Prompt once more and enter "python" to verify it works.
Get Help:
An extremely useful command is help(), which enters a help functionality to explore all the
students python lets you do, right from the interpreter. Press q to close the help window and
return to the Python prompt.
Google Colab
Colaboratory, or ―Colab‖ for short, is a product from Google Research. Colab allows
anybody to write and execute arbitrary python code through the browser, and is especially
well suited to machine learning, data analysis and education. More technically, Colab is a
hosted Jupyter notebook service that requires no setup to use, while providing access free
of charge to computing resources including GPUs.
Colab Interface
Resources in Colab are prioritized for interactive use cases. We prohibit actions associated
with bulk compute, actions that negatively impact others, as well as actions associated with
bypassing our policies. The following are disallowed from Colab runtimes:
• file hosting, media serving, or other web service offerings not related to
interactive compute with Colab
• downloading torrents or engaging in peer-to-peer file-sharing
• using a remote desktop or SSH
• connecting to remote proxies
• mining cryptocurrency
• running denial-of-service attacks
• password cracking
• using multiple accounts to work around access or resource usage restrictions
creating deep fakes.
Objective:
To perform the basic python programming using Data Types and Variables, Types of
Operators and Operator Precedence, Advance data types, List/tuple/dictionaries/slicing.
Python Strings
Strings in Python are identified as a contiguous set of characters represented in the quotation
marks. Python allows either pair of single or double quotes. Subsets of strings can be taken
using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string
and working their way from -1 to the end. The plus (+) sign is the string concatenation
operator and the asterisk (*) is the repetition operator.
String Input:
Python allows for command line input. That means we are able to ask the user for input.
The following example asks for the user's name, then, by using the input() method, the
program prints the name to the screen:
Python Lists
Lists are the most versatile of Python's compound data types. A list contains items separated
by commas and enclosed within square brackets ([]). To some extent, lists are similar to
arrays in C. One of the differences between them is that all the items belonging to a list can
be of different data type.
The values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes
starting at 0 in the beginning of the list and working their way to end -1. The plus (+) sign
is the list concatenation operator, and the asterisk (*) is the repetition operator.
Python Tuples
A tuple is another sequence data type that is similar to the list. A tuple consists of a number
of values separated by commas. Unlike lists, however, tuples are enclosed within
parenthesis. The main difference between lists and tuples is- Lists are enclosed in brackets
( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses
( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists.
The following code is invalid with tuple, because we attempted to update a tuple,
which is not allowed. Similar case is possible with lists: tuple = ( 'abcd', 786 , 2.23,
'john', 70.2 ) list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] tuple[2] = 1000 # Invalid syntax
with tuple list[2] = 1000 # Valid syntax with list
Python Dictionary
Python's dictionaries are kind of hash-table type. They work like associative arrays or hashes
found in Perl and consist of key-value pairs. A dictionary key can be almost any
Python type, but are usually numbers or strings. Values, on the other hand, can be any
arbitrary Python object.
Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed
using square braces ([]).
Dictionaries have no concept of order among the elements. It is incorrect to say that the
elements are "out of order"; they are simply unordered.
We can generate a sequence of numbers using range() function. range(10) will generate
numbers from 0 to 9 (10 numbers).
We can also define the start, stop and step size as range(start, stop,step_size). step_size
defaults to 1 if not provided.
This function does not store all the values in memory; it would be inefficient. So it
remembers the start, stop, step size and generates the next number on the go.
A slice object is used to specify how to slice a sequence. You can specify where to
start the slicing, and where to end. You can also specify the step, which allows you to
e.g. slice only every other item. Following is the syntax:
A sequence of objects of any type (string, bytes, tuple, list or range) or the object which
implements __getitem__() and __len__() method then this object can be sliced using slice()
method.
Python Variables
Variables are nothing but reserved memory locations to store values. It means that when
you create a variable, you reserve some space in the memory.
Based on the data type of a variable, the interpreter allocates memory and decides what can
be stored in the reserved memory. Therefore, by assigning different data types to the
variables, you can store integers, floats or characters in these variables.
Variables are defined with the assignment operator, \=". Python is dynamically typed,
meaning that variables can be assigned without declaring their type, and that their type can
change. Values can come from constants, from computation involving values of other
variables, or from the output of a function.
Basic rules for Python variables:
• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters (A-z, 0-9) and
underscores
• Variable names are case-sensitive, e.g., amount, Amount and AMOUNT are
three different variables.
Multiple Assignments
For
exampl
ea=b
=c=1
Here, an integer object is created with the value 1, and all the three variables are assigned to
the same memory location. You can also assign multiple objects to multiple variables.
For example
a, b, c = 1, 2, "john"
Here, two integer objects with values 1 and 2 are assigned to the variables a and b
respectively, and one string object with the value "john" is assigned to the variable c.
Types of Operators
Python language supports the following types of operators:
• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators, Bitwise Operators
• Membership Operators
• Identity Operators
Example Program:
Assume variable a holds 50 and variable b holds 23:
Example Program:
Example Program:
Objective:
To perform python programming using Conditional statements (If-Else, Nested if-else and
ifelif-else ladder), Understanding of Control Structure: For Loop, While Loop), Concept of
Functions (built-in, user defined functions, Recursion), Concept of Arrays, Structures.
Conditional Statements
The „if‟ Statement
The ability to control the flow of your program, letting it make decisions on what code to execute,
is valuable to the programmer. The ―if statement‖ allows you to control if a program enters a
section of code or not based on whether a given condition is true or false. Decision-making is the
anticipation of conditions occurring during the execution of a program and specified actions taken
according to the conditions.
Decision structures evaluate multiple expressions, which produce TRUE or FALSE as the
outcome. You need to determine which action to take and which statements to execute if the
outcome is TRUE or FALSE otherwise.
The if statement contains a logical expression using which the data is compared and a decision is
made based on the result of the comparison,
If the Boolean expression evaluates to TRUE, then the block of statement(s) inside the if statement
is executed. In Python, statements in a block are uniformly indented after the : symbol. If Boolean
expression evaluates to FALSE, then the first set of code after the end of block is executed.
Syntax:
if (CONDITION):
Execute the next statement
Program
Syntax:
if
expression
:
statement(
s) else:
statement(
s)
Program
Syntax:
Program
In the above example, discount is calculated on the input amount. Rate of discount is 5%, if the
amount is less than 1000, and 10% if it is above 5000 and else takes care of all values by 15% rate
of discount.
Syntax:
Program
Syntax
if (condition1):
# Executes when condition1 is
true if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here
statement
(s) else
statement
(s) elif
expressio
n4:
statement
(s) else:
statement
(s)
Loop
In general, statements are executed sequentially: The first statement is executed first, followed by
the second, and so on. There may be a situation when you need to execute a block of code several
number of times. A loop statement allows us to execute a statement or group of statements multiple
times. The following diagram illustrates a loop statement.
For Loop
In Python, for keyword provides a more comprehensive mechanism to constitute a loop. The for
loop is used with sequence types such as list, tuple, set, range, etc. The body of the for loop is
executed for each member element in the sequence. Syntax for x in sequence:
statement1
statement2
...
statementN
While Loop
In most computer programming languages, a while loop is a control flow statement that allows
code to be executed repeatedly based on a given Boolean condition. The while loop can be thought
of as a repeating if statement.
The ‗for‘ loop does something a fixed number of times. But in a case where you don‘t know how
many times you want to do something before you start the loop, you use a different loop: the while
loop. Here, statement(s) may be a single statement or a block of statements. The condition may be
any expression, and true is any non-zero value. The loop iterates while the condition is true. When
the condition becomes false, program control passes to the line immediately following the loop.
In Python, all the statements indented by the same number of character spaces after a programming
construct are considered to be part of a single block of code. Python uses indentation as its method
of grouping statements
Syntax
while
expression
:
statement(
s)
Concept of Functions
A function can be called as a section of a program that is written once and can be executed
whenever required in the program, thus making code reusability.
• Keyword def that marks the start of the function header.
• A function name to uniquely identify the function. Function naming follows the
same rules of writing identifiers in Python.
• Parameters (arguments) through which we pass values to a function. They are
optional.
• A colon (:) to mark the end of the function header.
• Optional documentation string (docstring) to describe what the function does.
• One or more valid python statements that make up the function body. Statements
must have the same indentation level (usually 4 spaces).
• An optional return statement to return a value from the function.
• Once we have defined a function, we can call it from another function, program, or
even the Python prompt. To call a function we simply type the function name with
appropriate parameters.
Concept of Arrays
An array is a collection of items stored at contiguous memory locations. The idea is to store
multiple items of the same type together. This makes it easier to calculate the position of each
element by simply adding an offset to a base value, i.e., the memory location of the first element
of the array (generally denoted by the name of the array)
Array in Python can be created by importing array module. array(data_type, value_list) is used to
create an array with data type and value list specified in its arguments.
Objective:
To perform the basic machine learning artificial neural network (Single Layer Perceptron)
algorithm and determine the outcome with familiarization with python packages Pandas, Numpy,
Sci-kit learn, and Mat-plot library.
NumPy:
NumPy is the fundamental package for scientific computing with Python SciPy:
SciPy is a free and open-source Python library used for scientific computing and technical
computing. SciPy contains modules for optimization, linear algebra, integration, interpolation,
special functions, FFT, signal and image processing, ODE solvers and other tasks common in
science and engineering.
Matplotlib:
Matplotlib is a Python 2D plotting library.
Pandas:
Pandas Python Data Analysis Library
Built-in Functions:
Python consists of lots of built-in functions. Some examples are the print (function that we already
have used (perhaps without noticing it is actually a Built-in function).
Python also consists of different Modules, Libraries or Packages. These Modules, Libraries or
Packages consists of lots of predefined functions for different topics or areas, such as mathematics,
plotting, handling database systems, etc.
More functions we will explore later
Plotting in Python:
Typically you need to create some plots or charts. In order to make plots or charts in
Python you will need an external library. The most used library is Matplotlib. Matplotlib
is a Python 2D plotting library.
Subplots:
The subplot command enables you to display multiple plots in the same window.
Typing "subplot(m,n,p)" partitions the figure window into an m-by-n matrix of small
subplots and selects the subplot for the current plot. The plots are numbered along the first
row of the figure window, then the second row, and soon.
Single-Layer Perceptron
a) Round 1
Σ = x1 * w1 + x2 * w2 = 0 * 0.9 + 0 * 0.9 = 0
Activation unit checks sum unit is greater than a threshold. If this rule is satisfied, then it is
fired and the unit will return 1, otherwise it will return 0. BTW, modern neural networks
architectures do not use this kind of a step function as activation.
Sum unit was 0 for the 1st instance. So, activation unit would return 0 because it is less than
0.5. Similarly, its output should be 0 as well. We will not update weights because there is
no error in this case.
Errors:
Activation unit will return 1 because sum unit is greater than 0.5. However, output of this
instance should be 0. This instance is not predicted correctly. That‘s why, we will update
weights based on the error.
ε = actual – prediction = 0 – 1 = -1
We will add error times learning rate value to the weights. Learning rate would be 0.5. BTW,
we mostly set learning rate value between 0 and 1.
Activation unit will return 0 this time because output of the sum unit is 0.5 and it is less than 0.5.
We will not update weights.
Activation unit will return 1 because output of the sum unit is 0.8 and it is greater than the threshold
value 0.5. Its actual value should 1 as well. This means that 4th instance is predicted correctly. We
will not update anything.
b) Round 2
In previous round, we‘ve used previous weight values for the 1st instance and it was classified
correctly. Let‘s apply feed forward for the new weight values.
Activation unit will return 0 because sum unit is 0.4 and it is less than the threshold value 0.5. The
output of the 1st instance should be 0 as well. This means that the instance is classified correctly.
We will not update weights.
Activation unit will return 0 because sum unit is less than the threshold 0.5. Its output should be 0
as well. This means that it is classified correctly and we will not update weights.
We‘ve applied feed forward calculation for 3rd and 4th instances already for the current weight
values in the previous round. They were classified correctly.
Learning Term:
We should continue this procedure until learning completed. We can terminate the learning
procedure here. Luckily, we can find the best weights in 2 rounds.
Updating weights means learning in the perceptron. We set weights to 0.9 initially but it
causes some errors. Then, we update the weight values to 0.4. In this way, we can predict
all instances correctly.
Experiment
Implementation of Single Layer Perceptron
We can create perceptron that act like gates: they take 2 binary inputs and produce a single
binary output.
1. Defining inputs size, the learning rate and number of epochs as inputs and
weights vector for the network. One is added to the input size to include the bias in
the weight vector.
2. Implementing activation function which returns 1 if the input is greater than
or equal to 0 and 0 otherwise.
3. Creating prediction function to run an input through the perceptron and
return an output. The bias is added into the input vector then we can simply compute
the inner product and apply the activation function.
4. Perceptron Learning Algorithm Code: Create a function to keep applying this
update rule until our perceptron can correctly classify all of our inputs. We need to
keep iterating through our training data until this happens; one epoch is when our
perceptron has seen all of the training data once. Usually, we run our learning
algorithm for multiple epochs. We can create a function, given inputs and desired
outputs, run our perceptron learning algorithm. We keep updating the weights for a
Basic perceptron can generalize any kind of linear problem. The both AND and OR Gate problems
are linearly separable problems. On the other hand, this form cannot generalize nonlinear problems
such as XOR Gate. Perceptron evolved to multilayer perceptron to solve nonlinear problems and
deep neural networks were born.
Lab Exercises:
1. To reinforce the perceptron, apply learning procedure for OR Gate. The gate returns 0 if
and only if both inputs are 0. Do not hesitate to change the initial weights and learning rate
values.
2. Can this form generalized the same algorithm for nonlinear problem XOR gate , why
or why not?
________________________________________________________________________
___
________________________________________________________________________
___
________________________________________________________________________
___
______________________________________________________________________
_____ _______________________________________________________________
Conclusion:
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
Objective:
To perform the basic machine learning artificial neural network (Multi Layer Perceptron)
algorithm and determine the outcome with familiarization with python packages Pandas,
Numpy, Sci-kit learn, and Mat-plot library.
Introduction:
In this implementation of Multi-Layer Perceptron neural network, a single hidden layer
neural network is used, with sigmoid activation function in hidden layer units and sigmoid
activation function for output layer too, since the output of XOR logic is binary i.e. 0 or 1
only one neuron is in the output layer. This time we are not adding bias with inputs as SLP.
However, the algorithm will run fine without adding bias but can‘t without weight updates.
The mathematics and basic logic behind the neural network is also explained.
[Hints]
X = inputs
Y= desired output
No_x = number of inputs
No_y = number of neurons in output layer
No_h = Number of neurons in hidden layer
Tot = Total training examples
Lr = learning rate
W1, w2 = hidden layers weights
Losses = error or loss calculation
frwd_prop = defining forward propagation
method sigmoid = defining sigmoid method
z = pre-activation /weights vector sum
z1,a1,z2,a2 = weight updates and activation variables (forward propagation)
dz2,dw2,dz1,dw1 = derivative of weights and weights updates (w.r.t. weight vector and
activation function)
mlp_test = testing multilayer perceptron learning and network prediction
Rewrite the Coding as Define Below and Place Output Screen shot:
Lab Exercises:
1. To reinforce the perceptron, apply learning procedure for XOR Gate. The gate returns 0
if and only if both inputs are 0. Do not hesitate to change the initial weights and learning
rate values.
3. Can this form generalized the same algorithm for nonlinear problem XNOR gate , why
or why not?
________________________________________________________________________
___
________________________________________________________________________
___
________________________________________________________________________
___
______________________________________________________________________
_____ _______________________________________________________________
Conclusion:
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
K-Nearest Neighbor
Implementation of KNN Algorithm using IRIS dataset About IRIS Data Set
This dataset contains 3 classes of 50 instances each, which refers to a type of iris plant.
Attribute Information:
1. Sepal length in cm
2. Sepal width in cm
3. Petal length in cm
4. Petal width in cm
Lab Exercises:
4. Can this learning generalized the same algorithm for unsupervised problem, why or
why not?
________________________________________________________________________
___
________________________________________________________________________
___
________________________________________________________________________
___
______________________________________________________________________
_____ _______________________________________________________________
Conclusion:
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
Statistics and data science are often concerned about the relationships between two or more
variables (or features) of a dataset. Each data point in the dataset is an observation, and the
features are the properties or attributes of those observations.
Regression
Regression searches for relationships among variables. Linear regression is used to identify
the relationship between a dependent variable and one or more independent variables and is
typically leveraged to make predictions about future outcomes.
When there is only one independent variable and one dependent variable, it is known as
simple linear regression. As the number of independent variables increases, it is referred to
as multiple linear regression. For each type of linear regression, it seeks to plot a line of best
fit, which is calculated through the method of least squares.
However, unlike other regression models, this line is straight when plotted on a graph.
Linear regression is leveraged when dependent variables are continuous, whereas logistical
regression is selected when the dependent variable is categorical, meaning they have binary
outputs, such as
Lab Exercises:
2. Implement linear regression technique for a sample dataset for house price
prediction.
Conclusion:
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
Clustering
Clustering is a set of techniques used to partition data into groups, or clusters. Clusters are loosely
defined as groups of data objects that are more similar to other objects in their cluster than they
are to data objects in other clusters. In practice, clustering helps identify two qualities of data:
• Meaningfulness
• Usefulness
Meaningful clusters expand domain knowledge. For example, in the medical field, researchers
applied clustering to gene expression experiments. The clustering results identified groups of
patients who respond differently to medical treatments.
Useful clusters, on the other hand, serve as an intermediate step in a data pipeline. For example,
businesses use clustering for customer segmentation. The clustering results segment customers
into groups with similar purchase histories, which businesses can then use to create targeted
advertising campaigns.
When we are working with huge volumes of data, it makes sense to partition the data into logical
groups and doing the analysis. We can use Clustering to make the data into groups with the help
of several algorithms like K-Means.
Lab Exercises:
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
Decision Tree
A Decision Tree is just a Flow Chart, and can help you make decisions based on previous
experience.
Lab Exercises:
Conclusion:
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Naïve Bayes
It is a classification technique based on Bayes‘ Theorem with an assumption of independence
among predictors. In simple terms, a Naive Bayes classifier assumes that the presence of a
particular feature in a class is unrelated to the presence of any other feature.
For example, a fruit may be considered to be an apple if it is red, round, and about 3 inches in
diameter. Even if these features depend on each other or upon the existence of the other features,
all of these properties independently contribute to the probability that this fruit is an apple and that
is why it is known as ‗Naive‘.
Naive Bayes model is easy to build and particularly useful for very large data sets. Along with
simplicity, Naive Bayes is known to outperform even highly sophisticated classification methods.
Bayes theorem provides a way of calculating posterior probability P(c|x) from P(c), P(x) and
P(x|c). Look at the equation below:
Above,
P(c|x) is the posterior probability of class (c, target) given predictor (x, attributes).
P(c) is the prior probability of class.
P(x|c) is the likelihood which is the probability of predictor given class. P(x) is the prior probability
of predictor.
Conclusion:
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
Objective:
Learn about SVM and SVC
Learn about how SVM can be used with PCA
MINIST using SVM
Introduction:
Support Vector Machine (SVM) is a supervised machine learning algorithm used for both
classification and regression. Though we say regression problems as well it’s best suited for
classification. The main objective of the SVM algorithm is to find the optimal hyperplane in an N-
dimensional space that can separate the data points in different classes in the feature space. The
hyperplane tries that the margin between the closest points of different classes should be as
maximum as possible. The dimension of the hyperplane depends upon the number of features. If
the number of input features is two, then the hyperplane is just a line. If the number of input
features is three, then the hyperplane becomes a 2-D plane. It becomes difficult to imagine when
the number of features exceeds three.
Let’s consider two independent variables x1, x2, and one dependent variable which is either a blue
circle or a red circle.
One reasonable choice as the best hyperplane is the one that represents the largest separation or
margin between the two classes.
So we choose the hyperplane whose distance from it to the nearest data point on each side is
maximized. If such a hyperplane exists it is known as the maximum-margin hyperplane/hard
margin. So from the above figure, we choose L2. Let’s consider a scenario like shown below
So in this type of data point what SVM does is, finds the maximum margin as done with previous
data sets along with that it adds a penalty each time a point crosses the margin. So the margins in
these types of cases are called soft margins. When there is a soft margin to the data set, the SVM
tries to minimize (1/margin+∧(∑penalty)). Hinge loss is a commonly used penalty. If no violations
no hinge loss.If violations hinge loss proportional to the distance of violation. Till now, we were
talking about linearly separable data(the group of blue balls and red balls are separable by a straight
line/linear line). What to do if data are not linearly separable?
In this case, the new variable y is created as a function of distance from the origin. A non-linear
function that creates a new variable is referred to as a kernel.
In this lab, we'll use the SVC module from the sklearn.svm
The SVC() function can be used to fit a support vector classifier when the argument
kernel="linear" is used. This function uses a slightly different formulation of the equations we
saw in lecture to build the support vector classifier. The c argument allows us to specify the cost
of a violation to the margin. When the c argument is small, then the margins will be wide and
many support vectors will be on the margin or will violate the margin. When the c argument is
large, then the margins will be narrow and there will be few support vectors on the margin or
violating the margin. We can use the SVC() function to fit the support vector classifier for a given
value of the cost parameter. Here we demonstrate the use of this function on a two-dimensional
Let's plot the data to see whether the classes are linearly separable:
We can now plot the support vector classifier by calling the plot_svc()
function on the output of the call to SVC(), as well as the data used in the call to SVC()
plot_svc(svc, X, y)
The region of feature space that will be assigned to the −1 class is shown in light blue, and the
region that will be assigned to the +1 class is shown in brown. The decision boundary between the
two classes is linear (because we used the argument kernel="linear"
).
The support vectors are plotted with crosses and the remaining observations are plotted as circles;
we see here that there are 13 support vectors. We can determine their identities as follows:
svc.support_
Now that a smaller value of the c parameter is being used, we obtain a larger number of support
vectors, because the margin is now wider.
We can easily access the cross-validation errors for each of these models:
clf.cv_results_
The GridSearchCV() function stores the best parameters obtained, which can be accessed as
follows:
clf.best_params_
As usual, the predict() function can be used to predict the class label on a set of test observations,
at any given value of the cost parameter. Let's generate a test data set:
np.random.seed(1)
X_test = np.random.randn(20,2)
y_test = np.random.choice([-1,1], 20)
X_test[y_test == 1] = X_test[y_test == 1]-1
Now we predict the class labels of these test observations. Here we use the best model obtained
through cross-validation in order to make predictions:
Now consider a situation in which the two classes are linearly separable. Then we can find a
separating hyperplane using the svm() function. First we'll give our simulated data a little nudge
so that they are linearly separable:
X_test[y_test == 1] = X_test[y_test == 1] -1
plt.scatter(X_test[:,0], X_test[:,1], s=70, c=y_test, cmap=mpl.cm.Paired)
plt.xlabel('X1')
plt.ylabel('X2')
Now the observations are just barely linearly separable. We fit the support vector classifier and
plot the resulting hyperplane, using a very large value of cost so that no observations are
misclassified.
No training errors were made and only three support vectors were used. However, we can see from
the figure that the margin is very narrow (because the observations that are not support vectors,
indicated as circles, are very close to the decision boundary). It seems likely that this model will
perform poorly on test data. Let's try a smaller value of cost
Using cost=1
, we misclassify a training observation, but we also obtain a much wider margin and make use of
five support vectors. It seems likely that this model will perform better on test data than the model
with cost=1e5.
In order to fit an SVM using a non-linear kernel, we once again use the SVC()
function. However, now we use a different value of the parameter kernel. To fit an SVM with a
polynomial kernel we use kernel="poly", and to fit an SVM with a radial kernel we use
np.random.seed(8)
X = np.random.randn(200,2)
X[:100] = X[:100] +2
X[101:150] = X[101:150] -2
y = np.concatenate([np.repeat(-1, 150), np.repeat(1,50)])
See how one class is kind of stuck in the middle of another class? This suggests that we might
want to use a radial kernel in our SVM. Now let's fit the training data using the SVC()
Not too shabby! The plot shows that the resulting SVM has a decidedly non-linear boundary. We
can see from the figure that there are a fair number of training errors in this SVM fit. If we increase
the value of cost, we can reduce the number of training errors:
However, this comes at the price of a more irregular decision boundary that seems to be at risk of
overfitting the data. We can perform cross-validation using GridSearchCV()
and gamma=0.5. We can plot the resulting fit using the plot_svc() function, and view the test
set predictions for this model by applying the predict()
85% of test observations are correctly classified by this SVM. Not bad!
3. ROC Curves
The auc()
package can be used to produce ROC curves such as those we saw in lecture:
Let's start by fitting two models, one more flexible than the other:
SVMs and support vector classifiers output class labels for each observation. However, it is also
possible to obtain fitted values for each observation, which are the numerical scores used to obtain
For an SVM with a non-linear kernel, the equation that yields the fitted value is given in (9.23) on
p. 352 of the ISLR book. In essence, the sign of the fitted value determines on which side of the
decision boundary the observation lies. Therefore, the relationship between the fitted value and the
class prediction for a given observation is simple: if the fitted value exceeds zero then the
observation is assigned to one class, and if it is less than zero than it is assigned to the other.
In order to obtain the fitted values for a given SVM model fit, we use the .decision_function()
y_train_score3 = svm3.decision_function(X_train)
y_train_score4 = svm4.decision_function(X_train)
Now we can produce the ROC plot to see how the models perform on both the training and the
test data:
y_train_score3 = svm3.decision_function(X_train)
y_train_score4 = svm4.decision_function(X_train)
y_test_score3 = svm3.decision_function(X_test)
y_test_score4 = svm4.decision_function(X_test)
for ax in fig.axes:
ax.plot([0, 1], [0, 1], 'k--')
ax.set_xlim([-0.05, 1.0])
ax.set_ylim([0.0, 1.05])
ax.set_xlabel('False Positive Rate')
ax.set_ylabel('True Positive Rate')
ax.legend(loc="lower right")
4 SVM with Multiple Classes
If the response is a factor containing more than two levels, then the svm()
function will perform multi-class classification using the one-versus-one approach. We explore
that setting here by generating a third class of observations:
np.random.seed(8)
XX = np.vstack([X, np.random.randn(50,2)])
yy = np.hstack([y, np.repeat(0,50)])
XX[yy ==0] = XX[yy == 0] +4
Fitting an SVM to multiclass data uses identical syntax to fitting a simple two-class model:
We now examine Optical Recognition of Handwritten Digits Data Set, which contains
5,620 samples of handwritten digits 0..9. You can use these links to download the training data
and test data, and then we'll load them into python:
print(X_train.shape)
print(X_test.shape)
This data set consists of preprocessed images of handwriting samples gathered from 43 different
people. Each image was converted into an 8x8 matrix (64 pixels), which was then flattened into a
vector of 64 numeric values. The final column contains the class label for each digit.
The training and test sets consist of 3,823 and 1,797 observations respectively. Let's see what one
of these digits looks like:
plt.imshow(X_train.values[1].reshape(8,8), cmap="gray")
plt.show()
y_train[0]
Phew, looks like our SVM has its work cut out for it! Let's start with a linear kernel to see how we
do:
svc = SVC(kernel='linear')
svc.fit(X_train, y_train)
We see that there are no training errors. In fact, this is not surprising, because the large number
of variables relative to the number of observations implies that it is easy to find hyperplanes that
fully separate the classes. We are most interested not in the support vector classifier’s performance
on the training observations, but rather its performance on the test observations:
cm = confusion_matrix(y_test, svc.predict(X_test))
print(pd.DataFrame(cm.T, index=svc.classes_, columns=svc.classes_))
We see that using cost = 10 yields just 71 test set errors on this data. Now try using the
GridSearchCV() function to select an optimal value for c.
Consider values in the range 0.01 to 100:
Solution:
• The Apriori algorithm is a well-known Machine Learning algorithm used for association
rule learning.
• Association rule learning is taking a dataset and finding relationships between items in
the data. For example, if you have a dataset of grocery store items, you could use
association rule learning to find items that are often purchased together.
• The Apriori algorithm is used on frequent item sets to generate association rules and is
designed to work on the databases containing transactions.
• The process of generating association rules is called association rule mining or association
rule learning. We can use these association rules to measure how strongly or weakly two
objects from the dataset are related.
• Frequent itemsets are those whose support value exceeds the user-specified minimum
support value.
The most common problems that this algorithm helps to solve are:
• Product recommendation
• Market basket recommendation
Confidence
Measures how often items in Y appear in transactions that contain X
Lift
Lift describes how much confident we are if B will be purchased too when the customer buys A:
Example:
Let’s imagine we have a history of 3000 customers’ transactions in our database, and we have to
calculate the Support, Confidence, and Lift to figure out how likely the customers who buy
Biscuits will buy Chocolate.
the confidence value shows the probability that customers buy Chocolate if they buy Biscuits
To calculate this value, we need to divide the number of transactions that contain Biscuits and
Chocolates by the total number of transactions having Biscuits:
the Lift value shows the potential increase in the ratio of the sale of Chocolates when you sell
Biscuits. The larger the value of the lift, the better:
Example:
First, the algorithm will create a table containing each item set’s support count in the given
dataset – the Candidate set
Let’s assume that we’ve set the minimum support value to 3, meaning the algorithm will drop
all the items with a support value of less than three.
The algorithm will take out all the itemsets with a greater support count than the minimum
support (frequent itemset) in the next step:
Next, the algorithm will generate the second candidate set (C2) with the help of the frequent
itemset (L1) from the previous calculation. The candidate set 2 (C2) will be formed by creating
the pairs of itemsets of L1. After creating new subsets, the algorithm will again find the support
count from the main transaction table of datasets by calculating how often these pairs have
occurred together in the given dataset.
After that, the algorithm will compare the C2’s support count values with the minimum support
count (3), and the itemset with less support count will be eliminated from table C2.
Sample Output:
Step-2: Using Apriori algorithm, generate frequent itemsets with min_support=0.01 (1%)
Code:
Sample Output:
Step-3: Add a column ‘length’ and store the length of each frequent itemset
Code:
Sample Output:
Sample Output:
Step-5: Generate Association rules for the frequent item sets of step-4 with
confidence=50%
Code:
From the above output, the rules generated with support>=15% and confidence=50% are:
Instructions
Name your files Task1.py, Task2.py and so on. Compress them in a single file and
name it as LabXX YourRollNumber. Submit this file at end of Lab.
Classifier
In supervised learning, the data is split into two parts; training data and testing data. We use the
training data along with its labels, to train our model. For testing, we only input the testing data
and not the labels. We use our trained model to predict the labels of this testing data and compare
it with the actual labels to see how accurate our model is.
The classifier that we will be using is Support Vector Machine (SVM). Without getting into
much detail, SVM tries to learn the boundary between different classes. You can imagine data as
a scatter plot, with data points of each class lying together as a cluster in a 2D plane. Now imagine
drawing a boundary such that the boundary separates/isolates each class from each other in the
plane. SVM tries to learn that boundary and use it to classify data.
Use the following line of code to load the training and testing data. You should have 60,000
training images, each with dimensions 28x28, and labels. The testing data should contain 10,000
images and labels. Check it after you have loaded the data.
(x_train, y_train), (x_test, y_test) = mnist.load_data()
1. Flatten each image into a 1D vector. Your training images should be of the shape
(60000,28,28). After flattening each image, the data should be of the shape (60000,784). Do
the same with the testing images. Also convert their type to
3. Use SVM from the sklearn library. Use a linear kernel and train the model using training
data. After training it, use the model to predict labels for the testing data.
Once we have the predicted labels, we can make a confusion matrix. It is a matrix with predicted
labels on one axis and actual labels on the other. The number in cell indicates the number of times
the corresponding actual label was classified as the corresponding predicted label. The diagonal of
course represents the correct predictions while the off diagonal terms are wrong predictions.
4. Using the in built function, ‘metrics’, find the confusion matrix of actual and predicted
labels of the testing data. You can plot it using the following code (cm is the confusion matrix):
plt.figure()
plt.imshow(cm, interpolation=’nearest’, cmap=’Pastel1’)
plt.title(’Confusion matrix’, size = 15) plt.colorbar()
tick_marks = np.arange(10)
plt.xticks(tick_marks, ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
plt.yticks(tick_marks, ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
plt.tight_layout() plt.ylabel(’Actual label’, size = 15) plt.xlabel(’Predicted label’,
size = 15) width, height = cm.shape
for x in range(width):
for y in range(height):
plt.annotate(str(cm[x][y]), xy=(y, x),
horizontalalignment=’center’,
verticalalignment=’center’)
plt.show()
Using the confusion matrix, we can calculate different merits of our model.
Two such merits are Accuracy and False Positive Rate (FPR) :
Number of correct predictions
Accuracy =
Total number of predictions
which can be calculated from the confusion matrix as:
• components = 5
• components = 11
• components = 44
P.S. You can compute the accuracy using the command metrics.accuracy score(y test, y
predict) where y predict are the predicted labels.
6. Comment on the time taken for classification using PCA as compared to using raw data.
Why do you think there is a difference? Why does increasing the number of components
leads to improved accuracy?
Conclusion:
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Genetic Algorithm
The genetic algorithm is a search-based optimization technique. It is frequently used to find the
optimal or nearest optimal solution
In genetic we will use some biological terms such as population, chromosome, gene, selection,
crossover, mutation. Now, first of all, let‘s try to understand what these terms mean.
At the beginning of this process, we need to initialize some possible solutions to this problem. The
population is a subset of all possible solutions to the given problem. In another way, we can say
that the population is a set of chromosomes. A chromosome is one of that solution to that current
problem. And each chromosome is a set of genes.
For simplicity, We can describe a chromosome as a string. So, we can say that a population is a
collection of some string(each character is a binary value, either 0 or 1 ). And each character of
the string is a gene.
For starting the process of the genetic algorithm, we first need to initialize the population. We can
initialize the population in two ways. The first one is random and the second one is heuristical. It
is always better to start the algorithm with some random population.
Now, let consider that we have a random population of four chromosomes like below. The
length of our chromosome is 5 as 2⁵=32 and 0≤x≤31.
Our fitness function will calculate the functional value of each chromosome as stated in the
problem statement :
For the 1st chromosome, 01110 means 14 in integer. So, f(x) = -(14*14) + 5 = -191.
For the 2nd chromosome, 10101 means 21 in integer. So, f(x) = -(21*21) + 5 = -436.
For the 3rd chromosome, 00011 means 3 in integer. So, f(x) = -(3*3) + 5 = -4.
For the 4th chromosome, 10111 means 23 in integer. So, f(x) = -(23*23) + 5 = -524.
Parent Selection
Parent selection is done by using the fitness values of the chromosomes calculated by the fitness
function. Based on these fitness values we need to select a pair chromosomes with the highest
fitness value.
There are many ways for fitness calculation like Roulette wheel selection, rank selection. In
Roulette wheel selection, the chromosome with the highest fitness value has the maximum
possibility to be selected as a parent. But in this selection process, a lower can be selected. In rank
selection, chromosomes are ranked based on their fitness values from higher to lower. As an
example, According to those fitness values calculated above, we can rank those chromosomes
from higher to lower like 3rd>1st>2nd>4th. So, in the selection phase, 3rd and 1st chromosomes
will be selected based on the fitness valued calculated from the fitness function.
Crossover
For a two-point crossover, we need to select two points and then exchange the bits.
Mutation
Mutation brings diversity to the population. There are different kinds of mutations like Bit Flip
mutation, Swap mutation, Inversion mutation, etc. These are so so simple.
In Bit Flip mutation, Just select one or more bits and then flip them. If the selected bit is 0 then
turn it to 1 and if the selected bit is 1 then turn it to 0.
In Swap Bit mutation, select two bits and just swap them.
Let‘s try to implement the genetic algorithm in python for function optimization.
Problem Statement
Let consider that we have an equation, f(x) = -x² + 5 . We need the solution for which it has
the maximum value and the constraint is 0≤x≤31. To select an initial population use the
probability 0.2.
Initial Population
Fitness Function
The fitness function calculates the fitness value of chromosomes. The functionality of the
fitness function depends on the problem‘s requirements.
Selection
Fittest chromosomes are selected based on the fitness scores. Here rank selection process is
used.
After selecting the fittest parents, a crossover is required to generate offsprings. Here, the
singlepoint crossover is used.
Mutation
After crossover is done, a mutation is done for maintaining the diversity from one generation
to another. Here, we will single point bit-flip mutation.
We need to iterate the whole process multiple times until we find our best solution.
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
Breadth-first search is an algorithm for traversing or searching tree or graph data structures. It
starts at the root node and explores all nodes at the present depth before moving on to the nodes at
the next depth level.
In other words, it expands the shallowest unexpanded node which can be implemented by a First-
In-First-Out (FIFO) queue. Let‘s go through an example with the following GRAPH:
From the above step by step expansion diagram, we can see that the BFS algorithm prioritizes
edges closest to the starting vertex to perform a search.
BFS Implementation
Creating the function that takes in edges of the GRAPH which outputs the adjacency list for
undirected graph
Creating the function that takes in the adjacency list and starting vertex which output the sequence
of BFS search
E is called the edge set and its elements are called edges. An edge is represented by a pair
of vertices.
The diagram is a schematic representation of the graph with vertices V={1, 2, 3, 4, 5, 6}, E={{1,
2},{1, 5},{2, 3},{2, 5},{3, 4},{4, 5},{4, 6}}
It starts at the root node and expands the deepest unexpanded node, backtracks only when no more
expansion. Let‘s go through an example with the following GRAPH:
From the above step by step expansion diagram, we can see that the DFS algorithm
prioritizes edges along the path to perform a search.
Completeness:
Infinite-depth spaces: No
Finite-depth spaces with loops: No
Finite-depth spaces with repeated-state checking: Yes
Finite-depth spaces without loops: Yes
Time Complexity: O(bᵐ)
Space Complexity: O(bm) Optimality:
No
DFS Implementation
Creating the function that takes in edges of the GRAPH which outputs the adjacency list for
undirected graph.
Creating the function that takes in the adjacency list and starting vertex which output the
sequence of DFS search.
Lab Exercises:
1. Implement search algorithm techniques BFS and DFS as discussed in the example.
Conclusion:
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______
________________________________________________________________________
______