Adjacency matrix for Java graphs

Source: Internet
Author: User

There is a direction graph in the graph, the node pairs is ordered, and the node has a forward edge to called from Node X to node Y, so and are two different sides. The node pairs in the graph are surrounded by a pair of angle brackets, X is the starting point of the forward edge, Y is the end of the forward edge, and the edges in the graph are also called arcs.


The non-direction graph in the graph, the node pair (x, y) is unordered, and the node pair (x, y) is called an edge associated with node x and Node Y. (x, y) is equivalent to and .


In an n-1 graph with n nodes, if there are N ()/2 edges, that is, there is only one edge between any two nodes, the graph is called an absolute full graph. In a directed graph with n nodes, if there is an n (n-1) edge, that is, there are two edges between any two nodes with only the opposite direction, the graph is called a directed full graph.


The adjacency node is in the graph G, if (u,v) is an edge in E (G), then u and v are mutually adjacent nodes, and the edges (u,v) are attached to nodes U and v. In the graph G, if is an edge in E (G), then the node U is said to be adjacent to Node V, node V is adjacent to the node U, and the edge is associated with node U and Node v.


The degree of Node V is the number of edges associated with it, which is recorded as TD (V).
Path in Figure g= (v,e), if there is a set of edges from node VI to reach the node VJ, then the node of the node vi to the node VJ is the path from node VI to the node VJ.


Right some of the graphs are accompanied by data information, which are referred to as rights. The right of the section I side is indicated by the sign WI.
Path length for a graph with no weights, the path length of a path refers to the number of edges on that path; for weighted graphs, the path length of a path is the sum of the weights of each edge on that path.


Minimum spanning tree The spanning tree of a connected graph with n nodes is the smallest connected sub-graph of the original, and contains all n nodes in the original image, and has the fewest edges to maintain the graph unicom. (n-1) edge.


The adjacency matrix storage structure of graphs
Suppose that figure g= (v,e) has n nodes, that is V={v0,v1,..., vn-1},e can be described as matrix A in the following form, for each element in a AIJ, satisfies: Aij=1 means I and J nodes have edges connected, aij=0 means I and J have no edge connected.
Because the element in matrix a AIJ represents the relationship between the end point VI and the edge of the node VJ, or that the element in a AIJ represents the adjacency of the settlement point VI and the node VJ (0≤j≤n-1), the matrix A is called the adjacency matrix. aij= How many numbers represent the path weights for I and J.

import java.util.ArrayList;//adjacency Matrix class Public classmyadjgraphic {StaticFinalintmaxweight=-1;//If there is no edge between the two nodes, the weight is-1;ArrayList vertices =NewArrayList ();//set of storage nodes    int[] edges;//two-dimensional array of adjacency matrices    intNumofedges;//the number of edges         PublicMyadjgraphic (intN) {edges=New int[N][n];  for(intI=0; i<n;i++)        {             for(intj=0; j<n;j++)            {                if(I==J)//the element on the diagonal is 0{Edges[i][j]=0; }                Else{Edges[i][j]=Maxweight; } }} numofedges=0; }        //returns the number of edges     Public intgetnumofedges () {return  This. numofedges; }        //returns the number of nodes     Public intGetnumofvertice () {return  This. Vertices.size (); }        //returns the value of the node.     PublicObject Getvalueofvertice (intindex) {        return  This. vertices.Get(index); }        //get the weight of an edge     Public intGetweightofedges (intV1,intv2) throws Exception {if((V1 <0|| V1 >= vertices.size ()) | | (V2 <0|| V2 >=vertices.size ())) {           Throw NewException ("v1 or v2 parameter out of bounds error! "); }       return  This. Edges[v1][v2]; }        //inserting Nodes     Public voidinsertvertice (Object obj) { This. Vertices.add (obj); }        //inserting an edge with a weighted value     Public voidInsertedges (intV1,intV2,intweight) throws Exception {if((V1 <0|| V1 >= vertices.size ()) | | (V2 <0|| V2 >=vertices.size ())) {          Throw NewException ("v1 or v2 parameter out of bounds error! "); }                 This. edges[v1][v2]=weight;  This. numofedges++; }        //Delete an edge     Public voidDeleteedges (intV1,intv2) throws Exception {if((V1 <0|| V1 >= vertices.size ()) | | (V2 <0|| V2 >=vertices.size ())) {          Throw NewException ("v1 or v2 parameter out of bounds error! "); }        if(V1==v2 | | This. Edges[v1][v2]==maxweight)//You don't have to delete yourself if you don't exist on your own side or side.         {            Throw NewException ("The edge doesn't exist! "); }                 This. edges[v1][v2]=Maxweight;  This. numofedges--; }        //Print adjacency matrix     Public voidprint () { for(intI=0;i< This. edges.length;i++ )        {             for(intj=0;j< This. edges[i].length;j++) {System. out. Print (edges[i][j]+" "); } System. out. println (); }    }}//The class of the inserted edge Public classWeight {intRow//starting point    intCol//End    intWeight//Weighted ValueWeight (intRowintColintweight) {         This. Row =Row;  This. Col =Col;  This. Weight =weight; }         Public Static voidCreateadjgraphic (Myadjgraphic g, object[] vertices,intN,weight[] Weight,inte) throws Exception {//Initializing Nodes        for(intI=0; i<n;i++) {G.insertvertice (vertices[i]); }       //initialize all of the edges        for(intI=0; i<e;i++) {g.insertedges (Weight[i].row, Weight[i].col, weight[i].weight); }    }} Public classTest { Public Static voidMain (string[] args) {intn=5;//5 Nodes        intE=5;//5 Sidesmyadjgraphic G=Newmyadjgraphic (n); Object[] vertices=Newobject[]{NewCharacter ('A'),NewCharacter ('B'),NewCharacter ('C'),NewCharacter ('D'),NewCharacter ('E')}; Weight[] Weights=Newweight[]{NewWeight (0,1,Ten),NewWeight (0,4, -),NewWeight (2,1, +),NewWeight (1,3, -),NewWeight (3,2, -)}; Try{weight.createadjgraphic (g, vertices, n, weights, E); System. out. println ("--------The frontage matrix as follows---------");           G.print (); System. out. println ("Number of nodes:"+G.getnumofvertice ()); System. out. println ("number of edges:"+g.getnumofedges ()); G.deleteedges (0,4); System. out. println ("after--------delete---------");           G.print (); System. out. println ("Number of nodes:"+G.getnumofvertice ()); System. out. println ("number of edges:"+g.getnumofedges ()); }        Catch(Exception ex) {} }}/*--------The frontage matrix is as follows---------0 10-1-1 20-1 0-1 30-1-1 40 0-1-1-1-1 50 0-1-1-1-1-1 0 Node Number: 5 side: 5--------Delete After---------0 10-1-1-1-1 0-1 30-1-1 40 0-1-1-1-1 50 0-1-1-1-1-1 0 Node Number: 5 Sides: 4*/

Adjacency matrix for Java graphs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.