当前位置:网站首页>Basic introduction of figure
Basic introduction of figure
2022-07-06 21:44:00 【HairLossException】
List of articles
chart
A graph is a data structure , Where a node can have zero or more adjacent elements . The connection between two nodes is called an edge .
A node can also be called a vertex . Graphs can be divided into digraphs and undirected graphs
- Undirected graph : Edges connect only two vertices , There is no other meaning .
- Directed graph : Edges not only connect two vertices , And has direction .
The representation of graphs
There are two ways to represent graphs : Adjacency matrix ( Two dimensional array representation )、 Adjacency list ( The linked list shows )
The code for creating the diagram
public class Graph {
/* Store a collection of vertices */
private ArrayList<String> vertexList;
/* Store the adjacency matrix corresponding to the graph */
private int[][] edges;
/* The number of edges */
private int numOfEdges;
public static void main(String[] args) {
String[] vertexs = {
"A","B","C","D","E"};
/* Create a graph object */
Graph graph = new Graph(vertexs.length);
/* Add vertex */
for (String vertex:vertexs) {
graph.insertVertex(vertex);
}
/* Add edge */
graph.insertEdges(0,1,1);
graph.insertEdges(0,2,1);
graph.insertEdges(1,2,1);
graph.insertEdges(1,3,1);
graph.insertEdges(1,4,1);
graph.show();
}
public Graph(int n){
/* Initialize matrix and set */
edges = new int[n][n];
vertexList = new ArrayList<>(n);
numOfEdges = 0;
}
/* Access vertex */
public void insertVertex(String vertex){
vertexList.add(vertex);
}
/** * Insert edge * @param v1 Subscript of vertex in adjacency matrix * @param v2 Subscript of vertex in adjacency matrix * @param weight A weight */
public void insertEdges(int v1,int v2,int weight){
edges[v1][v2] = weight;
edges[v2][v1] = weight;
numOfEdges++;
}
/* Get the number of vertices */
public int getNumOfVertex(){
return vertexList.size();
}
/* Get the number of sides */
public int getNumOfEdges(){
return numOfEdges;
}
/* According to the subscript index Get the corresponding data */
public String getValueByIndex(int index){
return vertexList.get(index);
}
/* return v1v2 A weight */
public int getWeight(int v1,int v2){
return edges[v1][v2];
}
/* Displays the adjacency matrix of the graph */
public void show(){
for (int[] arr : edges) {
System.out.println(Arrays.toString(arr));
}
}
}
Print the adjacency matrix of the graph
[0, 1, 1, 0, 0]
[1, 0, 1, 1, 1]
[1, 1, 0, 0, 0]
[0, 1, 0, 0, 0]
[0, 1, 0, 0, 0]
Process finished with exit code 0
边栏推荐
- 基于InsightFace的高精度人脸识别,可直接对标虹软
- @Detailed differences among getmapping, @postmapping and @requestmapping, with actual combat code (all)
- Leetcode topic [array] -118 Yang Hui triangle
- jvm:大对象在老年代的分配
- 1292_FreeROS中vTaskResume()以及xTaskResumeFromISR()的实现分析
- 华为在多个行业同时出击,吓人的技术让欧美企业瑟瑟发抖
- Redistemplate common collection instructions opsforlist (III)
- PostgreSQL install GIS plug-in create extension PostGIS_ topology
- What's the best way to get TFS to output each project to its own directory?
- Why is the cluster mode of spark on Yan better than the client mode
猜你喜欢
Vit paper details
麦趣尔砸了小众奶招牌
Reptile practice (V): climbing watercress top250
[Digital IC manual tearing code] Verilog automatic beverage machine | topic | principle | design | simulation
numpy 下载安装
guava:Collections.unmodifiableXXX创建的collection并不immutable
Why does MySQL index fail? When do I use indexes?
Yuan Xiaolin: safety is not only a standard, but also Volvo's unchanging belief and pursuit
数字化转型挂帅复产复工,线上线下全融合重建商业逻辑
C how to set two columns comboboxcolumn in DataGridView to bind a secondary linkage effect of cascading events
随机推荐
R language for text mining Part4 text classification
Thinking about agile development
Explain ESM module and commonjs module in simple terms
ROS error: could not find a package configuration file provided by "move_base“
Binary tree node at the longest distance
R语言做文本挖掘 Part4文本分类
Divide candy
In JS, string and array are converted to each other (I) -- the method of converting string into array
C language: comprehensive application of if, def and ifndef
1292_ Implementation analysis of vtask resume() and xtask resume fromisr() in freeros
Digital transformation takes the lead to resume production and work, and online and offline full integration rebuilds business logic
Torch Cookbook
代理和反向代理
3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
MySQL - 事务(Transaction)详解
1D convolution detail
Yyds dry inventory run kubeedge official example_ Counter demo counter
VIM basic configuration and frequently used commands
uni-app App端半屏连续扫码
在最长的距离二叉树结点