JAVA图的邻接矩阵表示法ATP以及操作的实现(包括深度优先搜索和广度优先遍历)

//邻接矩阵存储
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class GraphExample {
	private List<Object> vertexList;//存储点的表
	private int[][] edges;//邻接矩阵
	private int numOfEdges;//边的数目
	private boolean[] flag;//递归标记
	private Queue<Object> queue;//广度优先遍历队列
	public GraphExample(int n){
		edges=new int[n][n];//邻接矩阵
		vertexList=new ArrayList<Object>(n);//初始化定点数
		numOfEdges=0;//初始化边数
		flag=new boolean[n];
		queue=new LinkedList<Object>();
	}
	//得到节点的个数
	public int getNumberOfVertex(){
		return vertexList.size();
	}
	//得到边的数目
	public int getNumOfEdges(){
		return numOfEdges;
	}
	//返回节点i的数据
	public Object getValueByIndex(int i){
		return vertexList.get(i);
	}
	//返回V1-V2的权值
	public int getWeight(int v1,int v2){
		return edges[v1][v2];
	}
	//插入节点
	public void insertVertex(Object vertex){
		vertexList.add(vertex);
	}
	//插入边
	public void insertEdge(int weight,int v1,int v2){
		edges[v1][v2]=weight;
		numOfEdges++;
	}
	//删除边
	public void deleteEdge(int v1,int v2){
		edges[v1][v2]=0;
		numOfEdges--;
	}
	/** * 根据一个顶点的下标,返回该顶点的第一个邻接点的下标 * index 该定点的所在的矩阵下标 */
	public int getFirstNeighbor(int index){
		for(int j=0;j<vertexList.size();j++){
			if(edges[index][j]>0){
				return j;
			}
		}
		return -1;
	}
	/** * 根据前一个邻接点的下标来取得下一个邻接点 * v1指定节点 * v2前一个邻接结点 */
	public int getNextNeighbor(int v1,int v2){
		for(int j=v2+1;j<vertexList.size();j++){
			if(edges[v1][j]>0){
				return j;
			}
		}
		return -1;
	}
	//图的遍历
	//1.深度优先遍历
	public  void DepthFirstSearch(GraphExample g,int v0){
		System.out.print(g.getValueByIndex(v0));
		g.flag[v0]=true;
		for(int i=0;i<g.getNumberOfVertex();i++){
			if(!g.flag[i]&&g.getWeight(v0, i)!=0){
				DepthFirstSearch(g,i);
			}
		}
	}
//2.图的广度优先便利
	//利用队列先进先出的思想
	public void BreadthFirstSearch(GraphExample g,int v0){
		g.queue.offer(v0);
		System.out.print(g.getValueByIndex(v0));
		while(!g.queue.isEmpty()){
		int vertex=(int) g.queue.poll();
		 for(int i=vertex;i<g.getNumberOfVertex();i++){
		  if(!g.flag[i]&&g.getWeight(vertex, i)!=0){
			  g.flag[i]=true;
			  System.out.print(g.getValueByIndex(i));
			  g.queue.offer(i);
		  }
		 }
		
		}
	}
	public static void main(String[] args){
		int n=4;//分别表示节点个数和边个数
		String labels[]={"V1","V2","V3","V4"};
		GraphExample graph=new GraphExample(n);
		for(String label:labels){
			graph.insertVertex(label);
		}
		graph.insertEdge(2,0,1);
		graph.insertEdge(5,0,2);
		graph.insertEdge(8,2,3);
		graph.insertEdge(7,3,0);
		System.out.println("结点个数是:"+graph.getNumberOfVertex());
		System.out.println("边数是:"+graph.getNumOfEdges());
		System.out.println("获取2的下一个邻接点:"+graph.getNextNeighbor(0,1));
		//System.out.print("图的深度优先搜索:");
		//graph.DepthFirstSearch(graph,0);
		System.out.print("图的广度优先搜索:");
		graph.BreadthFirstSearch(graph, 0);
		System.out.println();
		graph.deleteEdge(0, 1);//删除V1到V2的边
		System.out.println("结点个数是:"+graph.getNumberOfVertex());
		System.out.println("边数是:"+graph.getNumOfEdges());
		System.out.println("获取V1的第一个邻接点:"+graph.getValueByIndex(graph.getFirstNeighbor(0)));
		
		
	} 
}

    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/qq_40163148/article/details/83896553
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