java数据结构图的基本操作

文中内容来源于《数据结构 --Java语言描述》(第二版) 刘小晶 杜选 主编
此系列文章作为学校实验记录,若文中内容有误,请大家指出,谢谢

实验目的

1、熟练掌握图的邻接矩阵和邻接表存储结构;
2、掌握图的创建方法;
3、掌握求顶点度的方法;
4、掌握图的深度优先和广度优先遍历方法;

实验内容和具体要求

1、分别定义图的邻接矩阵和邻接表存储结构;
2、分别在两种存储结构下根据输入的顶点和边(或弧)创建图;
3、分别在两种存储结构下实现求顶点度的操作;
4、分别在两种存储结构下实现图的深度和广度优先遍历算法。

实验步骤

1、定义图的存储结构
2、实现图的创建方法,并创建一个如下的图:
在这里插入图片描述
3、实现求第一个邻接点firstAdjVex()和下一个邻接点nextAdjVex()的操作;
4、写一个算法,求各个顶点的度;
5、对创建的图进行深度优先和广度优先遍历。

话不多说,直接上代码

//Graphkind.java
package ch6;
public enum GraphKind {
	UDG,
	DG,
	UDN,
	DN;

}


//IGraph.java
package ch6;
public interface IGraph {
	void createGraph();
	int getVexNum();
	int getArcNum();
	Object getVex(int v) throws Exception;
	int locateVex(Object vex);
	int firstAdjVex(int v) throws Exception;
	int nextAdjVex(int v,int w) throws Exception;
}

//MGraph.java
package ch6;
import java.util.Scanner;
import ch3.*;
import java.util.Queue;
public class MGraph implements IGraph {
	public static final int INFINITY = Integer.MAX_VALUE;
	private GraphKind kind;
	private int vexNum,arcNum;
	private Object vexs[];
	private int arcs[][];
	
	public GraphKind getKind() {
		return kind;
	}

	public Object[] getVexs() {
		return vexs;
	}

	public int[][] getArcs() {
		return arcs;
	}

	public MGraph() {
		this(null,0,0,null,null);
	}
	
	public MGraph(GraphKind kind,int vexNum,int arcNum,Object[] vexs,int[][] arcs) {
		this.kind = kind;
		this.vexNum = vexNum;
		this.arcNum = arcNum;
		this.vexs = vexs;
		this.arcs = arcs;
	}
	
	//顶点定位
	public int locateVex(Object vex) {
		for(int v=0;v<vexNum;v++) {
			if(vexs[v].equals(vex)) {
				return v;
			}
		}
		return -1;
	}
	
	//创建图的种类
	public void createGraph() {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入图的类型:DG:有向图 UDG:无向图 DN:有向网 UDN:无向网");
		GraphKind kind = GraphKind.valueOf(sc.next());
		switch(kind) {
		case DG:
			createDG();
			return;
		case UDG:
			createUDG();
			return;
		case DN:
			createDN();
			return;
		case UDN:
			createUDN();
			return;
		}
	}
	
	//创建邻接矩阵有向图
	private void createDG() {
		// TODO Auto-generated method stub
		Scanner sc= new Scanner(System.in);
		kind = GraphKind.valueOf("DG");
		System.out.println("请输入图的顶点数和边数:");
		vexNum = sc.nextInt();
		arcNum = sc.nextInt();
		vexs = new Object[vexNum];
		System.out.println("请输入图的各顶点值:");
		for(int i=0;i<vexNum;i++) {
			vexs[i] = sc.next();
		}
		arcs = new int[vexNum][vexNum];
		for(int i=0;i<vexNum;i++) {
			for(int j=0;j<vexNum;j++) {
				arcs[i][j] = 0;
			}
		}
		System.out.println("请输入边的信息:");
		for(int k=0;k<arcNum;k++) {
			int v = locateVex(sc.next());
			int u = locateVex(sc.next());
			arcs[v][u] = 1;
		}
	}

