当前位置:网站首页>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
边栏推荐
- Nodejs tutorial expressjs article quick start
- [sliding window] group B of the 9th Landbridge cup provincial tournament: log statistics
- VIM basic configuration and frequently used commands
- First batch selected! Tencent security tianyufeng control has obtained the business security capability certification of the ICT Institute
- In JS, string and array are converted to each other (II) -- the method of converting array into string
- mysql根据两个字段去重
- Redistemplate common collection instructions opsforzset (VI)
- document. Usage of write () - write text - modify style and position control
- R语言做文本挖掘 Part4文本分类
- Shake Sound poussera l'application indépendante de plantation d'herbe "louable", les octets ne peuvent pas oublier le petit livre rouge?
猜你喜欢
随机推荐
数字化转型挂帅复产复工,线上线下全融合重建商业逻辑
JS learning notes OO create suspicious objects
From campus to Tencent work for a year of those stumbles!
Thinking about agile development
Nodejs教程之Expressjs一篇文章快速入门
document. Usage of write () - write text - modify style and position control
袁小林:安全不只是标准,更是沃尔沃不变的信仰和追求
[in depth learning] pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
High precision face recognition based on insightface, which can directly benchmark hongruan
Torch Cookbook
在最长的距离二叉树结点
R语言做文本挖掘 Part4文本分类
Binary tree node at the longest distance
JS according to the Chinese Alphabet (province) or according to the English alphabet - Za sort &az sort
抖音將推獨立種草App“可頌”,字節忘不掉小紅書?
Quick access to video links at station B
c语言char, wchar_t, char16_t, char32_t和字符集的关系
PostgreSQL 安装gis插件 CREATE EXTENSION postgis_topology
代理和反向代理
Why does MySQL index fail? When do I use indexes?









