Question

In: Computer Science

One simple representation for a graph is to use an adjacency matrix: each of the N...

One simple representation for a graph is to use an adjacency matrix: each of the N nodes is given a unique number in the range 0 to N-1 to identify it. A large two dimensional array A with N rows and N columns is created so that A[x][y] stores the cost of travelling directly from node x to node y. if A[x][y] is zero, then there is no direct connection from x to y. A[x][y] does not need to equal A[y][x] - there can be one-way links. Assume that a priority queue exactly as you described for part A has been implemented, and you can use it. Also assume that an adjacency matrix as just described has been created for an N node graph, and you can use it too. Write a function that finds the length of the shortest path (or the cost of the cheapest path) between nodes S and D, which are provided as parameters.

must be in C++ code

Solutions

Expert Solution

GIVEN:

write a C++ code to find the length of the shortest path ( or the cost of the cheapest path ) between nodes S and D, which are provided as parameters. Adjacency matrix has been created for N node graph. A large two dimentional array A with N rows and N columns.

A[x][y] stores the cost of travelling directly from node x to node y.

// Adjacency matrix representation

#include<iostream>

#include<stdio.h>

using namespace std;

#define max 6

void dijkstra ( int G[max,max], int d, int s );

int main ( ) {

int d = 6;

int s = 0;

dijkstra (G,d,s);

return 0;

}

void dijkstra(G[max][max], int d, int s)  

{

int A[x][y], distance [max], pred[max];

int visited [max], count, mdistance, nextnode, i, j;

for( i=0;i<n;i++)

for( j=0;j<n;j++)

if (G[ i ][ i ]==0)

A[ i ][ j ] =INFINITY;

else

A[ i ][ j ] = G[ i ][ j ];

for ( i=0;i<n;i++) {

distance [ i ] =cost [s][ i ];

pred[ i ] =s ;

visited [ i ] =0;

}

distance [ s ] =0;

visited [ s ]=1;

count =1;

while ( count<n-1 ) {

mdistance = INFINITY;

for ( i=0;i<n;i++)

if (distance[ i ] < mdistance&&! visited[ i ] )

{

mdistance = distance [ i ]

nextnode = i;

}

visited [nextnode ] =1;

for( i=0;i<n;i++)

if ( !visited [ i ] )

if ( mdistance + cost [nextnode ][ i ];

pred[ i ] = nextnode;

}

count ++;

}

for ( i=0;i<n;i++)

if ( i!= s ) {

cout<< " \n distance of node " << i << "= " << distance [ i ];

cout << "\n path ="<< i;

j=i;

do {

j= pred [ j ];

cout<< "< - " << j;

}

while ( j! = s);

}

}


Related Solutions

Answer True or False 1. For graph representation, adjacency Matrix is more efficiency than adjacency list...
Answer True or False 1. For graph representation, adjacency Matrix is more efficiency than adjacency list in term of searching for edge. 2. Topological sort runs in O(|V| + |E|) where |V| is the number of vertices, and |E| is the number of edges in the input graph. 3. If vertex u can reach vertex v, and vertex v can reach vertex u, then vertices u and v are in the same Strongly-connected component (SCC). 4. The Bellman-Ford algorithm will...
Use the Table button in the Rich-Text Editor to provide an adjacency matrix for a simple...
Use the Table button in the Rich-Text Editor to provide an adjacency matrix for a simple graph that meets the following requirements: 1) has 5 vertices 2) is maximal planar
maximumST is a function that should take an n × n vector (representing the adjacency matrix...
maximumST is a function that should take an n × n vector (representing the adjacency matrix of a graph) as input and return the value of the maximum spanning tree of the graph defined by the n × n vector. Code: #include <vector> #include <cmath> #include <iostream> #include <cstdio> using namespace std; double maximumST( vector< vector<double> > adjacencyMatrix ){   vector<int> row(4,-1);   vector< vector<int> > matrix(5,row);   cerr << matrix.size() << endl;   cerr << matrix[0].size() << endl;   for( int i = 0;...
Python3 question. Suppose that a graph represent as adjacency matrix, how could it perform DFS by...
Python3 question. Suppose that a graph represent as adjacency matrix, how could it perform DFS by using stack. Graph is a undirect and unweighted graph.
Prove: An (n × n) matrix A is not invertible ⇐⇒ one of the eigenvalues of...
Prove: An (n × n) matrix A is not invertible ⇐⇒ one of the eigenvalues of A is λ = 0
(a) What is the maximum degree of a vertex in a simple graph with n vertices?...
(a) What is the maximum degree of a vertex in a simple graph with n vertices? (b) What is the maximum number of edges in a simple graph of n vertices? (c) Given a natural number n, does there exist a simple graph with n vertices and the maximum number of edges?
Let G be a simple undirected graph with n vertices where n is an even number....
Let G be a simple undirected graph with n vertices where n is an even number. Prove that G contains a triangle if it has at least (n^2 / 4) + 1 edges using mathematical induction.
Java Implementation It uses adjacency list representation, and already has the loadAdjList() function implemented for reading...
Java Implementation It uses adjacency list representation, and already has the loadAdjList() function implemented for reading adjacency lists from standard input (make sure you understand the input format and how loadAdjList() works). You need to complete the function printAdjMatrix(). import java.util.LinkedList; import java.util.Scanner; import java.util.Iterator; class Graph { private int totalVertex; private LinkedList<LinkedList<Integer>> adjList; //adjacency list of edges public Graph() { totalVertex = 0; } public void loadAdjList() { Scanner in = new Scanner(System.in); totalVertex = in.nextInt(); adjList = new...
Given the following adjacency matrix, A, for nodes a, b, c, and d, find the transitive...
Given the following adjacency matrix, A, for nodes a, b, c, and d, find the transitive closure of A. Is the result an equivalence relation, and why or why not? A = ⌈ 1 0 1 0 ⌉ | 0 1 1 0 | | 1 0 0 1 | ⌊ 1 1 0 0 ⌋
Express the degree of a vertex in terms of the adjacency matrix Describe how you can...
Express the degree of a vertex in terms of the adjacency matrix Describe how you can find out whether a graph is connected, based on its adjacency matrix, using only matrix addition and multiplication.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT