Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

whrit program in java to implement the shortest- path Dijkstra\'s algorithm if w

ID: 3735041 • Letter: W

Question

whrit program in java to implement the shortest- path Dijkstra's algorithm if we have:

Graph.java

import java.util.ArrayList;

import java.util.Comparator;

import java.util.HashMap;

import java.util.LinkedList;

import java.util.List;

import java.util.Map;

import java.util.PriorityQueue;

import java.util.Scanner;

import java.io.*;

public class Graph

{

private static final double INF = Double.MAX_VALUE;

private ArrayList table = new ArrayList();

int numV = 0;

private Map vertexMap = new HashMap();

public Graph()

{

table = new ArrayList();

numV = 0;

vertexMap = new HashMap();

}

public void addEdge(String srcName, String destName, double cost)

{

Vertex v = getVertex(srcName);

Vertex w = getVertex(destName);

v.adj.add(new Edge(w, cost));

}

public void addUndirectedEdge(String srcName, String destName, double cost)

{

Vertex v = getVertex(srcName);

Vertex w = getVertex(destName);

v.adj.add(new Edge(w, cost));

w.adj.add(new Edge(v, cost));

}

private Vertex getVertex(String name)

{

Vertex v;

Integer i = vertexMap.get(name);

if (i == null) { // first time we've seen this vertex name

v = new Vertex(name);

table.add(v);

vertexMap.put(name, numV);

numV++;

}

else

v = table.get(i);

return v;

}

public void printGraph()

{

for(Vertex v : table) {

System.out.print(v.name + ":");

for(Edge e : v.adj) {

System.out.print(" " + e.dest.name + "(" + e.cost + ")");

}

System.out.println();

}

}

public void dijkstra(String sourceName)

{

}

public double getDist(String vname)

{

return 0.0;

}

public ArrayList getPath(String vname)

{

return new ArrayList();

}

public void readGraph(String fname)

//

// read in a graph, where file fname has following format:

//

// first line: single integer n = number of edges

// each of next n lines: two Strings v1 v2 and double c indicating

// an edge from v1 to v2 with cost c

//

{

Scanner fin = null;

try {

fin = new Scanner(new FileInputStream(fname));

} catch (IOException e) {

System.out.println(e);

System.exit(-1);

}

int nedges = fin.nextInt();

for(int i=1; i<=nedges; i++) {

String v1 = fin.next();

String v2 = fin.next();;

double cost = fin.nextDouble();

addEdge(v1, v2, cost);

}

}

public static void main(String [] args)

//

// example driver program

//

{

Graph g = new Graph();

g.readGraph("example.graph");

g.printGraph();

g.bellmanFord("v0");

ArrayList path = g.getPath("v3");

System.out.print("Path to v3:");

for(Vertex v : path) {

System.out.print(" " + v.name);

}

System.out.println(" Dist to v3: " + g.getDist("v3"));

}

}

Vertex.java

import java.util.*;

class Vertex

{

public String name;

public List adj;

public Vertex(String n)

{

name = n;

adj = new LinkedList();

}

}

Edge.java

public class Edge

{

public Vertex dest;

public double cost;

public Edge(Vertex w, double c)

{

dest = w;

cost = c;

}

}

a sample input file example.graph:

12
v0 v1 4
v0 v3 0
v0 v5 3
v0 v2 0
v2 v1 1
v3 v5 5
v4 v2 -5
v4 v3 -1
v5 v1 -1
v5 v3 -2
v5 v4 -3
v1 v3 1

Explanation / Answer

impor java.util.HashSet ;

import java.util.LinkdList;

import java.Util.Queue;

import java.Util.Scanner;

import java.Util.Set;

Public class Dijkstar

{

Private int distances[];

Private Queue <integer> queue;

Private Set <integer> setid;

Private int Number_of_nodes;

Private int adjacencymatrix [ ] [ ];

Public Dijkstra (int Number _of _nodes)

{

this.Number _of _nodes = Number _of _nodes;

disances = new int [Number _of _nodes +1];

settld = new HashSet<integer> ();

Queue = new LinkedList<integer>();

ajecencyMatrix = 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 _nodess ; 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++)

{

distance[ i ] = Integer.Max_VALUE;

}

queue.add (source);

distances [ source] = 0;

while (! queue. is Empty())

{

evaluationNode = getNodeWithMinimumDistanceFromQueue();

settled.add (evaluationNode);

evaluateNeighbours (evaluationNode);

}

}

private int getNodeWithMinimumDistanceFromQueue()

{

int min;

int node = 0;

Iterator <Integer> iterator = queue.iterator ();

node = iterator . next();

min = distances [node];

for (int i =1; i <= distances.length ; i++)

{

if (queue . contains( i ) )

{

if (distances [i] <= min )

{

min = Distances[ i ];

node = i ;

}

}

}

queue.remove(node);

retuen node ;

}

Private void evluateNeighbours (int evaluationNode)

{

int edgDistance = -1;

int newDistance = -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;

if ( newDistance <distances [ destinationNode ] )

{

distances [distinationNode ] = newDistance;

}

queue . add(destinationNode);

}

}

}

}

public satic void main(String [] args)

{

int adjacency_matrix [ ] [ ];

int number_of_vertices ;

int source = 0 ;

Scanner scan = new Scanner (System.in)

try

{

system.out.println (" enter the number of vertices");

Number_of_vertices = scan.next .Int();

adjacency_ matrix = new int [ Number_ of_vertices + 1] [ Number_ of_vertices + 1]

system.out.println (" enter the weighted matrx for the graph");

for (int i = 1 ; i <= Number_of_vertices ; i++)

{

for (int j = 1 ; j <= Number_of_vertices ; j++)

{

adjacency_matrx [ i ] [ j ] = scan . nextInt();

if (i = = j)

{

   adjacency_matrx [ i ] [ j ] = 0 ;

continue;

}

if ( adjacency_matrx [ i ] [ j ] == 0 )

{

    adjacency_matrx [ i ] [ j ] = Integer .MAX_VALUE ;

}

}

}

system.out.println ("enter the source ");

source = next.Int ();

DijkstraQueue dijakstrasQueue = new DijkstraQueue ( Numbe_of_vertices);

DijkstraQueue . dijakstra_algoritnm (adjacency_matrix , source );

system.out.println ("the sorted path to all nodes are");

for (int i =1 ; i <= dijkstraQueue . distances . length -1 ; i++ )

{

System.out.println (source + " to " + i + " is "+dijkstraQueue.distance[ i ]);

}

catch (inputMismatchException ip )

{  

system.out.println ("wrong input formate ");

}

scan.close ();

}

}

OUTPUT:-

enter the Number of vertices

5

enter the weighed matrix for the graph

0 7 0 0 2

0 0 1 0 2

0 0 0 4 0

0 0 5 0 0

0 3 8 5 0

enter the source

1

the shorted path to all nodes are

1 to 1 is 0

1 to 2 is 5

1 to 3 is 6

1 to 4 is 7

1 to 5 is 2