Considering the adjacency list representation using java which involves multiple classes in a package. PriorityQueue is used in this algorithm
Edge.java // represents edge with weight
package temp;
public class Edge implements Comparable{
private int startPoint;
private int endPoint;
private float weight;
public Edge(int startPoint, int
endPoint, float weight) {
this.startPoint =
startPoint;
this.endPoint =
endPoint;
this.weight =
weight;
}
public boolean equals(Edge other)
{
if (this.startPoint
== other.startPoint) {
if
(this.endPoint == other.endPoint) {
return
true;
}
}
return false;
}
public int compareTo(Object o) {
Edge other = (Edge)
o;
return
Double.compare(this.weight, other.weight);
}
}
Graph.java //executes operation of adding,removing edges
package temp;
import java.util.Iterator;
import java.util.PriorityQueue;
public class Graph {
private int vCount;
private PriorityQueue<Edge>[]
adj;
public int getvCount() {
return
vCount;
}
public Graph(int vCount) {
this.vCount =
vCount;
// initialize
adj
adj = new
PriorityQueue[vCount];
for (int i = 0; i
< vCount; i++) {
adj[i]
= new PriorityQueue<Edge>();
}
}
public void addEdge(int i, int j, float
weight) {
adj[i].add(new
Edge(i, j, weight));
}
public void addEdge(Edge e) {
adj[e.startPoint].add(e);
}
public void removeEdge(int i, int j)
{
Iterator<Edge>
it = adj[i].iterator();
Edge other = new
Edge(i, j, 0);
while
(it.hasNext()) {
if
(it.next().equals(other)) {
it.remove();
return;
}
}
}
public PriorityQueue<Edge>
neighbours(int vertex) {
return
adj[vertex];
}
public void printGraph() {
for (int i = 0; i
< vCount; i++) {
PriorityQueue<Edge>
edges = neighbours(i);
Iterator<Edge>
it = edges.iterator();
System.out.print(i
+ ": ");
for
(int j = 0; j < edges.size(); j++) {
System.out.print(it.next()
+ " ");
}
System.out.println();
}
}
}
Vertex.java
package temp;
public class Vertex implements Comparable{
private int id;
private float distance;
private Vertex parent;
public Vertex(){
distance =
Float.MAX_VALUE; // "infinity"
parent =
null;
}
public Vertex(int id){
this.id = id;
distance =
Float.MAX_VALUE; // "infinity"
parent =
null;
}
public float getDistance() {
return
distance;
}
public void setDistance(float distance)
{
this.distance =
distance;
}
public Vertex getParent() {
return
parent;
}
public void setParent(Vertex parent){
this.parent =
parent;
}
public int compareTo(Object o) {
Vertex other =
(Vertex) o;
return
Double.compare(this.distance, other.distance);
}
}
TestGraph.java
package temp;
import java.util.Iterator;
import java.util.PriorityQueue;
public class TestGraphs {
public static void main(String[] args)
{
Graph g = new
Graph(5);
System.out.println("Graph:");
// add Edges
g.addEdge(0, 1, 4);
g.addEdge(0, 7, 8);
g.addEdge(1, 2, 8);
g.addEdge(1, 7, 11);
g.addEdge(2, 3, 7);
g.addEdge(2, 8, 2);
g.addEdge(2, 5, 4);
g.addEdge(3, 4, 9);
g.addEdge(3, 5, 14);
g.addEdge(4, 5, 10);
g.addEdge(5, 6, 2);
g.addEdge(6, 7, 1);
g.addEdge(6, 8, 6);
g.addEdge(7, 8, 7);
// print
Graph
g.printGraph();
// Dijkstra
Shortest Path Algorithm
System.out.println("Dijkstra
Shortest Path:");
Dijkstra(g,
0);
}
public static void Dijkstra(Graph g, int
startVertex) {
// for storing
distances after removing vertex from Queue
float[] distances =
new float[g.getvCount()];
// for storing
father id's after removing vertex from Queue
int[] parents = new
int[g.getvCount()];
for (int i = 0; i
< g.getvCount(); i++) {
parents[i]
= -1;
}
// set up vertex
queue
PriorityQueue<Vertex>
Q = new PriorityQueue<Vertex>();
for (int i = 0; i
< g.getvCount(); i++) {
if
(i != startVertex) {
Q.add(new
Vertex(i));
}
}
// add
startVertex
Vertex node = new
Vertex(startVertex);
node.setDistance(0);
Q.add(node);
// loop through
all vertices
while
(!Q.isEmpty()) {
//
get vertex with shortest distance
Vertex
u = Q.remove();
distances[u.id]
= u.getDistance();
//
iterate through all neighbours
Iterator<Edge>
it = g.neighbours(u.id).iterator();
while
(it.hasNext()) {
Edge
e = it.next();
Iterator<Vertex>
it2 = Q.iterator();
while
(it2.hasNext()) {
Vertex
v = it2.next();
//
check if vertex was visited already
if
(e.endPoint != v.id) {
continue;
}
//
check distance
if
(v.getDistance() > u.getDistance() + e.weight) {
v.setDistance(u.getDistance()
+ e.weight);
v.setParent(u);
parents[v.id]
= v.getParent().id;
}
}
}
}
// print final
shortest paths
System.out.println("Vertex\tDistance");
for (int i = 0; i
< g.getvCount(); i++) {
System.out.println(i
+ "\t" + distances[i] );
}
}
}
Implement Dijkstra's algorithm to find the shortest path from vertex O to all other vertices in...
Use Dijkstra's algorithm to determine the shortest path from vertex a to every other vertex in the following graph. Draw your steps on your own draft paper using notation as described in class (you do not need to submit this), then clearly identify and list the following in the text field below: (1) Which edges are included in the SSP; in the format of (vertex1, vertex 2, weight), for example (a, b, 7),(a, c, 9), ... (2) The order and...
Dijkstra's single source shortest path algorithm when run from vertex a in the below graph, in what order do the nodes get included into the set of vertices for which the shortest path distances are finalized?
Consider the graph below. Use Dijkstra's algorithm to find the shortest path from vertex A to vertex F. Write your answer as a sequence of nodes separated by commas (no blank spaces) starting with the source node: _______ What's the weight of the shortest path? _______
Consider the graph below. Use Dijkstra's algorithm to find the shortest path from vertex A to vertex C. Write your answer as a sequence of nodes with no blank spaces or any separators in between, starting with the source node: What's the weight of the shortest path?
Question 6 Let G be the weighted graph (a) Use Dijkstra's algorithm to find the shortest path from A to F. You can do all the work on a single diagram, but, to show that you have used the algorithm correctly, if an annotation needs updating do not erase itjust put a line through it and write the new annotation above that b) In what order are the vertices added to the tree? (c) Notice that the algorithm does not,...
Question 5 (5 points) Apply Dijkstra's Algorithm to the following graph, computing the shortest path for al vertices from vertex A. Present the results after each vertex has been processed 3 20 B 47 20 You may wish to present the results in the format of the following table: Stage Current Vertex Labels and Distances A 0 A 0 D 231 A 213 E 4 F21 A 90 Each row states (a) the current stage, (b) the vertex just added...
Apply Dijkstra's algorithm to find the shortest distance from vertex 0 to every other vertex in the graph shown in Figure 1 below. You must show supporting. You need to list the paths and the minimum distances.
Algorithm Question 5. Below is a graph with edge lengths. Apply Dijkstra's algorithm to find the shortest paths, starting at vertex A, to all other vertices. Write down the sequence in which the edges are chosen, breaking ties by using vertices at the same length in alphabetic orde. 3 Ga 2 5. Below is a graph with edge lengths. Apply Dijkstra's algorithm to find the shortest paths, starting at vertex A, to all other vertices. Write down the sequence in...
Using the following graph and Dijkstra's algorithm, calculate the shortest distance to each other vertex starting from vertex A. Label all vertices with the total distance (from A). Indicate the order nodes are added to cloud. Draw a Minimum Spanning Tree for the graph. You should label all nodes in the tree, but you do not need to indicate edge weights or total distance. 2 D C L 7 6 2 7 2 A K B 4 7 4 1...
Run Dijkstra's algorithm on the graph G below, where s is the source vertex. Draw a table that shows the vertices in Q at each iteration. Write thed and I values of each vertex. Color the edges in the shortest-path tree, similar to the example from the notes. List the order in which vertices are added to S. Use the algorithm learned in class.