Fancy DSA Fancy DSA
数据结构
算法
LeetCode
  • 关于
  • 导航 (opens new window)
  • 分类
  • 标签
  • 归档
设计模式 (opens new window)
博客 (opens new window)
GitHub (opens new window)

Jonsam NG

想的更多,也要想的更远
数据结构
算法
LeetCode
  • 关于
  • 导航 (opens new window)
  • 分类
  • 标签
  • 归档
设计模式 (opens new window)
博客 (opens new window)
GitHub (opens new window)
  • 开始上手
  • Plan 计划
  • Roadmap 路线
  • 算法简介
  • Sort 排序

  • Search 搜索

  • Recursive 递归

  • Graph 图

    • BellmanFord [贝尔曼-福特算法]
    • BreadthFirstSearch [广度优先搜索/BFS]
    • BreadthFirstShortestPath [广度优先寻最短路径]
    • ConnectedComponents [连通元件]
    • Density [图的密度]
      • 介绍
      • 实现
      • 参考
    • DepthFirstSearch [深度优先搜索]
    • Dijkstra [迪杰斯特拉算法]
    • FloydWarshall [弗洛伊德算法]
    • KruskalMST [克鲁斯克尔算法]
    • NodeNeighbors [节点邻域]
    • NumberOfIslands [岛屿数量]
    • PrimMST [普林姆算法]
  • Tree 树

  • Math 数学

  • Hash 哈希

  • String 字符串

  • BitManipulation 位操纵

  • Backtracking 回溯

  • DynamicProgramming 动态规划

  • Cache 缓存

  • Array 数组

  • Ciphers 密码学

  • Conversions 转换

  • ProjectEuler 欧拉计划

  • 其他

  • 算法
  • Graph 图
jonsam
2022-05-01
目录

Density [图的密度]

# 介绍

# 图的密度

图的密度表示图中存在的边与图可包含的最大边数之间的比率。从概念上讲,它提供了一个关于一个图在边的连接(edge connectivity)方面的密度的概念。当我们有一个庞大的网络并想在网络中添加新的边时,它特别有用。此外,图的密度让我们了解到我们还能在网络中增加多少条边。

现在,在推导图密度的公式之前,让我们谈谈如何计算一个简单的有向图和无向图中的最大边数。让我们看一下一个简单的无向图 G1:

image

现在,我们要推导出一个标准的公式来计算一个简单的无向图中的最大边数:

image

同样地,我们可以用有向图中的两条双向边来代替每条无向边。因此,最大的边数可以用类似的公式来计算:

image

我们现在可以定义图形密度公式。图中存在的边除以该图可包含的最大边数。让我们看看一个简单无向图的公式:

image

同样地,我们可以为有向图重写前面的密度公式:

image

对于一个完全有向或无向图,密度总是 1。在一个完全有向图或无向图的情况下,它已经有最大数量的边,我们不能再增加任何边。此外,它还表明该图是完全密集的(fully dense)。

一个具有所有孤立顶点的图的密度为 0。 此外,它表示图中没有边,可以添加到图中的边的数量等于最大可添加的边的数量。

# 实现

# JavaScript

/*
The density of a network is a measure of how many edges exist proportional to
how many edges would exist in a complete network (where all possible edges).
https://networkx.org/documentation/networkx-1.9/reference/generated/networkx.classes.function.density.html
*/
function density (numberOfNodes, numberOfEdges, isDirected = false) {
  const multi = isDirected ? 1 : 2
  return (multi * numberOfEdges) / (numberOfNodes * (numberOfNodes - 1))
}
1
2
3
4
5
6
7
8
9

# 参考

  • Graph Density | Baeldung on Computer Science (opens new window)
编辑 (opens new window)
上次更新: 2022/10/18, 09:41:50
ConnectedComponents [连通元件]
DepthFirstSearch [深度优先搜索]

← ConnectedComponents [连通元件] DepthFirstSearch [深度优先搜索]→

最近更新
01
0-20题解
10-31
02
本章导读
10-31
03
算法与转换:Part1
10-28
更多文章>
Theme by Vdoing | Copyright © 2022-2022 Fancy DSA | Made by Jonsam by ❤
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式