Largest subarray sum of all connected components in undirected graph | Java 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
import java . io . * ; import java . util . * ; class GFG { static void depthFirst ( int v , List < List < Integer > > graph , boolean [ ] visited , List < Integer > storeChain ) { visited [ v ] = true ; storeChain . add ( v ) ; for ( int i : graph . get ( 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 < Integer > > graph , int vertices , List < Integer > values ) { boolean [ ] visited = new boolean [ 1001 ] ; int maxSum = Integer . MIN_VALUE ; for ( int i = 1 ; i <= vertices ; i ++ ) { if ( visited [ i ] == false ) { int sizeChain ; int tempSum ; List < Integer > storeChain = new ArrayList < Integer > ( ) ; depthFirst ( i , graph , visited , storeChain ) ; sizeChain = storeChain . size ( ) ; int [ ] chainValues = new int [ sizeChain + 1 ] ; for ( int j = 0 ; j < sizeChain ; j ++ ) { int temp = values . get ( storeChain . get ( j ) - 1 ) ; chainValues [ j ] = temp ; } tempSum = subarraySum ( chainValues , sizeChain ) ; if ( tempSum > maxSum ) { maxSum = tempSum ; } } } System . out . print ( " Maximum ▁ subarray ▁ sum ▁ among ▁ all ▁ " ) ; System . out . print ( " connected ▁ components ▁ = ▁ " ) ; System . out . print ( maxSum ) ; } public static void main ( String [ ] args ) { List < List < Integer > > graph = new ArrayList ( ) ; for ( int i = 0 ; i < 1001 ; i ++ ) graph . add ( new ArrayList < Integer > ( ) ) ; int E = 4 , V = 7 ; List < Integer > values = new ArrayList < Integer > ( ) ; values . add ( 3 ) ; values . add ( 2 ) ; values . add ( 4 ) ; values . add ( - 2 ) ; values . add ( 0 ) ; values . add ( - 1 ) ; values . add ( - 5 ) ; graph . get ( 1 ) . add ( 2 ) ; graph . get ( 2 ) . add ( 1 ) ; graph . get ( 3 ) . add ( 4 ) ; graph . get ( 4 ) . add ( 3 ) ; graph . get ( 4 ) . add ( 5 ) ; graph . get ( 5 ) . add ( 4 ) ; graph . get ( 6 ) . add ( 7 ) ; graph . get ( 7 ) . add ( 6 ) ; maxSubarraySum ( graph , V , values ) ; } }