From 6c0964d02291ba2d67183e3658b6d80213906be8 Mon Sep 17 00:00:00 2001 From: sourav-naskar Date: Sat, 1 Oct 2022 21:38:17 +0530 Subject: [PATCH 1/3] =?UTF-8?q?added=20Kosaraju=E2=80=99s=20Algorithm=20fo?= =?UTF-8?q?r=20Strongly=20Connected=20Components(SCC)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CPP/graph_tree/Kosaraju_Algorithm.cpp | 77 +++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 CPP/graph_tree/Kosaraju_Algorithm.cpp diff --git a/CPP/graph_tree/Kosaraju_Algorithm.cpp b/CPP/graph_tree/Kosaraju_Algorithm.cpp new file mode 100644 index 00000000..eb28b656 --- /dev/null +++ b/CPP/graph_tree/Kosaraju_Algorithm.cpp @@ -0,0 +1,77 @@ +#include +using namespace std; +void dfs(int node, stack &st, vector &vis, vector adj[]) { + vis[node] = 1; + for(auto it: adj[node]) { + if(!vis[it]) { + dfs(it, st, vis, adj); + } + } + + st.push(node); +} +void revDfs(int node, vector &vis, vector transpose[]) { + cout << node << " "; + vis[node] = 1; + for(auto it: transpose[node]) { + if(!vis[it]) { + revDfs(it, vis, transpose); + } + } +} +int main() { + int n=6, m=7; + vector adj[n+1]; + adj[1].push_back(3); + adj[2].push_back(1); + adj[3].push_back(2); + adj[3].push_back(5); + adj[4].push_back(6); + adj[5].push_back(4); + adj[6].push_back(5); + + stack st; + vector vis(n+1, 0); + for(int i = 1;i<=n;i++) { + if(!vis[i]) { + dfs(i, st, vis, adj); + } + } + + vector transpose[n+1]; + + for(int i = 1;i<=n;i++) { + vis[i] = 0; + for(auto it: adj[i]) { + transpose[it].push_back(i); + } + } + + + + while(!st.empty()) { + int node = st.top(); + st.pop(); + if(!vis[node]) { + cout << "SCC: "; + revDfs(node, vis, transpose); + cout << endl; + } + } + + + return 0; +} + +/* +Output: + +SCC: 1 2 3 +SCC: 5 6 4 + +Time Complexity: O(N+E), DFS+TopoSort + +Space Complexity: O(N+E), Transposing the graph +*/ + +//contributed by Sourav Naskar \ No newline at end of file From 5b9c9eea2b84a221a40cd3ebe8e1e2583f99ed6a Mon Sep 17 00:00:00 2001 From: sourav-naskar Date: Sat, 1 Oct 2022 21:44:57 +0530 Subject: [PATCH 2/3] added Coin Change 2 --- Dynamic Programming/Coin_Change.cpp | 55 +++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Dynamic Programming/Coin_Change.cpp diff --git a/Dynamic Programming/Coin_Change.cpp b/Dynamic Programming/Coin_Change.cpp new file mode 100644 index 00000000..12331222 --- /dev/null +++ b/Dynamic Programming/Coin_Change.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +long countWaysToMakeChangeUtil(vector& arr,int ind, int T, vector>& dp){ + + if(ind == 0){ + return (T%arr[0]==0); + } + + if(dp[ind][T]!=-1) + return dp[ind][T]; + + long notTaken = countWaysToMakeChangeUtil(arr,ind-1,T,dp); + + long taken = 0; + if(arr[ind] <= T) + taken = countWaysToMakeChangeUtil(arr,ind,T-arr[ind],dp); + + return dp[ind][T] = notTaken + taken; +} + + +long countWaysToMakeChange(vector& arr, int n, int T){ + + vector> dp(n,vector(T+1,-1)); + return countWaysToMakeChangeUtil(arr,n-1, T, dp); +} + +int main() { + + vector arr ={1,2,3}; + int target=4; + + int n =arr.size(); + + cout<<"The total number of ways is " < Date: Sat, 1 Oct 2022 22:07:46 +0530 Subject: [PATCH 3/3] Delete Coin_Change.cpp --- Dynamic Programming/Coin_Change.cpp | 55 ----------------------------- 1 file changed, 55 deletions(-) delete mode 100644 Dynamic Programming/Coin_Change.cpp diff --git a/Dynamic Programming/Coin_Change.cpp b/Dynamic Programming/Coin_Change.cpp deleted file mode 100644 index 12331222..00000000 --- a/Dynamic Programming/Coin_Change.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include - -using namespace std; - -long countWaysToMakeChangeUtil(vector& arr,int ind, int T, vector>& dp){ - - if(ind == 0){ - return (T%arr[0]==0); - } - - if(dp[ind][T]!=-1) - return dp[ind][T]; - - long notTaken = countWaysToMakeChangeUtil(arr,ind-1,T,dp); - - long taken = 0; - if(arr[ind] <= T) - taken = countWaysToMakeChangeUtil(arr,ind,T-arr[ind],dp); - - return dp[ind][T] = notTaken + taken; -} - - -long countWaysToMakeChange(vector& arr, int n, int T){ - - vector> dp(n,vector(T+1,-1)); - return countWaysToMakeChangeUtil(arr,n-1, T, dp); -} - -int main() { - - vector arr ={1,2,3}; - int target=4; - - int n =arr.size(); - - cout<<"The total number of ways is " <