Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check whether given degrees of vertices represent a Graph or Tree in Python
In graph theory, we can determine whether a given sequence of vertex degrees represents a tree or a general graph using the Handshaking Lemma. A tree with n vertices always has exactly n-1 edges, which gives us a mathematical condition to check.
Understanding the Theory
According to the Handshaking Lemma, the sum of all vertex degrees equals twice the number of edges. For a tree with n vertices:
- Number of edges =
n - 1 - Sum of degrees =
2 × (n - 1)
If this condition is satisfied, the degree sequence can represent a tree. Otherwise, it represents a general graph.
Algorithm
To solve this problem, we follow these steps −
- Calculate the number of vertices (
vert) - Calculate the sum of all degree values (
deg_sum) - If
2 × (vert - 1) == deg_sum, return 'Tree' - Otherwise, return 'Graph'
Implementation
def solve(deg):
vert = len(deg)
deg_sum = sum(deg)
if 2 * (vert - 1) == deg_sum:
return 'Tree'
return 'Graph'
# Example 1: Tree case
deg = [2, 2, 3, 1, 1, 1]
print(f"Degrees: {deg}")
print(f"Result: {solve(deg)}")
Degrees: [2, 2, 3, 1, 1, 1] Result: Tree
Example with Graph
Let's test with a degree sequence that represents a general graph ?
def solve(deg):
vert = len(deg)
deg_sum = sum(deg)
if 2 * (vert - 1) == deg_sum:
return 'Tree'
return 'Graph'
# Example 2: Graph case
deg_graph = [3, 3, 2, 2, 2, 2]
print(f"Degrees: {deg_graph}")
print(f"Vertices: {len(deg_graph)}")
print(f"Sum of degrees: {sum(deg_graph)}")
print(f"2 × (vertices - 1) = {2 * (len(deg_graph) - 1)}")
print(f"Result: {solve(deg_graph)}")
Degrees: [3, 3, 2, 2, 2, 2] Vertices: 6 Sum of degrees: 14 2 × (vertices - 1) = 10 Result: Graph
How It Works
For the tree example [2, 2, 3, 1, 1, 1]:
- Number of vertices = 6
- Sum of degrees = 2 + 2 + 3 + 1 + 1 + 1 = 10
- Expected sum for tree = 2 × (6 - 1) = 10
- Since they match, it's a tree
Complete Solution
def check_tree_or_graph(degrees):
"""
Check if given vertex degrees represent a tree or graph
Args:
degrees: List of vertex degrees
Returns:
String: 'Tree' if degrees represent a tree, 'Graph' otherwise
"""
num_vertices = len(degrees)
degree_sum = sum(degrees)
# For a tree: sum of degrees = 2 * (number of vertices - 1)
if degree_sum == 2 * (num_vertices - 1):
return 'Tree'
else:
return 'Graph'
# Test cases
test_cases = [
[2, 2, 3, 1, 1, 1], # Tree
[3, 3, 2, 2, 2, 2], # Graph
[1, 1], # Tree (single edge)
[2, 2, 2, 2] # Graph (cycle)
]
for i, degrees in enumerate(test_cases, 1):
result = check_tree_or_graph(degrees)
print(f"Test {i}: {degrees} ? {result}")
Test 1: [2, 2, 3, 1, 1, 1] ? Tree Test 2: [3, 3, 2, 2, 2, 2] ? Graph Test 3: [1, 1] ? Tree Test 4: [2, 2, 2, 2] ? Graph
Conclusion
The algorithm uses the Handshaking Lemma to determine if vertex degrees represent a tree or graph. A degree sequence represents a tree if and only if the sum of degrees equals 2 × (n - 1), where n is the number of vertices.
