如果有人可以帮助我完成此任务,我将不胜感激。
任务:实现一个Graph类(有向和加权),而无需使用Java的标准Graph-class。
使用adjacencyList或adjMatrix都没关系
我的密码
import java.util.*;
public class Graph {
static class Edge {
char vA;
char vB;
double weight;
Edge(char a, char b, double weight) {
vA = a;
vB = b;
this.weight = weight;
}
void setWeight(double w) {
weight = w;
}
double getWeight() {
return weight;
}
}
private Map<Character, LinkedList<Character>> edges = new HashMap<>();
void addNode(char node) {
if (!edges.containsKey(node)) {
edges.put(node, new LinkedList<Character>());
}
}
public void addEdge(char a, char b, double weight) {
Edge e = new Edge(a, b, weight);
a = e.vA;
b = e.vB;
e.setWeight(weight);
edges.get(a).add(b);
}
void printNodes() {
System.out.println(edges.keySet());
}
void printEdges() {
System.out.println(edges.values());
}
void dfs() {
//TODO
}
void dijkstra(char startNodeID){
}
public static void main(String... args) {
Graph graph = new Graph();
graph.addNode('A');
graph.addNode('B');
graph.addNode('C');
graph.addNode('D');
graph.addNode('E');
graph.addEdge('A', 'B', 2);
graph.addEdge('A', 'C', 3);
graph.addEdge('B', 'D', 6);
graph.addEdge('C', 'D', 8);
graph.addEdge('C', 'E', 9);
graph.printEdges();
graph.printNodes();
}
}
问题:
如何在EdgeMap的LinkedList中添加边?
我的方法addEdges有什么问题?
如何打印边缘?
相关分类