Data Structures & Algorithms - Lecture 2

1mohamedgamal54 146 views 161 slides Sep 09, 2024
Slide 1
Slide 1 of 161
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89
Slide 90
90
Slide 91
91
Slide 92
92
Slide 93
93
Slide 94
94
Slide 95
95
Slide 96
96
Slide 97
97
Slide 98
98
Slide 99
99
Slide 100
100
Slide 101
101
Slide 102
102
Slide 103
103
Slide 104
104
Slide 105
105
Slide 106
106
Slide 107
107
Slide 108
108
Slide 109
109
Slide 110
110
Slide 111
111
Slide 112
112
Slide 113
113
Slide 114
114
Slide 115
115
Slide 116
116
Slide 117
117
Slide 118
118
Slide 119
119
Slide 120
120
Slide 121
121
Slide 122
122
Slide 123
123
Slide 124
124
Slide 125
125
Slide 126
126
Slide 127
127
Slide 128
128
Slide 129
129
Slide 130
130
Slide 131
131
Slide 132
132
Slide 133
133
Slide 134
134
Slide 135
135
Slide 136
136
Slide 137
137
Slide 138
138
Slide 139
139
Slide 140
140
Slide 141
141
Slide 142
142
Slide 143
143
Slide 144
144
Slide 145
145
Slide 146
146
Slide 147
147
Slide 148
148
Slide 149
149
Slide 150
150
Slide 151
151
Slide 152
152
Slide 153
153
Slide 154
154
Slide 155
155
Slide 156
156
Slide 157
157
Slide 158
158
Slide 159
159
Slide 160
160
Slide 161
161

About This Presentation

Data Structures and Algorithms using C++ programming language.


Slide Content

Data Structures and
Algorithms
By Mohamed Gamal
© Mohamed Gamal 2024
C++

