Graph 计算无向图中的度数之和

Graph 计算无向图中的度数之和,graph,pseudocode,Graph,Pseudocode,这是我的伪代码: for all u in V: degree[u]=0 for all (u,w) in E: degree[u] = degree[u]+1 for all u in V: twodegree[u]=0 for all (u,w) in E: twodegree[u]=twodegree[u]+degree[w] 这段代码试图计算无向图中u的邻居的度数之和(u拥有的邻居度数-#或与其相关的边的度数)。然而,我在理解这两个for循环时遇到了困难

这是我的伪代码:

for all u in V:
  degree[u]=0
  for all (u,w) in E:
    degree[u] = degree[u]+1
for all u in V:
  twodegree[u]=0
  for all (u,w) in E:
    twodegree[u]=twodegree[u]+degree[w]

这段代码试图计算无向图中u的邻居的度数之和(u拥有的邻居度数-#或与其相关的边的度数)。然而,我在理解这两个for循环时遇到了困难。有人能解释一下什么是degree[u]=degree[u]+1和twodgree[u]=twodgree[u]+degree[w]在做什么(我知道它在计算度数之和,但我不知道怎么做)有人能解释一下这个算法是如何在u和w方面工作的吗?

这个算法假设顶点在列表V中表示为索引。边在列表E中表示为元组。我假设这个算法是基于E中的“for all(u,w)”,用于有向边的。有向边是只向一个方向移动的边,因此在元组(u,w)中,u将是边离开以转到w的节点

    #first loop will calculate the number of edges directly adjacent to each vertex
    for all u in V:
      degree[u]=0  # initializes the number to 0
      for all (u,w) in E:  #find all tuples that contain u
        degree[u] = degree[u]+1 #add 1 for each edge in list
    # The second loop will calculate a slightly different metric but
    # it follows the same algorithm as the first
    for all u in V:
      twodegree[u]=0
      for all (u,w) in E:
        twodegree[u]=twodegree[u]+degree[w]
“for all(u,w)in E:”包含一个隐含的条件,该条件创建一个E的子列表,该子列表仅包含以u作为第一个元素的元组