Largest subarray sum of all connected components in undirected graph | C # implementation to find largest subarray sum among all connected components ; Function to traverse the undirected graph using the Depth first traversal ; Marking the visited vertex as true ; Store the connected chain ; Recursive call to the DFS algorithm ; Function to return maximum subarray sum of each connected component using Kadane 's Algorithm ; Following loop finds maximum subarray sum based on Kadane 's  algorithm ; Global maximum subarray sum ; Returning the sum ; Function to find the maximum subarray sum among all connected components ; Initializing boolean array to mark visited vertices ; maxSum stores the maximum subarray sum ; Following loop invokes DFS algorithm ; Variable to hold temporary length ; Variable to hold temporary maximum subarray sum values ; Container to store each chain ; DFS algorithm ; Variable to hold each chain size ; Container to store values of vertices of individual chains ; Storing the values of each chain ; Function call to find maximum subarray sum of current connection ; Conditional to store current maximum subarray sum ; Printing global maximum subarray sum ; Driver code ; Initializing graph in the form of adjacency list ; Defining the number of edges and vertices ; Assigning the values for each vertex of the undirected graph ; Constructing the undirected graph
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static void depthFirst ( int v , List < List < int > > graph , bool [ ] visited , List < int > storeChain ) { visited [ v ] = true ; storeChain . Add ( v ) ; foreach ( int i in graph [ v ] ) { if ( visited [ i ] == false ) { depthFirst ( i , graph , visited , storeChain ) ; } } } static int subarraySum ( int [ ] arr , int n ) { int maxSubarraySum = arr [ 0 ] ; int currentMax = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { currentMax = Math . Max ( arr [ i ] , arr [ i ] + currentMax ) ; maxSubarraySum = Math . Max ( maxSubarraySum , currentMax ) ; } return maxSubarraySum ; } static void maxSubarraySum ( List < List < int > > graph , int vertices , List < int > values ) { bool [ ] visited = new bool [ 1001 ] ; int maxSum = - 1000000 ; for ( int i = 1 ; i <= vertices ; i ++ ) { if ( visited [ i ] == false ) { int sizeChain ; int tempSum ; List < int > storeChain = new List < int > ( ) ; depthFirst ( i , graph , visited , storeChain ) ; sizeChain = storeChain . Count ; int [ ] chainValues = new int [ sizeChain + 1 ] ; for ( int j = 0 ; j < sizeChain ; j ++ ) { int temp = values [ storeChain [ j ] - 1 ] ; chainValues [ j ] = temp ; } tempSum = subarraySum ( chainValues , sizeChain ) ; if ( tempSum > maxSum ) { maxSum = tempSum ; } } } Console . Write ( " Maximum ▁ subarray ▁ sum ▁ among ▁ all ▁ " ) ; Console . Write ( " connected ▁ components ▁ = ▁ " ) ; Console . Write ( maxSum ) ; } public static void Main ( string [ ] args ) { List < List < int > > graph = new List < List < int > > ( ) ; for ( int i = 0 ; i < 1001 ; i ++ ) graph . Add ( new List < int > ( ) ) ; int V = 7 ; List < int > values = new List < int > ( ) ; values . Add ( 3 ) ; values . Add ( 2 ) ; values . Add ( 4 ) ; values . Add ( - 2 ) ; values . Add ( 0 ) ; values . Add ( - 1 ) ; values . Add ( - 5 ) ; graph [ 1 ] . Add ( 2 ) ; graph [ 2 ] . Add ( 1 ) ; graph [ 3 ] . Add ( 4 ) ; graph [ 4 ] . Add ( 3 ) ; graph [ 4 ] . Add ( 5 ) ; graph [ 5 ] . Add ( 4 ) ; graph [ 6 ] . Add ( 7 ) ; graph [ 7 ] . Add ( 6 ) ; maxSubarraySum ( graph , V , values ) ; } }