当前位置:网站首页>Recognition and storage of Graphs
Recognition and storage of Graphs
2022-07-24 08:03:00 【bj_ hacker】
Recognition and storage of graphs
The understanding of graph
form
The graph is mainly composed of a two tuple G=<V,E>,V Represents a node set ,E Represents an edge set ,w Represents the weight on an edge ( Define your own )
Direction
Graph Division Directed graph and undirected graph
Directed graph
All edge sets have directions , such as A->B,A You can go to B, however B Not to A;
Undirected graph
All edge sets have no direction , such as A<–>B,A You can go to B And B You can go to A.
Degrees
Undirected graph
The degree of a point in an undirected graph refers to the number of edges connected by this point , Undirected graph degree = The degree of
Directed graph
The degree of digraph is not necessarily equal to the degree
Figure of the storage
Points are very easy to store , But it's not easy to save , But we can turn the edge into two points .
Adjacency matrix
Storage principle
Adjacency matrix uses a two-dimensional array to store the edges of the graph ;
a [ i ] [ j ] Representation node i To the node j There is one side , A weight of a[ i ] [ j ]
The code template
// Undirected graph
// Definition
const int maxn=1e3+10;
int a[maxn][maxn];
//...
// Bordering
for(int i=1;i<=m;i++){
int u,v;
scanf("%d%d",u,v);
a[u][v]=1;
a[v][u]=1;// According to the title requirements , Directed graph without this line of code
}
Adjacency list
Storage principle
Adjacency table can be based on the requirements of the opposite side , Open space , Like a queue ;
The code template
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+10;
int n,m;
// Definition
vector< pair<int,int> >e[maxn];
int main(){
scanf("%d%d",&n,&m);
// Bordering
for(int i=1;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
e[u].push_back(make_pair(v,w));
e[v].push_back(make_pair(u,w));// According to the title requirements , Directed graph without this line of code
}
// Call operation
for(int i=1;i<=n;i++){
for(int j=0;j<e[i].size();j++){
//……
}
}
return 0;
}
Chain forward star
Storage principle
The edge that will be related to a point , Store in a linked list
The code template
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+10;
int n,m,tot;
// Definition
int head[maxn];
struct node {
int v,w,nxt;
}e[maxn];
void add(int u,int v,int w){
e[++tot].v=v;// Update the number of edges and record the new edge arc head
e[tot].w=w;// Record weights
e[tot].nxt=head[u];// Insert the list
head[u]=tot;// Update pointer
}
int main(){
scanf("%d%d",&n,&m);
// Bordering
for(int i=1;i<=m;i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
return 0;
}
Storage summary of graphs
Adjacency matrix
Spatial complexity O(n^2)
Time complexity of edge checking O(1)Adjacency list
Spatial complexity O(n)
Time complexity of edge checking O(n)
Space is unstable
ps: Adjacency table space will generally double for youChain forward star
Spatial complexity O(n)
Edge checking complexity O(n)
Spatial stability
边栏推荐
- Intelligent robots and intelligent systems (Professor Zhengzheng of Dalian University of Technology) -- 4. Autonomous robots
- 33-SparkSql的介绍、DataFrame和DataSet
- Introduction to webmethods
- Learn - use do... While loop according to the formula e=1+1/1+ 1/2!+ 1/3!+…+ 1/n! Calculate the value of E (accuracy is 1e-6)
- Intelligent robots and intelligent systems (Professor Zhengzheng of Dalian University of Technology) -- 3. Industrial robots
- Kotlin coroutine (II): scope and cancellation
- Multiple optimization methods print prime numbers between 100 and 200
- Install librosa using Tsinghua image
- Devops essay
- Detailed explanation of VAO
猜你喜欢

Hcip 13th day notes

Decision tree - ID3, C4.5, cart

Intelligent robots and intelligent systems (Professor Zheng Zheng of Dalian University of Technology) -- 1. robots and mobile robots

Thesis reading: geotransformer

Do you want to have a robot that can make cartoon avatars in three steps?

Introduction of some functions or methods in DGL Library
![[matlab] (III) application of MATLAB in Higher Mathematics](/img/ff/72b13fb597d5bdf3a989dd86cb6888.png)
[matlab] (III) application of MATLAB in Higher Mathematics

*Yolo5 learning * data experiment based on yolo5 face combined with attention model CBAM

What is the NFT concept.. Fully understand NFT market, technology and cases

Digital twin demonstration project -- Talking about simple pendulum (3) solid model exploration
随机推荐
Digital twin demonstration project -- Talking about simple pendulum (2) vision exploration and application scenarios
【线性代数】深入理解矩阵乘法、对称矩阵、正定矩阵
Common DOS commands
The solution of unable to import custom library in pycharm
[target detection] IOU (intersection and combination ratio)
Kotlin coroutine (I): foundation and deepening
45. Jumping game II
Implement a queue with two stacks.
1005. Maximized array sum after K negations
Hegong sky team vision training Day2 - traditional vision, opencv basic operation
33-SparkSql的介绍、DataFrame和DataSet
Detailed notes on pytoch building neural network
The vision group of Hegong University Sky team trained day3 - machine learning, strengthened the use of Yolo models, and learned pumpkin books and watermelon books
学习笔记总结篇(一)
Case practice - panoramic image mosaic: feature matching method
Zhouzhihua machine learning watermelon book chapter 2 model evaluation and selection - accuracy and model generalization evaluation method, self-help method and integrated learning
EZDML逆向工程导入数据库分析实操教程
MySQL -- subquery scalar subquery
SIFT feature point extraction
hcip第八天笔记