繁体   English   中英

图类中的加权边表示

[英]Weighted edges representation in a graph class

我目前正在实现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;
        }
    }
}

我将其作为答案,因为写评论的时间太长了,但是圣洁的烟熏,不要再嵌套了!

请注意,您不需要太多名称,例如Position Vertex由位置定义。 这是一个模型,在多个文件中使用多个类...

顶点

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM