Depth-first traversal under the adjacency table stored by the adjacency table of the graph

Source: Internet
Author: User

Depth-first traversal in the adjacency table of graphs: a hash table is needed to assist.

The specific implementation code is as follows:

Package com.threeTop.www;

/**
 * adjacency table node definition
 * @author wjgs * * *
 /public
class Listgraphnode {
	
	//Add index
	int;
     value
	int value;
	Pointer to next node (reference)
	Listgraphnode next;
	
	
	 The constructor implementation initializes the public
	listgraphnode (int value,listgraphnode next)
	{
			this.value=value;
			This.next=next;
			
	}
	
	The constructor implements the initialization of public
	listgraphnode (int index,int value,listgraphnode next)
	{
		this.index=index;
		This.value=value;
		This.next=next
		
	}
	
}

Package com.threeTop.www;
Import java.util.Hashtable;

Import Java.util.Stack;
	The realization of the adjacency table of the/** * graph * @author WJGS * * */public class Listgraph {//graph vertex array private listgraphnode []nodes;
    
	
	With the help of a hash table private hashtable<integer,integer> valueindex=new hashtable<integer,integer> ();
		
		/** * Initialization graph vertex * @param vertexes/public listgraph (int []vetexes) {nodes=new listgraphnode[vetexes.length];
			for (int i=0;i<vetexes.length;i++) {//Initialize each node nodes[i]=new listgraphnode (I,vetexes[i],null);
		Hash records each position valueindex.put (Vetexes[i], i); }/** * Add start attainable edge * @param start * @param end */public void Addedge (int start, int []end) {for (int
				i=0;i<nodes.length;i++) {if (Nodes[i].value==start) {Listgraphnode node=nodes[i];
					The traversal of the internal array is added to an adjacency table for (int j=0;j<end.length;j++) {node.next=new listgraphnode (end[j],null); Node=node.next; Link Next}}}/** * Add start to reach Edge * @param StArt * @param end */public void addedges (int start, int []end) {int index=valueindex.get (start);
		if (index<0) {throw new RuntimeException ("No specified starting vertex found");
		
		} Listgraphnode Node=nodes[index];
			
			for (int j=0;j<end.length;j++) {int i=valueindex.get (end[j]);
			if (i<0) {throw new RuntimeException ("No specified starting vertex found");
			} node.next=new Listgraphnode (i,end[j],null); Node=node.next;
		
		Link Next}/** * Print the data of the adjacency table */public void Printlistgraph () {System.out.println ("The adjacency matrix of the output graph is stored as:");
			for (int i=0;i<nodes.length;i++) {Listgraphnode node=nodes[i];
				do {System.out.print (node.value);
			Node=node.next;
			}while (Node!=null);
		Line-Wrapping Operation System.out.println ();
		
		}/** * * * adjacency Table Depth First traversal/public void Depthfirsttravel () {System.out.println ("adjacency table depth First traversal is:");
		Initialization stack stack <integer>stack=new stack<integer> ();
		
		Initialize the access state of each vertex int[] visited=new int[nodes.length]; Select a vertex from an access vertex as the starting topPoint int unvisited=getunvisited (visited);
			 while (unvisited>=0) {visited[unvisited]=1;
			 Pressure into the stack stack.push (unvisited);
			 The Output Access node System.out.print (nodes[unvisited].value+ "");
				 
				 Stack non-null while (!stack.isempty ()) {//get top of stack element, no stack int index=stack.peek ();
				 
				 Traversal to find the unreachable vertex boolean found=false;
				 
				 Get the top of the stack listgraphnode Node=nodes[index];
						 The next node of the while (Node!=null) {//node is not accessed by the if (node.next!=null&&visited[node.next.index]==0) {
						 If found, access to the stack visited[node.next.index]=1;
						 Stack.push (Node.next.index);
						 Output traversal to the element System.out.print (node.next.value+ "");
						 Found=true;
					 Break
				 //Looking for the next node node=node.next;
				 } if (!found) {//pop-up stack top element stack.pop ();
	    } unvisited=getunvisited (visited);	 
	} System.out.println (); 
	/** * Gets the 1th-found unreachable vertex subscript * from the access tag array * @param visited * @return If all are visited, return-1/private int getunvisited (int[] visited) {int index=-1;
				for (int i=0;i<visited.length;i++) {//i node is not accessed if (visited[i]==0) {index=i;
			Break
	} return index;
		public static void Main (string[] args) {//TODO auto-generated method stub int[] vetexes={0,1,2,3,4,5,6,7};
		Listgraph graph=new listgraph (vetexes);
		Graph.addedge (0, new int []{1,2});
		Graph.addedge (1, New int[]{3,4});
		Graph.addedge (2, New int[]{5,6});
		Graph.addedge (3, New int[]{7});
		Graph.addedge (4, New int[]{7});
		Graph.addedge (5, New int[]{7});
		Graph.addedge (6, New int[]{7});
		Graph.printlistgraph ();
	Graph.depthfirsttravel ();
 }

}


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.