	//创建邻接矩阵无向图
	public void createUDG() {
		Scanner sc = new Scanner(System.in);
		kind = GraphKind.valueOf("UDG");
		System.out.println("请输入图的顶点数和边数:");
		vexNum = sc.nextInt();
		arcNum = sc.nextInt();
		vexs = new Object[vexNum];
		System.out.println("请输入图的各顶点值:");
		for(int i=0;i<vexNum;i++) {
			vexs[i] = sc.next();
		}
		arcs = new int[vexNum][vexNum];
		for(int i=0;i<vexNum;i++) {
			for(int j=0;j<vexNum;j++) {
				arcs[i][j] = 0;
			}
		}
		System.out.println("请输入边的信息:");
		for(int k=0;k<arcNum;k++) {
			int v = locateVex(sc.next());
			int u = locateVex(sc.next());
			arcs[v][u] = arcs[u][v] = 1;
		}
	}	
	
	//创建邻接矩阵有向网
	public void createDN() {
		Scanner sc = new Scanner(System.in);
		kind = GraphKind.valueOf("DN");
		System.out.println("请输入图的顶点数和边数:");
		vexNum = sc.nextInt();//顶点数
		arcNum = sc.nextInt();//弧数
		vexs = new Object[vexNum];//创建存储顶点的数组
		System.out.println("请输入图的各顶点的值:");
		for(int i = 0;i<vexNum;i++) {
			vexs[i] = sc.next();
		}
		arcs = new int[vexNum][vexNum];
		for(int i = 0;i<vexNum;i++) {
			for(int j = 0;j<vexNum;j++) {
				arcs[i][j] = INFINITY;
			}
		}
		System.out.println("请输入边的两个顶点及其权值:");
		for(int k = 0;k<arcNum;k++) {
			int v = locateVex(sc.next());
			int u = locateVex(sc.next());
			arcs[v][u] = sc.nextInt();
		}
	}	

	//创建邻接矩阵无向网
	private void createUDN() {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		kind = GraphKind.valueOf("UDN");
		System.out.println("请分别输入图的顶点数和边数:");
		vexNum = sc.nextInt();
		arcNum = sc.nextInt();
		vexs = new Object[vexNum];
		System.out.println("请输入图的各个顶点:");
		for(int i=0;i<vexNum;i++) {
			vexs[i] = sc.next();
		}
		arcs = new int[vexNum][vexNum];
		for(int i = 0;i<vexNum;i++) {
			for(int j = 0;j<vexNum;j++) {
				arcs[i][j] = INFINITY;
			}
		}
		System.out.println("请输入边的两个顶点及其权值:");
		for(int k = 0;k<arcNum;k++) {
			int v1 = locateVex(sc.next());
			int u1 = locateVex(sc.next());
			arcs[v1][u1] = arcs[u1][v1] = sc.nextInt();
			
		}
	}
	
	//有向图顶点度数求法
	public int getDgreeNum(Object data) {
		int flag = locateVex(data);
		int DgreeNum = 0;
		for(int i=0;i<vexNum;i++) {
			if(arcs[i][flag]==1) {
				DgreeNum++;
			}
		}
		return (DgreeNum + getUDgreeNum(data));
	}
		
	//无向图顶点度数求法
	public int getUDgreeNum(Object data) {
		int flag = locateVex(data);
		int UDgreeNum = 0;
		for(int i=0;i<vexNum;i++) {
			if(arcs[flag][i]==1) {
				UDgreeNum++;
			}
		}
		return UDgreeNum;
	}
	
