Number of cycles formed by joining vertices of n sided polygon at the center
Given an N-sided regular polygon, we have connected all the vertices at the center of the polygon, thus dividing the polygon into N equal parts. Our task is to the Count of the total number of cycles in the polygon.
Note: A Cycle is a closed-loop starting and ending on same point.
Input: N = 3
When a 3 sided polygon is connected by vertices at the center then we get 7 cycles possible for it as shown in the image.
Input: N = 5
When a 5 sided polygon is connected by vertices at the center then we get 21 cycles possible for it as shown in the image.
Approach: To the problem mentioned above we are supposed to count the total number of closed loops possible in the given polygon after division. The approach is based upon Mathematical Pattern. There will be N cycles already created due to the division of polygon. One out of N blocks will form a cycle with rest (N – 1) blocks. The remaining (N – 1) blocks will form cycle with other (N – 2) blocks. So the total cycles we have can be found out using the formula given below:
Total Cycles = N + 1 * (N – 1) + (N – 1) * (N – 2)
Total Cycles = 2 * N – 1) + (N – 1) * (N – 2)
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.