Given the number of Vertices and the number of Edges of an Undirected Graph. The task is to determine the Circuit rank.
Circuit Rank: The Circuit rank of an undirected graph is defined as the minimum number of edges that must be removed from the graph to break all of its cycles, converting it into a tree or forest.Â
Examples:Â
Input :Edges = 7 , Vertices = 5 Output : Circuit rank = 3 Input : Edges = 7 , Vertices = 6 Output : Circuit rank = 2
Formula:Â
Circuit rank = Edges - (Vertices - 1)
Look at the sample graph below,
Total number of Edges = 7 and Vertices = 5.Â
According to the above formula,Â
Circuit Rank = Edges - (Vertices - 1)
= 7 - (5 - 1)
= 3
Therefore, Circuit Rank of the above graph = 3.Â
It can be seen in the below image that by removing 3 edges(a-d, a-e, c-d) from the above graph, all of it cycles can be removed.Â
Â
Implementation:
C++
// C++ Program to find Circuit Rank of an Undirected Graph#include <bits/stdc++.h>using namespace std;Â
// Function that calculates the// Circuit rank of the Graph.int Rank(int Edges, int Vertices){Â Â Â Â int result = 0;Â
    // calculates Circuit Rank    result = Edges - Vertices + 1;Â
    return result;}Â
// Driver Codeint main(){Â Â Â Â int Edges = 7, Vertices = 5;Â
    cout << "Circuit Rank = " << Rank(Edges, Vertices);Â
    return 0;} |
Java
//Java Program to find Circuit Rank of an Undirected GraphÂ
public class GFG {Â
    //Function that calculates the    //Circuit rank of the Graph.    static int Rank(int Edges, int Vertices)    {     int result = 0;Â
     // calculates Circuit Rank     result = Edges - Vertices + 1;Â
     return result;    }Â
    //Driver Code    public static void main(String[] args) {                  int Edges = 7, Vertices = 5;Â
         System.out.println("Circuit Rank = " + Rank(Edges, Vertices));    }} |
Python 3
# Python 3 program to find Circuit Rank of # an Undirected GraphÂ
# Function that calculates the # Circuit rank of the Graph. def Rank(Edges, Vertices) :Â
    # calculates Circuit Rank    result = Edges - Vertices + 1Â
    return resultÂ
# Driver code    if __name__ == "__main__" :Â
    Edges, Vertices = 7, 5Â
    print("Circuit Rank =",Rank(Edges, Vertices))Â
Â
# This code is contributed by ANKITRAI1 |
C#
// C# Program to find Circuit// Rank of an Undirected Graphusing System;Â
class GFG {Â
// Function that calculates the// Circuit rank of the Graph.static int Rank(int Edges,                int Vertices){    int result = 0;         // calculates Circuit Rank    result = Edges - Vertices + 1;         return result;}Â
// Driver Codepublic static void Main(){Â Â Â Â int Edges = 7, Vertices = 5;Â
    Console.WriteLine("Circuit Rank = " +                        Rank(Edges, Vertices));}}Â
// This code is contributed // by inder_verma |
PHP
<?php// PHP Program to find Circuit Rank of an Undirected GraphÂ
// Function that calculates the// Circuit rank of the Graph.function Rank($Edges, $Vertices){Â Â Â Â $result = 0;Â
    // calculates Circuit Rank    $result = $Edges - $Vertices + 1;Â
    return $result;}Â
// Driver Code$Edges = 7;$Vertices = 5;Â
echo ("Circuit Rank = ");echo (Rank($Edges, $Vertices));Â Â Â Â Â // This code is contributed // by Shivi_Aggarwal?> |
Javascript
<script>Â
// Javascript Program to find Circuit// Rank of an Undirected GraphÂ
// Function that calculates the// Circuit rank of the Graph.function Rank(Edges, Vertices){Â Â Â Â var result = 0;Â
    // calculates Circuit Rank    result = Edges - Vertices + 1;Â
    return result;}Â
// Driver Codevar Edges = 7, Vertices = 5;document.write( "Circuit Rank = " + Rank(Edges, Vertices));Â
</script> |
Circuit Rank = 3
Time Complexity: O(1)
Auxiliary Space: O(1)Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!


… [Trackback]
[…] Read More Information here to that Topic: geeksforgeeks.org/program-to-find-circuit-rank-of-an-undirected-graph/ […]