	//有向网顶点度数求法
	public int getNDgreeNum(Object data) {
		int flag = locateVex(data);
		if (flag == -1) {
			System.out.println("该顶点不存在!");
		}
		int NDgreeNum = 0;
		for(int i=0;i<vexNum;i++) {
			if(arcs[i][flag]!=INFINITY) {
				NDgreeNum++;
			}
		}
		if((kind.name()).equals("DG")) {
			return (getDgreeNum(data));
		} else if((kind.name()).equals("UDG")) {
			return getUDgreeNum(data);
		} else if((kind.name()).equals("DN")) {
			return (NDgreeNum + getUNDgreeNum(data));
		} else {
			return getUNDgreeNum(data);
		}
	}
	//无向网顶点度数求法
	public int getUNDgreeNum(Object data) {
		int flag = locateVex(data);
		int UNDgreeNum = 0;
		for(int i=0;i<vexNum;i++) {
			if(arcs[flag][i]!=INFINITY) {
				UNDgreeNum++;
			}
		}
		return UNDgreeNum;
	}

	//返回顶点数
	public int getVexNum() {
		return vexNum;
	}

	//返回边数
	public int getArcNum() {
		return arcNum;
	}

	//返回顶点
	public Object getVex(int v) throws Exception {
		if(v<0&&v>=vexNum) {
			throw new Exception("第" + v + "个顶点不存在!");
		}
		return vexs[v];
	}

	//返回顶点v的第一个邻接点
	public int firstAdjVex(int v) throws Exception {
		if(v<0&&v>=vexNum) {
			throw new Exception("第" + v + "个顶点不存在");
		}
		for(int i=0;i<vexNum;i++) {
			if(arcs[v][i] != 0&&arcs[v][i]<INFINITY) {
				return i;
			}
		}
		return -1;
	}

	//返回顶点v相对于w的下一个邻接点
	public int nextAdjVex(int v, int w) throws Exception {
		// TODO Auto-generated method stub
		if(v<0&&v>=vexNum) {
			throw new Exception("第" + v + "个顶点不存在!");
		}
		for(int i=w+1;i<vexNum;i++) {
			if(arcs[v][i] != 0&&arcs[v][i]<INFINITY) {
				return i;
			}
		}
		return -1;
	}
	
	//广搜
	private static boolean[] visited;//访问标志数组
	public static void BFSTraverse(IGraph G) throws Exception {
		System.out.println("广搜的结果为:");
		visited = new boolean[G.getVexNum()];
		for(int i=0;i<G.getVexNum();i++) {
			visited[i] = false;
		}
		for(int i=0;i<G.getVexNum();i++) {
			if(!visited[i]) {
				BFS(G,i);
			}
		}
	}
	private static void BFS(IGraph G,int v) throws Exception {
		visited[v] = true;
		System.out.print(G.getVex(v).toString() + " ");
		LinkQueue Q = new LinkQueue();
		Q.offer(v);//v入队
		while(Q.isEmpty()) {
			int u = (Integer)Q.poll();//队首元素出队,并赋值给u
			for(int w=G.firstAdjVex(u);w>0;w=G.nextAdjVex(u, w)) {
				if(!visited[w]) {//w为u的尚未访问的邻接顶点
					visited[w] = true;
					//System.out.println(((VNode)G.getVex(w)).data.toString() + " ");
					System.out.println(G.getVex(w).toString() + " ");
					Q.offer(w);
				}
			}
		}
	}
	
	//深搜
	private static boolean[] visited1;
	public static void DFSTraverse(IGraph G) throws Exception {
		System.out.println("深搜的结果为:");
		visited1 = new boolean[G.getVexNum()];
		for(int i=0;i<G.getVexNum();i++) {
			visited1[i] = false;
		}
		for(int i=0;i<G.getVexNum();i++) {
			if(!visited1[i]) {
				DFS(G,i);
			}
		}
	}
	private static void DFS(IGraph G,int v) throws Exception {
		visited1[v] = true;
		System.out.print(G.getVex(v).toString() + " ");
		for(int w=G.firstAdjVex(v);w>=0;w=G.nextAdjVex(v, w)) {
			if(!visited1[w]) {
				DFS(G,w);
			}
		}
	}
	
