简体   繁体   中英

How to get the best path in a weighted digraph (graph) using “adjacent hash”?

I'm trying to get the best path from a weighted graph where in my case are considered a set of three different weights, which are cost, slowness and risk. One of the goals is to represent a map where each edge is a road and each vertex a coordinate point. I'm using Map <Vertex, HashSet<ConnectionEdge> map; to represent my graph. The ConnectionEdge class represents a package with the target vertex and its respective weight of this connection (origin -> (destination, weight)). To find the best path is considered the sum of the average of one vertex to the other until the destination is found, that is, from one vertex to the other we add the weights (cost, slowness, risk) and divide by three, we take this result and we add to the next one in succession.

I can make the connections, but I'm having trouble getting the best way. The main purpose is to print the best path as A -> B -> D -> E according to its total weight value (the sum of the averages).

Below the class code:

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
}

It would help a lot of direction or solution.

You should look at Dijkstra's algorithm, which will find the shortest (least expensive) path efficiently. https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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