Program to find the number of region in Planar Graph

07 Jun, 2022
Given two integers V and E which represent the number of Vertices and Edges of a Planar Graph. The Task is to find the number of regions of that planar graph.
Planar Graph: A planar graph is one in which no edges cross each other or a graph that can be drawn on a plane without edges crossing is called planar graph.
Region: When a planar graph is drawn without edges crossing, the edges and vertices of the graph divide the plane into regions.
Examples:  
Input: V = 4, E = 5 Output: R = 3  
Input: V = 3, E = 3 Output: R = 2  
Approach: Euler found out the number of regions in a planar graph as a function of the number of vertices and number of edges in the graph i.e.  
Below is the implementation of the above approach: 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the number// of regions in a Planar Graphint Regions(int Vertices, int Edges){    int R = Edges + 2 - Vertices;     return R;} // Driver codeint main(){    int V = 5, E = 7;     cout << Regions(V, E);     return 0;}
// Java implementation of the approachimport java.io.*; class GFG {     // Function to return the number    // of regions in a Planar Graph    static int Regions(int Vertices, int Edges)    {        int R = Edges + 2 - Vertices;         return R;    }     // Driver code    public static void main(String[] args)    {         int V = 5, E = 7;        System.out.println(Regions(V, E));    }} // This code is contributed by akt_mit
# Python3 implementation of the approach # Function to return the number# of regions in a Planar Graphdef Regions(Vertices, Edges) :     R = Edges + 2 - Vertices;     return R; # Driver codeif __name__ == "__main__" :     V = 5; E = 7;     print(Regions(V, E)); # This code is contributed# by AnkitRai01
// C# implementation of the approachusing System; class GFG {     // Function to return the number    // of regions in a Planar Graph    static int Regions(int Vertices, int Edges)    {        int R = Edges + 2 - Vertices;         return R;    }     // Driver code    static public void Main()    {         int V = 5, E = 7;        Console.WriteLine(Regions(V, E));    }} // This code is contributed by ajit
<?php// PHP implementation of the approach // Function to return the number// of regions in a Planar Graphfunction Regions($Vertices, $Edges){    $R = $Edges + 2 - $Vertices;     return $R;} // Driver code$V = 5; $E = 7;echo(Regions($V, $E)); // This code is contributed// by Code_Mech?>
<script> // Javascript implementation of the approach // Function to return the number// of regions in a Planar Graphfunction Regions(Vertices, Edges){    var R = Edges + 2 - Vertices;     return R;} // Driver codevar V = 5, E = 7; document.write( Regions(V, E)); // This code is contributed by itsok </script>
4
 
Time Complexity: O(1)
Auxiliary Space: O(1)
ankthon
jit_t
Code_Mech
itsok
souravkumar29
Geometric
Graph
Mathematical
Mathematical
Graph
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum Manhattan distance between a distinct pair from N coordinates
Total area of two overlapping rectangles
Equation of circle when three points on the circle are given
Orientation of 3 ordered points
Area of the largest rectangle possible from given coordinates
Breadth First Search or BFS for a Graph
Depth First Search or DFS for a Graph
Dijkstra's shortest path algorithm | Greedy Algo-7
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Graph and its representations