图类中的加权边表示

时间:2017-04-27 16:32:52

标签: java graph-theory

我目前正在实施Graph类。我应该使用什么数据结构才能存储一组边缘,以及能够检查边缘是否存在或是否返回其重量?这是我目前的工作。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.NoSuchElementException;

public class Graph 
{   
    private HashMap<Integer,Position> vertices;
    private ArrayList<Edge> edges;

    private class Edge
    {
        private int weight;
        private int vertex1;
        private int vertex2;

        public Edge(int vertex1, int vertex2, int weight)
        {
            this.weight = weight;
            this.vertex1 = vertex1;
            this.vertex2 = vertex2;
        }   
    }

    //adds a vertex to the graph
    public void addVetex(int vertex, int x, int y)
    {
        vertices.put(vertex, new Position(x,y));
    }

    //connects two vertices in the graph
    public void addEdge(int vertex1, int vertex2, int weight)
    {
        if(!vertices.containsKey(vertex1) || !vertices.containsKey(vertex2))
        {   
            throw new NoSuchElementException();
        }

        edges.add(new Edge(vertex1, vertex2, weight));
    }


    //returns a list of the neighbours of a vertex
    public List<Integer> neighbours(int vertex)
    {
        if(!edges.contains(vertex))
        {
            throw new NoSuchElementException();
        }

        List<Integer> neighbours = new ArrayList<Integer>();

        for(Integer curVertex : vertices.keySet())
        {   
            if(edgeExists(vertex, curVertex))
            {
                neighbours.add(curVertex);
            }
        }

        return neighbours;
    }

    public boolean edgeExists(int vertex1, int vertex2)
    {


    }

    public int weight(int vertex1, int vertex2)
    {
        if(!edgeExists(vertex1, vertex2))
        {
            throw new NoSuchElementException();
        }

        return 
    }

    // returns the position of a vertex that exists
    public Position position(int vertex)
    {
        if(!vertices.containsKey(vertex))
        {
            throw new NoSuchElementException();
        }

        return vertices.get(vertex);
    }

    //creates a graph with n vertices where the probability of an edge is p
    public void randomGraph(int n, double p)
    {


    }

    private class Position
    {
        private int x;
        private int y;

        public Position(int x, int y)
        {
            this.x=x;
            this.y=y;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我会把这个作为答案,因为作为评论写的时间太长了,但是很冒烟,停止嵌套课程!

要小心,你不需要这么多名字,比如PositionVertex由位置定义。这是一个模型,在多个文件中使用多个CLASS ......

Vertex.java

public class Vertex
{
    private int x;
    private int y;

   @Override
   public boolean equals(Object o) {
   // self check
   if (this == o)
       return true;
   // null check
   if (o == null)
      return false;
   // type check and cast
   if (getClass() != o.getClass())
      return false;
   Vertex v = (Vertex) o;
   // field comparison
   return x == v.x && y == v.y;

}

Edge.java

public class Edge
{
    private int weight;
    private Vertex v1;
    private Vertex v2;

   @Override
   public boolean equals(Object o) {
   // self check
   if (this == o)
       return true;
   // null check
   if (o == null)
      return false;
   // type check and cast
   if (getClass() != o.getClass())
      return false;
   Edge e = (Edge) o;
   // field comparison
   return v1.equals(e.v1) && v2.equals(e.v2);
}

Graph.java

public class Graph
{
    private HashMap<Integer,Position> vertices;
    private ArrayList<Edge> edges;
}

然后在比较检查中执行:

Edge test = new Edge(vertex1, vertex2);
foreach edge in edges:
   # compare edge.equals(test)

抱歉,我正在学习Python