Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: Java implementing of Djikstra algorithm

  1. #1
    Junior Member
    Join Date
    Jun 2020
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java implementing of Djikstra algorithm

    I am trying to implement java for Dijkstra algorithm(Shortest Path Tree). The graph node are being read from a text file which contains string|(vertex) and int (weight) between vertexes. But when running the program it thrown me an error



    > Exception in thread "main" java.util.InputMismatchException



    This is the code of java

     
        import java.io.File;
     
        import java.io.FileNotFoundException;
     
        import java.util.Scanner;
     
     
     
        public class ASD4_dijkstra {
     
     
     
            // A utility function to find the vertex with minimum distance value,
     
            // from the set of vertices not yet included in shortest path tree
     
            static final int V = 5;
     
            int minDistance(int dist[], Boolean sptSet[]) {
     
                // Initialize min value
     
                int min = Integer.MAX_VALUE, min_index = -1;
     
     
     
                for (int v = 0; v < V; v++)
     
                    if (sptSet[v] == false && dist[v] <= min) {
     
                        min = dist[v];
     
                        min_index = v;
     
                    }
     
     
     
                return min_index;
     
            }
     
     
     
            // A utility function to print the constructed distance array
     
            void printSolution(int dist[], int n) {
     
                System.out.println("Distance from starting vertex");
     
                for (int i = 0; i < V; i++)
     
                    System.out.println(i + " \t\t " + dist[i]);
     
            }
     
     
     
            // Funtion that implements Dijkstra's single source shortest path
     
            // algorithm for a graph represented using adjacency matrix
     
            // representation
     
            void dijkstra(int graph[][], int src) {
     
                int dist[] = new int[V]; // The output array. dist[i] will hold
     
                // the shortest distance from src to i
     
     
     
                // sptSet[i] will true if vertex i is included in shortest
     
                // path tree or shortest distance from src to i is finalized
     
                Boolean sptSet[] = new Boolean[V];
     
     
     
                // Initialize all distances as INFINITE and stpSet[] as false
     
                for (int i = 0; i < V; i++) {
     
                    dist[i] = Integer.MAX_VALUE;
     
                    sptSet[i] = false;
     
                }
     
     
     
                // Distance of source vertex from itself is always 0
     
                dist[src] = 0;
     
     
     
                // Find shortest path for all vertices
     
                for (int count = 0; count < V - 1; count++) {
     
                    // Pick the minimum distance vertex from the set of vertices
     
                    // not yet processed. u is always equal to src in first
     
                    // iteration.
     
                    int u = minDistance(dist, sptSet);
     
     
     
                    // Mark the picked vertex as processed
     
                    sptSet[u] = true;
     
     
     
                    // Update dist value of the adjacent vertices of the
     
                    // picked vertex.
     
                    for (int v = 0; v < V; v++)
     
     
     
                        // Update dist[v] only if is not in sptSet, there is an
     
                        // edge from u to v, and total weight of path from src to
     
                        // v through u is smaller than current value of dist[v]
     
                        if (!sptSet[v] && graph[u][v] != 0 &&
     
                            dist[u] != Integer.MAX_VALUE &&
     
                            dist[u] + graph[u][v] < dist[v])
     
                            dist[v] = dist[u] + graph[u][v];
     
                }
     
     
     
                // print the constructed distance array
     
                printSolution(dist, V);
     
            }
     
     
     
            // Driver method
     
            public static void main(String[] args) {
     
                File file = new File("C:\\Users\\leotr\\Downloads\\alg4.txt");
     
                try {
     
     
     
                    Scanner sc = new Scanner(file);
     
                    int graph[][] = new int[5][5];
     
                    for (int i = 0; i < 5; i++) {
     
                        for (int j = 0; j < 5; j++) {
     
                            graph[i][j] = sc.nextInt();
     
                        }
     
                    }
     
                    sc.close();
     
                } catch (FileNotFoundException e) {
     
                    e.printStackTrace();
     
                }
     
            }
     
     
     
        }



    And the text file:

    5

    A,B-6,C-1

    B,A-6,C-3,D-7,E-2

    C,A-1,B-3,D-1

    D,B-7,C-1,E-2

    E,B-2,D-2



    I searched in google , and found the cause of error. It because the textfile contain int and string, and in the code its declared to get only int, but can't figure how to change the code, to make work.
    Last edited by leomi; June 16th, 2020 at 10:57 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,121
    Thanks
    53
    Thanked 2,455 Times in 2,406 Posts

    Default Re: Java implementing of Djikstra algorithm

    it thrown me an error
    > Exception in thread "main" java.util.InputMismatchException
    Please copy the full text that shows where the error happened and paste it here.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2020
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java implementing of Djikstra algorithm

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at ASD4_dijkstra.main(ASD4_dijkstra.java:94)

    Complete error code

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,121
    Thanks
    53
    Thanked 2,455 Times in 2,406 Posts

    Default Re: Java implementing of Djikstra algorithm

    how to change the code, to make work.
    If the code must read the input file, then you need a definition of the contents of that file.
    What values are on each line of the input file?
    For example, what do these values mean?
    A,B-6,C-1
    Why are the values separated by commas?
    What do the letters mean?
    What does the - followed by a digit mean?

    You can not write any code Until you have definitions for what is in the input file.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2020
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java implementing of Djikstra algorithm

    First line of file mean the number of nodes in the graph, and other lines represent the names of node and connection with other nodes , and connection weight
    For example A,B-12,C-5 means A connect with B, and the weight Of this connection is 12, node a connect with node c and the weight is 5
    Last edited by leomi; June 16th, 2020 at 10:37 AM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,121
    Thanks
    53
    Thanked 2,455 Times in 2,406 Posts

    Default Re: Java implementing of Djikstra algorithm

    other lines represent the names of node and connection with other nodes , and connection weight
    Can you break that down into details that answer the questions I asked in my last post:
    what do these values mean?
    A,B-6,C-1
    Why are the values separated by commas?
    What do the letters mean?
    What does the - followed by a digit mean?
    What is the name?
    What is a connection?
    What is the weight?

    How would those values be used to fill in the array?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2020
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java implementing of Djikstra algorithm

    Eath node of the graph has a name like , a,b,c etc
    Connection or vertex between two nodes
    Weight is the number declared on the vertex between two nodes( shortes path tree djikstra algorithm)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,121
    Thanks
    53
    Thanked 2,455 Times in 2,406 Posts

    Default Re: Java implementing of Djikstra algorithm

    We crossed posting. Please read rest of my post and answer the questions that were asked.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2020
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java implementing of Djikstra algorithm

    Here are all the information you required on comments
    first the error
    //
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at ASD4_dijkstra.main(ASD4_dijkstra.java:94)
    //
    Infos about text file
    //
    First line of file mean the number of nodes in the graph, and other lines represent the names of node and connection with other nodes , and connection weight
    For example A,B-12,C-5 means A connect with B, and the weight Of this connection is 12, node a connect with node c and the weight is 5
    Eath node of the graph has a name like , a,b,c etc"
    Connection" mean vertex or relation between two nodes
    "Weight" is the number declared on the vertex between two nodes( shortest path tree djikstra algorithm )

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,121
    Thanks
    53
    Thanked 2,455 Times in 2,406 Posts

    Default Re: Java implementing of Djikstra algorithm

    Ok, Each line defines a node giving its name followed by the nodes it connects to and the weight separated by commas.

    How with that info from each line be used to build the data structure describing the graph?

    --- Update ---

    Also posted here: https://www.dreamincode.net/forums/t...-error-thrown/

    Please read: http://www.javaprogrammingforums.com...s-posting.html
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jun 2020
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java implementing of Djikstra algorithm

    There is no limit of learning, more you learn or search for a topic its better

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,121
    Thanks
    53
    Thanked 2,455 Times in 2,406 Posts

    Default Re: Java implementing of Djikstra algorithm

    Did you read the link?
    Please read: http://www.javaprogrammingforums.com...s-posting.html
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Jun 2020
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java implementing of Djikstra algorithm

    Yeah I read. But as I said more to learn its better, also I am I panic, it's a task that I have seriously to accomplish tonight. Its a must. From this task its depended my further career . I' am on startup, but if I fail into three task, I can't continue further. I am on my begging and I going to learn a lot, but for the start I have to accomplish three task, from which you will be assigned into different field

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,121
    Thanks
    53
    Thanked 2,455 Times in 2,406 Posts

    Default Re: Java implementing of Djikstra algorithm

    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Jun 2020
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java implementing of Djikstra algorithm

    I know that I posted, but broo I really need this help. If I don't accomplish the task tonight im done. I am going to end . I know I am going to learn and become a prof ( I hope), but for startup need the help of you friends, colleagues to start up and finish the task

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,121
    Thanks
    53
    Thanked 2,455 Times in 2,406 Posts

    Default Re: Java implementing of Djikstra algorithm

    Which forum do you want to use? Using all 3 seems a waste of time.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Jun 2020
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java implementing of Djikstra algorithm

    this forum I will use, but I would appreciate some help

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,121
    Thanks
    53
    Thanked 2,455 Times in 2,406 Posts

    Default Re: Java implementing of Djikstra algorithm

    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Jun 2020
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java implementing of Djikstra algorithm

    I am using this forum, the last reply I posted there, was before I replied here. So you can help me here

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,121
    Thanks
    53
    Thanked 2,455 Times in 2,406 Posts

    Default Re: Java implementing of Djikstra algorithm

    See my response here: https://www.dreamincode.net/forums/t...&#entry2420474 for a suggestion.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,121
    Thanks
    53
    Thanked 2,455 Times in 2,406 Posts

    Default Re: Java implementing of Djikstra algorithm

    Did you find someone to write your code for you? You had been active on 3 forums and now there is nothing being posted.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Implementing probability in Java
    By PushBilly in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 7th, 2014, 03:01 PM
  2. Java 2D Game: Implementing Simultaneous Keystrokes
    By AvivC in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 1st, 2014, 02:35 PM
  3. Implementing an algorithm
    By me_newbie in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 16th, 2012, 08:50 PM
  4. Implementing restartApplication method in Java
    By davidvee in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 10th, 2012, 11:02 AM
  5. Replies: 1
    Last Post: September 15th, 2011, 07:50 AM

Tags for this Thread