0% found this document useful (0 votes)
4 views

Tutorial 12.4 - Tutorial on pseudocode for recursion and Depth First Search (DFS)

The document provides a tutorial on recursion and depth-first search (DFS) with pseudocode examples for calculating factorials using both iterative and recursive methods. It explains the concept of recursion, highlighting that it simplifies pseudocode and is applicable only to problems with a base condition. Additionally, it includes a visual representation of DFS on a sample graph, detailing the traversal process.

Uploaded by

Meera Shridhar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Tutorial 12.4 - Tutorial on pseudocode for recursion and Depth First Search (DFS)

The document provides a tutorial on recursion and depth-first search (DFS) with pseudocode examples for calculating factorials using both iterative and recursive methods. It explains the concept of recursion, highlighting that it simplifies pseudocode and is applicable only to problems with a base condition. Additionally, it includes a visual representation of DFS on a sample graph, detailing the traversal process.

Uploaded by

Meera Shridhar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Prof. Madhavan Mukund Prof. G.

Venkatesh
Department of Computer Science Indian Institute of Technology Madras
Chennai Mathematical Institute

Mr. Omkar Joshi


Course Instructor
IITM Online Degree Programme
Content
▪ Recursion
▪ Pseudocode for factorial (iterative process)
▪ Pseudocode for factorial (recursive procedure)
▪ Depth First Search (DFS)

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 1


Recursion
▪ Recursion is a process in which a procedure calls itself.
▪ All problems can not be solved using recursion.
▪ Only those problems which have a base condition can be solved
using recursion.
▪ Recursion simplifies the pseudocode.

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 2


Pseudocode to find the factorial of a number

Procedure factorial (n)


fact = 1
while (n > 0) {
fact = fact * n
n=n-1
}
return (fact)
End factorial

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 3


Pseudocode to find the factorial of a number

Procedure factorial (n) Procedure factorial (n)


fact = 1 if (n == 0) {
while (n > 0) { return (1)
fact = fact * n }
Recursion
n=n-1 else {
} return (n * factorial (n - 1))
return (fact) }
End factorial End factorial

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 4


Pseudocode to find the factorial using recursion (n = 5)
Procedure factorial (n)
if (n == 0) {
return (1)
}
else {
return (n * factorial (n - 1))
}
End factorial

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 5


Pseudocode to find the factorial using recursion (n = 5)
Procedure factorial (5)
if (5 == 0) {
return (1)
}
else {
return (5 * factorial (5 - 1))
}
End factorial

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 6


Pseudocode to find the factorial using recursion (n = 5)
Procedure factorial (5) Procedure factorial (4)
if (5 == 0) { if (4 == 0) {
return (1) return (1)
} }
else { else {
return (5 * factorial (5 - 1)) return (4 * factorial (4 - 1))
} }
End factorial End factorial

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 7


Pseudocode to find the factorial using recursion (n = 5)
Procedure factorial (5) Procedure factorial (4) Procedure factorial (3)
if (5 == 0) { if (4 == 0) { if (3 == 0) {
return (1) return (1) return (1)
} } }
else { else { else {
return (5 * factorial (5 - 1)) return (4 * factorial (4 - 1)) return (3 * factorial (3 - 1))
} } }
End factorial End factorial End factorial

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 8


Pseudocode to find the factorial using recursion (n = 5)
Procedure factorial (5) Procedure factorial (4) Procedure factorial (3)
if (5 == 0) { if (4 == 0) { if (3 == 0) {
return (1) return (1) return (1)
} } }
else { else { else {
return (5 * factorial (5 - 1)) return (4 * factorial (4 - 1)) return (3 * factorial (3 - 1))
} } }
End factorial End factorial End factorial
Procedure factorial (2)
if (2 == 0) {
return (1)
}
else {
return (2 * factorial (2 - 1))
}
End factorial

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 9


Pseudocode to find the factorial using recursion (n = 5)
Procedure factorial (5) Procedure factorial (4) Procedure factorial (3)
if (5 == 0) { if (4 == 0) { if (3 == 0) {
return (1) return (1) return (1)
} } }
else { else { else {
return (5 * factorial (5 - 1)) return (4 * factorial (4 - 1)) return (3 * factorial (3 - 1))
} } }
End factorial End factorial End factorial
Procedure factorial (2) Procedure factorial (1)
if (2 == 0) { if (1 == 0) {
return (1) return (1)
} }
else { else {
return (2 * factorial (2 - 1)) return (1 * factorial (1 - 1))
} }
End factorial End factorial

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 10


