当前位置:网站首页>Undirected graph -- computing the degree of a node in compressed storage
Undirected graph -- computing the degree of a node in compressed storage
2022-06-13 00:49:00 【Feiyichai】
Undirected graph —— Computing the degree of a node in compressed storage
- Problem description
Yes n An undirected graph with nodes , Use adjacency storage as the storage structure , To reduce storage space , Use the array to save only the lower triangular matrix in the way of row master mapping , Please give the mapping formula , And write an algorithm to calculate the degree of a given node .
- Algorithmic thought
The given adjacency matrix is shown in the figure , If you use an array to compress a storage matrix , And only the lower triangle part is stored , Then the formula stored in the element is as follows
As shown in the figure :
So the element aij The subscript of in the array is (i-1)(i-2)/2+j-1
i-2: Altogether i-2 term
i-1: First and last
(i-1)(i-2)/2: The first i The number of all line elements before the line
j-1:aij The first i The number of elements in front of the row and column
Algorithm description : Because after compressed storage ,aij The information of upper triangle degree will be lost , Therefore, the degree of nodes can only be calculated by its in degree and the out degree of its lower triangular part .
How to calculate :
1) First consider the second i Out of line , Just calculate according to the formula j Element number and number j The storage subscript of the row element , And judge whether it is 1, To make statistics, there are :
for(j=1;j<i;j++){
k=(i-1)*(i-2)/2+j-1;
if(a[k]==1) count++;
}
2) Consider penetration : Consider the j The elements of the column , Also calculate the subscript , Judge whether it is 1 that will do .
for(j=n;j>i;j--){
k=(j-1)*(j-2)/2+i-1;
if(a[k]==1) {
count++;
}
}
- Algorithm implementation
#include<stdio.h>
int CountD(int a[],int n,int key){
if(n==0) return 0;
int i=key,j,count=0,k;
for(j=n;j>i;j--){
k=(j-1)*(j-2)/2+i-1;
if(a[k]==1) {
count++;
}
}
for(j=1;j<i;j++){
k=(i-1)*(i-2)/2+j-1;
if(a[k]==1) count++;
}
return count;
}
int main(){
int a[]={
1,
1,0,
0,1,1,
1,1,0,1};
int c=CountD(a,5,3);//5 Stage serial number is 3 The degree of node
printf("%d",c);
}
边栏推荐
- ImportError: cannot import name &#039;get_ora_doc&#039; from partially initialized module
- Stack overflow learning summary
- Some basic design knowledge
- 什么是 Meebits?一个简短的解释
- [server data recovery] successful cases of data loss recovery during data migration between storage servers
- kotlin 协程withContext切换线程
- .net core 抛异常对性能影响的求证之路
- Why is there always a space (63 or 2048 sectors) in front of the first partition when partitioning a disk
- Introduction to ROS from introduction to mastery (zero) tutorial
- Zhouchuankai, Bank of Tianjin: from 0 to 1, my experience in implementing distributed databases
猜你喜欢
通过抓包下载钉钉直播回放
MySQL locates the position of the character in the string String substitution
Development notes of Mongoose
antdPro - ProTable 实现两个选择框联动效果
1. Google grpc framework source code analysis Hello World
In / out / inout details of MySQL stored procedures
[GYCTF2020]Ezsqli --BUUCTF
Influence of higher order poles on waveform
Maybe we can figure out the essence of the Internet after the dust falls
The scope builder coroutinescope, runblocking and supervisorscope of kotlin collaboration processes run synchronously. How can other collaboration processes not be suspended when the collaboration pro
随机推荐
(01).NET MAUI实战 建项目
[server data recovery] successful cases of data loss recovery during data migration between storage servers
Three kinds of thinking make me reborn
MySQL lpad() and rpad() concatenate string functions with specified length
什么是 Meebits?一个简短的解释
JPA execution failed in scheduled task -executing an update/delete query transactionrequiredexception
ROS2之OpenCV人脸识别foxy~galactic~humble
Influence of higher order poles on waveform
STM32 USB Basics
Four startup modes of kotlin collaboration
三栏简约typecho主题Lanstar/蓝星typecho主题
硬(磁)盘(一)
什么是 dummy change?
Canvas game lower level 100
Mysql批量插入数据时如何解决重复问题?
Basic operations of FreeMarker
How to solve the duplication problem when MySQL inserts data in batches?
为什么磁盘分区的时候,第一个分区前面总有一段空间(63或者2048个扇区)
[GXYCTF2019]禁止套娃--详解
Canvas game 2048 free map size