Check whether given degrees of vertices represent a Graph or Tree | Function returns true for tree false for graph ; Find sum of all degrees ; It is tree if sum is equal to 2 ( n - 1 ) ; main
def check ( degree , n ) : NEW_LINE INDENT deg_sum = sum ( degree ) NEW_LINE if ( 2 * ( n - 1 ) == deg_sum ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT n = 5 NEW_LINE degree = [ 2 , 3 , 1 , 1 , 1 ] ; NEW_LINE if ( check ( degree , n ) ) : NEW_LINE INDENT print " Tree " NEW_LINE DEDENT else : NEW_LINE INDENT print " Graph " NEW_LINE DEDENT