java 是否循环_java实现有向图和判断是否存在循环

最新在看xwork的源代码,XmlConfigurationProvider这个类,用来实现解析xwork.xml配置文件。该类使用了有向图这种数据结构,来判断是否存在元素的循环包含。

有向图的实现如下:

import java.util.Collections;

import java.util.HashMap;

import java.util.HashSet;

import java.util.Iterator;

import java.util.Map;

import java.util.NoSuchElementException;

import java.util.Set;

public final class DirectedGraph implements Iterable

{

private final Map> mGraph = new HashMap>();

/**

* Adds a new node to the graph. If the node already exists, this function is a no-op.

*

* @param node

* The node to add.

* @return Whether or not the node was added.

*/

public boolean addNode(T node) {

/* If the node already exists, don't do anything. */

if (mGraph.containsKey(node))

return false;

/* Otherwise, add the node with an empty set of outgoing edges. */

mGraph.put(node, new HashSet());

return true;

}

/**

* Given a start node, and a destination, adds an arc from the start node to the destination. If an arc already exists, this operation is a no-op.

* If either endpoint does not exist in the graph, throws a NoSuchElementException.

*

* @param start

* The start node.

* @param dest

* The destination node.

* @throws NoSuchElementException

* If either the start or destination nodes do not exist.

*/

public void addEdge(T start, T dest) {

/* Confirm both endpoints exist. */

if (!mGraph.containsKey(start)) {

throw new NoSuchElementException("The start node does not exist in the graph.");

} else if (!mGraph.containsKey(dest)) {

throw new NoSuchElementException("The destination node does not exist in the graph.");

}

/* Add the edge. */

mGraph.get(start).add(dest);

}

/**

* Removes the edge from start to dest from the graph. If the edge does not exist, this operation is a no-op. If either endpoint does not exist,

* this throws a NoSuchElementException.

*

* @param start

* The start node.

* @param dest

* The destination node.

* @throws NoSuchElementException

* If either node is not in the graph.

*/

public void removeEdge(T start, T dest) {

/* Confirm both endpoints exist. */

if (!mGraph.containsKey(start)) {

throw new NoSuchElementException("The start node does not exist in the graph.");

} else if (!mGraph.containsKey(dest)) {

throw new NoSuchElementException("The destination node does not exist in the graph.");

}

mGraph.get(start).remove(dest);

}

/**

* Given two nodes in the graph, returns whether there is an edge from the first node to the second node. If either node does not exist in the

* graph, throws a NoSuchElementException.

*

* @param start

* The start node.

* @param end

* The destination node.

* @return Whether there is an edge from start to end.

* @throws NoSuchElementException

* If either endpoint does not exist.

*/

public boolean edgeExists(T start, T end) {

/* Confirm both endpoints exist. */

if (!mGraph.containsKey(start)) {

throw new NoSuchElementException("The start node does not exist in the graph.");

} else if (!mGraph.containsKey(end)) {

throw new NoSuchElementException("The end node does not exist in the graph.");

}

return mGraph.get(start).contains(end);

}

/**

* Given a node in the graph, returns an immutable view of the edges leaving that node as a set of endpoints.

*

* @param node

* The node whose edges should be queried.

* @return An immutable view of the edges leaving that node.

* @throws NoSuchElementException

* If the node does not exist.

*/

public Set edgesFrom(T node) {

/* Check that the node exists. */

Set arcs = mGraph.get(node);

if (arcs == null)

throw new NoSuchElementException("Source node does not exist.");

return Collections.unmodifiableSet(arcs);

}

/**

* Returns an iterator that can traverse the nodes in the graph.

*

* @return An iterator that traverses the nodes in the graph.

*/

public Iterator iterator() {

return mGraph.keySet().iterator();

}

/**

* Returns the number of nodes in the graph.

*

* @return The number of nodes in the graph.

*/

public int size() {

return mGraph.size();

}

/**

* Returns whether the graph is empty.

*

* @return Whether the graph is empty.

*/

public boolean isEmpty() {

return mGraph.isEmpty();

}

}

通过上述源码发现:xwork框架采用的是有向图的

邻接表方法进行存储的。

xwork使用CycleDetector用来判断有向图是否存在循环,实现如下:

public class CycleDetector

{

private static final String marked = "marked";

private static final String complete = "complete";

private DirectedGraph graph;

private Map marks;

private List verticesInCycles;

public CycleDetector(DirectedGraph graph)

{

this.graph = graph;

marks = new HashMap();

verticesInCycles = new ArrayList();

}

public boolean containsCycle()

{

for (T v : graph)

{

// 如果v正在遍历或者遍历完成,不需要进入mark(),因为mark是一个递归调用,使用的是深度优先搜索算法;

// 这是为了保证1个顶点只会遍历一次

if (!marks.containsKey(v))

{

if (mark(v))

{

// return true;

}

}

}

return !verticesInCycles.isEmpty();

}

//DFS算法,遍历顶点vertex

// @return 当前顶点是否在环上

private boolean mark(T vertex)

{

List localCycles = new ArrayList();

// 当前顶点vertex,遍历开始

marks.put(vertex, marked);

for (T u : graph.edgesFrom(vertex))

{

// u的遍历还没有结束,说明存在u->vertex的通路,也存在vertex->u的通路,形成了循环

if (marks.containsKey(u) && marks.get(u).equals(marked))

{

localCycles.add(vertex);

// return true;

}

else if (!marks.containsKey(u))

{

if (mark(u))

{

localCycles.add(vertex);

// return true;

}

}

}

// 当前顶点vertex,遍历完成

marks.put(vertex, complete);

verticesInCycles.addAll(localCycles);

return !localCycles.isEmpty();

}

public List getVerticesInCycles()

{

return verticesInCycles;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
循环有向图的遍历可以使用拓扑排序来实现。拓扑排序是一种对有向无环图进行排序的算法,它将图中所有顶点排成一个线性序列,使得图中任意一条有向边的起点在序列中都排在终点的前面。 具体实现步骤如下: 1. 统计每个节点的入度,即有多少个节点指向该节点。 2. 将所有入度为 0 的节点加入队列。 3. 取出队首节点,将其加入结果集中,并将其所有指向的节点的入度减 1。 4. 如果某个节点的入度减为 0,则将该节点加入队列。 5. 重复步骤 3 和步骤 4,直到队列为空。 在遍历过程中,如果遇到了环,则说明该图不是有向无环图,无法进行拓扑排序。 下面是 Java 代码实现: ``` import java.util.*; public class TopologicalSort { public List<Integer> sort(int[][] graph) { int n = graph.length; int[] inDegree = new int[n]; List<Integer> result = new ArrayList<>(); // 统计入度 for (int[] edge : graph) { inDegree[edge[1]]++; } // 将入度为 0 的节点加入队列 Queue<Integer> queue = new LinkedList<>(); for (int i = 0; i < n; i++) { if (inDegree[i] == 0) { queue.offer(i); } } // 拓扑排序 while (!queue.isEmpty()) { int node = queue.poll(); result.add(node); for (int[] edge : graph) { if (edge[0] == node) { inDegree[edge[1]]--; if (inDegree[edge[1]] == 0) { queue.offer(edge[1]); } } } } // 如果存在环,则返回空列表 if (result.size() != n) { return new ArrayList<>(); } else { return result; } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值