The edge set of a graph can be transformed into an adjacency matrix.
The adjacency matrix is defined as follows:
Adjacency matrix (adjacency matrix): A matrix that represents the adjacency between vertices. Set g= (v,e) is a diagram in which V={v1,v2,..., vn}. The adjacency matrix of G is an n-order phalanx with the following properties:
① for a graph, the adjacency matrix must be symmetrical, and the diagonal must be 0 (only the simple graph is discussed here), and the graph is not necessarily the case.
② in the graph, the degree of any vertex i is the and of all the elements in column I, and in the direction graph, the out of vertex I is the and of all elements of line I, and the degree is the and of all the elements in column I.
③ using the adjacency matrix method to show that the diagram requires n^2 space, because the adjacency matrix of the graph must have a symmetric relationship, so the deduction diagonal is zero, only need to store the triangle or the lower triangle data can be, so only n (n-1)/2 space.
A side set is a collection of all the edges of a graph.
Here you use ArrayList edgeset a dynamic one-dimensional array that represents the edge set of an image without a direction. Definition: The order in Edgeset is an odd number and the order is an even number together to represent an edge. Test cases such as the following:
arraylist<integer> edgeset = new arraylist<integer> () Edgeset.add (1); Edgeset.add (2); EdgeSet.add (1); Edgeset.add (3); Edgeset.add (2); Edgeset.add (3); Edgeset.add (2); Edgeset.add (4);
is to define the existence of the edge set {(1,3), (2,3), (2,4)}, now the task is to convert it into the adjacency matrix of this graph:
1 2 3 4
1 0 1 1 0
2 1 0 1 1
3 1 1 0 0
4 0 1 0 0
Here the horizontal axis 1-4 and the array constitute 1-4 of the matrix element coordinates, if the result is 1, it means that the figure has this edge. such as the matrix element in the A[1][2]=1, the edge set must exist on the edge (0), the figure must not exist in this edge. such as A[1][4]=0, then in the edge concentration must not find (1,4) this edge.
The adjacency matrix appears to be a two-dimensional array, but here is a one-dimensional array representation, but output is output in the following way, then the result of the output is a matrix.
Outputs a matrix with a representation of a one-dimensional array public static void Printadjacencymatrix (int adjacencymatrix[]) {//traverse this one-dimensional data for (int i = 0; i < ADJACENCYM Atrix.length; i++) {//each output one element outputs a tab System.out.print (Adjacencymatrix[i] + "\ T"), or if the traversed counter i+1, just a multiple of the square of the matrix length, outputs a newline if (((i + 1 )% Math.sqrt (adjacencymatrix.length)) = = 0) {System.out.println ();}} System.out.println ();}
The key to this method is the output of the newline. For example, the output matrix, I start from 0 encountered 4,8,12,16 output a newline, the length of the matrix is 25, so there is a "if the counter i+1, just the matrix length of the square, a multiple, then output a newline":
0 1 2 3 4
1 0 11 0
2 1 01 1
3 1 10 0
4 0 10 0
To transform the edge set into an adjacency matrix, the first thing to do is to initialize the adjacency matrix with the following conditions, arrange the horizontal and vertical axes, and, of course, the 0 is to occupy the meaningless position of the adjacency matrix A[0][0].
0 1 2 3 4
1 0 0 0 0
2 0 0 0 0
3 0 0 0 0
4 0 0 0 0
The point set should be calculated first based on the edge set. According to the edge set to find out the point set in the "Java" for the ArrayList "(click Open link) has been introduced, here no longer repeat, is a side set element to the process of heavy.
Because the adjacency matrix is represented by a one-dimensional array. So the position of the longitudinal axis is the counter i x point set length +1, such as the above array, the point set is {1,2,3,4}, the length is 4, the longitudinal axis 1,2,3,4 respectively in the one-dimensional array 5,11,16,21 This position, so there is the following code:
SYSTEM.OUT.PRINTLN ("Initialize adjacency matrix:"); arraylist<integer> nodeset = new ArrayList (new HashSet (Edgeset)); int adjacencymatrix[] = new int[(nodeset.size () + 1) * (Nodeset.size () + 1)];for (int i = 0; I <= nodeset.size (); i++) {if (i = = 0) {Adjacencymatrix[i] = 0;} else {Adjace Ncymatrix[i] = Nodeset.get (i-1); Adjacencymatrix[i * (Nodeset.size () + 1)] = Nodeset.get (i-1);}}
Then, the most important step is to traverse the point set and find the adjacency node of the point. The adjacency node of a point is based on this point and the edge set, and the following method is obtained:
Neighbor node public static arraylist<integer> Neighbournode (arraylist<integer> edgeset,int node) {// Set a dynamic array ArrayList to hold the adjacency node of this point arraylist<integer> Neighbournode = new arraylist<integer> ();//traverse the edge set to see that edge has this point, Then the other point of this edge is the adjacency node for this point for (int i = 0; i < edgeset.size (); i = i + 2) {if (node = = Edgeset.get (i)) {Neighbournode.add (EdgeS Et.get (i + 1));}} for (int i = 1; i < edgeset.size (); i = i + 2) {if (node = = Edgeset.get (i)) {Neighbournode.add (Edgeset.get (i-1));}} return neighbournode;}
After finding the neighbor node set of the point, it begins to traverse the neighboring node set of the point and updates the elements in the adjacency matrix. To find this and its adjacent nodes, a matrix element coordinate, the matrix element coordinates corresponding to the value from 0 to 1, on all points to find its adjacency node, then the above method, update the adjacency matrix, so there is the following code:
System.out.println ("Final adjacency Matrix:"); for (int i =1; I <= nodeset.size (); i++) {arraylist<integer> neighbournode= Neighbournode (Edgeset,adjacencymatrix[i]); for (int j=0;j<neighbournode.size (); j + +) {for (int k=0;k<= Nodeset.size (); k++) {if (Neighbournode.get (j) ==adjacencymatrix[k * (Nodeset.size () +1)]) {adjacencymatrix[( Nodeset.size () +1) *k+i]=1;}}} Printadjacencymatrix (Adjacencymatrix);
At this point, the edge set is eventually transformed into an adjacency matrix. The full code is as follows:
Import java.util.*;p Ublic class Edgeset_adjacencymatrix {//The adjacency node of the seek point public static arraylist<integer> Neighbournode (arraylist<integer> edgeset,int node) {//Set a dynamic array ArrayList save the adjacency node for this point arraylist<integer> Neighbournode = new arraylist<integer> ();//Loop through the edge set, and see if that edge has this point, then another point of this edge is the adjacency node for this point for (int i = 0; i < edgeset.size (); i = i + 2) {if (node = = Edgeset.get (i)) {Neighbournode.add (Edgeset.get (i + 1))}} for (int i = 1; i < edgeset.size (); i = i + 2) {if (node = = Edgeset.get (i)) {Neighbournode.add (Edgeset.get (i-1));}} return neighbournode;} Outputs a matrix with a representation of a one-dimensional array public static void Printadjacencymatrix (int adjacencymatrix[]) {//traverse this one-dimensional data for (int i = 0; i < ADJACENCYM Atrix.length; i++) {//each output one element outputs a tab System.out.print (Adjacencymatrix[i] + "\ T"), or if the traversed counter i+1, just a multiple of the square of the matrix length, outputs a newline if (((i + 1 )% Math.sqrt (adjacencymatrix.length)) = = 0) {System.out.println ();}} System.out.println ();} public static void Edgeset_adjacencymatrix (Arraylist<integer> edgeset) {System.out.println ("Initialize adjacency matrix:"); arraylist<integer> nodeset = new ArrayList (new HashSet (Edgeset)); int adjacencymatrix[] = new int[(nodeset.size () + 1) * (Nodeset.size () + 1)];for (int i = 0; I <= nodeset.size (); i++) {if (i = = 0) {Adjacencymatrix[i] = 0;} else {Adjace Ncymatrix[i] = Nodeset.get (i-1); Adjacencymatrix[i * (Nodeset.size () + 1)] = Nodeset.get (i-1);}} Printadjacencymatrix (Adjacencymatrix); System.out.println ("Final adjacency Matrix:"); for (int i =1; I <= nodeset.size (); i++) {arraylist<integer> neighbournode= Neighbournode (Edgeset,adjacencymatrix[i]); for (int j=0;j<neighbournode.size (); j + +) {for (int k=0;k<= Nodeset.size (); k++) {if (Neighbournode.get (j) ==adjacencymatrix[k * (Nodeset.size () +1)]) {adjacencymatrix[( Nodeset.size () +1) *k+i]=1;}}} Printadjacencymatrix (Adjacencymatrix);} public static void Main (string[] args) {arraylist<integer> edgeset = new arraylist<integer> (); Edgeset.add (1 ); Edgeset.add (2); Edgeset.add (1); Edgeset.add (3); Edgeset.add (2); Edgeset.add (3); Edgeset.add (2); Edgeset. Add (4); Edgeset_adjacencymatrix (Edgeset);}}
Run result of Edge set {(1,3), (2,3), (2,4)}:
"Java" Edge set converted to adjacency matrix