Skip to content

Commit

Permalink
Merge pull request #1617 from Deepika14145/main
Browse files Browse the repository at this point in the history
Handshaking Lemma and Interesting Tree Properties
  • Loading branch information
pankaj-bind authored Nov 4, 2024
2 parents 13a0ae6 + 1a922f2 commit ab30415
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
-----

# Handshaking Lemma and Interesting Tree Properties

## Table of Contents
- [Introduction](#introduction)
- [Handshaking Lemma](#handshaking-lemma)
- [Formula and Explanation](#formula-and-explanation)
- [Example](#example)
- [Interesting Tree Properties](#interesting-tree-properties)
- [Properties of Trees](#properties-of-trees)
- [Tree Formulas](#tree-formulas)
- [Applications](#applications)
- [Summary](#summary)

---

## Introduction
The **Handshaking Lemma** and various **Tree Properties** form the foundation for understanding graphs and trees in computer science and mathematics. They’re widely used in network theory, data structures, and algorithm design.

---

## Handshaking Lemma
The **Handshaking Lemma** is a key concept in graph theory that relates the total number of edges in a graph to the sum of vertex degrees.

### Formula and Explanation
- **Formula**: The Handshaking Lemma states that in any undirected graph:

\[
2 \times \text{number of edges} = \sum \text{(degrees of all vertices)}
\]

- This lemma tells us that the sum of the degrees of all vertices in a graph is always even.

### Example
Consider a simple undirected graph with three vertices A, B, and C, and two edges (A-B, B-C):

- **Vertices**: A, B, C
- **Edges**: (A-B), (B-C)
- **Degrees**:
- Vertex A: 1
- Vertex B: 2
- Vertex C: 1
- **Application of Handshaking Lemma**:

\[
2 \times \text{number of edges} = 1 + 2 + 1 = 4
\]

Hence, this satisfies the lemma since we have two edges and the sum of degrees is 4.

---

## Interesting Tree Properties
Trees are a specific type of graph with unique properties. Here’s a breakdown of key properties and useful formulas related to trees.

### Properties of Trees
1. **Acyclic Connected Graph**: A tree is an undirected, connected graph with no cycles.
2. **N Nodes and N-1 Edges**: A tree with `N` nodes has exactly `N-1` edges.
3. **Unique Path**: Between any two nodes in a tree, there is exactly one path.
4. **Degree of Leaf Nodes**: Leaf nodes (end nodes) have a degree of 1.

### Tree Formulas
- **Number of Edges**: `Edges = Nodes - 1`
- **Sum of Degrees**:

\[
\sum \text{(degrees of all nodes)} = 2 \times (\text{number of edges})
\]

- **Rooted Tree Properties** (for rooted trees):
- Height of a tree: Maximum number of edges from root to any leaf.
- Total nodes at depth `d`: `2^d` for a binary tree.

### Example
Consider a tree with 4 nodes: A, B, C, D and edges A-B, A-C, A-D.

- **Properties Check**:
- **Number of nodes**: 4
- **Number of edges**: 3 (satisfies `N-1` rule)
- **Degree sum**: Degree(A) = 3, Degree(B) = 1, Degree(C) = 1, Degree(D) = 1 → Sum = 6 (which is `2 x edges`)

---

## Applications
The Handshaking Lemma and Tree Properties are applied in:

1. **Network Analysis**: Used to analyze and design communication and computer networks.
2. **Data Structures**: Essential in binary trees, binary search trees, heaps, etc.
3. **Algorithm Design**: Helps in developing algorithms for shortest path, spanning tree, and network flow.
4. **Social Network Theory**: Handshaking Lemma can model friend relationships and connectivity.
5. **Distributed Systems**: Tree properties are used to design and manage hierarchies in distributed databases.

---

## Summary
- **Handshaking Lemma**: Relates total degrees to the number of edges in any undirected graph.
- **Tree Properties**: Trees have a unique structure, are acyclic, and have `N-1` edges if they have `N` nodes.
- **Formulas**: Useful for calculations involving edges, degrees, and paths.

This knowledge is crucial in **graph theory**, **data structures**, and **network algorithms**, providing the mathematical foundation to solve many complex problems efficiently.

---
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <stdio.h>
#include <stdbool.h>

#define MAX_NODES 100

// Structure for Graph using Adjacency Matrix
typedef struct {
int adjMatrix[MAX_NODES][MAX_NODES];
int nodes;
int edges;
} Graph;

// Initialize the graph with zero edges
void initGraph(Graph* g, int nodes) {
g->nodes = nodes;
g->edges = 0;
for (int i = 0; i < nodes; i++) {
for (int j = 0; j < nodes; j++) {
g->adjMatrix[i][j] = 0;
}
}
}

// Add an undirected edge between two nodes
void addEdge(Graph* g, int u, int v) {
if (u >= g->nodes || v >= g->nodes) {
printf("Invalid nodes\n");
return;
}
g->adjMatrix[u][v] = 1;
g->adjMatrix[v][u] = 1;
g->edges++;
}

// Calculate the sum of degrees to verify Handshaking Lemma
bool verifyHandshakingLemma(Graph* g) {
int degreeSum = 0;
for (int i = 0; i < g->nodes; i++) {
int degree = 0;
for (int j = 0; j < g->nodes; j++) {
if (g->adjMatrix[i][j] == 1)
degree++;
}
degreeSum += degree;
}
return degreeSum == 2 * g->edges;
}

// Check if the structure is a tree
bool isTree(Graph* g) {
// A tree with n nodes has n-1 edges
if (g->edges != g->nodes - 1)
return false;

// A connected and acyclic graph with n-1 edges is a tree
// This is a simple approach, assuming a connected graph without cycles
return true;
}

int main() {
int nodes = 4;
Graph g;
initGraph(&g, nodes);

// Add edges
addEdge(&g, 0, 1);
addEdge(&g, 0, 2);
addEdge(&g, 0, 3);

printf("Total nodes: %d\n", g.nodes);
printf("Total edges: %d\n", g.edges);

// Check Handshaking Lemma
if (verifyHandshakingLemma(&g)) {
printf("Handshaking Lemma holds: Sum of degrees = 2 * Number of edges\n");
} else {
printf("Handshaking Lemma does not hold.\n");
}

// Check if the graph is a tree
if (isTree(&g)) {
printf("The graph is a tree.\n");
} else {
printf("The graph is not a tree.\n");
}

return 0;
}

0 comments on commit ab30415

Please sign in to comment.