數據結構-圖

java實現圖的數據結構

源代碼

圖的頂點,Vertex

public class Vertex<E> {
    //到該頂點的邊列表
    List<Edge> edgeList = new ArrayList<>();
    E value;

    public Vertex(E value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return value.toString();
    }
}

圖的邊,Edge

public class Edge<E> {
    Vertex<E> startV;//起點
    Vertex<E> endV;//終點

    public Edge(Vertex<E> startV, Vertex<E> endV) {
        this(startV, endV, 1);
    }

    public Edge(Vertex<E> startV, Vertex<E> endV, int weight) {
        this.startV = startV;
        this.endV = endV;
        this.weight = weight;
    }

    /**
     * 權重
     */
    int weight;
}

圖的建立圖,添加邊的操作,GraphMatrix


public class GraphMatrix<E> {
    GraphType type = GraphType.UNDIRECTED_GRAPH;//圖的類型

    enum GraphType {UNDIRECTED_GRAPH, DIRECTED_GRAPH}

    //頂點表
    List<Vertex<E>> vertexList = new ArrayList<>();
    //邊表
    List<Edge<E>> edgeList = new ArrayList<>();
    int[][] edges;

    public GraphMatrix(ArrayList<E> vertexList) {
        createVertexList(vertexList);
    }

    public GraphMatrix(E[] vertexList) {
        createVertexList(vertexList);
    }

    public GraphMatrix(String vertexList) {
        createVertexList(vertexList);
    }

    public int getVertexNum() {
        return vertexList.size();
    }

    public void addEdge(E startValue, E endValue, int weight) {
        Vertex<E> startV = searchVertexByValue(startValue);
        Vertex<E> endV = searchVertexByValue(endValue);
        if (startV != null && endV != null) {
            Edge<E> edge = new Edge<>(startV, endV, weight);
            edgeList.add(edge);
            edges[vertexList.indexOf(startV)][vertexList.indexOf(endV)] = edge.weight;
            if (type == GraphType.UNDIRECTED_GRAPH) {
                edgeList.add(new Edge<>(endV, startV));
                edges[vertexList.indexOf(endV)][vertexList.indexOf(startV)] = edge.weight;
            }
        } else {
            System.out.println("沒有找到頂點值");
        }
    }

    public void addEdge(E startValue, E endValue) {
        addEdge(startValue, endValue, 1);
    }

    public void information() {
        System.out.println("頂點信息:");
        for (Vertex<E> vertex : vertexList) {
            System.out.print(vertex.value + " ");
        }
        System.out.println("\n邊表:");
        for (Edge<E> edge : edgeList) {
            if (type == GraphType.UNDIRECTED_GRAPH) {
                System.out.print(edge.startV.value + "<->" + edge.endV.value + " ");
            } else {
                System.out.println("{" + edge.startV.value + "->" + edge.endV.value + " 權重:" + edge.weight + "} ");
            }
        }

        System.out.println("\n邊的權重表:");
        showEdges();
    }

    public void showEdges() {
        for (int i = 0; i < getVertexNum(); i++) {
            for (int j = 0; j < getVertexNum(); j++) {
                System.out.print(edges[i][j] + " ");
            }
            System.out.println();
        }
    }

    public int getVertexIndex(E value) {
        Vertex<E> vertex = searchVertexByValue(value);
        if (vertex != null) {
            return vertexList.indexOf(vertex);
        }
        return -1;
    }

    private Vertex<E> searchVertexByValue(E value) {
        for (Vertex<E> vertex : vertexList) {
            if (vertex.value.equals(value)) {
                return vertex;
            }
        }
        return null;
    }

    private void createVertexList(ArrayList<E> vertexList) {
        for (E value : vertexList) {
            this.vertexList.add(new Vertex<>(value));
        }
        edges = new int[getVertexNum()][getVertexNum()];
    }

    private void createVertexList(E[] vertexList) {
        for (E value : vertexList) {
            this.vertexList.add(new Vertex<>(value));
        }
        edges = new int[getVertexNum()][getVertexNum()];
    }

    private void createVertexList(String vertexList) {
        for (char value : vertexList.toCharArray()) {
            this.vertexList.add((Vertex<E>) new Vertex<>(String.valueOf(value)));
        }
        edges = new int[getVertexNum()][getVertexNum()];
    }
}

調用示例

public static void main(String[] args) {
        GraphMatrix<String> graph = new GraphMatrix<>("ABCD");
        graph.type = GraphMatrix.GraphType.DIRECTED_GRAPH;
        graph.addEdge("A", "B", 1);
        graph.addEdge("A", "C", 2);
        graph.addEdge("A", "D", 3);
        graph.addEdge("B", "A", 4);
        graph.addEdge("C", "B", 5);
        graph.addEdge("C", "D", 1);
        graph.addEdge("D", "B", 1);
        graph.addEdge("D", "C", 2);
        graph.information();
    }

log

頂點信息:
A B C D 
邊表:
{A->B 權重:1} 
{A->C 權重:2} 
{A->D 權重:3} 
{B->A 權重:4} 
{C->B 權重:5} 
{C->D 權重:1} 
{D->B 權重:1} 
{D->C 權重:2} 

邊的權重表:
0 1 2 3 
4 0 0 0 
0 5 0 1 
0 1 2 0 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章