Given two integers N and K. The task is to find a connected graph with N vertices such that there are exactly K pairs (i, j) where the shortest distance between them is 2. If no such graph exists then print -1.
- The first-line output should be the number of edges(say m) in the graph and the next m lines should contain two numbers represents the edge between the vertices.
- In case of multiple answers print any of them.
Input: N = 5, K = 3
Input: N = 5, K = 8
Approach: An N-vertices connected graph has at least N-1 edges. The shortest distance of each pair is equal to 1. So obviously, it is clear that there doesn’t exist a solution if K > N * (N – 1) / 2 – (N – 1) = (N – 1) * (N – 2) / 2.
Conversely, it can be shown that there exists a solution if K ≤ (N – 1) * (N – 2) / 2 by constructing a graph that satisfies the condition. First, let’s consider the graph where each vertex is connected with all the other vertices then the shortest between any two vertices is 1. Now remove any K edges then there exist exactly K such pairs.
Below is the implementation of the above approach:
7 1 2 1 3 1 4 1 5 3 4 3 5 4 5
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.
- Convert undirected connected graph to strongly connected directed graph
- Check if a directed graph is connected or not
- Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)
- Check if a graph is Strongly, Unilaterally or Weakly connected
- Check if a given directed graph is strongly connected | Set 2 (Kosaraju using BFS)
- Queries to check if vertices X and Y are in the same Connected Component of an Undirected Graph
- Check if longest connected component forms a palindrome in undirected graph
- Check if a cycle of length 3 exists or not in a graph that satisfy a given condition
- Check if alternate path exists from U to V with smaller individual weight in a given Graph
- Connected Components in an undirected graph
- All vertex pairs connected with exactly k edges in a graph
- Cycles of length n in an undirected and connected graph
- Clone an undirected graph with multiple connected components
- Sum of the minimum elements in all connected components of an undirected graph
- Largest subarray sum of all connected components in undirected graph
- Program to count Number of connected components in an undirected graph
- Count of unique lengths of connected components for an undirected graph using STL
- Octal equivalents of connected components in Binary valued graph
- Maximum number of edges among all connected components of an undirected graph
- Maximum sum of values of nodes among all connected components of an undirected graph
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.