数据结构——图

本文详细介绍了数据结构中的图,并重点讲解了如何使用邻接矩阵进行图的表示。通过SeqList类实现线性表,进而构建AdjMWGraph类来创建和操作邻接矩阵图。此外,还包括了测试类的代码示例,帮助程序员更好地理解和应用图的这一数据结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

邻接矩阵图

SeqList类(线性表类):

public class SeqList {
	final int defaultSize = 10;
	
	int maxSize;
	int size;
	Object[] listArray;
	
	public SeqList() {
		initiate(defaultSize);
	}
	
	public SeqList(int size) {
		initiate(size);
	}
	
	private void initiate(int sz) {
		maxSize = sz;
		size = 0;
		listArray = new Object[sz];
	}
	
	public void insert(int i , Object obj) throws Exception {
		if (size == maxSize) {
			throw new Exception("顺序表已满无法插入!");
		}
		
		if (i < 0 || i > size) {
			throw new Exception("参数错误!");
		}
		
		for ( int j = size ; j > i ; j --) {
			listArray[j] = listArray[j - 1];
		}
		listArray[i] = obj;
		size++;
	}
	
	public Object delete(int i) throws Exception {
		if (size == 0) {
			throw new Exception("顺序表已空无法删除!");
		}
		
		if (i < 0 || i > size - 1) {
			throw new Exception("参数错误!");
		}
		
		Object it = listArray[i];
		for ( int j = i ; j < size - 1 ; j ++ ) {
			listArray[j] = listArray[j + 1];
		}
		size--;
		return it;
	}
	
	public Object getData(int i) throws Exception {
		if (i < 0 || i > size - 1) {
			throw new Exception("参数错误!");
		}
		return listArray[i];
	}
	
	public int size() {
		return size;
	}
	
	public boolean isEmpty() {
		return size == 0;
	}
	
	public int MoreDataDelete(SeqList L , Object x) throws Exception {
		int i ;
		int tag = 0;
		
		for ( i = 0 ; i < L.size ; i ++ ) {
			if (x.equals(L.getData(i))) {
				L.delete(i);
				i --;
				tag = 1;
			}
		}
		return tag;
	}
}

AdjMWGraph类(邻接矩阵图类):


public class AdjMWGraph {
	static final int maxWeight = 10000;
	
	private SeqList vertices;  //存储结点的顺序表
	private int[][] edge;  //存储表的二维数组
	private int numOfEdges;  //边的个数
	
	public AdjMWGraph(int maxV) {  
		vertices = new SeqList(maxV);
		edge = new int[maxV][maxV];
		for ( int i = 0 ; i < maxV ; i ++ ) {
			for ( int j = 0 ; j < maxV ; j ++ ) {
				if ( i == j ) 
					edge[i][j] = 0;
				else 
					edge[i][j] = maxWeight;
			}
		}
		numOfEdges = 0;
	}
	
	public int getNumOfVertices() {  //返回结点个数
		return vertices.size;
	}
	
	public int getNumOfEdges() {  //返回边的个数
		return numOfEdges;
	}
	
	public Object getValue(int v) throws Exception {  //返回结点v的数据元素
		return vertices.getData(v);
	}
	
	public int getWeight(int v1 , int v2) throws Exception {  //返回边<v1,v2>的值
		if (v1 < 0 || v1 >= vertices.size || v2 < 0 || v2 >= vertices.size) 
			throw new Exception("参数v1或v2越界出错!");
		return edge[v1][v2];
	}
	
	public void insertVertex(Object vertex) throws Exception {  //插入节点
		vertices.insert(vertices.size, vertex);
	}
	
	public void insertEdge(int v1 , int v2 , int weight) throws Exception {  //插入边<v1,v2>,权值为weight
		if (v1 < 0 || v1 >= vertices.size || v2 < 0 || v2 >= vertices.size) 
			throw new Exception("参数v1或v2越界出错!");
		edge[v1][v2] = weight;
		numOfEdges++;
	}
	
	public void deleteEdge(int v1 , int v2) throws Exception {  //删除边<v1,v2>
		if (v1 < 0 || v1 >= vertices.size || v2 < 0 || v2 >= vertices.size) 
			throw new Exception("参数v1或v2越界出错!");
		if (edge[v1][v2] == maxWeight || v1 == v2)
			throw new Exception("该边不存在!");
		edge[v1][v2] = maxWeight;
		numOfEdges--;
	}
	
	public int getFirstNeighbor(int v) throws Exception {  //取结点v的第一个邻接结点。若存在返回该结点的下标序号,否则返回-1
		if (v < 0 || v >= vertices.size)
			throw new Exception("参数v越界出错!");
		for ( int col = 0 ; col < vertices.size ; col ++ ) {
			if (edge[v][col] > 0 && edge[v][col] < maxWeight) 
				return col;
		}
		return -1;
	}
	
	public int getNextNeighbor(int v1 , int v2) throws Exception {  //取结点v1的邻接结点v2后的邻接结点。若存在返回该结点的下标序号,否则返回-1
		if (v1 < 0 || v1 >= vertices.size || v2 < 0 || v2 >= vertices.size) 
			throw new Exception("参数v1或v2越界出错!");
		for ( int col = v2 + 1 ; col < vertices.size ; col ++ ) {
			if (edge[v1][col] > 0 && edge[v1][col] < maxWeight) 
				return col;
		}
		return -1;
	}
	
}

测试类:

public class RowColWeight {
	int row;  //行下标
	int col;  //列下标
	int weight;  //权值
	
	public RowColWeight(int r , int c , int w) {
		row = r;
		col = c;
		weight = w;
	}
	
	public static void createGraph(AdjMWGraph g , Object[] v , int n , RowColWeight[] rc , int e) throws Exception {
		for ( int i = 0 ; i < n ; i ++ ) 
			g.insertVertex(v[i]);
		for ( int k = 0 ; k < e ; k ++ ) 
			g.insertEdge(rc[k].row, rc[k].col, rc[k].weight);
	}
	
	public static void main(String[] args) {
		int n = 5 , e = 5;
		AdjMWGraph g = new AdjMWGraph(n);
		Character[] a = {new Character('A') , 
						 new Character('B') ,
						 new Character('C') , 
						 new Character('D') , 
						 new Character('E') , 
				};
		RowColWeight[] rcw = {new RowColWeight(0, 1, 10) ,
							  new RowColWeight(0, 4, 20) ,
							  new RowColWeight(1, 3, 30) ,
							  new RowColWeight(2, 1, 40) ,
							  new RowColWeight(3, 2, 50) ,
				};
		try {
			RowColWeight.createGraph(g, a, n, rcw, e);
			System.out.println("结点个数为:" + g.getNumOfVertices());
			System.out.println("边的个数为:" + g.getNumOfEdges());
			
			g.deleteEdge(0, 4);
			System.out.println();
			System.out.println("结点个数为:" + g.getNumOfVertices());
			System.out.println("边的个数为:" + g.getNumOfEdges());
		}
		catch (Exception e1) {
			e1.printStackTrace();
		}
	}
}
输出结果为:
结点个数为:5
边的个数为:5


结点个数为:5
边的个数为:4


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值