Pseudocode to find the factorial using recursion (n = 5)
Procedure factorial (5) Procedure factorial (4) Procedure factorial (3)
if (5 == 0) { if (4 == 0) { if (3 == 0) {
return (1) return (1) return (1)
} } }
else { else { else {
return (5 * factorial (5 - 1)) return (4 * factorial (4 - 1)) return (3 * factorial (3 - 1))
} } }
End factorial End factorial End factorial
Procedure factorial (2) Procedure factorial (1) Procedure factorial (0)
if (2 == 0) { if (1 == 0) { if (0 == 0) {
return (1) return (1) return (1)
} } }
else { else { else {
return (2 * factorial (2 - 1)) return (1 * factorial (1 - 1)) return (0 * factorial (0 - 1))
} } }
End factorial End factorial End factorial

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 11


Pseudocode to find the factorial using recursion (n = 5)
Procedure factorial (5) Procedure factorial (4) Procedure factorial (3)
if (5 == 0) { if (4 == 0) { if (3 == 0) {
return (1) return (1) return (1)
} } }
else { else { else {
return (5 * factorial (5 - 1)) return (4 * factorial (4 - 1)) return (3 * factorial (3 - 1))
} } }
End factorial End factorial End factorial
Procedure factorial (2) Procedure factorial (1)
if (2 == 0) { if (1 == 0) {
return (1) return (1)
} }
else { else {
return (2 * factorial (2 - 1)) return (1 * 1)
} }
End factorial End factorial

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 12


Pseudocode to find the factorial using recursion (n = 5)
Procedure factorial (5) Procedure factorial (4) Procedure factorial (3)
if (5 == 0) { if (4 == 0) { if (3 == 0) {
return (1) return (1) return (1)
} } }
else { else { else {
return (5 * factorial (5 - 1)) return (4 * factorial (4 - 1)) return (3 * factorial (3 - 1))
} } }
End factorial End factorial End factorial
Procedure factorial (2)
if (2 == 0) {
return (1)
}
else {
return (2 * 1)
}
End factorial

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 13


Pseudocode to find the factorial using recursion (n = 5)
Procedure factorial (5) Procedure factorial (4) Procedure factorial (3)
if (5 == 0) { if (4 == 0) { if (3 == 0) {
return (1) return (1) return (1)
} } }
else { else { else {
return (5 * factorial (5 - 1)) return (4 * factorial (4 - 1)) return (3 * 2)
} } }
End factorial End factorial End factorial

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 14


Pseudocode to find the factorial using recursion (n = 5)
Procedure factorial (5) Procedure factorial (4)
if (5 == 0) { if (4 == 0) {
return (1) return (1)
} }
else { else {
return (5 * factorial (5 - 1)) return (4 * 6))
} }
End factorial End factorial

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 15


Pseudocode to find the factorial using recursion (n = 5)
Procedure factorial (5)
if (5 == 0) {
return (1)
}
else {
return (5 * 24)
}
End factorial

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 16


Depth First Search (DFS)
1 2

4 3
Input graph
Starting vertex = 1

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 17


Depth First Search (DFS)
1 2 1

4 3
Input graph
Current vertex = 1

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 18


Depth First Search (DFS)
1 2 1

4 3
Input graph
Current vertex = 1
Neighbour = 2, 4

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 19


Depth First Search (DFS)
1 2 1

2
4 3
Input graph
Current vertex = 2
Neighbour = 3, 5

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 20


Depth First Search (DFS)
1 2 1

2
4 3
Input graph
Current vertex = 1
3
Neighbour = No

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 21


Depth First Search (DFS)
1 2 1

2
4 3
Input graph
Current vertex = 5 5
3
Neighbour = 4

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 22


Depth First Search (DFS)
1 2 1

2
4 3
Input graph
Current vertex = 4 5
3
Neighbour = No

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 23


Depth First Search (DFS)
1 2 1

2
4 3
Input graph

3 5

Output DFS tree

TUTORIAL ON PSEUDOCODE FOR RECURSION AND DEPTH FIRST SEARCH (DFS) 24

You might also like