The topics of today’s lecture:
Agenda

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

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
1
st
Iteration
2
nd
Iteration
3
rd
Iteration
4
th
Iteration
5
th
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(inti = 0; i < n; i++)
if (arr[i] == target)
return i;
return -1;
}
void print(int arr[], int n)
{
cout << "Index: ";
for(inti = 0; i < n; i++)
cout << i << " ";
cout << endl;
cout << "Array: ";
for(inti = 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: ??????(&#3627408475;)
▪Average Case: ??????(&#3627408475;)
▪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.

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
2
nd
Iteration
3
rd
Iteration
Target = arrmid ?
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 HighMid
Low
High
Mid
Low
No. Iterations =log
27=3 iterations
1
st
Iteration

Mid High
1 2 3 4 5 8 913172030
Low
Mid High
1 2 3 4 5 8 913172030
Low
Mid
High
1 2 3 4 5 8 913172030
Low
Target = 20
Mid
(High + Low) / 2
Target = arrmid ?
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 913172030
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
1234589
Low HighMid
Low
High
Mid
Low
1234589
1234589

#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&#3627408475;)
▪Average Case: ??????log&#3627408475;
▪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
&#3627408462;&#3627408463;
Cancel every row with even numbers.
&#3627408462;&#3627408463;

Add other rows with odd numbers.
&#3627408462;&#3627408463;

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; // doubleb
}
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: Olog&#3627408462; – &#3627408462; is a large integer.
▪Average Case: Olog&#3627408462; – &#3627408462; is a random integer.
▪Best Case: O(log&#3627408462;) – &#3627408462; 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.

–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

Saturday Sunday Monday Tuesday Wednesday Thursday Friday
0 1 2 3 4 5 6
Mar Apr May Jun JulAug 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,
da y of the week
corresponding month number
&#3627408485;isthe required day of the week.
&#3627408465;is the given day of the month.
&#3627408474;is the corresponding month number.
&#3627408446;is the year of century (i.e., last two digits of the given year).
&#3627408445;is the century (i.e., first two digits of the year.)
&#3627408485; =&#3627408465;+
13&#3627408474;+1
5
+&#3627408446;+
&#3627408446;
4
+
&#3627408445;
4
+5&#3627408445; mod 7
&#3627408446; = &#3627408486;&#3627408466;&#3627408462;?????? % 100
&#3627408445; = Τ&#3627408486;&#3627408466;&#3627408462;??????100
Note: an exception for both January and February, subtract 1 from the given year.

Example: calculate the day for the date 1
st
April 1983.
&#3627408465;=1, &#3627408474;=4, &#3627408446;=1983 % 100=83, &#3627408445;= Τ1983100=19
1
st
April 19 83
&#3627408446;&#3627408445;&#3627408474;&#3627408465;
&#3627408485; =1+
134+1
5
+83+
83
4
+
19
4
+519 mod 7
=216 mod 7
=6
6 is Friday according to the days chart.
&#3627408485; =&#3627408465;+
13&#3627408474;+1
5
+&#3627408446;+
&#3627408446;
4
+
&#3627408445;
4
+5&#3627408445; mod 7
&#3627408446; = &#3627408486;&#3627408466;&#3627408462;?????? % 100
&#3627408445; = Τ&#3627408486;&#3627408466;&#3627408462;??????100

Example: calculate the day for the date 2
nd
March 2004.
&#3627408485; =2+
133+1
5
+4+
4
4
+
20
4
+520 mod 7
=122 mod 7
=3
3 is Tuesday according to the days chart.
&#3627408485; =&#3627408465;+
13&#3627408474;+1
5
+&#3627408446;+
&#3627408446;
4
+
&#3627408445;
4
+5&#3627408445; mod 7
2
nd
March 20 04
&#3627408446;&#3627408445;&#3627408474;&#3627408465;
&#3627408465;=2, &#3627408474;=3, &#3627408446;=2004 % 100=04, &#3627408445;= Τ2004100=20
&#3627408446; = &#3627408486;&#3627408466;&#3627408462;?????? % 100
&#3627408445; = Τ&#3627408486;&#3627408466;&#3627408462;??????100

Example: calculate the day for the date 27
th
February 2023.
27
th
February 20 23
&#3627408446;&#3627408445;&#3627408474;&#3627408465;
&#3627408485; =&#3627408465;+
13&#3627408474;+1
5
+&#3627408446;+
&#3627408446;
4
+
&#3627408445;
4
+5&#3627408445; mod 7
&#3627408485; =27+
1314+1
5
+22+
22
4
+
20
4
+520 mod 7
=198 mod 7
=2
2 is Monday according to the days chart.
&#3627408465;=27, &#3627408474;=14, &#3627408446;=(2023−1) % 100=22, &#3627408445;= Τ(2023−1)100=20
&#3627408446; = &#3627408486;&#3627408466;&#3627408462;?????? % 100
&#3627408445; = Τ&#3627408486;&#3627408466;&#3627408462;??????100
Exception for
February !

Python Code

2) Key-Value Method
where,
da y of the week
month k ey v a lue number
century k ey v a lue
&#3627408485;isthe required day of the week.
&#3627408465;is the given day of the month.
&#3627408474;is the month key value number.
&#3627408446;is the year of century (i.e., last two digits of the given year).
&#3627408445;is the century key value (e.g., 0 for the 1900s)
&#3627408485; =&#3627408465;+&#3627408474;+&#3627408446;+
&#3627408446;
4
+&#3627408445; mod 7

Saturday Sunday Monday Tuesday Wednesday Thursday Friday
0 1 2 3 4 5 6
Jan Feb Mar Apr May Jun JulAug 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 1
st
April 1983.
&#3627408465;=1, &#3627408474;=0, &#3627408446;=83, &#3627408445;=0
1
st
April 19 83
&#3627408446;&#3627408445;&#3627408474;&#3627408465;
&#3627408485; =1+0+83+
83
4
+0 mod 7
=104 mod 7
=6
6 is Friday according to the days chart.
&#3627408485; =&#3627408465;+&#3627408474;+&#3627408446;+
&#3627408446;
4
+&#3627408445; mod 7
Months Chart
Century Chart

Example: calculate the day for the date 2
nd
March 2004.
&#3627408465;=2, &#3627408474;=4, &#3627408446;=04, &#3627408445;=6
&#3627408485; =2+4+4+
4
4
+6 mod 7
=17 mod 7
=3
&#3627408485; =&#3627408465;+&#3627408474;+&#3627408446;+
&#3627408446;
4
+&#3627408445; mod 7
Months Chart
Century Chart
3 is Tuesday according to the days chart.
2
nd
March 20 04
&#3627408446;&#3627408445;&#3627408474;&#3627408465;

Example: calculate the day for the date 27
th
February 2023.
&#3627408465;=27, &#3627408474;=4, &#3627408446;=23, &#3627408445;=6
&#3627408485; =27+4+23+
23
4
+6 mod 7
=65 mod 7
=2
&#3627408485; =&#3627408465;+&#3627408474;+&#3627408446;+
&#3627408446;
4
+&#3627408445; mod 7
Months Chart
Century Chart
2 is Monday according to the days chart.
27
th
February 20 23
&#3627408446;&#3627408445;&#3627408474;&#3627408465;

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
43
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(inti = 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(b
d
)
Space Complexity: O(b
d
), 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 ED
F G H I J
K L
O
M
N

◼A,
A
B C ED
Breadth First Search (BFS)

◼A,
◼B,
A
B C ED
F G
Breadth First Search (BFS)

◼A,
◼B,C
A
B C ED
F G H
Breadth First Search (BFS)

◼A,
◼B,C,D
A
B C ED
F G H I J
Breadth First Search (BFS)

◼A,
◼B,C,D,E
A
B C ED
F G H I J
Breadth First Search (BFS)

◼A,
◼B,C,D,E,
◼F,
A
B C ED
F G H I J
Breadth First Search (BFS)

◼A,
◼B,C,D,E,
◼F,G
A
B C ED
F G H I J
K L
Breadth First Search (BFS)

◼A,
◼B,C,D,E,
◼F,G,H
A
B C ED
F G H I J
K L
Breadth First Search (BFS)

◼A,
◼B,C,D,E,
◼F,G,H,I
A
B C ED
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 ED
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 ED
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 ED
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 ED
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 ED
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 ED
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 ED
F G H I J
K L
O
M
N
Breadth First Search (BFS)

Complete? No
Optimal? No
Time Complexity: O(b
m
)
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 ED
F G H I J
K L
O
M
N
Depth First Search (DFS)

◼A,
A
B C ED
Depth First Search (DFS)

◼A,B,
A
B C ED
F G
Depth First Search (DFS)

◼A,B,F,
A
B C ED
F G
Depth First Search (DFS)

◼A,B,F,
◼G,
A
B C ED
F G
K L
Depth First Search (DFS)

◼A,B,F,
◼G,K,
A
B C ED
F G
K L
Depth First Search (DFS)

◼A,B,F,
◼G,K,
◼L,
A
B C ED
F G
K L
O
Depth First Search (DFS)

◼A,B,F,
◼G,K,
◼L, O: Goal State
A
B C ED
F G
K L
O
Depth First Search (DFS)

A
B C ED
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(b
L
)
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 ED
F G H I J
K L
O
M
N
Limit = 0
Limit = 1
Limit = 2
Depth-Limited Search (DLS)

◼A,
A
B C ED
Limit = 2
Limit = 0
Limit = 1
Depth-Limited Search (DLS)

◼A,B,
A
B C ED
F G
Limit = 2
Limit = 0
Limit = 1
Limit = 2
Depth-Limited Search (DLS)

◼A,B,F,
A
B C ED
F G
Limit = 2
Limit = 0
Limit = 1
Limit = 2
Depth-Limited Search (DLS)

◼A,B,F,
◼G,
A
B C ED
F G
Limit = 2
Limit = 0
Limit = 1
Limit = 2
Depth-Limited Search (DLS)

◼A,B,F,
◼G,
◼C,
A
B C ED
F G HLimit = 2
Limit = 0
Limit = 1
Limit = 2
Depth-Limited Search (DLS)

◼A,B,F,
◼G,
◼C,H,
A
B C ED
F G HLimit = 2
Limit = 0
Limit = 1
Limit = 2
Depth-Limited Search (DLS)

◼A,B,F,
◼G,
◼C,H,
◼D, A
B C ED
F G H I JLimit = 2
Limit = 0
Limit = 1
Limit = 2
Depth-Limited Search (DLS)

◼A,B,F,
◼G,
◼C,H,
◼D,I A
B C ED
F G H I JLimit = 2
Limit = 0
Limit = 1
Limit = 2
Depth-Limited Search (DLS)

◼A,B,F,
◼G,
◼C,H,
◼D,I
◼J,
A
B C ED
F G H I JLimit = 2
Limit = 0
Limit = 1
Limit = 2
Depth-Limited Search (DLS)

◼A,B,F,
◼G,
◼C,H,
◼D,I
◼J,
◼E
A
B C ED
F G H I JLimit = 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 ED
F G H I JLimit = 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 ED
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(b
d
)
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 ED
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 EDLimit = 1
Iterative Deepening Search (IDS)

◼A,B,
A
B C EDLimit = 1
Iterative Deepening Search (IDS)

◼A,B,
◼C,
A
B C EDLimit = 1
Iterative Deepening Search (IDS)

◼A,B,
◼C,
◼D,
A
B C EDLimit = 1
Iterative Deepening Search (IDS)

◼A,B
◼C,
◼D,
◼E, A
B C EDLimit = 1
Iterative Deepening Search (IDS)

◼A,B,
◼C,
◼D,
◼E, Failure A
B C EDLimit = 1
Iterative Deepening Search (IDS)

Iterative Deepening Search (IDS)
DLS with bound = 2

◼A,
A
B C ED
Limit = 2
Iterative Deepening Search (IDS)

◼A,B,
A
B C ED
F GLimit = 2
Iterative Deepening Search (IDS)

◼A,B,F,
A
B C ED
F GLimit = 2
Iterative Deepening Search (IDS)

◼A,B,F,
◼G,
A
B C ED
F GLimit = 2
Iterative Deepening Search (IDS)

◼A,B,F,
◼G,
◼C,
A
B C ED
F G HLimit = 2
Iterative Deepening Search (IDS)

◼A,B,F,
◼G,
◼C,H,
A
B C ED
F G HLimit = 2
Iterative Deepening Search (IDS)

◼A,B,F,
◼G,
◼C,H,
◼D, A
B C ED
F G H I JLimit = 2
Iterative Deepening Search (IDS)

◼A,B,F,
◼G,
◼C,H,
◼D,I A
B C ED
F G H I JLimit = 2
Iterative Deepening Search (IDS)

◼A,B,F,
◼G,
◼C,H,
◼D,I
◼J,
A
B C ED
F G H I JLimit = 2
Iterative Deepening Search (IDS)

◼A,B,F,
◼G,
◼C,H,
◼D,I
◼J,
◼E
A
B C ED
F G H I JLimit = 2
Iterative Deepening Search (IDS)

◼A,B,F,
◼G,
◼C,H,
◼D,I
◼J,
◼E, Failure
A
B C ED
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 ED
Limit = 3
Iterative Deepening Search (IDS)

◼A,B,
A
B C ED
F G
Limit = 3
Iterative Deepening Search (IDS)

◼A,B,F,
A
B C ED
F G
Limit = 3
Iterative Deepening Search (IDS)

◼A,B,F,
◼G,
A
B C ED
F G
K LLimit = 3
Iterative Deepening Search (IDS)

◼A,B,F,
◼G,K,
A
B C ED
F G
K LLimit = 3
Iterative Deepening Search (IDS)

◼A,B,F,
◼G,K,
◼L,
A
B C ED
F G
K LLimit = 3
Iterative Deepening Search (IDS)

◼A,B,F,
◼G,K,
◼L,
◼C, A
B C ED
F G H
K LLimit = 3
Iterative Deepening Search (IDS)

◼A,B,F,
◼G,K,
◼L,
◼C,H, A
B C ED
F G H
K LLimit = 3
Iterative Deepening Search (IDS)

◼A,B,F,
◼G,K,
◼L,
◼C,H,
◼D,
A
B C ED
F G H I J
K LLimit = 3
Iterative Deepening Search (IDS)

◼A,B,F,
◼G,K,
◼L,
◼C,H,
◼D,I,
A
B C ED
F G H I J
K L MLimit = 3
Iterative Deepening Search (IDS)

◼A,B,F,
◼G,K,
◼L,
◼C,H,
◼D,I,M,
A
B C ED
F G H I J
K L MLimit = 3
Iterative Deepening Search (IDS)

◼A,B,F,
◼G,K,
◼L,
◼C,H,
◼D,I,M,
◼J,
A
B C ED
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 ED
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 ED
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 ED
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 ED
Limit = 4
Iterative Deepening Search (IDS)

◼A,B,
A
B C ED
F G
Limit = 4
Iterative Deepening Search (IDS)

◼A,B,F,
A
B C ED
F G
Limit = 4
Iterative Deepening Search (IDS)

◼A,B,F,
◼G,
A
B C ED
F G
K L
Limit = 4
Iterative Deepening Search (IDS)

◼A,B,F,
◼G,K,
A
B C ED
F G
K L
Limit = 4
Iterative Deepening Search (IDS)

◼A,B,F,
◼G,K,
◼L,
A
B C ED
F G
K L
OLimit = 4
Iterative Deepening Search (IDS)

◼A,B,F,
◼G,K,
◼L, O: Goal State
A
B C ED
F G
K L
OLimit = 4
Iterative Deepening Search (IDS)

The returned solution is the sequence of operators in the path:
A, B, G, L, O
A
B C ED
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

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);
intmax(inta, intb);
intmin(inta, intb);
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
intscores[] = { 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);
intres= 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
intmax(inta, intb) {
return (a > b) ? a : b;
}
// minimum element function
intmin(inta, intb) {
return (a < b) ? a : b;
}
Minimax
C++
implementation

–Time complexity: O(b
m
)
–Space complexity: O(b m)
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
Thank You!