SlideShare a Scribd company logo
Data Structures and
Algorithms
By Mohamed Gamal
© Mohamed Gamal 2024
C++
The topics of today’s lecture:
Agenda
Data Structures & Algorithms - Lecture 2
Searching Algorithms
– Searching is to find the location of specific value in a list of data
elements.
– Searching methods can be divided into two categories:
• Searching methods for both sorted and unsorted lists.
– e.g., Linear (or Sequential) Search.
• Searching methods for sorted lists.
– e.g., Binary Search.
– Direct access by key value (hashing).
Data Structures & Algorithms - Lecture 2
1) Linear (or Sequential) Search
– Linear Search is one of the simplest search algorithms to understand and
implement.
– It can be used on both sorted and unsorted data.
– The algorithm checks each element in the array or list sequentially until the
desired element is found or the end of the list is reached.
target
Linear Search Algorithm
1) Start from the beginning of the list or array.
2) Iterate through each element sequentially.
3) Compare each element with the target value.
4) If the element matches the target value, return its index (or position).
5) If the end of the list is reached without finding the target value, return a message
indicating the target value is not in the list.
6) Stop.
2 8 5 3 9 4
2 8 5 3 9 4
2 8 5 3 9 4
2 8 5 3 9 4
2 8 5 3 9 4
2 8 5 3 9 4
2 8 5 3 9 4
Initial
Unsorted List
Example:
Target: find 9
1st
Iteration
2nd
Iteration
3rd
Iteration
4th
Iteration
5th
Iteration
2 == 9 ?
8 == 9 ?
5 == 9 ?
3 == 9 ?
9 == 9 ?
Target
#include <iostream>
using namespace std;
int linearSearch(int arr[], int n, int target)
{
for (int i = 0; i < n; i++)
if (arr[i] == target)
return i;
return -1;
}
void print(int arr[], int n)
{
cout << "Index: ";
for (int i = 0; i < n; i++)
cout << i << " ";
cout << endl;
cout << "Array: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
int main(void)
{
int arr[] = { 2, 8, 5, 3, 9, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
int val = 9;
int index = linearSearch(arr, n, val);
print(arr, n);
if (index != -1)
cout << "nFound " << arr[index] << " at index " << index << endl;
else
cout << "nNot found!" << endl;
return 0;
}
Linear Search
C++
implementation
Complexity:
▪ Worst Case: 𝑂(𝑛)
▪ Average Case: 𝑂(𝑛)
▪ Best Case: 𝑂(1)
Advantages:
▪ The algorithm is straightforward to implement with minimal code.
▪ Linear Search can be applied to any data structure that supports sequential access, such as
arrays, linked lists, and even files.
▪ Linear Search is an in-place search algorithm and does not require any additional memory.
Disadvantages:
▪ It is inefficient for large datasets because it may require examining each element.
Data Structures & Algorithms - Lecture 2
2) Binary Search
– It uses the divide-and-conquer approach to reduce the search space by half with
each comparison.
– Binary Search continually divides the search interval in half and compares the
middle element with the target value.
– Binary Search requires the data to be sorted in ascending or descending order
before searching.
– Binary Search can be implemented recursively or iteratively.
1 2 3 4 5 8 9
Right or Low
0 1 2 3 4 5 6
Left or High
Sorted
List
Mid
(Low + High) / 2
Binary Search Algorithm
1) Let min = 0 and max = n – 1, where n is the number of elements in the sorted array.
2) Compute the midpoint:
a) mid = (low + high) / 2
3) Compare the target value with the element at the midpoint:
a) If target == array[mid], return mid (target found).
b) If target < array[mid], set high = mid - 1 (discard the right half).
c) If target > array[mid], set low = mid + 1 (discard the left half).
4) Repeat steps 2-3 until low <= high.
5) If the target value is not found after the loop, return a message indicating it is not
in the array.
Initial
Sorted List
Example:
Target: find 3
2nd
Iteration
3rd
Iteration
Target = arr mid ?
Return mid (target found).
Target < arr[mid] ?
Set high = mid - 1 (discard the right half).
Target > arr[mid] ?
Set low = mid + 1 (discard the left half).
Target
Mid High
1 2 3 4 5 8 9
1 2 3 4 5 8 9
1 2 3 4 5 8 9
Low High
Mid
Low
High
Mid
Low
No. Iterations = log2 7 = 3 iterations
1st
Iteration
Mid High
1 2 3 4 5 8 9 13 17 20 30
Low
Mid High
1 2 3 4 5 8 9 13 17 20 30
Low
Mid
High
1 2 3 4 5 8 9 13 17 20 30
Low
Target = 20
Mid
(High + Low) / 2
Target = arr mid ?
Return mid (target found).
Target < arr[mid] ?
Set high = mid - 1 (discard the right half).
Target > arr[mid] ?
Set low = mid + 1 (discard the left half).
1 2 3 4 5 8 9 13 17 20 30
Mid
High
Low
Given the list { 1, 2, 3, 4, 5, 8, 9 } and the target value 3
1. First iteration:
1. Calculate mid: mid = (0 + 6) / 2 = 3 (integer division)
2. Compare target 3 with the middle element 4
3. Since 3 < 4, update right boundary: High = 3 − 1 = 2
2. Second iteration:
1. Calculate new mid: mid = (0 + 2) / 2 = 1
2. Compare target 3 with middle element 2
3. Since 3 > 2, update low boundary: Low = 1 + 1 = 2
3. Third iteration:
1. Calculate new mid: mid = (2 + 2) / 2 = 2
2. Compare target 3 with element at index 2: 3
3. Target found at index 2
Thus, it took 3 iterations to find the target value 3.
Target
Mid High
1 2 3 4 5 8 9
Low High
Mid
Low
High
Mid
Low
1 2 3 4 5 8 9
1 2 3 4 5 8 9
#include <iostream>
using namespace std;
int binarySearch(int arr[], int n, int target)
{
int low = 0;
int high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
// Check if target is present at mid
if (arr[mid] == target)
return mid;
// If target greater, ignore left half
else if (target > arr[mid])
low = mid + 1;
// If target is smaller, ignore right half
else
high = mid - 1;
}
return -1;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 8, 9 };
int n = sizeof(arr) / sizeof(arr[0]);
int val = 3;
int index = binarySearch(arr, n, val);
if (index != -1)
cout << "Element found at index " << index << endl;
else
cout << "Element not found in the array" << endl;
return 0;
}
Binary Search
C++
implementation
Complexity:
▪ Worst Case: 𝑂(log 𝑛)
▪ Average Case: 𝑂 log 𝑛
▪ Best Case: 𝑂(1) — at the middle.
Advantages:
▪ The algorithm is relatively simple to implement, especially in its iterative form, and easy to understand.
▪ Highly efficient and faster for searching large sorted datasets.
▪ It does not require additional memory beyond a few variables for indices.
Disadvantages:
▪ If the data is not sorted, Binary Search cannot be applied directly. Sorting the data first would incur additional time complexity.
▪ Binary Search is not efficient for data structures that do not support random access, such as linked lists, because it relies on
indexing to access the middle element.
▪ While the iterative version is simple, the recursive version can be more complex due to additional overhead from recursive
function calls, which use more stack space.
▪ For small datasets, the overhead of repeatedly dividing the search space and comparing elements might make it slower compared
to a simpler linear search.
Decrease-and-Conquer
Russian Peasant Multiplication
– Russian Peasant Multiplication, also known as Egyptian Multiplication, is an
ancient algorithm for multiplying two numbers.
– Its origins date back to ancient civilizations, including the Egyptians and Russians,
who used this method for its simplicity and ease of use with basic arithmetic
operations.
– The algorithm's beauty lies in its use of only basic operations: addition, halving,
and doubling.
Russian Peasant Multiplication — Algorithm
1) Let the two given numbers be 'a' and 'b'.
2) Initialize result 'res' as 0.
3) Do the following while 'b' is greater than 0
a) If 'b' is odd, add 'a' to 'res'
b) Double 'a' and halve 'b'
4) Return 'res'.
÷ 2 × 2
𝑎 𝑏
Cancel every row with even numbers.
𝑎 𝑏
Add other rows with odd numbers.
𝑎 𝑏
Data Structures & Algorithms - Lecture 2
int russianMult(int a, int b) {
int res = 0;
while (a > 0) {
// if 'a' is odd, add 'b’ to 'res'
if (a % 2 != 0)
res += b;
a = a >> 1; // halve a
b = b << 1; // double b
}
return res;
}
Method #1
int russianMult(int a, int b) {
int res = 0;
while (a > 0) {
// if 'a' is odd, add 'b' to res
if (a % 2 != 0)
res += b;
a /= 2; // halve a
b *= 2; // double b
}
return res;
}
Method #2
Complexity:
▪ Worst Case: O log 𝑎 – 𝑎 is a large integer.
▪ Average Case: O log 𝑎 – 𝑎 is a random integer.
▪ Best Case: O(log 𝑎)– 𝑎 is a very small integer.
Advantages:
▪ Simplicity – easy to implement with basic arithmetic and bitwise operations.
▪ Efficiency – logarithmic time complexity, scalable for large values.
Disadvantages:
▪ Less practical compared to modern optimized multiplication algorithms.
▪ No Built-In Hardware Optimization – relies on software-based arithmetic without direct hardware
support.
Data Structures & Algorithms - Lecture 2
– There’re two formulas for calculating the day of the week for a given date.
» Zeller’s Congruence
» Key-Value Method
– Both the methods work for the Gregorian calendar.
Introduction
Christian Zeller
Data Structures & Algorithms - Lecture 2
Saturday Sunday Monday Tuesday Wednesday Thursday Friday
0 1 2 3 4 5 6
Mar Apr May Jun Jul Aug Sep Oct Nov Dec Jan Feb
3 4 5 6 7 8 9 10 11 12 13 14
Days Chart:
Months Chart:
1) Zeller’s Congruence
where,
day of the week
corresponding month number
𝑥 is the required day of the week.
𝑑 is the given day of the month.
𝑚 is the corresponding month number.
𝐾 is the year of century (i.e., last two digits of the given year).
𝐽 is the century (i.e., first two digits of the year.)
𝑥 = 𝑑 +
13 𝑚 + 1
5
+ 𝐾 +
𝐾
4
+
𝐽
4
+ 5𝐽 mod 7
𝐾 = 𝑦𝑒𝑎𝑟 % 100
𝐽 = Τ
𝑦𝑒𝑎𝑟 100
Note: an exception for both January and February, subtract 1 from the given year.
Example: calculate the day for the date 1st April 1983.
𝑑 = 1, 𝑚 = 4, 𝐾 = 1983 % 100 = 83, 𝐽 = Τ
1983 100 = 19
1st April 1983
𝐾
𝐽
𝑚
𝑑
𝑥 = 1 +
13 4 + 1
5
+ 83 +
83
4
+
19
4
+ 5 19 mod 7
= 216 mod 7
= 6
6 is Friday according to the days chart.
𝑥 = 𝑑 +
13 𝑚 + 1
5
+ 𝐾 +
𝐾
4
+
𝐽
4
+ 5𝐽 mod 7
𝐾 = 𝑦𝑒𝑎𝑟 % 100
𝐽 = Τ
𝑦𝑒𝑎𝑟 100
Example: calculate the day for the date 2nd March 2004.
𝑥 = 2 +
13 3 + 1
5
+ 4 +
4
4
+
20
4
+ 5 20 mod 7
= 122 mod 7
= 3
3 is Tuesday according to the days chart.
𝑥 = 𝑑 +
13 𝑚 + 1
5
+ 𝐾 +
𝐾
4
+
𝐽
4
+ 5𝐽 mod 7
2nd March 2004
𝐾
𝐽
𝑚
𝑑
𝑑 = 2, 𝑚 = 3, 𝐾 = 2004 % 100 = 04, 𝐽 = Τ
2004 100 = 20
𝐾 = 𝑦𝑒𝑎𝑟 % 100
𝐽 = Τ
𝑦𝑒𝑎𝑟 100
Example: calculate the day for the date 27th February 2023.
27th February 2023
𝐾
𝐽
𝑚
𝑑
𝑥 = 𝑑 +
13 𝑚 + 1
5
+ 𝐾 +
𝐾
4
+
𝐽
4
+ 5𝐽 mod 7
𝑥 = 27 +
13 14 + 1
5
+ 22 +
22
4
+
20
4
+ 5 20 mod 7
= 198 mod 7
= 2
2 is Monday according to the days chart.
𝑑 = 27, 𝑚 = 14, 𝐾 = (2023 − 1) % 100 = 22, 𝐽 = Τ
(2023 − 1) 100 = 20
𝐾 = 𝑦𝑒𝑎𝑟 % 100
𝐽 = Τ
𝑦𝑒𝑎𝑟 100
Exception for
February !
Python Code
Data Structures & Algorithms - Lecture 2
2) Key-Value Method
where,
day of the week
month key value number
century key value
𝑥 is the required day of the week.
𝑑 is the given day of the month.
𝑚 is the month key value number.
𝐾 is the year of century (i.e., last two digits of the given year).
𝐽 is the century key value (e.g., 0 for the 1900s)
𝑥 = 𝑑 + 𝑚 + 𝐾 +
𝐾
4
+ 𝐽 mod 7
Saturday Sunday Monday Tuesday Wednesday Thursday Friday
0 1 2 3 4 5 6
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1 4 4 0 2 5 0 3 6 1 4 6
Days Chart:
Months Key-Value Chart:
1400-1499 2
1500-1599 0
1600-1699 6
1700-1799 4
Century Key-Value Chart:
1800-1899 2
1900-1999 0
2000-2099 6
2100-2199 4
2200-2299 2
2300-2399 0
2400-2499 6
2500-2599 4
…
Example: calculate the day for the date 1st April 1983.
𝑑 = 1, 𝑚 = 0, 𝐾 = 83, 𝐽 = 0
1st April 1983
𝐾
𝐽
𝑚
𝑑
𝑥 = 1 + 0 + 83 +
83
4
+ 0 mod 7
= 104 mod 7
= 6 6 is Friday according to the days chart.
𝑥 = 𝑑 + 𝑚 + 𝐾 +
𝐾
4
+ 𝐽 mod 7
Months Chart
Century Chart
Example: calculate the day for the date 2nd March 2004.
𝑑 = 2, 𝑚 = 4, 𝐾 = 04, 𝐽 = 6
𝑥 = 2 + 4 + 4 +
4
4
+ 6 mod 7
= 17 mod 7
= 3
𝑥 = 𝑑 + 𝑚 + 𝐾 +
𝐾
4
+ 𝐽 mod 7
Months Chart
Century Chart
3 is Tuesday according to the days chart.
2nd March 2004
𝐾
𝐽
𝑚
𝑑
Example: calculate the day for the date 27th February 2023.
𝑑 = 27, 𝑚 = 4, 𝐾 = 23, 𝐽 = 6
𝑥 = 27 + 4 + 23 +
23
4
+ 6 mod 7
= 65 mod 7
= 2
𝑥 = 𝑑 + 𝑚 + 𝐾 +
𝐾
4
+ 𝐽 mod 7
Months Chart
Century Chart
2 is Monday according to the days chart.
27th February 2023
𝐾
𝐽
𝑚
𝑑
Data Structures & Algorithms - Lecture 2
Graph are versatile data structures used in many real-world applications.
Graphs Theory Concept
Social Networks
• Nodes: People.
• Edges: connection.
• Application: suggesting friends based on shortest path (i.e., nearby located friends).
Computer Networks
• Nodes: Computers or Routers.
• Edges: Network connections.
• Application: finding the shortest route for data packets.
Ahmed Ali
Saad Omar
3
1 5
Basic Terminology
1 2
4
3
1 : Node or Vertex
: Directed Edge
: Undirected Edge
: Weighted Edge (i.e., includes cost)
n
1 : Loop
1 2
4
3
5
Graph
Directed Undirected
Weighted Unweighted Weighted Unweighted
Positive Negative Positive Negative
Graph Types
Graphs Types
Adjacency Matrix: an adjacency matrix is a square matrix, or you can say it is a 2D array, and the elements
of the matrix indicate whether pairs of vertices are adjacent or not in the graph.
Graph Representation
1 2
4
3
5
1 2 3 4 5
1 0 1 0 0 0
2 0 0 0 0 1
3 1 0 0 0 0
4 1 0 1 0 0
5 0 0 0 1 0
#include <iostream>
using namespace std;
void print(int arr[][5], int rows, int cols);
int main(void) {
int graph[][5] = { { 0, 1, 0, 0, 0 },
{ 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0 },
{ 1, 0, 1, 0, 0 },
{ 0, 0, 0, 1, 0 } };
int rows = sizeof(graph) / sizeof(graph[0]);
int cols = sizeof(graph[0]) / sizeof(graph[0][0]);
print(graph, rows, cols);
return 0;
}
void print(int arr[][5], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
cout << arr[i][j] << " ";
cout << endl;
}
}
C++
implementation
– Uninformed (blind) strategies use only the information available in the problem definition.
– These strategies order nodes without using any domain specific information (Blind).
– Contrary to Informed (heuristic) search techniques which might have additional information.
• Breadth-first search (BFS)
• Depth-first search (DFS)
• Depth-limited search (DLS)
• Iterative deepening search (IDS)
• …
• etc.
Unguided/Blind search
Uninformed (Blind) Search Strategies
Complete? Yes
Optimal? Yes, if path cost is nondecreasing function of depth
Time Complexity: O(bd)
Space Complexity: O(bd), note that every node in the fringe is kept in the queue
Breadth First Search (BFS)
◼ Application 1:
Given the following state space (tree search), give the sequence of visited nodes when
using BFS (assume that the node O is the goal state).
A
B C E
D
F G H I J
K L
O
M N
◼ A,
A
B C E
D
Breadth First Search (BFS)
◼ A,
◼ B,
A
B C E
D
F G
Breadth First Search (BFS)
◼ A,
◼ B,C
A
B C E
D
F G H
Breadth First Search (BFS)
◼ A,
◼ B,C,D
A
B C E
D
F G H I J
Breadth First Search (BFS)
◼ A,
◼ B,C,D,E
A
B C E
D
F G H I J
Breadth First Search (BFS)
◼ A,
◼ B,C,D,E,
◼ F,
A
B C E
D
F G H I J
Breadth First Search (BFS)
◼ A,
◼ B,C,D,E,
◼ F,G
A
B C E
D
F G H I J
K L
Breadth First Search (BFS)
◼ A,
◼ B,C,D,E,
◼ F,G,H
A
B C E
D
F G H I J
K L
Breadth First Search (BFS)
◼ A,
◼ B,C,D,E,
◼ F,G,H,I
A
B C E
D
F G H I J
K L M
Breadth First Search (BFS)
◼ A,
◼ B,C,D,E,
◼ F,G,H,I,J,
A
B C E
D
F G H I J
K L M N
Breadth First Search (BFS)
◼ A,
◼ B,C,D,E,
◼ F,G,H,I,J,
◼ K, A
B C E
D
F G H I J
K L M N
Breadth First Search (BFS)
◼ A,
◼ B,C,D,E,
◼ F,G,H,I,J,
◼ K,L A
B C E
D
F G H I J
K L
O
M N
Breadth First Search (BFS)
◼ A,
◼ B,C,D,E,
◼ F,G,H,I,J,
◼ K,L,M, A
B C E
D
F G H I J
K L
O
M N
Breadth First Search (BFS)
◼ A,
◼ B,C,D,E,
◼ F,G,H,I,J,
◼ K,L,M,N, A
B C E
D
F G H I J
K L
O
M N
Breadth First Search (BFS)
◼ A,
◼ B,C,D,E,
◼ F,G,H,I,J,
◼ K,L,M,N,
◼ Goal state: O
A
B C E
D
F G H I J
K L
O
M N
Breadth First Search (BFS)
◼ The returned solution is the sequence of operators in the path:
A, B, G, L, O
A
B C E
D
F G H I J
K L
O
M N
Breadth First Search (BFS)
Complete? No
Optimal? No
Time Complexity: O(bm)
Space Complexity: O(b × m)
◼ Application 2:
Given the following state space (tree search), give the sequence of visited nodes when
using DFS (assume that the node O is the goal state).
A
B C E
D
F G H I J
K L
O
M N
Depth First Search (DFS)
◼ A,
A
B C E
D
Depth First Search (DFS)
◼ A,B,
A
B C E
D
F G
Depth First Search (DFS)
◼ A,B,F,
A
B C E
D
F G
Depth First Search (DFS)
◼ A,B,F,
◼ G,
A
B C E
D
F G
K L
Depth First Search (DFS)
◼ A,B,F,
◼ G,K,
A
B C E
D
F G
K L
Depth First Search (DFS)
◼ A,B,F,
◼ G,K,
◼ L,
A
B C E
D
F G
K L
O
Depth First Search (DFS)
◼ A,B,F,
◼ G,K,
◼ L, O: Goal State
A
B C E
D
F G
K L
O
Depth First Search (DFS)
A
B C E
D
F G
K L
O
◼ The returned solution is the sequence of operators in the path:
A, B, G, L, O
Depth First Search (DFS)
Complete? Yes, if there is a goal state at a depth less than L
Optimal? No
Time Complexity: O(bL)
Space Complexity: O(bL)
◼ Application 3:
Given the following state space (tree search), give the sequence of visited nodes when
using DLS (Limit = 2).
A
B C E
D
F G H I J
K L
O
M N
Limit = 0
Limit = 1
Limit = 2
Depth-Limited Search (DLS)
◼ A,
A
B C E
D
Limit = 2
Limit = 0
Limit = 1
Depth-Limited Search (DLS)
◼ A,B,
A
B C E
D
F G
Limit = 2
Limit = 0
Limit = 1
Limit = 2
Depth-Limited Search (DLS)
◼ A,B,F,
A
B C E
D
F G
Limit = 2
Limit = 0
Limit = 1
Limit = 2
Depth-Limited Search (DLS)
◼ A,B,F,
◼ G,
A
B C E
D
F G
Limit = 2
Limit = 0
Limit = 1
Limit = 2
Depth-Limited Search (DLS)
◼ A,B,F,
◼ G,
◼ C,
A
B C E
D
F G H
Limit = 2
Limit = 0
Limit = 1
Limit = 2
Depth-Limited Search (DLS)
◼ A,B,F,
◼ G,
◼ C,H,
A
B C E
D
F G H
Limit = 2
Limit = 0
Limit = 1
Limit = 2
Depth-Limited Search (DLS)
◼ A,B,F,
◼ G,
◼ C,H,
◼ D, A
B C E
D
F G H I J
Limit = 2
Limit = 0
Limit = 1
Limit = 2
Depth-Limited Search (DLS)
◼ A,B,F,
◼ G,
◼ C,H,
◼ D,I A
B C E
D
F G H I J
Limit = 2
Limit = 0
Limit = 1
Limit = 2
Depth-Limited Search (DLS)
◼ A,B,F,
◼ G,
◼ C,H,
◼ D,I
◼ J,
A
B C E
D
F G H I J
Limit = 2
Limit = 0
Limit = 1
Limit = 2
Depth-Limited Search (DLS)
◼ A,B,F,
◼ G,
◼ C,H,
◼ D,I
◼ J,
◼ E
A
B C E
D
F G H I J
Limit = 2
Limit = 0
Limit = 1
Limit = 2
Depth-Limited Search (DLS)
◼ A,B,F,
◼ G,
◼ C,H,
◼ D,I
◼ J,
◼ E, Failure
A
B C E
D
F G H I J
Limit = 2
Limit = 2
Depth-Limited Search (DLS)
◼ DLS algorithm returns Failure (no solution)
◼ The reason is that the goal is beyond the limit (Limit =2): the goal depth is (d=4)
A
B C E
D
F G H I J
K L
O
M N
Limit = 2
Solution: use IDS!
Depth-Limited Search (DLS)
Complete? Yes
Optimal? Yes
Time Complexity: O(bd)
Space Complexity: O(bd)
◼ Application 4:
Given the following state space (tree search), give the sequence of visited nodes when
using IDS.
A
B C E
D
F G H I J
K L
O
M N
Limit = 0
Limit = 1
Limit = 2
Limit = 3
Limit = 4
Iterative Deepening Search (IDS)
Iterative Deepening Search (IDS)
DLS with bound = 0
◼ A,
A
Limit = 0
Iterative Deepening Search (IDS)
◼ A, Failure
A
Limit = 0
Iterative Deepening Search (IDS)
Iterative Deepening Search (IDS)
DLS with bound = 1
◼ A,
A
B C E
D
Limit = 1
Iterative Deepening Search (IDS)
◼ A,B,
A
B C E
D
Limit = 1
Iterative Deepening Search (IDS)
◼ A,B,
◼ C,
A
B C E
D
Limit = 1
Iterative Deepening Search (IDS)
◼ A,B,
◼ C,
◼ D,
A
B C E
D
Limit = 1
Iterative Deepening Search (IDS)
◼ A,B
◼ C,
◼ D,
◼ E, A
B C E
D
Limit = 1
Iterative Deepening Search (IDS)
◼ A,B,
◼ C,
◼ D,
◼ E, Failure A
B C E
D
Limit = 1
Iterative Deepening Search (IDS)
Iterative Deepening Search (IDS)
DLS with bound = 2
◼ A,
A
B C E
D
Limit = 2
Iterative Deepening Search (IDS)
◼ A,B,
A
B C E
D
F G
Limit = 2
Iterative Deepening Search (IDS)
◼ A,B,F,
A
B C E
D
F G
Limit = 2
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,
A
B C E
D
F G
Limit = 2
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,
◼ C,
A
B C E
D
F G H
Limit = 2
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,
◼ C,H,
A
B C E
D
F G H
Limit = 2
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,
◼ C,H,
◼ D, A
B C E
D
F G H I J
Limit = 2
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,
◼ C,H,
◼ D,I A
B C E
D
F G H I J
Limit = 2
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,
◼ C,H,
◼ D,I
◼ J,
A
B C E
D
F G H I J
Limit = 2
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,
◼ C,H,
◼ D,I
◼ J,
◼ E
A
B C E
D
F G H I J
Limit = 2
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,
◼ C,H,
◼ D,I
◼ J,
◼ E, Failure
A
B C E
D
F G H I J
K L
O
M N
Limit = 2
Iterative Deepening Search (IDS)
Iterative Deepening Search (IDS)
DLS with bound = 3
◼ A,
A
B C E
D
Limit = 3
Iterative Deepening Search (IDS)
◼ A,B,
A
B C E
D
F G
Limit = 3
Iterative Deepening Search (IDS)
◼ A,B,F,
A
B C E
D
F G
Limit = 3
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,
A
B C E
D
F G
K L
Limit = 3
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,K,
A
B C E
D
F G
K L
Limit = 3
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,K,
◼ L,
A
B C E
D
F G
K L
Limit = 3
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,K,
◼ L,
◼ C, A
B C E
D
F G H
K L
Limit = 3
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,K,
◼ L,
◼ C,H, A
B C E
D
F G H
K L
Limit = 3
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,K,
◼ L,
◼ C,H,
◼ D,
A
B C E
D
F G H I J
K L
Limit = 3
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,K,
◼ L,
◼ C,H,
◼ D,I,
A
B C E
D
F G H I J
K L M
Limit = 3
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,K,
◼ L,
◼ C,H,
◼ D,I,M,
A
B C E
D
F G H I J
K L M
Limit = 3
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,K,
◼ L,
◼ C,H,
◼ D,I,M,
◼ J,
A
B C E
D
F G H I J
K L M N
Limit = 3
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,K,
◼ L,
◼ C,H,
◼ D,I,M,
◼ J,N,
A
B C E
D
F G H I J
K L M N
Limit = 3
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,K,
◼ L,
◼ C,H,
◼ D,I,M,
◼ J,N,
◼ E,
A
B C E
D
F G H I J
K L M N
Limit = 3
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,K,
◼ L,
◼ C,H,
◼ D,I,M,
◼ J,N,
◼ E,Failure
A
B C E
D
F G H I J
K L
O
M N
Limit = 3
Iterative Deepening Search (IDS)
Iterative Deepening Search (IDS)
DLS with bound = 4
◼ A,
A
B C E
D
Limit = 4
Iterative Deepening Search (IDS)
◼ A,B,
A
B C E
D
F G
Limit = 4
Iterative Deepening Search (IDS)
◼ A,B,F,
A
B C E
D
F G
Limit = 4
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,
A
B C E
D
F G
K L
Limit = 4
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,K,
A
B C E
D
F G
K L
Limit = 4
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,K,
◼ L,
A
B C E
D
F G
K L
O
Limit = 4
Iterative Deepening Search (IDS)
◼ A,B,F,
◼ G,K,
◼ L, O: Goal State
A
B C E
D
F G
K L
O
Limit = 4
Iterative Deepening Search (IDS)
The returned solution is the sequence of operators in the path:
A, B, G, L, O
A
B C E
D
F G
K L
O
Iterative Deepening Search (IDS)
Game Playing
• Minimax uses DFS to evaluate nodes.
• Perfect play for deterministic games.
• Idea: choose a move to position with highest minimax value (i.e., best achievable
payoff against best play.
• e.g., 2-play games.
• Minimax Visualizer
Minimax Algorithm
Game tree (2-player, deterministic, turns)
Tic Tac Teo
Data Structures & Algorithms - Lecture 2
max
max
min
min
© Patrick Winston
© Patrick Winston
max
max
min
min
max
max
min
min
© Patrick Winston
max
max
min
min
© Patrick Winston
max
max
min
min
© Patrick Winston
max
max
min
min
© Patrick Winston
max
max
min
min
© Patrick Winston
max
max
min
min
© Patrick Winston
max
max
min
min
© Patrick Winston
max
max
min
min
© Patrick Winston
max
max
min
min
© Patrick Winston
#include <iostream>
using namespace std;
int log2(int n);
int max(int a, int b);
int min(int a, int b);
int minimax(int depth, int nodeIndex, bool isMax, int scores[], int h);
int main()
{
// The number of elements in scores must be a power of 2
int scores[] = { 84, -29, -37, -25, 1, -43, -75, 49, -21, -51, 58, -46, -3, -13, 26, 79 };
int n = sizeof(scores) / sizeof(scores[0]);
int height = log2(n);
int res = minimax(0, 0, true, scores, height);
cout << "The optimal value is " << res << endl;
return 0;
}
/*
depth: current depth in game tree.
nodeIndex: index of current node in scores[].
isMax: true if current move is of maximizer, else false.
scores[]: stores leaves of Game tree.
h: maximum height of Game tree.
*/
int minimax(int depth, int nodeIndex, bool isMax, int scores[], int h)
{
// terminating condition (i.e leaf node is reached)
if (depth == h)
return scores[nodeIndex];
// if current move is maximizer, find the maximum attainable value
if (isMax)
return max(minimax(depth + 1, nodeIndex * 2, false, scores, h), minimax(depth + 1, nodeIndex * 2 + 1, false, scores, h));
// else (if current move is Minimizer), find the minimum attainable value
else
return min(minimax(depth + 1, nodeIndex * 2, true, scores, h), minimax(depth + 1, nodeIndex * 2 + 1, true, scores, h));
}
// function to find Log n in base 2 using recursion
int log2(int n) {
return (n == 1) ? 0 : 1 + log2(n / 2);
}
// maximum element function
int max(int a, int b) {
return (a > b) ? a : b;
}
// minimum element function
int min(int a, int b) {
return (a < b) ? a : b;
}
Minimax
C++
implementation
– Time complexity: O(bm)
– Space complexity: O(bm)
Mini-Max Properties
Minimax uses DFS to evaluate nodes !
Same as DFS
function minimax(node, depth, maximizingPlayer):
if depth = 0 or node is a terminal node:
return the heuristic value of the node
if maximizingPlayer:
bestValue = -infinity
for each child node of node:
v = minimax(child, depth - 1, FALSE)
bestValue = max(bestValue, v)
return bestValue
else:
bestValue = +infinity
for each child node of node:
v = minimax(child, depth - 1, TRUE)
bestValue = min(bestValue, v)
return bestValue
Minimax
Algorithm
Tic Tac Teo Game using
Minimax algorithm
End of lecture 2
ThankYou!
Ad

More Related Content

Similar to Data Structures & Algorithms - Lecture 2 (20)

PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPS 5.5.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...PPS 5.5.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Sitamarhi Institute of Technology
 
ds 2Arrays.ppt
ds 2Arrays.pptds 2Arrays.ppt
ds 2Arrays.ppt
AlliVinay1
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
infanciaj
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
Praveen M Jigajinni
 
Array in C full basic explanation
Array in C full basic explanationArray in C full basic explanation
Array in C full basic explanation
TeresaJencyBala
 
Data Structures & Algorithms - Lecture 1
Data Structures & Algorithms - Lecture 1Data Structures & Algorithms - Lecture 1
Data Structures & Algorithms - Lecture 1
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
In the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdfIn the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdf
arpitaeron555
 
Chapter #3 (Searchinmmmg ALgorithm).pptx
Chapter #3 (Searchinmmmg ALgorithm).pptxChapter #3 (Searchinmmmg ALgorithm).pptx
Chapter #3 (Searchinmmmg ALgorithm).pptx
hekmatyarzahir44
 
Ch 2Algo Analysis.pptxCh 2Algo Analysis.pptx
Ch 2Algo Analysis.pptxCh 2Algo Analysis.pptxCh 2Algo Analysis.pptxCh 2Algo Analysis.pptx
Ch 2Algo Analysis.pptxCh 2Algo Analysis.pptx
abduulahikhmaies
 
Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in Algoritm
Ehsan Ehrari
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
chouguleamruta24
 
Heap, quick and merge sort
Heap, quick and merge sortHeap, quick and merge sort
Heap, quick and merge sort
Dr. Mohammad Amir Khusru Akhtar (Ph.D)
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
BG Java EE Course
 
UNIT V.docx
UNIT V.docxUNIT V.docx
UNIT V.docx
Revathiparamanathan
 
CP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfCP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdf
saneshgamerz
 
Searching
SearchingSearching
Searching
A. S. M. Shafi
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
vrgokila
 
Arrays And Pointers in C programming language
Arrays And Pointers in C programming languageArrays And Pointers in C programming language
Arrays And Pointers in C programming language
Jasminelobo2
 
Data analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxData analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptx
sgrishma559
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
Dabbal Singh Mahara
 
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPS 5.5.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...PPS 5.5.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Sitamarhi Institute of Technology
 
ds 2Arrays.ppt
ds 2Arrays.pptds 2Arrays.ppt
ds 2Arrays.ppt
AlliVinay1
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
infanciaj
 
Array in C full basic explanation
Array in C full basic explanationArray in C full basic explanation
Array in C full basic explanation
TeresaJencyBala
 
In the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdfIn the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdf
arpitaeron555
 
Chapter #3 (Searchinmmmg ALgorithm).pptx
Chapter #3 (Searchinmmmg ALgorithm).pptxChapter #3 (Searchinmmmg ALgorithm).pptx
Chapter #3 (Searchinmmmg ALgorithm).pptx
hekmatyarzahir44
 
Ch 2Algo Analysis.pptxCh 2Algo Analysis.pptx
Ch 2Algo Analysis.pptxCh 2Algo Analysis.pptxCh 2Algo Analysis.pptxCh 2Algo Analysis.pptx
Ch 2Algo Analysis.pptxCh 2Algo Analysis.pptx
abduulahikhmaies
 
Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in Algoritm
Ehsan Ehrari
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
chouguleamruta24
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
BG Java EE Course
 
CP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfCP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdf
saneshgamerz
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
vrgokila
 
Arrays And Pointers in C programming language
Arrays And Pointers in C programming languageArrays And Pointers in C programming language
Arrays And Pointers in C programming language
Jasminelobo2
 
Data analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxData analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptx
sgrishma559
 

More from Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt (20)

How to install CS50 Library (Step-by-step guide)
How to install CS50 Library (Step-by-step guide)How to install CS50 Library (Step-by-step guide)
How to install CS50 Library (Step-by-step guide)
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Understanding Singular Value Decomposition (SVD)
Understanding Singular Value Decomposition (SVD)Understanding Singular Value Decomposition (SVD)
Understanding Singular Value Decomposition (SVD)
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Introduction to Linux OS (Part 2) - Tutorial
Introduction to Linux OS (Part 2) - TutorialIntroduction to Linux OS (Part 2) - Tutorial
Introduction to Linux OS (Part 2) - Tutorial
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Introduction to Linux OS (Part 1) - Tutorial
Introduction to Linux OS (Part 1) - TutorialIntroduction to Linux OS (Part 1) - Tutorial
Introduction to Linux OS (Part 1) - Tutorial
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
How to use tmux in Linux - A basic tutorial
How to use tmux in Linux - A basic tutorialHow to use tmux in Linux - A basic tutorial
How to use tmux in Linux - A basic tutorial
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Getting started with neural networks (NNs)
Getting started with neural networks (NNs)Getting started with neural networks (NNs)
Getting started with neural networks (NNs)
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Understanding K-Nearest Neighbor (KNN) Algorithm
Understanding K-Nearest Neighbor (KNN) AlgorithmUnderstanding K-Nearest Neighbor (KNN) Algorithm
Understanding K-Nearest Neighbor (KNN) Algorithm
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Understanding Convolutional Neural Networks (CNN)
Understanding Convolutional Neural Networks (CNN)Understanding Convolutional Neural Networks (CNN)
Understanding Convolutional Neural Networks (CNN)
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Luhn's algorithm to validate Egyptian ID numbers
Luhn's algorithm to validate Egyptian ID numbersLuhn's algorithm to validate Egyptian ID numbers
Luhn's algorithm to validate Egyptian ID numbers
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Difference between Mean and Weighted Mean
Difference between Mean and Weighted MeanDifference between Mean and Weighted Mean
Difference between Mean and Weighted Mean
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Complier Design - Operations on Languages, RE, Finite Automata
Complier Design - Operations on Languages, RE, Finite AutomataComplier Design - Operations on Languages, RE, Finite Automata
Complier Design - Operations on Languages, RE, Finite Automata
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Object Oriented Programming (OOP) using C++ - Lecture 5
Object Oriented Programming (OOP) using C++ - Lecture 5Object Oriented Programming (OOP) using C++ - Lecture 5
Object Oriented Programming (OOP) using C++ - Lecture 5
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Object Oriented Programming (OOP) using C++ - Lecture 2
Object Oriented Programming (OOP) using C++ - Lecture 2Object Oriented Programming (OOP) using C++ - Lecture 2
Object Oriented Programming (OOP) using C++ - Lecture 2
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Object Oriented Programming (OOP) using C++ - Lecture 1
Object Oriented Programming (OOP) using C++ - Lecture 1Object Oriented Programming (OOP) using C++ - Lecture 1
Object Oriented Programming (OOP) using C++ - Lecture 1
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Object Oriented Programming (OOP) using C++ - Lecture 3
Object Oriented Programming (OOP) using C++ - Lecture 3Object Oriented Programming (OOP) using C++ - Lecture 3
Object Oriented Programming (OOP) using C++ - Lecture 3
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Object Oriented Programming (OOP) using C++ - Lecture 4
Object Oriented Programming (OOP) using C++ - Lecture 4Object Oriented Programming (OOP) using C++ - Lecture 4
Object Oriented Programming (OOP) using C++ - Lecture 4
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Introduction to Operating System - Lecture 2
Introduction to Operating System - Lecture 2Introduction to Operating System - Lecture 2
Introduction to Operating System - Lecture 2
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Introduction to Operating System - Lecture 1
Introduction to Operating System - Lecture 1Introduction to Operating System - Lecture 1
Introduction to Operating System - Lecture 1
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Introduction to Operating System - Lecture 3
Introduction to Operating System - Lecture 3Introduction to Operating System - Lecture 3
Introduction to Operating System - Lecture 3
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Introduction to Freelancing - Quick Guide
Introduction to Freelancing - Quick GuideIntroduction to Freelancing - Quick Guide
Introduction to Freelancing - Quick Guide
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Ad

Recently uploaded (20)

seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Uses of drones in civil construction.pdf
Uses of drones in civil construction.pdfUses of drones in civil construction.pdf
Uses of drones in civil construction.pdf
surajsen1729
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink DisplayHow to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
CircuitDigest
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdfML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
rameshwarchintamani
 
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning ModelsMode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Journal of Soft Computing in Civil Engineering
 
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Journal of Soft Computing in Civil Engineering
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
Working with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to ImplementationWorking with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to Implementation
Alabama Transportation Assistance Program
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation RateModeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Journal of Soft Computing in Civil Engineering
 
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control
 
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Uses of drones in civil construction.pdf
Uses of drones in civil construction.pdfUses of drones in civil construction.pdf
Uses of drones in civil construction.pdf
surajsen1729
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink DisplayHow to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
CircuitDigest
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdfML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
rameshwarchintamani
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
Ad

Data Structures & Algorithms - Lecture 2

  • 1. Data Structures and Algorithms By Mohamed Gamal © Mohamed Gamal 2024 C++
  • 2. The topics of today’s lecture: Agenda
  • 4. Searching Algorithms – Searching is to find the location of specific value in a list of data elements. – Searching methods can be divided into two categories: • Searching methods for both sorted and unsorted lists. – e.g., Linear (or Sequential) Search. • Searching methods for sorted lists. – e.g., Binary Search. – Direct access by key value (hashing).
  • 6. 1) Linear (or Sequential) Search – Linear Search is one of the simplest search algorithms to understand and implement. – It can be used on both sorted and unsorted data. – The algorithm checks each element in the array or list sequentially until the desired element is found or the end of the list is reached. target
  • 7. Linear Search Algorithm 1) Start from the beginning of the list or array. 2) Iterate through each element sequentially. 3) Compare each element with the target value. 4) If the element matches the target value, return its index (or position). 5) If the end of the list is reached without finding the target value, return a message indicating the target value is not in the list. 6) Stop. 2 8 5 3 9 4
  • 8. 2 8 5 3 9 4 2 8 5 3 9 4 2 8 5 3 9 4 2 8 5 3 9 4 2 8 5 3 9 4 2 8 5 3 9 4 Initial Unsorted List Example: Target: find 9 1st Iteration 2nd Iteration 3rd Iteration 4th Iteration 5th Iteration 2 == 9 ? 8 == 9 ? 5 == 9 ? 3 == 9 ? 9 == 9 ? Target
  • 9. #include <iostream> using namespace std; int linearSearch(int arr[], int n, int target) { for (int i = 0; i < n; i++) if (arr[i] == target) return i; return -1; } void print(int arr[], int n) { cout << "Index: "; for (int i = 0; i < n; i++) cout << i << " "; cout << endl; cout << "Array: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; } int main(void) { int arr[] = { 2, 8, 5, 3, 9, 4 }; int n = sizeof(arr) / sizeof(arr[0]); int val = 9; int index = linearSearch(arr, n, val); print(arr, n); if (index != -1) cout << "nFound " << arr[index] << " at index " << index << endl; else cout << "nNot found!" << endl; return 0; } Linear Search C++ implementation
  • 10. Complexity: ▪ Worst Case: 𝑂(𝑛) ▪ Average Case: 𝑂(𝑛) ▪ Best Case: 𝑂(1) Advantages: ▪ The algorithm is straightforward to implement with minimal code. ▪ Linear Search can be applied to any data structure that supports sequential access, such as arrays, linked lists, and even files. ▪ Linear Search is an in-place search algorithm and does not require any additional memory. Disadvantages: ▪ It is inefficient for large datasets because it may require examining each element.
  • 12. 2) Binary Search – It uses the divide-and-conquer approach to reduce the search space by half with each comparison. – Binary Search continually divides the search interval in half and compares the middle element with the target value. – Binary Search requires the data to be sorted in ascending or descending order before searching. – Binary Search can be implemented recursively or iteratively. 1 2 3 4 5 8 9 Right or Low 0 1 2 3 4 5 6 Left or High Sorted List Mid (Low + High) / 2
  • 13. Binary Search Algorithm 1) Let min = 0 and max = n – 1, where n is the number of elements in the sorted array. 2) Compute the midpoint: a) mid = (low + high) / 2 3) Compare the target value with the element at the midpoint: a) If target == array[mid], return mid (target found). b) If target < array[mid], set high = mid - 1 (discard the right half). c) If target > array[mid], set low = mid + 1 (discard the left half). 4) Repeat steps 2-3 until low <= high. 5) If the target value is not found after the loop, return a message indicating it is not in the array.
  • 14. Initial Sorted List Example: Target: find 3 2nd Iteration 3rd Iteration Target = arr mid ? Return mid (target found). Target < arr[mid] ? Set high = mid - 1 (discard the right half). Target > arr[mid] ? Set low = mid + 1 (discard the left half). Target Mid High 1 2 3 4 5 8 9 1 2 3 4 5 8 9 1 2 3 4 5 8 9 Low High Mid Low High Mid Low No. Iterations = log2 7 = 3 iterations 1st Iteration
  • 15. Mid High 1 2 3 4 5 8 9 13 17 20 30 Low Mid High 1 2 3 4 5 8 9 13 17 20 30 Low Mid High 1 2 3 4 5 8 9 13 17 20 30 Low Target = 20 Mid (High + Low) / 2 Target = arr mid ? Return mid (target found). Target < arr[mid] ? Set high = mid - 1 (discard the right half). Target > arr[mid] ? Set low = mid + 1 (discard the left half). 1 2 3 4 5 8 9 13 17 20 30 Mid High Low
  • 16. Given the list { 1, 2, 3, 4, 5, 8, 9 } and the target value 3 1. First iteration: 1. Calculate mid: mid = (0 + 6) / 2 = 3 (integer division) 2. Compare target 3 with the middle element 4 3. Since 3 < 4, update right boundary: High = 3 − 1 = 2 2. Second iteration: 1. Calculate new mid: mid = (0 + 2) / 2 = 1 2. Compare target 3 with middle element 2 3. Since 3 > 2, update low boundary: Low = 1 + 1 = 2 3. Third iteration: 1. Calculate new mid: mid = (2 + 2) / 2 = 2 2. Compare target 3 with element at index 2: 3 3. Target found at index 2 Thus, it took 3 iterations to find the target value 3. Target Mid High 1 2 3 4 5 8 9 Low High Mid Low High Mid Low 1 2 3 4 5 8 9 1 2 3 4 5 8 9
  • 17. #include <iostream> using namespace std; int binarySearch(int arr[], int n, int target) { int low = 0; int high = n - 1; while (low <= high) { int mid = (low + high) / 2; // Check if target is present at mid if (arr[mid] == target) return mid; // If target greater, ignore left half else if (target > arr[mid]) low = mid + 1; // If target is smaller, ignore right half else high = mid - 1; } return -1; } int main() { int arr[] = { 1, 2, 3, 4, 5, 8, 9 }; int n = sizeof(arr) / sizeof(arr[0]); int val = 3; int index = binarySearch(arr, n, val); if (index != -1) cout << "Element found at index " << index << endl; else cout << "Element not found in the array" << endl; return 0; } Binary Search C++ implementation
  • 18. Complexity: ▪ Worst Case: 𝑂(log 𝑛) ▪ Average Case: 𝑂 log 𝑛 ▪ Best Case: 𝑂(1) — at the middle. Advantages: ▪ The algorithm is relatively simple to implement, especially in its iterative form, and easy to understand. ▪ Highly efficient and faster for searching large sorted datasets. ▪ It does not require additional memory beyond a few variables for indices. Disadvantages: ▪ If the data is not sorted, Binary Search cannot be applied directly. Sorting the data first would incur additional time complexity. ▪ Binary Search is not efficient for data structures that do not support random access, such as linked lists, because it relies on indexing to access the middle element. ▪ While the iterative version is simple, the recursive version can be more complex due to additional overhead from recursive function calls, which use more stack space. ▪ For small datasets, the overhead of repeatedly dividing the search space and comparing elements might make it slower compared to a simpler linear search.
  • 20. Russian Peasant Multiplication – Russian Peasant Multiplication, also known as Egyptian Multiplication, is an ancient algorithm for multiplying two numbers. – Its origins date back to ancient civilizations, including the Egyptians and Russians, who used this method for its simplicity and ease of use with basic arithmetic operations. – The algorithm's beauty lies in its use of only basic operations: addition, halving, and doubling.
  • 21. Russian Peasant Multiplication — Algorithm 1) Let the two given numbers be 'a' and 'b'. 2) Initialize result 'res' as 0. 3) Do the following while 'b' is greater than 0 a) If 'b' is odd, add 'a' to 'res' b) Double 'a' and halve 'b' 4) Return 'res'.
  • 22. ÷ 2 × 2 𝑎 𝑏 Cancel every row with even numbers. 𝑎 𝑏
  • 23. Add other rows with odd numbers. 𝑎 𝑏
  • 25. int russianMult(int a, int b) { int res = 0; while (a > 0) { // if 'a' is odd, add 'b’ to 'res' if (a % 2 != 0) res += b; a = a >> 1; // halve a b = b << 1; // double b } return res; } Method #1
  • 26. int russianMult(int a, int b) { int res = 0; while (a > 0) { // if 'a' is odd, add 'b' to res if (a % 2 != 0) res += b; a /= 2; // halve a b *= 2; // double b } return res; } Method #2
  • 27. Complexity: ▪ Worst Case: O log 𝑎 – 𝑎 is a large integer. ▪ Average Case: O log 𝑎 – 𝑎 is a random integer. ▪ Best Case: O(log 𝑎)– 𝑎 is a very small integer. Advantages: ▪ Simplicity – easy to implement with basic arithmetic and bitwise operations. ▪ Efficiency – logarithmic time complexity, scalable for large values. Disadvantages: ▪ Less practical compared to modern optimized multiplication algorithms. ▪ No Built-In Hardware Optimization – relies on software-based arithmetic without direct hardware support.
  • 29. – There’re two formulas for calculating the day of the week for a given date. » Zeller’s Congruence » Key-Value Method – Both the methods work for the Gregorian calendar. Introduction Christian Zeller
  • 31. Saturday Sunday Monday Tuesday Wednesday Thursday Friday 0 1 2 3 4 5 6 Mar Apr May Jun Jul Aug Sep Oct Nov Dec Jan Feb 3 4 5 6 7 8 9 10 11 12 13 14 Days Chart: Months Chart:
  • 32. 1) Zeller’s Congruence where, day of the week corresponding month number 𝑥 is the required day of the week. 𝑑 is the given day of the month. 𝑚 is the corresponding month number. 𝐾 is the year of century (i.e., last two digits of the given year). 𝐽 is the century (i.e., first two digits of the year.) 𝑥 = 𝑑 + 13 𝑚 + 1 5 + 𝐾 + 𝐾 4 + 𝐽 4 + 5𝐽 mod 7 𝐾 = 𝑦𝑒𝑎𝑟 % 100 𝐽 = Τ 𝑦𝑒𝑎𝑟 100 Note: an exception for both January and February, subtract 1 from the given year.
  • 33. Example: calculate the day for the date 1st April 1983. 𝑑 = 1, 𝑚 = 4, 𝐾 = 1983 % 100 = 83, 𝐽 = Τ 1983 100 = 19 1st April 1983 𝐾 𝐽 𝑚 𝑑 𝑥 = 1 + 13 4 + 1 5 + 83 + 83 4 + 19 4 + 5 19 mod 7 = 216 mod 7 = 6 6 is Friday according to the days chart. 𝑥 = 𝑑 + 13 𝑚 + 1 5 + 𝐾 + 𝐾 4 + 𝐽 4 + 5𝐽 mod 7 𝐾 = 𝑦𝑒𝑎𝑟 % 100 𝐽 = Τ 𝑦𝑒𝑎𝑟 100
  • 34. Example: calculate the day for the date 2nd March 2004. 𝑥 = 2 + 13 3 + 1 5 + 4 + 4 4 + 20 4 + 5 20 mod 7 = 122 mod 7 = 3 3 is Tuesday according to the days chart. 𝑥 = 𝑑 + 13 𝑚 + 1 5 + 𝐾 + 𝐾 4 + 𝐽 4 + 5𝐽 mod 7 2nd March 2004 𝐾 𝐽 𝑚 𝑑 𝑑 = 2, 𝑚 = 3, 𝐾 = 2004 % 100 = 04, 𝐽 = Τ 2004 100 = 20 𝐾 = 𝑦𝑒𝑎𝑟 % 100 𝐽 = Τ 𝑦𝑒𝑎𝑟 100
  • 35. Example: calculate the day for the date 27th February 2023. 27th February 2023 𝐾 𝐽 𝑚 𝑑 𝑥 = 𝑑 + 13 𝑚 + 1 5 + 𝐾 + 𝐾 4 + 𝐽 4 + 5𝐽 mod 7 𝑥 = 27 + 13 14 + 1 5 + 22 + 22 4 + 20 4 + 5 20 mod 7 = 198 mod 7 = 2 2 is Monday according to the days chart. 𝑑 = 27, 𝑚 = 14, 𝐾 = (2023 − 1) % 100 = 22, 𝐽 = Τ (2023 − 1) 100 = 20 𝐾 = 𝑦𝑒𝑎𝑟 % 100 𝐽 = Τ 𝑦𝑒𝑎𝑟 100 Exception for February !
  • 38. 2) Key-Value Method where, day of the week month key value number century key value 𝑥 is the required day of the week. 𝑑 is the given day of the month. 𝑚 is the month key value number. 𝐾 is the year of century (i.e., last two digits of the given year). 𝐽 is the century key value (e.g., 0 for the 1900s) 𝑥 = 𝑑 + 𝑚 + 𝐾 + 𝐾 4 + 𝐽 mod 7
  • 39. Saturday Sunday Monday Tuesday Wednesday Thursday Friday 0 1 2 3 4 5 6 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 1 4 4 0 2 5 0 3 6 1 4 6 Days Chart: Months Key-Value Chart: 1400-1499 2 1500-1599 0 1600-1699 6 1700-1799 4 Century Key-Value Chart: 1800-1899 2 1900-1999 0 2000-2099 6 2100-2199 4 2200-2299 2 2300-2399 0 2400-2499 6 2500-2599 4 …
  • 40. Example: calculate the day for the date 1st April 1983. 𝑑 = 1, 𝑚 = 0, 𝐾 = 83, 𝐽 = 0 1st April 1983 𝐾 𝐽 𝑚 𝑑 𝑥 = 1 + 0 + 83 + 83 4 + 0 mod 7 = 104 mod 7 = 6 6 is Friday according to the days chart. 𝑥 = 𝑑 + 𝑚 + 𝐾 + 𝐾 4 + 𝐽 mod 7 Months Chart Century Chart
  • 41. Example: calculate the day for the date 2nd March 2004. 𝑑 = 2, 𝑚 = 4, 𝐾 = 04, 𝐽 = 6 𝑥 = 2 + 4 + 4 + 4 4 + 6 mod 7 = 17 mod 7 = 3 𝑥 = 𝑑 + 𝑚 + 𝐾 + 𝐾 4 + 𝐽 mod 7 Months Chart Century Chart 3 is Tuesday according to the days chart. 2nd March 2004 𝐾 𝐽 𝑚 𝑑
  • 42. Example: calculate the day for the date 27th February 2023. 𝑑 = 27, 𝑚 = 4, 𝐾 = 23, 𝐽 = 6 𝑥 = 27 + 4 + 23 + 23 4 + 6 mod 7 = 65 mod 7 = 2 𝑥 = 𝑑 + 𝑚 + 𝐾 + 𝐾 4 + 𝐽 mod 7 Months Chart Century Chart 2 is Monday according to the days chart. 27th February 2023 𝐾 𝐽 𝑚 𝑑
  • 44. Graph are versatile data structures used in many real-world applications. Graphs Theory Concept Social Networks • Nodes: People. • Edges: connection. • Application: suggesting friends based on shortest path (i.e., nearby located friends). Computer Networks • Nodes: Computers or Routers. • Edges: Network connections. • Application: finding the shortest route for data packets. Ahmed Ali Saad Omar 3 1 5
  • 45. Basic Terminology 1 2 4 3 1 : Node or Vertex : Directed Edge : Undirected Edge : Weighted Edge (i.e., includes cost) n 1 : Loop 1 2 4 3 5
  • 46. Graph Directed Undirected Weighted Unweighted Weighted Unweighted Positive Negative Positive Negative Graph Types
  • 48. Adjacency Matrix: an adjacency matrix is a square matrix, or you can say it is a 2D array, and the elements of the matrix indicate whether pairs of vertices are adjacent or not in the graph. Graph Representation 1 2 4 3 5 1 2 3 4 5 1 0 1 0 0 0 2 0 0 0 0 1 3 1 0 0 0 0 4 1 0 1 0 0 5 0 0 0 1 0
  • 49. #include <iostream> using namespace std; void print(int arr[][5], int rows, int cols); int main(void) { int graph[][5] = { { 0, 1, 0, 0, 0 }, { 0, 0, 0, 0, 1 }, { 1, 0, 0, 0, 0 }, { 1, 0, 1, 0, 0 }, { 0, 0, 0, 1, 0 } }; int rows = sizeof(graph) / sizeof(graph[0]); int cols = sizeof(graph[0]) / sizeof(graph[0][0]); print(graph, rows, cols); return 0; } void print(int arr[][5], int rows, int cols) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) cout << arr[i][j] << " "; cout << endl; } } C++ implementation
  • 50. – Uninformed (blind) strategies use only the information available in the problem definition. – These strategies order nodes without using any domain specific information (Blind). – Contrary to Informed (heuristic) search techniques which might have additional information. • Breadth-first search (BFS) • Depth-first search (DFS) • Depth-limited search (DLS) • Iterative deepening search (IDS) • … • etc. Unguided/Blind search Uninformed (Blind) Search Strategies
  • 51. Complete? Yes Optimal? Yes, if path cost is nondecreasing function of depth Time Complexity: O(bd) Space Complexity: O(bd), note that every node in the fringe is kept in the queue
  • 52. Breadth First Search (BFS) ◼ Application 1: Given the following state space (tree search), give the sequence of visited nodes when using BFS (assume that the node O is the goal state). A B C E D F G H I J K L O M N
  • 53. ◼ A, A B C E D Breadth First Search (BFS)
  • 54. ◼ A, ◼ B, A B C E D F G Breadth First Search (BFS)
  • 55. ◼ A, ◼ B,C A B C E D F G H Breadth First Search (BFS)
  • 56. ◼ A, ◼ B,C,D A B C E D F G H I J Breadth First Search (BFS)
  • 57. ◼ A, ◼ B,C,D,E A B C E D F G H I J Breadth First Search (BFS)
  • 58. ◼ A, ◼ B,C,D,E, ◼ F, A B C E D F G H I J Breadth First Search (BFS)
  • 59. ◼ A, ◼ B,C,D,E, ◼ F,G A B C E D F G H I J K L Breadth First Search (BFS)
  • 60. ◼ A, ◼ B,C,D,E, ◼ F,G,H A B C E D F G H I J K L Breadth First Search (BFS)
  • 61. ◼ A, ◼ B,C,D,E, ◼ F,G,H,I A B C E D F G H I J K L M Breadth First Search (BFS)
  • 62. ◼ A, ◼ B,C,D,E, ◼ F,G,H,I,J, A B C E D F G H I J K L M N Breadth First Search (BFS)
  • 63. ◼ A, ◼ B,C,D,E, ◼ F,G,H,I,J, ◼ K, A B C E D F G H I J K L M N Breadth First Search (BFS)
  • 64. ◼ A, ◼ B,C,D,E, ◼ F,G,H,I,J, ◼ K,L A B C E D F G H I J K L O M N Breadth First Search (BFS)
  • 65. ◼ A, ◼ B,C,D,E, ◼ F,G,H,I,J, ◼ K,L,M, A B C E D F G H I J K L O M N Breadth First Search (BFS)
  • 66. ◼ A, ◼ B,C,D,E, ◼ F,G,H,I,J, ◼ K,L,M,N, A B C E D F G H I J K L O M N Breadth First Search (BFS)
  • 67. ◼ A, ◼ B,C,D,E, ◼ F,G,H,I,J, ◼ K,L,M,N, ◼ Goal state: O A B C E D F G H I J K L O M N Breadth First Search (BFS)
  • 68. ◼ The returned solution is the sequence of operators in the path: A, B, G, L, O A B C E D F G H I J K L O M N Breadth First Search (BFS)
  • 69. Complete? No Optimal? No Time Complexity: O(bm) Space Complexity: O(b × m)
  • 70. ◼ Application 2: Given the following state space (tree search), give the sequence of visited nodes when using DFS (assume that the node O is the goal state). A B C E D F G H I J K L O M N Depth First Search (DFS)
  • 71. ◼ A, A B C E D Depth First Search (DFS)
  • 72. ◼ A,B, A B C E D F G Depth First Search (DFS)
  • 73. ◼ A,B,F, A B C E D F G Depth First Search (DFS)
  • 74. ◼ A,B,F, ◼ G, A B C E D F G K L Depth First Search (DFS)
  • 75. ◼ A,B,F, ◼ G,K, A B C E D F G K L Depth First Search (DFS)
  • 76. ◼ A,B,F, ◼ G,K, ◼ L, A B C E D F G K L O Depth First Search (DFS)
  • 77. ◼ A,B,F, ◼ G,K, ◼ L, O: Goal State A B C E D F G K L O Depth First Search (DFS)
  • 78. A B C E D F G K L O ◼ The returned solution is the sequence of operators in the path: A, B, G, L, O Depth First Search (DFS)
  • 79. Complete? Yes, if there is a goal state at a depth less than L Optimal? No Time Complexity: O(bL) Space Complexity: O(bL)
  • 80. ◼ Application 3: Given the following state space (tree search), give the sequence of visited nodes when using DLS (Limit = 2). A B C E D F G H I J K L O M N Limit = 0 Limit = 1 Limit = 2 Depth-Limited Search (DLS)
  • 81. ◼ A, A B C E D Limit = 2 Limit = 0 Limit = 1 Depth-Limited Search (DLS)
  • 82. ◼ A,B, A B C E D F G Limit = 2 Limit = 0 Limit = 1 Limit = 2 Depth-Limited Search (DLS)
  • 83. ◼ A,B,F, A B C E D F G Limit = 2 Limit = 0 Limit = 1 Limit = 2 Depth-Limited Search (DLS)
  • 84. ◼ A,B,F, ◼ G, A B C E D F G Limit = 2 Limit = 0 Limit = 1 Limit = 2 Depth-Limited Search (DLS)
  • 85. ◼ A,B,F, ◼ G, ◼ C, A B C E D F G H Limit = 2 Limit = 0 Limit = 1 Limit = 2 Depth-Limited Search (DLS)
  • 86. ◼ A,B,F, ◼ G, ◼ C,H, A B C E D F G H Limit = 2 Limit = 0 Limit = 1 Limit = 2 Depth-Limited Search (DLS)
  • 87. ◼ A,B,F, ◼ G, ◼ C,H, ◼ D, A B C E D F G H I J Limit = 2 Limit = 0 Limit = 1 Limit = 2 Depth-Limited Search (DLS)
  • 88. ◼ A,B,F, ◼ G, ◼ C,H, ◼ D,I A B C E D F G H I J Limit = 2 Limit = 0 Limit = 1 Limit = 2 Depth-Limited Search (DLS)
  • 89. ◼ A,B,F, ◼ G, ◼ C,H, ◼ D,I ◼ J, A B C E D F G H I J Limit = 2 Limit = 0 Limit = 1 Limit = 2 Depth-Limited Search (DLS)
  • 90. ◼ A,B,F, ◼ G, ◼ C,H, ◼ D,I ◼ J, ◼ E A B C E D F G H I J Limit = 2 Limit = 0 Limit = 1 Limit = 2 Depth-Limited Search (DLS)
  • 91. ◼ A,B,F, ◼ G, ◼ C,H, ◼ D,I ◼ J, ◼ E, Failure A B C E D F G H I J Limit = 2 Limit = 2 Depth-Limited Search (DLS)
  • 92. ◼ DLS algorithm returns Failure (no solution) ◼ The reason is that the goal is beyond the limit (Limit =2): the goal depth is (d=4) A B C E D F G H I J K L O M N Limit = 2 Solution: use IDS! Depth-Limited Search (DLS)
  • 93. Complete? Yes Optimal? Yes Time Complexity: O(bd) Space Complexity: O(bd)
  • 94. ◼ Application 4: Given the following state space (tree search), give the sequence of visited nodes when using IDS. A B C E D F G H I J K L O M N Limit = 0 Limit = 1 Limit = 2 Limit = 3 Limit = 4 Iterative Deepening Search (IDS)
  • 95. Iterative Deepening Search (IDS) DLS with bound = 0
  • 96. ◼ A, A Limit = 0 Iterative Deepening Search (IDS)
  • 97. ◼ A, Failure A Limit = 0 Iterative Deepening Search (IDS)
  • 98. Iterative Deepening Search (IDS) DLS with bound = 1
  • 99. ◼ A, A B C E D Limit = 1 Iterative Deepening Search (IDS)
  • 100. ◼ A,B, A B C E D Limit = 1 Iterative Deepening Search (IDS)
  • 101. ◼ A,B, ◼ C, A B C E D Limit = 1 Iterative Deepening Search (IDS)
  • 102. ◼ A,B, ◼ C, ◼ D, A B C E D Limit = 1 Iterative Deepening Search (IDS)
  • 103. ◼ A,B ◼ C, ◼ D, ◼ E, A B C E D Limit = 1 Iterative Deepening Search (IDS)
  • 104. ◼ A,B, ◼ C, ◼ D, ◼ E, Failure A B C E D Limit = 1 Iterative Deepening Search (IDS)
  • 105. Iterative Deepening Search (IDS) DLS with bound = 2
  • 106. ◼ A, A B C E D Limit = 2 Iterative Deepening Search (IDS)
  • 107. ◼ A,B, A B C E D F G Limit = 2 Iterative Deepening Search (IDS)
  • 108. ◼ A,B,F, A B C E D F G Limit = 2 Iterative Deepening Search (IDS)
  • 109. ◼ A,B,F, ◼ G, A B C E D F G Limit = 2 Iterative Deepening Search (IDS)
  • 110. ◼ A,B,F, ◼ G, ◼ C, A B C E D F G H Limit = 2 Iterative Deepening Search (IDS)
  • 111. ◼ A,B,F, ◼ G, ◼ C,H, A B C E D F G H Limit = 2 Iterative Deepening Search (IDS)
  • 112. ◼ A,B,F, ◼ G, ◼ C,H, ◼ D, A B C E D F G H I J Limit = 2 Iterative Deepening Search (IDS)
  • 113. ◼ A,B,F, ◼ G, ◼ C,H, ◼ D,I A B C E D F G H I J Limit = 2 Iterative Deepening Search (IDS)
  • 114. ◼ A,B,F, ◼ G, ◼ C,H, ◼ D,I ◼ J, A B C E D F G H I J Limit = 2 Iterative Deepening Search (IDS)
  • 115. ◼ A,B,F, ◼ G, ◼ C,H, ◼ D,I ◼ J, ◼ E A B C E D F G H I J Limit = 2 Iterative Deepening Search (IDS)
  • 116. ◼ A,B,F, ◼ G, ◼ C,H, ◼ D,I ◼ J, ◼ E, Failure A B C E D F G H I J K L O M N Limit = 2 Iterative Deepening Search (IDS)
  • 117. Iterative Deepening Search (IDS) DLS with bound = 3
  • 118. ◼ A, A B C E D Limit = 3 Iterative Deepening Search (IDS)
  • 119. ◼ A,B, A B C E D F G Limit = 3 Iterative Deepening Search (IDS)
  • 120. ◼ A,B,F, A B C E D F G Limit = 3 Iterative Deepening Search (IDS)
  • 121. ◼ A,B,F, ◼ G, A B C E D F G K L Limit = 3 Iterative Deepening Search (IDS)
  • 122. ◼ A,B,F, ◼ G,K, A B C E D F G K L Limit = 3 Iterative Deepening Search (IDS)
  • 123. ◼ A,B,F, ◼ G,K, ◼ L, A B C E D F G K L Limit = 3 Iterative Deepening Search (IDS)
  • 124. ◼ A,B,F, ◼ G,K, ◼ L, ◼ C, A B C E D F G H K L Limit = 3 Iterative Deepening Search (IDS)
  • 125. ◼ A,B,F, ◼ G,K, ◼ L, ◼ C,H, A B C E D F G H K L Limit = 3 Iterative Deepening Search (IDS)
  • 126. ◼ A,B,F, ◼ G,K, ◼ L, ◼ C,H, ◼ D, A B C E D F G H I J K L Limit = 3 Iterative Deepening Search (IDS)
  • 127. ◼ A,B,F, ◼ G,K, ◼ L, ◼ C,H, ◼ D,I, A B C E D F G H I J K L M Limit = 3 Iterative Deepening Search (IDS)
  • 128. ◼ A,B,F, ◼ G,K, ◼ L, ◼ C,H, ◼ D,I,M, A B C E D F G H I J K L M Limit = 3 Iterative Deepening Search (IDS)
  • 129. ◼ A,B,F, ◼ G,K, ◼ L, ◼ C,H, ◼ D,I,M, ◼ J, A B C E D F G H I J K L M N Limit = 3 Iterative Deepening Search (IDS)
  • 130. ◼ A,B,F, ◼ G,K, ◼ L, ◼ C,H, ◼ D,I,M, ◼ J,N, A B C E D F G H I J K L M N Limit = 3 Iterative Deepening Search (IDS)
  • 131. ◼ A,B,F, ◼ G,K, ◼ L, ◼ C,H, ◼ D,I,M, ◼ J,N, ◼ E, A B C E D F G H I J K L M N Limit = 3 Iterative Deepening Search (IDS)
  • 132. ◼ A,B,F, ◼ G,K, ◼ L, ◼ C,H, ◼ D,I,M, ◼ J,N, ◼ E,Failure A B C E D F G H I J K L O M N Limit = 3 Iterative Deepening Search (IDS)
  • 133. Iterative Deepening Search (IDS) DLS with bound = 4
  • 134. ◼ A, A B C E D Limit = 4 Iterative Deepening Search (IDS)
  • 135. ◼ A,B, A B C E D F G Limit = 4 Iterative Deepening Search (IDS)
  • 136. ◼ A,B,F, A B C E D F G Limit = 4 Iterative Deepening Search (IDS)
  • 137. ◼ A,B,F, ◼ G, A B C E D F G K L Limit = 4 Iterative Deepening Search (IDS)
  • 138. ◼ A,B,F, ◼ G,K, A B C E D F G K L Limit = 4 Iterative Deepening Search (IDS)
  • 139. ◼ A,B,F, ◼ G,K, ◼ L, A B C E D F G K L O Limit = 4 Iterative Deepening Search (IDS)
  • 140. ◼ A,B,F, ◼ G,K, ◼ L, O: Goal State A B C E D F G K L O Limit = 4 Iterative Deepening Search (IDS)
  • 141. The returned solution is the sequence of operators in the path: A, B, G, L, O A B C E D F G K L O Iterative Deepening Search (IDS)
  • 143. • Minimax uses DFS to evaluate nodes. • Perfect play for deterministic games. • Idea: choose a move to position with highest minimax value (i.e., best achievable payoff against best play. • e.g., 2-play games. • Minimax Visualizer Minimax Algorithm
  • 144. Game tree (2-player, deterministic, turns) Tic Tac Teo
  • 157. #include <iostream> using namespace std; int log2(int n); int max(int a, int b); int min(int a, int b); int minimax(int depth, int nodeIndex, bool isMax, int scores[], int h); int main() { // The number of elements in scores must be a power of 2 int scores[] = { 84, -29, -37, -25, 1, -43, -75, 49, -21, -51, 58, -46, -3, -13, 26, 79 }; int n = sizeof(scores) / sizeof(scores[0]); int height = log2(n); int res = minimax(0, 0, true, scores, height); cout << "The optimal value is " << res << endl; return 0; } /* depth: current depth in game tree. nodeIndex: index of current node in scores[]. isMax: true if current move is of maximizer, else false. scores[]: stores leaves of Game tree. h: maximum height of Game tree. */ int minimax(int depth, int nodeIndex, bool isMax, int scores[], int h) { // terminating condition (i.e leaf node is reached) if (depth == h) return scores[nodeIndex]; // if current move is maximizer, find the maximum attainable value if (isMax) return max(minimax(depth + 1, nodeIndex * 2, false, scores, h), minimax(depth + 1, nodeIndex * 2 + 1, false, scores, h)); // else (if current move is Minimizer), find the minimum attainable value else return min(minimax(depth + 1, nodeIndex * 2, true, scores, h), minimax(depth + 1, nodeIndex * 2 + 1, true, scores, h)); } // function to find Log n in base 2 using recursion int log2(int n) { return (n == 1) ? 0 : 1 + log2(n / 2); } // maximum element function int max(int a, int b) { return (a > b) ? a : b; } // minimum element function int min(int a, int b) { return (a < b) ? a : b; } Minimax C++ implementation
  • 158. – Time complexity: O(bm) – Space complexity: O(bm) Mini-Max Properties Minimax uses DFS to evaluate nodes ! Same as DFS
  • 159. function minimax(node, depth, maximizingPlayer): if depth = 0 or node is a terminal node: return the heuristic value of the node if maximizingPlayer: bestValue = -infinity for each child node of node: v = minimax(child, depth - 1, FALSE) bestValue = max(bestValue, v) return bestValue else: bestValue = +infinity for each child node of node: v = minimax(child, depth - 1, TRUE) bestValue = min(bestValue, v) return bestValue Minimax Algorithm
  • 160. Tic Tac Teo Game using Minimax algorithm
  • 161. End of lecture 2 ThankYou!
  翻译: