The Wayback Machine - https://web.archive.org/web/20240528151806/https://www.geeksforgeeks.org/pendant-vertices-non-pendant-vertices-pendant-edges-and-non-pendant-edges-in-graph/
Open In App

Pendant Vertices, Non-Pendant Vertices, Pendant Edges and Non-Pendant Edges in Graph

Last Updated : 31 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Pre-requisites: Handshaking theorem.

Pendant Vertices 

Let G be a graph, A vertex v of G is called a pendant vertex if and only if v has degree 1. In other words, pendant vertices are the vertices that have degree 1, also called pendant vertex. 

Note: Degree = number of edges connected to a vertex

In the case of trees, a pendant vertex is known as a terminal node or leaf node, or leaf since it has only 1 degree. Remember leaf nodes have only 1 degree so pendant vertex is called a leaf node in the case of trees.

Example: In the given diagram A and B are pendant vertices because each of them has degree 1.

Graph 1.0

Let’s take this example to print all the pendant vertices in the graph.

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print all the pendant
// vertices
void printPendantVertices(map<char, vector<char> > graph)
{
 
    // All the vectors which contain only 1
    // vertex i.e, size 1 has only 1 edge
    // hence a pendant vertex.
    for (auto x : graph) {
        if (x.second.size() == 1) {
            cout << x.first << " ";
        }
    }
}
 
