Question

ADJACENCY LIST ( use Breadth-First Search algorithm) How to get the total number of paths of...

ADJACENCY LIST ( use Breadth-First Search algorithm)

How to get the total number of paths of fixed length from vector of vector adjacency LIST. REMEMBER: the fixed length has to be the shortest length.

So, const std::vector< std::vector<unsigned> > & adjList;

and I'm supposed to get the number of paths for a given shortest path from the adjList.

What I want:

1. To define the function countingGraphPathWays(const vector< vector > &adjVertex, unsigned source, vector & thPathLen, vector & numOfTheShortestPaths)

2. For the TESTED CODE to run (see attached code below) (this tested code help defining the function countingGraphPathWays)

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 & thPathLen, 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

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

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

By using Breadth-First Search algorithm

void countingGraphPathWays(vector<vector<int>> &adj, int src, vector<int> &thePathLen, vector<int> &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 <int> 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];
}
}  
}

Add a comment
Know the answer?
Add Answer to:
ADJACENCY LIST ( use Breadth-First Search algorithm) How to get the total number of paths of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You will be implementing a Breadth-First Search (BFS) and a Depth-First Search (DFS) algorithm on a...

    You will be implementing a Breadth-First Search (BFS) and a Depth-First Search (DFS) algorithm on a graph stored as an adjacency list. The AdjacencyList class inherits from the Graph class shown below. class Graph { private: vector _distances; vector _previous; public: Graph() { } virtual int vertices() const = 0; virtual int edges() const = 0; virtual int distance(int) const = 0; virtual void bfs(int) const = 0; virtual void dfs(int) const = 0; virtual void display() const = 0;...

  • /* Graph read from file, and represnted as adjacency list. To implement DFS and BFS on...

    /* Graph read from file, and represnted as adjacency list. To implement DFS and BFS on the graph */ #include <iostream> #include <sstream> #include <fstream> #include <vector> #include <utility> #include <unordered_map> #include <set> #include <queue> using namespace std; // Each vertex has an integer id. typedef vector<vector<pair<int,int>>> adjlist; // Pair: (head vertex, edge weight) adjlist makeGraph(ifstream& ifs); void printGraph(const adjlist& alist); vector<int> BFS(const adjlist& alist, int source); // Return vertices in BFS order vector<int> DFS(const adjlist& alist, int source); //...

  • code Dijkstra's Algorithm for a directed graph example graph.txt: 0 (1,3) (3,5) 1 (2,6) 2 (4,2)...

    code Dijkstra's Algorithm for a directed graph example graph.txt: 0 (1,3) (3,5) 1 (2,6) 2 (4,2) 3 (1,1) (2,4) (4,6) 4 (0,3) (2,7) example dist.txt: 0 1 2 3 4 0 8 9 5 7 using a priority queue implemented as a heap. Input is from a file "graph.txt" which contains adjacency lists. Format of the file will be discussed in class. Source vertex is the first vertex in the list. Output is to the file "dist.txt" which contains the...

  • PYTHON ONLY Implement the Dijkstra’s Shortest path algorithm in Python. A graph with 10 nodes (Node...

    PYTHON ONLY Implement the Dijkstra’s Shortest path algorithm in Python. A graph with 10 nodes (Node 0 to node 9) must be implemented. You are supposed to denote the distance of the edges via an adjacency matrix (You can assume the edge weights are either 0 or a positive value). The adjacency matrix is supposed to be a 2-D array and it is to be inputted to the graph. Remember that the adjacency list denotes the edge values for the...

  • Design and implement Dijkstra’s algorithm to compute all-pair shortest paths in any given graph using An...

    Design and implement Dijkstra’s algorithm to compute all-pair shortest paths in any given graph using An adjacency matrix using a one-dimensional array for storing only the elements of the lower triangle in the adjacency matrix.[Program in C language] The input to program must be connected, undirected, and weighted graphs. The programs must be able to find shortest paths on two types of connected, undirected, and weighted graphs: complete graph (a graph with a link between every pair of nodes) and...

  • Hi there. I tried to submit this before, but the copy/paste didn't copy my vectors correctly....

    Hi there. I tried to submit this before, but the copy/paste didn't copy my vectors correctly. Hello there. I am trying to implement the djikstras shortest path algorithm into my code in C++ with a vector of vectors. However, when I test I get an error "Exception thrown at 0x75FFC762 in graphMain.exe: Microsoft C++ exception: std::out_of_range at memory location 0x009CF26C" for line 115 in graph.cpp. Could you help me debug? I am so close! ----------------------------FILE NAME: pch.h--------------------------------------- // pch.cpp: source...

  • from collections import defaultdict    # This class represents a directed graph using # adjacency list...

    from collections import defaultdict    # This class represents a directed graph using # adjacency list representation class Graph:        # Constructor     def __init__(self):            # default dictionary to store graph         self.graph = defaultdict(list)        # function to add an edge to graph     def addEdge(self,u,v):         self.graph[u].append(v)        # Function to print a BFS of graph     def BFS(self, s):            # Mark all the vertices as not visited         visited = [False] * (len(self.graph))            # Create a queue for BFS         queue...

  • (5 marks) a. The pseudo-code for breadth-first search, modified slightly from Drozdek,1 is as follows: void...

    (5 marks) a. The pseudo-code for breadth-first search, modified slightly from Drozdek,1 is as follows: void breadthFirstSearch (vertex w) for all vertices u num (u) 0 null edges i=1; num (w) i++ enqueue (w) while queue is not empty dequeue ( V= for all vertices u adjacent to v if num(u) is 0 num (u) = i++; enqueue (u) attach edge (vu) to edges; output edges; Now consider the following graph. Give the breadth-first traversal of the graph, starting from...

  • [INPUT] First line number of vertex v adjacency matrix of graph Second v+1 line (If not connected, 1 If connected, weig...

    [INPUT] First line number of vertex v adjacency matrix of graph Second v+1 line (If not connected, 1 If connected, weight on the edge) [OUTPUT First line For completed MST Second line Completed MST cost as a result of running dfs(0) [EXAMPLE] input,txt 7 -1 28 -1 -1 -1 10 -1 28 -1 16 -1 -1 -1 14 -1 16 -1 12 -1 -1 -1 -1 -1 12 -1 22 -1 18 -1 -1 -1 22 -1 25 24 10...

  • Programming Traversal Methods in C++ (depth first & breadth first) Need solution ASAP any help is...

    Programming Traversal Methods in C++ (depth first & breadth first) Need solution ASAP any help is much appreciated. read a set of data representing a directed, unweighted graph build an in-memory graph structure using the data display the graph using depth-first traversal display the graph using breadth-first traversal Input data - The data consists of records like this: 16 3 15 4 -1 This represents a vertex 16 with neighbors 3, 15, and 4. The -1 is the indicator that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT