Now, to solve this problem we need to see the definition of what a multigraph is, so we see it from the perspective of graph theory and we have a graph with two important properties -
1) There will be multiple edges between a given set of nodes, for example, in a city, there are multiple times a scenario where you have over and under the bridge scenarios. We represent that as e1(a,b),e2(a,b).
2) Now there can be two meanings of multiple edges it's either two separate edges which means they have unique properties or they are unique by the means that the same is passed on twice in a path and is considered as two separate edges.
Now there are multiple attempts to solve this problem but the one that will work is, using Dynamic Programming.
The Basic Idea - You have a starting point let's say A and you have a graph and have to reach till 'G' to do that properly, we'll have let's say A connected to 3 nodes, B, C, D and then they are connected to E and F or both, and then that is connected to G directly or H and then G.
If you can see there are multiple, paths possible by choosing or taking one or the other decision. Dynamic Programming keep things in an updated manner as soon as things start to appear in the context ot tracing the graph, details of each step is explained below -
class multigraph {
static int count = 7; // we can choose how many nodes are available
static int max = 999999; // just a number extremely huge working as infinity
public static int closestdest(int[][] plot) // the matrix is given carrying the value of edge weight
{
int distance[] = new int[count];
dist[count - 1] = 0;
// Now here we have to remember even when we are trying to figure out the value of A to G we'll start from G and see if the nodes just a level (its immediate neighbor is one with only one edge) ahead at N-2, will be evaluated and see which is the best option if we have all the option and update the distance array.
for (int i = count - 2; i >= 0; i--)
{ distance[i] = 999999; / / initally all points which aren't connected are at a infinite distance
for (int j = i; j < count; j++)
{ if (plot[i][j] == INF) { continue;} // happens is distance is 999999 which means no edge
distance[i] = Math.min(distance[i], plot[i][j] + distance[j]); } }
return distance[0];
}
// The distance formula here is a pretty simple one, it's like if we have a direct connection and alternative, take the one minimum of those two. See the reason it works is, for level N-1 we have a direct edge, so' one of the connecting directly is chosen, next level has only to measure the minimum of N-2 edge plus distance of where it lands on N-1 and that is why we have the minimum of two option in the second last line
All we have to do now, to run this program is to give the problem a graph and it will work, just by typing
closestdest(graph_name);
Any doubts ask in comments
How to print the shortest path in multigraph java code ?with example
Shortest Path (DAG) Students, In this coding assignment, you will be asked to write Java code to read a text file (input.txt) of a graph represented as an adjacency list. Example: A, 7, B, 9, C B, 1, C C, 8, E, 2, F D, 1, E, 3, G E, 3, G F, 3, D, -3, H H, 6, G K, 3, H, -1, F The graph represented by the adjacency list above looks like this: The graph is a...
I need a java code to choose the shortest path through nodes using hill climbing algorithm.
In pseudocode, write the algorithm used by a service like "Uber" (drivers, customers, route) in Java for the SHORTEST PATH problem. Not looking for the code base of UBER - just the shortest path algorithm used (or an idea of it)...
Java c) Shortest Path on DAG Find the shortest cost path from vertex A to all other vertices for the following vertex. Show the intermediate steps and cost at each iteration of the algorithm, and show the final shortest path tree and cost.
What is this graph translated into Java Code Shortest path algorithm Suppose that the league matches are played between the X and Y teams. The team X is containing D, E, and F sub- teams and team Y is containing A, B and C sub-teams. The diagram to show the win of Y team over X team is as given below: X Team Y Team Win-2 Lose =1 Win = 1 Lose = 1 B) As the diagram is showing...
Algortithms Please answer question 7 using algorithm 3.5 7, /Analyze the Print Shortest Path algorithm (Algorithm 3.5) and show that it has a linear-time complexity Algorithm 3.5 Print Shortest Path Problem: Print the intermediate vertices on a shortest path from one vertex to another vertex in a weighted graph. Inputs: the array P produced by Algorithm 3.4, and two indices, q and r, of vertices in the graph that is the input to Algorithm 3.4. highest index of an intermediate...
Java b) Bellman-Ford distance valuesaer each iteration of the algorithm, and show the final shortest path tree and cost. 3. .2 b) Bellman-Ford distance valuesaer each iteration of the algorithm, and show the final shortest path tree and cost. 3. .2
Use Dijkstra’s Shortest Path Algorithm Example 2- A is the initial vertex: 9 6 2
Using Java Code. Thankyou. The file has 1 edge per line in the format (origin,destination) - Assume the graph is not directional - (2,5) means there is a path from 2 to 5 and from 5-2 For the Lab write the following code. 1) Write a function to find all adjacent vertices to a node N ( find_adjacent(n)) 2) Write a function that will return true or false that an edge exists ( edge_exists(origin,destination)) 3) Print out all Vertices using...
47. What is overriding? Describe using java code showing an example of overriding. 48. Does Java allow us to refine behavior in subclasses? If so, show Java code that demonstrates this property. 49. Does Java allow us to replace behavior in subclasses? If so, show Java code that demonstrates this property. 50. Can dynamic binding be used in C++? If so, show how this is accomplished. 51. Is there a cost to providing the dynamic binding capability? If so, describe...