Check whether given degrees of vertices represent a Graph or Tree | Function returns true for tree false for graph ; Find sum of all degrees ; Graph is tree if sum is equal to 2 ( n - 1 ) ; Driver Code
< ? php function check ( & $ degree , $ n ) { $ deg_sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ deg_sum += $ degree [ $ i ] ; return ( 2 * ( $ n - 1 ) == $ deg_sum ) ; } $ n = 5 ; $ degree = array ( 2 , 3 , 1 , 1 , 1 ) ; if ( check ( $ degree , $ n ) ) echo " Tree " ; else echo " Graph " ; ? >