Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c++ graph This is what I need: 1. Make a function to get numOfTheShortestPaths function. (I just need this! I already have a function definition

c++ graph

This is what I need:

1. Make a function to get numOfTheShortestPaths function. (I just need this! I already have a function definition for thePathLen)

2. I already have a working code for thePathLen function, and got the vectors already. I just need to do the numOfTheShortestPaths part. This is basically asking me to get how many walks is possible from the source (which will always be 0) to the destination with a fixed path length.

3. I will be using linked based queue for storing the adjacency list.

THIS IS THE TESTED CODE (the bold are the code, and the italicswords are the information of what each function does):

TRY(GraphingTester, GraphingTesterA) { vector< vector > gA = { {1, 2, 4}, {0,3}, {0,3}, {1,2,5, 7}, {0, 5, 6}, {3, 4}, {4, 7}, {3, 6}, }; //adjacency list

vector pathLengths(8);

vector numShortestPaths(8); countingGraphPathWays(gA, 0,thePathLen, numOfTheShortestPaths);

/**

The function: countingGraphPathWays(const vector< vector > &adjVertex, unsigned source, vector & thePathLen, vector & numOfTheShortestPaths)

**/

//thePathLen = path lengths is basically the shortest length to reach from the source vertex to the destination vertex

vector expectedOfThePathLen = {0, 1, 1, 2, 1, 2, 2, 3}; //source vertex is 0 , and this is shortest length vector expectedOfNumberShortestPath = {1, 1, 1, 2, 1, 1, 1, 3}; //source vertex is 0, how many ways to get to shortest length

//I need to make a function to get expectedOfNumberShortestPath

EXPECT_TRUE(thePathLen ==expectedOfThePathLen && expectedOfNumberShortestPath ==numOfTheShortestPaths);

}

Here's a code I found:

void countingGraphPathWays(vector> &adj, int src, vector &thePathLen, vector &numOfTheShortestPaths ) { int n = adj.size(); bool visited[n]; for (int i = 0; i < n; i++) { visited[i] = false; thePathLen.push_back(INT_MAX); numOfTheShortestPaths .push_back(0); } thePathLen[src] = 0; numOfTheShortestPaths [src] = 1; queue q; q.push(src); visited[src] = true; while (!q.empty()) { int curr = q.front(); q.pop();

for (auto x : adj[curr]) { if (visited[x] == false) { q.push(x); visited[x] = true; } if (thePathLen[x] > thePathLen[curr] + 1) { thePathLen[x] = thePathLen[curr] + 1; numOfTheShortestPaths [x] = numOfTheShortestPaths [curr]; } else if (thePathLen[x] == thePathLen[curr] + 1) numOfTheShortestPaths [x] += numOfTheShortestPaths [curr]; } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions

Question

6. Discuss the steps involved in conducting a task analysis.

Answered: 1 week ago

Question

8. Explain competency models and the process used to develop them.

Answered: 1 week ago