# Figure (3) -- join List Method

Source: Internet
Author: User
Join List Method

Basic Idea:Create a single-chain table for each vertex in the graph to store all adjacent vertices and their related information. Each single-chain table has a header node.

The I-th single-chain table indicates the edge attached to the vertex VI (the directed graph is an arc with the header or tail of the vertex VI ).

The nodes in the linked list are called table nodes. Each node is composed of three fields, as shown in (. The adjvex indicates the position of the vertex adjacent to the vertex VI in the graph (vertex number), and the link domain (nextarc) points to the next table node adjacent to the vertex VI, the data domain (Info) stores information related to edges or arcs, such as weights. This field can be omitted if no edge information exists for a no-Permission graph.

Each linked list has a header node (called a vertex node), which consists of two fields, as shown in (B. The chain domain (firstarc) points to the first node in the linked list, and the data domain (data) Stores vertex names or other information.

In the graph's adjacent linked list representation, all vertex nodes are stored in a vector in the form of a sequence structure, and can randomly access the linked list of any vertex. This vector is called the header vector, the subscript of the vector indicates the sequence number of the vertex.

When an undirected graph is stored in an adjacent linked list, its adjacent linked list is unique, as shown in 7-10. For a directed graph, its adjacent linked list has two forms, as shown in 7-11.

2. Features of the adjacent Table Method

◆ Each component in the header vector is the header node of a single-chain table, and the number of components is the number of vertices in the graph;

◆ When edges or edges are sparse, using an adjacent table means less storage space than using an adjacent matrix;

◆ In an undirected graph, the degree of vertex VI is the number of knots in the I-th linked list;

◆ You can create a positive or inverse adjacent table for a directed graph. A positive adjacent table is an adjacent table established based on vertex VI as the outbound degree (that is, the starting point of the arc). A reverse adjacent table is an inbound degree based on vertex VI (that is, the end point of the arc) the created adjacent table;

◆ In a directed graph, the number of knots in the I-th linked list is the output (or inbound) Degree of vertex VI; the inbound (or Outbound) degree must traverse the entire adjacent table;

◆ It is easy to find the first adjacent point and the next adjacent point of any vertex in the adjacent table;

3. vererex. Java
`Package Datastructure. graph; import Datastructure. list. linklist; import Datastructure. list. list;/*** vertex of the graph * @ author luoweifu **/public class vertex {private object data; private arcedge firstarc;/*** constructor */Public vertex () {DATA = NULL; firstarc = NULL;}/*** constructor * @ Param data vertex data */Public vertex (Object Data) {This. data = data; this. firstarc = NULL;}/*** obtain vertex data * @ return vertex data */public object getdata () {return data ;} /*** set vertex data * @ Param data vertex data */Public void setdata (Object Data) {This. data = data;}/*** get the first solitary node * @ return */Public arcedge getfirstarc () {return firstarc ;} /*** set the first solitary node * @ Param firstarc */Public void setfirstarc (arcedge firstarc) {This. firstarc = firstarc;} @ overridepublic Boolean equals (Object OBJ) {vertex v = (vertex) OBJ; If (data. equals (v. getdata () // & firstarc. equals (v. getfirstarc () return true; return false ;}@ overridepublic string tostring () {return data + "" + firstarc ;}}`

Isolated node arcedge. Java

`Package Datastructure. graph;/*** arc node definition * @ author luoweifu **/public class arcedge extends edge {private vertex; private arcedge prior; private arcedge next; /*** constructor */Public arcedge () {super ();}/*** constructor * @ Param weight */Public arcedge (double weight) {super (weight); prior = NULL; next = NULL ;} /*** constructor ** @ Param info edge information * @ Param weight */Public arcedge (Object info, double weight) {super (Info, weight ); prior = NULL; next = NULL ;} /*** constructor ** @ Param info edge information * @ Param weight * @ Param vertex */Public arcedge (Object info, double weight, vertex) {This (Info, weight); this. vertex = vertex; prior = NULL; next = NULL;}/*** obtain vertex data * @ return */Public vertex getvertex () {return vertex ;} /*** set the vertex * @ Param vertex */Public void setvertex (vertex) {This. vertex = vertex;}/*** get the previous orphan node * @ return */Public arcedge getprior () {return prior ;} /*** set the previous solitary node * @ Param prior */Public void setprior (arcedge prior) {This. prior = prior;}/*** get the next solitary node * @ return */Public arcedge getnext () {return next ;} /*** set the next solitary node * @ Param next */Public void setnext (arcedge next) {This. next = next ;}@ overridepublic int compareto (Object O) {double W2 = (edge) O ). getweight (); If (this. weight <W2) Return-1; else if (this. weight> W2) return 1; return 0 ;}@ overridepublic Boolean equals (Object OBJ) {arcedge arc = (arcedge) OBJ; If (this. next = arc. next & this. weight = arc. weight) return true; return false ;}@ overridepublic object getfirstvertex () {return prior. vertex. getdata () ;}@ overridepublic object getsecondvertex () {return vertex. getdata ();}}`

Listgraph. Java

`Package Datastructure. graph; import Datastructure. list. arraylist; import Datastructure. list. list; import Datastructure. queue. arrayqueue; import Datastructure. queue. queue; public class listgraph implements graph {private list <vertex> vertexs; private int edgenum; // Number of edges private Enum visit {unvisited, visited}; Public listgraph () {vertexs = new arraylist ();} public list getvertexs () {return vertexs;} public Li Stgraph (object [] vexs) {this (); For (INT I = 0; I <vexs. length; I ++) {vertexs. add (new vertex (vexs [I]);} private vertex find (object v) {vertex Vex = new vertex (V); int I = vertexs. indexof (Vex); if (I <0) {return NULL; // throw new arrayindexoutofboundsexception ("vertex" + V + "does not exist! ");} Return (vertex) vertexs. get (I) ;}@ overridepublic void addedge (Object V1, object V2, double weight) {vertex vex1 = find (V1); vertex vex2 = find (V2 ); if (vex1! = NULL & vex2! = NULL) {arcedge E = new arcedge (null, weight, vex2); If (vex1.getfirstarc () = NULL) {vex1.setfirstarc (E );} else {arcedge arc = vex1.getfirstarc (); While (arc. getnext ()! = NULL) {arc = arc. getnext ();} arc. setnext (e); E. setprior (ARC);} edgenum ++;} else {Throw new arrayindexoutofboundsexception ("vertex" + V1 + "or" + V2 + "does not exist! ") ;}@ Overridepublic void addedge (Object V1, object V2, object info, double weight) {vertex vex1 = find (V1); vertex vex2 = find (V2 ); if (vex1! = NULL & vex2! = NULL) {arcedge E = new arcedge (Info, weight, vex2); If (vex1.getfirstarc () = NULL) {vex1.setfirstarc (E );} else {arcedge arc = vex1.getfirstarc (); While (arc. getnext ()! = NULL) {arc = arc. getnext ();} arc. setnext (e); E. setprior (ARC);} edgenum ++;} else {Throw new arrayindexoutofboundsexception ("vertex" + V1 + "or" + V2 + "does not exist! ") ;}@ Overridepublic void addvex (object v) {vertexs. add (new vertex (V) ;}@ overridepublic string BFS (Object O) {// ---------------- this method has some problems ------------- visit [] = new visit [vertexs. size ()]; for (INT I = 0; I <vertexs. size (); I ++) visit [I] = visit. unvisited; stringbuilder sb = new stringbuilder (); vertex Vex = new vertex (o); // find (o); BFS (Vex, visit, Sb); return sb. tostring ();} private void BFS (vertex Vex, Visit [] visit, stringbuilder SB) {queue = new arrayqueue (); int n = vertexs. indexof (Vex); sb. append (Vex. getdata () + "\ t"); visit [N] = visit. visited; queue. push (Vex); While (! Queue. isempty () {vertex u = (vertex) queue. dequeue (); vertex v = getfirstvertex (U); While (null! = V) {If (visit. unvisited = visit [vertexs. indexof (v)]) {sb. append (v. getdata () + "\ t"); visit [vertexs. indexof (v)] = visit. visited; queue. push (v) ;}v = getnextvertex (u, v) ;}}@ overridepublic string DFS (Object O) {// ---------------- this method has some problems ------------- visit [] = new visit [vertexs. size ()]; for (INT I = 0; I <vertexs. size (); I ++) visit [I] = visit. unvisited; stringbuilder sb = new stringbuilder (); vertex Vex = New vertex (o); // find (o); DFS (Vex, visit, Sb); return sb. tostring ();} private void DFS (vertex Vex, visit [] visit, stringbuilder SB) {int n = vertexs. indexof (Vex); sb. append (Vex. getdata () + "\ t"); visit [N] = visit. visited; vertex v = getfirstvertex (Vex); While (null! = V) {If (visit. unvisited = visit [vertexs. indexof (v)]) DFS (v, visit, Sb); V = getnextvertex (Vex, v) ;}@ overridepublic void clear () {vertexs. clear () ;}@ overridepublic int getedgesize () {return edgenum;} @ overridepublic object getfirstvertex (object v) {vertex Vex = find (V); Return getfirstvertex (Vex ). getdata ();} private vertex getfirstvertex (vertex v) {If (v. getfirstarc ()! = NULL & V. getfirstarc (). getvertex ()! = NULL) return v. getfirstarc (). getvertex (); return NULL ;}@ overridepublic object getnextvertex (Object V1, object V2) {// ------------------ this method has some problems ------------- vertex vex1 = find (V1 ); vertex vex2 = find (V2); system. out. println ("V1:" + V1); system. out. println ("V2:" + V2); system. out. println ("vex1:" + vex1); system. out. println ("vex2:" + vex2); Return getnextvertex (vex1, vex2);}/*** ---------------- this method has some problems --- ---------- * @ Param vex1 * @ Param vex2 * @ return */private vertex getnextvertex (vertex vex1, vertex vex2) {arcedge arc = vex1.getfirstarc (); While (ARC. getnext ()! = NULL & arc. getvertex ()! = Vex2) {arc = arc. getnext () ;}if (ARC. getvertex ()! = NULL) {// system. out. println (arc. getvertex (); return arc. getnext (). getvertex () ;}return null ;}@ overridepublic int getvertexsize () {return vertexs. size () ;}@ overridepublic void removeedge (Object V1, object V2) {vertex vex1 = find (V1); vertex vex2 = find (V2); If (vex1! = NULL & vex2! = NULL) {arcedge arc = vex1.getfirstarc (); While (ARC. getnext ()! = NULL & arc. getvertex ()! = Vex2) {arc = arc. getnext ();} If (arc. getvertex () = vex2) {arcedge priedge = arc. getprior (); arcedge nextedge = arc. getnext (); priedge. setnext (nextedge); nextedge. setprior (priedge); edgenum --;} else {Throw new arrayindexoutofboundsexception ("edge" + V1 + "to" + V2 + "does not exist! ") ;}} Else {Throw new arrayindexoutofboundsexception (" vertex "+ V1 +" or "+ V2 +" does not exist! ") ;}@ Overridepublic void removevex (object v) {for (INT I = 0; I <vertexs. size (); I ++) {vertex vex1 = (vertex) (vertexs. get (I); arcedge arc = vex1.getfirstarc (); If (ARC! = NULL) {While (ARC. getnext ()! = NULL) {If (arc. getvertex (). getdata () = V) {removeedge (vex1, v) ;}}} vertex Vex = find (V); If (Vex! = NULL) {int I = vertexs. indexof (Vex); vertexs. remove (I) ;}@ overridepublic string printgraph () {stringbuilder sb = new stringbuilder (); For (INT I = 0; I <vertexs. size (); I ++) {vertex Vex = (vertex) vertexs. get (I); sb. append ("\ n vertex:" + Vex. getdata () + "\ t"); arcedge arc = Vex. getfirstarc (); If (ARC! = NULL) {sb. append ("solitary," + arc. getvertex (). getdata (); While (ARC. getnext ()! = NULL) {sb. append ("\ t" + arc. getnext (). getvertex (). getdata (); ARC = arc. getnext () ;}} return sb. tostring ();}}`
Related Keywords:

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.