Java实现图的深度和广度优先遍历算法

概述:

  最近要学习写网络爬虫,所以把图的深度和广度搜索都再温习一下。


图结构展示:



实现过程:

首先,我们来看看图结构在代码中的实现。有三块逻辑:

1.图中的节点:

public class GraphNode {

	public List<GraphEdge> edgeList = null;
	
	private String label = "";
	
	public GraphNode(String label) {
		this.label = label;
		if (edgeList == null) {
			edgeList = new ArrayList<GraphEdge>();
		}
	}
	
	/**
	 * 给当前节点添加一条边
	 * GraphNode
	 * @param edge
	 * 			添加的边
	 */
	public void addEdgeList(GraphEdge edge) {
		edgeList.add(edge);
	}
	
	public String getLabel() {
		return label;
	}
}
 

2.图中的边:

 

public class GraphEdge {

	private GraphNode nodeLeft;
	
	private GraphNode nodeRight;

	/**
	 * @param nodeLeft
	 * 			边的左端
	 * @param nodeRight
	 * 			边的右端
	 */
	public GraphEdge(GraphNode nodeLeft, GraphNode nodeRight) {
		this.nodeLeft = nodeLeft;
		this.nodeRight = nodeRight;
	}

	public GraphNode getNodeLeft() {
		return nodeLeft;
	}

	public GraphNode getNodeRight() {
		return nodeRight;
	}
	
}
 

3.把节点和边组合成一个图结构:

 

public class MyGraph {

	private List<GraphNode> nodes = null;
	
	public void initGraph(int n) {
		if (nodes == null) {
			nodes = new ArrayList<GraphNode>();
		}
		
		GraphNode node = null;
		for (int i = 0; i < n; i++) {
			node = new GraphNode(String.valueOf(i));
			nodes.add(node);
		}
	}
	
	public void initGraph(int n, boolean b) {
		initGraph(n);
		GraphEdge edge01 = new GraphEdge(nodes.get(0), nodes.get(1));
		GraphEdge edge02 = new GraphEdge(nodes.get(0), nodes.get(2));
		GraphEdge edge13 = new GraphEdge(nodes.get(1), nodes.get(3));
		GraphEdge edge14 = new GraphEdge(nodes.get(1), nodes.get(4));
		GraphEdge edge25 = new GraphEdge(nodes.get(2), nodes.get(5));
		GraphEdge edge26 = new GraphEdge(nodes.get(2), nodes.get(6));
		GraphEdge edge37 = new GraphEdge(nodes.get(3), nodes.get(7));
		GraphEdge edge47 = new GraphEdge(nodes.get(4), nodes.get(7));
		GraphEdge edge56 = new GraphEdge(nodes.get(5), nodes.get(6));
		
		
		nodes.get(0).addEdgeList(edge01);
		nodes.get(0).addEdgeList(edge02);
		nodes.get(1).addEdgeList(edge13);
		nodes.get(1).addEdgeList(edge14);
		nodes.get(2).addEdgeList(edge25);
		nodes.get(2).addEdgeList(edge26);
		nodes.get(3).addEdgeList(edge37);
		nodes.get(4).addEdgeList(edge47);
		nodes.get(5).addEdgeList(edge56);
	}
	
	public void initGraph() {
		initGraph(8, false);
	}
	
	public List<GraphNode> getGraphNodes() {
		return nodes;
	}
}
 
有了图的结构,我们就可以进行一些实际的操作了。

深度优先搜索:

 

public class DFSearch {
	
	/**
	 * 深度遍历
	 * DFSearch
	 * @param node
	 * 			当前节点
	 * @param visited
	 * 			被访问过的节点列表
	 */
	public void searchTraversing(GraphNode node, List<GraphNode> visited) {
		// 判断是否遍历过
		if (visited.contains(node)) {
			return;
		}
		
		visited.add(node);
		System.out.println("节点:" + node.getLabel());
		for (int i = 0; i < node.edgeList.size(); i++) {
			searchTraversing(node.edgeList.get(i).getNodeRight(), visited);
		}
	}
}
 

广度优先搜索:

 

public class BFSearch {
	
	/**
	 * 广度优先搜索
	 * BFSearch
	 * @param node
	 * 			搜索的入口节点
	 */
	public void searchTraversing(GraphNode node) {
		List<GraphNode> visited = new ArrayList<GraphNode>(); // 已经被访问过的元素
		Queue<GraphNode> q = new LinkedList<GraphNode>(); // 用队列存放依次要遍历的元素
		q.offer(node);
		
		while (!q.isEmpty()) {
			GraphNode currNode = q.poll();
			if (!visited.contains(currNode)) {
				visited.add(currNode);
				System.out.println("节点:" + currNode.getLabel());
				for (int i = 0; i < currNode.edgeList.size(); i++) {
					q.offer(currNode.edgeList.get(i).getNodeRight());
				}
			}
		}
	}
}

 

运行结果:



源码下载:

http://download.csdn.net/detail/u013761665/8968443

转载于:https://www.cnblogs.com/fengju/p/6336053.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值