📜  无向图中具有最大连接的节点数

📅  最后修改于: 2022-05-13 01:56:07.243000             🧑  作者: Mango

无向图中具有最大连接的节点数

给定一个有N个节点和E条边的无向图,然后是E条边连接。任务是找到具有最大连接的节点数

例子:

方法:可以通过将每个节点连接节点数存储在向量中来解决该任务。然后,找到任何节点的最大连接节点并获取其计数

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the number of nodes
// with maximum connections
void get(map > graph)
{
    // Stores the number of connections
    // of each node
    vector v;
 
    // Stores the maximum connections
    int mx = -1;
    for (int i = 0; i < graph.size(); i++) {
        v.push_back(graph[i].size());
        mx = max(mx, (int)graph[i].size());
    }
 
    // Resultant count
    int cnt = 0;
    for (auto i : v) {
        if (i == mx)
            cnt++;
    }
 
    cout << cnt << endl;
}
 
// Drive Code
int main()
{
    map > graph;
    int nodes = 10, edges = 13;
 
    // 1
    graph[1].push_back(4);
    graph[4].push_back(1);
 
    // 2
    graph[2].push_back(3);
    graph[3].push_back(2);
 
    // 3
    graph[4].push_back(5);
    graph[5].push_back(4);
 
    // 4
    graph[3].push_back(9);
    graph[9].push_back(3);
 
    // 5
    graph[6].push_back(9);
    graph[9].push_back(6);
 
    // 6
    graph[3].push_back(8);
    graph[8].push_back(3);
 
    // 7
    graph[10].push_back(4);
    graph[4].push_back(10);
 
    // 8
    graph[2].push_back(7);
    graph[7].push_back(2);
 
    // 9
    graph[3].push_back(6);
    graph[6].push_back(3);
 
    // 10
    graph[2].push_back(8);
    graph[8].push_back(2);
 
    // 11
    graph[9].push_back(2);
    graph[2].push_back(9);
 
    // 12
    graph[1].push_back(10);
    graph[10].push_back(1);
 
    // 13
    graph[9].push_back(10);
    graph[10].push_back(9);
 
    get(graph);
    return 0;
}


Java
// Java program for the above approach
import java.util.ArrayList;
import java.util.HashMap;
 
class GFG {
 
  // Function to count the number of nodes
  // with maximum connections
  static void get(HashMap> graph)
  {
     
    // Stores the number of connections
    // of each node
    ArrayList v = new ArrayList();
 
    // Stores the maximum connections
    int mx = -1;
    for (int i = 0; i < graph.size(); i++) {
      v.add(graph.get(i).size());
      mx = Math.max(mx, (int) graph.get(i).size());
    }
 
    // Resultant count
    int cnt = 0;
    for (int i : v) {
      if (i == mx)
        cnt++;
    }
 
    System.out.println(cnt);
  }
 
  // Drive Code
  public static void main(String args[]) {
 
    HashMap> graph = new HashMap>();
    int nodes = 10, edges = 13;
 
    for (int i = 0; i <= nodes; i++) {
      graph.put(i, new ArrayList<>());
    }
    // 1
    graph.get(1).add(4);
    graph.get(4).add(1);
 
    // 2
    graph.get(2).add(3);
    graph.get(3).add(2);
 
    // 3
    graph.get(4).add(5);
    graph.get(5).add(4);
 
    // 4
    graph.get(3).add(9);
    graph.get(9).add(3);
 
    // 5
    graph.get(6).add(9);
    graph.get(9).add(6);
 
    // 6
    graph.get(3).add(8);
    graph.get(8).add(3);
 
    // 7
    graph.get(10).add(4);
    graph.get(4).add(10);
 
    // 8
    graph.get(2).add(7);
    graph.get(7).add(2);
 
    // 9
    graph.get(3).add(6);
    graph.get(6).add(3);
 
    // 10
    graph.get(2).add(8);
    graph.get(8).add(2);
 
    // 11
    graph.get(9).add(2);
    graph.get(2).add(9);
 
    // 12
    graph.get(1).add(10);
    graph.get(10).add(1);
 
    // 13
    graph.get(9).add(10);
    graph.get(10).add(9);
 
    get(graph);
  }
}
 
// This code is contributed by saurabh_jaiswal.


Python3
# Python code for the above approach
 
# Function to count the number of nodes
# with maximum connections
def get(graph):
 
    # Stores the number of connections
    # of each node
    v = [];
 
    # Stores the maximum connections
    mx = -1;
    for arr in graph.values():
        v.append(len(arr));
        mx = max(mx, (len(arr)));
     
    # Resultant count
    cnt = 0;
    for i in v:
        if (i == mx):
            cnt += 1
     
    print(cnt)
 
# Drive Code
graph = {}
 
nodes = 10
edges = 13;
for i in range(1, nodes + 1):
    graph[i] = []
 
# 1
graph[1].append(4);
graph[4].append(1);
 
# 2
graph[2].append(3);
graph[3].append(2);
 
# 3
graph[4].append(5);
graph[5].append(4);
 
# 4
graph[3].append(9);
graph[9].append(3);
 
# 5
graph[6].append(9);
graph[9].append(6);
 
# 6
graph[3].append(8);
graph[8].append(3);
 
# 7
graph[10].append(4);
graph[4].append(10);
 
# 8
graph[2].append(7);
graph[7].append(2);
 
# 9
graph[3].append(6);
graph[6].append(3);
 
# 10
graph[2].append(8);
graph[8].append(2);
 
# 11
graph[9].append(2);
graph[2].append(9);
 
# 12
graph[1].append(10);
graph[10].append(1);
 
# 13
graph[9].append(10);
graph[10].append(9);
 
get(graph);
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG {
 
  // Function to count the number of nodes
  // with maximum connections
  static void get(Dictionary> graph)
  {
 
    // Stores the number of connections
    // of each node
    List v = new List();
 
    // Stores the maximum connections
    int mx = -1;
    for (int i = 0; i < graph.Count; i++) {
      v.Add(graph[i].Count);
      mx = Math.Max(mx, (int) graph[i].Count);
    }
 
    // Resultant count
    int cnt = 0;
    foreach (int i in v) {
      if (i == mx)
        cnt++;
    }
 
    Console.WriteLine(cnt);
  }
 
  // Drive Code
  public static void Main(String []args) {
 
    Dictionary> graph = new Dictionary>();
    int nodes = 10;
 
    for (int i = 0; i <= nodes; i++) {
      graph.Add(i, new List());
    }
    // 1
    graph[1].Add(4);
    graph[4].Add(1);
 
    // 2
    graph[2].Add(3);
    graph[3].Add(2);
 
    // 3
    graph[4].Add(5);
    graph[5].Add(4);
 
    // 4
    graph[3].Add(9);
    graph[9].Add(3);
 
    // 5
    graph[6].Add(9);
    graph[9].Add(6);
 
    // 6
    graph[3].Add(8);
    graph[8].Add(3);
 
    // 7
    graph[10].Add(4);
    graph[4].Add(10);
 
    // 8
    graph[2].Add(7);
    graph[7].Add(2);
 
    // 9
    graph[3].Add(6);
    graph[6].Add(3);
 
    // 10
    graph[2].Add(8);
    graph[8].Add(2);
 
    // 11
    graph[9].Add(2);
    graph[2].Add(9);
 
    // 12
    graph[1].Add(10);
    graph[10].Add(1);
 
    // 13
    graph[9].Add(10);
    graph[10].Add(9);
 
    get(graph);
  }
}
 
// This code is contributed by shikhasingrajput


Javascript



输出
3

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