	public void tip() {
		System.out.println("请输入以下数字执行您想要进行的操作:");
		System.out.println("1:   求顶点的度");
		System.out.println("2:   求顶点所在的数组下标");
		System.out.println("3:   求顶点的第一条边指向的顶点的数组下标");
		System.out.println("4:   求顶点的第w条边的下一条边指向的顶点的数组下标");
		System.out.println("5:   求顶点个数");
		System.out.println("6:   求边数");
		System.out.println("7:   求图的类型");
		System.out.println("8:   输出图的顶点的数值");
		System.out.println("9:   广搜");
		System.out.println("10: 深搜");
		System.out.println("11: 退出本次操作");
	}
}


//MGraphTest.java
package ch6;
import java.util.Scanner;
public class MGraphTest {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		MGraph M = new MGraph();
		M.createGraph();
		int flag;
		String judjement;
		while(true) {
			M.tip();
			int choice;
				choice = sc.nextInt();
			switch(choice) {
				case 1://求顶点的度
					System.out.println("请输入要查找的顶点:");
					Object input1 = sc.next();
					System.out.println("顶点" + input1.toString() + "的度数为:" + M.getNDgreeNum(input1));
					break;
				case 2://求顶点所在的数组下标
					System.out.println("请输入要查找的顶点:");
					Object input2 = sc.next();
					System.out.println("顶点" + input2.toString() + "的数组下标为:" + M.locateVex(input2));
					break;
				case 3://求顶点的第一条边指向的顶点的数组下标
					System.out.println("请输入该顶点的数组下标:");
					int input3 = sc.nextInt();
					System.out.println("下标为:" + M.firstAdjVex(input3));
					break;
				case 4://求顶点的第w条边的下一条边指向的顶点的数组下标
					System.out.println("请输入该顶点的数组下标v和第w条边:");
					int input4 = sc.nextInt();
					int input5 = sc.nextInt();
					System.out.println("下标为:" + M.nextAdjVex(input4, input5));
					break;
				case 5://求顶点个数
					System.out.println("顶点个数为:" + M.getVexNum());
					break;
				case 6://求边数
					System.out.println("边数为" + M.getArcNum());
					break;
				case 7://求图的类型
					System.out.println("图的类型为:" + M.getKind().name());
					break;
				case 8://输出图的顶点的数值
					System.out.println("请输入该顶点的数组下标:");
					int input6 = sc.nextInt();
					System.out.println("顶点数值为:" + M.getVex(input6));
					break;
				case 9://广搜
					M.BFSTraverse(M);
					break;
				case 10://深搜
					M.DFSTraverse(M);
					break;
				case 11://退出本次操作
					break;
				default :
					System.out.println("输入有误");
					break;
			}
			flag = 1;
			while(flag>0) {
				System.out.println();
				System.out.println("是否要结束对图的操作?y/n");
				judjement = sc.next();
				if(judjement.equals("y"))
					break;
				else if(judjement.equals("n"))
					flag = -1;
				else
					System.out.println("输入有误");
			}
			if(flag == 1) {
				break;
			}
		}
		sc.close();
		System.out.println("程序结束");
	}
}


//ALGraph.java
package ch6;
import java.util.Scanner;
import ch3.*;
//顶点结点类
class VNode {
	public Object data;
	public ArcNode firstArc;
	
	public VNode() {
		this(null,null);
	}
	
	public VNode(Object data) {
		this(data,null);
	}
	
	public VNode(Object data,ArcNode firstArc) {
		this.data = data;
		this.firstArc = firstArc;
	}
}
//边结点类
class ArcNode {
	public int adjVex;
	ArcNode nextArc;
	public int weight;
	
	public ArcNode() {
		this(-1,null,0);
	}
	
	public ArcNode(int adjVex) {
		this(adjVex,null,0);
	}
	
	public ArcNode(int adjVex,int weight) {
		this(adjVex,null,weight);
	}
	
	public ArcNode(int adjVex,ArcNode nextArc,int weight) {
		this.adjVex = adjVex;
		this.nextArc = nextArc;
		this.weight = weight;
	}
}

