当前位置:网站首页>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
边栏推荐
- Replace Internet TV set-top box application through digital TV and broadband network
- JS according to the Chinese Alphabet (province) or according to the English alphabet - Za sort &az sort
- b站视频链接快速获取
- 抖音將推獨立種草App“可頌”,字節忘不掉小紅書?
- Proxy and reverse proxy
- Dialogue with Jia Yangqing, vice president of Alibaba: pursuing a big model is not a bad thing
- Yuan Xiaolin: safety is not only a standard, but also Volvo's unchanging belief and pursuit
- 14 years Bachelor degree, transferred to software testing, salary 13.5k
- Sequoia China, just raised $9billion
- Absolute primes (C language)
猜你喜欢
美国科技行业结束黄金时代,芯片求售、裁员3万等哀声不断
一行代码可以做些什么?
【力扣刷题】32. 最长有效括号
Five wars of Chinese Baijiu
Aggregate function with key in spark
跨分片方案 总结
JS method to stop foreach
对话阿里巴巴副总裁贾扬清:追求大模型,并不是一件坏事
Dialogue with Jia Yangqing, vice president of Alibaba: pursuing a big model is not a bad thing
Microsoft technology empowerment position - February course Preview
随机推荐
R3live notes: image processing section
JS get array subscript through array content
039. (2.8) thoughts in the ward
C language char, wchar_ t, char16_ t, char32_ Relationship between T and character set
document. Usage of write () - write text - modify style and position control
The role of applicationmaster in spark on Yan's cluster mode
语谱图怎么看
JS学习笔记-OO创建怀疑的对象
首批入选!腾讯安全天御风控获信通院业务安全能力认证
JS according to the Chinese Alphabet (province) or according to the English alphabet - Za sort &az sort
Dialogue with Jia Yangqing, vice president of Alibaba: pursuing a big model is not a bad thing
Yuan Xiaolin: safety is not only a standard, but also Volvo's unchanging belief and pursuit
Five wars of Chinese Baijiu
Absolute primes (C language)
爬虫实战(五):爬豆瓣top250
Binary tree node at the longest distance
麦趣尔砸了小众奶招牌
50 commonly used numpy function explanations, parameters and usage examples
Z function (extended KMP)
Michael smashed the minority milk sign