我制作了一个Graph类,该类使用HashMap>将节点保留为键,并将相应边保留为值。
public class GraphAL {
private HashMap<Integer, ArrayList<Integer>> adjList;
private ArrayList<Integer> vertices;
int numberOfNodes;
boolean visited[];
GraphAL(HashMap<Integer, ArrayList<Integer>> adjList,
ArrayList<Integer> vertices, int numberOfNodes){
this.adjList = adjList;
this.vertices = vertices;
this.numberOfNodes = numberOfNodes;
visited = new boolean[this.numberOfNodes];
}
public HashMap<Integer, ArrayList<Integer>> getAdjList() {
return adjList;
}
public ArrayList<Integer> getVertices() {
return vertices;
}
public int getNumeberOfNodes()
{
return numberOfNodes;
}
public boolean[] getVIsitedNodes()
{
return visited;
}
public void setVisitedNodesToTrue(int node)
{
visited[node] = true;
}
}
然后,我制作了一种反转图形的方法。 问题是尽管我的键是唯一的,但是java.util.HashMap.get方法返回2个值而不是1个。这导致将边缘添加到的位置比我要添加边缘的确切节点更多。
public static GraphAL reverseGraph(GraphAL g)
{
HashMap<Integer, ArrayList<Integer>> revAdjList = new HashMap<Integer, ArrayList<Integer>>();
ArrayList<Integer> revVertices = new ArrayList<Integer>();
System.out.println("Printing in for loop");
for(Integer x:g.getVertices())
{
System.out.println("x = " + x);
ArrayList<Integer> edges = new ArrayList<Integer>();
edges.add(x);
System.out.println("Edges: ");
for(Integer m:edges)
{
System.out.print(m + " ");
}
for(Integer y:g.getAdjList().get(x))
{
System.out.println("y = " + y);
if(!revVertices.contains(y))
{
revVertices.add(y);
System.out.println("RevVertices: ");
for(Integer n:revVertices)
{
System.out.print(n + " ");
}
}
if(revAdjList.containsKey(y))
{
for(Integer o:revAdjList.get(5))
{
System.out.println(5 + " contains " + o);
}
System.out.println("adding " + x + " to " + y);
revAdjList.get(y).add(x);
for(Integer o:revAdjList.get(y))
{
System.out.println(y + " contains " + o);
}
for(Integer o:revAdjList.get(5))
{
System.out.println(5 + " contains " + o);
}
}
else
{
revAdjList.put(y, edges);
System.out.println("putting " + x + " at " + y);
}
System.out.println("Current AdjList: ");
for(Integer h:revVertices)
{
System.out.print("vertice is: " + h + " ");
for(Integer j:revAdjList.get(h))
{
System.out.print("edge : " + j + " ");
}
System.out.println();
}
}
}
System.out.println("Done printing in for loop");
GraphAL revGraph = new GraphAL(revAdjList, revVertices, g.getNumeberOfNodes());
return revGraph;
}
对所有print.ln的文档表示抱歉,但我想确定问题出在哪里。它看起来像revAdjList.get(y).add(x),其中y = 6和x = 3像我想要的那样也从键6和键5返回对应的ArrayList。Ofc这导致将边3添加到6就像我想要的一样,但它也将其添加到5。 有想法吗?
x = 3
Edges:
3 y = 6
5 contains 8
adding 3 to 6
6 contains 8
6 contains 3
5 contains 8
5 contains 3
adding 3 to 6
Current AdjList:
vertice is: 1 edge : 7
vertice is: 2 edge : 5
vertice is: 3 edge : 9
vertice is: 7 edge : 9
vertice is: 4 edge : 1
vertice is: 5 edge : 8 edge : 3
vertice is: 6 edge : 8 edge : 3
答案 0 :(得分:0)
问题在这条线上发生
for (Integer j : revAdjList.get(h)) {
System.out.print("edge : " + j + " ");
}
让我们在此指出问题以及为什么两次打印"edge"
revAdjList.get(h)
返回Integer
的集合,而不是单个Integer,因此它可以具有任意数量的整数值,因为您将其定义为集合而不是单吨。
您要从此处添加多个边缘
for (Integer m : edges) {
System.out.print(m + " ");
}
如果只需要1条边,请不要将edges
做成数组。
HashMap<Integer, ArrayList<Integer>> revAdjList = new HashMap<>();
应定义为
HashMap<Integer, Integer> revAdjList = new HashMap<>();
完成此操作后,您可以删除循环并清理代码以使用新语法。