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

implement in c++ The source for the graph can be found in the link below,https:/

ID: 3577793 • Letter: I

Question

implement in c++

The source for the graph can be found in the link below,https://dl.dropboxusercontent.com/u/4309261/site%20links/datasets/datasets%20backup/M10000_P_toro.graph

The graph contains 10000 vertices. Each vertex is given an x y z coordinate value. For example

VERTEX2 28 5.298951 3.084083 1.732966

This line from the file declares the 28th vertex and its respective x y and z coordinates.

The rest of the file contains the edges in the graph. We are only going to be concerned the out vertex and the in vertex. Which is the first two numbers for each edge. We will ignore the rest. For example.

EDGE2 3 4 1.038045 0.003036 0.011663 44.447240 -0.955703 371.210835 9770.758221 0.000000 0.000000

Simply states that there is an edge from vertex 3 to vertex 4. We will not be using the rest of the data in the file and will be weighting the edges with the distance between the two vertices. (Use the 3 dimensional distance function).

Produce a program with the following capabilities. The program will prompt for the start- ing vertex in the range of 0-9999. The program will then prompt for a different vertex in the range of 0-9999. The program will then execute a shortest path algorithm (Dijkstra’s is fine but you are welcome to use a different one). If a path does exist between the two vertices it will

1

output the sequence of vertices in the shortest path, as well as the total weight of the entire path between the two vertices.

here is sample code. and i want to use this code

Explanation / Answer

// A C / C++ program for Dijkstra's single source shortest path algorithm.
// The program is for adjacency matrix representation of the graph
  
#include <stdio.h>
#include <limits.h>
  
// Number of vertices in the graph
#define V 9
  
// A utility function to find the vertex with minimum distance value, from
// the set of vertices not yet included in shortest path tree
int minDistance(int dist[], bool sptSet[])
{
// Initialize min value
int min = INT_MAX, min_index;
  
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
int printSolution(int dist[], int n)
{
printf("Vertex Distance from Source ");
for (int i = 0; i < V; i++)
printf("%d %d ", i, dist[i]);
}
  
// Funtion that implements Dijkstra's single source shortest path algorithm
// for a graph represented using adjacency matrix representation
void dijkstra(int graph[V][V], int src)
{
int dist[V]; // The output array. dist[i] will hold the shortest
// distance from src to i
  
bool sptSet[V]; // sptSet[i] will true if vertex i is included in shortest
// path tree or shortest distance from src to i is finalized
  
// Initialize all distances as INFINITE and stpSet[] as false
for (int i = 0; i < V; i++)
dist[i] = INT_MAX, 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] && dist[u] != INT_MAX
&& dist[u]+graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
  
// print the constructed distance array
printSolution(dist, V);
}
  
// driver program to test above function
int main()
{
/* Let us create the example graph discussed above */
int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 14, 10, 0, 2, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};
  
dijkstra(graph, 0);
  
return 0;
}

import java.util.PriorityQueue;

import java.util.List;

import java.util.ArrayList;

import java.util.Collections;

class Vertex implements Comparable<Vertex>

{

    public final String name;

    public Edge[] adjacencies;

    public double minDistance = Double.POSITIVE_INFINITY;

    public Vertex previous;

    public Vertex(String argName) { name = argName; }

    public String toString() { return name; }

    public int compareTo(Vertex other)

    {

        return Double.compare(minDistance, other.minDistance);

    }

}

class Edge

{

    public final Vertex target;

    public final double weight;

    public Edge(Vertex argTarget, double argWeight)

    { target = argTarget; weight = argWeight; }

}

public class Dijkstra

{

    public static void computePaths(Vertex source)

    {

        source.minDistance = 0.;

        PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();

    vertexQueue.add(source);

    while (!vertexQueue.isEmpty()) {

        Vertex u = vertexQueue.poll();

            // Visit each edge exiting u

            for (Edge e : u.adjacencies)

            {

                Vertex v = e.target;

                double weight = e.weight;

                double distanceThroughU = u.minDistance + weight;

        if (distanceThroughU < v.minDistance) {

            vertexQueue.remove(v);

            v.minDistance = distanceThroughU ;

            v.previous = u;

            vertexQueue.add(v);

        }

            }

        }

    }

    public static List<Vertex> getShortestPathTo(Vertex target)

    {

        List<Vertex> path = new ArrayList<Vertex>();

        for (Vertex vertex = target; vertex != null; vertex = vertex.previous)

            path.add(vertex);

        Collections.reverse(path);

        return path;

    }

    public static void main(String[] args)

    {

        // mark all the vertices

        Vertex A = new Vertex("A");

        Vertex B = new Vertex("B");

        Vertex D = new Vertex("D");

        Vertex F = new Vertex("F");

        Vertex K = new Vertex("K");

        Vertex J = new Vertex("J");

        Vertex M = new Vertex("M");

        Vertex O = new Vertex("O");

        Vertex P = new Vertex("P");

        Vertex R = new Vertex("R");

        Vertex Z = new Vertex("Z");

        // set the edges and weight

        A.adjacencies = new Edge[]{ new Edge(M, 8) };

        B.adjacencies = new Edge[]{ new Edge(D, 11) };

        D.adjacencies = new Edge[]{ new Edge(B, 11) };

        F.adjacencies = new Edge[]{ new Edge(K, 23) };

        K.adjacencies = new Edge[]{ new Edge(O, 40) };

        J.adjacencies = new Edge[]{ new Edge(K, 25) };

        M.adjacencies = new Edge[]{ new Edge(R, 8) };

        O.adjacencies = new Edge[]{ new Edge(K, 40) };

        P.adjacencies = new Edge[]{ new Edge(Z, 18) };

        R.adjacencies = new Edge[]{ new Edge(P, 15) };

        Z.adjacencies = new Edge[]{ new Edge(P, 18) };

        computePaths(A); // run Dijkstra

        System.out.println("Distance to " + Z + ": " + Z.minDistance);

        List<Vertex> path = getShortestPathTo(Z);

        System.out.println("Path: " + path);

    }

}