当前位置:网站首页>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
边栏推荐
- 嵌入式开发的7大原罪
- El table table - get the row and column you click & the sort of El table and sort change, El table column and sort method & clear sort clearsort
- 一行代码可以做些什么?
- Uni app app half screen continuous code scanning
- PostgreSQL 修改数据库用户的密码
- Hill | insert sort
- High precision face recognition based on insightface, which can directly benchmark hongruan
- R语言做文本挖掘 Part4文本分类
- Torch Cookbook
- string的底层实现
猜你喜欢

跨分片方案 总结

50个常用的Numpy函数解释,参数和使用示例

袁小林:安全不只是标准,更是沃尔沃不变的信仰和追求

抖音将推独立种草App“可颂”,字节忘不掉小红书?

Why is the cluster mode of spark on Yan better than the client mode

Enhance network security of kubernetes with cilium

Quick news: the flybook players' conference is held online; Wechat payment launched "education and training service toolbox"

PostgreSQL install GIS plug-in create extension PostGIS_ topology

PostgreSQL modifies the password of the database user
![[redis design and implementation] part I: summary of redis data structure and objects](/img/2e/b147aa1e23757519a5d049c88113fe.png)
[redis design and implementation] part I: summary of redis data structure and objects
随机推荐
guava: Multiset的使用
Acdreamoj1110 (multiple backpacks)
美国科技行业结束黄金时代,芯片求售、裁员3万等哀声不断
【Redis设计与实现】第一部分 :Redis数据结构和对象 总结
Efficiency tool +wps check box shows the solution to the sun problem
Why is the cluster mode of spark on Yan better than the client mode
FZU 1686 龙之谜 重复覆盖
【力扣刷题】32. 最长有效括号
What can one line of code do?
互联网快讯:吉利正式收购魅族;胰岛素集采在31省全面落地
Replace Internet TV set-top box application through digital TV and broadband network
npm run dev启动项目报错 document is not defined
Dialogue with Jia Yangqing, vice president of Alibaba: pursuing a big model is not a bad thing
1292_ Implementation analysis of vtask resume() and xtask resume fromisr() in freeros
What about the spectrogram
SDL2来源分析7:演出(SDL_RenderPresent())
Ravendb starts -- document metadata
Reinforcement learning - learning notes 5 | alphago
Comparison between multithreaded CAS and synchronized
PostgreSQL modifies the password of the database user