Overall Solution Strategy
In this assignment, a program to find the shortest path from source to destination node using a Dijkstra algorithm. The program is implemented with the Java programming language. This program gives the best output for the shortest path from the all nodes.
- Compilation
To get the program running, before run the program set the path in Java. Run this program in command prompts. The path set command is set path =”C:Program Files (x86)Javajdk1.8.0_181bin”. Where C:Program Files (x86)Javajdk1.8.0_181bin is a Java path name. Then compile the program command is javac Dijkstrasalgorithm.java. Where Dijkstrasalgorithm is a source file name.
Once run the program the Java class created automatically in the program folder the class file name is Dijkstrasalgorithm.class. Then run the program command is java Dijkstrasalgorithm. Before the compilation must install the java on the system. The java does not install in the system this program does not run in the system. The jdk is the java development kit. The jdk contains the java virtual machine and a few resources for complete the java application.
- Program Output
The program output shows the source, destination, cost, and their path. The vertex is used to make a graph for finding the shortest path. The matrix contains the number of rows and columns. A vertex is a unit of a graph. The distance has referred the cost between the vertices. The shortest graph distance between each pair of vertices in the given graph. The output has the source is 0 and destination is except 0 nodes such as 1, 2, 3, and 4. The cost is the weight of between the vertices. For example 0 -> 1 meaning is the source is 0 and the destination is 1 their cost is 3. The path refers the 0 to 1.
Overall Solution Strategy
The program is implemented to finding the shortest path between the nodes while using the Dijkstra algorithm. This program is used to undirected graph are associated together. The edges are bidirectional and are also called the undirected network. The graph contains the nodes and edges. The Dijkstra algorithm sort the vertex based on the distance of the vertex from the source.
Workflow
The workflow of the program first the number of vertices is given. Next the weighted matrix for the undirected graph. The graph matrix weight is based on the number of the vertex. For example, the vertex number is 5 the matrix weight has five rows and five columns. The source node is the starting point and the destination node is the ending point of the node of finding the shortest path in the graph while using a Dijkstra algorithm. Then the answer is displayed in the command prompt. The output is displayed in the vertex, distance, and path.
Workflow
Vertex
The vertex is used to make a graph for finding the shortest path. The matrix contains the number of rows and columns. A vertex is a unit of a graph. The graph contains the set of vertex and set of edge with unordered. It is called the undirected graph. The graph connects with every vertex in the graph. The connected vertices are called the path. The single vertex connects with itself is called the trivial path. The graph has both undirected and directed. Both graph same argument is applied.
Source
The source is the starting point of the node. The starting point is used to find the path. For example the source is 0 the path is started from the 0.
Destination
The destination is the ending point of the node. The source and destination are used for finding the shortest path between all nodes. For example, the ending point is 7 and the starting point is 0 the path is considered 0 to 7. The shortest path calculates from 0 to 7.
Path
The shortest graph is the distance between each pair of vertices in the given graph. It is also used to find the Shortest Path.
Dijkstra Algorithm
The Dijkstra algorithm is used to find Shortest Path among everything nodes in the graph. In many common variants is considered the Single node as the source. To find the shortest path from the source node to another node. This Algorithm assigns the starting value of the distance such as,
- The Mark everything node is unvisited. Create the unvisited set is the set of all unvisited nodes.
- Mark all nodes is the distance value of tentative. The tentative distance value is set the 0 values for the starting node. The starting node is the current node.
- The current node considers the unvisited neighbor node. Calculate the tentative distance with the present node.
- Compare the new tentative distance value and the present value and allocate the smallest value. For example the present node is X. The X distance is 3 and edge is connected to neighboring Y. The Y length is 4. The distance from X to Y is 3 + 4 = 7. The Y is marked with the distance is greater than the 7. So Y is changed to 7. Otherwise, it has the present it has value.
- Mark the present node is visited. So, it is removed from the unvisited node. The visited node does not check again.
- The destination node mark as visited or there is no connection between the starting node to other unvisited node, then stop this algorithm. This algorithm is completed.
- Then to select the unvisited nodes and mark the smallest uncertain distance set is the new present node. Then again go to the third step.
The node of the minimum distance is used to find the minimum distance of the nodes. This coding using the vertexindex. The vertexindex is used to count the minimum distance in the graph. The Dijkstra algorithm is similar to the minimum spanning tree. There are two sets maintain in this algorithm such as vertex contain the Shortest Path tree and vertex does not contain the shortest path tree. The each step to find the vertex in the algorithm and it has a minimum distance from the source.
Evaluate neighbor
The evaluate neighbor is used to find the edge distance and new distance. The edge distance is used to find the nearest node in the graph. The graph is an undirected graph so it has the bi-directional. So the distance from edge calculate is difficult. Each and every time update the new distance in the graph. In a graph connect with the vertex. The neighbor node is used to represent the graph in the algorithm. It is also used to group the vertex in the graph. The graph class defines the neighbor properties. The vertex degree is the same as the number of vertexes. The loop is connected vertex itself. It occurs the edge, the vertex fits its individual neighbor value.
- Path
Vertex
The shortest graph distance between each pair of vertices in the given graph. It is used for finding shortest path. The path is print the present vertex and their parents also.
- Matrix
The matrix has the five rows and five columns. The data are entered in this program directly. The graph is based on the matrix. The matrix is a square box. It is used to represent the graph. The matrix element represents the pair of vertices not graph. This program used the undirected graph. The undirected graph is each and every edge add the one to the matrix cell and each loop adds the two in the matrix. It allows the vertex degree. The degree of the vertex is used to find the sum of the value in the row or column in the matrix.
The above diagram shows the undirected graph and their matrix format. The matrix contains the 5 rows and 5 columns. The undirected graph contains the 5 vertexes such as A, B, C, D and E. The A is connected to the B, C, E, and D. The B is connected to the A, E and D. The C is connected to the A and D. The D is connected to the A, B, C, E and D it. The matrix the node is connected to other nodes its value is 0 otherwise its value is 1. For example, the A is connected to the C, B, and D. The matrix format the A value is 1 in the B, C, and D. The A and E value I 0 in the A row. The same step follows in B, C, D, and E.
- Solution
The solution has the source and destination, cost and path. The source is stored in the startvertex variable. The destination is stored in the vertexindex variable. The cost is stored in the distances[vertexindex] array. The path is stored in the current vertex variable.
3. Reflection and Challenges
It has specified the output in a single source file. The many classes include in this program. I was interested in creating this program for finding the shortest path using the Dijkstra algorithm. I also interested to learn this Dijkstra algorithm. The undirected graph finds the shortest path is very challenging for me in java language. First, I learned the Dijkstra algorithm. The Dijkstra algorithm is used both directed and undirected graphs. Now we have taken only the undirected graph. Then draw the graph for developing the java program to find the Shortest Path between the nodes.
The graph contains the vertex, edge, and path. First, fix the current node is unvisited node, then find the nearest node to set the visited node. Find the Shortest Path between the vertices. The starting vertex doesn’t have the parent node. The parent array stores the shortest path tree. Then find the destination is the visited node the algorithm is stopped otherwise it is continued. The vertex value finding is very difficult for me in the program. I print the solution has the vertex, distance, and path.
The floating point value shortest path finds is difficult for me so I choose the integer value for finding the shortest path between the vertices. I got much error in developing this program. The important errors I mentioned this picture.
Conclusion
The program is to find the shortest path of the Dijkstra algorithm is verified and executed successfully. The Java program is implemented successfully in this project. The output will be attached above.