📌  相关文章
📜  使用N个顶点构造图,其中K对顶点之间的最短距离为2

📅  最后修改于: 2021-04-22 03:44:10             🧑  作者: Mango

给定两个正整数NK ,任务是构造一个简单且连接的图,该图由N个顶点组成,每条边的长度为1个单位,以使得正好K个顶点对之间的最短距离为2 。如果无法构造图形,则打印-1 。否则,打印图形的边缘。

例子:

方法:请按照以下步骤解决问题:

  • 由于该图是简单且连接的,因此,最大可能的边数为Max (((N – 1)*(N – 2)))/ 2
  • 如果K大于Max ,则打印-1
  • 初始化一个数组,例如edge [] ,以存储图形的边缘。
  • 否则,首先连接所有顶点为1并将其存储在edge []中,然后连接所有成对的顶点(i,j) ,使i> = 2j> i并将其存储在edge []中
  • 最后,打印edge []数组的前((N – 1)+ Max – K)个元素。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
#include 
using namespace std;
 
// Function to construct the simple and
// connected graph such that the distance
// between exactly K pairs of vertices is 2
void constGraphWithCon(int N, int K)
{
 
    // Stores maximum possible count
    // of edges in a graph
    int Max = ((N - 1) * (N - 2)) / 2;
 
    // Base Case
    if (K > Max) {
        cout << -1 << endl;
        return;
    }
 
    // Stores edges of a graph
    vector > ans;
 
    // Connect all vertices of pairs (i, j)
    for (int i = 1; i < N; i++) {
        for (int j = i + 1; j <= N; j++) {
            ans.emplace_back(make_pair(i, j));
        }
    }
 
    // Print first ((N - 1) + Max - K)  elements
    // of edges[]
    for (int i = 0; i < (N - 1) + Max - K; i++) {
        cout << ans[i].first << " "
             << ans[i].second << endl;
    }
}
 
// Driver Code
int main()
{
    int N = 5, K = 3;
    constGraphWithCon(N, K);
 
    return 0;
}


C
// C program to implement
// the above approach
 
#include 
 
// Function to construct the simple and
// connected graph such that the distance
// between exactly K pairs of vertices is 2
void constGraphWithCon(int N, int K)
{
 
    // Stores maximum possible count
    // of edges in a graph
    int Max = ((N - 1) * (N - 2)) / 2;
 
    // Base Case
    if (K > Max) {
        printf("-1");
        return;
    }
 
    // Stores count of edges in a graph
    int count = 0;
 
    // Connect all vertices of pairs (i, j)
    for (int i = 1; i < N; i++) {
 
        for (int j = i + 1; j <= N; j++) {
 
            printf("%d %d\n", i, j);
 
            // Update
            count++;
 
            if (count == N * (N - 1) / 2 - K)
                break;
        }
 
        if (count == N * (N - 1) / 2 - K)
            break;
    }
}
 
// Driver Code
int main()
{
    int N = 5, K = 3;
    constGraphWithCon(N, K);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
     
static class pair
{
    int first, second;
     
    public pair(int first, int second) 
    {
        this.first = first;
        this.second = second;
    }   
}
 
// Function to conthe simple and connected
// graph such that the distance between
// exactly K pairs of vertices is 2
static void constGraphWithCon(int N, int K)
{
     
    // Stores maximum possible count
    // of edges in a graph
    int Max = ((N - 1) * (N - 2)) / 2;
 
    // Base Case
    if (K > Max)
    {
        System.out.print(-1 + "\n");
        return;
    }
 
    // Stores edges of a graph
    Vector ans = new Vector<>();
 
    // Connect all vertices of pairs (i, j)
    for(int i = 1; i < N; i++)
    {
        for(int j = i + 1; j <= N; j++)
        {
            ans.add(new pair(i, j));
        }
    }
 
    // Print first ((N - 1) + Max - K)  elements
    // of edges[]
    for(int i = 0; i < (N - 1) + Max - K; i++)
    {
        System.out.print(ans.get(i).first + " " +
                         ans.get(i).second +"\n");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 5, K = 3;
     
    constGraphWithCon(N, K);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to implement
# the above approach
 
# Function to construct the simple and
# connected graph such that the distance
# between exactly K pairs of vertices is 2
def constGraphWithCon(N, K):
   
    # Stores maximum possible count
    # of edges in a graph
    Max = ((N - 1) * (N - 2)) // 2
     
    # Base case
    if (K > Max):
        print(-1)
        return
     
    # Stores edges of a graph
    ans = []
 
    # Connect all vertices of pairs (i, j)
    for i in range(1, N):
        for j in range(i + 1, N + 1):
            ans.append([i, j])
             
    # Print first ((N - 1) + Max - K)  elements
    # of edges[]
    for i in range(0, (N - 1) + Max - K):
        print(ans[i][0], ans[i][1], sep = " ")
 
# Driver code
if __name__ == '__main__':
     
    N = 5
    K = 3
     
    constGraphWithCon(N, K)
 
# This code is contributed by MuskanKalra1


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
 
public class GFG{
     
class pair
{
    public int first, second;
     
    public pair(int first, int second) 
    {
        this.first = first;
        this.second = second;
    }   
}
 
// Function to conthe simple and connected
// graph such that the distance between
// exactly K pairs of vertices is 2
static void constGraphWithCon(int N, int K)
{
     
    // Stores maximum possible count
    // of edges in a graph
    int Max = ((N - 1) * (N - 2)) / 2;
 
    // Base Case
    if (K > Max)
    {
        Console.Write(-1 + "\n");
        return;
    }
 
    // Stores edges of a graph
    List ans = new List();
 
    // Connect all vertices of pairs (i, j)
    for(int i = 1; i < N; i++)
    {
        for(int j = i + 1; j <= N; j++)
        {
            ans.Add(new pair(i, j));
        }
    }
 
    // Print first ((N - 1) + Max - K)  elements
    // of edges[]
    for(int i = 0; i < (N - 1) + Max - K; i++)
    {
        Console.Write(ans[i].first + " " +
                         ans[i].second +"\n");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 5, K = 3;
    constGraphWithCon(N, K);
}
}
 
// This code is contributed by 29AjayKumar


输出:
1 2
1 3
1 4
1 5
2 3
2 4
2 5

时间复杂度: O(N 2 )
辅助空间: O(N 2 )