图的邻接表ATP以及遍历(非递归深度和广度遍历)

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;


class Edge{//边
	Object verAdj;//该弧指向的节点名
	int weight;//权重
	Edge nextEdge;//指向下一条弧
}
class Vertex{//顶点
   Object verName;//顶点名称
   Edge edgeHead;//边链表的头节点 
   
}
public class GrapLinkedListExample {
	private List<Vertex> list;//用来存储顶点
	@SuppressWarnings("unused")
	private boolean isDirectcted;
    public 	GrapLinkedListExample(int[] vex,int[][] edges,boolean isDirected){//图的初始化
    	list=new ArrayList<Vertex>(vex.length);//初始化顶点个数
    	this.isDirectcted=isDirected;//判断图是否又向
    	//如果为无向图
    	if(!isDirected){
    		for(int i=0;i<vex.length;i++){
    			list.add(new Vertex());
    			list.get(i).verName=vex[i];//把节点放入list
    			Edge cur=null;//记录当前位置
    			for(int j=0;j<edges.length;j++){
    				if(edges[j][0]==vex[i]){
    					if(list.get(i).edgeHead==null){//表头边结点
    						list.get(i).edgeHead=new Edge();
    						list.get(i).edgeHead.verAdj=edges[j][1];//相邻边节点
    						list.get(i).edgeHead.weight=edges[j][2];//
    						cur=list.get(i).edgeHead;//当前边节点
    					}
    				else{//如果不是表头边节点
    					while(cur.nextEdge!=null){//让当前节点指向一个空节点
    						cur=cur.nextEdge;
    					}
    					cur.nextEdge=new Edge();
    					cur=cur.nextEdge;
    					cur.verAdj=edges[j][1];
    					cur.weight=edges[j][2];
    				}
    				}else if(edges[j][1]==vex[i]){//无向图为双向
    					if(list.get(i).edgeHead==null){//表头边结点
    						list.get(i).edgeHead=new Edge();
    						list.get(i).edgeHead.verAdj=edges[j][0];//相邻边节点
    						list.get(i).edgeHead.weight=edges[j][2];//
    						cur=list.get(i).edgeHead;//当前边节点
    					}
    				else{//如果不是表头边节点
    					while(cur.nextEdge!=null){//让当前节点指向一个空节点
    						cur=cur.nextEdge;
    					}
    					cur.nextEdge=new Edge();
    					cur=cur.nextEdge;
    					cur.verAdj=edges[j][0];
    					cur.weight=edges[j][2];
    				}
    					
    				}
    			}
    		}
    	}else{//如果为有向图
    		for(int i=0;i<vex.length;i++){//有向图为单向
    			list.add(new Vertex());
    			list.get(i).verName=vex[i];//把节点放入list
    			Edge cur=null;//记录当前位置
    			for(int j=0;j<edges.length;j++){
    				if(edges[j][0]==vex[i]){
    					if(list.get(i).edgeHead==null){//表头边结点
    						list.get(i).edgeHead=new Edge();
    						list.get(i).edgeHead.verAdj=edges[j][1];//相邻边节点
    						list.get(i).edgeHead.weight=edges[j][2];//
    						cur=list.get(i).edgeHead;//当前边节点
    					}
    				else{//如果不是表头边节点
    					while(cur.nextEdge!=null){//让当前节点指向一个空节点
    						cur=cur.nextEdge;
    					}
    					cur.nextEdge=new Edge();
    					cur=cur.nextEdge;
    					cur.verAdj=edges[j][1];
    					cur.weight=edges[j][2];
    				}
    				}
    			}
    			}
    			
    	}
    }
    //根据元素获取下标
    public int getIndexByVex(Object vex){
    	for(int i=0;i<list.size();i++){
    		if(list.get(i).verName.equals(vex)){
    			return i;
    		}
    	}
    	return -1;
    }
   //广度优先遍历
    public void BreadthFirstSearch(GrapLinkedListExample g){
    	if(g.list.size()==0||g.list==null){
    		return;
    	}
    	Queue<Vertex> vex=new LinkedList<Vertex>();
    	boolean visit[]=new boolean[g.list.size()];
    	vex.offer(g.list.get(0));//第一个元素入队
    		while(!vex.isEmpty()){
    		Vertex point=vex.poll();//第一个节点出队
    	    if(!visit[g.getIndexByVex(point.verName)]){
    		System.out.print(point.verName);//输出节点
    		visit[g.getIndexByVex(point.verName)]=true;//置标记为true
    	    }
    		if(point.edgeHead!=null){
    			Edge epoint=point.edgeHead;
    			//System.out.println(epoint.verAdj);
    			while(epoint.nextEdge!=null){
    				if(!visit[g.getIndexByVex(epoint.verAdj)]){
    				System.out.print(epoint.verAdj);
    				visit[g.getIndexByVex(epoint.verAdj)]=true;
    				vex.offer(g.list.get(g.getIndexByVex(epoint.verAdj)));
    				}
    				epoint=epoint.nextEdge;
    			}
    			if(!visit[g.getIndexByVex(epoint.verAdj)]){
    				System.out.print(epoint.verAdj);
    				visit[g.getIndexByVex(epoint.verAdj)]=true;
    				vex.offer(g.list.get(g.getIndexByVex(epoint.verAdj)));
    				}
    			}	
    		}
    		}
    //深度优先遍历
    public void FirstDepthSearch(GrapLinkedListExample g){
    	boolean visit[]=new boolean[g.list.size()];
        if(g.list.size()==0||g.list==null){
        	return;
        }
    	Stack<Vertex> stack=new Stack<Vertex>();
    	stack.push(g.list.get(0));//让第一个顶点压栈
    	while(!stack.isEmpty()){
    		Vertex vex=stack.pop();//弹栈
    		if(!visit[g.getIndexByVex(vex.verName)]){
    		System.out.print(vex.verName);//输出
    		visit[g.getIndexByVex(vex.verName)]=true;
    		}
    		if(vex.edgeHead!=null){
    			Edge edge=vex.edgeHead;
    			while(edge.nextEdge!=null){
    				if(!visit[g.getIndexByVex(edge.verAdj)]){
    				stack.push(g.list.get(g.getIndexByVex(edge.verAdj)));
    				}
    				edge=edge.nextEdge;
    			}
    			if(!visit[g.getIndexByVex(edge.verAdj)]){
    				stack.push(g.list.get(g.getIndexByVex(edge.verAdj)));
    				}
    		}
    	}
    }
	public static void main(String[] args){
		int[] vexs = {0,1,2,3,4,5};
        int[][] edges = {{0,1,100},{0,2,100},{0,3,100},{1,4,100},{2,5,100},{4,5,100},{5,3,100}};
        GrapLinkedListExample g=new GrapLinkedListExample(vexs,edges,false);
	    //g.BreadthFirstSearch(g);
        g.FirstDepthSearch(g);
	}
}
    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/qq_40163148/article/details/83996644
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