如何使用“相邻哈希”在加权有向图(图)中获得最佳路径?

时间:2019-05-27 20:21:39

标签: java data-structures graph

我正在尝试从权重图获得最佳路径,在本例中,权重图被视为一组三个不同的权重,分别是成本,缓慢性和风险。目标之一是代表一张地图,其中每个边是一条道路,每个顶点是一个坐标点。我正在使用Map <Vertex, HashSet<ConnectionEdge> map;来表示我的图表。 ConnectionEdge类表示具有目标顶点及其连接权重的包(起点->(目的地,权重))。找到最佳路径被认为是一个顶点到另一个顶点的平均值之和,直到找到目的地为止,也就是说,从一个顶点到另一个顶点,我们将权重(成本,缓慢性,风险)相加并除以三,得到这个结果,我们就连续添加到下一个。

我可以进行连接,但是我无法找到最佳方法。主要目的是根据总重量值(平均值之和)将最佳路径打印为A -> B -> D -> E

在课程代码下面:

Graph.java

import java.util.*;

public class Graph<T> {
    private Map<Vertex, HashSet<ConnectionEdge>> map;

    public Graph() {
        this.map = new HashMap<Vertex, HashSet<ConnectionEdge>>();
    }

    public boolean add(Vertex<T> vextex) {
        if (!this.map.containsKey(vextex)) {
            this.map.put(vextex, new HashSet<ConnectionEdge>());
            return true;
        }
        return false;
    }


    public boolean put(Vertex<T> origin, Vertex<T> destination, int cost, int slowness, int risk) {
        if (this.map.containsKey(origin) && this.map.containsKey(destination)) {
            if (!this.map.get(origin).contains(destination)) {
                List<Integer> weights = Arrays.asList(cost, slowness, risk);
                this.map.get(origin).add(new ConnectionEdge(destination, weights));
                return true;
            }
        }
        return false;
    }
}

Vertex.java

public class Vertex<T> {
    private String label;
    private T value;

    public Vertex(String label) {
        this.label = label;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }
}

ConnectionEdge.java

import java.util.List;

public class ConnectionEdge<T> {
    private Vertex<T> destination;
    private List<Weight> weights;

    public ConnectionEdge(Vertex<T> destination, List<Weight> pesos) {
        this.destination = destination;
        this.weights = pesos;
    }

    public Vertex<T> getDestination() {
        return destination;
    }

    public void setDestination(Vertex<T> destination) {
        this.destination = destination;
    }

    public List<Weight> getWeights() {
        return weights;
    }

    public void setWeights(List<Weight> weights) {
        this.weights = weights;
    }
}

Weight.java

import java.util.HashMap;
import java.util.Map;

public class Weight {
    private Map<WeightType, Integer> weights = new HashMap<WeightType, Integer>();

    public Weight(WeightType weightType, Integer value) {
        this.weights.put(weightType, value);
    }

    public Map<WeightType, Integer> getWeights() {
        return weights;
    }

    public void setWeights(Map<WeightType, Integer> weights) {
        this.weights = weights;
    }
}

WeightType.java

public enum WeightType {
    COST,
    SLOWNESS,
    RISK
}

这将为您提供很多指导或解决方案。

1 个答案:

答案 0 :(得分:0)

您应该看一下Dijkstra的算法,该算法将有效地找到最短(最便宜)的路径。 https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm