📌  相关文章
📜  一个无向图的所有节点的度数之和

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

给定一个图的边列表,我们必须找到一个无向图的所有节点的度数之和。
例子

例子:

Input : edge list : (1, 2), (2, 3), (1, 4), (2, 4)  
Output : sum= 8

蛮力方法
我们将添加图形的每个节点的度数并打印总和。

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// returns the sum of degree of all
// the nodes in a undirected graph
int count(int edges[][2], int len, int n)
{
    int degree[n + 1] = { 0 };
 
    // compute the degree of each node
    for (int i = 0; i < len; i++) {
 
        // increase the degree of the
        // nodes
        degree[edges[i][0]]++;
        degree[edges[i][1]]++;
    }
 
    // calculate the sum of degree
    int sum = 0;
    for (int i = 1; i <= n; i++)
        sum += degree[i];
 
    return sum;
}
 
// main function
int main()
{
    // the edge list
    int edges[][2] = { { 1, 2 },
                       { 2, 3 },
                       { 1, 4 },
                       { 2, 4 } };
    int len = sizeof(edges) / (sizeof(int) * 2), n = 4;
 
    // display the result
    cout << "sum = " << count(edges, len, n) << endl;
    return 0;
}


Java
// Java implementation of the approach
class GFG {
 
    // returns the sum of degree of all
    // the nodes in a undirected graph
    static int count(int edges[][], int len, int n)
    {
        int degree[] = new int[n + 1];
 
        // compute the degree of each node
        for (int i = 0; i < len; i++) {
 
            // increase the degree of the
            // nodes
            degree[edges[i][0]]++;
            degree[edges[i][1]]++;
        }
 
        // calculate the sum of degree
        int sum = 0;
        for (int i = 1; i <= n; i++)
            sum += degree[i];
 
        return sum;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // the edge list
        int edges[][] = { { 1, 2 },
                          { 2, 3 },
                          { 1, 4 },
                          { 2, 4 } };
        int len = edges.length, n = 4;
 
        // display the result
        System.out.println("sum = " + count(edges, len, n));
    }
}
 
// This code has been contributed by 29AjayKumar


Python3
# Python 3 implementation of above approach
 
# returns the sum of degree of all
# the nodes in a undirected graph
def count(edges, len1, n):
    degree = [0 for i in range(n + 1)]
 
    # compute the degree of each node
    for i in range(len1):
        # increase the degree of the
        # nodes
        degree[edges[i][0]] += 1
        degree[edges[i][1]] += 1
 
    # calculate the sum of degree
    sum = 0
    for i in range(1, n + 1, 1):
        sum += degree[i]
 
    return sum
 
# main function
if __name__ == '__main__':
    # the edge list
    edges = [[1, 2], [2, 3], [1, 4], [2, 4]]
    len1 = len(edges)
    n = 4
 
    # display the result
    print("sum =", count(edges, len1, n))
     
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
 
class GFG {
 
    // returns the sum of degree of all
    // the nodes in a undirected graph
    static int count(int[][] edges, int len, int n)
    {
        int[] degree = new int[n + 1];
 
        // compute the degree of each node
        for (int i = 0; i < len; i++) {
 
            // increase the degree of the
            // nodes
            degree[edges[i][0]]++;
            degree[edges[i][1]]++;
        }
 
        // calculate the sum of degree
        int sum = 0;
        for (int i = 1; i <= n; i++)
            sum += degree[i];
 
        return sum;
    }
 
    // Driver code
    public static void Main()
    {
        // the edge list
        int[][] edges = new int[][] { new int[] { 1, 2 },
                                      new int[] { 2, 3 },
                                      new int[] { 1, 4 },
                                      new int[] { 2, 4 } };
        int len = edges.Length, n = 4;
 
        // display the result
        Console.WriteLine("sum = " + count(edges, len, n));
    }
}
 
// This code has been contributed by Code_Mech.


PHP


Javascript


C++
// C++ implementation of above approach
#include 
using namespace std;
 
// returns the sum of degree of all
// the nodes in a undirected graph
int count(int edges[][2], int len)
{
    return 2 * len;
}
 
// main function
int main()
{
    // the edge list
    int edges[][2] = { { 1, 2 },
                       { 2, 3 },
                       { 1, 4 },
                       { 2, 4 } };
    int len = sizeof(edges) / (sizeof(int) * 2);
 
    // display the result
    cout << "sum = " << count(edges, len) << endl;
    return 0;
}


Java
// Java implementation for above approach
class GFG {
 
    // returns the sum of degree of all
    // the nodes in a undirected graph
    static int count(int edges[][], int len)
    {
        return 2 * len;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // the edge list
        int edges[][] = { { 1, 2 },
                          { 2, 3 },
                          { 1, 4 },
                          { 2, 4 } };
        int len = edges.length;
 
        // display the result
        System.out.println("sum = " + count(edges, len));
    }
}
 
// This code contributed by Rajput-Ji


Python 3
# Python3 implementation of above approach
 
# returns the sum of degree of all
# the nodes in a undirected graph
def count(edges, length) :
     
    return 2 * length;
 
# Driver Code
if __name__ == "__main__" :
 
    # the edge list
    edges = [[ 1, 2 ],
             [ 2, 3 ],
             [ 1, 4 ],
             [ 2, 4 ]];
    length = len(edges);
 
    # display the result
    print("sum = ", count(edges, length));
 
# This code is contributed by Ryuga


C#
// C# implementation for above approach
using System;
 
class GFG {
 
    // returns the sum of degree of all
    // the nodes in a undirected graph
    static int count(int[, ] edges, int len)
    {
        return 2 * len;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        // the edge list
        int[, ] edges = { { 1, 2 },
                          { 2, 3 },
                          { 1, 4 },
                          { 2, 4 } };
        int len = edges.GetLength(0);
 
        // display the result
        Console.WriteLine("sum = " + count(edges, len));
    }
}
 
/* This code contributed by PrinciRaj1992 */


PHP


Javascript


输出:

sum = 8

有效的方法
如果我们获得有向图中的边数,那么我们可以找到该图的度数之和。让我们考虑一个没有边的图。如果我们添加一条边,我们将图的两个节点的度数增加 1,因此添加每条边后,节点的度数之和增加 2,因此度数之和为 2*e。

C++

// C++ implementation of above approach
#include 
using namespace std;
 
// returns the sum of degree of all
// the nodes in a undirected graph
int count(int edges[][2], int len)
{
    return 2 * len;
}
 
// main function
int main()
{
    // the edge list
    int edges[][2] = { { 1, 2 },
                       { 2, 3 },
                       { 1, 4 },
                       { 2, 4 } };
    int len = sizeof(edges) / (sizeof(int) * 2);
 
    // display the result
    cout << "sum = " << count(edges, len) << endl;
    return 0;
}

Java

// Java implementation for above approach
class GFG {
 
    // returns the sum of degree of all
    // the nodes in a undirected graph
    static int count(int edges[][], int len)
    {
        return 2 * len;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // the edge list
        int edges[][] = { { 1, 2 },
                          { 2, 3 },
                          { 1, 4 },
                          { 2, 4 } };
        int len = edges.length;
 
        // display the result
        System.out.println("sum = " + count(edges, len));
    }
}
 
// This code contributed by Rajput-Ji

Python3

# Python3 implementation of above approach
 
# returns the sum of degree of all
# the nodes in a undirected graph
def count(edges, length) :
     
    return 2 * length;
 
# Driver Code
if __name__ == "__main__" :
 
    # the edge list
    edges = [[ 1, 2 ],
             [ 2, 3 ],
             [ 1, 4 ],
             [ 2, 4 ]];
    length = len(edges);
 
    # display the result
    print("sum = ", count(edges, length));
 
# This code is contributed by Ryuga

C#

// C# implementation for above approach
using System;
 
class GFG {
 
    // returns the sum of degree of all
    // the nodes in a undirected graph
    static int count(int[, ] edges, int len)
    {
        return 2 * len;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        // the edge list
        int[, ] edges = { { 1, 2 },
                          { 2, 3 },
                          { 1, 4 },
                          { 2, 4 } };
        int len = edges.GetLength(0);
 
        // display the result
        Console.WriteLine("sum = " + count(edges, len));
    }
}
 
/* This code contributed by PrinciRaj1992 */

PHP


Javascript

   

输出:

sum = 8

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程