posts - 201,  comments - 45,  views - 20万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
package com.tyson.graph;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class GraphMatrix {
	private ArrayList<Integer> vertexList;
	private int[][] edges;
	private int edgesNum;
	
	public GraphMatrix(int n) {
		edges = new int[n][n];
		edgesNum = 0;
		vertexList = new ArrayList<Integer>(n);
	}
	
	public void insertVertex(int vertex) {
		vertexList.add(vertex);
	}
	
	public void insertVertex(int[] vertexes) {
		for(int i=0;i<vertexes.length;i++) {
			vertexList.add(vertexes[i]);
		}
	}
	
	public void insertEdge(int i, int j, int weight) {
		if(i<0||j<0||i>=vertexList.size()||j>=vertexList.size()) {
			return;
		}
		
		edges[i][j] = weight;
		edgesNum++;
	}
	
	public int getVertexNum() {
		return vertexList.size();
	}
	
	//得到第一个邻接节点的下标
	public int getFirstNeighbor(int index) {
		for(int i=0;i<vertexList.size();i++) {
			if(edges[index][i]>0) {
				return i;
			}
		}
		return -1;
	}
	
	//得到第k个邻接节点的下一个节点的下标
	public int getNextNeighbor(int index, int k) {
		for(int i=k+1;i<vertexList.size();i++) {
			if(edges[index][i]>0) 
				return i;
		}
		return -1;
	}
	
	private void DFS(boolean[] isVisited, int i) {
		if(isVisited[i] == false) {
			System.out.print(vertexList.get(i) + "-->");
			//标记为已访问
			isVisited[i] = true;
			int index = getFirstNeighbor(i);
			while(index != -1) {
				if(isVisited[index] == false) {
					DFS(isVisited, index);
				}
				index = getNextNeighbor(i, index);
			} 
		}
		System.out.println();
	}
	
	public void DFS() {
		boolean[] isVisited = new boolean[vertexList.size()];
		
		for(int j=0;j<vertexList.size();j++) {
			isVisited[j] = false;
		}
		for(int i=0;i<vertexList.size();i++) {
			if(isVisited[i] == false)
				DFS(isVisited, i);
		}
	}
	
	public void BFS() {
		boolean isVisited[] = new boolean[vertexList.size()];
		for(int i=0;i<vertexList.size();i++) {
			isVisited[i] = false;
		}
		
		for(int j=0;j<vertexList.size();j++) {
			BFS(isVisited, j);
		}
	}
	
	private void BFS(boolean[] isVisited, int index) {
		if(isVisited[index] == true)
			return;
		Queue<Integer> queue = new LinkedList<Integer>();
		queue.offer(index);
		
		while(!queue.isEmpty()) {
			index = queue.poll();
			if(isVisited[index] == false) {
				System.out.print(vertexList.get(index) + "-->");
				isVisited[index] = true;
				
				int neighbor = getFirstNeighbor(index);
				
				while(neighbor != -1) {
					queue.offer(neighbor);
					neighbor = getNextNeighbor(index, neighbor);
					System.out.print(" put ");
				}
				System.out.println();
			}
		}
	}


    public static void main(String args[]){
    	GraphMatrix g = new GraphMatrix(5);
    	
    	int[] vertexes = {1, 2, 3, 4, 5};
    	
    	g.insertVertex(vertexes);
    	
    	g.insertEdge(0, 1, 1);
    	g.insertEdge(0, 2, 1);
    	g.insertEdge(0, 3, 1);
    	g.insertEdge(1, 3, 1);
    	g.insertEdge(2, 3, 1);
	
    	g.BFS();
    }
}
posted on   程序员大彬  阅读(146)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示