Algorithms Analysis & Design - Lecture 7

1mohamedgamal54 25 views 65 slides Aug 19, 2024
Slide 1
Slide 1 of 65
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

About This Presentation

Introduction to the Design and Analysis of Algorithms


Slide Content

Algorithms
By Mohamed Gamal
© Mohamed Gamal 2024

Exhaustive Search (Brute-Force)

–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

Tree Basic Terminology
»Root: no parent.
»Leaf: no child.
»Height: distance from root to leaf (maximum number of edges in path from root).
»Depth (level): number of edge in the path from the root to that node.
»Branch: internal node, neither the root nor the leaf.
»Successor nodes of a node: its children.
»Predecessor node of a node: its parent.
»Path: sequence of nodes along the edges of a tree.

Depth of the whole tree is 3.
Tree height = 3
No. nodes = 16.
No. edges = No. nodes – 1 = 16 – 1 = 15
Lvl 0
Lvl 1
Lvl 2
Lvl 3
Root
Leaf Leaf
LeafLeafLeafLeafLeafLeaf
LeafLeaf
Node Height Depth
A 3 0
B 0 1
C 0 1
D 1 1
… … …

Graph 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

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

Criteria Tree Graph
Path Only one between two vertices More than one path is allowed
Root node It has exactly one root node Graph doesn’t have a root node
Loops No loops are permitted Graph can have loops
Complexity Less complex More complex comparatively
Traversal techniquesPre-order, In-order and Post-order BFS and DFS
No. edges (n – 1), where n is the no. edgesNot defined
Model type Hierarchical Network
Examples
Social networks, road networks,
computer networks, …, etc.
File systems, family trees, HTML DOM
structure, …, etc.
Tree versus Graph

»Completeness: is the technique guaranteed to find a solution (if there is one)?
»Optimality/Admissibility: does it always find a least-cost solution?
»Time Complexity: how long does it take to find a solution?
» Space Complexity: how much memory does it take to find a solution?
10
Criteria to compare different search techniques
–As we are going to consider different techniques to search the problem space,
we need to consider what criteria we will use to compare them.

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 algorithm
▪Put an arbitrary node ?????? in queue ??????.
▪Mark the node x as visited.
▪Repeat:
»ℎ = remove the first node from queue ??????.
»Put all of ℎ's unvisited neighbor nodes in a queue ??????.
»Mark all inserted nodes as visited.
queue

Breadth First Search (BFS) – Tree
◼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) – Tree

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

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

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

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

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

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

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

◼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) – Tree

◼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) – Tree

◼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) – Tree

◼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) – Tree

◼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) – Tree

◼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) – Tree

◼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) – Tree

◼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) – Tree

Breadth First Search (BFS) – Graph
◼Application 2:

Breadth First Search (BFS) – Graph

Breadth First Search (BFS) – Graph

Breadth First Search (BFS) – Graph

Breadth First Search (BFS) – Graph

Breadth First Search (BFS) – Graph

Breadth First Search (BFS) – Graph

Breadth First Search (BFS) – Graph

Breadth First Search (BFS) – Graph

Breadth First Search (BFS) – Graph

Breadth First Search (BFS) – Graph
DONE!
(The queue has become empty)

Complete? No
Optimal? No
Time Complexity: O(b
m
)
Space Complexity: O(b ×m)

◼Application 1:
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) – Tree

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

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

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

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

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

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

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

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) – Tree

Depth First Search (DFS) – Graph
◼Application 2:
Stack
(LIFO)

Depth First Search (DFS) – Graph

Depth First Search (DFS) – Graph

Depth First Search (DFS) – Graph

Depth First Search (DFS) – Graph

Depth First Search (DFS) – Graph

Depth First Search (DFS) – Graph

Depth First Search (DFS) – Graph

Depth First Search (DFS) – Graph

Depth First Search (DFS) – Graph

Depth First Search (DFS) – Graph
3 is visited, skip!

Depth First Search (DFS) – Graph
8 is visited, skip!

Depth First Search (DFS) – Graph
DONE!

Cairo
Giza Alexandria Port Said
Ismailia
SuezZagazigDamietta
Banha
Arish
127.5 km
172.1 km
218.4 km
5.1 km
198.1 km
356.2 km
202.2 km
336.0 km
141.1 km
200.4 km
Assignment:
•Traverse this graph using BFS.

Thank You!