public class ALGraph implements IGraph {
	private GraphKind kind;
	private VNode vexs[];
	private int vexNum,arcNum;
	
	public ALGraph() {
		this(null,0,0,null);
	}
	
	public ALGraph(GraphKind kind,int vexNum,int arcNum,VNode[] vexs) {
		this.kind = kind;
		this.vexNum = vexNum;
		this.arcNum = arcNum;
		this.vexs = vexs;
	}
	
	//创建图
	public void createGraph() {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入图的类型:DG:有向图 UDG:无向图 DN:有向网 UDN:无向网");
		GraphKind kind = GraphKind.valueOf(sc.next());
		switch(kind) {
		case DG:
			createDG();
			break;
		case UDG:
			createUDG();
			break;
		case DN:
			createDN();
			break;
		case UDN:
			createUDN();
			break;
		}
	}
	
	//创建有向图
	public void createDG() {
		Scanner sc = new Scanner(System.in);
		kind = GraphKind.valueOf("DG");
		System.out.println("请输入图的顶点数和边数:");
		vexNum = sc.nextInt();
		arcNum = sc.nextInt();
		vexs = new VNode[vexNum];
		System.out.println("请输入图的各顶点值:");
		for(int i=0;i<vexNum;i++) {
			vexs[i] = new VNode(sc.next());
		}
		System.out.println("请输入边的信息:");
		for(int j=0;j<arcNum;j++) {
			int v = locateVex(sc.next());
			int w = locateVex(sc.next());
			ArcNode p = new ArcNode(w,1);
			p.nextArc = vexs[v].firstArc;
			vexs[v].firstArc = p;
		}
	}
	
	//创建无向图
	public void createUDG( ) {
		Scanner sc = new Scanner(System.in);
		kind = GraphKind.valueOf("UDG");
		System.out.println("请输入图的顶点数和边数:");
		vexNum = sc.nextInt();
		arcNum = sc.nextInt();
		vexs = new VNode[vexNum];
		System.out.println("请输入图的各顶点值:");
		for(int i=0;i<vexNum;i++) {
			vexs[i] = new VNode(sc.next());
		}
		System.out.println("请输入边的信息:");
		for(int j=0;j<arcNum;j++) {
			int v = locateVex(sc.next());
			int w = locateVex(sc.next());
			ArcNode p = new ArcNode(w);
			p.nextArc = vexs[v].firstArc;
			vexs[v].firstArc = p;
			ArcNode q = new ArcNode(v);
			q.nextArc = vexs[w].firstArc;
			vexs[w].firstArc = q;
		}
	}
	
	//创建有向网
	public void createDN() {
		Scanner sc = new Scanner(System.in);
		kind = GraphKind.valueOf("DN");
		System.out.println("请输入图的顶点数和边数:");
		vexNum = sc.nextInt();
		arcNum = sc.nextInt();
		vexs = new VNode[vexNum];
		System.out.println("请输入图的各定点值:");
		for(int i=0;i<vexNum;i++) {
			vexs[i] = new VNode(sc.next());
		}
		System.out.println("请输入边的信息");
		for(int j=0;j<arcNum;j++) {
			int v = locateVex(sc.next());
			int w = locateVex(sc.next());
			int value = sc.nextInt();
			ArcNode p = new ArcNode(w,value);
			p.nextArc = vexs[v].firstArc;
			vexs[v].firstArc = p;
		}
	}
	
	//创建无向网
	public void createUDN() {
		Scanner sc = new Scanner(System.in);
		kind = GraphKind.valueOf("UDN");
		System.out.println("请输入图的顶点数和边数:");
		vexNum = sc.nextInt();
		arcNum = sc.nextInt();
		vexs = new VNode[vexNum];
		System.out.println("请输入图的各顶点值:");
		for(int i=0;i<vexNum;i++) {
			vexs[i] = new VNode(sc.next());
		}
		System.out.println("请输入边的信息:");
		for(int j=0;j<arcNum;j++) {
			int v = locateVex(sc.next());
			int w = locateVex(sc.next());
			int value = sc.nextInt();
			ArcNode p = new ArcNode(w,value);
			p.nextArc = vexs[v].firstArc;
			vexs[v].firstArc = p;
			ArcNode q = new ArcNode(v,value);
			q.nextArc = vexs[w].firstArc;
			vexs[w].firstArc = q;
		}
	}

