当前位置:网站首页>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
边栏推荐
- Four common ways and performance comparison of ArrayList de duplication (jmh performance analysis)
- Fastjson parses JSON strings (deserialized to list, map)
- JS traversal array and string
- NPM run dev start project error document is not defined
- Fzu 1686 dragon mystery repeated coverage
- Run the deep network on PI and Jetson nano, and the program is killed
- One line by line explanation of the source code of anchor free series network yolox (a total of ten articles, you can change the network at will after reading it, if you won't complain to me)
- Microsoft technology empowerment position - February course Preview
- [redis design and implementation] part I: summary of redis data structure and objects
- 缓存更新策略概览(Caching Strategies Overview)
猜你喜欢
Internet News: Geely officially acquired Meizu; Intensive insulin purchase was fully implemented in 31 provinces
The difference between break and continue in the for loop -- break completely end the loop & continue terminate this loop
[sliding window] group B of the 9th Landbridge cup provincial tournament: log statistics
PostgreSQL 安装gis插件 CREATE EXTENSION postgis_topology
Numpy download and installation
Why is the cluster mode of spark on Yan better than the client mode
Four common ways and performance comparison of ArrayList de duplication (jmh performance analysis)
对话阿里巴巴副总裁贾扬清:追求大模型,并不是一件坏事
[redis design and implementation] part I: summary of redis data structure and objects
袁小林:安全不只是标准,更是沃尔沃不变的信仰和追求
随机推荐
Technology sharing | packet capturing analysis TCP protocol
Four common ways and performance comparison of ArrayList de duplication (jmh performance analysis)
Redistemplate common collection instructions opsforzset (VI)
[Li Kou brushing questions] one dimensional dynamic planning record (53 change exchanges, 300 longest increasing subsequence, 53 largest subarray and)
jvm:大对象在老年代的分配
Shake Sound poussera l'application indépendante de plantation d'herbe "louable", les octets ne peuvent pas oublier le petit livre rouge?
Michael smashed the minority milk sign
Redistemplate common collection instructions opsforhash (IV)
Vit paper details
c语言char, wchar_t, char16_t, char32_t和字符集的关系
WEB功能测试说明
Numpy download and installation
numpy 下载安装
Why does MySQL index fail? When do I use indexes?
【力扣刷题】一维动态规划记录(53零钱兑换、300最长递增子序列、53最大子数组和)
The role of applicationmaster in spark on Yan's cluster mode
Yuan Xiaolin: safety is not only a standard, but also Volvo's unchanging belief and pursuit
美国科技行业结束黄金时代,芯片求售、裁员3万等哀声不断
uni-app App端半屏连续扫码
Fzu 1686 dragon mystery repeated coverage