给定顶点数量和无向图的边数量。任务是确定电路等级。
电路等级:无向图的电路等级定义为必须从图中移除以破坏其所有循环,将其转换为树木或森林的最小边数。
例子:
Input : Edges = 7 , Vertices = 5
Output : Circuit rank = 3
Input : Edges = 7 , Vertices = 6
Output : Circuit rank = 2
配方:
Circuit rank = Edges - (Vertices - 1)
看下面的示例图,
边总数= 7,顶点= 5。
根据上面的公式,
Circuit Rank = Edges - (Vertices - 1)
= 7 - (5 - 1)
= 3
因此,上图的电路等级= 3。
在下图中可以看到,通过从上图中删除3个边(ad,ae,cd),可以删除所有循环。
下面是上述方法的实现:
C++
// C++ Program to find Circuit Rank of an Undirected Graph
#include
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 Code
int 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 Graph
using 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 Code
public static void Main()
{
int Edges = 7, Vertices = 5;
Console.WriteLine("Circuit Rank = " +
Rank(Edges, Vertices));
}
}
// This code is contributed
// by inder_verma
PHP
Javascript
输出:
Circuit Rank = 3
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。