	//无向图顶点度数求法
	public int getUDgreeNum(Object data) {
		int flag = locateVex(data);
		int UDgreeNum = 0;
		ArcNode p = vexs[flag].firstArc;
		while(p != null) {
			UDgreeNum++;
			p = p.nextArc;
		}
		return UDgreeNum;
	}
	
	//有向图顶点度数求法
	public int getDgreeNum(IGraph G,Object data) throws Exception {
		int DgreeNum = 0;
		for(int i=0;i<vexNum;i++) {//外围的顶点下标
			for(int w=G.firstAdjVex(i);w>=0;w=G.nextAdjVex(i, w)) {
				VNode p = vexs[w];
				if((p.data).equals(data))
					DgreeNum++;
			}
		}
		if((kind.name()).equals("DG")||(kind.name()).equals("DN")) {
			return (DgreeNum + getUDgreeNum(data));
		} else {
			return getUDgreeNum(data);
		}
	}
	
	//在顶点v和w之间添加一条弧,权值为weight
	public void addArc(int v,int w,int weight) {
		ArcNode arc = new ArcNode(w,weight);
		arc.nextArc = vexs[v].firstArc;
		vexs[v].firstArc = arc;
	}
	
	//返回顶点v的第一个邻接点
	public int firstAdjVex(int v) throws Exception {
		if(v<0 && v>=vexNum) {
			throw new Exception("第" + v + "个顶点不存在!");
		}
		ArcNode p;
		p = vexs[v].firstArc;
		if(p==null) {
			return -1;
		}
		return p.adjVex;
	}
	
	//返回顶点v相对于w的下一个邻接点
	public int nextAdjVex(int v, int w) throws Exception {
		if(v<0 && v>=vexNum) {
			throw new Exception("第" + v + "个顶点不存在!");
		}
		ArcNode p=vexs[v].firstArc; //v的第1个邻接点
		while(p!=null && p.adjVex != w)
			p=p.nextArc;
		if(p!=null)
		    p = p.nextArc;  //w之后的下一个邻接点
		if(p!=null)
		    return p.adjVex;
		else
		    return -1;
	}

	public int getVexNum() {
		return vexNum;
	}

	public int getArcNum() {
		return arcNum;
	}
	
	public VNode[] getVexs() {
		return vexs;
	}
	
	public GraphKind getKind() {
		return kind;
	}
	
	//返回顶点数值域
	public Object getVex(int v) throws Exception {
		if(v<0 && v>=vexNum) {
			throw new Exception("第" + v + "个顶点不存在!");
		}
		return vexs[v].data;
	}

	//顶点定位
	public int locateVex(Object vex) {
		for(int i=0;i<vexNum;i++) {
			if(vexs[i].data.equals(vex)) {
				return i;
			}
		}
		return -1;
	}
	
	//广搜
	private static boolean[] visited;//访问标志数组
	public static void BFSTraverse(IGraph G) throws Exception {
		System.out.println("广搜的结果为:");
		visited = new boolean[G.getVexNum()];
		for(int i=0;i<G.getVexNum();i++) {
			visited[i] = false;
		}
		for(int i=0;i<G.getVexNum();i++) {
			if(!visited[i]) {
				BFS(G,i);
			}
		}
	}
	private static void BFS(IGraph G,int v) throws Exception {
		visited[v] = true;
		System.out.print(G.getVex(v).toString() + " ");
		LinkQueue Q = new LinkQueue();
		Q.offer(v);//v入队
		while(Q.isEmpty()) {
			int u = (Integer)Q.poll();//队首元素出队,并赋值给u
			for(int w=G.firstAdjVex(u);w>0;w=G.nextAdjVex(u, w)) {
				if(!visited[w]) {//w为u的尚未访问的邻接顶点
					visited[w] = true;
					//System.out.println(((VNode)G.getVex(w)).data.toString() + " ");
					System.out.println(G.getVex(w).toString() + " ");
					Q.offer(w);
				}
			}
		}
	}
	
