F
F
Fedor unknown2019-04-21 19:58:31
Java
Fedor unknown, 2019-04-21 19:58:31

How to find Finding the shortest distance on a graph with path recovery?

Hello, help me understand. The input to the program is a graph in the form of an adjacency matrix. It is necessary to find the shortest distance from one point to another, and reconstruct this path as a list of vertices through which it passes. I use the Floyd-Warshall algorithm, the algorithm itself works, I checked it manually for each pair of points, since there are not many of them. The problem arises when reconstructing the path, the algorithm of which, to be honest, I do not fully understand, but still tried to implement, because time is running out, the coursework must be handed over. Reconstructed the path according to the principle indicated on the site hci.fenster.name/304y/lab5(method 1). And in 7 cases out of 25 I get wrong results. Please help me correct the errors and explain how to restore the path correctly. I have climbed a lot of sites, but somehow it doesn’t reach me how to do it. The listing is given below, it is rather ridiculous, because this is a draft version of the program and was written in order to understand all these algorithms and then be able to build it all into a more complex course project. There are quite a few extra checks here that you shouldn't pay attention to, because I'm still a fool. The main thing is to figure out how to write the path as a list of vertices Thanks in advance
CODE:

package tratata;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
/**
 *
 * @author Антон
 */
public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<ArrayList<Integer>> qwa=new ArrayList<ArrayList<Integer>>(); // массив кратчайших путей
        ArrayList<Integer> qwo;
        ArrayList<ArrayList<Integer>> lal=new ArrayList<ArrayList<Integer>>();  // массив весов
        ArrayList<Integer> lol;
        ArrayList<ArrayList<Integer>> tops=new ArrayList<ArrayList<Integer>>(); // массив предков. 
        ArrayList<Integer> top;
        ArrayList<Integer> route=new ArrayList<Integer>(); // список вершин в пути
        Scanner in=new Scanner(new File("lol.txt"));
        Scanner inn=new Scanner(new File("lol.txt"));
        for (int i=0;i<5;i++)    // заполнение lal и tops 
        {
            lol=new ArrayList<Integer>();
            top=new ArrayList<Integer>();
            for (int j=0;j<5;j++)
            {
                top.add(i+1);
                lol.add(in.nextInt());
                System.out.print(lol.get(j));
            }
            System.out.println();
           // qwa.add(lol);
            lal.add(lol);
            tops.add(top);
        }
        System.out.println("вывел считанный lal"); // контролирую ввод, т.к. были с этим небольшие проблемы 
        for (int i=0;i<lal.size();i++)
            System.out.println(lal.get(i));
        System.out.println("Вывел tops");   // массив tops заполняется по алгоритму, указанному на сайте           
        for (int i=0;i<tops.size();i++)          // [url]http://hci.fenster.name/304y/lab5/[/url] 
            System.out.println(tops.get(i));     // реконструирование пути, способ №1
 
        for (int i=0;i<5;i++)
        {
            qwo=new ArrayList<Integer>();
            for (int j=0;j<5;j++)
            {
                qwo.add(inn.nextInt());
                System.out.print(qwo.get(j));
            }
            System.out.println();
           // qwa.add(lol);
            qwa.add(qwo);
        }
        System.out.println("вывел считанный qwa");
        for (int i=0;i<qwa.size();i++)
            System.out.println(qwa.get(i));
 /*       for (int i=0;i<lal.size();i++)
        {
            System.out.println("lal"+lal.get(i));
            System.out.println("qwa"+qwa.get(i));
        }
*/        System.out.println("вывел преобразованный qwa");  // несуществующим ребрам ставим в соотв-ие
        for (int i=0;i<qwa.size();i++)                               // огромное, в рамках задачи, число
        {
            for (int j=0;j<qwa.get(i).size();j++)
            {
                if (qwa.get(i).get(j)==0)
                    qwa.get(i).set(j,999999);
            }System.out.println(qwa.get(i));
        }
        System.out.println("контроль изменения lal");  // проверка чисто для себя, т.к. массив изменялся сам по себе
        for (int i=0;i<lal.size();i++)
            System.out.println(lal.get(i));
        System.out.println("преобразование матрицы кратч путей");
        for (int k=0;k<qwa.size();k++)
        {
            for (int i=0;i<qwa.size();i++)
            {
                for (int j=0;j<qwa.get(i).size();j++)
                {
                    if (qwa.get(i).get(j)>(qwa.get(i).get(k)+qwa.get(k).get(j)))
                    {
                        qwa.get(i).set(j, qwa.get(i).get(k)+qwa.get(k).get(j)); // изменение матр кратч путей
                        tops.get(i).set(j,tops.get(k).get(j));        // заполнение матрицы предков(предыдущ вершин)
                        route.add(tops.get(4).get(4));         // реконструирование пути
                    }
                }
            }
        }
        System.out.println("матрица кратч путей:");
 
        for (int i=0;i<qwa.size();i++)
            System.out.println(qwa.get(i));
        System.out.println("матрица предков");
        for (int i=0;i<tops.size();i++)
            System.out.println(tops.get(i));
        System.out.println("путь:");
        for (int i=0;i<route.size();i++)
            System.out.print(route.get(i));
}
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question