Without Self-loops and Parallel Edges
In a directed graph without self-loops and parallel edges each vertex can have an edge to the every other vertex except itself. The maximum number of the edges E max in such a graph with the V vertices is given by the formula:
E max=V×(V−1)
Implementation:
#include <iostream>
using namespace std;
// Function to return the maximum number of directed edges
// if self loops and parallel edges are not allowed
int GFG(int V) { return V * (V - 1); }
int main()
{
int V = 5;
int maxEdges = GFG(V);
cout << "Maximum number of edges without self-loops "
"and parallel edges: "
<< maxEdges << endl;
return 0;
}
public class Main {
// Function to return the maximum number of directed
// edges if self loops and parallel edges are not
// allowed
public static int maxEdges(int V)
{
return V * (V - 1);
}
public static void main(String[] args)
{
int V = 5;
int maxEdges = maxEdges(V);
System.out.println(
"Maximum number of edges without self-loops and parallel edges: "
+ maxEdges);
}
}
# Function to return the maximum number of directed edges
# if self loops and parallel edges are not allowed
def GFG(V):
return V * (V - 1)
def main():
V = 5
maxEdges = GFG(V)
print("Maximum number of edges without self-loops "
"and parallel edges:", maxEdges)
if __name__ == "__main__":
main()
# This code is contributed by Ayush Mishra
function maxEdges(V) {
return V * (V - 1);
}
function main() {
const V = 5;
const maxEdgesValue = maxEdges(V);
console.log("Maximum number of edges without self-loops and parallel edges: " + maxEdgesValue);
}
// Call the main function
main();
Output
Maximum number of edges without self-loops and parallel edges: 20
Maximum Number of Edges in a Directed Graph
In a directed graph, edges have a direction associated with them meaning they point from the one vertex to another. The maximum number of edges in the directed graph depends on the number of the vertices and type of graph.