	//深搜
	private static boolean[] visited1;
	public static void DFSTraverse(IGraph G) throws Exception {
		System.out.println("深搜的结果为:");
		visited1 = new boolean[G.getVexNum()];
		for(int i=0;i<G.getVexNum();i++) {
			visited1[i] = false;
		}
		for(int i=0;i<G.getVexNum();i++) {
			if(!visited1[i]) {
				DFS(G,i);
			}
		}
	}
	private static void DFS(IGraph G,int v) throws Exception {
		visited1[v] = true;
		System.out.print(G.getVex(v).toString() + " ");
		for(int w=G.firstAdjVex(v);w>=0;w=G.nextAdjVex(v, w)) {
			if(!visited1[w]) {
				DFS(G,w);
			}
		}
	}
	
	public void tip() {
		System.out.println("请输入以下数字执行您想要进行的操作:");
		System.out.println("1:   求顶点的度");
		System.out.println("2:   求顶点所在的数组下标");
		System.out.println("3:   求顶点的第一条边指向的顶点的数组下标");
		System.out.println("4:   求顶点的第w条边的下一条边指向的顶点的数组下标");
		System.out.println("5:   求顶点个数");
		System.out.println("6:   求边数");
		System.out.println("7:   求图的类型");
		System.out.println("8:   输出图的顶点的数值");
		System.out.println("9:   广搜");
		System.out.println("10: 深搜");
		System.out.println("11: 退出本次操作");
	}
}

//ALGraphTest.java
package ch6;
import java.util.Scanner;
public class ALGraphTest {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		ALGraph G = new ALGraph();
		G.createGraph();
		int flag;
		String judjement;
		while(true) {
			G.tip();
			int choice;
				choice = sc.nextInt();
			switch(choice) {
				case 1://求顶点的度
					System.out.println("请输入要查找的顶点:");
					Object input1 = sc.next();
					System.out.println("顶点" + input1.toString() + "的度数为:" + G.getDgreeNum(G,input1));
					break;
				case 2://求顶点所在的数组下标
					System.out.println("请输入要查找的顶点:");
					Object input2 = sc.next();
					System.out.println("顶点" + input2.toString() + "的数组下标为:" + G.locateVex(input2));
					break;
				case 3://求顶点的第一条边指向的顶点的数组下标
					System.out.println("请输入该顶点的数组下标:");
					int input3 = sc.nextInt();
					System.out.println("下标为:" + G.firstAdjVex(input3));
					break;
				case 4://求顶点的第w条边的下一条边指向的顶点的数组下标
					System.out.println("请输入该顶点的数组下标v和第w条边:");
					int input4 = sc.nextInt();
					int input5 = sc.nextInt();
					System.out.println("下标为:" + G.nextAdjVex(input4, input5));
					break;
				case 5://求顶点个数
					System.out.println("顶点个数为:" + G.getVexNum());
					break;
				case 6://求边数
					System.out.println("边数为" + G.getArcNum());
					break;
				case 7://求图的类型
					System.out.println("图的类型为:" + G.getKind().name());
					break;
				case 8://输出图的顶点的数值
					System.out.println("请输入该顶点的数组下标:");
					int input6 = sc.nextInt();
					System.out.println("顶点数值为:" + G.getVex(input6));
					break;
				case 9://广搜
					G.BFSTraverse(G);
					break;
				case 10://深搜
					G.DFSTraverse(G);
					break;
				case 11://退出本次操作
					break;
				default :
					System.out.println("输入有误");
					break;
			}
			flag = 1;
			while(flag>0) {
				System.out.println();
				System.out.println("是否要结束对图的操作?y/n");
				judjement = sc.next();
				if(judjement.equals("y"))
					break;
				else if(judjement.equals("n"))
					flag = -1;
				else
					System.out.println("输入有误");
			}
			if(flag == 1) {
				break;
			}
		}
		sc.close();
		System.out.println("程序结束");
	}
}

