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++
#include <bits/stdc++.h>
using namespace std;
int Rank( int Edges, int Vertices)
{
int result = 0;
result = Edges - Vertices + 1;
return result;
}
int main()
{
int Edges = 7, Vertices = 5;
cout << "Circuit Rank = " << Rank(Edges, Vertices);
return 0;
}
|
Java
public class GFG {
static int Rank( int Edges, int Vertices)
{
int result = 0 ;
result = Edges - Vertices + 1 ;
return result;
}
public static void main(String[] args) {
int Edges = 7 , Vertices = 5 ;
System.out.println( "Circuit Rank = " + Rank(Edges, Vertices));
}
}
|
Python 3
def Rank(Edges, Vertices) :
result = Edges - Vertices + 1
return result
if __name__ = = "__main__" :
Edges, Vertices = 7 , 5
print ( "Circuit Rank =" ,Rank(Edges, Vertices))
|
C#
using System;
class GFG
{
static int Rank( int Edges,
int Vertices)
{
int result = 0;
result = Edges - Vertices + 1;
return result;
}
public static void Main()
{
int Edges = 7, Vertices = 5;
Console.WriteLine( "Circuit Rank = " +
Rank(Edges, Vertices));
}
}
|
PHP
<?php
function Rank( $Edges , $Vertices )
{
$result = 0;
$result = $Edges - $Vertices + 1;
return $result ;
}
$Edges = 7;
$Vertices = 5;
echo ( "Circuit Rank = " );
echo (Rank( $Edges , $Vertices ));
?>
|
Javascript
<script>
function Rank(Edges, Vertices)
{
var result = 0;
result = Edges - Vertices + 1;
return result;
}
var Edges = 7, Vertices = 5;
document.write( "Circuit Rank = " + Rank(Edges, Vertices));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
16 Nov, 2022
Like Article
Save Article