查找图表中遍历的最小权重和边数

时间:2017-04-27 13:12:16

标签: java algorithm graph-theory dijkstra

我必须找到两个顶点之间的最小距离而不是。遍历的边缘在寻找加权图中的最小距离。我确实写了一个关于使用Dijkstras算法的代码。但我的测试用例失败了。我在这里缺少什么?

理想输入:给定任意两个顶点A,B 输出:两个节点之间的最小距离,没有。遍历的边缘

如果节点无法访问,则输出应为-1,-1。

//This is a java program to find the shortest path between source vertex and destination vertex
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

public class Dijkstras_Shortest_Path
{
    private int          distances[];
    private int         Numnodes[];
    private int         numnodes;
    private Set<Integer> settled;
    private Set<Integer> unsettled;
    private int          number_of_nodes;
    private int          adjacencyMatrix[][];

   public Dijkstras_Shortest_Path(int number_of_nodes)
    {
        this.number_of_nodes = number_of_nodes;
        distances = new int[number_of_nodes + 1];
        Numnodes=new int[number_of_nodes+1];
        settled = new HashSet<Integer>();
        unsettled = new HashSet<Integer>();
        adjacencyMatrix = new int[number_of_nodes + 1][number_of_nodes + 1];
    }

public void dijkstra_algorithm(int adjacency_matrix[][], int source)
{
    int evaluationNode;
    for (int i = 1; i <= number_of_nodes; i++)
        for (int j = 1; j <= number_of_nodes; j++)
            adjacencyMatrix[i][j] = adjacency_matrix[i][j];

    for (int i = 1; i <= number_of_nodes; i++)
    {
        distances[i] = Integer.MAX_VALUE;

    }

    unsettled.add(source);
    distances[source] = 0;
    while (!unsettled.isEmpty())
    {
        evaluationNode = getNodeWithMinimumDistanceFromUnsettled();
        unsettled.remove(evaluationNode);
        settled.add(evaluationNode);
        evaluateNeighbours(evaluationNode);

    }
}

private int getNodeWithMinimumDistanceFromUnsettled()
{
    int min;
    int node = 0;

    Iterator<Integer> iterator = unsettled.iterator();
    node = iterator.next();
    min = distances[node];
    for (int i = 1; i <= distances.length; i++)
    {
        if (unsettled.contains(i))
        {
            if (distances[i] <= min)
            {
                min = distances[i];
                node = i;
            }
        }
    }
    return node;
}

private void evaluateNeighbours(int evaluationNode)
{
    int edgeDistance = -1;
    int newDistance = -1;
    int newNodes=1;

    for (int destinationNode = 1; destinationNode <= number_of_nodes; 
  destinationNode++)
    {
        if (!settled.contains(destinationNode))
        {
            if (adjacencyMatrix[evaluationNode][destinationNode] != 
 Integer.MAX_VALUE)
            {
                edgeDistance = adjacencyMatrix[evaluationNode]
 [destinationNode];
                newDistance = distances[evaluationNode] + edgeDistance;
                newNodes=Numnodes[evaluationNode]+1;
                if (newDistance < distances[destinationNode])
                {
                    distances[destinationNode] = newDistance;
                    Numnodes[destinationNode]=newNodes;
                }
                unsettled.add(destinationNode);
            }
        }
    }
}

public static void main(String... arg)
{
    int adjacency_matrix[][];
    int number_of_vertices;
    int number_of_edges;
    int source = 0, destination = 0;
    Scanner scan = new Scanner(System.in);
    try
    {
        number_of_vertices = scan.nextInt();
        number_of_edges=scan.nextInt();

        if (number_of_vertices<1||number_of_vertices>10000)
        {
            System.out.println("Number of vertices out of boundary");
        }
        if (number_of_edges<1||number_of_edges>10000)
        {
            System.out.println("Number of edges out of boundary");
        }

        adjacency_matrix = new int[number_of_vertices + 1]
  [number_of_vertices + 1];


        for (int i = 1; i <= number_of_vertices; i++)
        {
            for (int j = 1; j <= number_of_vertices; j++)
            {
                adjacency_matrix[i][j] = 0;
            }
        }

        for(int i=1;i<=number_of_edges;i++)
        {
        int node1=scan.nextInt();
        int node2=scan.nextInt();
        adjacency_matrix[node1][node2]=scan.nextInt();
        }
        for (int i = 1; i <= number_of_vertices; i++)
        {
            for (int j = 1; j <= number_of_vertices; j++)
            {
                if (adjacency_matrix[i][j] == 0)
                {
                    adjacency_matrix[i][j] = Integer.MAX_VALUE;
                }
            }
        }


        source = scan.nextInt();         
        destination = scan.nextInt();

        Dijkstras_Shortest_Path dijkstrasAlgorithm = new 
   Dijkstras_Shortest_Path(
                number_of_vertices);
        dijkstrasAlgorithm.dijkstra_algorithm(adjacency_matrix, source);

        for (int i = 1; i <= dijkstrasAlgorithm.distances.length - 1; i++)
        {
            if (i == destination)
                System.out.println(dijkstrasAlgorithm.distances[i] +" "+ 
   dijkstrasAlgorithm.Numnodes[i]);
        }
    } catch (InputMismatchException inputMismatch)
    {
        System.out.println("Wrong Input Format");
    }
    scan.close();
 }
}

0 个答案:

没有答案