9thJanACALAB
9thJanACALAB
cout << " Shortest distances from source " << source << " : " << endl ;
for ( int i = 0; i < V ; i ++)
if ( distance [ i ] == INF )
cout << " Vertex " << i << " : INF " << endl ;
else
53
cout << " Vertex " << i << " : " << distance [ i ] << endl ;
}
int main ()
{
int V , E ;
cout << " Enter the number of vertices : " ;
cin >> V ;
cout << " Enter the number of edges : " ;
cin >> E ;
int source ;
cout << " Enter the source vertex : " ;
cin >> source ;
return 0;
}
Output 1:
Enter the number of vertices: 5
Enter the number of edges: 6
Enter the edges (u, v, weight):
0 1 2
0 2 4
1 2 1
1 3 7
2 4 3
3 4 1
Enter the source vertex: 0
Shortest distances from source 0:
Vertex 0: 0
Vertex 1: 2
Vertex 2: 3
Vertex 3: 9
Vertex 4: 6
Output 2:
Enter the number of vertices: 4
Enter the number of edges: 4
Enter the edges (u, v, weight):
0 1 1
1 2 2
0 2 4
2 3 1
Enter the source vertex: 0
Shortest distances from source 0:
Vertex 0: 0
Vertex 1: 1
Vertex 2: 3
Vertex 3: 4
54
9.3 Cycle Detection: Undirected Graph
Algorithm:
1. Represent the graph using an adjacency matrix graph.
2. Use a visited array to track visited nodes.
3. Perform DFS:
• If a visited node is encountered that is not the parent, a cycle is detected.
4. Repeat the process for all components of the graph.
Time Complexity:
• O(V 2 ): Adjacency matrix requires checking all nodes for edges.
Space Complexity:
• O(V ): Due to the visited and recStack arrays.
Code:
# include < iostream >
# include < cstring >
using namespace std ;
bool dfsUndirected ( int node , int parent , int graph [ MAX ][ MAX ] , bool visited [] , int n )
{
visited [ node ] = true ;
int main ()
{
int n , m ;
cout << " Enter the number of nodes and edges : " ;
cin >> n >> m ;
55
if ( h a s C y c l e U n d i r e c t e d (n , graph ) )
cout << " Cycle detected in the undirected graph . " << endl ;
else
cout << " No cycle detected in the undirected graph . " << endl ;
return 0;
}
Output 1:
Enter the number of nodes and edges: 4 3
Enter the edges (u v):
0 1
1 2
2 0
Cycle detected in the undirected graph.
Output 2:
Enter the number of nodes and edges: 4 3
Enter the edges (u v):
0 1
1 2
2 3
No cycle detected in the undirected graph.
bool dfsDirected ( int node , int graph [ MAX ][ MAX ] , bool visited [] , bool recStack [] , int n )
{
visited [ node ] = true ;
recStack [ node ] = true ;
56
{
return true ;
}
}
else if ( recStack [ neighbor ])
return true ;
}
int main ()
{
int n , m ;
cout << " Enter the number of nodes and edges : " ;
cin >> n >> m ;
if ( h a s C y c l e D i r e c t e d (n , graph ) )
cout << " Cycle detected in the directed graph . " << endl ;
else
cout << " No cycle detected in the directed graph . " << endl ;
return 0;
}
Output 1:
57
10 Dynamic Programming
10.1 Binomial Co efficient
Algorithm:
1. Define a DP table dp[n + 1][k + 1]:
dp[i][j] stores C(i, j), the number of ways to choose j items from i items.
return dp [ n ][ k ];
}
int main ()
{
int n , k ;
cout << " Enter the values of n and k : " ;
cin >> n >> k ;
if ( k > n )
{
58
cout << " Invalid input : k cannot be greater than n . " << endl ;
return 0;
}
cout << " Binomial Coefficient C ( " << n << " , " << k << " ) = " << b i n o m i a l C o e f f i c i e n t (n , k ) << endl ;
return 0;
}
Output 1:
Enter the values of n and k: 5 2
Binomial Coefficient C(5, 2) = 10
Output 2:
Enter the values of n and k: 6 3
Binomial Coefficient C(6, 3) = 20
dp[i][j] stores the length of the longest common substring ending at indices i − 1 and j − 1.
59
dp [ i ][ j ] = 0;
return maxLen ;
}
int main ()
{
char A [ MAX ] , B [ MAX ];
cout << " Enter the first string : " ;
cin >> A ;
cout << " Enter the second string : " ;
cin >> B ;
return 0;
}
Output 1:
Enter the first string: abcde
Enter the second string: abfce
Length of Longest Common Substring: 2
Output 2:
Enter the first string: abcxyz
Enter the second string: xyzabc
Length of Longest Common Substring: 3
60
for ( int i = 0; i < n ; i ++)
dp [1 << i ][ i ] = 0;
return result ;
}
int main ()
{
int n ;
cout << " Enter the number of cities : " ;
cin >> n ;
return 0;
}
Output 1:
Enter the number of cities: 4
Enter the distance matrix:
0 10 15 20
10 0 35 25
15 35 0 30
20 25 30 0
The minimum cost of the Travelling Salesman Problem is: 80
Output 2:
Enter the number of cities: 3
Enter the distance matrix:
0 1 2
1 0 3
2 3 0
The minimum cost of the Travelling Salesman Problem is: 6
61