// Driver Code
int main()
{
 
    map<char, vector<char> > graph;
    graph['A'].push_back('B');
    graph['B'].push_back('A');
    graph['C'].push_back('B');
    graph['B'].push_back('C');
 
    printPendantVertices(graph);
 
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
 
class GFG
{
 
  // Function to print all the pendant
  // vertices
  static void printPendantVertices(
    HashMap<Character, ArrayList<Character> > graph)
  {
 
    // All the vectors which contain only 1
    // vertex i.e, size 1 has only 1 edge
    // hence a pendant vertex.
    for (Character x : graph.keySet()) {
      if (graph.get(x).size() == 1) {
        System.out.print(x + " ");
      }
    }
  }
 
  public static void main(String[] args)
  {
    HashMap<Character, ArrayList<Character> > graph
      = new HashMap<>();
    graph.put('A', new ArrayList<>());
    graph.get('A').add('B');
    graph.put('B', new ArrayList<>());
    graph.get('B').add('A');
    graph.put('C', new ArrayList<>());
    graph.get('C').add('B');
    graph.get('B').add('C');
 
    printPendantVertices(graph);
  }
}
 
// This code is contributed by Karandeep1234


Python3




# Python program for the above approach
 
# Function to print all the pendant
# vertices
def printPendantVertices(graph):
   
# All the vectors which contain only 1
# vertex i.e, size 1 has only 1 edge
# hence a pendant vertex.
    for x,y in graph.items():
        if (len(y) == 1):
            print(x, end = " ")
 
# Driver Code
 
graph ={}
graph["A"] =  ["B"]
graph["B"] = ["A"]
graph["C"] = ["B"]
graph["B"].append("A")
 
printPendantVertices(graph)
 
# This code is contributed by shinjanpatra


C#




using System;
using System.Collections.Generic;
 
class GFG
{
   
  // Function to print all the pendant vertices
  static void printPendantVertices(
    Dictionary<char, List<char> > graph)
  {
     
    // All the vectors which contain only 1
    // vertex i.e, size 1 has only 1 edge
    // hence a pendant vertex.
    foreach(var x in graph.Keys)
    {
      if (graph[x].Count == 1) {
        Console.Write(x + " ");
      }
    }
  }
 
  public static void Main(string[] args)
  {
    var graph = new Dictionary<char, List<char> >();
    graph['A'] = new List<char>();
    graph['A'].Add('B');
    graph['B'] = new List<char>();
    graph['B'].Add('A');
    graph['C'] = new List<char>();
    graph['C'].Add('B');
    graph['B'].Add('C');
 
    printPendantVertices(graph);
  }
}
 
// This code is contributed by ishankhandelwals.


Javascript




<script>
// Javascript program for the above approach
 
// Function to print all the pendant
// vertices
function printPendantVertices(graph) {
  // All the vectors which contain only 1
  // vertex i.e, size 1 has only 1 edge
  // hence a pendant vertex.
  for (x of graph) {
    if (x[1].length == 1) {
      document.write(x[0] + " ");
    }
  }
}
 
// Driver Code
 
let graph = new Map();
graph.set("A", ["B"]);
graph.set("B", ["A"]);
graph.set("C", ["B"]);
graph.set("B", [...graph.get("B")].push("A"));
 
printPendantVertices(graph);
 
// This code is contributed by saurabh_jaiswal.
</script>


 
 

Output

A C 

Complexity : 

Time complexity :  O(n), where n is the number of vertices in the graph. This is because the code iterates through all the vertices in the graph once and checks if the size of the vector associated with each vertex is equal to 1. 

Space complexity : O(n) as the code uses a map to store the graph, where each vertex is a key and the value is a vector containing its adjacent vertices. The size of the map is directly proportional to the number of vertices in the graph.

Non-Pendant Vertices

 

Non-Pendant Vertices are the vertices that have degrees other than 1. In the case of trees, a non-pendant vertex is a non-leaf node, since it does not have degree 1 ( leaf nodes have degree 1).

 

Example: In the given diagram A and C are pendant vertices because A and C have degree 1, B is a non-pendant vertex because it contains degrees other than 1 which is 2.

 

Graph 2.0

 

Pendant Edge 

 

An edge of a graph is said to be a pendant edge if and only if one of its vertices is a pendant vertex.

 

Example: In the given diagram AB is a pendant edge since it contains pendant vertex as one of its vertexes.

 

Graph 3.0

Non-Pendant Edge

 

An edge of a graph is said to be a non-pendant edge if it does not contain a pendant vertex as one of its vertexes.

 

Example: in the given diagram AB is a pendant edge since it has pendant vertex (A) as one of its vertexes. BD, BC, DC are non-pendant vertices.

 

Graph 4.0

 

 

Examples:

 

Let us see some questions based examples on the above topics:

 

Q1. Suppose that a tree T has 2 vertices of degree 2, 4 vertices of degree 3, and 3 vertices of degree 4. find the number of pendant vertices in T.

 

Finding number pendant vertices is nothing but finding the number of leaf nodes.
         Let’s use the Handshaking Theorem formula 

         Sum of all degrees  = 2 * (Sum of Edges)

(2 vertices) * (2 degrees) + (4 vertices) * (3 degrees) + (3 vertices) * (4 degrees) + (k vertices) * (1 degree) = (2 * edges)

where 
          k is pendant vertices or leaf nodes which have degree 1
          e is total number of edges in the tree

        2*2 + 4*3 + 3*4 + k*1 = 2*e      ——-(1)

remember : number of edges = number of vertices – 1 

        e=(2+4+3+k)-1

        e=9+k-1

        e=8+k                                         ——-(2)

        putting equation 2 in 1 gives

        4 + 12 + 12 + k = 2(8+k)

        28 + k = 16 + 2k

       -2k + k = 16 – 28

       -k = -12

        k = 12

        so total number of pendant vertices are 12

 

Q2. If a tree T has 4 vertices of degree 2, 1 vertex of degree 3 and 2 vertices of degree 4 and 1 vertex of degree 5. find the number of pendant vertices in T.

 

Finding number pendant vertices is nothing but finding the number of leaf nodes.

        Let’s use the Handshaking Theorem formula

        Sum of all degrees  = 2 * (Sum of Edges)

(4 vertices)*(2 degrees) + (1 vertex)*(3 degrees) + (2 vertices)*(4 degrees) + (1 vertex)*(5 degree) + (k vertices)*(1 degree) = (2 * edges)

where
          k is pendant vertices or leaf nodes which have degree 1
          e is total number of edges in the tree

        4*2 + 1*3 + 2*4 + 1*5 + k*1 = 2*e      ——-(1)

        remember number of edges = number of vertices – 1

        e=(4+1+2+1+k)-1

        e=8+k-1

        e=7+k                                         ——-(2)

        putting equation 2 in 1 gives

        8 + 3 + 8 + 5 + k = 2(7+k)

       24 + k = 14 + 2k

       -2k + k = 14 – 24

       -k = -10

        k = 10

       so total number of pendant vertices are 10



Similar Reads

Connect a graph by M edges such that the graph does not contain any cycle and Bitwise AND of connected vertices is maximum
Given an array arr[] consisting of values of N vertices of an initially unconnected Graph and an integer M, the task is to connect some vertices of the graph with exactly M edges, forming only one connected component, such that no cycle can be formed and Bitwise AND of the connected vertices is maximum possible. Examples: Input: arr[] = {1, 2, 3, 4
9 min read
Number of Simple Graph with N Vertices and M Edges
Given two integers N and M, the task is to count the number of simple undirected graphs that can be drawn with N vertices and M edges. A simple graph is a graph that does not contain multiple edges and self-loops. Examples: Input: N = 3, M = 1 Output: 3 The 3 graphs are {1-2, 3}, {2-3, 1}, {1-3, 2}. Input: N = 5, M = 1 Output: 10 Approach: The N ve
6 min read
Find K vertices in the graph which are connected to at least one of remaining vertices
Given a connected graph with N vertices. The task is to select k(k must be less than or equals to n/2, not necessarily minimum) vertices from the graph such that all these selected vertices are connected to at least one of the non selected vertex. In case of multiple answers print any one of them. Examples: Input : Output : 1 Vertex 1 is connected
8 min read
Construct a graph using N vertices whose shortest distance between K pair of vertices is 2
Given two positive integers N and K, the task is to construct a simple and connected graph consisting of N vertices with the length of each edge as 1 unit, such that the shortest distance between exactly K pairs of vertices is 2. If it is not possible to construct the graph, then print -1. Otherwise, print the edges of the graph. Examples: Input: N
7 min read
Minimum number of edges between two vertices of a graph using DFS
Given an undirected graph G(V, E) with N vertices and M edges. We need to find the minimum number of edges between a given pair of vertices (u, v). We have already discussed this problem using the BFS approach, here we will use the DFS approach. Examples: Input: For the following given graph, find the minimum number of edges between vertex pair (0,
10 min read
Maximum Bitwise XOR of node values of an Acyclic Graph made up of N given vertices using M edges
Given N nodes valued by [1, N], an array arr[] consisting of N positive integers such that the ith node ( 1-based indexing ) has the value arr[i] and an integer M, the task is to find the maximum Bitwise XOR of node values of an acyclic graph formed by M edges. Examples: Input: arr[]= {1, 2, 3, 4}, M = 2Output: 7Explanation:Acyclic graphs having M(
7 min read
Minimum number of edges to be removed from given Graph such that no path exists between given pairs of vertices
Given an undirected graph consisting of N valued over the range [1, N] such that vertices (i, i + 1) are connected and an array arr[] consisting of M pair of integers, the task is to find the minimum number of edges that should be removed from the graph such that there doesn't exist any path for every pair (u, v) in the array arr[]. Examples: Input
8 min read
Largest subset of Graph vertices with edges of 2 or more colors
Given an undirected complete graph with N nodes or vertices. Edges of the graph are colored, find the largest subset of vertices with edges of 2 or more colors. We are given graph as adjacency matrix C[][] where C[i][j] is color of edge from vertex i to vertex j. Since graph is undirected, values C[i][j] of C[j][i] are same. We define C[i][i] to be
9 min read
Minimum number of edges between two vertices of a Graph
You are given an undirected graph G(V, E) with N vertices and M edges. We need to find the minimum number of edges between a given pair of vertices (u, v). Examples: Input: For given graph G. Find minimum number of edges between (1, 5). Output: 2Explanation: (1, 2) and (2, 5) are the only edges resulting into shortest path between 1 and 5. The idea
8 min read
Ways to Remove Edges from a Complete Graph to make Odd Edges
Given a complete graph with N vertices, the task is to count the number of ways to remove edges such that the resulting graph has odd number of edges. Examples: Input: N = 3 Output: 4 The initial graph has 3 edges as it is a complete graph. We can remove edges (1, 2) and (1, 3) or (1, 2) and (2, 3) or (1, 3) and (2, 3) or we do not remove any of th
4 min read
Practice Tags :