JAVA data structure -- adjacency table of graph storage structure

The data structure series is a note that I have learned to make, which will be continuously updated. The source code is ...

The data structure series is a note that I have learned to make, which will be continuously updated. The source code is shared in github: data structure Of course, you can also get it from the following code snippet
Note: github code update will be delayed. Pay attention not to get lost

This article introduces the characteristics of adjacency table and its storage graph, and implements it in java

1. adjacency list

Adjacency list is a kind of chained storage method of graph, which is composed of a sequential vertex table and n chained edge tables. Among them, the vertex table is composed of fixed-point nodes, and the edge table is a single chain table composed of edge (ARC) nodes, which represents all the edges attached to the vertex v IUI.

2. Specific implementation code

2.1 type enumeration of graphs: GraphKind.java
package code.Graph; /* * Four types of column chart * */ public enum GraphKind { UDG, //Undirected graph DG, //Directed graph UDN, //Undirected network DN; //Directed network }
2.2 interface of figure: iGraph. Java
package code.Graph; /* * The storage structure of a graph not only stores the information of each vertex in the graph, but also stores the information of the edge associated with the vertex, * The common storage structures of graphs are adjacency matrix, adjacency list, adjacency multiple list and cross linked list * * Interface of graph * * */ public interface IGraph { /**Create a graph*/ public void createGraph(); /**Returns the number of vertices in a graph*/ public int getVexNum(); /**Returns the number of edges in a graph*/ public int getArcNum(); /** * The position v of a given point returns its corresponding vertex value, where 0 < = v < vexnum * @param v * */ public Object getVex(int v) throws Exception; /** * Given the value of vertex vex, return its position in the graph, if the graph does not contain this point, return - 1 * @param vex */ public int locateVex(Object vex); /** * Returns the first adjacent point of V, or - 1 if V has no adjacent point, where 0 < = V < vexnum * @param v */ public int firstAdjVex(int v) throws Exception; /** * Returns the next adjacent contact of v with respect to W. if W is the last adjacent contact of v, returns - 1, where 0 < = v, w < vexnum * @param v * @param w */ public int nextAdjVex(int v,int w) throws Exception; }
2.3 vertex node class: VNode.java
package code.Graph; /** * Vertex node classes in adjacency table storage representation of Graphs * */ public class VNode { public Object data;//Vertex information public ArcNode firstArc;//Point to the first arc attached to the vertex public VNode(Object data) { this(data,null); } public VNode(Object data, ArcNode firstArc) { this.data = data; this.firstArc = firstArc; } public VNode() { this(null,null); } public Object getData() { return data; } public ArcNode getFirstArc() { return firstArc; } }
2.4 edge (or arc) node class: ArcNode.java
package code.Graph; /** * The class of edge (or arc) nodes in the adjacency table storage representation of Graphs * * */ public class ArcNode { public int adjVex;//The vertex position pointed by the arc public int value;//Weight of edge or arc private ArcNode nextArc;//Point to the next arc public ArcNode(int adjVex, int value, ArcNode nextArc) { this.adjVex = adjVex; this.value = value; this.nextArc = nextArc; } public ArcNode(int adjVex, int value) { this(adjVex,value,null); } public ArcNode(int adjVex) { this(adjVex,0,null); } public ArcNode() { this(-1,0,null); } public int getAdjVex() { return adjVex; } public int getValue() { return value; } public ArcNode getNextArc() { return nextArc; } public void setAdjVex(int adjVex) { this.adjVex = adjVex; } public void setValue(int value) { this.value = value; } public void setNextArc(ArcNode nextArc) { this.nextArc = nextArc; } }
2.5 adjacency table class of graph: ALGraph.java
package code.Graph; import java.util.Scanner; public class ALGraph implements IGraph{ private GraphKind kind;//Category mark of figure private int vexNum,arcNum;//The current number of vertices and edges of a graph private VNode[] vexs;//vertex public ALGraph() { this(null,0,0,null); } public ALGraph(GraphKind kind, int vexNum, int arcNum, VNode[] vexs) { this.kind = kind; this.vexNum = vexNum; this.arcNum = arcNum; this.vexs = vexs; } /** * Create a graph */ @Override public void createGraph() { Scanner sc = new Scanner(System.in); System.out.println("Please enter the type of diagram:"); GraphKind kind = GraphKind.valueOf(sc.next()); switch (kind){ case UDG: createUDG(); return; case DG: createDG(); return; case UDN: createUDN(); return; case DN: createDN(); return; } } //Tool method of creating graph and net private void createNetworkOrGraphUnit(){ Scanner sc = new Scanner(System.in); System.out.println("Please input the vertex number and the edge number of the graph respectively"); vexNum = sc.nextInt();//The number of vertices of a graph arcNum = sc.nextInt();//Edges of Graphs vexs = new VNode[vexNum];//Array of stored vertices System.out.println("Please input each vertex of the graph separately:"); for (int v = 0;v < vexNum;v++){//Construct vertex vector vexs[v] = new VNode(sc.next()); } } //Directed graph private void createDG() { Scanner sc = new Scanner(System.in); createNetworkOrGraphUnit(); System.out.println("Please enter the vertex and weight of each edge:"); for (int k = 0;k < arcNum;k++){ int v = locateVex(sc.next());//Arc head int u = locateVex(sc.next());//Arc tail addArc(v,u,1); } } //Undirected graph private void createUDG() { Scanner sc = new Scanner(System.in); createNetworkOrGraphUnit(); System.out.println("Please enter the vertices of each side:"); for (int k = 0;k < arcNum;k++){ int v = locateVex(sc.next());//Arc head int u = locateVex(sc.next());//Arc tail int value = 1; addArc(v,u,value); addArc(u,v,value); } } //Undirected network private void createUDN(){ Scanner sc = new Scanner(System.in); createNetworkOrGraphUnit(); System.out.println("Please enter the vertex and weight of each edge:"); for (int k = 0;k < arcNum;k++){ int v = locateVex(sc.next());//Arc head int u = locateVex(sc.next());//Arc tail int value = sc.nextInt(); addArc(v,u,value); addArc(u,v,value); } } //Directed network private void createDN(){ Scanner sc = new Scanner(System.in); createNetworkOrGraphUnit(); System.out.println("Enter the vertices of each side and their weights:"); for (int k = 0;k < arcNum;k++){ int v = locateVex(sc.next());//Arc head int u = locateVex(sc.next());//Arc tail int value = sc.nextInt(); addArc(v,u,value); } } /** * Add an arc between the vertices of position v,u with the weight value * */ public void addArc(int v,int u,int value){ ArcNode arc = new ArcNode(u,value); arc.setNextArc(vexs[v].firstArc); vexs[v].firstArc = arc; } /** * Returns the number of vertices in a graph */ @Override public int getVexNum() { return vexNum; } /** * Returns the number of edges in a graph */ @Override public int getArcNum() { return arcNum; } /** * The position v of a given point returns its corresponding vertex value, where 0 < = v < vexnum * * @param v */ @Override public Object getVex(int v) throws Exception { if (v < 0 && v >= vexNum){ throw new Exception("The first" + v + "Vertex does not exist!"); } return vexs[v].getData(); } /** * Given the value of vertex vex, return its position in the graph, if the graph does not contain this point, return - 1 * * @param vex */ @Override public int locateVex(Object vex) { for (int v = 0;v<vexNum;v++){ if (vexs[v].data.equals(vex)){ return v; } } return -1; } /** * Returns the first adjacent point of V, or - 1 if V has no adjacent point, where 0 < = V < vexnum * * @param v */ @Override public int firstAdjVex(int v) throws Exception { if (v < 0 && v >= vexNum){ throw new Exception("The first" + v + "Vertex does not exist!"); } VNode vex = vexs[v]; if (vex.firstArc != null){ return vex.firstArc.adjVex; }else { return -1; } } /** * Returns the next adjacent contact of v with respect to W. if W is the last adjacent contact of v, returns - 1, where 0 < = v, w < vexnum * * @param v * @param w */ @Override public int nextAdjVex(int v, int w) throws Exception { if (v < 0 && v >= vexNum){ throw new Exception("The first" + v + "Vertex does not exist!"); } VNode vex = vexs[v]; ArcNode arcvw = null; for (ArcNode arc = vex.firstArc;arc != null;arc = arc.getNextArc()){ if (arc.adjVex == w){ arcvw = arc; break; } } if (arcvw != null && arcvw.getNextArc() != null){ return arcvw.getNextArc().adjVex; }else { return -1; } } public GraphKind getKind() { return kind; } public VNode[] getVexs() { return vexs; } }

Graph related See this link for the realization of adjacency matrix

Big Qing er Published 18 original articles, won praise 19, visited 1556 Private letter follow

14 February 2020, 09:17 | Views: 3482

Add new comment

For adding a comment, please log in
or create account

0 comments