- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
谁能帮我在我的图表上实现 bfs?我将我的图形实现放在这里,并将 bfs 算法放到我的图形中。我只需要一些想法如何去做。
public class DiGraph<V> {
public static class Edge<V>{
private V vertex;
private int cost;
public Edge(V v, int c){
vertex = v;
cost = c;
}
@Override
public String toString() {
return "{" + vertex + ", " + cost + "}";
}
}
private Map<V, List<Edge<V>>> inNeighbors = new HashMap<V, List<Edge<V>>>();
private Map<V, List<Edge<V>>> outNeighbors = new HashMap<V, List<Edge<V>>>();
public int nr_vertices;
public int nr_edges;
public String toString () {
StringBuffer s = new StringBuffer();
for (V v: inNeighbors.keySet())
s.append("\n " + v + " -> " + inNeighbors.get(v));
return s.toString();
}
public void addIn (V vertex) {
if (inNeighbors.containsKey(vertex))
return;
inNeighbors.put(vertex, new ArrayList<Edge<V>>());
}
public void addOut(V vertex) {
if (outNeighbors.containsKey(vertex))
return;
outNeighbors.put(vertex, new ArrayList<Edge<V>>());
}
public boolean contains (V vertex) {
return inNeighbors.containsKey(vertex);
}
public void add (V from, V to, int cost) {
this.addIn(from);
this.addIn(to);
this.addOut(to);
this.addOut(from);
inNeighbors.get(from).add(new Edge<V>(to,cost));
outNeighbors.get(to).add(new Edge<V>(from,cost));
}
public int outDegree (V vertex) {
return inNeighbors.get(vertex).size();
}
public int inDegree (V vertex) {
return inboundNeighbors(vertex).size();
}
}
public void bfs()
{
// BFS uses Queue data structure
Queue queue = new LinkedList();
queue.add(this.rootNode);
printNode(this.rootNode);
rootNode.visited = true;
while(!queue.isEmpty()) {
Node node = (Node)queue.remove();
Node child=null;
while((child=getUnvisitedChildNode(node))!=null) {
child.visited=true;
printNode(child);
queue.add(child);
}
}
// Clear visited property of nodes
clearNodes();
}
bfs算法我从网上拿来的,我理解它但是它是针对一般情况的,我不知道如何将它适应我的图表
最佳答案
这就是解决您的问题的方法。我更喜欢只粘贴最终解决方案而不重复整个代码以使您更容易阅读。如果您有兴趣查看代码之前的情况,请查看编辑历史。
package stackoverflow.questions.q19757371;
import java.io.IOException;
import java.util.*;
public class Digraph<V> {
public static class Edge<V>{
private V vertex;
private int cost;
public Edge(V v, int c){
vertex = v; cost = c;
}
public V getVertex() {
return vertex;
}
public int getCost() {
return cost;
}
@Override
public String toString() {
return "Edge [vertex=" + vertex + ", cost=" + cost + "]";
}
}
/**
* A Map is used to map each vertex to its list of adjacent vertices.
*/
private Map<V, List<Edge<V>>> neighbors = new HashMap<V, List<Edge<V>>>();
private int nr_edges;
/**
* String representation of graph.
*/
public String toString() {
StringBuffer s = new StringBuffer();
for (V v : neighbors.keySet())
s.append("\n " + v + " -> " + neighbors.get(v));
return s.toString();
}
/**
* Add a vertex to the graph. Nothing happens if vertex is already in graph.
*/
public void add(V vertex) {
if (neighbors.containsKey(vertex))
return;
neighbors.put(vertex, new ArrayList<Edge<V>>());
}
public int getNumberOfEdges(){
int sum = 0;
for(List<Edge<V>> outBounds : neighbors.values()){
sum += outBounds.size();
}
return sum;
}
/**
* True iff graph contains vertex.
*/
public boolean contains(V vertex) {
return neighbors.containsKey(vertex);
}
/**
* Add an edge to the graph; if either vertex does not exist, it's added.
* This implementation allows the creation of multi-edges and self-loops.
*/
public void add(V from, V to, int cost) {
this.add(from);
this.add(to);
neighbors.get(from).add(new Edge<V>(to, cost));
}
public int outDegree(int vertex) {
return neighbors.get(vertex).size();
}
public int inDegree(V vertex) {
return inboundNeighbors(vertex).size();
}
public List<V> outboundNeighbors(V vertex) {
List<V> list = new ArrayList<V>();
for(Edge<V> e: neighbors.get(vertex))
list.add(e.vertex);
return list;
}
public List<V> inboundNeighbors(V inboundVertex) {
List<V> inList = new ArrayList<V>();
for (V to : neighbors.keySet()) {
for (Edge e : neighbors.get(to))
if (e.vertex.equals(inboundVertex))
inList.add(to);
}
return inList;
}
public boolean isEdge(V from, V to) {
for(Edge<V> e : neighbors.get(from)){
if(e.vertex.equals(to))
return true;
}
return false;
}
public int getCost(V from, V to) {
for(Edge<V> e : neighbors.get(from)){
if(e.vertex.equals(to))
return e.cost;
}
return -1;
}
public static void main(String[] args) throws IOException {
Digraph<Integer> graph = new Digraph<Integer>();
graph.add(0);
graph.add(1);
graph.add(2);
graph.add(3);
graph.add(0, 1, 1);
graph.add(1, 2, 2);
graph.add(2, 3, 2);
graph.add(3, 0, 2);
graph.add(1, 3, 1);
graph.add(2, 1, 5);
System.out.println("The nr. of vertices is: " + graph.neighbors.keySet().size());
System.out.println("The nr. of edges is: " + graph.getNumberOfEdges()); // to be fixed
System.out.println("The current graph: " + graph);
System.out.println("In-degrees for 0: " + graph.inDegree(0));
System.out.println("Out-degrees for 0: " + graph.outDegree(0));
System.out.println("In-degrees for 3: " + graph.inDegree(3));
System.out.println("Out-degrees for 3: " + graph.outDegree(3));
System.out.println("Outbounds for 1: "+ graph.outboundNeighbors(1));
System.out.println("Inbounds for 1: "+ graph.inboundNeighbors(1));
System.out.println("(0,2)? " + (graph.isEdge(0, 2) ? "It's an edge" : "It's not an edge"));
System.out.println("(1,3)? " + (graph.isEdge(1, 3) ? "It's an edge" : "It's not an edge"));
System.out.println("Cost for (1,3)? "+ graph.getCost(1, 3));
}
}
This is my result:
The nr. of vertices is: 4
The nr. of edges is: 6
The current graph:
0 -> [Edge [vertex=1, cost=1]]
1 -> [Edge [vertex=2, cost=2], Edge [vertex=3, cost=1]]
2 -> [Edge [vertex=3, cost=2], Edge [vertex=1, cost=5]]
3 -> [Edge [vertex=0, cost=2]]
In-degrees for 0: 1
Out-degrees for 0: 1
In-degrees for 3: 2
Out-degrees for 3: 1
Outbounds for 1: [2, 3]
Inbounds for 1: [0, 2]
(0,2)? It's not an edge
(1,3)? It's an edge
Cost for (1,3)? 1
编辑我修复了 inboundNeighbors(V vertex)
并实现了 getCost(V from, V to)
。请注意,只有当您拥有优势并且我们假设成本为正时,第二个功能才有意义。否则,我们需要其他考虑因素,但我认为您最好再发一个问题。
在 main
中,我为您提供了一个完整的示例,因此,如果您尝试进行一些更改,请确保您获得与我在此处发布的相同的结果。当然所有的类都可以写得更好,但是,根据你的经验,最好让事情变得简单。
关于java - java中的有向图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19757371/
我想做的是让 JTextPane 在 JPanel 中占用尽可能多的空间。对于我使用的 UpdateInfoPanel: public class UpdateInfoPanel extends JP
我在 JPanel 中有一个 JTextArea,我想将其与 JScrollPane 一起使用。我正在使用 GridBagLayout。当我运行它时,框架似乎为 JScrollPane 腾出了空间,但
我想在 xcode 中实现以下功能。 我有一个 View Controller 。在这个 UIViewController 中,我有一个 UITabBar。它们下面是一个 UIView。将 UITab
有谁知道Firebird 2.5有没有类似于SQL中“STUFF”函数的功能? 我有一个包含父用户记录的表,另一个表包含与父相关的子用户记录。我希望能够提取用户拥有的“ROLES”的逗号分隔字符串,而
我想使用 JSON 作为 mirth channel 的输入和输出,例如详细信息保存在数据库中或创建 HL7 消息。 简而言之,输入为 JSON 解析它并输出为任何格式。 最佳答案 var objec
通常我会使用 R 并执行 merge.by,但这个文件似乎太大了,部门中的任何一台计算机都无法处理它! (任何从事遗传学工作的人的附加信息)本质上,插补似乎删除了 snp ID 的 rs 数字,我只剩
我有一个以前可能被问过的问题,但我很难找到正确的描述。我希望有人能帮助我。 在下面的代码中,我设置了varprice,我想添加javascript变量accu_id以通过rails在我的数据库中查找记
我有一个简单的 SVG 文件,在 Firefox 中可以正常查看 - 它的一些包装文本使用 foreignObject 包含一些 HTML - 文本包装在 div 中:
所以我正在为学校编写一个 Ruby 程序,如果某个值是 1 或 3,则将 bool 值更改为 true,如果是 0 或 2,则更改为 false。由于我有 Java 背景,所以我认为这段代码应该有效:
我做了什么: 我在这些账户之间创建了 VPC 对等连接 互联网网关也连接到每个 VPC 还配置了路由表(以允许来自双方的流量) 情况1: 当这两个 VPC 在同一个账户中时,我成功测试了从另一个 La
我有一个名为 contacts 的表: user_id contact_id 10294 10295 10294 10293 10293 10294 102
我正在使用 Magento 中的新模板。为避免重复代码,我想为每个产品预览使用相同的子模板。 特别是我做了这样一个展示: $products = Mage::getModel('catalog/pro
“for”是否总是检查协议(protocol)中定义的每个函数中第一个参数的类型? 编辑(改写): 当协议(protocol)方法只有一个参数时,根据该单个参数的类型(直接或任意)找到实现。当协议(p
我想从我的 PHP 代码中调用 JavaScript 函数。我通过使用以下方法实现了这一点: echo ' drawChart($id); '; 这工作正常,但我想从我的 PHP 代码中获取数据,我使
这个问题已经有答案了: Event binding on dynamically created elements? (23 个回答) 已关闭 5 年前。 我有一个动态表单,我想在其中附加一些其他 h
我正在尝试找到一种解决方案,以在 componentDidMount 中的映射项上使用 setState。 我正在使用 GraphQL连同 Gatsby返回许多 data 项目,但要求在特定的 pat
我在 ScrollView 中有一个 View 。只要用户按住该 View ,我想每 80 毫秒调用一次方法。这是我已经实现的: final Runnable vibrate = new Runnab
我用 jni 开发了一个 android 应用程序。我在 GetStringUTFChars 的 dvmDecodeIndirectRef 中得到了一个 dvmabort。我只中止了一次。 为什么会这
当我到达我的 Activity 时,我调用 FragmentPagerAdapter 来处理我的不同选项卡。在我的一个选项卡中,我想显示一个 RecyclerView,但他从未出现过,有了断点,我看到
当我按下 Activity 中的按钮时,会弹出一个 DialogFragment。在对话框 fragment 中,有一个看起来像普通 ListView 的 RecyclerView。 我想要的行为是当
我是一名优秀的程序员,十分优秀!