运行结果

(代码运行起来有些冗长,小弟能力有限,见谅)

请输入图的类型:DG:有向图 UDG:无向图 DN:有向网 UDN:无向网
DG
请输入图的顶点数和边数:
4 4
请输入图的各顶点值:
V1 V2 V3 V4
请输入边的信息:
V1 V2
V2 V3
V2 V4
V3 V4
请输入以下数字执行您想要进行的操作:
1:   求顶点的度
2:   求顶点所在的数组下标
3:   求顶点的第一条边指向的顶点的数组下标
4:   求顶点的第w条边的下一条边指向的顶点的数组下标
5:   求顶点个数
6:   求边数
7:   求图的类型
8:   输出图的顶点的数值
9:   广搜
10: 深搜
11: 退出本次操作
1
请输入要查找的顶点:
V1
顶点V1的度数为:1

是否要结束对图的操作?y/n
n
请输入以下数字执行您想要进行的操作:
1:   求顶点的度
2:   求顶点所在的数组下标
3:   求顶点的第一条边指向的顶点的数组下标
4:   求顶点的第w条边的下一条边指向的顶点的数组下标
5:   求顶点个数
6:   求边数
7:   求图的类型
8:   输出图的顶点的数值
9:   广搜
10: 深搜
11: 退出本次操作
1
请输入要查找的顶点:
V2
顶点V2的度数为:3

是否要结束对图的操作?y/n
n
请输入以下数字执行您想要进行的操作:
1:   求顶点的度
2:   求顶点所在的数组下标
3:   求顶点的第一条边指向的顶点的数组下标
4:   求顶点的第w条边的下一条边指向的顶点的数组下标
5:   求顶点个数
6:   求边数
7:   求图的类型
8:   输出图的顶点的数值
9:   广搜
10: 深搜
11: 退出本次操作
1
请输入要查找的顶点:
V3
顶点V3的度数为:2

是否要结束对图的操作?y/n
n
请输入以下数字执行您想要进行的操作:
1:   求顶点的度
2:   求顶点所在的数组下标
3:   求顶点的第一条边指向的顶点的数组下标
4:   求顶点的第w条边的下一条边指向的顶点的数组下标
5:   求顶点个数
6:   求边数
7:   求图的类型
8:   输出图的顶点的数值
9:   广搜
10: 深搜
11: 退出本次操作
1
请输入要查找的顶点:
V4
顶点V4的度数为:2

是否要结束对图的操作?y/n
n
请输入以下数字执行您想要进行的操作:
1:   求顶点的度
2:   求顶点所在的数组下标
3:   求顶点的第一条边指向的顶点的数组下标
4:   求顶点的第w条边的下一条边指向的顶点的数组下标
5:   求顶点个数
6:   求边数
7:   求图的类型
8:   输出图的顶点的数值
9:   广搜
10: 深搜
11: 退出本次操作
10
深搜的结果为:
V1 V2 V4 V3 
是否要结束对图的操作?y/n
n
请输入以下数字执行您想要进行的操作:
1:   求顶点的度
2:   求顶点所在的数组下标
3:   求顶点的第一条边指向的顶点的数组下标
4:   求顶点的第w条边的下一条边指向的顶点的数组下标
5:   求顶点个数
6:   求边数
7:   求图的类型
8:   输出图的顶点的数值
9:   广搜
10: 深搜
11: 退出本次操作
9
广搜的结果为:
V1 V2 V3 V4 
是否要结束对图的操作?y/n
y
程序结束

  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值