0% found this document useful (0 votes)
2 views4 pages

19 Thdec ACA

The document outlines algorithms for solving the Tower of Hanoi and summing array elements using recursion, along with their time and space complexities. It also describes a Depth First Search (DFS) algorithm for graph traversal, including code examples for each algorithm. Each section provides a clear structure with base and recursive cases, input handling, and expected outputs.
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)
2 views4 pages

19 Thdec ACA

The document outlines algorithms for solving the Tower of Hanoi and summing array elements using recursion, along with their time and space complexities. It also describes a Depth First Search (DFS) algorithm for graph traversal, including code examples for each algorithm. Each section provides a clear structure with base and recursive cases, input handling, and expected outputs.
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/ 4

Date: 19-12-2024

8.3 Tower of Hanoi


Algorithm:
1. Define a recursive function towerOfHanoi(n, source, destination, auxiliary):
• Base Case: If n = 1, move the disk from the source rod to the destination rod.
• Recursive Case:
– Move n − 1 disks from the source rod to the auxiliary rod, using the destination rod as a helper.
– Move the nth disk from the source rod to the destination rod.
– Move n − 1 disks from the auxiliary rod to the destination rod, using the source rod as a helper.

2. In the main function:


• Take the number of disks (n) as input.
• Call the recursive function to display the steps.
Time Complexity:

• O(2n − 1): Each recursive call splits into two smaller recursive calls, doubling the work with each additional disk.
Space Complexity:
• O(n): Due to the recursive call stack.
Code:
# include < iostream >
using namespace std ;

void towerOfHanoi ( int n , char source , char destination , char auxiliary )


{
if ( n == 1)
{
cout << " Move disk 1 from " << source << " to " << destination << endl ;
return ;
}
towerOfHanoi ( n - 1 , source , auxiliary , destination ) ;
cout << " Move disk " << n << " from " << source << " to " << destination << endl ;
towerOfHanoi ( n - 1 , auxiliary , destination , source ) ;
}

int main ()
{
int n ;
cout << " Enter the number of disks : " ;
cin >> n ;

if ( n <= 0)
{
cout << " Number of disks must be positive . " << endl ;
return 0;
}

cout << " The sequence of moves to solve Tower of Hanoi is :\ n " ;
towerOfHanoi (n , ’A ’ , ’C ’ , ’B ’) ;
return 0;
}

Output 1:
Enter the number of disks: 2
The sequence of moves to solve Tower of Hanoi is:
Move disk 1 from A to B
Move disk 2 from A to C
Move disk 1 from B to C

49
Output 2:
Enter the number of disks: 3
The sequence of moves to solve Tower of Hanoi is:
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C

8.4 Sorting Array Elements using Recursion


Algorithm:
1. Define a recursive function sumArray(arr, n):
• Base Case: If the size of the array (n) is 0, return 0.
• Recursive Case: Return the sum of the last element and the sum of the first n − 1 elements:

sumArray(arr, n) = arr[n-1] + sumArray(arr, n-1).

2. In the main function:


• Take the size of the array (n) and its elements as input.
• Call the recursive function sumArray(arr, n).
• Display the sum.
Time Complexity:
• O(n): The function is called recursively n times, where n is the size of the array.
Space Complexity:
• O(n): Due to the recursive call stack.
Code:
# include < iostream >
# include < vector >
using namespace std ;

int sumArray ( const vector < int >& arr , int n ) {


if ( n == 0)
return 0;
return arr [ n - 1] + sumArray ( arr , n - 1) ;
}

int main () {
int n ;
cout << " Enter the number of elements : " ;
cin >> n ;

if ( n <= 0)
{
cout << " Number of elements must be positive . " << endl ;
return 0;
}

vector < int > arr ( n ) ;


cout << " Enter the elements : " ;
for ( int i = 0; i < n ; i ++)
cin >> arr [ i ];

int sum = sumArray ( arr , n ) ;


cout << " Sum of array elements : " << sum << endl ;
return 0;
}

50
Output 1:
Enter the number of elements: 5
Enter the elements: 1 2 3 4 5
Sum of array elements: 15
Output 2:
Enter the number of elements: 4
Enter the elements: 10 20 30 40
Sum of array elements: 100

9 Graphs
9.1 Depth First Traversal
Algorithm:
1. Use a recursive function to perform Depth First Search (DFS).
2. Define a function DFS(node, visited, graph):
• Base Case: If the node has already been visited, return.
• Recursive Case:
(a) Mark the current node as visited.
(b) Print the current node or store it in a result list.
(c) Recursively call DFS for all unvisited neighbors of the current node.
3. For disconnected graphs:
• Iterate through all nodes and perform DFS for each unvisited node.
Time Complexity:
• O(V + E): V is the number of vertices, and E is the number of edges. Each vertex and edge is visited once.
Space Complexity:
• O(V ): Due to the recursion stack and visited array.
Code:
# include < iostream >
# include < vector >
# include < list >
using namespace std ;

void DFS ( int node , vector < bool > & visited , const vector < list < int > > & graph )
{
visited [ node ] = true ;
cout << node << " " ;

for ( int neighbor : graph [ node ])


if (! visited [ neighbor ])
DFS ( neighbor , visited , graph ) ;
}

int main ()
{
int V , E ;
cout << " Enter the number of vertices : " ;
cin >> V ;
cout << " Enter the number of edges : " ;
cin >> E ;

vector < list < int > > graph ( V ) ;


cout << " Enter the edges ( u v ) : " << endl ;
for ( int i = 0; i < E ; i ++)

51
{
int u , v ;
cin >> u >> v ;
graph [ u ]. push_back ( v ) ;
graph [ v ]. push_back ( u ) ;
}

vector < bool > visited (V , false ) ;

cout << " Depth First Traversal starting from node 0: " ;
for ( int i = 0; i < V ; i ++)
if (! visited [ i ])
DFS (i , visited , graph ) ;
cout << endl ;

return 0;
}

Output 1:
Enter the number of vertices: 5
Enter the number of edges: 4
Enter the edges (u v):
0 1
0 2
1 3
1 4
Depth First Traversal starting from node 0: 0 1 3 4 2
Output 2:
Enter the number of vertices: 6
Enter the number of edges: 5
Enter the edges (u v):
0 1
0 2
1 3
3 4
4 5
Depth First Traversal starting from node 0: 0 1 3 4 5 2

52

You might also like